<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class DayLedger extends Model
{
    public const STATUS_OPEN = 'open';
    public const STATUS_CLOSED = 'closed';

    protected $fillable = [
        'date',
        'location_id',
        'cash_register_id',
        'opening_balance',
        'total_sales',
        'total_collection',
        'total_expense',
        'total_refund',
        'closing_balance',
        'status',
        'closed_by',
        'closed_at',
    ];

    protected function casts(): array
    {
        return [
            'date' => 'date',
            'opening_balance' => 'decimal:2',
            'total_sales' => 'decimal:2',
            'total_collection' => 'decimal:2',
            'total_expense' => 'decimal:2',
            'total_refund' => 'decimal:2',
            'closing_balance' => 'decimal:2',
            'closed_at' => 'datetime',
        ];
    }

    public function closedBy(): BelongsTo
    {
        return $this->belongsTo(User::class, 'closed_by');
    }

    public function location(): BelongsTo
    {
        return $this->belongsTo(Location::class);
    }

    public function isClosed(): bool
    {
        return $this->status === self::STATUS_CLOSED;
    }
}
