mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/shipping-portal.git
synced 2026-08-19 04:24:12 +00:00
62 lines
3.5 KiB
PHP
62 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Resources;
|
|
|
|
use App\Classes\ValueObjects\Constants\ApprovalStatus;
|
|
use App\Classes\ValueObjects\Constants\OrderRoleTypes;
|
|
use App\Classes\ValueObjects\Constants\PackingListType;
|
|
use App\Classes\ValueObjects\Constants\TransportType;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Resources\Json\JsonResource;
|
|
|
|
class OrderResource extends JsonResource
|
|
{
|
|
/**
|
|
* Transform the resource into an array.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return array
|
|
*/
|
|
public function toArray($request)
|
|
{
|
|
return [
|
|
'id' => $this->id,
|
|
'reference' => $this->reference,
|
|
'reference_contract' => (int) $this->type,
|
|
'type' => (int) $this->type,
|
|
'status' => (int) $this->status,
|
|
'company_module' => new CompanyModuleResource($this->companyModule),
|
|
'warehouse' => new CompanyModuleResource($this->orderRoles()->where('role_id', '=', OrderRoleTypes::ORIGIN_WAREHOUSE)->first()->appointee),
|
|
'address' => new AddressResource($this->addresses()->where('status', '=', ApprovalStatus::APPROVED)->first()),
|
|
'address_change_request' => new AddressResource($this->addressesPendingVerification()->first()),
|
|
'parcels' => $this->whenLoaded('packingLists', function(){
|
|
return [
|
|
'origin_warehouse_packages' => PackageResource::collection($this->parcels()->whereDoesntHave('shippingSchedules', function($schedule){
|
|
return $schedule->where('etd', '<', Carbon::now())->where('schedules.status', ApprovalStatus::APPROVED);
|
|
})->get()),
|
|
'in_transit_packages' => PackageResource::collection($this->parcels()->whereHas('shippingSchedules', function($schedule){
|
|
return $schedule->where('etd', '<', Carbon::now())->where('schedules.status', ApprovalStatus::APPROVED);
|
|
})->whereDoesntHave('shippingTransports', function($transport){
|
|
return $transport->where('transport_arrangements.status', ApprovalStatus::COMPLETED);
|
|
})->get()),
|
|
'destination_warehouse_packages' => PackageResource::collection($this->parcels()->whereHas('shippingTransports', function($transport){
|
|
return $transport->where('transport_arrangements.status', ApprovalStatus::COMPLETED);
|
|
})->whereDoesntHave('deliverySchedules', function($schedule){
|
|
return $schedule->where('etd', '<', Carbon::now())->where('schedules.status', ApprovalStatus::APPROVED);
|
|
})->get()),
|
|
'delivered_packages' => PackageResource::collection($this->parcels()->whereHas('shippingSchedules', function($schedule){
|
|
return $schedule->where('eta', '<', Carbon::now())->where('schedules.status', ApprovalStatus::APPROVED);
|
|
})->whereHas('deliverySchedules', function($schedule){
|
|
return $schedule->where('etd', '<', Carbon::now())->where('schedules.status', ApprovalStatus::APPROVED);
|
|
})->get()),
|
|
'received_packages' => PackageResource::collection($this->parcels()->whereHas('packingList', function($query){
|
|
return $query->where('type', PackingListType::WAREHOUSE_RECEIVE_LIST);
|
|
})->get()),
|
|
];
|
|
}),
|
|
'created_at' => $this->created_at->format('d-m-Y')
|
|
];
|
|
|
|
}
|
|
}
|