diff --git a/src/index.html b/src/index.html index c85c88d..6fe3805 100644 --- a/src/index.html +++ b/src/index.html @@ -1157,7 +1157,7 @@ function saveState() { }; estado.clientes = parseInt(document.getElementById('f-clientes').value) || 0; estado.frango = parseInt(document.getElementById('f-frango').value) || 0; - estado.vendas = parseFloat(document.getElementById('f-vendas').value) || 0; + estado.vendas = parseNum(val('f-vendas')); estado.updatedAt = new Date().toISOString(); try { localStorage.setItem(LS_KEY, JSON.stringify(estado)); } catch(e) {} } @@ -2350,96 +2350,9 @@ function buildRelatorioModal(full) { ${obs ? `
Obs: ${obs}
` : ''} - -
- - - +
`; @@ -2543,59 +2456,201 @@ window.abrirFechamentoEdicao = function(full) { alert('✅ Caixa carregado para edição. Verifique todos os valores antes de enviar.'); }; -// Toggle modo de edição — pede senha admin via modal (não via prompt) +// Abre a tela de edição (modal separado) — requer senha admin window.toggleEditConsulta = async function() { - if (window._editMode) { - // Cancela edição - window._editMode = false; - document.getElementById('edit-section').style.display = 'none'; - document.getElementById('btn-salvar-consulta').style.display = 'none'; - document.getElementById('btn-edit-consulta').textContent = '✏️ Editar (senha)'; - return; - } + const full = window._currentConsulta; + if (!full) return; + // Se já está editando, apenas fecha + if (window._editModalOpen) { closeEditModal(); return; } + + // Pede senha admin primeiro (se configurada) const pw = config.admin_password; - if (!pw) { - window._editMode = true; - document.getElementById('edit-section').style.display = 'block'; - document.getElementById('btn-salvar-consulta').style.display = 'block'; - document.getElementById('btn-edit-consulta').textContent = '✕ Cancelar'; - return; + if (pw) { + const ok = await askPassword(); + if (!ok) return; } - // Modal com senha oculta + openEditModal(full); +}; + +function askPassword() { + return new Promise(resolve => { + const overlay = document.createElement('div'); + overlay.style.cssText = 'position:fixed;top:0;left:0;right:0;bottom:0;background:rgba(0,0,0,0.5);z-index:999999;display:flex;align-items:center;justify-content:center;'; + overlay.id = 'pw-modal'; + overlay.innerHTML = ` +
+
🔐 Senha Admin
+ + +
+ + +
+
`; + document.body.appendChild(overlay); + const inp = document.getElementById('pw-input'); + inp.focus(); + window._pwResolve = resolve; + inp.addEventListener('keydown', e => { if (e.key === 'Enter') document.getElementById('pw-ok-btn').click(); }); + document.getElementById('pw-ok-btn').addEventListener('click', () => { + const val = document.getElementById('pw-input').value; + if (val === pw) { + document.getElementById('pw-modal').remove(); + window._pwResult = true; + window._pwResolve && window._pwResolve(true); + } else { + document.getElementById('pw-error').style.display = 'block'; + inp.value = ''; + inp.focus(); + } + }); + }); +} + +// Modal de edição separado e compacto +function openEditModal(full) { + window._editModalOpen = true; + + const dados = full.dados || {}; + const fmtM = v => parseFloat(v||0).toFixed(2).replace('.', ','); + const fmtMR = v => 'R$ ' + fmtM(v); + const editedInfo = full.edited_at + ? `
+ ⚠️ Última edição: ${full.edited_at.replace('T',' ').substring(0,16)} por ${full.edited_by||'?'} +
` + : ''; + + const troco = parseFloat(full.saldo_troco||0); + const credito = parseFloat(full.saldo_credito||0); + const debito = parseFloat(full.saldo_debito||0); + const alimentacao = parseFloat(full.saldo_alimentacao||0); + const vales = parseFloat(full.saldo_vales||0); + const pixcnpj = parseFloat(full.total_pixcnpj||0); + const areceber = parseFloat(full.saldo_areceber||0); + const clientes = dados.clientes ?? full.clientes ?? 0; + const frango = dados.frango ?? full.frango ?? 0; + const vendas = dados.vendas ?? full.vendas ?? 0; + const saldo_esperado = parseFloat(full.saldo_esperado||0); + const obs = full.observacoes||''; + const overlay = document.createElement('div'); - overlay.style.cssText = 'position:fixed;top:0;left:0;right:0;bottom:0;background:rgba(0,0,0,0.5);z-index:999999;display:flex;align-items:center;justify-content:center;'; - overlay.id = 'pw-modal'; + overlay.id = 'modal-edit'; + overlay.style.cssText = 'position:fixed;top:0;left:0;right:0;bottom:0;background:rgba(0,0,0,0.6);z-index:99998;display:flex;align-items:center;justify-content:center;'; overlay.innerHTML = ` -
-
🔐 Senha Admin
- - -
- - +
+
+

✏️ Editar Fechamento — ${full.loja||''} ${full.data||''}

+ +
+ + ${editedInfo} + +
+
📅 Data: ${full.data||'?'}   Turno: ${full.turno||'?'}   Operador: ${full.operador||'?'}
+
+ + + +
+
+
+
+
+
+
+
+
+
+
+
+ +
+
+
+
+
+
+
+
+ +
+ + +
+ +
+ +
`; + document.body.appendChild(overlay); - const inp = document.getElementById('pw-input'); - inp.focus(); - inp.addEventListener('keydown', e => { if (e.key === 'Enter') document.getElementById('pw-ok-btn').click(); }); - document.getElementById('pw-ok-btn').addEventListener('click', () => { - const val = document.getElementById('pw-input').value; - const err = document.getElementById('pw-error'); - if (val === pw) { - document.getElementById('pw-modal').remove(); - window._editMode = true; - document.getElementById('edit-section').style.display = 'block'; - document.getElementById('btn-salvar-consulta').style.display = 'block'; - document.getElementById('btn-edit-consulta').textContent = '✕ Cancelar'; - } else { - err.style.display = 'block'; - inp.value = ''; - inp.focus(); - } - }); +} + +// Formata campo de dinheiro brasileiro +function fmtBR(inp) { + const v = parseNum(inp.value); + if (isNaN(v) || v === 0) return ''; + return 'R$ ' + v.toLocaleString('pt-BR', { minimumFractionDigits: 2 }); +} + +// Fecha modal de edição +function closeEditModal() { + const m = document.getElementById('modal-edit'); + if (m) document.body.removeChild(m); + window._editModalOpen = false; +} + +// Salva edições do modal de edição separado +window.salvarEdicaoModal = async function() { + const full = window._currentConsulta; + if (!full) return; + + const p = v => parseNum(v); + const inp = id => { const el = document.getElementById(id); return el ? el.value : ''; }; + + const now = new Date().toISOString(); + const operador = estado.operador || full.operador || ''; + + const updates = { + saldo_troco: p(inp('edit-troco')), + saldo_credito: p(inp('edit-credito')), + saldo_debito: p(inp('edit-debito')), + saldo_alimentacao: p(inp('edit-alimentacao')), + saldo_vales: p(inp('edit-vales')), + total_pixcnpj: p(inp('edit-pixcnpj')), + saldo_areceber: p(inp('edit-areceber')), + fechamento_dinheiro: p(inp('edit-fd')), + fechamento_cartoes: p(inp('edit-fc')), + fechamento_areceber: p(inp('edit-fr')), + total_sangrias: p(inp('edit-sangrias')), + total_despesas: p(inp('edit-despesas')), + clientes: parseInt(inp('edit-clientes') || '0'), + frango: parseInt(inp('edit-frango') || '0'), + vendas: p(inp('edit-vendas')), + saldo_esperado: p(inp('edit-esperado')), + observacoes: inp('edit-obs'), + edited_at: now, + edited_by: operador, + }; + + const hasTauri = !!(window.__TAURI__ && window.__TAURI__.core); + if (!hasTauri) { alert('Tauri não disponível.'); return; } + + try { + const id = document.getElementById('edit-id').value || full.id; + if (!id) { alert('ID do registro não encontrado.'); return; } + + await window.__TAURI__.core.invoke('sb_atualizar_fechamento', { id, updates }); + closeEditModal(); + closeRelatorioModal(); + alert('✅ Atualizado com sucesso!'); + } catch(e) { + console.error('Erro ao salvar:', e); + alert('Erro ao salvar: ' + (e?.message || e)); + } }; // Salva edições de volta ao Supabase