IO-256 Begin QBO Changes for Individual code mapping on receivables.
This commit is contained in:
378
server/accounting/qb-receivables-lines.js
Normal file
378
server/accounting/qb-receivables-lines.js
Normal file
@@ -0,0 +1,378 @@
|
||||
const DineroQbFormat = require("./accounting-constants").DineroQbFormat;
|
||||
|
||||
const Dinero = require("dinero.js");
|
||||
const logger = require("../utils/logger");
|
||||
|
||||
module.exports = function ({
|
||||
bodyshop,
|
||||
jobs_by_pk,
|
||||
qbo = false,
|
||||
items,
|
||||
taxCodes,
|
||||
}) {
|
||||
const InvoiceLineAdd = [];
|
||||
const responsibilityCenters = bodyshop.md_responsibility_centers;
|
||||
|
||||
const invoiceLineHash = {}; //The hash of cost and profit centers based on the center name.
|
||||
|
||||
//Determine if there are MAPA and MASH lines already on the estimate.
|
||||
//If there are, don't do anything extra (mitchell estimate)
|
||||
//Otherwise, calculate them and add them to the default MAPA and MASH centers.
|
||||
let hasMapaLine = false;
|
||||
let hasMashLine = false;
|
||||
|
||||
//Create the invoice lines mapping.
|
||||
jobs_by_pk.joblines.map((jobline) => {
|
||||
if (jobline.db_ref === "936008") {
|
||||
//If either of these DB REFs change, they also need to change in job-totals/job-costing calculations.
|
||||
hasMapaLine = true;
|
||||
}
|
||||
if (jobline.db_ref === "936007") {
|
||||
hasMashLine = true;
|
||||
}
|
||||
//Parts Lines Mappings.
|
||||
if (jobline.profitcenter_part && jobline.act_price) {
|
||||
let DineroAmount = Dinero({
|
||||
amount: Math.round(jobline.act_price * 100),
|
||||
}).multiply(jobline.part_qty || 1);
|
||||
|
||||
if (jobline.prt_dsmk_p && jobline.prt_dsmk_p !== 0) {
|
||||
// console.log("Have a part discount", jobline);
|
||||
DineroAmount = DineroAmount.add(
|
||||
DineroAmount.percentage(Math.abs(jobline.prt_dsmk_p || 0)).multiply(
|
||||
jobline.prt_dsmk_p > 0 ? 1 : -1
|
||||
)
|
||||
);
|
||||
}
|
||||
const account = responsibilityCenters.profits.find(
|
||||
(i) => jobline.profitcenter_part.toLowerCase() === i.name.toLowerCase()
|
||||
);
|
||||
|
||||
if (!account) {
|
||||
logger.log(
|
||||
"qbxml-receivables-no-account",
|
||||
"warn",
|
||||
null,
|
||||
jobline.id,
|
||||
null
|
||||
);
|
||||
throw new Error(
|
||||
`A matching account does not exist for the part allocation. Center: ${jobline.profitcenter_part}`
|
||||
);
|
||||
}
|
||||
if (qbo) {
|
||||
//Do the mapping as per QBO.
|
||||
//Determine the Tax code grouping.
|
||||
|
||||
//Going to always assume that we need to apply GST.
|
||||
const taxAccountCode = findTaxCode(
|
||||
{
|
||||
local: false,
|
||||
federal: "true",
|
||||
state: jobline.tax_part,
|
||||
},
|
||||
bodyshop.md_responsibility_centers.sales_tax_codes
|
||||
);
|
||||
|
||||
//GST/PST BC is what comes in, need to find the tax rate reference.
|
||||
const QboTaxId = taxCodes[taxAccountCode];
|
||||
if (!invoiceLineHash[account.name]) invoiceLineHash[account.name] = {};
|
||||
if (!invoiceLineHash[account.name][QboTaxId]) {
|
||||
invoiceLineHash[account.name][QboTaxId] = {
|
||||
DetailType: "SalesItemLineDetail",
|
||||
Amount: DineroAmount,
|
||||
SalesItemLineDetail: {
|
||||
ItemRef: {
|
||||
value: items[account.accountitem],
|
||||
},
|
||||
TaxCodeRef: {
|
||||
value: QboTaxId,
|
||||
},
|
||||
Qty: 1,
|
||||
},
|
||||
};
|
||||
} else {
|
||||
invoiceLineHash[account.name][QboTaxId].Amount =
|
||||
invoiceLineHash[account.name][QboTaxId].Amount.add(DineroAmount);
|
||||
}
|
||||
} else {
|
||||
if (!invoiceLineHash[account.name]) {
|
||||
invoiceLineHash[account.name] = {
|
||||
ItemRef: { FullName: account.accountitem },
|
||||
Desc: account.accountdesc,
|
||||
Quantity: 1, //jobline.part_qty,
|
||||
Amount: DineroAmount, //.toFormat(DineroQbFormat),
|
||||
SalesTaxCodeRef: {
|
||||
FullName: "E",
|
||||
},
|
||||
};
|
||||
} else {
|
||||
invoiceLineHash[account.name].Amount =
|
||||
invoiceLineHash[account.name].Amount.add(DineroAmount);
|
||||
}
|
||||
}
|
||||
}
|
||||
//End Parts line mappings.
|
||||
|
||||
// Labor Lines
|
||||
if (jobline.profitcenter_labor && jobline.mod_lb_hrs) {
|
||||
const DineroAmount = Dinero({
|
||||
amount: Math.round(
|
||||
jobs_by_pk[`rate_${jobline.mod_lbr_ty.toLowerCase()}`] * 100
|
||||
),
|
||||
}).multiply(jobline.mod_lb_hrs);
|
||||
const account = responsibilityCenters.profits.find(
|
||||
(i) => jobline.profitcenter_labor.toLowerCase() === i.name.toLowerCase()
|
||||
);
|
||||
|
||||
if (!account) {
|
||||
throw new Error(
|
||||
`A matching account does not exist for the labor allocation. Center: ${jobline.profitcenter_labor}`
|
||||
);
|
||||
}
|
||||
if (qbo) {
|
||||
//Do some QBO Stuff.
|
||||
} else {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (!hasMapaLine && jobs_by_pk.job_totals.rates.mapa.total.amount > 0) {
|
||||
// console.log("Adding MAPA Line Manually.");
|
||||
const mapaAccountName = responsibilityCenters.defaults.profits.MAPA;
|
||||
|
||||
const mapaAccount = responsibilityCenters.profits.find(
|
||||
(c) => c.name === mapaAccountName
|
||||
);
|
||||
|
||||
if (mapaAccount) {
|
||||
if (qbo) {
|
||||
//Add QBO MAPA
|
||||
} else {
|
||||
InvoiceLineAdd.push({
|
||||
ItemRef: { FullName: mapaAccount.accountitem },
|
||||
Desc: mapaAccount.accountdesc,
|
||||
Quantity: 1,
|
||||
Amount: Dinero(jobs_by_pk.job_totals.rates.mapa.total).toFormat(
|
||||
DineroQbFormat
|
||||
),
|
||||
SalesTaxCodeRef: {
|
||||
FullName: "E",
|
||||
},
|
||||
});
|
||||
}
|
||||
} else {
|
||||
//console.log("NO MAPA ACCOUNT FOUND!!");
|
||||
}
|
||||
}
|
||||
|
||||
if (!hasMashLine && jobs_by_pk.job_totals.rates.mash.total.amount > 0) {
|
||||
// console.log("Adding MASH Line Manually.");
|
||||
|
||||
const mashAccountName = responsibilityCenters.defaults.profits.MASH;
|
||||
|
||||
const mashAccount = responsibilityCenters.profits.find(
|
||||
(c) => c.name === mashAccountName
|
||||
);
|
||||
|
||||
if (mashAccount) {
|
||||
if (qbo) {
|
||||
//do some QBO
|
||||
} else {
|
||||
InvoiceLineAdd.push({
|
||||
ItemRef: { FullName: mashAccount.accountitem },
|
||||
Desc: mashAccount.accountdesc,
|
||||
Quantity: 1,
|
||||
Amount: Dinero(jobs_by_pk.job_totals.rates.mash.total).toFormat(
|
||||
DineroQbFormat
|
||||
),
|
||||
SalesTaxCodeRef: {
|
||||
FullName: "E",
|
||||
},
|
||||
});
|
||||
}
|
||||
} else {
|
||||
// console.log("NO MASH ACCOUNT FOUND!!");
|
||||
}
|
||||
}
|
||||
|
||||
//Convert the hash to an array.
|
||||
Object.keys(invoiceLineHash).forEach((key) => {
|
||||
Object.keys(invoiceLineHash[key]).forEach((key2) => {
|
||||
InvoiceLineAdd.push({
|
||||
...invoiceLineHash[key][key2],
|
||||
Amount: invoiceLineHash[key][key2].Amount.toFormat(DineroQbFormat),
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
//Add Towing, storage and adjustment lines.
|
||||
|
||||
if (jobs_by_pk.towing_payable && jobs_by_pk.towing_payable !== 0) {
|
||||
if (qbo) {
|
||||
// do qbo
|
||||
} else {
|
||||
InvoiceLineAdd.push({
|
||||
ItemRef: {
|
||||
FullName: responsibilityCenters.profits.find(
|
||||
(c) => c.name === responsibilityCenters.defaults.profits["TOW"]
|
||||
).accountitem,
|
||||
},
|
||||
Desc: "Towing",
|
||||
Quantity: 1,
|
||||
Amount: Dinero({
|
||||
amount: Math.round((jobs_by_pk.towing_payable || 0) * 100),
|
||||
}).toFormat(DineroQbFormat),
|
||||
SalesTaxCodeRef: {
|
||||
FullName: "E",
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
if (jobs_by_pk.storage_payable && jobs_by_pk.storage_payable !== 0) {
|
||||
if (qbo) {
|
||||
// do qbo
|
||||
} else {
|
||||
InvoiceLineAdd.push({
|
||||
ItemRef: {
|
||||
FullName: responsibilityCenters.profits.find(
|
||||
(c) => c.name === responsibilityCenters.defaults.profits["TOW"]
|
||||
).accountitem,
|
||||
},
|
||||
Desc: "Storage",
|
||||
Quantity: 1,
|
||||
Amount: Dinero({
|
||||
amount: Math.round((jobs_by_pk.storage_payable || 0) * 100),
|
||||
}).toFormat(DineroQbFormat),
|
||||
SalesTaxCodeRef: {
|
||||
FullName: "E",
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
if (
|
||||
jobs_by_pk.adjustment_bottom_line &&
|
||||
jobs_by_pk.adjustment_bottom_line !== 0
|
||||
) {
|
||||
if (qbo) {
|
||||
// do qbo
|
||||
} else {
|
||||
InvoiceLineAdd.push({
|
||||
ItemRef: {
|
||||
FullName: responsibilityCenters.profits.find(
|
||||
(c) => c.name === responsibilityCenters.defaults.profits["PAO"]
|
||||
).accountitem,
|
||||
},
|
||||
Desc: "Adjustment",
|
||||
Quantity: 1,
|
||||
Amount: Dinero({
|
||||
amount: Math.round((jobs_by_pk.adjustment_bottom_line || 0) * 100),
|
||||
}).toFormat(DineroQbFormat),
|
||||
SalesTaxCodeRef: {
|
||||
FullName: "E",
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
//Add tax lines
|
||||
const job_totals = jobs_by_pk.job_totals;
|
||||
|
||||
const federal_tax = Dinero(job_totals.totals.federal_tax);
|
||||
const state_tax = Dinero(job_totals.totals.state_tax);
|
||||
const local_tax = Dinero(job_totals.totals.local_tax);
|
||||
|
||||
if (federal_tax.getAmount() > 0) {
|
||||
if (qbo) {
|
||||
// do qbo
|
||||
} else {
|
||||
InvoiceLineAdd.push({
|
||||
ItemRef: {
|
||||
FullName:
|
||||
bodyshop.md_responsibility_centers.taxes.federal.accountitem,
|
||||
},
|
||||
Desc: bodyshop.md_responsibility_centers.taxes.federal.accountdesc,
|
||||
Amount: federal_tax.toFormat(DineroQbFormat),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (state_tax.getAmount() > 0) {
|
||||
if (qbo) {
|
||||
// do qbo
|
||||
} else {
|
||||
InvoiceLineAdd.push({
|
||||
ItemRef: {
|
||||
FullName: bodyshop.md_responsibility_centers.taxes.state.accountitem,
|
||||
},
|
||||
Desc: bodyshop.md_responsibility_centers.taxes.state.accountdesc,
|
||||
Amount: state_tax.toFormat(DineroQbFormat),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (local_tax.getAmount() > 0) {
|
||||
if (qbo) {
|
||||
// do qbo
|
||||
} else {
|
||||
InvoiceLineAdd.push({
|
||||
ItemRef: {
|
||||
FullName: bodyshop.md_responsibility_centers.taxes.local.accountitem,
|
||||
},
|
||||
Desc: bodyshop.md_responsibility_centers.taxes.local.accountdesc,
|
||||
Amount: local_tax.toFormat(DineroQbFormat),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
//Region Specific
|
||||
const { ca_bc_pvrt } = jobs_by_pk;
|
||||
if (ca_bc_pvrt) {
|
||||
if (qbo) {
|
||||
// do qbo
|
||||
} else {
|
||||
InvoiceLineAdd.push({
|
||||
ItemRef: {
|
||||
FullName: bodyshop.md_responsibility_centers.taxes.state.accountitem,
|
||||
},
|
||||
Desc: "PVRT",
|
||||
Amount: Dinero({ amount: (ca_bc_pvrt || 0) * 100 }).toFormat(
|
||||
DineroQbFormat
|
||||
),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return InvoiceLineAdd;
|
||||
};
|
||||
|
||||
const findTaxCode = ({ local, state, federal }, taxcode) => {
|
||||
const t = taxcode.filter(
|
||||
(t) =>
|
||||
!!t.local === !!local &&
|
||||
!!t.state === !!state &&
|
||||
!!t.federal === !!federal
|
||||
);
|
||||
if (t.length === 1) {
|
||||
return t[0].code;
|
||||
} else if (t.length > 1) {
|
||||
return "Multiple Tax Codes Match";
|
||||
} else {
|
||||
return "No Tax Code Matches";
|
||||
}
|
||||
};
|
||||
@@ -16,6 +16,7 @@ const {
|
||||
setNewRefreshToken,
|
||||
} = require("./qbo-callback");
|
||||
const OAuthClient = require("intuit-oauth");
|
||||
const CreateInvoiceLines = require("../qb-receivables-lines");
|
||||
|
||||
const GraphQLClient = require("graphql-request").GraphQLClient;
|
||||
const { generateOwnerTier } = require("../qbxml/qbxml-utils");
|
||||
@@ -348,321 +349,34 @@ async function QueryMetaData(oauthClient, req) {
|
||||
|
||||
async function InsertInvoice(oauthClient, req, job, bodyshop, parentTierRef) {
|
||||
const { items, taxCodes } = await QueryMetaData(oauthClient, req);
|
||||
const InvoiceLineAdd = [];
|
||||
const responsibilityCenters = bodyshop.md_responsibility_centers;
|
||||
|
||||
const invoiceLineHash = {}; //The hash of cost and profit centers based on the center name.
|
||||
|
||||
//Determine if there are MAPA and MASH lines already on the estimate.
|
||||
//If there are, don't do anything extra (mitchell estimate)
|
||||
//Otherwise, calculate them and add them to the default MAPA and MASH centers.
|
||||
let hasMapaLine = false;
|
||||
let hasMashLine = false;
|
||||
|
||||
//Create the invoice lines mapping.
|
||||
job.joblines.map((jobline) => {
|
||||
//Parts Lines
|
||||
if (jobline.db_ref === "936008") {
|
||||
//If either of these DB REFs change, they also need to change in job-totals/job-costing calculations.
|
||||
hasMapaLine = true;
|
||||
}
|
||||
if (jobline.db_ref === "936007") {
|
||||
hasMashLine = true;
|
||||
}
|
||||
|
||||
if (jobline.profitcenter_part && jobline.act_price) {
|
||||
let DineroAmount = Dinero({
|
||||
amount: Math.round(jobline.act_price * 100),
|
||||
}).multiply(jobline.part_qty || 1);
|
||||
|
||||
if (jobline.prt_dsmk_p && jobline.prt_dsmk_p !== 0) {
|
||||
// console.log("Have a part discount", jobline);
|
||||
DineroAmount = DineroAmount.add(
|
||||
DineroAmount.percentage(jobline.prt_dsmk_p || 0)
|
||||
);
|
||||
}
|
||||
const account = responsibilityCenters.profits.find(
|
||||
(i) => jobline.profitcenter_part.toLowerCase() === i.name.toLowerCase()
|
||||
);
|
||||
|
||||
if (!account) {
|
||||
logger.log(
|
||||
"qbxml-receivables-no-account",
|
||||
"warn",
|
||||
null,
|
||||
jobline.id,
|
||||
null
|
||||
);
|
||||
throw new Error(
|
||||
`A matching account does not exist for the part allocation. Center: ${jobline.profitcenter_part}`
|
||||
);
|
||||
}
|
||||
if (!invoiceLineHash[account.name]) {
|
||||
invoiceLineHash[account.name] = {
|
||||
ItemRef: { FullName: account.accountitem },
|
||||
Desc: account.accountdesc,
|
||||
Quantity: 1, //jobline.part_qty,
|
||||
Amount: DineroAmount, //.toFormat(DineroQbFormat),
|
||||
SalesTaxCodeRef: {
|
||||
FullName: "E",
|
||||
},
|
||||
};
|
||||
} else {
|
||||
invoiceLineHash[account.name].Amount =
|
||||
invoiceLineHash[account.name].Amount.add(DineroAmount);
|
||||
}
|
||||
}
|
||||
// Labor Lines
|
||||
if (jobline.profitcenter_labor && jobline.mod_lb_hrs) {
|
||||
const DineroAmount = Dinero({
|
||||
amount: Math.round(
|
||||
job[`rate_${jobline.mod_lbr_ty.toLowerCase()}`] * 100
|
||||
),
|
||||
}).multiply(jobline.mod_lb_hrs);
|
||||
const account = responsibilityCenters.profits.find(
|
||||
(i) => jobline.profitcenter_labor.toLowerCase() === i.name.toLowerCase()
|
||||
);
|
||||
|
||||
if (!account) {
|
||||
throw new Error(
|
||||
`A matching account does not exist for the labor allocation. Center: ${jobline.profitcenter_labor}`
|
||||
);
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
const InvoiceLineAdd = CreateInvoiceLines({
|
||||
bodyshop,
|
||||
jobs_by_pk: job,
|
||||
qbo: true,
|
||||
items,
|
||||
taxCodes,
|
||||
});
|
||||
// console.log("Done creating hash", JSON.stringify(invoiceLineHash));
|
||||
|
||||
if (!hasMapaLine && job.job_totals.rates.mapa.total.amount > 0) {
|
||||
// console.log("Adding MAPA Line Manually.");
|
||||
const mapaAccountName = responsibilityCenters.defaults.profits.MAPA;
|
||||
|
||||
const mapaAccount = responsibilityCenters.profits.find(
|
||||
(c) => c.name === mapaAccountName
|
||||
);
|
||||
|
||||
if (mapaAccount) {
|
||||
InvoiceLineAdd.push({
|
||||
ItemRef: { FullName: mapaAccount.accountitem },
|
||||
Desc: mapaAccount.accountdesc,
|
||||
Quantity: 1,
|
||||
Amount: Dinero(job.job_totals.rates.mapa.total).toFormat(
|
||||
DineroQbFormat
|
||||
),
|
||||
SalesTaxCodeRef: {
|
||||
FullName: "E",
|
||||
},
|
||||
});
|
||||
} else {
|
||||
//console.log("NO MAPA ACCOUNT FOUND!!");
|
||||
}
|
||||
}
|
||||
|
||||
if (!hasMashLine && job.job_totals.rates.mash.total.amount > 0) {
|
||||
// console.log("Adding MASH Line Manually.");
|
||||
|
||||
const mashAccountName = responsibilityCenters.defaults.profits.MASH;
|
||||
|
||||
const mashAccount = responsibilityCenters.profits.find(
|
||||
(c) => c.name === mashAccountName
|
||||
);
|
||||
|
||||
if (mashAccount) {
|
||||
InvoiceLineAdd.push({
|
||||
ItemRef: { FullName: mashAccount.accountitem },
|
||||
Desc: mashAccount.accountdesc,
|
||||
Quantity: 1,
|
||||
Amount: Dinero(job.job_totals.rates.mash.total).toFormat(
|
||||
DineroQbFormat
|
||||
),
|
||||
SalesTaxCodeRef: {
|
||||
FullName: "E",
|
||||
},
|
||||
});
|
||||
} else {
|
||||
// console.log("NO MASH ACCOUNT FOUND!!");
|
||||
}
|
||||
}
|
||||
|
||||
//Convert the hash to an array.
|
||||
Object.keys(invoiceLineHash).forEach((key) => {
|
||||
InvoiceLineAdd.push({
|
||||
...invoiceLineHash[key],
|
||||
Amount: invoiceLineHash[key].Amount.toFormat(DineroQbFormat),
|
||||
});
|
||||
});
|
||||
|
||||
//Add Towing, storage and adjustment lines.
|
||||
|
||||
if (job.towing_payable && job.towing_payable !== 0) {
|
||||
InvoiceLineAdd.push({
|
||||
ItemRef: {
|
||||
FullName: responsibilityCenters.profits.find(
|
||||
(c) => c.name === responsibilityCenters.defaults.profits["TOW"]
|
||||
).accountitem,
|
||||
},
|
||||
Desc: "Towing",
|
||||
Quantity: 1,
|
||||
Amount: Dinero({
|
||||
amount: Math.round((job.towing_payable || 0) * 100),
|
||||
}).toFormat(DineroQbFormat),
|
||||
SalesTaxCodeRef: {
|
||||
FullName: "E",
|
||||
},
|
||||
});
|
||||
}
|
||||
if (job.storage_payable && job.storage_payable !== 0) {
|
||||
InvoiceLineAdd.push({
|
||||
ItemRef: {
|
||||
FullName: responsibilityCenters.profits.find(
|
||||
(c) => c.name === responsibilityCenters.defaults.profits["TOW"]
|
||||
).accountitem,
|
||||
},
|
||||
Desc: "Storage",
|
||||
Quantity: 1,
|
||||
Amount: Dinero({
|
||||
amount: Math.round((job.storage_payable || 0) * 100),
|
||||
}).toFormat(DineroQbFormat),
|
||||
SalesTaxCodeRef: {
|
||||
FullName: "E",
|
||||
},
|
||||
});
|
||||
}
|
||||
if (job.adjustment_bottom_line && job.adjustment_bottom_line !== 0) {
|
||||
InvoiceLineAdd.push({
|
||||
ItemRef: {
|
||||
FullName: responsibilityCenters.profits.find(
|
||||
(c) => c.name === responsibilityCenters.defaults.profits["PAO"]
|
||||
).accountitem,
|
||||
},
|
||||
Desc: "Adjustment",
|
||||
Quantity: 1,
|
||||
Amount: Dinero({
|
||||
amount: Math.round((job.adjustment_bottom_line || 0) * 100),
|
||||
}).toFormat(DineroQbFormat),
|
||||
SalesTaxCodeRef: {
|
||||
FullName: "E",
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
//Add tax lines
|
||||
const job_totals = job.job_totals;
|
||||
|
||||
const federal_tax = Dinero(job_totals.totals.federal_tax);
|
||||
const state_tax = Dinero(job_totals.totals.state_tax);
|
||||
const local_tax = Dinero(job_totals.totals.local_tax);
|
||||
|
||||
if (federal_tax.getAmount() > 0) {
|
||||
InvoiceLineAdd.push({
|
||||
ItemRef: {
|
||||
FullName: bodyshop.md_responsibility_centers.taxes.federal.accountitem,
|
||||
},
|
||||
Desc: bodyshop.md_responsibility_centers.taxes.federal.accountdesc,
|
||||
Amount: federal_tax.toFormat(DineroQbFormat),
|
||||
});
|
||||
}
|
||||
|
||||
if (state_tax.getAmount() > 0) {
|
||||
InvoiceLineAdd.push({
|
||||
ItemRef: {
|
||||
FullName: bodyshop.md_responsibility_centers.taxes.state.accountitem,
|
||||
},
|
||||
Desc: bodyshop.md_responsibility_centers.taxes.state.accountdesc,
|
||||
Amount: state_tax.toFormat(DineroQbFormat),
|
||||
});
|
||||
}
|
||||
|
||||
if (local_tax.getAmount() > 0) {
|
||||
InvoiceLineAdd.push({
|
||||
ItemRef: {
|
||||
FullName: bodyshop.md_responsibility_centers.taxes.local.accountitem,
|
||||
},
|
||||
Desc: bodyshop.md_responsibility_centers.taxes.local.accountdesc,
|
||||
Amount: local_tax.toFormat(DineroQbFormat),
|
||||
});
|
||||
}
|
||||
|
||||
//Region Specific
|
||||
const { ca_bc_pvrt } = job;
|
||||
if (ca_bc_pvrt) {
|
||||
InvoiceLineAdd.push({
|
||||
ItemRef: {
|
||||
FullName: bodyshop.md_responsibility_centers.taxes.state.accountitem,
|
||||
},
|
||||
Desc: "PVRT",
|
||||
Amount: Dinero({ amount: (ca_bc_pvrt || 0) * 100 }).toFormat(
|
||||
DineroQbFormat
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
//map each invoice line to the correct style for QBO.
|
||||
|
||||
const invoiceObj = {
|
||||
Line: [
|
||||
{
|
||||
DetailType: "SalesItemLineDetail",
|
||||
Amount: 100,
|
||||
SalesItemLineDetail: {
|
||||
// ItemRef: {
|
||||
// // name: "Services",
|
||||
// value: "16",
|
||||
// },
|
||||
TaxCodeRef: {
|
||||
value: "2",
|
||||
},
|
||||
Qty: 1,
|
||||
UnitPrice: 100,
|
||||
},
|
||||
},
|
||||
],
|
||||
// Line: InvoiceLineAdd.map((i) => {
|
||||
// return {
|
||||
// Line: [
|
||||
// {
|
||||
// DetailType: "SalesItemLineDetail",
|
||||
// Amount: i.Amount,
|
||||
// Amount: 100,
|
||||
// SalesItemLineDetail: {
|
||||
// ItemRef: {
|
||||
// //name: "Services",
|
||||
// value: items[i.ItemRef.FullName],
|
||||
// // name: "Services",
|
||||
// value: "1",
|
||||
// },
|
||||
// TaxCodeRef: {
|
||||
// value: "2",
|
||||
// },
|
||||
// // TaxCodeRef: {
|
||||
// // value: "2",
|
||||
// // },
|
||||
// Qty: 1,
|
||||
// UnitPrice: 100,
|
||||
// },
|
||||
// };
|
||||
// }),
|
||||
TxnTaxDetail: {
|
||||
TaxLine: [
|
||||
{
|
||||
DetailType: "TaxLineDetail",
|
||||
// },
|
||||
// ],
|
||||
Line: InvoiceLineAdd,
|
||||
|
||||
TaxLineDetail: {
|
||||
NetAmountTaxable: 100,
|
||||
TaxPercent: 7,
|
||||
TaxRateRef: {
|
||||
value: "16",
|
||||
},
|
||||
PercentBased: true,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
CustomerRef: {
|
||||
value: parentTierRef.Id,
|
||||
},
|
||||
|
||||
@@ -7,6 +7,7 @@ const moment = require("moment");
|
||||
var builder = require("xmlbuilder2");
|
||||
const QbXmlUtils = require("./qbxml-utils");
|
||||
const logger = require("../../utils/logger");
|
||||
const CreateInvoiceLines = require("../qb-receivables-lines");
|
||||
|
||||
require("dotenv").config({
|
||||
path: path.resolve(
|
||||
@@ -227,273 +228,8 @@ const generateInvoiceQbxml = (
|
||||
twoTierPref
|
||||
) => {
|
||||
//Build the Invoice XML file.
|
||||
const InvoiceLineAdd = [];
|
||||
const responsibilityCenters = bodyshop.md_responsibility_centers;
|
||||
|
||||
const invoiceLineHash = {}; //The hash of cost and profit centers based on the center name.
|
||||
|
||||
//Determine if there are MAPA and MASH lines already on the estimate.
|
||||
//If there are, don't do anything extra (mitchell estimate)
|
||||
//Otherwise, calculate them and add them to the default MAPA and MASH centers.
|
||||
let hasMapaLine = false;
|
||||
let hasMashLine = false;
|
||||
|
||||
//Create the invoice lines mapping.
|
||||
jobs_by_pk.joblines.map((jobline) => {
|
||||
//Parts Lines
|
||||
if (jobline.db_ref === "936008") {
|
||||
//If either of these DB REFs change, they also need to change in job-totals/job-costing calculations.
|
||||
hasMapaLine = true;
|
||||
}
|
||||
if (jobline.db_ref === "936007") {
|
||||
hasMashLine = true;
|
||||
}
|
||||
|
||||
if (jobline.profitcenter_part && jobline.act_price) {
|
||||
let DineroAmount = Dinero({
|
||||
amount: Math.round(jobline.act_price * 100),
|
||||
}).multiply(jobline.part_qty || 1);
|
||||
|
||||
if (jobline.prt_dsmk_p && jobline.prt_dsmk_p !== 0) {
|
||||
// console.log("Have a part discount", jobline);
|
||||
DineroAmount = DineroAmount.add(
|
||||
DineroAmount.percentage(Math.abs(jobline.prt_dsmk_p || 0)).multiply(
|
||||
jobline.prt_dsmk_p > 0 ? 1 : -1
|
||||
)
|
||||
);
|
||||
}
|
||||
const account = responsibilityCenters.profits.find(
|
||||
(i) => jobline.profitcenter_part.toLowerCase() === i.name.toLowerCase()
|
||||
);
|
||||
|
||||
if (!account) {
|
||||
logger.log(
|
||||
"qbxml-receivables-no-account",
|
||||
"warn",
|
||||
null,
|
||||
jobline.id,
|
||||
null
|
||||
);
|
||||
throw new Error(
|
||||
`A matching account does not exist for the part allocation. Center: ${jobline.profitcenter_part}`
|
||||
);
|
||||
}
|
||||
if (!invoiceLineHash[account.name]) {
|
||||
invoiceLineHash[account.name] = {
|
||||
ItemRef: { FullName: account.accountitem },
|
||||
Desc: account.accountdesc,
|
||||
Quantity: 1, //jobline.part_qty,
|
||||
Amount: DineroAmount, //.toFormat(DineroQbFormat),
|
||||
SalesTaxCodeRef: {
|
||||
FullName: "E",
|
||||
},
|
||||
};
|
||||
} else {
|
||||
invoiceLineHash[account.name].Amount =
|
||||
invoiceLineHash[account.name].Amount.add(DineroAmount);
|
||||
}
|
||||
}
|
||||
// Labor Lines
|
||||
if (jobline.profitcenter_labor && jobline.mod_lb_hrs) {
|
||||
const DineroAmount = Dinero({
|
||||
amount: Math.round(
|
||||
jobs_by_pk[`rate_${jobline.mod_lbr_ty.toLowerCase()}`] * 100
|
||||
),
|
||||
}).multiply(jobline.mod_lb_hrs);
|
||||
const account = responsibilityCenters.profits.find(
|
||||
(i) => jobline.profitcenter_labor.toLowerCase() === i.name.toLowerCase()
|
||||
);
|
||||
|
||||
if (!account) {
|
||||
throw new Error(
|
||||
`A matching account does not exist for the labor allocation. Center: ${jobline.profitcenter_labor}`
|
||||
);
|
||||
}
|
||||
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));
|
||||
|
||||
if (!hasMapaLine && jobs_by_pk.job_totals.rates.mapa.total.amount > 0) {
|
||||
// console.log("Adding MAPA Line Manually.");
|
||||
const mapaAccountName = responsibilityCenters.defaults.profits.MAPA;
|
||||
|
||||
const mapaAccount = responsibilityCenters.profits.find(
|
||||
(c) => c.name === mapaAccountName
|
||||
);
|
||||
|
||||
if (mapaAccount) {
|
||||
InvoiceLineAdd.push({
|
||||
ItemRef: { FullName: mapaAccount.accountitem },
|
||||
Desc: mapaAccount.accountdesc,
|
||||
Quantity: 1,
|
||||
Amount: Dinero(jobs_by_pk.job_totals.rates.mapa.total).toFormat(
|
||||
DineroQbFormat
|
||||
),
|
||||
SalesTaxCodeRef: {
|
||||
FullName: "E",
|
||||
},
|
||||
});
|
||||
} else {
|
||||
//console.log("NO MAPA ACCOUNT FOUND!!");
|
||||
}
|
||||
}
|
||||
|
||||
if (!hasMashLine && jobs_by_pk.job_totals.rates.mash.total.amount > 0) {
|
||||
// console.log("Adding MASH Line Manually.");
|
||||
|
||||
const mashAccountName = responsibilityCenters.defaults.profits.MASH;
|
||||
|
||||
const mashAccount = responsibilityCenters.profits.find(
|
||||
(c) => c.name === mashAccountName
|
||||
);
|
||||
|
||||
if (mashAccount) {
|
||||
InvoiceLineAdd.push({
|
||||
ItemRef: { FullName: mashAccount.accountitem },
|
||||
Desc: mashAccount.accountdesc,
|
||||
Quantity: 1,
|
||||
Amount: Dinero(jobs_by_pk.job_totals.rates.mash.total).toFormat(
|
||||
DineroQbFormat
|
||||
),
|
||||
SalesTaxCodeRef: {
|
||||
FullName: "E",
|
||||
},
|
||||
});
|
||||
} else {
|
||||
// console.log("NO MASH ACCOUNT FOUND!!");
|
||||
}
|
||||
}
|
||||
|
||||
//Convert the hash to an array.
|
||||
Object.keys(invoiceLineHash).forEach((key) => {
|
||||
InvoiceLineAdd.push({
|
||||
...invoiceLineHash[key],
|
||||
Amount: invoiceLineHash[key].Amount.toFormat(DineroQbFormat),
|
||||
});
|
||||
});
|
||||
|
||||
//Add Towing, storage and adjustment lines.
|
||||
|
||||
if (jobs_by_pk.towing_payable && jobs_by_pk.towing_payable !== 0) {
|
||||
InvoiceLineAdd.push({
|
||||
ItemRef: {
|
||||
FullName: responsibilityCenters.profits.find(
|
||||
(c) => c.name === responsibilityCenters.defaults.profits["TOW"]
|
||||
).accountitem,
|
||||
},
|
||||
Desc: "Towing",
|
||||
Quantity: 1,
|
||||
Amount: Dinero({
|
||||
amount: Math.round((jobs_by_pk.towing_payable || 0) * 100),
|
||||
}).toFormat(DineroQbFormat),
|
||||
SalesTaxCodeRef: {
|
||||
FullName: "E",
|
||||
},
|
||||
});
|
||||
}
|
||||
if (jobs_by_pk.storage_payable && jobs_by_pk.storage_payable !== 0) {
|
||||
InvoiceLineAdd.push({
|
||||
ItemRef: {
|
||||
FullName: responsibilityCenters.profits.find(
|
||||
(c) => c.name === responsibilityCenters.defaults.profits["TOW"]
|
||||
).accountitem,
|
||||
},
|
||||
Desc: "Storage",
|
||||
Quantity: 1,
|
||||
Amount: Dinero({
|
||||
amount: Math.round((jobs_by_pk.storage_payable || 0) * 100),
|
||||
}).toFormat(DineroQbFormat),
|
||||
SalesTaxCodeRef: {
|
||||
FullName: "E",
|
||||
},
|
||||
});
|
||||
}
|
||||
if (
|
||||
jobs_by_pk.adjustment_bottom_line &&
|
||||
jobs_by_pk.adjustment_bottom_line !== 0
|
||||
) {
|
||||
InvoiceLineAdd.push({
|
||||
ItemRef: {
|
||||
FullName: responsibilityCenters.profits.find(
|
||||
(c) => c.name === responsibilityCenters.defaults.profits["PAO"]
|
||||
).accountitem,
|
||||
},
|
||||
Desc: "Adjustment",
|
||||
Quantity: 1,
|
||||
Amount: Dinero({
|
||||
amount: Math.round((jobs_by_pk.adjustment_bottom_line || 0) * 100),
|
||||
}).toFormat(DineroQbFormat),
|
||||
SalesTaxCodeRef: {
|
||||
FullName: "E",
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
//Add tax lines
|
||||
const job_totals = jobs_by_pk.job_totals;
|
||||
|
||||
const federal_tax = Dinero(job_totals.totals.federal_tax);
|
||||
const state_tax = Dinero(job_totals.totals.state_tax);
|
||||
const local_tax = Dinero(job_totals.totals.local_tax);
|
||||
|
||||
if (federal_tax.getAmount() > 0) {
|
||||
InvoiceLineAdd.push({
|
||||
ItemRef: {
|
||||
FullName: bodyshop.md_responsibility_centers.taxes.federal.accountitem,
|
||||
},
|
||||
Desc: bodyshop.md_responsibility_centers.taxes.federal.accountdesc,
|
||||
Amount: federal_tax.toFormat(DineroQbFormat),
|
||||
});
|
||||
}
|
||||
|
||||
if (state_tax.getAmount() > 0) {
|
||||
InvoiceLineAdd.push({
|
||||
ItemRef: {
|
||||
FullName: bodyshop.md_responsibility_centers.taxes.state.accountitem,
|
||||
},
|
||||
Desc: bodyshop.md_responsibility_centers.taxes.state.accountdesc,
|
||||
Amount: state_tax.toFormat(DineroQbFormat),
|
||||
});
|
||||
}
|
||||
|
||||
if (local_tax.getAmount() > 0) {
|
||||
InvoiceLineAdd.push({
|
||||
ItemRef: {
|
||||
FullName: bodyshop.md_responsibility_centers.taxes.local.accountitem,
|
||||
},
|
||||
Desc: bodyshop.md_responsibility_centers.taxes.local.accountdesc,
|
||||
Amount: local_tax.toFormat(DineroQbFormat),
|
||||
});
|
||||
}
|
||||
|
||||
//Region Specific
|
||||
const { ca_bc_pvrt } = jobs_by_pk;
|
||||
if (ca_bc_pvrt) {
|
||||
InvoiceLineAdd.push({
|
||||
ItemRef: {
|
||||
FullName: bodyshop.md_responsibility_centers.taxes.state.accountitem,
|
||||
},
|
||||
Desc: "PVRT",
|
||||
Amount: Dinero({ amount: (ca_bc_pvrt || 0) * 100 }).toFormat(
|
||||
DineroQbFormat
|
||||
),
|
||||
});
|
||||
}
|
||||
const InvoiceLineAdd = CreateInvoiceLines({ bodyshop, jobs_by_pk });
|
||||
|
||||
const invoiceQbxmlObj = {
|
||||
QBXML: {
|
||||
|
||||
@@ -120,6 +120,7 @@ query QUERY_JOBS_FOR_RECEIVABLES_EXPORT($ids: [uuid!]!) {
|
||||
profitcenter_part
|
||||
db_ref
|
||||
prt_dsmk_p
|
||||
tax_part
|
||||
}
|
||||
}
|
||||
bodyshops(where: {associations: {active: {_eq: true}}}) {
|
||||
|
||||
Reference in New Issue
Block a user