fix
This commit is contained in:
@@ -392,6 +392,8 @@ async function getTaxSummary({ startDate, endDate, accountingMethod = 'Accrual'
|
|||||||
|
|
||||||
const whereClause = `TxnDate >= '${startDate}' AND TxnDate <= '${endDate}'`;
|
const whereClause = `TxnDate >= '${startDate}' AND TxnDate <= '${endDate}'`;
|
||||||
const agg = {};
|
const agg = {};
|
||||||
|
let totalSales = 0;
|
||||||
|
let totalTaxable = 0;
|
||||||
|
|
||||||
for (const entity of ['Invoice', 'SalesReceipt']) {
|
for (const entity of ['Invoice', 'SalesReceipt']) {
|
||||||
try {
|
try {
|
||||||
@@ -401,8 +403,14 @@ async function getTaxSummary({ startDate, endDate, accountingMethod = 'Accrual'
|
|||||||
const docs = data?.QueryResponse?.[entity] || [];
|
const docs = data?.QueryResponse?.[entity] || [];
|
||||||
for (const doc of docs) {
|
for (const doc of docs) {
|
||||||
const taxDetail = doc.TxnTaxDetail;
|
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;
|
if (!taxDetail || !taxDetail.TaxLine) continue;
|
||||||
|
|
||||||
const taxLines = Array.isArray(taxDetail.TaxLine) ? taxDetail.TaxLine : [taxDetail.TaxLine];
|
const taxLines = Array.isArray(taxDetail.TaxLine) ? taxDetail.TaxLine : [taxDetail.TaxLine];
|
||||||
|
let docTaxable = 0;
|
||||||
for (const line of taxLines) {
|
for (const line of taxLines) {
|
||||||
const detail = line.TaxLineDetail;
|
const detail = line.TaxLineDetail;
|
||||||
if (!detail || !detail.TaxRateRef) continue;
|
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 };
|
if (!agg[rateId]) agg[rateId] = { taxableSales: 0, taxCollected: 0 };
|
||||||
agg[rateId].taxableSales += taxable;
|
agg[rateId].taxableSales += taxable;
|
||||||
agg[rateId].taxCollected += collected;
|
agg[rateId].taxCollected += collected;
|
||||||
|
docTaxable = Math.max(docTaxable, taxable);
|
||||||
}
|
}
|
||||||
|
totalTaxable += docTaxable;
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(`${entity} query failed:`, e.message);
|
console.error(`${entity} query failed:`, e.message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const nontaxableSales = totalSales - totalTaxable;
|
||||||
const rows = Object.entries(agg).map(([rateId, amounts]) => {
|
const rows = Object.entries(agg).map(([rateId, amounts]) => {
|
||||||
const info = rateMap[rateId] || { name: `Tax Rate ${rateId}`, rateValue: 0 };
|
const info = rateMap[rateId] || { name: `Tax Rate ${rateId}`, rateValue: 0 };
|
||||||
const ratePct = info.rateValue.toFixed(3).replace(/0+$/, '').replace(/\.$/, '');
|
const ratePct = info.rateValue.toFixed(3).replace(/0+$/, '').replace(/\.$/, '');
|
||||||
@@ -429,12 +440,16 @@ async function getTaxSummary({ startDate, endDate, accountingMethod = 'Accrual'
|
|||||||
{ value: info.name },
|
{ value: info.name },
|
||||||
{ value: '' },
|
{ value: '' },
|
||||||
{ value: '' },
|
{ value: '' },
|
||||||
|
{ value: '' },
|
||||||
|
{ value: '' },
|
||||||
{ value: `${ratePct}%` }
|
{ value: `${ratePct}%` }
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
Summary: {
|
Summary: {
|
||||||
ColData: [
|
ColData: [
|
||||||
{ value: 'Total' },
|
{ value: 'Total' },
|
||||||
|
{ value: totalSales.toFixed(2) },
|
||||||
|
{ value: nontaxableSales.toFixed(2) },
|
||||||
{ value: amounts.taxableSales.toFixed(2) },
|
{ value: amounts.taxableSales.toFixed(2) },
|
||||||
{ value: amounts.taxCollected.toFixed(2) },
|
{ value: amounts.taxCollected.toFixed(2) },
|
||||||
{ value: `${ratePct}%` }
|
{ value: `${ratePct}%` }
|
||||||
@@ -451,6 +466,27 @@ async function getTaxSummary({ startDate, endDate, accountingMethod = 'Accrual'
|
|||||||
return na.localeCompare(nb);
|
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 {
|
return {
|
||||||
Header: {
|
Header: {
|
||||||
ReportName: 'Sales Tax Liability',
|
ReportName: 'Sales Tax Liability',
|
||||||
@@ -461,7 +497,9 @@ async function getTaxSummary({ startDate, endDate, accountingMethod = 'Accrual'
|
|||||||
Columns: {
|
Columns: {
|
||||||
Column: [
|
Column: [
|
||||||
{ ColTitle: '', ColType: 'Text' },
|
{ 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 Collected', ColType: 'Money' },
|
||||||
{ ColTitle: 'Tax Rate', ColType: 'Percent' }
|
{ ColTitle: 'Tax Rate', ColType: 'Percent' }
|
||||||
]
|
]
|
||||||
|
|||||||
Reference in New Issue
Block a user