<?php

namespace App\Models;

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

class SupplierLedger extends Model
{
    use HasFactory;

    protected $table = 'supplier_ledger';

    protected $fillable = [
        'supplier_id',
        'branch_id',
        'transaction_type',
        'reference_type',
        'reference_id',
        'description',
        'debit',
        'credit',
        'balance',
        'transaction_date',
    ];

    protected $casts = [
        'debit'            => 'decimal:2',
        'credit'           => 'decimal:2',
        'balance'          => 'decimal:2',
        'transaction_date' => 'date',
    ];

    public const TYPE_OPENING_BALANCE = 'opening_balance';
    public const TYPE_INVOICE         = 'invoice';
    public const TYPE_PAYMENT         = 'payment';
    public const TYPE_CREDIT_NOTE     = 'credit_note';
    public const TYPE_ADJUSTMENT      = 'adjustment';

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

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