Files
compras/app/Models/Budget.php
T

47 lines
1.3 KiB
PHP

<?php
namespace App\Models;
use App\Models\Concerns\BelongsToTenant;
use Illuminate\Database\Eloquent\Model;
use Tempo\Finance\Budget\BudgetControl;
/** @property int $id
* @property int $amount_minor
* @property int $created_by
* @property int|null $approved_by
* @property string $kind
*/
class Budget extends Model
{
use BelongsToTenant;
protected $guarded = ['id'];
public function approvedTotalMinor(): int
{
$sum = BudgetReinforcement::query()->where('budget_id', $this->id)->where('status', 'approved')->sum('amount_minor');
return BudgetControl::approvedTotal((int) $this->amount_minor, (int) $sum);
}
protected static function booted(): void
{
static::updating(function (Budget $budget) {
if ($budget->getRawOriginal('approved_by') !== null) {
throw new \LogicException('Uma dotação aprovada não pode ser reescrita.');
}
});
static::deleting(function (Budget $budget) {
if ($budget->approved_by !== null) {
throw new \LogicException('Preservar a dotação aprovada e os seus movimentos.');
}
});
}
protected function casts(): array
{
return ['includes_tax' => 'boolean'];
}
}