@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\Concerns\BelongsToTenant;
|
||||
use Carbon\CarbonInterface;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property int $tenant_id
|
||||
* @property int $supplier_evaluation_id
|
||||
* @property int $created_by
|
||||
* @property int $responsible_id
|
||||
* @property int $version
|
||||
* @property string $status
|
||||
* @property list<int> $contributors
|
||||
* @property CarbonInterface|null $reviewed_at
|
||||
*/
|
||||
class SupplierImprovement extends Model
|
||||
{
|
||||
use BelongsToTenant;
|
||||
|
||||
protected $guarded = ['id'];
|
||||
|
||||
protected $attributes = ['version' => 1, 'status' => 'open'];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return ['contributors' => 'array', 'reviewed_at' => 'datetime'];
|
||||
}
|
||||
|
||||
/** @param Builder<SupplierImprovement> $query */
|
||||
public function scopeVisibleTo(Builder $query, User $actor): void
|
||||
{
|
||||
$query->where('tenant_id', $actor->tenant_id)->whereIn('supplier_evaluation_id', SupplierEvaluation::query()->visibleTo($actor)->select('id'));
|
||||
}
|
||||
|
||||
/** @return BelongsTo<SupplierEvaluation, $this> */
|
||||
public function evaluation(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(SupplierEvaluation::class, 'supplier_evaluation_id');
|
||||
}
|
||||
|
||||
/** @return BelongsTo<User, $this> */
|
||||
public function responsible(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'responsible_id');
|
||||
}
|
||||
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::updating(function (self $measure) {
|
||||
if ($measure->getRawOriginal('status') === 'closed') {
|
||||
throw new \LogicException('A medida encerrada conserva-se no histórico.');
|
||||
}
|
||||
});
|
||||
static::deleting(fn () => throw new \LogicException('Preservar as medidas de melhoria.'));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user