diff --git a/app.js b/app.js index 14ea37a..dd92abb 100644 --- a/app.js +++ b/app.js @@ -46,7 +46,8 @@ let wizardSelectedSkills = new Set(); let wizardSelectedIntegrations = new Set(); let wizardFieldValues = {}; // { stepIndex: { fieldKey: value } } let wizardIntegrationStep = 0; // current index in fields phase -let wizardSelectedIntegrationList = []; // ordered list of selected integration objects +let wizardSelectedIntegrationList = []; +let invoicesData = []; // cached full invoice list for filtering/export /* ─── Debug logger ─────────────────────────────────────────── */ function dbg(label, data) { @@ -1196,7 +1197,10 @@ async function loadInvoices() { } else { data = []; } - renderInvoices(data); + invoicesData = data; + applyInvoiceFilter(); + const filterInput = document.getElementById("invoice-filter"); + if (filterInput) filterInput.value = ""; } catch (err) { dbg("\u2190 Invoices error", err.message); body.innerHTML = @@ -1204,6 +1208,22 @@ async function loadInvoices() { } } +function applyInvoiceFilter() { + const q = ( + document.getElementById("invoice-filter").value || "" + ).toLowerCase(); + const filtered = q + ? invoicesData.filter( + (inv) => + (inv.date || "").toLowerCase().includes(q) || + (inv.uuid || "").toLowerCase().includes(q) || + (inv.product || "").toLowerCase().includes(q) || + (inv.price || "").toLowerCase().includes(q), + ) + : invoicesData; + renderInvoices(filtered); +} + function renderInvoices(data) { const body = document.getElementById("invoices-body"); if (!data.length) { @@ -1233,6 +1253,42 @@ function renderInvoices(data) { `; } +function exportInvoicesCSV() { + const q = ( + document.getElementById("invoice-filter").value || "" + ).toLowerCase(); + const filtered = q + ? invoicesData.filter( + (inv) => + (inv.date || "").toLowerCase().includes(q) || + (inv.uuid || "").toLowerCase().includes(q) || + (inv.product || "").toLowerCase().includes(q) || + (inv.price || "").toLowerCase().includes(q), + ) + : invoicesData; + if (!filtered.length) { + toast("No invoices to export.", "warning"); + return; + } + const rows = ["Date,Agent,Product,Price"]; + for (const inv of filtered) { + rows.push( + [inv.date || "", inv.uuid || "", inv.product || "", inv.price || ""].join( + ",", + ), + ); + } + const blob = new Blob([rows.join("\n")], { type: "text/csv" }); + const url = URL.createObjectURL(blob); + const a = document.createElement("a"); + a.href = url; + a.download = "invoices.csv"; + document.body.appendChild(a); + a.click(); + document.body.removeChild(a); + URL.revokeObjectURL(url); +} + /* ═══════════════════════════════════════════════════════════════ RESTART ═══════════════════════════════════════════════════════════════ */ diff --git a/index.html b/index.html index 5f73ec3..8b15285 100644 --- a/index.html +++ b/index.html @@ -937,7 +937,25 @@