43 lines
1.2 KiB
PHP
43 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Models\Concerns\BelongsToTenant;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
/** @property int $id
|
|
* @property int $tenant_id
|
|
* @property int $purchase_order_id
|
|
* @property int $created_by
|
|
* @property string $status
|
|
* @property array<string,mixed> $snapshot
|
|
*/
|
|
class Invoice extends Model
|
|
{
|
|
use BelongsToTenant;
|
|
|
|
protected $guarded = ['id'];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return ['snapshot' => 'array'];
|
|
}
|
|
|
|
protected static function booted(): void
|
|
{
|
|
static::updating(function (Invoice $invoice) {
|
|
if ($invoice->getRawOriginal('status') !== 'pending' || array_diff(array_keys($invoice->getDirty()), ['status', 'validated_by', 'validated_at', 'updated_at']) !== []) {
|
|
throw new \LogicException('Preservar a factura registada e a validação.');
|
|
}
|
|
});
|
|
static::deleting(fn () => throw new \LogicException('Preservar o histórico de facturas.'));
|
|
}
|
|
|
|
/** @return HasMany<InvoiceItem,$this> */
|
|
public function items(): HasMany
|
|
{
|
|
return $this->hasMany(InvoiceItem::class);
|
|
}
|
|
}
|