74 lines
2.9 KiB
PHP
74 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\PurchaseControlPolicy;
|
|
use App\Models\PurchaseControlReview;
|
|
use App\Models\User;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Support\Facades\Schema;
|
|
|
|
class OrderCancellationPolicyWorkflow extends PurchaseControlPolicyWorkflow
|
|
{
|
|
public function canReview(User $actor, PurchaseControlPolicy $policy): bool
|
|
{
|
|
return (bool) config('order_cancellation.enabled') && $actor->tenant_id === $policy->tenant_id && $actor->hasFunctions(['admin'])
|
|
&& ! app(OrganizationAccess::class)->maintenanceOnly($actor) && $policy->getTable() === 'order_cancellation_policies'
|
|
&& $policy->status === 'pending' && $policy->created_by !== $actor->id && $policy->revision === $this->policies()->max('revision')
|
|
&& ! $this->reviews()->where('purchase_control_policy_id', $policy->id)->where('reviewed_by', $actor->id)->exists();
|
|
}
|
|
|
|
/** @return Builder<PurchaseControlPolicy> */
|
|
protected function policies(): Builder
|
|
{
|
|
return (new PurchaseControlPolicy)->setTable('order_cancellation_policies')->newQuery();
|
|
}
|
|
|
|
/** @return Builder<PurchaseControlReview> */
|
|
protected function reviews(): Builder
|
|
{
|
|
return (new PurchaseControlReview)->setTable('order_cancellation_policy_reviews')->newQuery();
|
|
}
|
|
|
|
protected function mode(): string
|
|
{
|
|
return 'issued_order';
|
|
}
|
|
|
|
protected function auditPrefix(): string
|
|
{
|
|
return 'order_cancellation_policy';
|
|
}
|
|
|
|
private function enabled(): void
|
|
{
|
|
abort_unless(config('order_cancellation.enabled') && Schema::hasTable('order_cancellation_policies'), 503, 'Cancelamento de ordens não activado.');
|
|
}
|
|
|
|
public function propose(User $user, array $data): PurchaseControlPolicy
|
|
{
|
|
$this->enabled();
|
|
abort_if(app(OrganizationAccess::class)->maintenanceOnly($user), 403);
|
|
validator($data, ['previous_id' => 'required|integer|min:0', 'mode' => 'required|in:issued_order', 'enabled' => 'required|boolean',
|
|
'valid_from' => 'required|date_format:Y-m-d', 'valid_until' => 'required|date_format:Y-m-d|after_or_equal:valid_from', 'reason' => 'required|string|min:10|max:4000'])->validate();
|
|
|
|
return parent::propose($user, $data);
|
|
}
|
|
|
|
public function review(User $user, PurchaseControlPolicy $policy, string $decision, string $reason): void
|
|
{
|
|
$this->enabled();
|
|
abort_if(app(OrganizationAccess::class)->maintenanceOnly($user), 403);
|
|
parent::review($user, $policy, $decision, $reason);
|
|
}
|
|
|
|
public function requireCurrent(): PurchaseControlPolicy
|
|
{
|
|
$this->enabled();
|
|
$policy = $this->current();
|
|
abort_unless($policy && $policy->enabled && $policy->mode === 'issued_order' && $policy->valid_until >= today()->format('Y-m-d'), 422, 'É necessária política vigente de cancelamento de ordens, com duas aprovações administrativas independentes.');
|
|
|
|
return $policy;
|
|
}
|
|
}
|