From 1c8568927c978bd11c94fa3c4825632399600d65 Mon Sep 17 00:00:00 2001 From: Welton Silva Date: Tue, 28 Apr 2026 19:36:37 -0300 Subject: [PATCH] feat: add date filter and report printing to payments page --- src/renderer/pages/pagamentos.js | 273 ++++++++++++++----------------- 1 file changed, 123 insertions(+), 150 deletions(-) diff --git a/src/renderer/pages/pagamentos.js b/src/renderer/pages/pagamentos.js index e6d5250..5134fed 100644 --- a/src/renderer/pages/pagamentos.js +++ b/src/renderer/pages/pagamentos.js @@ -26,6 +26,22 @@ export async function renderPagamentos(container) { ${tiposPag.map(t => ``).join('')} +
+
+
+ + +
+
+ + +
+ +
+
+ +
+
`; @@ -34,6 +50,14 @@ export async function renderPagamentos(container) { document.getElementById('btn-novo-pag').addEventListener('click', () => abrirModalPagamento(tiposPag, comandas)); document.getElementById('search-pag').addEventListener('input', () => filtrarPagamentos()); document.getElementById('filter-tipo').addEventListener('change', () => filtrarPagamentos()); + document.getElementById('filter-dataini').addEventListener('change', () => filtrarPagamentos()); + document.getElementById('filter-datafim').addEventListener('change', () => filtrarPagamentos()); + document.getElementById('btn-limpar-filtro-data').addEventListener('click', () => { + document.getElementById('filter-dataini').value = ''; + document.getElementById('filter-datafim').value = ''; + filtrarPagamentos(); + }); + document.getElementById('btn-imprimir-relatorio').addEventListener('click', () => imprimirRelatorio()); } let _pagsData = []; @@ -72,159 +96,12 @@ async function loadPagamentos(tiposPag, comandas, clientes) { }, {}); } - renderPagsTable(); + window._pagsFiltered = _pagsData; +renderPagsTableFiltered(_pagsData); } -function renderPagsTable() { +function renderPagsTableFiltered(filtered) { const wrap = document.getElementById('pagamentos-table'); - if (!wrap) return; - - if (!_pagsData.length) { - wrap.innerHTML = `
Nenhum pagamento registrado.
`; - return; - } - - const total = _pagsData.reduce((acc, p) => acc + parseFloat(p.value || 0), 0); - - wrap.innerHTML = ` - - - - - - - - - - - - - ${_pagsData.map(p => { - const cInfo = _cmdMap[String(p.comanda)]; - const clienteNome = _clientesMap[String(p.client)] || p.client_name || '–'; - const cDesc = cInfo ? `${cInfo.name} (${cInfo.mesa})` : (p.comanda_name || '–'); - return ` - - - - - - - - - - `; - }).join('')} - -
#ClienteComandaTipoValorDescriçãoDataAções
#${p.id}${clienteNome} - ${p.comanda ? ` - #${p.comanda} - ${cDesc} - ` : '–'} - ${p.type_pay_name || '–'}R$ ${parseFloat(p.value || 0).toFixed(2)} - ${p.description || '–'} - ${formatDate(p.datetime)} - -
-
- Total exibido: - R$ ${total.toFixed(2)} -
`; - - wrap.querySelectorAll('.btn-view-pag').forEach(btn => - btn.addEventListener('click', () => { - const pag = _pagsData.find(p => String(p.id) === String(btn.dataset.id)); - if (pag) abrirDetalhesPagamento(pag); - }) - ); -} - -function abrirDetalhesPagamento(pagamento) { - const cInfo = _cmdMap[String(pagamento.comanda)]; - const tipoNome = _tiposPag.find(t => String(t.id) === String(pagamento.type_pay))?.name || - _tiposPag.find(t => String(t.id) === String(pagamento.type_pay))?.nome || - pagamento.type_pay_name || '–'; - const clienteNome = _clientesMap[String(pagamento.client)] || pagamento.client_name || '–'; - - openModal({ - title: `💳 Detalhes do Pagamento #${pagamento.id}`, - body: ` -
-
-
-
-
Valor
-
R$ ${parseFloat(pagamento.value || 0).toFixed(2)}
-
-
-
Forma de Pagamento
-
${tipoNome}
-
-
-
- -
-
-
Cliente
-
${clienteNome}
-
-
-
Data/Hora
-
${formatDate(pagamento.datetime)}
-
-
- - ${pagamento.comanda ? ` -
-
Comanda
-
-
-
-
#${pagamento.comanda} — ${cInfo?.name || '–'}
-
${cInfo?.mesa || '–'}
-
- - ${cInfo?.status || '–'} - -
- ${cInfo?.dt_open ? ` -
- Abertura: ${formatDate(cInfo.dt_open)} -
- ` : ''} -
-
- ` : ''} - - ${pagamento.description ? ` -
-
Descrição
-
${pagamento.description}
-
- ` : ''} -
- `, - footer: `` - }); -} - -function filtrarPagamentos() { - const q = document.getElementById('search-pag')?.value.toLowerCase() || ''; - const tipo = parseInt(document.getElementById('filter-tipo')?.value) || null; - - const filtered = _pagsData.filter(p => { - const clienteNome = _clientesMap[String(p.client)] || p.client_name || ''; - const cInfo = _cmdMap[String(p.comanda)]; - const comandaNome = cInfo?.name || p.comanda_name || ''; - const matchQ = !q || - clienteNome.toLowerCase().includes(q) || - comandaNome.toLowerCase().includes(q) || - (p.description || '').toLowerCase().includes(q) || - String(p.id).includes(q); - const matchTipo = !tipo || p.type_pay === tipo; - return matchQ && matchTipo; - }); - const wrap = document.getElementById('pagamentos-table'); if (!wrap) return; @@ -288,6 +165,102 @@ function filtrarPagamentos() { ); } +function imprimirRelatorio() { + const filtered = window._pagsFiltered || _pagsData; + if (!filtered.length) return showToast('Nenhum pagamento para imprimir.', 'warning'); + + const dataIni = document.getElementById('filter-dataini')?.value; + const dataFim = document.getElementById('filter-datafim')?.value; + + const dataIniStr = dataIni ? new Date(dataIni).toLocaleString('pt-BR') : 'Início'; + const dataFimStr = dataFim ? new Date(dataFim).toLocaleString('pt-BR') : 'Agora'; + + const porTipo = {}; + filtered.forEach(p => { + const tipoNome = p.type_pay_name || _tiposPag.find(t => String(t.id) === String(p.type_pay))?.nome || _tiposPag.find(t => String(t.id) === String(p.type_pay))?.name || 'Outro'; + if (!porTipo[tipoNome]) porTipo[tipoNome] = 0; + porTipo[tipoNome] += parseFloat(p.value || 0); + }); + + const totalGeral = filtered.reduce((acc, p) => acc + parseFloat(p.value || 0), 0); + + const htmlRelatorio = ` + + + + + Relatório de Pagamentos + + + +
+
+
RRBEC - Bar & Restaurante
+
Relatório de Pagamentos
+
${dataIniStr} - ${dataFimStr}
+
+ +
+
RESUMO POR TIPO
+ ${Object.entries(porTipo).map(([tipo, valor]) => ` +
+ ${tipo} + R$ ${valor.toFixed(2)} +
+ `).join('')} +
+ +
+
+ TOTAL GERAL: + R$ ${totalGeral.toFixed(2)} +
+
+ + +
+ + + `; + + window.electronAPI.printDirect(htmlRelatorio).then(r => { + if (r.ok) { + showToast('Relatório enviado para impressão!', 'success'); + } else if (r.error === 'NO_PRINTER') { + showToast('Nenhuma impressora configurada.', 'warning', 5000); + const printWindow = window.open('', '', 'width=300,height=400'); + printWindow.document.write(htmlRelatorio); + printWindow.document.close(); + setTimeout(() => printWindow.print(), 300); + } else { + const printWindow = window.open('', '', 'width=300,height=400'); + printWindow.document.write(htmlRelatorio); + printWindow.document.close(); + setTimeout(() => printWindow.print(), 300); + } + }); +} + function abrirModalPagamento(tiposPag, comandas) { const comandasAbertas = comandas.filter(c => c.status === 'OPEN' || c.status === 'PAYING');