<?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\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphOne;

final class Receipt extends Model
{
    use HasPublicId, ImmutableWhenPosted;

    protected $fillable = ['public_id','company_id','branch_id','party_id','receipt_number','receipt_type','receipt_date','amount','currency_code','exchange_rate','payment_method','cash_bank_account_id','receivable_account_id','customer_advance_account_id','reference_number','description','allocated_total','status','approved_by','approved_at','posted_at','created_by'];
    protected function casts(): array { return ['receipt_date'=>'date','amount'=>'decimal:4','exchange_rate'=>'decimal:8','allocated_total'=>'decimal:4','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 allocations(): HasMany { return $this->hasMany(ReceiptAllocation::class); }
    public function creator(): BelongsTo { return $this->belongsTo(User::class, 'created_by'); }
    public function approver(): BelongsTo { return $this->belongsTo(User::class, 'approved_by'); }
    public function journal(): MorphOne { return $this->morphOne(JournalEntry::class, 'source'); }
}
