BOD-72 Refactored calculations to use Dinero library.

This commit is contained in:
Patrick Fic
2020-04-29 13:25:31 -07:00
parent a9bd0a5ccb
commit 99d3a6a64e
6 changed files with 240 additions and 209 deletions

View File

@@ -6,68 +6,70 @@ export function CalculateJob(job, shoprates) {
rates: CalculateRatesTotals(job, shoprates),
custPayable: CalculateCustPayable(job),
};
ret.totals = CalculateTaxesTotals(job, ret);
console.log("CalculateJob -> Final", 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 subtotal = otherTotals.parts.parts.subtotal
.add(otherTotals.parts.sublets.subtotal)
.add(otherTotals.rates.subtotal)
.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 +
val.act_price *
val.part_qty *
(job.parts_tax_rates[val.part_type].prt_tax_rt || 0)
return acc.add(
Dinero({ amount: val.act_price * 100 })
.multiply(val.part_qty)
.percentage(
(job.parts_tax_rates[val.part_type].prt_tax_rt || 0) * 100
)
);
} 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)
// );
}, Dinero({ amount: 0 }));
let ret = {
subtotal: subtotal,
federal_tax: subtotal * (job.federal_tax_rate || 0),
federal_tax: subtotal.percentage((job.federal_tax_rate || 0) * 100),
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),
state_tax: statePartsTax
.add(
otherTotals.rates.rates_subtotal.percentage((job.tax_lbr_rt || 0) * 100)
)
.add(
Dinero({ amount: (job.towing_payable || 0) * 100 }).percentage(
(job.tax_tow_rt || 0) * 100
)
)
.add(
Dinero({ amount: (job.storage_payable || 0) * 100 }).percentage(
(job.tax_str_rt || 0) * 100
)
)
.add(
otherTotals.rates.paint_mat.total
.add(otherTotals.rates.shop_mat.total)
.percentage((job.tax_paint_mat_rt || 0) * 100)
),
local_tax: subtotal.percentage((job.local_tax_rate || 0) * 100),
};
ret.total_repairs =
ret.subtotal + ret.federal_tax + ret.state_tax + ret.local_tax;
ret.net_repairs = ret.total_repairs - otherTotals.custPayable.total;
ret.total_repairs = ret.subtotal
.add(ret.federal_tax)
.add(ret.state_tax)
.add(ret.local_tax);
ret.net_repairs = ret.total_repairs.subtract(otherTotals.custPayable.total);
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;
@@ -75,86 +77,86 @@ function CalculateRatesTotals(ratesList, shoprates) {
rate_la1: {
hours: jobLines
.filter((item) => item.mod_lbr_ty === "LA1")
.reduce((acc, value) => acc + value.mod_lb_hrs, 0),
.reduce((acc, value) => acc + value.mod_lb_hrs * 10, 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),
.reduce((acc, value) => acc + value.mod_lb_hrs * 10, 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),
.reduce((acc, value) => acc + value.mod_lb_hrs * 10, 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),
.reduce((acc, value) => acc + value.mod_lb_hrs * 10, 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),
.reduce((acc, value) => acc + value.mod_lb_hrs * 10, 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),
.reduce((acc, value) => acc + value.mod_lb_hrs * 10, 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),
.reduce((acc, value) => acc + value.mod_lb_hrs * 10, 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),
.reduce((acc, value) => acc + value.mod_lb_hrs * 10, 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),
.reduce((acc, value) => acc + value.mod_lb_hrs * 10, 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),
.reduce((acc, value) => acc + value.mod_lb_hrs * 10, 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),
.reduce((acc, value) => acc + value.mod_lb_hrs * 10, 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),
.reduce((acc, value) => acc + value.mod_lb_hrs * 10, 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),
.reduce((acc, value) => acc + value.mod_lb_hrs * 10, 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),
.reduce((acc, value) => acc + value.mod_lb_hrs * 10, 0),
},
rate_atp: {
rate: shoprates.rate_atp || 0,
@@ -173,38 +175,40 @@ function CalculateRatesTotals(ratesList, shoprates) {
item.mod_lbr_ty !== "LAS" &&
item.mod_lbr_ty !== "LAA"
)
.reduce((acc, value) => acc + value.mod_lb_hrs, 0)
.reduce((acc, value) => acc + value.mod_lb_hrs * 10, 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),
.reduce((acc, value) => acc + value.mod_lb_hrs * 10, 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),
.reduce((acc, value) => acc + value.mod_lb_hrs * 10, 0),
},
};
let subtotal = 0;
let rates_subtotal = 0;
let subtotal = Dinero({ amount: 0 });
let rates_subtotal = Dinero({ amount: 0 });
for (const property in ret) {
ret[property].total = ret[property].hours * ret[property].rate;
subtotal = subtotal + ret[property].hours * ret[property].rate;
ret[property].total = Dinero({ amount: ret[property].rate * 100 })
.multiply(ret[property].hours)
.divide(10);
subtotal = subtotal.add(ret[property].total);
if (
property !== "paint_mat" &&
property !== "shop_mat"
//&& property !== "rate_atp"
)
rates_subtotal =
rates_subtotal + ret[property].hours * ret[property].rate;
rates_subtotal = rates_subtotal.add(ret[property].total);
}
ret.subtotal = subtotal;
ret.rates_subtotal = rates_subtotal;
return ret;
}
@@ -225,7 +229,11 @@ function CalculatePartsTotals(jobLines) {
...acc,
parts: {
...acc.parts,
subtotal: acc.parts.subtotal + value.act_price * value.part_qty,
subtotal: acc.parts.subtotal.add(
Dinero({ amount: value.act_price * 100 }).multiply(
value.part_qty
)
),
//TODO Add Adjustments in
},
};
@@ -235,7 +243,9 @@ function CalculatePartsTotals(jobLines) {
...acc,
sublets: {
...acc.sublets,
subtotal: acc.sublets.subtotal + value.act_price,
subtotal: acc.sublets.subtotal.add(
Dinero({ amount: value.act_price * 100 })
),
//TODO Add Adjustments in
},
};
@@ -244,31 +254,46 @@ function CalculatePartsTotals(jobLines) {
}
},
{
parts: { subtotal: 0, adjustments: 0, total: 0 },
sublets: { subtotal: 0, adjustments: 0, total: 0 },
parts: {
subtotal: Dinero({ amount: 0 }),
adjustments: Dinero({ amount: 0 }),
total: Dinero({ amount: 0 }),
},
sublets: {
subtotal: Dinero({ amount: 0 }),
adjustments: Dinero({ amount: 0 }),
total: Dinero({ amount: 0 }),
},
}
);
return {
parts: { ...ret.parts, total: ret.parts.subtotal + ret.parts.adjustments },
parts: {
...ret.parts,
total: ret.parts.subtotal, //+ ret.parts.adjustments
},
sublets: {
...ret.sublets,
total: ret.sublets.subtotal + ret.sublets.adjustments,
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,
let ret = {
deductible: Dinero({ amount: (job.ded_amt || 0) * 100 }) || 0,
federal_tax: Dinero({ amount: (job.federal_tax_payable || 0) * 100 }), //TODO Should this be renamed to make it more clear this is customer GST?
other_customer_amount: Dinero({
amount: (job.other_amount_payable || 0) * 100,
}),
dep_taxes: Dinero({ amount: job.depreciation_taxes || 0 }),
};
ret.total = ret.deductible
.add(ret.federal_tax)
.add(ret.federal_tax)
.add(ret.other_customer_amount)
.add(ret.dep_taxes);
return ret;
}