Added server side job calculation. BOD-267
This commit is contained in:
@@ -26,6 +26,20 @@ export function JobsTotalsTableComponent({ bodyshop, job }) {
|
||||
setTotals(CalculateJob(job, bodyshop.shoprates));
|
||||
}, [bodyshop, job]);
|
||||
|
||||
// useEffect(() => {
|
||||
// const Calculate = async () => {
|
||||
// const newTotals = (
|
||||
// await Axios.post("/job/totals", {
|
||||
// job: job,
|
||||
// shoprates: bodyshop.shoprates,
|
||||
// })
|
||||
// ).data;
|
||||
// setTotals(newTotals);
|
||||
// };
|
||||
|
||||
// Calculate();
|
||||
// }, [bodyshop, job]);
|
||||
|
||||
if (!!!totals) {
|
||||
return <LoadingSkeleton />;
|
||||
}
|
||||
|
||||
@@ -30,6 +30,7 @@ export function JobsDetailHeader({
|
||||
bodyshop,
|
||||
updateJobStatus,
|
||||
setScheduleContext,
|
||||
loading,
|
||||
form,
|
||||
}) {
|
||||
const { t } = useTranslation();
|
||||
@@ -79,6 +80,7 @@ export function JobsDetailHeader({
|
||||
<JobsDetailHeaderActions key="actions" job={job} refetch={refetch} />
|
||||
<Button
|
||||
type="primary"
|
||||
loading={loading}
|
||||
className="imex-flex-row__margin"
|
||||
onClick={() => form.submit()}
|
||||
>
|
||||
|
||||
@@ -6,9 +6,11 @@ import Icon, {
|
||||
ToolFilled,
|
||||
} from "@ant-design/icons";
|
||||
import { Form, notification, Tabs } from "antd";
|
||||
import Axios from "axios";
|
||||
import Dinero from "dinero.js";
|
||||
import moment from "moment";
|
||||
import queryString from "query-string";
|
||||
import React, { lazy, Suspense } from "react";
|
||||
import React, { lazy, Suspense, useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import {
|
||||
FaHardHat,
|
||||
@@ -20,7 +22,6 @@ import { connect } from "react-redux";
|
||||
import { useHistory, useLocation } from "react-router-dom";
|
||||
import { createStructuredSelector } from "reselect";
|
||||
import FormFieldsChanged from "../../components/form-fields-changed-alert/form-fields-changed-alert.component";
|
||||
import { CalculateJob } from "../../components/job-totals-table/job-totals.utility";
|
||||
import LoadingSpinner from "../../components/loading-spinner/loading-spinner.component";
|
||||
import { selectBodyshop } from "../../redux/user/user.selectors";
|
||||
|
||||
@@ -89,7 +90,7 @@ export function JobsDetailPage({
|
||||
const { t } = useTranslation();
|
||||
const [form] = Form.useForm();
|
||||
const history = useHistory();
|
||||
|
||||
const [loading, setLoading] = useState(false);
|
||||
const search = queryString.parse(useLocation().search);
|
||||
const formItemLayout = {
|
||||
layout: "vertical",
|
||||
@@ -97,16 +98,23 @@ export function JobsDetailPage({
|
||||
};
|
||||
|
||||
const handleFinish = async (values) => {
|
||||
const newTotals = CalculateJob({ ...job, ...values }, bodyshop.shoprates);
|
||||
setLoading(true);
|
||||
//const newTotals = CalculateJob({ ...job, ...values }, bodyshop.shoprates);
|
||||
const newTotals = (
|
||||
await Axios.post("/job/totals", {
|
||||
job: { ...job, ...values },
|
||||
shoprates: bodyshop.shoprates,
|
||||
})
|
||||
).data;
|
||||
|
||||
const result = await mutationUpdateJob({
|
||||
variables: {
|
||||
jobId: job.id,
|
||||
job: {
|
||||
...values,
|
||||
clm_total: newTotals.totals.total_repairs.toFormat("0.00"),
|
||||
owner_owing: newTotals.custPayable.total.toFormat("0.00"),
|
||||
job_totals: JSON.stringify(newTotals),
|
||||
clm_total: Dinero(newTotals.totals.total_repairs).toFormat("0.00"),
|
||||
owner_owing: Dinero(newTotals.custPayable.total).toFormat("0.00"),
|
||||
job_totals: newTotals, //JSON.stringify(newTotals),
|
||||
},
|
||||
},
|
||||
});
|
||||
@@ -119,6 +127,7 @@ export function JobsDetailPage({
|
||||
form.resetFields();
|
||||
form.resetFields();
|
||||
}
|
||||
setLoading(false);
|
||||
};
|
||||
|
||||
return (
|
||||
@@ -147,6 +156,7 @@ export function JobsDetailPage({
|
||||
refetch={refetch}
|
||||
handleSubmit={handleSubmit}
|
||||
updateJobStatus={updateJobStatus}
|
||||
loading={loading}
|
||||
/>
|
||||
<Tabs
|
||||
defaultActiveKey={search.tab}
|
||||
|
||||
@@ -72,6 +72,9 @@ app.post(
|
||||
smsStatus.status
|
||||
);
|
||||
|
||||
var job = require("./server/job/job");
|
||||
app.post("/job/totals", job.totals);
|
||||
|
||||
//Scheduling
|
||||
var scheduling = require("./server/scheduling/scheduling-job");
|
||||
app.post("/scheduling/job", scheduling.job);
|
||||
|
||||
325
server/job/job-totals.js
Normal file
325
server/job/job-totals.js
Normal file
@@ -0,0 +1,325 @@
|
||||
const Dinero = require("dinero.js");
|
||||
|
||||
exports.default = async function (req, res) {
|
||||
const { job, shoprates } = req.body;
|
||||
try {
|
||||
let ret = {
|
||||
parts: CalculatePartsTotals(job.joblines),
|
||||
rates: CalculateRatesTotals(job, shoprates),
|
||||
custPayable: CalculateCustPayable(job),
|
||||
};
|
||||
ret.totals = CalculateTaxesTotals(job, ret);
|
||||
// console.log("CalculateJob -> Final", ret);
|
||||
res.status(200).json(ret);
|
||||
} catch (error) {
|
||||
console.log("error", error);
|
||||
res.status(400).send(JSON.stringify(error));
|
||||
}
|
||||
};
|
||||
|
||||
function CalculateTaxesTotals(job, otherTotals) {
|
||||
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.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;
|
||||
}
|
||||
}, Dinero({ amount: 0 }));
|
||||
let ret = {
|
||||
subtotal: subtotal,
|
||||
federal_tax: subtotal.percentage((job.federal_tax_rate || 0) * 100),
|
||||
statePartsTax,
|
||||
state_tax: statePartsTax
|
||||
.add(
|
||||
otherTotals.rates.rates_subtotal.percentage((job.tax_lbr_rt || 0) * 100)
|
||||
)
|
||||
.add(
|
||||
Dinero({
|
||||
amount: Math.round((job.towing_payable || 0) * 100),
|
||||
}).percentage((job.tax_tow_rt || 0) * 100)
|
||||
)
|
||||
.add(
|
||||
Dinero({
|
||||
amount: Math.round((job.storage_payable || 0) * 100),
|
||||
}).percentage((job.tax_str_rt || 0) * 100)
|
||||
)
|
||||
.add(
|
||||
otherTotals.rates.mapa.total
|
||||
.add(otherTotals.rates.mash.total)
|
||||
.percentage((job.tax_paint_mat_rt || 0) * 100)
|
||||
),
|
||||
local_tax: subtotal.percentage((job.local_tax_rate || 0) * 100),
|
||||
};
|
||||
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;
|
||||
|
||||
let ret = {
|
||||
la1: {
|
||||
hours: jobLines
|
||||
.filter((item) => item.mod_lbr_ty === "LA1")
|
||||
.reduce((acc, value) => acc + value.mod_lb_hrs * 10, 0),
|
||||
rate: ratesList.rate_la1 || 0,
|
||||
},
|
||||
la2: {
|
||||
hours: jobLines
|
||||
.filter((item) => item.mod_lbr_ty === "LA2")
|
||||
.reduce((acc, value) => acc + value.mod_lb_hrs * 10, 0),
|
||||
rate: ratesList.rate_la2 || 0,
|
||||
},
|
||||
la3: {
|
||||
rate: ratesList.rate_la3 || 0,
|
||||
hours: jobLines
|
||||
.filter((item) => item.mod_lbr_ty === "LA3")
|
||||
.reduce((acc, value) => acc + value.mod_lb_hrs * 10, 0),
|
||||
},
|
||||
la4: {
|
||||
rate: ratesList.rate_la4 || 0,
|
||||
hours: jobLines
|
||||
.filter((item) => item.mod_lbr_ty === "LA4")
|
||||
.reduce((acc, value) => acc + value.mod_lb_hrs * 10, 0),
|
||||
},
|
||||
laa: {
|
||||
rate: ratesList.rate_laa || 0,
|
||||
hours: jobLines
|
||||
.filter((item) => item.mod_lbr_ty === "LAA")
|
||||
.reduce((acc, value) => acc + value.mod_lb_hrs * 10, 0),
|
||||
},
|
||||
lab: {
|
||||
rate: ratesList.rate_lab || 0,
|
||||
hours: jobLines
|
||||
.filter((item) => item.mod_lbr_ty === "LAB")
|
||||
.reduce((acc, value) => acc + value.mod_lb_hrs * 10, 0),
|
||||
},
|
||||
lad: {
|
||||
rate: ratesList.rate_lad || 0,
|
||||
hours: jobLines
|
||||
.filter((item) => item.mod_lbr_ty === "LAD")
|
||||
.reduce((acc, value) => acc + value.mod_lb_hrs * 10, 0),
|
||||
},
|
||||
lae: {
|
||||
rate: ratesList.rate_lae || 0,
|
||||
hours: jobLines
|
||||
.filter((item) => item.mod_lbr_ty === "LAE")
|
||||
.reduce((acc, value) => acc + value.mod_lb_hrs * 10, 0),
|
||||
},
|
||||
laf: {
|
||||
rate: ratesList.rate_laf || 0,
|
||||
hours: jobLines
|
||||
.filter((item) => item.mod_lbr_ty === "LAF")
|
||||
.reduce((acc, value) => acc + value.mod_lb_hrs * 10, 0),
|
||||
},
|
||||
lag: {
|
||||
rate: ratesList.rate_lag || 0,
|
||||
hours: jobLines
|
||||
.filter((item) => item.mod_lbr_ty === "LAG")
|
||||
.reduce((acc, value) => acc + value.mod_lb_hrs * 10, 0),
|
||||
},
|
||||
lam: {
|
||||
rate: ratesList.rate_lam || 0,
|
||||
hours: jobLines
|
||||
.filter((item) => item.mod_lbr_ty === "LAM")
|
||||
.reduce((acc, value) => acc + value.mod_lb_hrs * 10, 0),
|
||||
},
|
||||
lar: {
|
||||
rate: ratesList.rate_lar || 0,
|
||||
hours: jobLines
|
||||
.filter((item) => item.mod_lbr_ty === "LAR")
|
||||
.reduce((acc, value) => acc + value.mod_lb_hrs * 10, 0),
|
||||
},
|
||||
las: {
|
||||
rate: ratesList.rate_las || 0,
|
||||
hours: jobLines
|
||||
.filter((item) => item.mod_lbr_ty === "LAS")
|
||||
.reduce((acc, value) => acc + value.mod_lb_hrs * 10, 0),
|
||||
},
|
||||
lau: {
|
||||
rate: ratesList.rate_lau || 0,
|
||||
hours: jobLines
|
||||
.filter((item) => item.mod_lbr_ty === "LAU")
|
||||
.reduce((acc, value) => acc + value.mod_lb_hrs * 10, 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 * 10, 0)
|
||||
: 0,
|
||||
},
|
||||
mapa: {
|
||||
rate: ratesList.rate_mapa || 0,
|
||||
hours: jobLines
|
||||
.filter((item) => item.mod_lbr_ty === "LAR")
|
||||
.reduce((acc, value) => acc + value.mod_lb_hrs * 10, 0),
|
||||
},
|
||||
mash: {
|
||||
rate: ratesList.rate_mash || 0,
|
||||
hours: jobLines
|
||||
.filter((item) => item.mod_lbr_ty !== "LAR")
|
||||
.reduce((acc, value) => acc + value.mod_lb_hrs * 10, 0),
|
||||
},
|
||||
};
|
||||
|
||||
let subtotal = Dinero({ amount: 0 });
|
||||
let rates_subtotal = Dinero({ amount: 0 });
|
||||
for (const property in ret) {
|
||||
ret[property].total = Dinero({ amount: ret[property].rate * 100 })
|
||||
.multiply(ret[property].hours)
|
||||
.divide(10);
|
||||
subtotal = subtotal.add(ret[property].total);
|
||||
if (
|
||||
property !== "mapa" &&
|
||||
property !== "mash"
|
||||
//&& property !== "rate_atp"
|
||||
)
|
||||
rates_subtotal = rates_subtotal.add(ret[property].total);
|
||||
}
|
||||
ret.subtotal = subtotal;
|
||||
ret.rates_subtotal = rates_subtotal;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
function CalculatePartsTotals(jobLines) {
|
||||
const ret = jobLines.reduce(
|
||||
(acc, value) => {
|
||||
switch (value.part_type) {
|
||||
case "PAS":
|
||||
case "PASL":
|
||||
return {
|
||||
...acc,
|
||||
sublets: {
|
||||
...acc.sublets,
|
||||
subtotal: acc.sublets.subtotal.add(
|
||||
Dinero({ amount: Math.round(value.act_price * 100) })
|
||||
),
|
||||
//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 {
|
||||
...acc,
|
||||
parts: {
|
||||
...acc.parts,
|
||||
list: {
|
||||
...acc.parts.list,
|
||||
[value.part_type]:
|
||||
acc.parts.list[value.part_type] &&
|
||||
acc.parts.list[value.part_type].total
|
||||
? {
|
||||
total: acc.parts.list[value.part_type].total.add(
|
||||
Dinero({
|
||||
amount: Math.round((value.act_price || 0) * 100),
|
||||
}).multiply(value.part_qty || 1)
|
||||
),
|
||||
}
|
||||
: {
|
||||
total: Dinero({
|
||||
amount: Math.round((value.act_price || 0) * 100),
|
||||
}).multiply(value.part_qty || 1),
|
||||
},
|
||||
},
|
||||
subtotal: acc.parts.subtotal.add(
|
||||
Dinero({ amount: Math.round(value.act_price * 100) }).multiply(
|
||||
value.part_qty
|
||||
)
|
||||
),
|
||||
//TODO Add Adjustments in
|
||||
},
|
||||
};
|
||||
// default:
|
||||
// return acc;
|
||||
}
|
||||
},
|
||||
{
|
||||
parts: {
|
||||
list: {},
|
||||
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
|
||||
},
|
||||
sublets: {
|
||||
...ret.sublets,
|
||||
total: ret.sublets.subtotal, // + ret.sublets.adjustments,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function CalculateCustPayable(job) {
|
||||
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;
|
||||
}
|
||||
1
server/job/job.js
Normal file
1
server/job/job.js
Normal file
@@ -0,0 +1 @@
|
||||
exports.totals = require("./job-totals").default;
|
||||
Reference in New Issue
Block a user