<?php
namespace App\Models;
use App\Enums\DocumentStatus;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use LogicException;
final class VendorBillItem extends Model
{
    protected $fillable=['vendor_bill_id','line_number','description','quantity','unit','unit_rate','discount_amount','taxable_amount','recoverable_vat_amount','nonrecoverable_vat_amount','line_total','expense_account_id','input_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','recoverable_vat_amount'=>'decimal:4','nonrecoverable_vat_amount'=>'decimal:4','line_total'=>'decimal:4'];}
    public function bill(): BelongsTo{return $this->belongsTo(VendorBill::class,'vendor_bill_id');}
    public function expenseAccount(): BelongsTo{return $this->belongsTo(Account::class,'expense_account_id');}
    public function taxCode(): BelongsTo{return $this->belongsTo(TaxCode::class);}
    protected static function booted(): void { $guard=function(self $item): void { if($item->bill()->where('status',DocumentStatus::POSTED->value)->exists()) throw new LogicException('Items of a posted vendor bill are immutable.'); }; static::updating($guard); static::deleting($guard); }
}
