Implemented auto insertion of lbr adjustments on bill posting IO-571

This commit is contained in:
Patrick Fic
2021-01-11 11:11:46 -08:00
parent 12f14e5425
commit ae7f67461b
3 changed files with 70 additions and 9 deletions

View File

@@ -1,11 +1,16 @@
import { useMutation } from "@apollo/react-hooks";
import { useApolloClient, useMutation } from "@apollo/react-hooks";
import { Button, Form, Modal, notification } from "antd";
import _ from "lodash";
import React, { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import { connect } from "react-redux";
import { createStructuredSelector } from "reselect";
import { INSERT_NEW_BILL } from "../../graphql/bills.queries";
import { UPDATE_JOB_LINE_STATUS } from "../../graphql/jobs-lines.queries";
import {
QUERY_JOB_LBR_ADJUSTMENTS,
UPDATE_JOB,
} from "../../graphql/jobs.queries";
import { toggleModalVisible } from "../../redux/modals/modals.actions";
import { selectBillEnterModal } from "../../redux/modals/modals.selectors";
import {
@@ -36,6 +41,7 @@ function BillEnterModalContainer({
const [insertBill] = useMutation(INSERT_NEW_BILL);
const [updateJobLines] = useMutation(UPDATE_JOB_LINE_STATUS);
const [loading, setLoading] = useState(false);
const client = useApolloClient();
const handleFinish = async (values) => {
console.log(
@@ -45,7 +51,7 @@ function BillEnterModalContainer({
setLoading(true);
const { upload, location, ...remainingValues } = values;
const adjustmentsToInsert = [];
let adjustmentsToInsert = {};
const r1 = await insertBill({
variables: {
@@ -57,11 +63,14 @@ function BillEnterModalContainer({
remainingValues.billlines &&
remainingValues.billlines.map((i) => {
const { deductfromlabor, lbr_adjustment, ...restI } = i;
console.log(
"🚀 ~ file: bill-enter-modal.container.jsx ~ line 60 ~ remainingValues.billlines.map ~ lbr_adjustment",
lbr_adjustment
);
if (deductfromlabor) {
adjustmentsToInsert.push({
...lbr_adjustment,
act_price: i.act_price,
});
adjustmentsToInsert[lbr_adjustment.mod_lbr_ty] =
(adjustmentsToInsert[lbr_adjustment.mod_lbr_ty] || 0) -
restI.actual_price / lbr_adjustment.rate;
}
return {
...restI,
@@ -73,6 +82,49 @@ function BillEnterModalContainer({
],
},
});
console.log("adjustmentsToInsert", adjustmentsToInsert);
const adjKeys = Object.keys(adjustmentsToInsert);
if (adjKeys.length > 0) {
//Query the adjustments, merge, and update them.
const existingAdjustments = await client.query({
query: QUERY_JOB_LBR_ADJUSTMENTS,
variables: {
id: values.jobid,
},
});
const newAdjustments = _.cloneDeep(
existingAdjustments.data.jobs_by_pk.lbr_adjustments
);
console.log(
"🚀 ~ file: bill-enter-modal.container.jsx ~ line 99 ~ handleFinish ~ newAdjustments",
newAdjustments
);
adjKeys.forEach((key) => {
newAdjustments[key] =
(newAdjustments[key] || 0) + adjustmentsToInsert[key];
});
console.log(
"🚀 ~ file: bill-enter-modal.container.jsx ~ line 109 ~ adjKeys.forEach ~ newAdjustments",
newAdjustments
);
const jobUpdate = client.mutate({
mutation: UPDATE_JOB,
variables: {
jobId: values.jobid,
job: { lbr_adjustments: newAdjustments },
},
});
if (!!jobUpdate.errors) {
notification["error"]({
message: t("jobs.errors.saving", {
message: JSON.stringify(jobUpdate.errors),
}),
});
}
}
if (!!r1.errors) {
setLoading(false);
@@ -96,8 +148,6 @@ function BillEnterModalContainer({
},
});
console.log("adjustmentsToInsert", adjustmentsToInsert);
/////////////////////////
if (upload && upload.length > 0) {
//insert Each of the documents?

View File

@@ -235,7 +235,9 @@ export function BillEnterModalLinesComponent({
</Form.Item>
<Form.Item
shouldUpdate={(prev, cur) =>
prev.billlines[index] &&
prev.billlines[index].deductfromlabor !==
cur.billlines[index] &&
cur.billlines[index].deductfromlabor
}
>
@@ -322,7 +324,7 @@ export function BillEnterModalLinesComponent({
},
]}
>
<InputNumber precision={2} />
<InputNumber precision={2} min={0.01} />
</Form.Item>
</div>
);

View File

@@ -1309,3 +1309,12 @@ export const generate_UPDATE_JOB_KANBAN = (
}
`;
};
export const QUERY_JOB_LBR_ADJUSTMENTS = gql`
query QUERY_JOB_LBR_ADJUSTMENTS($id: uuid!) {
jobs_by_pk(id: $id) {
id
lbr_adjustments
}
}
`;