Files
bodyshop/client/src/components/job-totals-table/job-totals.utility.js

273 lines
7.9 KiB
JavaScript

export function CalculateJob(job, shoprates) {
let ret = {
parts: CalculatePartsTotals(job.joblines),
rates: CalculateRatesTotals(job, shoprates),
custPayable: CalculateCustPayable(job),
};
ret.totals = CalculateTaxesTotals(job, ret);
return ret;
}
function CalculateTaxesTotals(job, otherTotals) {
const subtotal =
otherTotals.parts.parts.subtotal +
otherTotals.parts.sublets.subtotal +
otherTotals.rates.subtotal +
(job.towing_payable || 0) +
(job.storage_payable || 0); //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 +
val.act_price *
val.part_qty *
(job.parts_tax_rates[val.part_type].prt_tax_rt || 0)
);
} else {
return acc;
}
}, 0);
// console.log("otherTotals", otherTotals);
// console.log("job", job);
// console.log("parts pst", statePartsTax);
// console.log(
// "pst on labor",
// otherTotals.rates.rates_subtotal * (job.tax_lbr_rt || 0)
// );
// console.log(
// "pst on mat",
// (otherTotals.rates.paint_mat.total + otherTotals.rates.shop_mat.total) *
// (job.tax_paint_mat_rt || 0)
// );
let ret = {
subtotal: subtotal,
federal_tax: subtotal * (job.federal_tax_rate || 0),
statePartsTax,
state_tax:
statePartsTax +
otherTotals.rates.rates_subtotal * (job.tax_lbr_rt || 0) +
((job.towing_payable || 0) * job.tax_tow_rt || 0) +
((job.storage_payable || 0) * job.tax_str_rt || 0) +
(otherTotals.rates.paint_mat.total + otherTotals.rates.shop_mat.total) *
(job.tax_paint_mat_rt || 0),
local_tax: subtotal * (job.local_tax_rate || 0),
};
ret.total_repairs =
ret.subtotal + ret.federal_tax + ret.state_tax + ret.local_tax;
ret.net_repairs = ret.total_repairs - otherTotals.custPayable.total;
return ret;
}
function CalculateRatesTotals(ratesList, shoprates) {
const jobLines = ratesList.joblines;
let ret = {
rate_la1: {
hours: jobLines
.filter((item) => item.mod_lbr_ty === "LA1")
.reduce((acc, value) => acc + value.mod_lb_hrs, 0),
rate: ratesList.rate_la1 || 0,
},
rate_la2: {
hours: jobLines
.filter((item) => item.mod_lbr_ty === "LA2")
.reduce((acc, value) => acc + value.mod_lb_hrs, 0),
rate: ratesList.rate_la2 || 0,
},
rate_la3: {
rate: ratesList.rate_la3 || 0,
hours: jobLines
.filter((item) => item.mod_lbr_ty === "LA3")
.reduce((acc, value) => acc + value.mod_lb_hrs, 0),
},
rate_la4: {
rate: ratesList.rate_la4 || 0,
hours: jobLines
.filter((item) => item.mod_lbr_ty === "LA4")
.reduce((acc, value) => acc + value.mod_lb_hrs, 0),
},
rate_laa: {
rate: ratesList.rate_laa || 0,
hours: jobLines
.filter((item) => item.mod_lbr_ty === "LAA")
.reduce((acc, value) => acc + value.mod_lb_hrs, 0),
},
rate_lab: {
rate: ratesList.rate_lab || 0,
hours: jobLines
.filter((item) => item.mod_lbr_ty === "LAB")
.reduce((acc, value) => acc + value.mod_lb_hrs, 0),
},
rate_lad: {
rate: ratesList.rate_lad || 0,
hours: jobLines
.filter((item) => item.mod_lbr_ty === "LAD")
.reduce((acc, value) => acc + value.mod_lb_hrs, 0),
},
rate_lae: {
rate: ratesList.rate_lae || 0,
hours: jobLines
.filter((item) => item.mod_lbr_ty === "LAE")
.reduce((acc, value) => acc + value.mod_lb_hrs, 0),
},
rate_laf: {
rate: ratesList.rate_laf || 0,
hours: jobLines
.filter((item) => item.mod_lbr_ty === "LAF")
.reduce((acc, value) => acc + value.mod_lb_hrs, 0),
},
rate_lag: {
rate: ratesList.rate_lag || 0,
hours: jobLines
.filter((item) => item.mod_lbr_ty === "LAG")
.reduce((acc, value) => acc + value.mod_lb_hrs, 0),
},
rate_lam: {
rate: ratesList.rate_lam || 0,
hours: jobLines
.filter((item) => item.mod_lbr_ty === "LAM")
.reduce((acc, value) => acc + value.mod_lb_hrs, 0),
},
rate_lar: {
rate: ratesList.rate_lar || 0,
hours: jobLines
.filter((item) => item.mod_lbr_ty === "LAR")
.reduce((acc, value) => acc + value.mod_lb_hrs, 0),
},
rate_las: {
rate: ratesList.rate_las || 0,
hours: jobLines
.filter((item) => item.mod_lbr_ty === "LAS")
.reduce((acc, value) => acc + value.mod_lb_hrs, 0),
},
rate_lau: {
rate: ratesList.rate_lau || 0,
hours: jobLines
.filter((item) => item.mod_lbr_ty === "LAU")
.reduce((acc, value) => acc + value.mod_lb_hrs, 0),
},
rate_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,
},
paint_mat: {
rate: ratesList.rate_mapa || 0,
hours: jobLines
.filter((item) => item.mod_lbr_ty === "LAR")
.reduce((acc, value) => acc + value.mod_lb_hrs, 0),
},
shop_mat: {
rate: ratesList.rate_mash || 0,
hours: jobLines
.filter((item) => item.mod_lbr_ty !== "LAR")
.reduce((acc, value) => acc + value.mod_lb_hrs, 0),
},
};
let subtotal = 0;
let rates_subtotal = 0;
for (const property in ret) {
ret[property].total = ret[property].hours * ret[property].rate;
subtotal = subtotal + ret[property].hours * ret[property].rate;
if (
property !== "paint_mat" &&
property !== "shop_mat"
//&& property !== "rate_atp"
)
rates_subtotal =
rates_subtotal + ret[property].hours * ret[property].rate;
}
ret.subtotal = subtotal;
ret.rates_subtotal = rates_subtotal;
return ret;
}
function CalculatePartsTotals(jobLines) {
const ret = jobLines.reduce(
(acc, value) => {
switch (value.part_type) {
case "PAA":
case "PAC":
case "PAG":
case "PAL":
case "PAM":
case "PAN":
case "PAO":
case "PAP":
case "PAR":
return {
...acc,
parts: {
...acc.parts,
subtotal: acc.parts.subtotal + value.act_price * value.part_qty,
//TODO Add Adjustments in
},
};
case "PAS":
case "PASL":
return {
...acc,
sublets: {
...acc.sublets,
subtotal: acc.sublets.subtotal + value.act_price,
//TODO Add Adjustments in
},
};
default:
return acc;
}
},
{
parts: { subtotal: 0, adjustments: 0, total: 0 },
sublets: { subtotal: 0, adjustments: 0, total: 0 },
}
);
return {
parts: { ...ret.parts, total: ret.parts.subtotal + ret.parts.adjustments },
sublets: {
...ret.sublets,
total: ret.sublets.subtotal + ret.sublets.adjustments,
},
};
}
function CalculateCustPayable(job) {
return {
deductible: job.ded_amt || 0,
federal_tax: job.federal_tax_payable || 0, //TODO Should this be renamed to make it more clear this is customer GST?
other_customer_amount: job.other_amount_payable || 0,
dep_taxes: job.depreciation_taxes || 0,
total:
job.ded_amt ||
0 + job.federal_tax_payable ||
0 + job.other_amount_payable ||
0 + job.depreciation_taxes ||
0,
};
}