<?php
require_once __DIR__ . '/../config/database.php';

class CashClosureController {

    private $conn;

    public function __construct() {
        $this->conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
        if ($this->conn->connect_error) {
            die("Error de conexión: " . $this->conn->connect_error);
        }
    }

    public function createClosure($userId) {
        $result = $this->conn->query("SELECT payment_method, SUM(total) as total FROM sales WHERE DATE(created_at) = CURDATE() GROUP BY payment_method");
        
        $totals = [
            'efectivo' => 0,
            'transferencia' => 0,
            'tarjeta' => 0,
        ];

        while ($row = $result->fetch_assoc()) {
            $totals[$row['payment_method']] = $row['total'];
        }

        $stmt = $this->conn->prepare("INSERT INTO cash_closures (user_id, total_efectivo, total_transferencia, total_tarjeta) VALUES (?, ?, ?, ?)");
        $stmt->bind_param("iddd", $userId, $totals['efectivo'], $totals['transferencia'], $totals['tarjeta']);
        
        if ($stmt->execute()) {
            return true;
        } else {
            return false;
        }
    }
}
?>