diff --git a/src/services/accounting-service.js b/src/services/accounting-service.js index 39ccb18..423f27e 100644 --- a/src/services/accounting-service.js +++ b/src/services/accounting-service.js @@ -392,6 +392,8 @@ async function getTaxSummary({ startDate, endDate, accountingMethod = 'Accrual' const whereClause = `TxnDate >= '${startDate}' AND TxnDate <= '${endDate}'`; const agg = {}; + let totalSales = 0; + let totalTaxable = 0; for (const entity of ['Invoice', 'SalesReceipt']) { try { @@ -401,8 +403,14 @@ async function getTaxSummary({ startDate, endDate, accountingMethod = 'Accrual' const docs = data?.QueryResponse?.[entity] || []; for (const doc of docs) { const taxDetail = doc.TxnTaxDetail; + const totalAmt = parseFloat(doc.TotalAmt) || 0; + const totalTax = taxDetail ? (parseFloat(taxDetail.TotalTax) || 0) : 0; + totalSales += (totalAmt - totalTax); + if (!taxDetail || !taxDetail.TaxLine) continue; + const taxLines = Array.isArray(taxDetail.TaxLine) ? taxDetail.TaxLine : [taxDetail.TaxLine]; + let docTaxable = 0; for (const line of taxLines) { const detail = line.TaxLineDetail; if (!detail || !detail.TaxRateRef) continue; @@ -412,13 +420,16 @@ async function getTaxSummary({ startDate, endDate, accountingMethod = 'Accrual' if (!agg[rateId]) agg[rateId] = { taxableSales: 0, taxCollected: 0 }; agg[rateId].taxableSales += taxable; agg[rateId].taxCollected += collected; + docTaxable = Math.max(docTaxable, taxable); } + totalTaxable += docTaxable; } } catch (e) { console.error(`${entity} query failed:`, e.message); } } + const nontaxableSales = totalSales - totalTaxable; const rows = Object.entries(agg).map(([rateId, amounts]) => { const info = rateMap[rateId] || { name: `Tax Rate ${rateId}`, rateValue: 0 }; const ratePct = info.rateValue.toFixed(3).replace(/0+$/, '').replace(/\.$/, ''); @@ -429,12 +440,16 @@ async function getTaxSummary({ startDate, endDate, accountingMethod = 'Accrual' { value: info.name }, { value: '' }, { value: '' }, + { value: '' }, + { value: '' }, { value: `${ratePct}%` } ] }, Summary: { ColData: [ { value: 'Total' }, + { value: totalSales.toFixed(2) }, + { value: nontaxableSales.toFixed(2) }, { value: amounts.taxableSales.toFixed(2) }, { value: amounts.taxCollected.toFixed(2) }, { value: `${ratePct}%` } @@ -451,6 +466,27 @@ async function getTaxSummary({ startDate, endDate, accountingMethod = 'Accrual' return na.localeCompare(nb); }); + const grandTaxCollected = Object.values(agg).reduce((s, a) => s + a.taxCollected, 0); + rows.push({ + type: 'Section', + Header: { + ColData: [ + { value: 'GRAND TOTAL' }, + { value: '' }, { value: '' }, { value: '' }, { value: '' }, { value: '' } + ] + }, + Summary: { + ColData: [ + { value: 'Total' }, + { value: totalSales.toFixed(2) }, + { value: nontaxableSales.toFixed(2) }, + { value: totalTaxable.toFixed(2) }, + { value: grandTaxCollected.toFixed(2) }, + { value: '' } + ] + } + }); + return { Header: { ReportName: 'Sales Tax Liability', @@ -461,7 +497,9 @@ async function getTaxSummary({ startDate, endDate, accountingMethod = 'Accrual' Columns: { Column: [ { ColTitle: '', ColType: 'Text' }, - { ColTitle: 'Taxable Sales', ColType: 'Money' }, + { ColTitle: 'Total Sales', ColType: 'Money' }, + { ColTitle: 'Nontaxable', ColType: 'Money' }, + { ColTitle: 'Taxable', ColType: 'Money' }, { ColTitle: 'Tax Collected', ColType: 'Money' }, { ColTitle: 'Tax Rate', ColType: 'Percent' } ]