IO-1855 Change QBO changes to server side.

This commit is contained in:
Patrick Fic
2022-05-02 15:52:05 -07:00
parent 2d9de4703b
commit 4f6d1d27d5
18 changed files with 567 additions and 337 deletions

View File

@@ -59,8 +59,9 @@ exports.default = async (req, res) => {
bills: billsToQuery,
});
const { bills } = result;
const { bills, bodyshops } = result;
const ret = [];
const bodyshop = bodyshops[0];
for (const bill of bills) {
try {
@@ -86,9 +87,29 @@ exports.default = async (req, res) => {
qbo_realmId,
req,
bill,
vendorRecord
vendorRecord,
bodyshop
);
// //No error. Mark the job exported & insert export log.
const result = await client
.setHeaders({ Authorization: BearerToken })
.request(queries.QBO_MARK_BILL_EXPORTED, {
billId: bill.id,
bill: {
exported: true,
exported_at: moment().tz(bodyshop.timezone),
},
logs: [
{
bodyshopid: bodyshop.id,
billid: bill.id,
successful: true,
useremail: req.user.email,
},
],
});
ret.push({ billid: bill.id, success: true });
} catch (error) {
ret.push({
@@ -98,6 +119,24 @@ exports.default = async (req, res) => {
(error && error.authResponse && error.authResponse.body) ||
(error && error.message),
});
//Add the export log error.
const result = await client
.setHeaders({ Authorization: BearerToken })
.request(queries.INSERT_EXPORT_LOG, {
logs: [
{
bodyshopid: bodyshop.id,
billid: bill.id,
successful: false,
message: JSON.stringify([
(error && error.authResponse && error.authResponse.body) ||
(error && error.message),
]),
useremail: req.user.email,
},
],
});
}
}
@@ -167,7 +206,7 @@ async function InsertVendorRecord(oauthClient, qbo_realmId, req, bill) {
}
}
async function InsertBill(oauthClient, qbo_realmId, req, bill, vendor) {
async function InsertBill(oauthClient, qbo_realmId, req, bill, vendor, bodyshop) {
const { accounts, taxCodes, classes } = await QueryMetaData(
oauthClient,
qbo_realmId,
@@ -179,20 +218,20 @@ async function InsertBill(oauthClient, qbo_realmId, req, bill, vendor) {
il,
accounts,
bill.job.class,
bill.job.bodyshop.md_responsibility_centers.sales_tax_codes,
bodyshop.md_responsibility_centers.sales_tax_codes,
classes,
taxCodes,
bill.job.bodyshop.md_responsibility_centers.costs
bodyshop.md_responsibility_centers.costs
)
);
//QB USA with GST
//This was required for the No. 1 Collision Group.
if (
bill.job.bodyshop.accountingconfig &&
bill.job.bodyshop.accountingconfig.qbo &&
bill.job.bodyshop.accountingconfig.qbo_usa &&
bill.job.bodyshop.region_config.includes("CA_")
bodyshop.accountingconfig &&
bodyshop.accountingconfig.qbo &&
bodyshop.accountingconfig.qbo_usa &&
bodyshop.region_config.includes("CA_")
) {
lines.push({
DetailType: "AccountBasedExpenseLineDetail",
@@ -204,7 +243,7 @@ async function InsertBill(oauthClient, qbo_realmId, req, bill, vendor) {
AccountRef: {
value:
accounts[
bill.job.bodyshop.md_responsibility_centers.taxes.federal
bodyshop.md_responsibility_centers.taxes.federal
.accountdesc
],
},
@@ -239,6 +278,14 @@ async function InsertBill(oauthClient, qbo_realmId, req, bill, vendor) {
}),
DocNumber: bill.invoice_number,
//...(bill.job.class ? { ClassRef: { Id: classes[bill.job.class] } } : {}),
...(!(
bodyshop.accountingconfig &&
bodyshop.accountingconfig.qbo &&
bodyshop.accountingconfig.qbo_usa &&
bodyshop.region_config.includes("CA_")
)
? { GlobalTaxCalculation: "TaxExcluded" }
: {}),
PrivateNote: `RO ${bill.job.ro_number || ""}`,
Line: lines,