IO-2244 Restrict claimable productive hours based on remaining hours
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { useQuery } from "@apollo/client";
|
||||
import { useLazyQuery, useQuery } from "@apollo/client";
|
||||
import { Form, Input, InputNumber, Select, Switch } from "antd";
|
||||
import React from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
@@ -18,6 +18,7 @@ import LayoutFormRow from "../layout-form-row/layout-form-row.component";
|
||||
import LoadingSkeleton from "../loading-skeleton/loading-skeleton.component";
|
||||
import { HasRbacAccess } from "../rbac-wrapper/rbac-wrapper.component";
|
||||
import TimeTicketList from "../time-ticket-list/time-ticket-list.component";
|
||||
import { CalculateAllocationsTotals } from "../labor-allocations-table/labor-allocations-table.utility";
|
||||
|
||||
const mapStateToProps = createStructuredSelector({
|
||||
bodyshop: selectBodyshop,
|
||||
@@ -38,7 +39,11 @@ export function TimeTicketModalComponent({
|
||||
employeeSelectDisabled,
|
||||
}) {
|
||||
const { t } = useTranslation();
|
||||
|
||||
const [loadLineTicketData, { called, loading, data: lineTicketData }] =
|
||||
useLazyQuery(GET_LINE_TICKET_BY_PK, {
|
||||
fetchPolicy: "network-only",
|
||||
nextFetchPolicy: "network-only",
|
||||
});
|
||||
const CostCenterSelect = ({ emps, value, ...props }) => {
|
||||
return (
|
||||
<Select
|
||||
@@ -176,6 +181,47 @@ export function TimeTicketModalComponent({
|
||||
label={t("timetickets.fields.productivehrs")}
|
||||
name="productivehrs"
|
||||
rules={[
|
||||
({ getFieldValue }) => ({
|
||||
validator(rule, value) {
|
||||
if (!bodyshop.tt_enforce_hours_for_tech_console) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
if (
|
||||
!value ||
|
||||
getFieldValue("cost_center") === null ||
|
||||
!lineTicketData
|
||||
)
|
||||
return Promise.resolve();
|
||||
|
||||
//Check the cost center,
|
||||
const totals = CalculateAllocationsTotals(
|
||||
bodyshop,
|
||||
lineTicketData.joblines,
|
||||
lineTicketData.timetickets,
|
||||
lineTicketData.jobs_by_pk.lbr_adjustments
|
||||
);
|
||||
|
||||
const fieldTypeToCheck =
|
||||
bodyshop.cdk_dealerid || bodyshop.pbs_serialnumber
|
||||
? "mod_lbr_ty"
|
||||
: "cost_center";
|
||||
|
||||
const costCenterDiff = totals.find(
|
||||
(total) =>
|
||||
total[fieldTypeToCheck] === getFieldValue("cost_center")
|
||||
)?.difference;
|
||||
|
||||
if (value > costCenterDiff)
|
||||
return Promise.reject(
|
||||
t(
|
||||
"timetickets.validation.hoursenteredmorethanavailable"
|
||||
)
|
||||
);
|
||||
else {
|
||||
return Promise.resolve();
|
||||
}
|
||||
},
|
||||
}),
|
||||
{
|
||||
required:
|
||||
form.getFieldValue("cost_center") !==
|
||||
@@ -291,23 +337,28 @@ export function TimeTicketModalComponent({
|
||||
</Form.Item>
|
||||
</LayoutFormRow>
|
||||
<Form.Item dependencies={["jobid"]}>
|
||||
{() => (
|
||||
<LaborAllocationContainer
|
||||
jobid={form.getFieldValue("jobid") || null}
|
||||
/>
|
||||
)}
|
||||
{() => {
|
||||
const jobid = form.getFieldValue("jobid");
|
||||
if (
|
||||
(!called && jobid) ||
|
||||
(jobid && lineTicketData?.jobs_by_pk?.id !== jobid && !loading)
|
||||
) {
|
||||
loadLineTicketData({ variables: { id: jobid } });
|
||||
}
|
||||
return (
|
||||
<LaborAllocationContainer
|
||||
jobid={jobid || null}
|
||||
loading={loading}
|
||||
lineTicketData={lineTicketData}
|
||||
/>
|
||||
);
|
||||
}}
|
||||
</Form.Item>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export function LaborAllocationContainer({ jobid }) {
|
||||
const { loading, data: lineTicketData } = useQuery(GET_LINE_TICKET_BY_PK, {
|
||||
variables: { id: jobid },
|
||||
skip: !jobid,
|
||||
fetchPolicy: "network-only",
|
||||
nextFetchPolicy: "network-only",
|
||||
});
|
||||
export function LaborAllocationContainer({ jobid, loading, lineTicketData }) {
|
||||
if (loading) return <LoadingSkeleton />;
|
||||
if (!lineTicketData) return null;
|
||||
return (
|
||||
|
||||
Reference in New Issue
Block a user