<?php
require '../../conexao/conexao.php';

class Caixa
{
    private $connectionmysql;
    private $connection;

    public function __construct()
    {
        global $connectionmysql;
        global $connection;
        $this->connectionmysql = $connectionmysql;
        $this->connection = $connection;
    }

    public function CaixaTotal($dataInicial, $dataFinal, $tipo, $filial)
    {
        $params = []; // <-- Inicializa os parâmetros

        $manipulado = "
        SELECT
            z.cdconre AS FILIAL,
            0 AS VALOR_REVENDA,
            0 AS VALOR_MARCA_PROPRIA,
            SUM(z.soma) AS VALOR_MANIPULADO
        FROM (
            SELECT c.dtope, (c.vrcxa - c.vrdscv) AS soma, r.cdconre, r.nrrqu
            FROM fc31110 c
            INNER JOIN FC12100 r ON c.cdfilr = r.cdfil AND c.cdpro = r.nrrqu
            WHERE c.dtope BETWEEN ? AND ?
            GROUP BY c.dtope, r.cdconre, r.nrrqu, soma
        ) z
        GROUP BY z.cdconre";

        $revenda = "
        SELECT
            a.cdconre AS FILIAL,
            SUM(b.vrliq + b.vrtxag - b.vrdscv) AS VALOR_REVENDA,
            0 AS VALOR_MARCA_PROPRIA,
            0 AS VALOR_MANIPULADO
        FROM FC31100 a
        JOIN FC31110 b ON a.cdfil = b.cdfil AND a.cdtml = b.cdtml AND a.operid = b.operid AND a.nrcpm = b.nrcpm
        JOIN FC03000 p ON p.cdpro = b.cdpro
        WHERE a.dtope BETWEEN ? AND ?
            AND b.tpitm = 'V' AND b.tppag NOT IN ('2','3')
            AND p.setor NOT IN (276, 330)
        GROUP BY a.cdconre";

        $marcaPropria = "
        SELECT
            a.cdconre AS FILIAL,
            0 AS VALOR_REVENDA,
            SUM(b.vrliq + b.vrtxag - b.vrdscv) AS VALOR_MARCA_PROPRIA,
            0 AS VALOR_MANIPULADO
        FROM FC31100 a
        JOIN FC31110 b ON a.cdfil = b.cdfil AND a.cdtml = b.cdtml AND a.operid = b.operid AND a.nrcpm = b.nrcpm
        JOIN FC03000 p ON p.cdpro = b.cdpro
        WHERE a.dtope BETWEEN ? AND ?
            AND b.tpitm = 'V' AND b.tppag NOT IN ('2','3')
            AND p.setor IN (276, 330)
        GROUP BY a.cdconre";

        $params[] = $dataInicial;
        $params[] = $dataFinal;

        $sql = "SELECT FILIAL, SUM(VALOR_MANIPULADO) AS VALOR_MANIPULADO, SUM(VALOR_REVENDA) AS VALOR_REVENDA, SUM(VALOR_MARCA_PROPRIA) AS VALOR_MARCA_PROPRIA FROM (";

        switch (strtolower(trim($tipo))) {
            case 'manipulado':
                $sql .= $manipulado;
                break;
            case 'revenda':
                $sql .= $revenda;
                break;
            case 'marca propria':
            case 'marca própria':
                $sql .= $marcaPropria;
                break;
            default:
                $sql .= "$manipulado UNION ALL $revenda UNION ALL $marcaPropria";
                $params[] = $dataInicial;
                $params[] = $dataFinal;
                $params[] = $dataInicial;
                $params[] = $dataFinal;
                break;
        }

        $sql .= ") base WHERE 1=1";

        if (!empty($filial) && $filial !== "Todos") {
            if (!is_array($filial)) {
                $filial = explode(',', $filial);
            }
            $filial = array_filter($filial, 'is_numeric');
            if (count($filial) > 0) {
                $placeholders = implode(',', array_fill(0, count($filial), '?'));
                $sql .= " AND base.FILIAL IN ($placeholders)";
                $params = array_merge($params, array_map('intval', $filial));
            }
        }

        $sql .= " GROUP BY base.FILIAL ORDER BY base.FILIAL";

        try {
            $stmt = ibase_prepare($this->connection, $sql);
            $result = ibase_execute($stmt, ...$params);

            if (!$result) return false;

            $data = [];
            while ($row = ibase_fetch_assoc($result)) {
                $data[] = $row;
            }

            return $data;
        } catch (Exception $e) {
            return false;
        }
    }
}
