Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| fea957dfb9 | |||
| 130b2b6da7 |
+60
-56
@@ -1279,10 +1279,10 @@ function buildConfirmHTML(data) {
|
||||
const al = cartAli_arr[i] || 0;
|
||||
const px = cartPix_arr[i] || 0;
|
||||
cartTableRows += `<tr>
|
||||
<td>${cr > 0 ? fmt(cr) : ''}</td>
|
||||
<td>${db > 0 ? fmt(db) : ''}</td>
|
||||
<td>${al > 0 ? fmt(al) : ''}</td>
|
||||
<td>${px > 0 ? fmt(px) : ''}</td>
|
||||
<td>${cr > 0 ? fmt(cr) : '-'}</td>
|
||||
<td>${db > 0 ? fmt(db) : '-'}</td>
|
||||
<td>${al > 0 ? fmt(al) : '-'}</td>
|
||||
<td>${px > 0 ? fmt(px) : '-'}</td>
|
||||
</tr>`;
|
||||
}
|
||||
cartTableRows += `<tr class="total-row">
|
||||
@@ -1487,10 +1487,10 @@ function buildPrintHTML(data) {
|
||||
const al = cartAli_arr[i] || 0;
|
||||
const px = cartPix_arr[i] || 0;
|
||||
cartTableRows += `<tr>
|
||||
<td>${cr > 0 ? fmt(cr) : ''}</td>
|
||||
<td>${db > 0 ? fmt(db) : ''}</td>
|
||||
<td>${al > 0 ? fmt(al) : ''}</td>
|
||||
<td>${px > 0 ? fmt(px) : ''}</td>
|
||||
<td>${cr > 0 ? fmt(cr) : '-'}</td>
|
||||
<td>${db > 0 ? fmt(db) : '-'}</td>
|
||||
<td>${al > 0 ? fmt(al) : '-'}</td>
|
||||
<td>${px > 0 ? fmt(px) : '-'}</td>
|
||||
</tr>`;
|
||||
}
|
||||
// Totals row
|
||||
@@ -1687,8 +1687,9 @@ function triggerWindowPrint() {
|
||||
}
|
||||
|
||||
// ─── Recibo 80mm via Tauri (ESC/POS USB) ─────────────────────────────────
|
||||
async function tauriRecibo() {
|
||||
// Build flat totals for the Rust ReciboData struct
|
||||
// ─── Recibo 80mm via Tauri (ESC/POS USB) ─────────────────────────────────
|
||||
// Build data object shared by both ESC/POS and preview
|
||||
function buildReciboData() {
|
||||
const cartCred = estado.cartoes.credito.reduce((s,r) => s+(r.valor||0), 0);
|
||||
const cartDeb = estado.cartoes.debito.reduce((s,r) => s+(r.valor||0), 0);
|
||||
const cartAli = estado.cartoes.alimentacao.reduce((s,r) => s+(r.valor||0), 0);
|
||||
@@ -1703,7 +1704,7 @@ async function tauriRecibo() {
|
||||
const saldoEsp = parseNum(val('f-saldo-esperado'));
|
||||
// SYS total = troco + crédito + débito + alimentação + vales + receber + pixCartão + pixCnpj
|
||||
const sysTotal = parseNum(val('sys-troco')) + cartCred + cartDeb + cartAli + valesTotal + receberTotal + cartPix + pixCnpjTotal;
|
||||
const data = {
|
||||
return {
|
||||
uuid: estado.uuid,
|
||||
id_local: estado.id_local,
|
||||
loja: estado.loja,
|
||||
@@ -1715,6 +1716,9 @@ async function tauriRecibo() {
|
||||
fechamento: fechamentoCounted,
|
||||
diferenca: sysTotal - saldoEsp,
|
||||
sys_total: sysTotal,
|
||||
fechamento_dinheiro: parseNum(val('f-fech-dinheiro')),
|
||||
fechamento_cartoes: parseNum(val('f-fech-cartoes')),
|
||||
fechamento_receber: parseNum(val('f-fech-receber')),
|
||||
// Flat totals for ReciboData struct (Rust) — must be numbers
|
||||
credito: cartCred,
|
||||
debito: cartDeb,
|
||||
@@ -1726,25 +1730,31 @@ async function tauriRecibo() {
|
||||
vales: valesTotal,
|
||||
areceber: receberTotal,
|
||||
cancelamentos: cancelTotal,
|
||||
// Full arrays for backend/storage
|
||||
// Full arrays for preview
|
||||
despesas_arr: estado.despesas.map(r => ({ desc: r.desc||'', obs: r.obs||'', valor: r.valor||0 })),
|
||||
sangrias_arr: estado.sangrias.map(r => ({ hora: r.hora||'', retirada: parseNum(r.retirada)||0, gerente: r.gerente||'' })),
|
||||
pixcnpj_arr: estado.pixcnpj.map(r => ({ nome: r.nome||'', valor: r.valor||0 })),
|
||||
vales_arr: estado.vales.map(r => ({ nome: r.nome||'', obs: r.obs||'', valor: r.valor||0 })),
|
||||
receber_arr: estado.receber.map(r => ({ nome: r.nome||'', valor: r.valor||0 })),
|
||||
cancelamentos_arr: estado.cancelamentos.map(r => ({ numero: r.numero||'', valor: r.valor||0, motivo: r.motivo||'' })),
|
||||
cartoes: { credito: cartCred, debito: cartDeb, alimentacao: cartAli, pix: cartPix },
|
||||
cartoes_credito: estado.cartoes.credito.map(r => r.valor || 0),
|
||||
cartoes_debito: estado.cartoes.debito.map(r => r.valor || 0),
|
||||
cartoes_alimentacao: estado.cartoes.alimentacao.map(r => r.valor || 0),
|
||||
cartoes_pix: estado.cartoes.pix.map(r => r.valor || 0),
|
||||
observacoes: estado.observacoes,
|
||||
sync_status: estado.sync_status,
|
||||
};
|
||||
}
|
||||
|
||||
// Tenta ESC/POS via Tauri primeiro; se falhar, abre preview
|
||||
async function tauriRecibo() {
|
||||
const data = buildReciboData();
|
||||
|
||||
// Tenta ESC/POS via Tauri primeiro; se falhar, so lanca erro (preview tratado pelo caller)
|
||||
try {
|
||||
return await window.__TAURI__.core.invoke('imprimir_recibo', { data: JSON.stringify(data) });
|
||||
} catch(e) {
|
||||
console.warn('ESC/POS falhou, abrindo preview:', e);
|
||||
openPrintPreview(data);
|
||||
throw e;
|
||||
console.warn('ESC/POS falhou:', e);
|
||||
throw e; // re-lanca para o caller tratar o preview
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1818,39 +1828,17 @@ document.getElementById('modal-confirm').addEventListener('click', e => { if (e.
|
||||
|
||||
// Print from confirm modal — abre preview idêntico ao modal de confirmação
|
||||
document.getElementById('confirm-print').addEventListener('click', async () => {
|
||||
// Usa buildReciboData para não duplicar lógica e ter todos os campos certos
|
||||
let data;
|
||||
try {
|
||||
data = buildReciboData();
|
||||
} catch(e) {
|
||||
console.error('Erro ao montar dados do recibo:', e);
|
||||
alert('Erro ao montar dados do recibo.');
|
||||
return;
|
||||
}
|
||||
// Fecha modal APÓS gerar preview (não antes)
|
||||
closeConfirmModal();
|
||||
const cartCred = estado.cartoes.credito.reduce((s,r) => s+(r.valor||0), 0);
|
||||
const cartDeb = estado.cartoes.debito.reduce((s,r) => s+(r.valor||0), 0);
|
||||
const cartAli = estado.cartoes.alimentacao.reduce((s,r) => s+(r.valor||0), 0);
|
||||
const cartPix = estado.cartoes.pix.reduce((s,r) => s+(r.valor||0), 0);
|
||||
const fechamentoCounted = parseNum(val('f-fech-dinheiro')) + parseNum(val('f-fech-cartoes')) + parseNum(val('f-fech-receber'));
|
||||
const saldoEsp = parseNum(val('f-saldo-esperado'));
|
||||
const sysTotal = (parseNum(val('sys-troco')) || 0) + cartCred + cartDeb + cartAli + cartPix + vales + receber + pixcnpj;
|
||||
const diferenca = sysTotal - saldoEsp;
|
||||
const data = {
|
||||
loja: estado.loja, data: estado.data, operador: estado.operador, turno: estado.turno,
|
||||
saldo_troco: parseNum(val('sys-troco')) || 0,
|
||||
saldo_esperado: saldoEsp,
|
||||
fechamento_dinheiro: parseNum(val('f-fech-dinheiro')),
|
||||
fechamento_cartoes: parseNum(val('f-fech-cartoes')),
|
||||
fechamento_receber: parseNum(val('f-fech-receber')),
|
||||
fechamento: fechamentoCounted,
|
||||
diferenca: diferenca,
|
||||
sys_total: sysTotal,
|
||||
despesas_arr: estado.despesas.map(r => ({ desc: r.desc||'', obs: r.obs||'', valor: r.valor||0 })),
|
||||
sangrias_arr: estado.sangrias.map(r => ({ hora: r.hora||'', retirada: parseNum(r.retirada)||0, gerente: r.gerente||'' })),
|
||||
pixcnpj_arr: estado.pixcnpj.map(r => ({ nome: r.nome||'', valor: r.valor||0 })),
|
||||
vales_arr: estado.vales.map(r => ({ nome: r.nome||'', obs: r.obs||'', valor: r.valor||0 })),
|
||||
receber_arr: estado.receber.map(r => ({ nome: r.nome||'', valor: r.valor||0 })),
|
||||
cancelamentos_arr: estado.cancelamentos.map(r => ({ numero: r.numero||'', valor: r.valor||0, motivo: r.motivo||'' })),
|
||||
credito: cartCred, debito: cartDeb, alimentacao: cartAli,
|
||||
voucher: cartPix,
|
||||
cartoes_credito: estado.cartoes.credito.map(r => r.valor || 0),
|
||||
cartoes_debito: estado.cartoes.debito.map(r => r.valor || 0),
|
||||
cartoes_alimentacao: estado.cartoes.alimentacao.map(r => r.valor || 0),
|
||||
cartoes_pix: estado.cartoes.pix.map(r => r.valor || 0),
|
||||
observacoes: estado.observacoes,
|
||||
};
|
||||
openPrintPreview(data, 'confirm');
|
||||
});
|
||||
|
||||
@@ -1895,13 +1883,19 @@ document.getElementById('btn-recibo').addEventListener('click', async () => {
|
||||
const btn = document.getElementById('btn-recibo');
|
||||
btn.disabled = true;
|
||||
try {
|
||||
// Tenta ESC/POS primeiro; se falhar, abre preview no navegador
|
||||
// Tenta ESC/POS primeiro; se falhar, nada acontece visualmente
|
||||
// — siempre abre preview depois para o usuário ver algo
|
||||
await tauriRecibo();
|
||||
// Se chegou aqui sem erro, ESC/POS funcionou
|
||||
} catch(e) {
|
||||
// ESC/POS falhou — openPrintPreview já foi chamado dentro de tauriRecibo
|
||||
// Só mostra alerta se nem o preview abriu
|
||||
console.warn('Recibo:', e);
|
||||
console.warn('ESC/POS falhou:', e);
|
||||
}
|
||||
// Sempre abre preview depois da tentativa ESC/POS
|
||||
try {
|
||||
const data = buildReciboData();
|
||||
openPrintPreview(data, 'recibo');
|
||||
} catch(e2) {
|
||||
console.error('Preview falhou:', e2);
|
||||
alert('Erro ao gerar preview do recibo.');
|
||||
} finally {
|
||||
btn.disabled = false;
|
||||
}
|
||||
@@ -1936,9 +1930,12 @@ if (btnAnterior) {
|
||||
console.log('[DEBUG] btn-anterior clicado');
|
||||
const loja = val('f-loja') || 'União';
|
||||
console.log('[DEBUG] btn-anterior loja:', loja);
|
||||
const hasTauri = !!(window.__TAURI__ && window.__TAURI__.core);
|
||||
|
||||
// Salva rascunho atual antes de tudo
|
||||
await tauriSaveLocal();
|
||||
if (hasTauri) {
|
||||
try { await window.__TAURI__.core.invoke('salvar_rascunho_local', {}); } catch(e) { console.warn('salvar_rascunho_local:', e); }
|
||||
}
|
||||
|
||||
// Calcula dia anterior
|
||||
const prevDate = new Date(estado.data);
|
||||
@@ -1954,12 +1951,16 @@ if (btnAnterior) {
|
||||
|
||||
// Carrega lista de fechamentos recentes do Supabase com uuid
|
||||
let recentList = [];
|
||||
if (hasTauri) {
|
||||
try {
|
||||
recentList = await window.__TAURI__.core.invoke('sb_listar_recentes', { loja, limite: 20 });
|
||||
console.log('[DEBUG] sb_listar_recentes retornou:', recentList?.length, 'itens');
|
||||
} catch(e) {
|
||||
console.warn('sb_listar_recentes falhou:', e);
|
||||
}
|
||||
} else {
|
||||
console.warn('Tauri não disponível, pulando sb_listar_recentes');
|
||||
}
|
||||
|
||||
// Extrai datas únicas ordenadas + mapeamento data→id
|
||||
const dateIdMap = {};
|
||||
@@ -2003,6 +2004,7 @@ if (btnAnterior) {
|
||||
|
||||
// Tenta Supabase primeiro (com id se disponível), senão local
|
||||
let rascunho = null;
|
||||
if (hasTauri) {
|
||||
if (selectedId) {
|
||||
try {
|
||||
rascunho = await window.__TAURI__.core.invoke('sb_buscar_por_id', { id: selectedId });
|
||||
@@ -2015,13 +2017,15 @@ if (btnAnterior) {
|
||||
console.log('[DEBUG] sb_carregar_rascunho retornou:', !!rascunho);
|
||||
} catch(e) { console.warn('sb_carregar_rascunho falhou:', e); }
|
||||
}
|
||||
|
||||
if (!rascunho) {
|
||||
try {
|
||||
rascunho = await window.__TAURI__.core.invoke('carregar_rascunho', { loja, data: selectedDate });
|
||||
console.log('[DEBUG] carregar_rascunho local retornou:', !!rascunho);
|
||||
} catch(e) { console.warn('carregar_rascunho local falhou:', e); }
|
||||
}
|
||||
} else {
|
||||
console.warn('Tauri não disponível, não foi possível carregar rascunho');
|
||||
}
|
||||
|
||||
if (rascunho) {
|
||||
console.log('[DEBUG] rascunho carregado com sucesso');
|
||||
|
||||
Reference in New Issue
Block a user