73 lines
2.0 KiB
PHP
73 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\PurchasingRole;
|
|
use App\Models\Concerns\BelongsToTenant;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
|
use LogicException;
|
|
|
|
/**
|
|
* @property int $id
|
|
* @property int $tenant_id
|
|
* @property int $user_id
|
|
* @property int|null $supplier_id
|
|
* @property int|null $requisition_id
|
|
* @property int|null $root_id
|
|
* @property bool $has_conflict
|
|
*/
|
|
class ConflictDeclaration extends Model
|
|
{
|
|
use BelongsToTenant;
|
|
|
|
public $timestamps = false;
|
|
|
|
protected $guarded = ['id'];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return ['has_conflict' => 'boolean', 'known_since' => 'date:Y-m-d', 'declared_at' => 'datetime'];
|
|
}
|
|
|
|
protected static function booted(): void
|
|
{
|
|
static::updating(fn () => throw new LogicException('As declarações são preservadas; registe uma correcção.'));
|
|
static::deleting(fn () => throw new LogicException('As declarações de conflito não podem ser eliminadas.'));
|
|
}
|
|
|
|
/** @param Builder<ConflictDeclaration> $query */
|
|
public function scopeVisibleTo(Builder $query, User $user): void
|
|
{
|
|
if (! in_array($user->purchasing_role, [PurchasingRole::Admin, PurchasingRole::Finance])) {
|
|
$query->where('user_id', $user->id);
|
|
}
|
|
}
|
|
|
|
/** @return BelongsTo<User, $this> */
|
|
public function author(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'user_id');
|
|
}
|
|
|
|
/** @return BelongsTo<Supplier, $this> */
|
|
public function supplier(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Supplier::class);
|
|
}
|
|
|
|
/** @return BelongsTo<Requisition, $this> */
|
|
public function requisition(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Requisition::class);
|
|
}
|
|
|
|
/** @return HasOne<ConflictDeclaration, $this> */
|
|
public function successor(): HasOne
|
|
{
|
|
return $this->hasOne(self::class, 'previous_id');
|
|
}
|
|
}
|