Implementa reforcos orcamentais com aprovacao independente
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\Concerns\BelongsToTenant;
|
||||
use Brick\Math\BigDecimal;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/** @property int $id
|
||||
@@ -16,6 +17,13 @@ class Budget extends Model
|
||||
|
||||
protected $guarded = ['id'];
|
||||
|
||||
public function approvedTotalMinor(): int
|
||||
{
|
||||
$sum = BudgetReinforcement::query()->where('budget_id', $this->id)->where('status', 'approved')->sum('amount_minor');
|
||||
|
||||
return BigDecimal::of((string) $sum)->plus($this->amount_minor)->toInt();
|
||||
}
|
||||
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::updating(function (Budget $budget) {
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\Concerns\BelongsToTenant;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/** @property int $id
|
||||
* @property int $tenant_id
|
||||
* @property int $budget_id
|
||||
* @property int $created_by
|
||||
* @property int $amount_minor
|
||||
* @property string $status
|
||||
* @property string $payload_hash
|
||||
*/
|
||||
class BudgetReinforcement extends Model
|
||||
{
|
||||
use BelongsToTenant;
|
||||
|
||||
protected $guarded = ['id'];
|
||||
|
||||
protected $attributes = ['status' => 'pending'];
|
||||
|
||||
protected $hidden = ['payload_hash'];
|
||||
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::updating(function (BudgetReinforcement $reinforcement) {
|
||||
if ($reinforcement->getRawOriginal('status') !== 'pending' || array_diff(array_keys($reinforcement->getDirty()), ['status', 'reviewed_by', 'review_reason', 'reviewed_at', 'updated_at']) !== []) {
|
||||
throw new \LogicException('Preservar o reforço e a decisão.');
|
||||
}
|
||||
});
|
||||
static::deleting(fn () => throw new \LogicException('Não eliminar reforços orçamentais.'));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user