<?php

namespace App\Models\Stats;

use Illuminate\Database\Eloquent\Model;

class StatFinanceDaily extends Model
{
    protected $connection = 'sam_stat';

    protected $table = 'stat_finance_daily';

    protected $casts = [
        'stat_date' => 'date',
        'deposit_count' => 'integer',
        'deposit_amount' => 'decimal:2',
        'withdrawal_count' => 'integer',
        'withdrawal_amount' => 'decimal:2',
        'net_cashflow' => 'decimal:2',
        'bank_balance_total' => 'decimal:2',
        'receivable_balance' => 'decimal:2',
        'payable_balance' => 'decimal:2',
    ];

    public function scopeForTenant($query, ?int $tenantId)
    {
        if ($tenantId) {
            return $query->where('tenant_id', $tenantId);
        }

        return $query;
    }

    public function scopeForDateRange($query, string $from, string $to)
    {
        return $query->whereBetween('stat_date', [$from, $to]);
    }

    public function scopeForDate($query, string $date)
    {
        return $query->where('stat_date', $date);
    }
}
