diff --git a/app/Models/Package.php b/app/Models/Package.php index f46b1fb8..cb2bf35f 100644 --- a/app/Models/Package.php +++ b/app/Models/Package.php @@ -26,7 +26,7 @@ class Package extends AbstractModel /** * @return BelongsTo */ - public function packingLists(): BelongsTo + public function packingList(): BelongsTo { return $this->belongsTo(PackingList::class, 'packing_list_id'); } diff --git a/app/Models/PackingList.php b/app/Models/PackingList.php index fc775ba2..77feb7f6 100644 --- a/app/Models/PackingList.php +++ b/app/Models/PackingList.php @@ -4,16 +4,21 @@ namespace App\Models; use App\Classes\General\Interfaces\Steppable; use App\Classes\General\Interfaces\Transportable; +use App\Classes\ValueObjects\Constants\ApprovalStatus; +use Carbon\Carbon; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\MorphMany; use Illuminate\Database\Eloquent\Relations\MorphTo; use Illuminate\Database\Eloquent\SoftDeletes; +use Staudenmeir\EloquentHasManyDeep\HasManyDeep; +use Staudenmeir\EloquentHasManyDeep\HasRelationships; class PackingList extends AbstractModel implements Transportable, Steppable { use SoftDeletes; + use HasRelationships; protected $table = 'packing_lists'; @@ -71,4 +76,49 @@ class PackingList extends AbstractModel implements Transportable, Steppable { return $this->BelongsToMany(PackingList::class, 'packing_list_owner', 'owner_packing_list_id', 'packing_list_id'); } + + /** + * @return MorphMany + */ + public function packingLists(): morphMany + { + return $this->morphMany(PackingList::class, 'owner'); + } + + /** + * @return hasManyDeep + */ + public function shippingTransports(): hasManyDeep + { + return $this->hasManyDeep(Transport::class, [ContainerPackingList::class, Container::class], ['packing_list_id', 'id', ['owner_type', 'owner_id']], ['id', 'container_id', null]); + } + + /** + * @return hasManyDeep + */ + public function shippingSchedules(): hasManyDeep + { + return $this->hasManyDeep(Schedule::class, [ContainerPackingList::class, Container::class, Transport::class], ['packing_list_id', 'id', ['owner_type', 'owner_id'], ['owner_type', 'owner_id']], ['id', 'container_id', null, null]); + } + + + /** + * @return hasManyDeep + */ + public function deliverySchedules(): hasManyDeep + { + return $this->hasManyDeep(Schedule::class, [Transport::class], [['owner_type', 'owner_id'], ['owner_type', 'owner_id']], [null, 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); + }); + } }