39 lines
1.5 KiB
PHP
39 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Models\Concerns\BelongsToTenant;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use LogicException;
|
|
|
|
/** @property array<string,mixed> $snapshot */
|
|
class OrderCancellation extends Model
|
|
{
|
|
use BelongsToTenant;
|
|
|
|
protected $guarded = ['id'];
|
|
|
|
protected $attributes = ['status' => 'pending'];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return ['snapshot' => 'array'];
|
|
}
|
|
|
|
protected static function booted(): void
|
|
{
|
|
static::updating(function (self $record) {
|
|
if ($record->getRawOriginal('status') === 'pending' && (! in_array($record->status, ['approved', 'rejected'], true)
|
|
|| $record->reviewed_by === null || $record->reviewed_at === null || trim((string) $record->review_reason) === '')) {
|
|
throw new LogicException('Decisão completa e definitiva obrigatória.');
|
|
}
|
|
$allowed = $record->getRawOriginal('status') === 'pending' ? ['status', 'reviewed_by', 'review_reason', 'reviewed_at', 'updated_at']
|
|
: ($record->getRawOriginal('status') === 'approved' && $record->getRawOriginal('revised_requisition_id') === null ? ['revised_requisition_id', 'updated_at'] : []);
|
|
if (array_diff(array_keys($record->getDirty()), $allowed) !== []) {
|
|
throw new LogicException('Preservar pedido e decisão de cancelamento.');
|
|
}
|
|
});
|
|
static::deleting(fn () => throw new LogicException('Preservar cancelamento.'));
|
|
}
|
|
}
|