Files

207 lines
7.8 KiB
PHP

<?php
namespace App\Models;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\PackingListType;
use App\Classes\ValueObjects\Constants\RoleTypes;
use App\Scopes\CustomerOrdersScope;
use App\Scopes\CustomerPackagesScope;
use Barryvdh\LaravelIdeHelper\Eloquent;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\Relations\HasOneThrough;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Query\Builder;
use Spatie\Activitylog\Models\Activity;
use Staudenmeir\EloquentHasManyDeep\HasManyDeep;
use Staudenmeir\EloquentHasManyDeep\HasOneDeep;
use Staudenmeir\EloquentHasManyDeep\HasRelationships;
/**
* App\Models\Package
*
* @property int $id
* @property int $packing_list_id
* @property int $type
* @property string|null $reference
* @property string|null $description
* @property string $width
* @property string $height
* @property string $length
* @property string $weight
* @property int $quantity
* @property int $status
* @property \Illuminate\Support\Carbon|null $deleted_at
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property-read Collection|Activity[] $activities
* @property-read int|null $activities_count
* @property-read Model|Eloquent $causer
* @property-read Collection|Transport[] $deliveryTransports
* @property-read int|null $delivery_transports_count
* @property-read PackingList|null $packingList
* @property-read Model|Eloquent $subject
* @property-read Model|Eloquent $target
* @method static \Illuminate\Database\Eloquent\Builder|Package newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|Package newQuery()
* @method static Builder|Package onlyTrashed()
* @method static \Illuminate\Database\Eloquent\Builder|Package query()
* @method static \Illuminate\Database\Eloquent\Builder|Package shipped()
* @method static \Illuminate\Database\Eloquent\Builder|Package shipping()
* @method static \Illuminate\Database\Eloquent\Builder|Package shippingList()
* @method static \Illuminate\Database\Eloquent\Builder|Package warehouseReceiveList()
* @method static \Illuminate\Database\Eloquent\Builder|Package whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|Package whereDeletedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|Package whereDescription($value)
* @method static \Illuminate\Database\Eloquent\Builder|Package whereHeight($value)
* @method static \Illuminate\Database\Eloquent\Builder|Package whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|Package whereLength($value)
* @method static \Illuminate\Database\Eloquent\Builder|Package wherePackingListId($value)
* @method static \Illuminate\Database\Eloquent\Builder|Package whereQuantity($value)
* @method static \Illuminate\Database\Eloquent\Builder|Package whereReference($value)
* @method static \Illuminate\Database\Eloquent\Builder|Package whereStatus($value)
* @method static \Illuminate\Database\Eloquent\Builder|Package whereType($value)
* @method static \Illuminate\Database\Eloquent\Builder|Package whereUpdatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|Package whereWeight($value)
* @method static \Illuminate\Database\Eloquent\Builder|Package whereWidth($value)
* @method static Builder|Package withTrashed()
* @method static Builder|Package withoutTrashed()
* @mixin Eloquent
*/
class Package extends AbstractModel
{
use HasRelationships;
use SoftDeletes;
protected $table = 'packages';
/**
* @return BelongsTo
*/
public function packingList(): BelongsTo
{
return $this->belongsTo(PackingList::class, 'packing_list_id');
}
/**
* @return hasOneDeep
*/
public function order(): hasOneDeep
{
return $this->hasOneDeep(Order::class, [PackingList::class], ['id', 'id'], ['packing_list_id', 'owner_id']);
}
/**
* @return hasOneDeep
*/
public function companyModule(): hasOneDeep
{
return $this->hasOneDeep(CompanyModule::class, [PackingList::class, Order::class], ['id', 'id', 'id'], ['packing_list_id', 'owner_id', 'company_module_id']);
}
/**
* @return hasManyDeep
*/
public function shippingTransports(): hasManyDeep
{
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 hasManyDeep
*/
public function shippingSchedules(): hasManyDeep
{
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 hasManyDeep
*/
public function deliverySchedules(): hasManyDeep
{
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]);
}
/**
* @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);
});
}
protected static function booted()
{
// if (auth()->user()->type === RoleTypes::USER) {
// static::addGlobalScope(new CustomerPackagesScope());
// }
}
}