<?php

namespace App\Models;

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

class OrderReceipt extends Model
{
    use HasFactory;

    public const ACCOUNT_TYPE_CUSTOMER_ADVANCE = 'customer_advance_liability';

    protected $guarded = [];

    protected $casts = [
        'account_id' => 'integer',
        'accounting_voucher_id' => 'integer',
        'amount' => 'float',
        'received_at' => 'datetime',
        'voided_at' => 'datetime',
    ];

    public function order()
    {
        return $this->belongsTo(Order::class);
    }

    public function customer()
    {
        return $this->belongsTo(User::class, 'user_id');
    }

    public function payment()
    {
        return $this->hasOne(OrderPayment::class);
    }

    public function account()
    {
        return $this->belongsTo(AccountingAccount::class, 'account_id');
    }

    public function accountingVoucher()
    {
        return $this->belongsTo(AccountingVoucher::class, 'accounting_voucher_id');
    }

    public function ledgerEntry()
    {
        return $this->hasOne(CustomerLedgerEntry::class, 'order_receipt_id');
    }

    public function createdByAdmin()
    {
        return $this->belongsTo(Admin::class, 'created_by_admin_id');
    }

    public function voidedByAdmin()
    {
        return $this->belongsTo(Admin::class, 'voided_by_admin_id');
    }
}
