98 lines
3.0 KiB
PHP
98 lines
3.0 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 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 $subtotal_minor
|
|
* @property int $tax_minor
|
|
* @property int $total_minor
|
|
* @property Carbon|null $submitted_at
|
|
* @property Carbon|null $needed_by
|
|
* @property array<string,mixed>|null $comparison_policy
|
|
* @property Collection<int, ApprovalStep> $steps
|
|
* @property Collection<int, RequisitionItem> $items
|
|
*/
|
|
class Requisition extends Model
|
|
{
|
|
use BelongsToTenant;
|
|
use HasWorkContext;
|
|
|
|
protected $guarded = ['id'];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return ['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()) {
|
|
$query->where('department_id', $user->department_id);
|
|
} elseif (! $user->hasFunctions([PurchasingRole::Admin, PurchasingRole::Procurement, PurchasingRole::Finance])) {
|
|
$query->where('requester_id', $user->id);
|
|
}
|
|
}
|
|
|
|
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 && $user->hasFunctions([$next['role']])
|
|
&& ($next['role'] !== 'department_head' || $this->department_id === $user->department_id)
|
|
&& ! app(ConflictGuard::class)->participationBlocked($user, $this);
|
|
}
|
|
}
|