diff --git a/app/Classes/Modules/Orders/ControllersLogic/FetchOrderLogic.php b/app/Classes/Modules/Orders/ControllersLogic/FetchOrderLogic.php index c58b14a5..d8b1180d 100644 --- a/app/Classes/Modules/Orders/ControllersLogic/FetchOrderLogic.php +++ b/app/Classes/Modules/Orders/ControllersLogic/FetchOrderLogic.php @@ -52,7 +52,7 @@ class FetchOrderLogic extends AbstractControllerLogic $this->canFetchOrder->passes(); - $query = $this->fetchesOrder->execute(['reference' => $request->route('id'), 'with_parcels' => true]); + $query = $this->fetchesOrder->execute(['reference' => $request->route('id'), 'with_packing_lists' => true]); return $this->resourceResponse(new OrderResource($query)); diff --git a/app/Http/Resources/OrderResource.php b/app/Http/Resources/OrderResource.php index e3a66981..0fd3a657 100644 --- a/app/Http/Resources/OrderResource.php +++ b/app/Http/Resources/OrderResource.php @@ -19,71 +19,43 @@ class OrderResource extends JsonResource */ public function toArray($request) { - try { - 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()), - $this->mergeWhen($this->relationLoaded('parcels'), [ - 'origin_warehouse_packages' => PackageResource::collection($this->parcels()->whereHas('packingList', function($query){ - return $query->where('type', PackingListType::SHIPPING_PACKING_LIST)->whereDoesntHave('containers', function($query) { - return $query->whereHas('transports', function($query){ - return $query->whereHas('schedules', function($query){ - return $query->where('etd', '<', Carbon::now()); - }); - }); - }); + 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('packingList', function($query){ - return $query->where('type', PackingListType::SHIPPING_PACKING_LIST)->whereHas('containers', function($query) { - return $query->whereHas('transports', function($query){ - return $query->where('status', '=', ApprovalStatus::APPROVED)->whereHas('schedules', function($query){ - return $query->where('etd', '<', Carbon::now()); - }); - }); - }); + '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('packingList', function($query){ - return $query->where('type', PackingListType::SHIPPING_PACKING_LIST)->whereHas('containers', function($query) { - return $query->whereHas('transports', function($query){ - return $query->where('type', '=', TransportType::SEA)->where('status', '=', ApprovalStatus::COMPLETED)->whereHas('schedules', function($query){ - return $query->where('etd', '<', Carbon::now()); - }); - }); - })->whereDoesntHave('transports', function($query){ - return $query->where('type', '=', TransportType::LAND)->where('status', '=', ApprovalStatus::APPROVED)->whereHas('schedules', function($query){ - return $query->where('etd', '<', Carbon::now()); - }); - }); + '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('packingList', function($query){ - return $query->where('type', PackingListType::SHIPPING_PACKING_LIST)->whereHas('containers', function($query) { - return $query->whereHas('transports', function($query){ - return $query->where('type', '=', TransportType::SEA)->where('status', '=', ApprovalStatus::COMPLETED)->whereHas('schedules', function($query){ - return $query->where('etd', '<', Carbon::now()); - }); - }); - })->whereHas('transports', function($query){ - return $query->where('type', '=', TransportType::LAND)->where('status', '=', ApprovalStatus::APPROVED)->whereHas('schedules', function($query){ - return $query->where('etd', '<', Carbon::now()); - }); - }); + '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()), - 'parcels' => PackageResource::collection($this->parcels()->whereHas('packingList', function($query){ + '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') - ]; - } catch (\Exception $exception){ - $this->id; - } + ]; + }), + 'created_at' => $this->created_at->format('d-m-Y') + ]; } } diff --git a/app/Http/Resources/PackageResource.php b/app/Http/Resources/PackageResource.php index d69ae652..75f5589f 100644 --- a/app/Http/Resources/PackageResource.php +++ b/app/Http/Resources/PackageResource.php @@ -26,8 +26,8 @@ class PackageResource extends JsonResource 'cbm' => (($this->width / 100) * ($this->height / 100) * ($this->length / 100)) * $this->quantity, 'status' => $this->status, 'order' => new OrderResource($this->packingList->owner), - 'container' => new ContainerResource($this->packingList->containers()->first()), - 'transport' => new TransportResource($this->packingList->transports()->first()) + 'container' => new ContainerResource($this->container), + 'transport' => new TransportResource($this->transport) ]; } } diff --git a/app/Models/Container.php b/app/Models/Container.php index e9acf41e..a103790b 100644 --- a/app/Models/Container.php +++ b/app/Models/Container.php @@ -4,11 +4,14 @@ namespace App\Models; use App\Classes\General\Interfaces\Transportable; use Illuminate\Database\Eloquent\Relations\BelongsToMany; +use Illuminate\Database\Eloquent\Relations\HasManyThrough; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\Relations\MorphMany; +use Staudenmeir\EloquentHasManyDeep\HasRelationships; class Container extends AbstractModel implements Transportable { + use HasRelationships; use SoftDeletes; protected $table = 'containers'; @@ -22,6 +25,22 @@ class Container extends AbstractModel implements Transportable return $this->belongsToMany(PackingList::class, ContainerPackingList::class, 'container_id', 'packing_list_id'); } + /** + * @return HasManyThrough + */ + public function packages(): hasManyThrough + { + return $this->hasManyDeep(Package::class, [ContainerPackingList::class, PackingList::class], ['container_id', 'id', 'packing_list_id'], ['id', 'packing_list_id', 'id']); + } + + /** + * @return HasManyThrough + */ + public function companyModules(): hasManyThrough + { + return $this->hasManyDeep(CompanyModule::class, [ContainerPackingList::class, PackingList::class, Order::class], ['container_id', 'id', 'company_module'], ['id', 'packing_list_id', 'id']); + } + /** * @return MorphMany */ diff --git a/app/Models/Package.php b/app/Models/Package.php index 2c987764..2c6f695f 100644 --- a/app/Models/Package.php +++ b/app/Models/Package.php @@ -3,12 +3,16 @@ namespace App\Models; use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Relations\HasManyThrough; use Illuminate\Database\Eloquent\Relations\HasOneThrough; use Illuminate\Database\Eloquent\Relations\MorphTo; use Illuminate\Database\Eloquent\SoftDeletes; +use Staudenmeir\EloquentHasManyDeep\HasOneDeep; +use Staudenmeir\EloquentHasManyDeep\HasRelationships; class Package extends AbstractModel { + use HasRelationships; use SoftDeletes; protected $table = 'packages'; @@ -21,4 +25,52 @@ class Package extends AbstractModel return $this->belongsTo(PackingList::class, 'packing_list_id'); } + /** + * @return HasManyThrough + */ + public function shippingTransports(): hasManyThrough + { + return $this->hasManyDeep(Transport::class, [PackingList::class, ContainerPackingList::class, Container::class], ['id', 'packing_list_id', 'id', ['owner_type', 'owner_id']], ['packing_list_id', 'id', 'container_id', null]); + } + + /** + * @return HasManyThrough + */ + public function shippingSchedules(): hasManyThrough + { + return $this->hasManyDeep(Schedule::class, [PackingList::class, ContainerPackingList::class, Container::class, Transport::class], ['id', 'packing_list_id', 'id', ['owner_type', 'owner_id'], ['owner_type', 'owner_id']], ['packing_list_id', 'id', 'container_id', null, null]); + } + + /** + * @return HasManyThrough + */ + public function deliveryTransports(): hasManyThrough + { + return $this->hasManyDeep(Transport::class, [PackingList::class], ['id', ['owner_type', 'owner_id']], ['packing_list_id', null]); + } + + /** + * @return HasManyThrough + */ + public function deliverySchedules(): hasManyThrough + { + return $this->hasManyDeep(Schedule::class, [PackingList::class, Transport::class], ['id', ['owner_type', 'owner_id'], ['owner_type', 'owner_id']], ['packing_list_id', null, null]); + } + + /** + * @return hasOneDeep + */ + public function container(): hasOneDeep + { + return $this->hasOneDeep(Container::class, [PackingList::class, ContainerPackingList::class], ['id', 'packing_list_id', 'id'], ['packing_list_id', 'id', 'container_id']); + } + + /** + * @return hasOneDeep + */ + public function transport(): hasOneDeep + { + return $this->hasOneDeep(Transport::class, [PackingList::class], ['id', ['owner_type', 'owner_id']], ['packing_list_id', null]); + } + } diff --git a/composer.json b/composer.json index 56ab4aac..af20f32d 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,7 @@ "require": { "php": "^7.3", "ext-fileinfo": "^7.3", - "ext-json": "^1.7", + "ext-json": "*", "barryvdh/laravel-dompdf": "^0.9.0", "carlos-meneses/laravel-mpdf": "^2.1", "doctrine/dbal": "^3.1", @@ -21,6 +21,7 @@ "rinvex/countries": "^6.1", "spatie/laravel-activitylog": "^3.14", "spatie/laravel-permission": "^4.2", + "staudenmeir/eloquent-has-many-deep": "^1.7", "tymon/jwt-auth": "^1.0" }, "require-dev": { diff --git a/resources/assets/vue/components/orders/elements/OrderComponent.vue b/resources/assets/vue/components/orders/elements/OrderComponent.vue index 2262d768..31cc56af 100644 --- a/resources/assets/vue/components/orders/elements/OrderComponent.vue +++ b/resources/assets/vue/components/orders/elements/OrderComponent.vue @@ -51,15 +51,15 @@



