53 lines
1.6 KiB
PHP
53 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Models\Concerns\BelongsToTenant;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
/** @property int $id
|
|
* @property int $tenant_id
|
|
* @property int $supplier_id
|
|
* @property int|null $department_id
|
|
* @property int $created_by
|
|
* @property int $version
|
|
* @property int $revision
|
|
* @property string $status
|
|
* @property array<string,mixed> $payload
|
|
* @property array<string,mixed> $supplier_snapshot
|
|
* @property list<int> $contributors
|
|
*/
|
|
class SupplierEvaluation extends Model
|
|
{
|
|
use BelongsToTenant;
|
|
|
|
protected $guarded = ['id'];
|
|
|
|
protected $attributes = ['version' => 1, 'revision' => 1, 'status' => 'draft'];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return ['payload' => 'array', 'supplier_snapshot' => 'array', 'contributors' => 'array', 'reviewed_at' => 'datetime'];
|
|
}
|
|
|
|
/** @param Builder<SupplierEvaluation> $query */
|
|
public function scopeVisibleTo(Builder $query, User $actor): void
|
|
{
|
|
$query->where('tenant_id', $actor->tenant_id);
|
|
if ($actor->departmentScoped()) {
|
|
$query->where('department_id', $actor->department_id ?? 0);
|
|
}
|
|
}
|
|
|
|
protected static function booted(): void
|
|
{
|
|
static::updating(function (self $evaluation) {
|
|
if ($evaluation->getRawOriginal('status') === 'approved') {
|
|
throw new \LogicException('A avaliação aprovada conserva-se; prepare uma nova revisão.');
|
|
}
|
|
});
|
|
static::deleting(fn () => throw new \LogicException('Preservar o histórico de avaliações.'));
|
|
}
|
|
}
|