Files

235 lines
8.0 KiB
PHP

<?php
namespace App\Models;
use App\Classes\General\Interfaces\Addressable;
use App\Classes\General\Interfaces\Contactable;
use App\Classes\General\Interfaces\KeyValueInterface;
use App\Classes\General\Interfaces\Packable;
use App\Classes\General\Interfaces\Remarkable;
use App\Classes\General\Interfaces\Notifiable;
use App\Classes\ValueObjects\Constants\PackingListType;
use App\Classes\ValueObjects\Constants\RoleTypes;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Scopes\CustomerOrdersScope;
use Eloquent;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
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 Illuminate\Database\Query\Builder;
use Illuminate\Support\Carbon;
use Spatie\Activitylog\Models\Activity;
use Staudenmeir\EloquentHasManyDeep\HasManyDeep;
use Staudenmeir\EloquentHasManyDeep\HasRelationships;
/**
* App\Models\Order
*
* @method static whereBetween(string $string, array $array)
* @property int $id
* @property int $company_module_id
* @property string $reference
* @property string|null $reference_contract
* @property int $type Order packages can be shipped in shared or dedicated container
* @property int $status Status of this order, e.g. processing, shipping etc.
* @property Carbon|null $deleted_at
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
* @property-read Collection|OrderRole[] $OrderRoles
* @property-read int|null $order_roles_count
* @property-read Collection|Activity[] $activities
* @property-read int|null $activities_count
* @property-read Collection|Addon[] $addons
* @property-read int|null $addons_count
* @property-read Collection|Address[] $addresses
* @property-read int|null $addresses_count
* @property-read Model|Eloquent $causer
* @property-read CompanyModule|null $companyModule
* @property-read Collection|Step[] $orderSteps
* @property-read int|null $order_steps_count
* @property-read Collection|PackingList[] $packingLists
* @property-read int|null $packing_lists_count
* @property-read Collection|Package[] $parcels
* @property-read int|null $parcels_count
* @property-read Collection|Remark[] $remarks
* @property-read int|null $remarks_count
* @property-read Model|Eloquent $subject
* @property-read Model|Eloquent $target
* @method static \Illuminate\Database\Eloquent\Builder|Order newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|Order newQuery()
* @method static Builder|Order onlyTrashed()
* @method static \Illuminate\Database\Eloquent\Builder|Order query()
* @method static \Illuminate\Database\Eloquent\Builder|Order whereCompanyModuleId($value)
* @method static \Illuminate\Database\Eloquent\Builder|Order whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|Order whereDeletedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|Order whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|Order whereReference($value)
* @method static \Illuminate\Database\Eloquent\Builder|Order whereReferenceContract($value)
* @method static \Illuminate\Database\Eloquent\Builder|Order whereStatus($value)
* @method static \Illuminate\Database\Eloquent\Builder|Order whereType($value)
* @method static \Illuminate\Database\Eloquent\Builder|Order whereUpdatedAt($value)
* @method static Builder|Order withTrashed()
* @method static Builder|Order withoutTrashed()
* @mixin Eloquent
*/
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']);
}
public function attributes(): MorphMany
{
return $this->morphMany(KeyValuePair::class, 'owner');
}
}