mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/shipping-portal.git
synced 2026-08-19 12:34:18 +00:00
191 lines
5.5 KiB
PHP
191 lines
5.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Classes\General\Interfaces\Addressable;
|
|
use App\Classes\General\Interfaces\Contactable;
|
|
use App\Classes\General\Interfaces\Packable;
|
|
use App\Classes\General\Interfaces\Remarkable;
|
|
use App\Classes\General\Interfaces\Notifiable;
|
|
use App\Classes\General\Interfaces\KeyValueInterface;
|
|
use App\Classes\ValueObjects\Constants\PackingListType;
|
|
use App\Classes\ValueObjects\Constants\RoleTypes;
|
|
use App\Classes\ValueObjects\Constants\ApprovalStatus;
|
|
use App\Scopes\CustomerOrdersScope;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
|
use Staudenmeir\EloquentHasManyDeep\HasManyDeep;
|
|
use Staudenmeir\EloquentHasManyDeep\HasRelationships;
|
|
|
|
class Order extends AbstractModel implements Addressable, Packable, Remarkable, Notifiable, KeyValueInterface
|
|
{
|
|
use SoftDeletes;
|
|
use HasRelationships;
|
|
|
|
protected $table = 'orders';
|
|
|
|
protected $dates = ['deleted_at'];
|
|
|
|
|
|
/**
|
|
* @return BelongsTo
|
|
*/
|
|
public function companyModule(): belongsTo
|
|
{
|
|
return $this->belongsTo(CompanyModule::class, 'company_module_id', 'id');
|
|
}
|
|
|
|
/**
|
|
* @return MorphMany
|
|
*/
|
|
public function orderSteps(): morphMany
|
|
{
|
|
return $this->morphMany(Step::class, 'owner');
|
|
}
|
|
|
|
/**
|
|
* @return MorphMany
|
|
*/
|
|
public function addresses(): morphMany
|
|
{
|
|
return $this->morphMany(Address::class, 'owner');
|
|
}
|
|
|
|
/**
|
|
* @return MorphMany
|
|
*/
|
|
public function packingLists(): morphMany
|
|
{
|
|
return $this->morphMany(PackingList::class, 'owner');
|
|
}
|
|
|
|
/**
|
|
* @return MorphMany
|
|
*/
|
|
public function addons(): morphMany
|
|
{
|
|
return $this->morphMany(Addon::class, 'owner');
|
|
}
|
|
|
|
/**
|
|
* @return MorphMany
|
|
*/
|
|
public function remarks(): morphMany
|
|
{
|
|
return $this->morphMany(Remark::class, 'owner');
|
|
}
|
|
|
|
/**
|
|
* @return HasManyThrough
|
|
*/
|
|
public function parcels(): HasManyThrough
|
|
{
|
|
return $this->HasManyThrough(Package::class, PackingList::class, 'owner_id', 'packing_list_id')->where('owner_type', Order::class);
|
|
}
|
|
|
|
/**
|
|
* @return HasMany
|
|
*/
|
|
public function OrderRoles(): HasMany
|
|
{
|
|
return $this->HasMany(OrderRole::class, 'order_id');
|
|
}
|
|
|
|
|
|
protected static function booted()
|
|
{
|
|
// if (auth()->user()->type === RoleTypes::USER) {
|
|
// static::addGlobalScope(new CustomerOrdersScope);
|
|
// }
|
|
}
|
|
|
|
public function addressesPendingVerification()
|
|
{
|
|
return $this->addresses()->where('status','=',ApprovalStatus::PENDING_VERIFICATION);
|
|
}
|
|
|
|
public function addressesApproved()
|
|
{
|
|
return $this->addresses()->where('status','=',ApprovalStatus::APPROVED);
|
|
}
|
|
|
|
public function originWarehousePackages()
|
|
{
|
|
return $this->packingLists()->where('type', PackingListType::SHIPPING_PACKING_LIST)
|
|
->whereDoesntHave('shippingSchedules',
|
|
function($schedule) {
|
|
return $schedule->dispatched()
|
|
->whereIn('schedules.status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]);
|
|
}
|
|
)->whereHas('packages');
|
|
}
|
|
|
|
public function inTransitPackages()
|
|
{
|
|
return $this->packingLists()->where('type', PackingListType::SHIPPING_PACKING_LIST)->Shipping()
|
|
->whereHas('shippingSchedules',
|
|
function($schedule) {
|
|
return $schedule->dispatched()
|
|
->whereIn('schedules.status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]);
|
|
}
|
|
)
|
|
->whereDoesntHave('containers', function($container) {
|
|
return $container->where('containers.status', ApprovalStatus::COMPLETED);
|
|
}
|
|
)->whereHas('packages');
|
|
}
|
|
|
|
public function destinationWarehousePackages()
|
|
{
|
|
return $this->packingLists()->where('type', PackingListType::SHIPPING_PACKING_LIST)
|
|
->whereHas('containers',
|
|
function($container) {
|
|
return $container->where('containers.status', ApprovalStatus::COMPLETED);
|
|
}
|
|
)
|
|
->whereDoesntHave('deliverySchedules',
|
|
function($schedule) {
|
|
return $schedule->dispatched()->whereIn('schedules.status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]);
|
|
}
|
|
)->whereHas('packages');
|
|
}
|
|
|
|
public function deliveredPackages()
|
|
{
|
|
return $this->packingLists()->where('type', PackingListType::SHIPPING_PACKING_LIST)
|
|
->whereHas('deliverySchedules',
|
|
function($schedule) {
|
|
return $schedule->dispatched()->whereIn('schedules.status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]);
|
|
}
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @return hasManyDeep
|
|
*/
|
|
public function transactions(): hasManyDeep
|
|
{
|
|
return $this->hasManyDeep(Transaction::class, [PackingList::class], ['owner_id', 'owner_id'], ['id', 'id']);
|
|
}
|
|
|
|
/**
|
|
* @return HasMany
|
|
*/
|
|
public function supplierTaxRebate(): HasMany
|
|
{
|
|
return $this->HasMany(SupplierTaxRebate::class, 'order_id');
|
|
}
|
|
|
|
/**
|
|
* @return MorphMany
|
|
*/
|
|
public function attributesKVP(): MorphMany
|
|
{
|
|
return $this->morphMany(KeyValuePair::class, 'owner');
|
|
}
|
|
|
|
}
|