From ae7f67461b9a99b0efc39bf98ff2913ffd576b0c Mon Sep 17 00:00:00 2001 From: Patrick Fic <> Date: Mon, 11 Jan 2021 11:11:46 -0800 Subject: [PATCH] Implemented auto insertion of lbr adjustments on bill posting IO-571 --- .../bill-enter-modal.container.jsx | 66 ++++++++++++++++--- .../bill-form/bill-form.lines.component.jsx | 4 +- client/src/graphql/jobs.queries.js | 9 +++ 3 files changed, 70 insertions(+), 9 deletions(-) diff --git a/client/src/components/bill-enter-modal/bill-enter-modal.container.jsx b/client/src/components/bill-enter-modal/bill-enter-modal.container.jsx index 55081c128..d2440b6d1 100644 --- a/client/src/components/bill-enter-modal/bill-enter-modal.container.jsx +++ b/client/src/components/bill-enter-modal/bill-enter-modal.container.jsx @@ -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? diff --git a/client/src/components/bill-form/bill-form.lines.component.jsx b/client/src/components/bill-form/bill-form.lines.component.jsx index 11e044c07..06b14f3c2 100644 --- a/client/src/components/bill-form/bill-form.lines.component.jsx +++ b/client/src/components/bill-form/bill-form.lines.component.jsx @@ -235,7 +235,9 @@ export function BillEnterModalLinesComponent({