remove sales tax
This commit is contained in:
@@ -114,18 +114,9 @@ router.post('/sales-tax/periods', async (req, res) => {
|
||||
} catch (err) { handleQboError(err, res, 'sales-tax-periods'); }
|
||||
});
|
||||
|
||||
router.post('/sales-tax/periods/:id/record', async (req, res) => {
|
||||
try {
|
||||
res.json(await accountingService.createTaxPaymentJE({
|
||||
periodId: req.params.id,
|
||||
...req.body
|
||||
}));
|
||||
} catch (err) { handleQboError(err, res, 'sales-tax-record'); }
|
||||
});
|
||||
|
||||
router.post('/sales-tax/periods/:id/mark-external', async (req, res) => {
|
||||
try {
|
||||
res.json(await accountingService.markTaxPaidExternal(req.params.id));
|
||||
res.json(await accountingService.markTaxPaidExternal(req.params.id, req.body.paidDate));
|
||||
} catch (err) { handleQboError(err, res, 'sales-tax-mark-external'); }
|
||||
});
|
||||
|
||||
|
||||
@@ -566,101 +566,13 @@ async function upsertTaxPeriod(period) {
|
||||
return result.rows[0];
|
||||
}
|
||||
|
||||
async function createTaxPaymentJE({
|
||||
periodId, txnDate, taxCollected, adjustmentAmount, netPaid,
|
||||
salesTaxPayableId, salesTaxPayableName,
|
||||
adjustmentAccountId, adjustmentAccountName, adjustmentReason,
|
||||
bankAccountId, bankAccountName
|
||||
}) {
|
||||
const { companyId, baseUrl } = getClientInfo();
|
||||
const url = withMinorVersion(`${baseUrl}/v3/company/${companyId}/journalentry`);
|
||||
|
||||
const lines = [
|
||||
{
|
||||
DetailType: 'JournalEntryLineDetail',
|
||||
Amount: taxCollected,
|
||||
JournalEntryLineDetail: {
|
||||
PostingType: 'Debit',
|
||||
AccountRef: { value: String(salesTaxPayableId), name: salesTaxPayableName }
|
||||
}
|
||||
}
|
||||
];
|
||||
|
||||
if (adjustmentAmount > 0) {
|
||||
lines.push({
|
||||
DetailType: 'JournalEntryLineDetail',
|
||||
Amount: adjustmentAmount,
|
||||
Description: adjustmentReason || undefined,
|
||||
JournalEntryLineDetail: {
|
||||
PostingType: 'Credit',
|
||||
AccountRef: { value: String(adjustmentAccountId), name: adjustmentAccountName }
|
||||
}
|
||||
});
|
||||
} else if (adjustmentAmount < 0) {
|
||||
lines.push({
|
||||
DetailType: 'JournalEntryLineDetail',
|
||||
Amount: Math.abs(adjustmentAmount),
|
||||
Description: adjustmentReason || undefined,
|
||||
JournalEntryLineDetail: {
|
||||
PostingType: 'Debit',
|
||||
AccountRef: { value: String(adjustmentAccountId), name: adjustmentAccountName }
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
lines.push({
|
||||
DetailType: 'JournalEntryLineDetail',
|
||||
Amount: netPaid,
|
||||
JournalEntryLineDetail: {
|
||||
PostingType: 'Credit',
|
||||
AccountRef: { value: String(bankAccountId), name: bankAccountName }
|
||||
}
|
||||
});
|
||||
|
||||
const payload = {
|
||||
TxnDate: txnDate,
|
||||
DocNumber: `STP-${periodId}`,
|
||||
Line: lines,
|
||||
PrivateNote: adjustmentReason ? `Adjustment: ${adjustmentReason}` : undefined
|
||||
};
|
||||
|
||||
let qboResponse;
|
||||
try {
|
||||
const response = await makeQboApiCall({
|
||||
url,
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(payload)
|
||||
});
|
||||
qboResponse = response;
|
||||
} catch (e) {
|
||||
const errMsg = e.originalMessage || e.message || String(e);
|
||||
throw new Error(`QBO JournalEntry failed: ${errMsg}`);
|
||||
}
|
||||
|
||||
const data = getJson(qboResponse);
|
||||
throwIfFault(data, 'JournalEntry create');
|
||||
|
||||
const je = data.JournalEntry;
|
||||
if (!je || !je.Id) throw new Error('QBO did not return a JournalEntry ID');
|
||||
|
||||
await pool.query(
|
||||
`UPDATE sales_tax_periods
|
||||
SET qbo_journal_entry_id = $1, status = 'booked', booked_at = CURRENT_TIMESTAMP, updated_at = CURRENT_TIMESTAMP
|
||||
WHERE id = $2`,
|
||||
[je.Id, periodId]
|
||||
);
|
||||
|
||||
return { qbo_journal_entry_id: je.Id, qbo_sync_token: je.SyncToken };
|
||||
}
|
||||
|
||||
async function markTaxPaidExternal(periodId) {
|
||||
async function markTaxPaidExternal(periodId, paidDate = null) {
|
||||
const result = await pool.query(
|
||||
`UPDATE sales_tax_periods
|
||||
SET status = 'external', booked_at = CURRENT_TIMESTAMP, updated_at = CURRENT_TIMESTAMP
|
||||
SET status = 'paid', booked_at = $2, updated_at = CURRENT_TIMESTAMP
|
||||
WHERE id = $1 AND status = 'open'
|
||||
RETURNING *`,
|
||||
[periodId]
|
||||
[periodId, paidDate || new Date().toISOString().split('T')[0]]
|
||||
);
|
||||
if (result.rows.length === 0) {
|
||||
const existing = await pool.query('SELECT status FROM sales_tax_periods WHERE id = $1', [periodId]);
|
||||
@@ -1724,7 +1636,6 @@ module.exports = {
|
||||
getTaxSummary,
|
||||
getTaxPeriods,
|
||||
upsertTaxPeriod,
|
||||
createTaxPaymentJE,
|
||||
markTaxPaidExternal,
|
||||
normalizeTransactionListReport,
|
||||
|
||||
|
||||
Reference in New Issue
Block a user