83 lines
3.4 KiB
PHP
83 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Models\Concerns\BelongsToTenant;
|
|
use App\Models\Concerns\ImmutablePurchaseRecord;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Support\Facades\Schema;
|
|
|
|
/**
|
|
* @property int $id
|
|
* @property int $tenant_id
|
|
* @property int $requisition_id
|
|
* @property string $number
|
|
* @property array<string, mixed> $snapshot
|
|
*/
|
|
class PurchaseOrder extends Model
|
|
{
|
|
use BelongsToTenant, ImmutablePurchaseRecord;
|
|
|
|
public $timestamps = false;
|
|
|
|
protected $guarded = ['id'];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return ['snapshot' => 'array', 'issued_at' => 'datetime'];
|
|
}
|
|
|
|
/** Older synthetic documents may predate the complete frozen document structure. */
|
|
public function hasCompleteDocumentSnapshot(): bool
|
|
{
|
|
foreach (['company', 'department', 'cost_center', 'requisition', 'issuer', 'requester', 'supplier', 'quotation', 'award', 'approval_rules', 'decision_matrix', 'final_approvals'] as $key) {
|
|
if (! is_array($this->snapshot[$key] ?? null)) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/** @return HasMany<PurchaseOrderItem, $this> */
|
|
public function items(): HasMany
|
|
{
|
|
return $this->hasMany(PurchaseOrderItem::class);
|
|
}
|
|
|
|
/** @param Builder<PurchaseOrder> $query */
|
|
public function scopeVisibleTo(Builder $query, User $user): void
|
|
{
|
|
$query->whereIn('requisition_id', Requisition::query()->visibleTo($user)->select('id'));
|
|
if (Schema::hasColumn('purchase_orders', 'order_key')) {
|
|
if (! Schema::hasTable('purchase_order_sources')) {
|
|
$query->whereNull('order_key');
|
|
} else {
|
|
$query->where(function ($orders) use ($user) {
|
|
$orders->whereNull('order_key')->orWhere(function ($parts) use ($user) {
|
|
$parts->whereExists(fn ($sources) => $sources->selectRaw('1')->from('purchase_order_sources as os')
|
|
->whereColumn('os.purchase_order_id', 'purchase_orders.id')->whereColumn('os.tenant_id', 'purchase_orders.tenant_id'))
|
|
->whereRaw('(select count(*) from purchase_order_sources os where os.purchase_order_id = purchase_orders.id and os.tenant_id = purchase_orders.tenant_id) = purchase_orders.order_source_count')
|
|
->whereNotExists(fn ($sources) => $sources->selectRaw('1')->from('purchase_order_sources as os')
|
|
->whereColumn('os.purchase_order_id', 'purchase_orders.id')->whereColumn('os.tenant_id', 'purchase_orders.tenant_id')
|
|
->whereNotIn('os.requisition_id', Requisition::query()->visibleTo($user)->select('id')));
|
|
});
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
/** @param Builder<PurchaseOrder> $query */
|
|
public function scopeOperational(Builder $query): void
|
|
{
|
|
$query->where('purchase_orders.status', 'issued');
|
|
if (Schema::hasTable('order_cancellations')) {
|
|
$query->whereNotExists(fn ($q) => $q->selectRaw('1')->from('order_cancellations as oc')
|
|
->whereColumn('oc.purchase_order_id', 'purchase_orders.id')->whereColumn('oc.tenant_id', 'purchase_orders.tenant_id')
|
|
->whereIn('oc.status', ['pending', 'approved']));
|
|
}
|
|
}
|
|
}
|