<?php
namespace App\Models;
use App\Enums\DocumentStatus;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use LogicException;
final class CustomerInvoiceItem extends Model
{
    protected $fillable=['customer_invoice_id','line_number','description','quantity','unit','unit_rate','discount_amount','taxable_amount','vat_rate','vat_amount','line_total','income_account_id','output_vat_account_id','tax_code_id'];
    protected function casts(): array{return ['quantity'=>'decimal:4','unit_rate'=>'decimal:4','discount_amount'=>'decimal:4','taxable_amount'=>'decimal:4','vat_rate'=>'decimal:4','vat_amount'=>'decimal:4','line_total'=>'decimal:4'];}
    public function invoice(): BelongsTo{return $this->belongsTo(CustomerInvoice::class,'customer_invoice_id');}
    public function incomeAccount(): BelongsTo{return $this->belongsTo(Account::class,'income_account_id');}
    public function taxCode(): BelongsTo{return $this->belongsTo(TaxCode::class);}
    protected static function booted(): void { $guard=function(self $item): void { if($item->invoice()->where('status',DocumentStatus::POSTED->value)->exists()) throw new LogicException('Items of a posted invoice are immutable.'); }; static::updating($guard); static::deleting($guard); }
}
