<?php

namespace App\Http\Controllers;

use App\Models\Factura;
use Illuminate\Http\Request;
use Illuminate\View\View;

class CorteCajaController extends Controller
{
    public function index(Request $request): View
    {
        $fecha = $request->date('fecha') ?? now();

        $facturas = Factura::with('cliente', 'user')
            ->whereDate('created_at', $fecha)
            ->orderBy('created_at')
            ->get();

        $totales = [
            'efectivo' => $facturas->sum('efectivo'),
            'debito' => $facturas->sum('debito'),
            'credito' => $facturas->sum('credito'),
            'cheque' => $facturas->sum('cheque'),
            'total' => $facturas->sum('total'),
        ];

        return view('cortecaja.index', compact('facturas', 'totales', 'fecha'));
    }
}
