mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/shipping-portal.git
synced 2026-08-19 12:34:18 +00:00
85 lines
2.5 KiB
PHP
85 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Transformers;
|
|
|
|
use App\Classes\ValueObjects\Constants\ApprovalStatus;
|
|
use App\Models\Transport;
|
|
use Illuminate\Support\Carbon;
|
|
use League\Fractal\Resource\Collection;
|
|
use League\Fractal\Resource\Item;
|
|
use League\Fractal\TransformerAbstract;
|
|
|
|
class TransportTransformer extends TransformerAbstract
|
|
{
|
|
|
|
/**
|
|
* List of resources to automatically include
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $defaultIncludes = [
|
|
'current_schedule',
|
|
'schedule_history'
|
|
];
|
|
|
|
/**
|
|
* List of resources possible to include
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $availableIncludes = [
|
|
'packing_lists',
|
|
'receive_packing_list'
|
|
];
|
|
|
|
/**
|
|
* A Fractal transformer.
|
|
*
|
|
* @param Transport $transport
|
|
* @return array
|
|
*/
|
|
public function transform(Transport $transport): array
|
|
{
|
|
return [
|
|
'id' => $transport->id,
|
|
'type' => $transport->type,
|
|
'courier' => $transport->courier,
|
|
'tracking_number' => $transport->tracking_number,
|
|
'dispatch_date' => $transport->dispatch_date ? $transport->dispatch_date->format('d-m-Y') : $transport->dispatch_date,
|
|
'drop_date' => $transport->drop_date ? $transport->drop_date->format('d-m-Y') : $transport->drop_date,
|
|
'status' => $transport->status,
|
|
'dropped_days' => Carbon::now()->diffInDays($transport->drop_date, false),
|
|
'schedule_complete' => $this->isScheduleComplete($transport)
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Include Current Schedule
|
|
* @param Transport $transport
|
|
* @return Item
|
|
*/
|
|
public function includeCurrentSchedule(Transport $transport): Item
|
|
{
|
|
$currentSchedule = $transport->schedules()->where('status', '=', ApprovalStatus::APPROVED)->first();
|
|
return $this->item($currentSchedule, new ScheduleTransformer);
|
|
}
|
|
|
|
/**
|
|
* include Schedule History
|
|
* @param Transport $transport
|
|
* @return Collection
|
|
*/
|
|
public function includeScheduleHistory(Transport $transport): Collection
|
|
{
|
|
$history = $transport->schedules()->where('status', '=', ApprovalStatus::EXPIRED)->get();
|
|
return $this->collection($history, new ScheduleTransformer);
|
|
}
|
|
|
|
public function isScheduleComplete(Transport $transport): bool
|
|
{
|
|
$currentSchedule = $transport->schedules()->where('status', '=', ApprovalStatus::APPROVED)->first();
|
|
return $currentSchedule && $currentSchedule->eta <= Carbon::now();
|
|
}
|
|
|
|
|
|
} |