<?php

namespace App\Models;

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

class Cashbook extends Model
{
    protected $table = 'cashbook';

   protected $fillable = [
    'company_id',
    'supplier_id',
    'cash_bank_account_id',
    'corresponding_account_id',
    'bill_id',
    'fixed_assets_id',
    'expense_id',
    'invoice_id',
    'customer_id',
    'sales_receipt_id',          // ADD THIS
    'sales_receipt_reversal_id', // ADD THIS
    'reference',
    'memo',
    'payment_method',
    'debit',
    'credit',
    'transaction_date',
    'status',
    'reversal_reason',
    'created_by'
];

    protected $casts = [
        'transaction_date' => 'date',
        'due_section_date' => 'date',
        'debit'            => 'decimal:2',
        'credit'           => 'decimal:2',
        'created_at'       => 'datetime',
        'updated_at'       => 'datetime',
    ];

    /**
     * The bank or cash account used for the transaction.
     */
    public function bankAccount(): BelongsTo
    {
        return $this->belongsTo(ChartOfAccount::class, 'cash_bank_account_id');
    }

    /**
     * The secondary account affected (e.g., Trade Payables).
     */
    public function correspondingAccount(): BelongsTo
    {
        return $this->belongsTo(ChartOfAccount::class, 'corresponding_account_id');
    }

    public function supplier(): BelongsTo
    {
        return $this->belongsTo(Supplier::class, 'supplier_id');
    }

    public function bill(): BelongsTo
    {
        return $this->belongsTo(Bill::class, 'bill_id');
    }

    public function expense(): BelongsTo
    {
        return $this->belongsTo(Expense::class, 'expense_id');
    }

    public function customer(): BelongsTo
    {
        return $this->belongsTo(Customer::class, 'customer_id');
    }

    public function invoice(): BelongsTo
    {
        return $this->belongsTo(Invoice::class, 'invoice_id');
    }

    public function creator(): BelongsTo
    {
        return $this->belongsTo(User::class, 'created_by');
    }
}