33 lines
979 B
PHP
33 lines
979 B
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\PurchasingRole;
|
|
use App\Models\Concerns\BelongsToTenant;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class PurchasePlanItem extends Model
|
|
{
|
|
use BelongsToTenant;
|
|
|
|
protected $guarded = ['id'];
|
|
|
|
protected $attributes = ['version' => 1];
|
|
|
|
/** @param Builder<PurchasePlanItem> $query */
|
|
public function scopeVisibleTo(Builder $query, User $user): void
|
|
{
|
|
if ($user->departmentScoped()) {
|
|
$query->where(fn ($visible) => $visible->where('department_id', $user->department_id)->orWhere('responsible_id', $user->id));
|
|
} elseif (! $user->hasFunctions([PurchasingRole::Admin, PurchasingRole::Procurement, PurchasingRole::Finance])) {
|
|
$query->where('responsible_id', $user->id);
|
|
}
|
|
}
|
|
|
|
protected static function booted(): void
|
|
{
|
|
static::deleting(fn () => throw new \LogicException('Preservar o planeamento.'));
|
|
}
|
|
}
|