BOD-62 Added new job calculations on import + supplement.
This commit is contained in:
@@ -1,31 +1,36 @@
|
||||
import { useMutation, useQuery } from "@apollo/react-hooks";
|
||||
import { notification } from "antd";
|
||||
import React, { useState } from "react";
|
||||
import { useMutation, useQuery } from "@apollo/react-hooks";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { withRouter } from "react-router-dom";
|
||||
import {
|
||||
DELETE_ALL_AVAILABLE_SUPPLEMENT_JOBS,
|
||||
QUERY_AVAILABLE_SUPPLEMENT_JOBS
|
||||
} from "../../graphql/available-jobs.queries";
|
||||
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 { DELETE_ALL_AVAILABLE_SUPPLEMENT_JOBS, QUERY_AVAILABLE_SUPPLEMENT_JOBS } from "../../graphql/available-jobs.queries";
|
||||
import { UPDATE_JOB } from "../../graphql/jobs.queries";
|
||||
import { selectBodyshop } from "../../redux/user/user.selectors";
|
||||
import AlertComponent from "../alert/alert.component";
|
||||
import LoadingSpinner from "../loading-spinner/loading-spinner.component";
|
||||
import JobsAvailableSupplementComponent from "./jobs-available-supplement.component";
|
||||
import HeaderFields from "./jobs-available-supplement.headerfields";
|
||||
|
||||
export default withRouter(function JobsAvailableSupplementContainer({
|
||||
const mapStateToProps = createStructuredSelector({
|
||||
bodyshop: selectBodyshop,
|
||||
});
|
||||
|
||||
export function JobsAvailableSupplementContainer({
|
||||
deleteJob,
|
||||
estDataLazyLoad,
|
||||
history
|
||||
bodyshop,
|
||||
}) {
|
||||
const { loading, error, data, refetch } = useQuery(
|
||||
QUERY_AVAILABLE_SUPPLEMENT_JOBS,
|
||||
{
|
||||
fetchPolicy: "network-only"
|
||||
fetchPolicy: "network-only",
|
||||
}
|
||||
);
|
||||
const { t } = useTranslation();
|
||||
|
||||
const history = useHistory();
|
||||
const [deleteAllNewJobs] = useMutation(DELETE_ALL_AVAILABLE_SUPPLEMENT_JOBS);
|
||||
|
||||
const [modalVisible, setModalVisible] = useState(false);
|
||||
@@ -49,48 +54,63 @@ export default withRouter(function JobsAvailableSupplementContainer({
|
||||
//We don't have the right data. Error!
|
||||
setInsertLoading(false);
|
||||
notification["error"]({
|
||||
message: t("jobs.errors.creating", { error: "No job data present." })
|
||||
message: t("jobs.errors.creating", { error: "No job data present." }),
|
||||
});
|
||||
} else {
|
||||
//create upsert job
|
||||
let supp = estData.data.available_jobs_by_pk.est_data;
|
||||
console.log("supp before", supp);
|
||||
delete supp.joblines;
|
||||
|
||||
//TODO How to update the estimate lines.
|
||||
delete supp.owner;
|
||||
delete supp.vehicle;
|
||||
|
||||
if (importOptions.overrideHeaders) {
|
||||
HeaderFields.forEach(item => delete supp[item]);
|
||||
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
|
||||
);
|
||||
|
||||
console.log('newTotals', newTotals)
|
||||
delete supp.joblines;
|
||||
updateJob({
|
||||
variables: {
|
||||
jobId: selectedJob,
|
||||
job: supp
|
||||
}
|
||||
job: {
|
||||
...supp,
|
||||
clm_total: newTotals.totals.total_repairs,
|
||||
owner_owing: newTotals.custPayable.total,
|
||||
job_totals: newTotals,
|
||||
},
|
||||
},
|
||||
})
|
||||
.then(r => {
|
||||
.then((r) => {
|
||||
notification["success"]({
|
||||
message: t("jobs.successes.supplemented"),
|
||||
onClick: () => {
|
||||
history.push(
|
||||
`/manage/jobs/${r.data.update_jobs.returning[0].id}`
|
||||
);
|
||||
}
|
||||
},
|
||||
});
|
||||
//Job has been inserted. Clean up the available jobs record.
|
||||
deleteJob({
|
||||
variables: { id: estData.data.available_jobs_by_pk.id }
|
||||
}).then(r => {
|
||||
variables: { id: estData.data.available_jobs_by_pk.id },
|
||||
}).then((r) => {
|
||||
refetch();
|
||||
setInsertLoading(false);
|
||||
});
|
||||
})
|
||||
.catch(r => {
|
||||
.catch((r) => {
|
||||
//error while inserting
|
||||
notification["error"]({
|
||||
message: t("jobs.errors.creating", { error: r.message })
|
||||
message: t("jobs.errors.creating", { error: r.message }),
|
||||
});
|
||||
refetch();
|
||||
setInsertLoading(false);
|
||||
@@ -128,4 +148,5 @@ export default withRouter(function JobsAvailableSupplementContainer({
|
||||
/>
|
||||
</LoadingSpinner>
|
||||
);
|
||||
});
|
||||
}
|
||||
export default connect(mapStateToProps, null)(JobsAvailableSupplementContainer);
|
||||
|
||||
Reference in New Issue
Block a user