Additional changes for CCC calculations.
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
const { objectLength } = require("@oozcitak/util");
|
||||
const Dinero = require("dinero.js");
|
||||
const queries = require("../graphql-client/queries");
|
||||
const GraphQLClient = require("graphql-request").GraphQLClient;
|
||||
@@ -58,7 +59,7 @@ async function TotalsServerSide(req, res) {
|
||||
|
||||
try {
|
||||
let ret = {
|
||||
parts: CalculatePartsTotals(job.joblines),
|
||||
parts: CalculatePartsTotals(job.joblines, job.parts_tax_rates),
|
||||
rates: CalculateRatesTotals(job),
|
||||
additional: CalculateAdditional(job),
|
||||
};
|
||||
@@ -92,7 +93,7 @@ async function Totals(req, res) {
|
||||
await AutoAddAtsIfRequired({ job, client });
|
||||
try {
|
||||
let ret = {
|
||||
parts: CalculatePartsTotals(job.joblines),
|
||||
parts: CalculatePartsTotals(job.joblines, job.parts_tax_rates),
|
||||
rates: CalculateRatesTotals(job),
|
||||
additional: CalculateAdditional(job),
|
||||
};
|
||||
@@ -260,6 +261,8 @@ function CalculateRatesTotals(ratesList) {
|
||||
//Otherwise, calculate them and add them to the default MAPA and MASH centers.
|
||||
let hasMapaLine = false;
|
||||
let hasMashLine = false;
|
||||
let mapaOpCodes = ParseCalopCode(ratesList.materials["mapa"].cal_opcode);
|
||||
let mashOpCodes = ParseCalopCode(ratesList.materials["mash"].cal_opcode);
|
||||
|
||||
jobLines.forEach((item) => {
|
||||
//IO-1317 Use the lines on the estimate if they exist instead.
|
||||
@@ -301,10 +304,18 @@ function CalculateRatesTotals(ratesList) {
|
||||
ret[item.mod_lbr_ty.toLowerCase()].hours =
|
||||
ret[item.mod_lbr_ty.toLowerCase()].hours + item.mod_lb_hrs;
|
||||
|
||||
//Count up the number of materials/paint hours.
|
||||
|
||||
//Following change may be CCC specific.
|
||||
if (item.mod_lbr_ty === "LAR") {
|
||||
// if (mapaOpCodes.includes(item.lbr_op)) { //Unknown if this is needed. Seems to be ignored.
|
||||
ret.mapa.hours = ret.mapa.hours + item.mod_lb_hrs;
|
||||
// }
|
||||
} else {
|
||||
ret.mash.hours = ret.mash.hours + item.mod_lb_hrs; //Apparently there may be an exclusion for glass hours in BC.
|
||||
if (mashOpCodes.includes(item.lbr_op)) {
|
||||
// Added when processing CIECA ID 14A60015 to have materials match.
|
||||
ret.mash.hours = ret.mash.hours + item.mod_lb_hrs; //Apparently there may be an exclusion for glass hours in BC.
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -327,9 +338,10 @@ function CalculateRatesTotals(ratesList) {
|
||||
//Check if there is a max for this type.
|
||||
if (ratesList.materials && ratesList.materials[property]) {
|
||||
//
|
||||
|
||||
if (
|
||||
ratesList.materials[property].cal_maxdlr &&
|
||||
ratesList.materials[property].cal_maxdlr > 0
|
||||
ratesList.materials[property].cal_maxdlr !== undefined &&
|
||||
ratesList.materials[property].cal_maxdlr >= 0
|
||||
) {
|
||||
//It has an upper threshhold.
|
||||
threshold = Dinero({
|
||||
@@ -361,7 +373,7 @@ function CalculateRatesTotals(ratesList) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
function CalculatePartsTotals(jobLines) {
|
||||
function CalculatePartsTotals(jobLines, parts_tax_rates) {
|
||||
const jl = jobLines.filter((jl) => !jl.removed);
|
||||
|
||||
const ret = jl.reduce(
|
||||
@@ -486,7 +498,60 @@ function CalculatePartsTotals(jobLines) {
|
||||
}
|
||||
);
|
||||
|
||||
//Apply insurance based parts discuounts/markups.
|
||||
let adjustments = {
|
||||
PAA: Dinero(),
|
||||
PAC: Dinero(),
|
||||
PAG: Dinero(),
|
||||
PAL: Dinero(),
|
||||
PAN: Dinero(),
|
||||
PAO: Dinero(),
|
||||
PAP: Dinero(),
|
||||
PAR: Dinero(),
|
||||
PAS: Dinero(),
|
||||
PAT: Dinero(),
|
||||
};
|
||||
Object.keys(parts_tax_rates).forEach((key) => {
|
||||
//Check if there's a discount or a mark up.
|
||||
let disc = Dinero(),
|
||||
markup = Dinero();
|
||||
if (
|
||||
parts_tax_rates[key].prt_discp !== undefined &&
|
||||
parts_tax_rates[key].prt_discp >= 0
|
||||
) {
|
||||
//Check if there's any parts in this part type.
|
||||
if (ret.parts.list[key] !== undefined) {
|
||||
disc = ret.parts.list[key].total
|
||||
.percentage(parts_tax_rates[key].prt_discp)
|
||||
.multiply(-1);
|
||||
}
|
||||
}
|
||||
if (
|
||||
parts_tax_rates[key].prt_mkupp !== undefined &&
|
||||
parts_tax_rates[key].prt_mkupp >= 0
|
||||
) {
|
||||
//Check if there's any parts in this part type.
|
||||
if (ret.parts.list[key] !== undefined) {
|
||||
markup = ret.parts.list[key].total.percentage(
|
||||
parts_tax_rates[key].prt_mkupp
|
||||
);
|
||||
}
|
||||
}
|
||||
let adjustment = disc.add(markup);
|
||||
adjustments[key] = adjustment;
|
||||
});
|
||||
|
||||
Object.keys(adjustments).forEach((key) => {
|
||||
if (ret.parts.list[key] !== undefined) {
|
||||
ret.parts.list[key].total = ret.parts.list[key].total.add(
|
||||
adjustments[key]
|
||||
);
|
||||
ret.parts.subtotal = ret.parts.subtotal.add(adjustments[key]);
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
adjustments,
|
||||
parts: {
|
||||
...ret.parts,
|
||||
total: ret.parts.subtotal,
|
||||
@@ -647,7 +712,7 @@ function CalculateTaxesTotals(job, otherTotals) {
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
let ret = {
|
||||
subtotal: subtotal,
|
||||
federal_tax: subtotal
|
||||
@@ -735,3 +800,8 @@ function DiscountNotAlreadyCounted(jobline, joblines) {
|
||||
}
|
||||
|
||||
exports.DiscountNotAlreadyCounted = DiscountNotAlreadyCounted;
|
||||
|
||||
function ParseCalopCode(opcode) {
|
||||
if (!opcode) return [];
|
||||
return opcode.trim().split(" ");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user