Added automatic reconciliation for jobs. BOD-406
This commit is contained in:
@@ -40,9 +40,9 @@ export default function JobReconciliationModalComponent({ job, bills }) {
|
||||
<Row>
|
||||
<JobReconciliationTotals
|
||||
jobLines={jobLineData}
|
||||
selectedJobLines={jobLineState[0]}
|
||||
jobLineState={jobLineState}
|
||||
billLines={invoiceLineData}
|
||||
selectedBillLines={billLineState[0]}
|
||||
billLineState={billLineState}
|
||||
/>
|
||||
</Row>
|
||||
</div>
|
||||
|
||||
@@ -1,16 +1,23 @@
|
||||
import React, { useMemo } from "react";
|
||||
import { Button, Space, Statistic } from "antd";
|
||||
import Dinero from "dinero.js";
|
||||
import _ from "lodash";
|
||||
import { Space, Statistic } from "antd";
|
||||
import React, { useMemo, useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import {
|
||||
reconcileByAssocLine,
|
||||
reconcileByPrice,
|
||||
} from "./job-reconciliation-totals.utility";
|
||||
|
||||
export default function JobReconciliationTotals({
|
||||
billLines,
|
||||
jobLines,
|
||||
selectedBillLines,
|
||||
selectedJobLines,
|
||||
jobLineState,
|
||||
billLineState,
|
||||
}) {
|
||||
const [errors, setErrors] = useState([]);
|
||||
const { t } = useTranslation();
|
||||
const [selectedBillLines, setSelectedBillLines] = billLineState;
|
||||
const [selectedJobLines, setSelectedJobLines] = jobLineState;
|
||||
|
||||
const totals = useMemo(() => {
|
||||
const jlLookup = _.keyBy(selectedJobLines, (i) => i);
|
||||
@@ -20,7 +27,6 @@ export default function JobReconciliationTotals({
|
||||
joblinesTotal: jobLines
|
||||
.filter((jl) => !!jlLookup[jl.id])
|
||||
.reduce((acc, val) => {
|
||||
console.log("acc :>> ", val);
|
||||
return acc.add(
|
||||
Dinero({ amount: val.act_price * 100 }).multiply(val.part_qty || 1)
|
||||
);
|
||||
@@ -38,15 +44,53 @@ export default function JobReconciliationTotals({
|
||||
}, [billLines, jobLines, selectedBillLines, selectedJobLines]);
|
||||
|
||||
return (
|
||||
<Space direction="horizontal">
|
||||
<Statistic
|
||||
title={t("jobs.labels.reconciliation.joblinestotal")}
|
||||
value={totals.joblinesTotal.toFormat()}
|
||||
/>
|
||||
<Statistic
|
||||
title={t("jobs.labels.reconciliation.billlinestotal")}
|
||||
value={totals.billLinesTotal.toFormat()}
|
||||
/>
|
||||
</Space>
|
||||
<div>
|
||||
<Space direction="horizontal">
|
||||
<Statistic
|
||||
title={t("jobs.labels.reconciliation.joblinestotal")}
|
||||
value={totals.joblinesTotal.toFormat()}
|
||||
/>
|
||||
<Statistic
|
||||
title={t("jobs.labels.reconciliation.billlinestotal")}
|
||||
value={totals.billLinesTotal.toFormat()}
|
||||
/>
|
||||
<Button
|
||||
onClick={() =>
|
||||
reconcileByAssocLine(
|
||||
jobLines,
|
||||
jobLineState,
|
||||
billLines,
|
||||
billLineState,
|
||||
setErrors
|
||||
)
|
||||
}
|
||||
>
|
||||
{t("jobs.labels.reconciliation.byassoc")}
|
||||
</Button>
|
||||
<Button
|
||||
onClick={() =>
|
||||
reconcileByPrice(
|
||||
jobLines,
|
||||
jobLineState,
|
||||
billLines,
|
||||
billLineState,
|
||||
setErrors
|
||||
)
|
||||
}
|
||||
>
|
||||
{t("jobs.labels.reconciliation.byprice")}
|
||||
</Button>
|
||||
</Space>
|
||||
{errors.length > 0 && (
|
||||
<div>
|
||||
{t("general.labels.errors")}
|
||||
<ul>
|
||||
{errors.map((error, idx) => (
|
||||
<li key={idx}>{error}</li>
|
||||
))}
|
||||
</ul>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
import i18next from "i18next";
|
||||
import _ from "lodash";
|
||||
export const reconcileByAssocLine = (
|
||||
jobLines,
|
||||
jobLineState,
|
||||
billLines,
|
||||
billLineState,
|
||||
setErrors
|
||||
) => {
|
||||
const [selectedBillLines, setSelectedBillLines] = billLineState;
|
||||
const [selectedJobLines, setSelectedJobLines] = jobLineState;
|
||||
|
||||
const allJoblinesFromBills = billLines
|
||||
.filter((bl) => !!bl.joblineid)
|
||||
.map((bl) => bl.joblineid);
|
||||
|
||||
const duplicatedJobLinesbyInvoiceId = _.filter(
|
||||
allJoblinesFromBills,
|
||||
(val, i, iteratee) => _.includes(iteratee, val, i + 1)
|
||||
);
|
||||
|
||||
if (duplicatedJobLinesbyInvoiceId.length > 0)
|
||||
setErrors((errors) => [
|
||||
...errors,
|
||||
..._.uniqBy(duplicatedJobLinesbyInvoiceId).map((dupedId) =>
|
||||
i18next.t("jobs.labels.reconciliation.multiplebilllines", {
|
||||
line_desc: jobLines.find((j) => j.id === dupedId).line_desc,
|
||||
})
|
||||
),
|
||||
]);
|
||||
|
||||
setSelectedBillLines(
|
||||
_.union(
|
||||
selectedBillLines,
|
||||
billLines.filter((bl) => !!bl.joblineid).map((bl) => bl.id)
|
||||
)
|
||||
);
|
||||
|
||||
setSelectedJobLines(
|
||||
_.union(selectedJobLines, _.uniqBy(allJoblinesFromBills))
|
||||
);
|
||||
};
|
||||
|
||||
export const reconcileByPrice = (
|
||||
jobLines,
|
||||
jobLineState,
|
||||
billLines,
|
||||
billLineState,
|
||||
setErrors
|
||||
) => {
|
||||
const [selectedBillLines, setSelectedBillLines] = billLineState;
|
||||
const [selectedJobLines, setSelectedJobLines] = jobLineState;
|
||||
|
||||
const allActPricesFromJobs = jobLines.map((jl) => jl.act_price);
|
||||
const duplicateActPrices = _.filter(
|
||||
allActPricesFromJobs,
|
||||
(val, i, iteratee) => _.includes(iteratee, val, i + 1)
|
||||
);
|
||||
|
||||
if (duplicateActPrices.length > 0)
|
||||
setErrors((errors) => [
|
||||
...errors,
|
||||
..._.uniqBy(duplicateActPrices).map((dupeAp) =>
|
||||
i18next.t("jobs.labels.reconciliation.multipleactprices", {
|
||||
act_price: dupeAp,
|
||||
})
|
||||
),
|
||||
]);
|
||||
|
||||
const foundJobLines = [];
|
||||
var foundBillLines = [];
|
||||
const actPricesWithMoreThan1MatchingBill = [];
|
||||
|
||||
jobLines.forEach((jl) => {
|
||||
const matchingBillLineIds = billLines
|
||||
.filter((bl) => bl.actual_price === jl.act_price)
|
||||
.map((bl) => bl.id);
|
||||
|
||||
if (matchingBillLineIds.length > 1) {
|
||||
actPricesWithMoreThan1MatchingBill.push(jl.act_price);
|
||||
}
|
||||
|
||||
foundBillLines = [...foundBillLines, ...matchingBillLineIds];
|
||||
if (matchingBillLineIds.length > 0) {
|
||||
foundJobLines.push(jl.id);
|
||||
}
|
||||
});
|
||||
|
||||
setErrors((errors) => [
|
||||
...errors,
|
||||
..._.uniqBy(duplicateActPrices).map((dupeAp) =>
|
||||
i18next.t("jobs.labels.reconciliation.multipleactprices", {
|
||||
act_price: dupeAp,
|
||||
})
|
||||
),
|
||||
..._.uniqBy(actPricesWithMoreThan1MatchingBill).map((act_price) =>
|
||||
i18next.t("jobs.labels.reconciliation.multiplebillsforactprice", {
|
||||
act_price: act_price,
|
||||
})
|
||||
),
|
||||
]);
|
||||
|
||||
setSelectedBillLines(_.union(selectedBillLines, _.uniqBy(foundBillLines)));
|
||||
|
||||
setSelectedJobLines(_.union(selectedJobLines, _.uniqBy(foundJobLines)));
|
||||
};
|
||||
Reference in New Issue
Block a user