Files
bodyshop/client/src/components/job-reconciliation-totals/job-reconciliation-totals.utility.js
2021-03-25 08:41:36 -07:00

107 lines
2.9 KiB
JavaScript

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 && !(bl.jobline && bl.jobline.removed))
.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) => {
return 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 && !jl.removed)
.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)));
};