Finished moving job totals calculation to server side BOD-267.

This commit is contained in:
Patrick Fic
2020-08-24 10:17:32 -07:00
parent 4b44fdfde5
commit 2acef8f726
18 changed files with 801 additions and 618 deletions

View File

@@ -0,0 +1,59 @@
import { Button, notification } from "antd";
import Axios from "axios";
import React, { useState } from "react";
import { useMutation } from "react-apollo";
import { useTranslation } from "react-i18next";
import { connect } from "react-redux";
import { createStructuredSelector } from "reselect";
import { UPDATE_JOB } from "../../graphql/jobs.queries";
import { selectBodyshop } from "../../redux/user/user.selectors";
const mapStateToProps = createStructuredSelector({
bodyshop: selectBodyshop,
});
export function JobCalculateTotals({ bodyshop, job }) {
const { t } = useTranslation();
const [loading, setLoading] = useState(false);
const [updateJob] = useMutation(UPDATE_JOB);
const handleCalculate = async () => {
setLoading(true);
const newTotals = (
await Axios.post("/job/totals", {
job: job,
shoprates: bodyshop.shoprates,
})
).data;
const result = await updateJob({
refetchQueries: ["GET_JOB_BY_PK"],
awaitRefetchQueries: true,
variables: {
jobId: job.id,
job: {
job_totals: newTotals,
},
},
});
if (!!!result.errors) {
notification["success"]({ message: t("jobs.successes.updated") });
} else {
notification["error"]({
message: t("jobs.errors.updating", {
error: JSON.stringify(result.errors),
}),
});
}
setLoading(false);
};
return (
<div>
<Button loading={loading} onClick={handleCalculate}>
{t("jobs.actions.recalculate")}
</Button>
</div>
);
}
export default connect(mapStateToProps, null)(JobCalculateTotals);

View File

@@ -1,12 +1,12 @@
import { Statistic, Typography, Row, Col } from "antd";
import React, { useEffect, useState } from "react";
import { Col, Result, Row, Statistic, Typography } from "antd";
import Dinero from "dinero.js";
import React from "react";
import { useTranslation } from "react-i18next";
import { connect } from "react-redux";
import { createStructuredSelector } from "reselect";
import { selectBodyshop } from "../../redux/user/user.selectors";
import LoadingSkeleton from "../loading-skeleton/loading-skeleton.component";
import JobCalculateTotals from "../job-calculate-totals/job-calculate-totals.component";
import "./job-totals-table.styles.scss";
import { CalculateJob } from "./job-totals.utility";
const mapStateToProps = createStructuredSelector({
//currentUser: selectCurrentUser
@@ -20,28 +20,16 @@ const colSpan = {
export function JobsTotalsTableComponent({ bodyshop, job }) {
const { t } = useTranslation();
const [totals, setTotals] = useState(null);
useEffect(() => {
setTotals(CalculateJob(job, bodyshop.shoprates));
}, [bodyshop, job]);
console.log("job", 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 />;
if (!!!job.job_totals) {
return (
<Result
title={t("jobs.errors.nofinancial")}
extra={<JobCalculateTotals job={job} />}
/>
);
}
return (
@@ -57,142 +45,142 @@ export function JobsTotalsTableComponent({ bodyshop, job }) {
<tr>
<td>{t("jobs.fields.rate_laa")}</td>
<td className="currency">
{totals.rates.laa.total.toFormat()}
{Dinero(job.job_totals.rates.laa.total).toFormat()}
</td>
<td>{`(${totals.rates.laa.hours.toFixed(2)} @ ${
totals.rates.laa.rate
<td>{`(${job.job_totals.rates.laa.hours.toFixed(2)} @ ${
job.job_totals.rates.laa.rate
})`}</td>
</tr>
<tr>
<td>{t("jobs.fields.rate_lab")}</td>
<td className="currency">
{totals.rates.lab.total.toFormat()}
{Dinero(job.job_totals.rates.lab.total).toFormat()}
</td>
<td>{`(${totals.rates.lab.hours.toFixed(2)} @ ${
totals.rates.lab.rate
<td>{`(${job.job_totals.rates.lab.hours.toFixed(2)} @ ${
job.job_totals.rates.lab.rate
})`}</td>
</tr>
<tr>
<td>{t("jobs.fields.rate_lad")}</td>
<td className="currency">
{totals.rates.lad.total.toFormat()}
{Dinero(job.job_totals.rates.lad.total).toFormat()}
</td>
<td>{`(${totals.rates.lad.hours.toFixed(2)} @ ${
totals.rates.lad.rate
<td>{`(${job.job_totals.rates.lad.hours.toFixed(2)} @ ${
job.job_totals.rates.lad.rate
})`}</td>
</tr>
<tr>
<td>{t("jobs.fields.rate_lae")}</td>
<td className="currency">
{totals.rates.lae.total.toFormat()}
{Dinero(job.job_totals.rates.lae.total).toFormat()}
</td>
<td>{`(${totals.rates.lae.hours.toFixed(2)} @ ${
totals.rates.lae.rate
<td>{`(${job.job_totals.rates.lae.hours.toFixed(2)} @ ${
job.job_totals.rates.lae.rate
})`}</td>
</tr>
<tr>
<td>{t("jobs.fields.rate_laf")}</td>
<td className="currency">
{totals.rates.laf.total.toFormat()}
{Dinero(job.job_totals.rates.laf.total).toFormat()}
</td>
<td>{`(${totals.rates.laf.hours.toFixed(2)} @ ${
totals.rates.laf.rate
<td>{`(${job.job_totals.rates.laf.hours.toFixed(2)} @ ${
job.job_totals.rates.laf.rate
})`}</td>
</tr>
<tr>
<td>{t("jobs.fields.rate_lag")}</td>
<td className="currency">
{totals.rates.lag.total.toFormat()}
{Dinero(job.job_totals.rates.lag.total).toFormat()}
</td>
<td>{`(${totals.rates.lag.hours.toFixed(2)} @ ${
totals.rates.lag.rate
<td>{`(${job.job_totals.rates.lag.hours.toFixed(2)} @ ${
job.job_totals.rates.lag.rate
})`}</td>
</tr>
<tr>
<td>{t("jobs.fields.rate_lam")}</td>
<td className="currency">
{totals.rates.lam.total.toFormat()}
{Dinero(job.job_totals.rates.lam.total).toFormat()}
</td>
<td>{`(${totals.rates.lam.hours.toFixed(2)} @ ${
totals.rates.lam.rate
<td>{`(${job.job_totals.rates.lam.hours.toFixed(2)} @ ${
job.job_totals.rates.lam.rate
})`}</td>
</tr>
<tr>
<td>{t("jobs.fields.rate_lar")}</td>
<td className="currency">
{totals.rates.lar.total.toFormat()}
{Dinero(job.job_totals.rates.lar.total).toFormat()}
</td>
<td>{`(${totals.rates.lar.hours.toFixed(2)} @ ${
totals.rates.lar.rate
<td>{`(${job.job_totals.rates.lar.hours.toFixed(2)} @ ${
job.job_totals.rates.lar.rate
})`}</td>
</tr>
<tr>
<td>{t("jobs.fields.rate_las")}</td>
<td className="currency">
{totals.rates.las.total.toFormat()}
{Dinero(job.job_totals.rates.las.total).toFormat()}
</td>
<td>{`(${totals.rates.las.hours.toFixed(2)} @ ${
totals.rates.las.rate
<td>{`(${job.job_totals.rates.las.hours.toFixed(2)} @ ${
job.job_totals.rates.las.rate
})`}</td>
</tr>
<tr>
<td>{t("jobs.fields.rate_lau")}</td>
<td className="currency">
{totals.rates.lau.total.toFormat()}
{Dinero(job.job_totals.rates.lau.total).toFormat()}
</td>
<td>{`(${totals.rates.lau.hours.toFixed(2)} @ ${
totals.rates.lau.rate
<td>{`(${job.job_totals.rates.lau.hours.toFixed(2)} @ ${
job.job_totals.rates.lau.rate
})`}</td>
</tr>
<tr>
<td>{t("jobs.fields.rate_la1")}</td>
<td className="currency">
{totals.rates.la1.total.toFormat()}
{Dinero(job.job_totals.rates.la1.total).toFormat()}
</td>
<td>{`(${totals.rates.la1.hours.toFixed(2)} @ ${
totals.rates.la1.rate
<td>{`(${job.job_totals.rates.la1.hours.toFixed(2)} @ ${
job.job_totals.rates.la1.rate
})`}</td>
</tr>
<tr>
<td>{t("jobs.fields.rate_la2")}</td>
<td className="currency">
{totals.rates.la2.total.toFormat()}
{Dinero(job.job_totals.rates.la2.total).toFormat()}
</td>
<td>{`(${totals.rates.la2.hours.toFixed(2)} @ ${
totals.rates.la2.rate
<td>{`(${job.job_totals.rates.la2.hours.toFixed(2)} @ ${
job.job_totals.rates.la2.rate
})`}</td>
</tr>
<tr>
<td>{t("jobs.fields.rate_la3")}</td>
<td className="currency">
{totals.rates.la3.total.toFormat()}
{Dinero(job.job_totals.rates.la3.total).toFormat()}
</td>
<td>{`(${totals.rates.la3.hours.toFixed(2)} @ ${
totals.rates.la3.rate
<td>{`(${job.job_totals.rates.la3.hours.toFixed(2)} @ ${
job.job_totals.rates.la3.rate
})`}</td>
</tr>
<tr>
<td>{t("jobs.fields.rate_la4")}</td>
<td className="currency">
{totals.rates.la4.total.toFormat()}
{Dinero(job.job_totals.rates.la4.total).toFormat()}
</td>
<td>{`(${totals.rates.la4.hours.toFixed(2)} @ ${
totals.rates.la4.rate
<td>{`(${job.job_totals.rates.la4.hours.toFixed(2)} @ ${
job.job_totals.rates.la4.rate
})`}</td>
</tr>
<tr>
<td>{t("jobs.fields.rate_atp")}</td>
<td className="currency">
{totals.rates.atp.total.toFormat()}
{Dinero(job.job_totals.rates.atp.total).toFormat()}
</td>
<td>{`(${totals.rates.atp.hours.toFixed(2)} @ ${
totals.rates.atp.rate
<td>{`(${job.job_totals.rates.atp.hours.toFixed(2)} @ ${
job.job_totals.rates.atp.rate
})`}</td>
</tr>
<tr>
<td>{t("jobs.labels.rates_subtotal")}</td>
<td>{totals.rates.subtotal.toFormat()}</td>
<td>{Dinero(job.job_totals.rates.subtotal).toFormat()}</td>
<td></td>
</tr>
</tbody>
@@ -203,45 +191,57 @@ export function JobsTotalsTableComponent({ bodyshop, job }) {
<div className="job-totals-half">
<table>
<tbody>
{Object.keys(totals.parts.parts.list).map((key, idx) => (
<tr key={idx}>
<td>{t(`jobs.fields.${key.toLowerCase()}`)}</td>
<td className="currency">
{totals.parts.parts.list[key].total.toFormat()}
</td>
<td></td>
</tr>
))}
{Object.keys(job.job_totals.parts.parts.list).map(
(key, idx) => (
<tr key={idx}>
<td>{t(`jobs.fields.${key.toLowerCase()}`)}</td>
<td className="currency">
{Dinero(
job.job_totals.parts.parts.list[key].total
).toFormat()}
</td>
<td></td>
</tr>
)
)}
<tr>
<td>{t("jobs.labels.partstotal")}</td>
<td className="currency">
{totals.parts.parts.total.toFormat()}
{Dinero(job.job_totals.parts.parts.total).toFormat()}
</td>
<td>{`(${totals.parts.parts.subtotal.toFormat()} ± ${totals.parts.parts.adjustments.toFormat()})`}</td>
<td>{`(${Dinero(
job.job_totals.parts.parts.subtotal
).toFormat()} ± ${Dinero(
job.job_totals.parts.parts.adjustments
).toFormat()})`}</td>
</tr>
<tr>
<td>{t("jobs.labels.subletstotal")}</td>
<td className="currency">
{totals.parts.sublets.total.toFormat()}
{Dinero(job.job_totals.parts.sublets.total).toFormat()}
</td>
<td>{`(${totals.parts.sublets.subtotal.toFormat()} ± ${totals.parts.sublets.adjustments.toFormat()})`}</td>
<td>{`(${Dinero(
job.job_totals.parts.sublets.subtotal
).toFormat()} ± ${Dinero(
job.job_totals.parts.sublets.adjustments
).toFormat()})`}</td>
</tr>
<tr>
<td>{t("jobs.labels.mapa")}</td>
<td className="currency">
{totals.rates.mapa.total.toFormat()}
{Dinero(job.job_totals.rates.mapa.total).toFormat()}
</td>
<td>{`(${totals.rates.mapa.hours.toFixed(2)} @ ${
totals.rates.mapa.rate
<td>{`(${job.job_totals.rates.mapa.hours.toFixed(2)} @ ${
job.job_totals.rates.mapa.rate
})`}</td>
</tr>
<tr>
<td>{t("jobs.labels.mash")}</td>
<td className="currency">
{totals.rates.mash.total.toFormat()}
{Dinero(job.job_totals.rates.mash.total).toFormat()}
</td>
<td>{`(${totals.rates.mash.hours.toFixed(2)} @ ${
totals.rates.mash.rate
<td>{`(${job.job_totals.rates.mash.hours.toFixed(2)} @ ${
job.job_totals.rates.mash.rate
})`}</td>
</tr>
</tbody>
@@ -260,15 +260,15 @@ export function JobsTotalsTableComponent({ bodyshop, job }) {
>
<Statistic
title={t("jobs.labels.state_tax_amt")}
value={totals.totals.state_tax.toFormat()}
value={Dinero(job.job_totals.totals.state_tax).toFormat()}
/>
<Statistic
title={t("jobs.labels.local_tax_amt")}
value={totals.totals.local_tax.toFormat()}
value={Dinero(job.job_totals.totals.local_tax).toFormat()}
/>
<Statistic
title={t("jobs.labels.federal_tax_amt")}
value={totals.totals.federal_tax.toFormat()}
value={Dinero(job.job_totals.totals.federal_tax).toFormat()}
/>
</div>
<div
@@ -285,17 +285,18 @@ export function JobsTotalsTableComponent({ bodyshop, job }) {
>
<Statistic
title={t("jobs.labels.subtotal")}
value={totals.totals.subtotal.toFormat()}
value={Dinero(job.job_totals.totals.subtotal).toFormat()}
/>
<Statistic
title={t("jobs.labels.total_repairs")}
value={totals.totals.total_repairs.toFormat()}
value={Dinero(job.job_totals.totals.total_repairs).toFormat()}
/>
<Statistic
title={t("jobs.labels.net_repairs")}
value={totals.totals.net_repairs.toFormat()}
value={Dinero(job.job_totals.totals.net_repairs).toFormat()}
/>
</div>
<JobCalculateTotals job={job} />
</div>
</Col>
</Row>

View File

@@ -1,322 +1,322 @@
import Dinero from "dinero.js";
import { logImEXEvent } from "../../firebase/firebase.utils";
// import Dinero from "dinero.js";
// import { logImEXEvent } from "../../firebase/firebase.utils";
export function CalculateJob(job, shoprates) {
logImEXEvent("job_calculate_total");
// export function CalculateJob(job, shoprates) {
// logImEXEvent("job_calculate_total");
let ret = {
parts: CalculatePartsTotals(job.joblines),
rates: CalculateRatesTotals(job, shoprates),
custPayable: CalculateCustPayable(job),
};
ret.totals = CalculateTaxesTotals(job, ret);
console.log("CalculateJob -> Final", ret);
return ret;
}
// let ret = {
// parts: CalculatePartsTotals(job.joblines),
// 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
.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??
// 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);
// 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;
}
// 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;
// //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 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;
// 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;
}
// 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 }),
},
}
);
// 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,
},
};
}
// 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 }),
};
// 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);
// ret.total = ret.deductible
// .add(ret.federal_tax)
// .add(ret.federal_tax)
// .add(ret.other_customer_amount)
// .add(ret.dep_taxes);
return ret;
}
// return ret;
// }

View File

@@ -1,11 +1,13 @@
import { useMutation, useQuery } from "@apollo/react-hooks";
import { notification } from "antd";
import Axios from "axios";
import Dinero from "dinero.js";
import React, { useState } from "react";
import { useTranslation } from "react-i18next";
import { connect } from "react-redux";
import { useHistory } from "react-router-dom";
import { createStructuredSelector } from "reselect";
import { CalculateJob } from "../../components/job-totals-table/job-totals.utility";
import { logImEXEvent } from "../../firebase/firebase.utils";
import {
DELETE_ALL_AVAILABLE_NEW_JOBS,
QUERY_AVAILABLE_NEW_JOBS,
@@ -15,7 +17,6 @@ import { selectBodyshop } from "../../redux/user/user.selectors";
import AlertComponent from "../alert/alert.component";
import LoadingSpinner from "../loading-spinner/loading-spinner.component";
import JobsAvailableComponent from "./jobs-available-new.component";
import { logImEXEvent } from "../../firebase/firebase.utils";
const mapStateToProps = createStructuredSelector({
bodyshop: selectBodyshop,
@@ -40,7 +41,7 @@ export function JobsAvailableContainer({
const [insertNewJob] = useMutation(INSERT_NEW_JOB);
const [loadEstData, estData] = estDataLazyLoad;
const onModalOk = () => {
const onModalOk = async () => {
logImEXEvent("job_import_new");
setModalVisible(false);
@@ -59,19 +60,21 @@ export function JobsAvailableContainer({
message: t("jobs.errors.creating", { error: "No job data present." }),
});
} else {
const newTotals = CalculateJob(
{
...estData.data.available_jobs_by_pk.est_data,
joblines: estData.data.available_jobs_by_pk.est_data.joblines.data,
},
bodyshop.shoprates
);
const newTotals = (
await Axios.post("/job/totals", {
job: {
...estData.data.available_jobs_by_pk.est_data,
joblines: estData.data.available_jobs_by_pk.est_data.joblines.data,
},
shoprates: bodyshop.shoprates,
})
).data;
const newJob = {
...estData.data.available_jobs_by_pk.est_data,
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,
};
insertNewJob({

View File

@@ -1,12 +1,14 @@
import { useApolloClient, useMutation, useQuery } from "@apollo/react-hooks";
import { notification } from "antd";
import Axios from "axios";
import Dinero from "dinero.js";
import gql from "graphql-tag";
import React, { useState } from "react";
import { useTranslation } from "react-i18next";
import { connect } from "react-redux";
import { useHistory } from "react-router-dom";
import { createStructuredSelector } from "reselect";
import { CalculateJob } from "../../components/job-totals-table/job-totals.utility";
import { logImEXEvent } from "../../firebase/firebase.utils";
import {
DELETE_ALL_AVAILABLE_SUPPLEMENT_JOBS,
QUERY_AVAILABLE_SUPPLEMENT_JOBS,
@@ -18,7 +20,6 @@ import LoadingSpinner from "../loading-spinner/loading-spinner.component";
import JobsAvailableSupplementComponent from "./jobs-available-supplement.component";
import { GetSupplementDelta } from "./jobs-available-supplement.estlines.util";
import HeaderFields from "./jobs-available-supplement.headerfields";
import { logImEXEvent } from "../../firebase/firebase.utils";
const mapStateToProps = createStructuredSelector({
bodyshop: selectBodyshop,
@@ -74,13 +75,15 @@ export function JobsAvailableSupplementContainer({
HeaderFields.forEach((item) => delete supp[item]);
}
const newTotals = CalculateJob(
{
...estData.data.available_jobs_by_pk.est_data,
joblines: estData.data.available_jobs_by_pk.est_data.joblines.data,
},
bodyshop.shoprates
);
const newTotals = (
await Axios.post("/job/totals", {
job: {
...estData.data.available_jobs_by_pk.est_data,
joblines: estData.data.available_jobs_by_pk.est_data.joblines.data,
},
shoprates: bodyshop.shoprates,
})
).data;
let suppDelta = await GetSupplementDelta(
client,
@@ -99,9 +102,9 @@ export function JobsAvailableSupplementContainer({
jobId: selectedJob,
job: {
...supp,
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,
},
},
})

View File

@@ -16,7 +16,7 @@ export function JobsCloseSaveButton({
bodyshop,
suspenseAmount,
jobId,
jobTotals,
labMatAllocations,
partsAllocations,
setInvoicedState,
@@ -60,9 +60,10 @@ export function JobsCloseSaveButton({
return (
<Button
onClick={handleSave}
type='primary'
type="primary"
disabled={suspenseAmount > 0 || disabled}
loading={loading}>
loading={loading}
>
{t("general.actions.save")}
</Button>
);

View File

@@ -1,6 +1,8 @@
import { Descriptions, Statistic } from "antd";
import React from "react";
import { useTranslation } from "react-i18next";
import Dinero from "dinero.js";
export default function JobsCloseTotals({
jobTotals,
labMatTotal,
@@ -17,47 +19,57 @@ export default function JobsCloseTotals({
>
<Descriptions.Item label={t("jobs.labels.partstotal")}>
<Statistic
value={jobTotals.parts.parts.total.toFormat()}
suffix={`(${jobTotals.parts.parts.subtotal.toFormat()} ± ${jobTotals.parts.parts.adjustments.toFormat()})`}
value={Dinero(jobTotals.parts.parts.total).toFormat()}
suffix={`(${Dinero(
jobTotals.parts.parts.subtotal
).toFormat()} ± ${Dinero(
jobTotals.parts.parts.adjustments
).toFormat()})`}
/>
</Descriptions.Item>
<Descriptions.Item label={t("jobs.labels.subletstotal")}>
<Statistic
value={jobTotals.parts.sublets.total.toFormat()}
suffix={`(${jobTotals.parts.sublets.subtotal.toFormat()} ± ${jobTotals.parts.sublets.adjustments.toFormat()})`}
value={Dinero(jobTotals.parts.sublets.total).toFormat()}
suffix={`(${Dinero(
jobTotals.parts.sublets.subtotal
).toFormat()} ± ${Dinero(
jobTotals.parts.sublets.adjustments
).toFormat()})`}
/>
</Descriptions.Item>
<Descriptions.Item label={t("jobs.labels.subtotal")}>
<Statistic value={jobTotals.totals.subtotal.toFormat()} />
<Statistic value={Dinero(jobTotals.totals.subtotal).toFormat()} />
</Descriptions.Item>
<Descriptions.Item label={t("jobs.labels.federal_tax_amt")}>
<Statistic value={jobTotals.totals.federal_tax.toFormat()} />
<Statistic value={Dinero(jobTotals.totals.federal_tax).toFormat()} />
</Descriptions.Item>
<Descriptions.Item label={t("jobs.labels.state_tax_amt")}>
<Statistic value={jobTotals.totals.state_tax.toFormat()} />
<Statistic value={Dinero(jobTotals.totals.state_tax).toFormat()} />
</Descriptions.Item>
<Descriptions.Item label={t("jobs.labels.local_tax_amt")}>
<Statistic value={jobTotals.totals.local_tax.toFormat()} />
<Statistic value={Dinero(jobTotals.totals.local_tax).toFormat()} />
</Descriptions.Item>
</Descriptions>
<Statistic
title={t("jobs.labels.total_repairs")}
value={jobTotals.totals.total_repairs.toFormat()}
value={Dinero(jobTotals.totals.total_repairs).toFormat()}
/>
<Statistic
title={t("jobs.labels.net_repairs")}
value={jobTotals.totals.net_repairs.toFormat()}
value={Dinero(jobTotals.totals.net_repairs).toFormat()}
/>
<Statistic
title={t("jobs.labels.suspense")}
valueStyle={{
color:
jobTotals.totals.subtotal.subtract(labMatTotal).subtract(partsTotal)
Dinero(jobTotals.totals.subtotal)
.subtract(labMatTotal)
.subtract(partsTotal)
.getAmount() === 0
? "green"
: "red",
}}
value={jobTotals.totals.subtotal
value={Dinero(jobTotals.totals.subtotal)
.subtract(labMatTotal)
.subtract(partsTotal)
.toFormat()}