diff --git a/app/Http/Controllers/Reports/MonthlyReportController.php b/app/Http/Controllers/Reports/MonthlyReportController.php index c00fc588..780302d8 100644 --- a/app/Http/Controllers/Reports/MonthlyReportController.php +++ b/app/Http/Controllers/Reports/MonthlyReportController.php @@ -61,26 +61,22 @@ class MonthlyReportController return (new ApiResponseObject('fetch service report Successful', '', HttpStatus::OK_WITH_MESSAGE, ['data' => [ - 'received' => Package::whereHas('packingList', function($query){ - return $query->where('type', PackingListType::WAREHOUSE_RECEIVE_LIST); + 'received' => Package::warehouseReceiveList()->sum('quantity'), + 'pending_shipment' => Package::shippingList()->whereDoesntHave('shippingSchedules', function($schedule){ + return $schedule->dispatched()->whereIn('schedules.status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]); })->sum('quantity'), - 'pending_shipment' => Package::whereDoesntHave('shippingSchedules', function($schedule){ - return $schedule->where('etd', '<', Carbon::now())->where('schedules.status', ApprovalStatus::APPROVED); + 'in_transit' => Package::shippingList()->Shipping()->whereHas('shippingSchedules', function($schedule){ + return $schedule->dispatched()->whereIn('schedules.status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]); + })->whereDoesntHave('shippingSchedules', function($schedule){ + return $schedule->dropped()->whereIn('schedules.status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]); })->sum('quantity'), - 'in_transit' => Package::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); - })->sum('quantity'), - 'pending_delivery' => Package::whereHas('shippingTransports', function($transport){ - return $transport->where('transport_arrangements.status', ApprovalStatus::COMPLETED); + 'pending_delivery' => Package::shippingList()->whereHas('shippingSchedules', function($schedule){ + return $schedule->dropped()->whereIn('schedules.status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]); })->whereDoesntHave('deliverySchedules', function($schedule){ - return $schedule->where('etd', '<', Carbon::now())->where('schedules.status', ApprovalStatus::APPROVED); + return $schedule->dispatched()->whereIn('schedules.status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]); })->sum('quantity'), - 'delivered' => Package::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); + 'delivered' => Package::shippingList()->whereHas('deliverySchedules', function($schedule){ + return $schedule->dispatched()->whereIn('schedules.status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]); })->sum('quantity'), ]]))->handler(); } diff --git a/app/Http/Resources/OrderResource.php b/app/Http/Resources/OrderResource.php index 0fd3a657..b722e2a7 100644 --- a/app/Http/Resources/OrderResource.php +++ b/app/Http/Resources/OrderResource.php @@ -31,27 +31,23 @@ class OrderResource extends JsonResource '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); + 'origin_warehouse_packages' => PackageResource::collection($this->parcels()->shippingList()->whereDoesntHave('shippingSchedules', function($schedule){ + return $schedule->dispatched()->whereIn('schedules.status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]); })->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); + 'in_transit_packages' => PackageResource::collection($this->parcels()->shippingList()->Shipping()->whereHas('shippingSchedules', function($schedule){ + return $schedule->dispatched()->whereIn('schedules.status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]); + })->whereDoesntHave('shippingSchedules', function($schedule){ + return $schedule->dropped()->whereIn('schedules.status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]); })->get()), - 'destination_warehouse_packages' => PackageResource::collection($this->parcels()->whereHas('shippingTransports', function($transport){ - return $transport->where('transport_arrangements.status', ApprovalStatus::COMPLETED); + 'destination_warehouse_packages' => PackageResource::collection($this->parcels()->shippingList()->whereHas('shippingSchedules', function($schedule){ + return $schedule->dropped()->whereIn('schedules.status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]); })->whereDoesntHave('deliverySchedules', function($schedule){ - return $schedule->where('etd', '<', Carbon::now())->where('schedules.status', ApprovalStatus::APPROVED); + return $schedule->dispatched()->whereIn('schedules.status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]); })->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); + 'delivered_packages' => PackageResource::collection($this->parcels()->shippingList()->whereHas('deliverySchedules', function($schedule){ + return $schedule->dispatched()->whereIn('schedules.status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]); })->get()), + 'received_packages' => PackageResource::collection($this->parcels()->warehouseReceiveList()->get()), ]; }), 'created_at' => $this->created_at->format('d-m-Y') diff --git a/app/Models/Package.php b/app/Models/Package.php index 2c6f695f..b010ea34 100644 --- a/app/Models/Package.php +++ b/app/Models/Package.php @@ -2,10 +2,11 @@ namespace App\Models; +use App\Classes\ValueObjects\Constants\ApprovalStatus; +use App\Classes\ValueObjects\Constants\PackingListType; +use Carbon\Carbon; 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; @@ -25,6 +26,7 @@ class Package extends AbstractModel return $this->belongsTo(PackingList::class, 'packing_list_id'); } + /** * @return HasManyThrough */ @@ -73,4 +75,48 @@ class Package extends AbstractModel return $this->hasOneDeep(Transport::class, [PackingList::class], ['id', ['owner_type', 'owner_id']], ['packing_list_id', null]); } + /** + * @param $query + * @return mixed + */ + public function scopeShipping($query){ + return $query->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); + }); + } + + /** + * @param $query + * @return mixed + */ + public function scopeShipped($query){ + return $query->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); + }); + } + + /** + * @param $query + * @return mixed + */ + public function scopeShippingList($query){ + return $query->whereHas('packingList', function($packingList){ + return $packingList->where('type', PackingListType::SHIPPING_PACKING_LIST); + }); + } + + /** + * @param $query + * @return mixed + */ + public function scopeWarehouseReceiveList($query){ + return $query->whereHas('packingList', function($packingList){ + return $packingList->where('type', PackingListType::WAREHOUSE_RECEIVE_LIST); + }); + } + } diff --git a/app/Models/Schedule.php b/app/Models/Schedule.php index bc7022a9..ba13e072 100644 --- a/app/Models/Schedule.php +++ b/app/Models/Schedule.php @@ -2,6 +2,8 @@ namespace App\Models; +use App\Classes\ValueObjects\Constants\ApprovalStatus; +use Carbon\Carbon; use Illuminate\Database\Eloquent\Relations\MorphTo; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\Relations\BelongsTo; @@ -23,4 +25,20 @@ class Schedule extends AbstractModel { return $this->BelongsTo(Transport::class, 'transport_id', 'id'); } + + /** + * @param $query + * @return mixed + */ + public function scopeDispatched($query){ + return $query->where('etd', '<', Carbon::now()); + } + + /** + * @param $query + * @return mixed + */ + public function scopeDropped($query){ + return $query->where('eta', '<', Carbon::now()); + } } diff --git a/app/Models/Transport.php b/app/Models/Transport.php index 1d817097..f6b58571 100644 --- a/app/Models/Transport.php +++ b/app/Models/Transport.php @@ -2,9 +2,10 @@ namespace App\Models; +use App\Classes\ValueObjects\Constants\ApprovalStatus; +use Carbon\Carbon; use Illuminate\Database\Eloquent\Relations\MorphTo; use Illuminate\Database\Eloquent\SoftDeletes; -use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\MorphMany; class Transport extends AbstractModel @@ -32,4 +33,20 @@ class Transport extends AbstractModel { return $this->morphMany(Schedule::class, 'owner'); } + + /** + * @param $query + * @return mixed + */ + public function scopeDispatched($query){ + return $query->where('dispatch_date', '<', Carbon::now())->whereIn('status', [ApprovalStatus::APPROVED]); + } + + /** + * @param $query + * @return mixed + */ + public function scopeDropped($query){ + return $query->where('drop_date', '<', Carbon::now())->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]); + } }