105 lines
3.7 KiB
PHP
105 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\PurchasingRole;
|
|
use App\Models\Concerns\BelongsToTenant;
|
|
use App\Models\Concerns\HasWorkContext;
|
|
use App\Services\ConflictGuard;
|
|
use App\Services\RequisitionApprovalLevels;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Support\Carbon;
|
|
use Tempo\Purchasing\Internal\InternalRequisitions;
|
|
|
|
/**
|
|
* @property int $id
|
|
* @property int $tenant_id
|
|
* @property int $department_id
|
|
* @property int $requester_id
|
|
* @property string $number
|
|
* @property string $status
|
|
* @property int|null $subtotal_minor
|
|
* @property int|null $tax_minor
|
|
* @property int|null $total_minor
|
|
* @property Carbon|null $submitted_at
|
|
* @property Carbon|null $needed_by
|
|
* @property array<string,mixed>|null $comparison_policy
|
|
* @property array<string,mixed>|null $budget_distribution_preparation
|
|
* @property Collection<int, ApprovalStep> $steps
|
|
* @property Collection<int, RequisitionItem> $items
|
|
*/
|
|
class Requisition extends Model
|
|
{
|
|
use BelongsToTenant;
|
|
use HasWorkContext;
|
|
|
|
protected $guarded = ['id'];
|
|
|
|
// Stage-specific controllers expose authorised preparation explicitly, never quotation details through generic request JSON.
|
|
protected $hidden = ['budget_distribution_preparation'];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return ['draft_input' => 'array', 'budget_distribution_preparation' => 'array', 'approval_snapshot' => 'array', 'comparison_policy' => 'array', 'submitted_at' => 'datetime', 'needed_by' => 'date:Y-m-d'];
|
|
}
|
|
|
|
/** @return HasMany<RequisitionItem, $this> */
|
|
public function items(): HasMany
|
|
{
|
|
return $this->hasMany(RequisitionItem::class);
|
|
}
|
|
|
|
/** @return HasMany<ApprovalStep, $this> */
|
|
public function steps(): HasMany
|
|
{
|
|
return $this->hasMany(ApprovalStep::class)->orderBy('position');
|
|
}
|
|
|
|
/** @return BelongsTo<User, $this> */
|
|
public function requester(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'requester_id');
|
|
}
|
|
|
|
/** @return BelongsTo<Department, $this> */
|
|
public function department(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Department::class);
|
|
}
|
|
|
|
/** @return BelongsTo<CostCenter, $this> */
|
|
public function costCenter(): BelongsTo
|
|
{
|
|
return $this->belongsTo(CostCenter::class);
|
|
}
|
|
|
|
/** @param Builder<Requisition> $query */
|
|
public function scopeVisibleTo(Builder $query, User $user): void
|
|
{
|
|
if ($user->departmentScoped() || ! $user->hasFunctions([PurchasingRole::Admin, PurchasingRole::Procurement, PurchasingRole::Finance])) {
|
|
$query->where(function ($visible) use ($user) {
|
|
$visible->where($user->departmentScoped() ? 'department_id' : 'requester_id', $user->departmentScoped() ? $user->department_id : $user->id);
|
|
app(RequisitionApprovalLevels::class)->includeVisible($visible, $user);
|
|
});
|
|
}
|
|
}
|
|
|
|
public function canDecide(User $user): bool
|
|
{
|
|
if ($this->tenant_id !== $user->tenant_id) {
|
|
return false;
|
|
}
|
|
|
|
$next = InternalRequisitions::nextStep(['status' => $this->status, 'requester_id' => $this->requester_id, 'steps' => $this->steps->toArray()], $user->id);
|
|
|
|
return $next !== null && (str_starts_with($next['role'], 'configured_')
|
|
? app(RequisitionApprovalLevels::class)->eligible($user, $this, $next)
|
|
: ($user->hasFunctions([$next['role']]) && ($next['role'] !== 'department_head' || $this->department_id === $user->department_id)))
|
|
&& ! app(ConflictGuard::class)->participationBlocked($user, $this);
|
|
}
|
|
}
|