Renamed ATP to ATS and updated totals calculation logic. Logic is still WIP
This commit is contained in:
@@ -4,12 +4,13 @@ Dinero.defaultCurrency = "CAD";
|
||||
Dinero.globalLocale = "en-CA";
|
||||
|
||||
exports.default = async function (req, res) {
|
||||
const { job, shoprates } = req.body;
|
||||
const { job } = req.body;
|
||||
console.log(`Calculating Job Totals for ${job.id} - ${job.ro_number}`);
|
||||
try {
|
||||
let ret = {
|
||||
parts: CalculatePartsTotals(job.joblines),
|
||||
rates: CalculateRatesTotals(job, shoprates),
|
||||
rates: CalculateRatesTotals(job),
|
||||
additional: CalculateAdditional(job),
|
||||
custPayable: CalculateCustPayable(job),
|
||||
};
|
||||
ret.totals = CalculateTaxesTotals(job, ret);
|
||||
@@ -21,28 +22,61 @@ exports.default = async function (req, res) {
|
||||
}
|
||||
};
|
||||
|
||||
function CalculateAdditional(job) {
|
||||
return job.joblines
|
||||
.filter(
|
||||
(jl) =>
|
||||
jl.lbr_op === "OP2" ||
|
||||
jl.lbr_op === "OP3" ||
|
||||
jl.lbr_op === "OP4" ||
|
||||
jl.lbr_op === "OP5" ||
|
||||
jl.lbr_op === "OP6" ||
|
||||
jl.lbr_op === "OP7" ||
|
||||
jl.lbr_op === "OP8" ||
|
||||
jl.lbr_op === "OP9" ||
|
||||
jl.lbr_op === "OP10" ||
|
||||
jl.lbr_op === "OP13" ||
|
||||
jl.lbr_op === "OP13" ||
|
||||
jl.lbr_op === "OP14" ||
|
||||
jl.lbr_op === "OP15"
|
||||
)
|
||||
.reduce((acc, val) => {
|
||||
console.log("val", val);
|
||||
return acc.add(
|
||||
Dinero({ amount: Math.round((val.act_price || 0) * 100) })
|
||||
);
|
||||
}, Dinero());
|
||||
}
|
||||
|
||||
function CalculateTaxesTotals(job, otherTotals) {
|
||||
const theObj = JSON.parse(JSON.stringify(otherTotals));
|
||||
|
||||
const subtotal = otherTotals.parts.parts.subtotal
|
||||
.add(otherTotals.parts.sublets.subtotal)
|
||||
.add(otherTotals.rates.subtotal)
|
||||
.add(otherTotals.rates.rates_subtotal)
|
||||
.add(otherTotals.additional)
|
||||
.add(Dinero({ amount: (job.towing_payable || 0) * 100 }))
|
||||
.add(Dinero({ amount: (job.storage_payable || 0) * 100 }));
|
||||
//TODO Levies should be included??
|
||||
|
||||
const statePartsTax = job.joblines.reduce((acc, val) => {
|
||||
if (!!!val.tax_part) return acc;
|
||||
if (!!job.parts_tax_rates[val.part_type]) {
|
||||
return acc.add(
|
||||
Dinero({ amount: Math.round(val.act_price * 100) })
|
||||
.multiply(val.part_qty)
|
||||
.percentage(
|
||||
(job.parts_tax_rates[val.part_type].prt_tax_rt || 0) * 100
|
||||
)
|
||||
);
|
||||
} else {
|
||||
return acc;
|
||||
}
|
||||
// if (!!job.parts_tax_rates[val.part_type]) {
|
||||
// console.log("val.line_desc", val.line_desc);
|
||||
return acc.add(
|
||||
Dinero({ amount: Math.round(val.act_price * 100) })
|
||||
.multiply(val.part_qty)
|
||||
.percentage(
|
||||
((job.parts_tax_rates[val.part_type] &&
|
||||
job.parts_tax_rates[val.part_type].prt_tax_rt) ||
|
||||
0.07) * 100
|
||||
)
|
||||
);
|
||||
// } else {
|
||||
// return acc;
|
||||
// }
|
||||
}, Dinero({ amount: 0 }));
|
||||
|
||||
let ret = {
|
||||
subtotal: subtotal,
|
||||
federal_tax: subtotal.percentage((job.federal_tax_rate || 0) * 100),
|
||||
@@ -61,10 +95,20 @@ function CalculateTaxesTotals(job, otherTotals) {
|
||||
amount: Math.round((job.storage_payable || 0) * 100),
|
||||
}).percentage((job.tax_str_rt || 0) * 100)
|
||||
)
|
||||
// .add(
|
||||
// otherTotals.rates.mapa.total.percentage(
|
||||
// (job.tax_paint_mat_rt || 0) * 100
|
||||
// )
|
||||
// )
|
||||
// .add(
|
||||
// otherTotals.rates.mapa.total.percentage(
|
||||
// (job.tax_paint_mat_rt || 0) * 100
|
||||
// )
|
||||
// )
|
||||
.add(
|
||||
otherTotals.rates.mapa.total
|
||||
.add(otherTotals.rates.mash.total)
|
||||
.percentage((job.tax_paint_mat_rt || 0) * 100)
|
||||
otherTotals.rates.mash.total.percentage(
|
||||
(job.tax_shop_mat_rt || 0) * 100
|
||||
)
|
||||
),
|
||||
local_tax: subtotal.percentage((job.local_tax_rate || 0) * 100),
|
||||
};
|
||||
@@ -77,8 +121,6 @@ function CalculateTaxesTotals(job, otherTotals) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
//Rates are multipled by 10 to reduce the errors of rounding.
|
||||
//Adjusted for when adding to total by dividing by 10.
|
||||
function CalculateRatesTotals(ratesList, shoprates) {
|
||||
const jobLines = ratesList.joblines;
|
||||
|
||||
@@ -167,26 +209,7 @@ function CalculateRatesTotals(ratesList, shoprates) {
|
||||
.filter((item) => item.mod_lbr_ty === "LAU")
|
||||
.reduce((acc, value) => acc + value.mod_lb_hrs, 0),
|
||||
},
|
||||
atp: {
|
||||
rate: shoprates.rate_atp || 0,
|
||||
hours:
|
||||
jobLines.filter((item) => item.line_desc.includes("ATS Amount"))
|
||||
.length > 0
|
||||
? jobLines
|
||||
.filter(
|
||||
(item) =>
|
||||
item.mod_lbr_ty !== "LA1" &&
|
||||
item.mod_lbr_ty !== "LA2" &&
|
||||
item.mod_lbr_ty !== "LA3" &&
|
||||
item.mod_lbr_ty !== "LA4" &&
|
||||
item.mod_lbr_ty !== "LAU" &&
|
||||
item.mod_lbr_ty !== "LAG" &&
|
||||
item.mod_lbr_ty !== "LAS" &&
|
||||
item.mod_lbr_ty !== "LAA"
|
||||
)
|
||||
.reduce((acc, value) => acc + value.mod_lb_hrs, 0)
|
||||
: 0,
|
||||
},
|
||||
|
||||
mapa: {
|
||||
rate: ratesList.rate_mapa || 0,
|
||||
hours: jobLines
|
||||
@@ -237,15 +260,7 @@ function CalculatePartsTotals(jobLines) {
|
||||
//TODO Add Adjustments in
|
||||
},
|
||||
};
|
||||
// case "PAA":
|
||||
// case "PAC":
|
||||
// case "PAG":
|
||||
// case "PAL":
|
||||
// case "PAM":
|
||||
// case "PAN":
|
||||
// case "PAO":
|
||||
// case "PAP":
|
||||
// case "PAR":
|
||||
|
||||
default:
|
||||
if (value.part_type === null) return acc;
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user