fix(invoicing): wire emisor config save/load and align fiscal config keys
Some checks failed
CI / lint-and-test (3.11) (push) Has been cancelled
CI / lint-and-test (3.13) (push) Has been cancelled

This commit is contained in:
2026-07-06 23:49:23 +00:00
parent 07dad0801e
commit a8e748e08f
4 changed files with 80 additions and 8 deletions

View File

@@ -62,7 +62,10 @@ const Invoicing = (() => {
if (name === 'notas') loadNotas();
if (name === 'complementos') loadComplementos();
if (name === 'cancelaciones') loadCancelaciones();
if (name === 'config') loadFacturapiStatus();
if (name === 'config') {
loadFacturapiStatus();
loadEmisorData();
}
}
// ---- Badge helpers ----
@@ -377,6 +380,70 @@ const Invoicing = (() => {
}
}
// ---- Emisor data (config tab) ----
async function loadEmisorData() {
const rfcEl = document.getElementById('rfc-emisor');
const cpEl = document.getElementById('cp-fiscal');
const razonEl = document.getElementById('razon-social');
const regimenEl = document.getElementById('regimen-fiscal');
if (!rfcEl || !cpEl || !razonEl || !regimenEl) return;
try {
const res = await fetch('/pos/api/config/business', { headers: headers() });
if (!res.ok) throw new Error('Error al cargar datos fiscales');
const data = await res.json();
rfcEl.value = data.rfc || '';
cpEl.value = data.cp || '';
razonEl.value = data.razon_social || '';
const regimen = data.regimen_fiscal || '601';
regimenEl.value = regimen + ' — ' + (regimenEl.querySelector('option[value^="' + regimen + '"')?.textContent.split('—')[1]?.trim() || '');
// If exact value not matched, leave first option selected by SAT code prefix
if (!regimenEl.value.startsWith(regimen)) {
Array.from(regimenEl.options).forEach(function(opt) {
if (opt.value.startsWith(regimen)) opt.selected = true;
});
}
} catch (e) {
console.error('Invoicing.loadEmisorData:', e);
}
}
async function saveEmisorData() {
const rfc = (document.getElementById('rfc-emisor')?.value || '').trim();
const cp = (document.getElementById('cp-fiscal')?.value || '').trim();
const razon_social = (document.getElementById('razon-social')?.value || '').trim();
const regimenValue = document.getElementById('regimen-fiscal')?.value || '601';
const regimen_fiscal = regimenValue.split('—')[0].trim();
const statusEl = document.getElementById('emisor-save-status');
if (!rfc || !razon_social || !cp) {
if (statusEl) statusEl.textContent = 'RFC, Razón Social y C.P. son obligatorios';
return;
}
try {
const res = await fetch('/pos/api/config/business', {
method: 'PUT',
headers: headers(),
body: JSON.stringify({ rfc, razon_social, regimen_fiscal, cp, cfdi_regimen_fiscal: regimen_fiscal, cfdi_serie: 'A' })
});
if (!res.ok) {
const err = await res.json().catch(() => ({ error: res.statusText }));
throw new Error(err.error || 'Error al guardar');
}
if (statusEl) {
statusEl.textContent = 'Datos fiscales guardados';
statusEl.style.color = 'var(--color-success)';
}
setTimeout(() => { if (statusEl) { statusEl.textContent = ''; statusEl.style.color = ''; } }, 4000);
} catch (e) {
if (statusEl) {
statusEl.textContent = e.message;
statusEl.style.color = 'var(--color-error)';
}
}
}
async function setupFacturapi(btn) {
if (!btn) return;
btn.disabled = true;
@@ -747,6 +814,7 @@ const Invoicing = (() => {
return {
switchTab, loadFacturas, loadNotas, loadComplementos, loadCancelaciones, loadFacturapiStatus,
loadEmisorData, saveEmisorData,
showDetail, showCancelModal, confirmCancel, processQueue,
showNewInvoiceModal, closeNewInvoiceModal, submitNewInvoice, notaCreditoPlaceholder,
openGlobalInvoiceModal, previewGlobalInvoice, generateGlobalInvoice, setupFacturapi,