<?php

namespace App\Models;

use App\Enums\DocumentStatus;
use App\Models\Concerns\HasPublicId;
use App\Models\Concerns\ImmutableWhenPosted;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphOne;

final class Expense extends Model
{
    use HasPublicId, ImmutableWhenPosted;

    protected $fillable = ['public_id','company_id','branch_id','party_id','transport_job_id','trip_id','vehicle_id','driver_id','department_id','cost_center_id','expense_number','expense_date','category','description','expense_account_id','input_vat_account_id','payment_account_id','tax_code_id','amount_before_vat','recoverable_vat','nonrecoverable_vat','total_amount','payment_method','is_billable','reimbursement_status','status','approved_by','approved_at','posted_at','created_by'];
    protected function casts(): array { return ['expense_date'=>'date','amount_before_vat'=>'decimal:4','recoverable_vat'=>'decimal:4','nonrecoverable_vat'=>'decimal:4','total_amount'=>'decimal:4','is_billable'=>'boolean','status'=>DocumentStatus::class,'approved_at'=>'datetime','posted_at'=>'datetime']; }
    public function company(): BelongsTo { return $this->belongsTo(Company::class); }
    public function branch(): BelongsTo { return $this->belongsTo(Branch::class); }
    public function party(): BelongsTo { return $this->belongsTo(Party::class); }
    public function transportJob(): BelongsTo { return $this->belongsTo(TransportJob::class); }
    public function trip(): BelongsTo { return $this->belongsTo(Trip::class); }
    public function vehicle(): BelongsTo { return $this->belongsTo(Vehicle::class); }
    public function driver(): BelongsTo { return $this->belongsTo(Driver::class); }
    public function expenseAccount(): BelongsTo { return $this->belongsTo(Account::class, 'expense_account_id'); }
    public function paymentAccount(): BelongsTo { return $this->belongsTo(Account::class, 'payment_account_id'); }
    public function journal(): MorphOne { return $this->morphOne(JournalEntry::class, 'source'); }
}
