feature/IO-3357-Reynolds-and-Reynolds-DMS-API-Integration -Cleaned up DMS key check (consolidated into a helper function), Clean up DMS post form and make it agnostic, same with customer selector.

This commit is contained in:
Dave
2025-11-13 11:18:11 -05:00
parent 577c3bec04
commit 09ea6dff2b
32 changed files with 1747 additions and 1411 deletions

View File

@@ -1,102 +1,97 @@
import { Alert, Button, Card, Table, Typography } from "antd";
import { useEffect, useState } from "react";
import { SyncOutlined } from "@ant-design/icons";
import { useCallback, useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import { connect } from "react-redux";
import { createStructuredSelector } from "reselect";
import { selectBodyshop } from "../../redux/user/user.selectors";
import Dinero from "dinero.js";
import { SyncOutlined } from "@ant-design/icons";
import { DMS_MAP } from "../../utils/dmsUtils";
import { selectBodyshop } from "../../redux/user/user.selectors";
import { pageLimit } from "../../utils/config";
import { useSplitTreatments } from "@splitsoftware/splitio-react";
import { useSocket } from "../../contexts/SocketIO/useSocket";
import { determineDmsType } from "../../utils/determineDMSType";
const mapStateToProps = createStructuredSelector({
//currentUser: selectCurrentUser
bodyshop: selectBodyshop
});
const mapDispatchToProps = () => ({
//setUserLanguage: language => dispatch(setUserLanguage(language))
});
const mapDispatchToProps = () => ({});
export default connect(mapStateToProps, mapDispatchToProps)(DmsAllocationsSummary);
export function DmsAllocationsSummary({ socket, bodyshop, jobId, title }) {
/**
* DMS Allocations Summary component
* @param mode
* @param socket
* @param bodyshop
* @param jobId
* @param title
* @returns {JSX.Element}
* @constructor
*/
export function DmsAllocationsSummary({ mode, socket, bodyshop, jobId, title }) {
const { t } = useTranslation();
const [allocationsSummary, setAllocationsSummary] = useState([]);
const {
treatments: { Fortellis }
} = useSplitTreatments({
attributes: {},
names: ["Fortellis"],
splitKey: bodyshop.imexshopid
});
const { socket: wsssocket } = useSocket();
const dms = determineDmsType(bodyshop);
// Resolve event name by mode (PBS reuses the CDK event per existing behavior)
const allocationsEvent =
mode === DMS_MAP.reynolds
? "rr-calculate-allocations"
: mode === DMS_MAP.fortellis
? "fortellis-calculate-allocations"
: /* "cdk" | "pbs" (legacy) */ "cdk-calculate-allocations";
const fetchAllocations = () => {
// ✅ RR takes precedence over Fortellis
if (dms === "rr") {
wsssocket.emit("rr-calculate-allocations", jobId, (ack) => {
console.dir({ ack });
setAllocationsSummary(ack);
socket.allocationsSummary = ack;
});
} else if (Fortellis.treatment === "on") {
// Fortellis path (unchanged)
wsssocket.emit("fortellis-calculate-allocations", jobId, (ack) => {
setAllocationsSummary(ack);
socket.allocationsSummary = ack;
});
} else if (socket.connected) {
socket.emit("cdk-calculate-allocations", jobId, (ack) => {
setAllocationsSummary(ack);
socket.allocationsSummary = ack;
const fetchAllocations = useCallback(() => {
if (!socket || !jobId || !mode) return;
try {
socket.emit(allocationsEvent, jobId, (ack) => {
const list = Array.isArray(ack) ? ack : [];
setAllocationsSummary(list);
// Preserve side-channel used by the post form for discrepancy checks
socket.allocationsSummary = list;
});
} catch {
// Best-effort; leave table empty on error
setAllocationsSummary([]);
socket && (socket.allocationsSummary = []);
}
};
}, [socket, jobId, mode, allocationsEvent]);
// Initial + whenever mode/socket/jobId changes
useEffect(() => {
fetchAllocations();
}, [socket, socket.connected, jobId, dms, Fortellis?.treatment]);
}, [fetchAllocations]);
const columns = [
{
title: t("jobs.fields.dms.center"),
dataIndex: "center",
key: "center"
},
{ title: t("jobs.fields.dms.center"), dataIndex: "center", key: "center" },
{
title: t("jobs.fields.dms.sale"),
dataIndex: "sale",
key: "sale",
render: (text, record) => Dinero(record.sale).toFormat()
render: (_text, record) => Dinero(record.sale).toFormat()
},
{
title: t("jobs.fields.dms.cost"),
dataIndex: "cost",
key: "cost",
render: (text, record) => Dinero(record.cost).toFormat()
render: (_text, record) => Dinero(record.cost).toFormat()
},
{
title: t("jobs.fields.dms.sale_dms_acctnumber"),
dataIndex: "sale_dms_acctnumber",
key: "sale_dms_acctnumber",
render: (text, record) => record.profitCenter?.dms_acctnumber
render: (_text, record) => record.profitCenter?.dms_acctnumber
},
{
title: t("jobs.fields.dms.cost_dms_acctnumber"),
dataIndex: "cost_dms_acctnumber",
key: "cost_dms_acctnumber",
render: (text, record) => record.costCenter?.dms_acctnumber
render: (_text, record) => record.costCenter?.dms_acctnumber
},
{
title: t("jobs.fields.dms.dms_wip_acctnumber"),
dataIndex: "dms_wip_acctnumber",
key: "dms_wip_acctnumber",
render: (text, record) => record.costCenter?.dms_wip_acctnumber
render: (_text, record) => record.costCenter?.dms_wip_acctnumber
}
];
@@ -104,7 +99,7 @@ export function DmsAllocationsSummary({ socket, bodyshop, jobId, title }) {
<Card
title={title}
extra={
<Button onClick={fetchAllocations}>
<Button onClick={fetchAllocations} aria-label={t("general.actions.refresh")}>
<SyncOutlined />
</Button>
}
@@ -112,6 +107,7 @@ export function DmsAllocationsSummary({ socket, bodyshop, jobId, title }) {
{bodyshop.pbs_configuration?.disablebillwip && (
<Alert type="warning" message={t("jobs.labels.dms.disablebillwip")} />
)}
<Table
pagination={{ position: "top", defaultPageSize: pageLimit }}
columns={columns}
@@ -120,34 +116,23 @@ export function DmsAllocationsSummary({ socket, bodyshop, jobId, title }) {
locale={{ emptyText: t("dms.labels.refreshallocations") }}
scroll={{ x: true }}
summary={() => {
const totals =
allocationsSummary &&
allocationsSummary.reduce(
(acc, val) => {
return {
totalSale: acc.totalSale.add(Dinero(val.sale)),
totalCost: acc.totalCost.add(Dinero(val.cost))
};
},
{
totalSale: Dinero(),
totalCost: Dinero()
}
);
const totals = allocationsSummary?.reduce(
(acc, val) => ({
totalSale: acc.totalSale.add(Dinero(val.sale)),
totalCost: acc.totalCost.add(Dinero(val.cost))
}),
{ totalSale: Dinero(), totalCost: Dinero() }
) || { totalSale: Dinero(), totalCost: Dinero() };
return (
<Table.Summary.Row>
<Table.Summary.Cell>
<Typography.Title level={4}>{t("general.labels.totals")}</Typography.Title>
</Table.Summary.Cell>
<Table.Summary.Cell>{totals && totals.totalSale.toFormat()}</Table.Summary.Cell>
<Table.Summary.Cell>
{
// totals.totalCost.toFormat()
}
</Table.Summary.Cell>
<Table.Summary.Cell></Table.Summary.Cell>
<Table.Summary.Cell></Table.Summary.Cell>
<Table.Summary.Cell>{totals.totalSale.toFormat()}</Table.Summary.Cell>
<Table.Summary.Cell />
<Table.Summary.Cell />
<Table.Summary.Cell />
</Table.Summary.Row>
);
}}