IO-1855 Change QBO changes to server side.
This commit is contained in:
@@ -71,7 +71,7 @@ exports.default = async (req, res) => {
|
||||
|
||||
exports.refresh = async (oauthClient, req) => {
|
||||
try {
|
||||
logger.log("qbo-token-refresh", "DEBUG", req.user.email, null, null);
|
||||
// logger.log("qbo-token-refresh", "DEBUG", req.user.email, null, null);
|
||||
const authResponse = await oauthClient.refresh();
|
||||
await client.request(queries.SET_QBO_AUTH, {
|
||||
email: req.user.email,
|
||||
@@ -85,7 +85,7 @@ exports.refresh = async (oauthClient, req) => {
|
||||
};
|
||||
|
||||
exports.setNewRefreshToken = async (email, apiResponse) => {
|
||||
logger.log("qbo-token-updated", "DEBUG", email, null, null);
|
||||
//logger.log("qbo-token-updated", "DEBUG", email, null, null);
|
||||
|
||||
await client.request(queries.SET_QBO_AUTH, {
|
||||
email,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -155,6 +155,25 @@ exports.default = async (req, res) => {
|
||||
bodyshop
|
||||
);
|
||||
}
|
||||
|
||||
// //No error. Mark the payment exported & insert export log.
|
||||
const result = await client
|
||||
.setHeaders({ Authorization: BearerToken })
|
||||
.request(queries.QBO_MARK_PAYMENT_EXPORTED, {
|
||||
paymentId: payment.id,
|
||||
payment: {
|
||||
exportedat: moment().tz(bodyshop.timezone),
|
||||
},
|
||||
logs: [
|
||||
{
|
||||
bodyshopid: bodyshop.id,
|
||||
paymentid: payment.id,
|
||||
successful: true,
|
||||
useremail: req.user.email,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
ret.push({ paymentid: payment.id, success: true });
|
||||
} catch (error) {
|
||||
logger.log("qbo-payment-create-error", "ERROR", req.user.email, {
|
||||
@@ -162,7 +181,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,
|
||||
paymentid: payment.id,
|
||||
successful: false,
|
||||
message: JSON.stringify([
|
||||
(error && error.authResponse && error.authResponse.body) ||
|
||||
(error && error.message),
|
||||
]),
|
||||
useremail: req.user.email,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
ret.push({
|
||||
paymentid: payment.id,
|
||||
success: false,
|
||||
|
||||
@@ -139,6 +139,25 @@ exports.default = async (req, res) => {
|
||||
bodyshop,
|
||||
jobTier
|
||||
);
|
||||
|
||||
// //No error. Mark the job exported & insert export log.
|
||||
const result = await client
|
||||
.setHeaders({ Authorization: BearerToken })
|
||||
.request(queries.QBO_MARK_JOB_EXPORTED, {
|
||||
jobId: job.id,
|
||||
job: {
|
||||
status: bodyshop.md_ro_statuses.default_exported || "Exported*",
|
||||
date_exported: moment().tz(bodyshop.timezone),
|
||||
},
|
||||
logs: [
|
||||
{
|
||||
bodyshopid: bodyshop.id,
|
||||
jobid: job.id,
|
||||
successful: true,
|
||||
useremail: req.user.email,
|
||||
},
|
||||
],
|
||||
});
|
||||
}
|
||||
ret.push({ jobid: job.id, success: true });
|
||||
} catch (error) {
|
||||
@@ -149,6 +168,23 @@ 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,
|
||||
jobid: job.id,
|
||||
successful: false,
|
||||
message: JSON.stringify([
|
||||
(error && error.authResponse && error.authResponse.body) ||
|
||||
(error && error.message),
|
||||
]),
|
||||
useremail: req.user.email,
|
||||
},
|
||||
],
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -37,14 +37,15 @@ exports.default = async (req, res) => {
|
||||
.request(queries.QUERY_BILLS_FOR_PAYABLES_EXPORT, {
|
||||
bills: billsToQuery,
|
||||
});
|
||||
const { bills } = result;
|
||||
const { bills, bodyshops } = result;
|
||||
const bodyshop = bodyshops[0];
|
||||
|
||||
const QbXmlToExecute = [];
|
||||
bills.map((i) => {
|
||||
QbXmlToExecute.push({
|
||||
id: i.id,
|
||||
okStatusCodes: ["0"],
|
||||
qbxml: generateBill(i),
|
||||
qbxml: generateBill(i, bodyshop),
|
||||
});
|
||||
});
|
||||
|
||||
@@ -62,7 +63,7 @@ exports.default = async (req, res) => {
|
||||
}
|
||||
};
|
||||
|
||||
const generateBill = (bill) => {
|
||||
const generateBill = (bill, bodyshop) => {
|
||||
const billQbxmlObj = {
|
||||
QBXML: {
|
||||
QBXMLMsgsRq: {
|
||||
@@ -87,7 +88,7 @@ const generateBill = (bill) => {
|
||||
ExpenseLineAdd: bill.billlines.map((il) =>
|
||||
generateBillLine(
|
||||
il,
|
||||
bill.job.bodyshop.md_responsibility_centers,
|
||||
bodyshop.md_responsibility_centers,
|
||||
bill.job.class
|
||||
)
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user