IO-913 Group receivables lines.

This commit is contained in:
Patrick Fic
2021-04-26 17:36:53 -07:00
parent 2eaed9a617
commit b03d215cbf
2 changed files with 213 additions and 117 deletions

View File

@@ -13,6 +13,7 @@ require("dotenv").config({
),
});
Dinero.globalRoundingMode = 'HALF_EVEN'
const { generateJobTier, generateOwnerTier, generateSourceTier } = QbXmlUtils;
exports.default = async (req, res) => {
@@ -179,6 +180,7 @@ const generateJobQbxml = (
console.log("jobQbxml_Full", jobQbxml_Full);
return jobQbxml_Full;
};
exports.generateJobQbxml = generateJobQbxml;
const generateInvoiceQbxml = (
jobs_by_pk,
@@ -190,32 +192,43 @@ const generateInvoiceQbxml = (
const InvoiceLineAdd = [];
const responsibilityCenters = bodyshop.md_responsibility_centers;
const invoiceLineHash = {};
//Create the invoice lines mapping.
jobs_by_pk.joblines.map((jobline) => {
//Parts Lines
console.log("Jobline desc", jobline.line_desc);
if (jobline.profitcenter_part && jobline.act_price) {
const DineroAmount = Dinero({
amount: Math.round(jobline.act_price * 100),
});
}).multiply(jobline.part_qty || 1);
const account = responsibilityCenters.profits.find(
(i) => jobline.profitcenter_part.toLowerCase() === i.name.toLowerCase()
);
if (!account) {
throw new Error(
`A matching account does not exist for the allocation. Center: ${jobline.profitcenter_part}`
`A matching account does not exist for the part allocation. Center: ${jobline.profitcenter_part}`
);
}
InvoiceLineAdd.push({
ItemRef: { FullName: account.accountitem },
Desc: `${account.accountdesc} - ${jobline.line_desc}`,
Quantity: jobline.part_qty,
Rate: DineroAmount.toFormat(DineroQbFormat),
//Amount: DineroAmount.toFormat(DineroQbFormat),
SalesTaxCodeRef: {
FullName: "E",
},
});
if (!invoiceLineHash[account.name]) {
invoiceLineHash[account.name] = {
ItemRef: { FullName: account.accountitem },
Desc: account.accountdesc,
Quantity: 1, //jobline.part_qty,
Amount: DineroAmount, //.toFormat(DineroQbFormat),
//Amount: DineroAmount.toFormat(DineroQbFormat),
SalesTaxCodeRef: {
FullName: "E",
},
};
} else {
invoiceLineHash[account.name].Amount = invoiceLineHash[
account.name
].Amount.add(DineroAmount);
}
}
console.log("Parts Done");
// Labor Lines
if (
jobline.profitcenter_labor &&
jobline.mod_lb_hrs &&
@@ -225,34 +238,44 @@ const generateInvoiceQbxml = (
amount: Math.round(
jobs_by_pk[`rate_${jobline.mod_lbr_ty.toLowerCase()}`] * 100
),
});
console.log(
"Rate",
jobline.mod_lbr_ty,
jobs_by_pk[`rate_${jobline.mod_lbr_ty.toLowerCase()}`]
);
}).multiply(jobline.mod_lb_hrs);
const account = responsibilityCenters.profits.find(
(i) => jobline.profitcenter_labor.toLowerCase() === i.name.toLowerCase()
);
if (!!!account) {
if (!account) {
throw new Error(
`A matching account does not exist for the allocation. Center: ${center}`
`A matching account does not exist for the labor allocation. Center: ${jobline.profitcenter_labor}`
);
}
InvoiceLineAdd.push({
ItemRef: { FullName: account.accountitem },
Desc: `${account.accountdesc} - ${jobline.op_code_desc} ${jobline.line_desc}`,
Quantity: jobline.mod_lb_hrs,
Rate: DineroAmount.toFormat(DineroQbFormat),
//Amount: DineroAmount.toFormat(DineroQbFormat),
SalesTaxCodeRef: {
FullName: "E",
},
});
if (!invoiceLineHash[account.name]) {
invoiceLineHash[account.name] = {
ItemRef: { FullName: account.accountitem },
Desc: account.accountdesc,
Quantity: 1, // jobline.mod_lb_hrs,
Amount: DineroAmount,
//Amount: DineroAmount.toFormat(DineroQbFormat),
SalesTaxCodeRef: {
FullName: "E",
},
};
} else {
invoiceLineHash[account.name].Amount = invoiceLineHash[
account.name
].Amount.add(DineroAmount);
}
}
});
// console.log("Done creating hash", JSON.stringify(invoiceLineHash));
//Convert the hash to an array.
Object.keys(invoiceLineHash).forEach((key) => {
console.log(key, invoiceLineHash[key]);
InvoiceLineAdd.push({
...invoiceLineHash[key],
Amount: invoiceLineHash[key].Amount.toFormat(DineroQbFormat),
});
});
//Add tax lines
const job_totals = jobs_by_pk.job_totals;