<?php

namespace App\Models;

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

class Receipt extends Model
{
    use HasFactory, SoftDeletes;

    protected $fillable = [
        'receipt_number',
        'invoice_id',
        'customer_id',
        'receipt_date',
        'amount_received',
        'payment_method',
        'notes',
        'signature_path',
        'stamp_path',
    ];

    protected $casts = [
        'receipt_date'    => 'date',
        'amount_received' => 'decimal:2',
    ];

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

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

    /**
     * Generate receipt number otomatis: TTR-2024-0001
     */
    public static function generateNumber(): string
    {
        $year  = date('Y');
        $month = date('m');
        $count = static::whereYear('receipt_date', $year)->count();
        $seq   = str_pad($count + 1, 2, '0', STR_PAD_LEFT);

        return "TT/RJ/{$seq}/{$month}/{$year}";
    }
}
