<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphTo;

class CashbookEntry extends Model
{
    protected $fillable = [
        'cashbook_id', 'date', 'module', 'account_id', 'account_type', 
        'reference_id', 'reference_type', 'description',
        'tr_code', 'currency_id', 'project_id' , 'amount', 'dr_amount', 'cr_amount'
    ];

    public function currency(): BelongsTo
    {
        return $this->belongsTo(Currency::class);
    }

     public function project(): BelongsTo
    {
        return $this->belongsTo(Project::class);
    }

    public function cashbook(): BelongsTo
    {
        return $this->belongsTo(Cashbook::class);
    }

    public function account(): MorphTo
    {
        return $this->morphTo();
    }

    public function reference(): MorphTo
    {
        return $this->morphTo();
    }

    // Add customer relationship for AR module
    public function customer()
    {
        return $this->belongsTo(CustomerAccount::class, 'account_id')
            ->where('account_type', 'App\Models\CustomerAccount');
    }
       public function supplier()
    {
        return $this->belongsTo(SupplierAccount::class, 'account_id');
    }
}