diff --git a/services/billingSendService.js b/services/billingSendService.js index 8f86876..99baa4c 100644 --- a/services/billingSendService.js +++ b/services/billingSendService.js @@ -40,8 +40,8 @@ function spintax(t) { }); } -function buildMessage(tplRaw, c, inv, qr) { - const amount = Number(inv.amount || 0); +function buildMessage(tplRaw, c, inv, qr, totalAmount) { + const amount = Number(totalAmount || inv.amount || 0); const qrisAmount = Number((qr && qr.amount) || 0) || amount; const qrisCode = Number((qr && qr.uniqueCode) || 0) || 0; const qrisAttached = !!(qr && qr.payload); @@ -77,7 +77,8 @@ function buildTargets(isolateDay) { if (!invs.length) continue; if (!c.phone || String(c.phone).replace(/\D/g, '').length < 9) continue; // invoice terurut dari periode terlama; tagih yang paling lama dulu - targets.push({ c, inv: invs[0] }); + const total = invs.reduce((sum, row) => sum + (Number(row.amount) || 0), 0); + targets.push({ c, inv: invs[0], total }); } return targets; } @@ -118,10 +119,10 @@ async function start(opts = {}) { (async () => { for (let i = 0; i < targets.length; i++) { if (STATUS.stopped) break; - const { c, inv } = targets[i]; + const { c, inv, total } = targets[i]; try { - const qr = (isQris && ensureInvoiceQris) ? (ensureInvoiceQris(inv) || {}) : null; - const caption = buildMessage(tplRaw, c, inv, qr); + const qr = (isQris && ensureInvoiceQris) ? (ensureInvoiceQris(inv, total) || {}) : null; + const caption = buildMessage(tplRaw, c, inv, qr, total); if (isQris && QRCode && qr && qr.payload) { const url = await QRCode.toDataURL(qr.payload, { errorCorrectionLevel: 'M', margin: 1, width: 520 }); const buf = Buffer.from(String(url).split(',')[1], 'base64'); diff --git a/services/invoiceQrisService.js b/services/invoiceQrisService.js index b135a77..892b9da 100644 --- a/services/invoiceQrisService.js +++ b/services/invoiceQrisService.js @@ -30,10 +30,11 @@ function isQrisAmountTaken(amount, opts = {}) { return false; } -function ensureInvoiceQris(inv) { +function ensureInvoiceQris(inv, baseAmountOverride) { const settings = getSettings() || {}; const invId = Number((inv && inv.id) || 0); - const base = Number((inv && inv.amount) || 0); + // baseAmountOverride dipakai bila nominal QRIS = total beberapa tagihan + const base = Number(baseAmountOverride || (inv && inv.amount) || 0); if (!invId || base <= 0) return { uniqueCode: 0, amount: base, payload: '' }; let code = Number(inv.qris_unique_code || 0);