Compare commits

..

17 Commits

Author SHA1 Message Date
Dave
910d388e05 feature/IO-3357-Reynolds-and-Reynolds-DMS-API-Integration / RRScratch2 / Improve reconnect logic 2025-11-25 18:04:15 -05:00
Dave
9faad53b99 feature/IO-3357-Reynolds-and-Reynolds-DMS-API-Integration / RRScratch2 / Polish 2025-11-25 17:59:19 -05:00
Dave
3b07055d5a feature/IO-3357-Reynolds-and-Reynolds-DMS-API-Integration / RRScratch2 / Polish 2025-11-25 17:36:50 -05:00
Dave
ec29a22984 feature/IO-3357-Reynolds-and-Reynolds-DMS-API-Integration / RRScratch2 / Math Adjustments / DMS State cleaning on Page load 2025-11-25 14:58:53 -05:00
Dave
2b1836d450 feature/IO-3357-Reynolds-and-Reynolds-DMS-API-Integration / RRScratch2 / Checkpoint 2025-11-25 14:13:10 -05:00
Dave
ae7d150a6c feature/IO-3357-Reynolds-and-Reynolds-DMS-API-Integration / RRScratch2 / Checkpoint 2025-11-24 17:21:33 -05:00
Dave
b2184a2d11 feature/IO-3357-Reynolds-and-Reynolds-DMS-API-Integration Checkpoint 2025-11-20 21:57:49 -05:00
Dave
9b1c8fa72b Merge remote-tracking branch 'origin/release/2025-11-21' into feature/IO-3357-Reynolds-and-Reynolds-DMS-API-Integration 2025-11-20 14:54:21 -05:00
Allan Carr
6d6b64ebc3 Merged in feature/IO-3446-Kaizen-Datapump-Extension (pull request #2667)
IO-3446 Kaizen Datapump Extension

Approved-by: Dave Richer
2025-11-20 19:53:47 +00:00
Allan Carr
c954695d3c IO-3446 Kaizen Datapump Extension
Signed-off-by: Allan Carr <allan@imexsystems.ca>
2025-11-19 19:43:55 -08:00
Patrick Fic
338d8e2136 Merged in feature/media-analytics-logging (pull request #2663)
Add unique/dupe columns to media analytics.

Approved-by: Dave Richer
2025-11-19 19:26:00 +00:00
Allan Carr
6674206b4f Merged in feature/IO-3440-Payment-By-Date-Excel (pull request #2665)
IO-3440 Payment By Date - Excel

Approved-by: Dave Richer
2025-11-19 19:16:31 +00:00
Allan Carr
c46ad521d1 Merged in feature/IO-3445-RBAC-BILL-ENTER (pull request #2664)
IO-3445 RBAC Bill:Enter

Approved-by: Dave Richer
2025-11-19 19:13:26 +00:00
Allan Carr
66e5bec4d8 IO-3440 Payment By Date - Excel
Signed-off-by: Allan Carr <allan@imexsystems.ca>
2025-11-18 17:05:42 -08:00
Allan Carr
0d3161ef84 IO-3445 RBAC Bill:Enter
Signed-off-by: Allan Carr <allan@imexsystems.ca>
2025-11-18 16:47:26 -08:00
Patrick Fic
1cd11bdc18 Add unique/dupe columns to media analytics. 2025-11-17 16:42:39 -08:00
Patrick Fic
9cce2696e2 Merge branch 'master-AIO' into feature/media-analytics-logging 2025-11-17 16:31:04 -08:00
32 changed files with 1943 additions and 231 deletions

View File

@@ -425,6 +425,24 @@
}
}
.dms-equal-height-col {
display: flex; // make the Col a flex container
}
/* If the direct child is an AntD Card, make it fill the column */
.dms-equal-height-col > .ant-card {
flex: 1 1 auto;
display: flex;
flex-direction: column;
}
/* Optional: if you want the card body to fill vertically too */
.dms-equal-height-col > .ant-card .ant-card-body {
flex: 1;
display: flex;
flex-direction: column;
}
//.rbc-time-header-gutter {
// padding: 0;
//}

View File

@@ -26,6 +26,7 @@ import { CalculateBillTotal } from "../bill-form/bill-form.totals.utility";
import { handleUpload as handleLocalUpload } from "../documents-local-upload/documents-local-upload.utility";
import { handleUpload } from "../documents-upload/documents-upload.utility";
import { handleUpload as handleUploadToImageProxy } from "../documents-upload-imgproxy/documents-upload-imgproxy.utility";
import RbacWrapper from "../../components/rbac-wrapper/rbac-wrapper.component";
const mapStateToProps = createStructuredSelector({
billEnterModal: selectBillEnterModal,
@@ -450,7 +451,9 @@ function BillEnterModalContainer({ billEnterModal, toggleModalVisible, bodyshop,
setEnterAgain(false);
}}
>
<BillFormContainer form={form} disableInvNumber={billEnterModal.context.disableInvNumber} />
<RbacWrapper action="bills:enter">
<BillFormContainer form={form} disableInvNumber={billEnterModal.context.disableInvNumber} />
</RbacWrapper>
</Form>
</Modal>
);

View File

@@ -124,12 +124,14 @@ export function DmsAllocationsSummary({ mode, socket, bodyshop, jobId, title })
{ totalSale: Dinero(), totalCost: Dinero() }
) || { totalSale: Dinero(), totalCost: Dinero() };
const hasNonZeroSaleTotal = totals.totalSale.getAmount() !== 0;
return (
<Table.Summary.Row>
<Table.Summary.Cell>
<Typography.Title level={4}>{t("general.labels.totals")}</Typography.Title>
</Table.Summary.Cell>
<Table.Summary.Cell>{totals.totalSale.toFormat()}</Table.Summary.Cell>
<Table.Summary.Cell>{hasNonZeroSaleTotal ? totals.totalSale.toFormat() : null}</Table.Summary.Cell>
<Table.Summary.Cell />
<Table.Summary.Cell />
<Table.Summary.Cell />

View File

@@ -0,0 +1,300 @@
import { Alert, Button, Card, Table, Tabs, Typography } from "antd";
import { SyncOutlined } from "@ant-design/icons";
import { useCallback, useEffect, useMemo, useState } from "react";
import { useTranslation } from "react-i18next";
import { connect } from "react-redux";
import { createStructuredSelector } from "reselect";
import { selectBodyshop } from "../../redux/user/user.selectors";
const mapStateToProps = createStructuredSelector({
bodyshop: selectBodyshop
});
const mapDispatchToProps = () => ({});
export default connect(mapStateToProps, mapDispatchToProps)(RrAllocationsSummary);
/**
* Normalize job allocations into a flat list for display / preview building.
* @param ack
* @returns {{center: *, sale, partsSale, laborTaxableSale, laborNonTaxableSale, extrasSale, cost, profitCenter, costCenter}[]|*[]}
*/
function normalizeJobAllocations(ack) {
if (!ack || !Array.isArray(ack.jobAllocations)) return [];
return ack.jobAllocations.map((row) => ({
center: row.center,
// legacy "sale" (total) if we ever want to show it again
sale: row.sale || row.totalSale || null,
// bucketed sales used to build split ROGOG/ROLABOR
partsSale: row.partsSale || null,
laborTaxableSale: row.laborTaxableSale || null,
laborNonTaxableSale: row.laborNonTaxableSale || null,
extrasSale: row.extrasSale || null,
cost: row.cost || null,
profitCenter: row.profitCenter || null,
costCenter: row.costCenter || null
}));
}
/**
* RR-specific DMS Allocations Summary
* Focused on what we actually send to RR:
* - ROGOG (split by taxable / non-taxable segments)
* - ROLABOR shell
*
* The heavy lifting (ROGOG/ROLABOR split, cost allocation, tax flags)
* is now done on the backend via buildRogogFromAllocations/buildRolaborFromRogog.
* This component just renders the preview from `ack.rogg` / `ack.rolabor`.
*/
export function RrAllocationsSummary({ socket, bodyshop, jobId, title }) {
const { t } = useTranslation();
const [roggPreview, setRoggPreview] = useState(null);
const [rolaborPreview, setRolaborPreview] = useState(null);
const [error, setError] = useState(null);
const fetchAllocations = useCallback(() => {
if (!socket || !jobId) return;
try {
socket.emit("rr-calculate-allocations", jobId, (ack) => {
if (ack && ack.ok === false) {
setRoggPreview(null);
setRolaborPreview(null);
setError(ack.error || t("dms.labels.allocations_error"));
if (socket) {
socket.allocationsSummary = [];
socket.rrAllocationsRaw = ack;
}
return;
}
const jobAllocRows = normalizeJobAllocations(ack);
setRoggPreview(ack?.rogg || null);
setRolaborPreview(ack?.rolabor || null);
setError(null);
if (socket) {
socket.allocationsSummary = jobAllocRows;
socket.rrAllocationsRaw = ack;
}
});
} catch {
setRoggPreview(null);
setRolaborPreview(null);
setError(t("dms.labels.allocations_error"));
if (socket) {
socket.allocationsSummary = [];
}
}
}, [socket, jobId, t]);
useEffect(() => {
fetchAllocations();
}, [fetchAllocations]);
const opCode = bodyshop?.rr_configuration?.baseOpCode || "28TOZ";
const segmentLabelMap = {
partsExtras: "Parts/Extras",
laborTaxable: "Taxable Labor",
laborNonTaxable: "Non-Taxable Labor"
};
const roggRows = useMemo(() => {
if (!roggPreview || !Array.isArray(roggPreview.ops)) return [];
const rows = [];
roggPreview.ops.forEach((op) => {
(op.lines || []).forEach((line, idx) => {
const baseDesc = line.itemDesc;
const segmentKind = op.segmentKind;
const segmentCount = op.segmentCount || 0;
const segmentLabel = segmentLabelMap[segmentKind] || segmentKind;
const displayDesc = segmentCount > 1 && segmentLabel ? `${baseDesc} (${segmentLabel})` : baseDesc;
rows.push({
key: `${op.jobNo}-${idx}`,
opCode: op.opCode,
jobNo: op.jobNo,
breakOut: line.breakOut,
itemType: line.itemType,
itemDesc: displayDesc,
custQty: line.custQty,
custPayTypeFlag: line.custPayTypeFlag,
custTxblNtxblFlag: line.custTxblNtxblFlag,
custPrice: line.amount?.custPrice,
dlrCost: line.amount?.dlrCost,
// segment metadata for visual styling
segmentKind,
segmentCount
});
});
});
return rows;
}, [roggPreview]);
const rolaborRows = useMemo(() => {
if (!rolaborPreview || !Array.isArray(rolaborPreview.ops)) return [];
return rolaborPreview.ops.map((op, idx) => ({
key: `${op.jobNo}-${idx}`,
opCode: op.opCode,
jobNo: op.jobNo,
custPayTypeFlag: op.custPayTypeFlag,
custTxblNtxblFlag: op.custTxblNtxblFlag,
payType: op.bill?.payType,
amtType: op.amount?.amtType,
custPrice: op.amount?.custPrice,
totalAmt: op.amount?.totalAmt
}));
}, [rolaborPreview]);
// Totals for ROGOG (sum custPrice + dlrCost over all lines)
const roggTotals = useMemo(() => {
if (!roggPreview || !Array.isArray(roggPreview.ops)) {
return { totalCustPrice: "0.00", totalDlrCost: "0.00" };
}
let totalCustCents = 0;
let totalCostCents = 0;
roggPreview.ops.forEach((op) => {
(op.lines || []).forEach((line) => {
const cp = parseFloat(line.amount?.custPrice || "0");
if (!Number.isNaN(cp)) {
totalCustCents += Math.round(cp * 100);
}
const dc = parseFloat(line.amount?.dlrCost || "0");
if (!Number.isNaN(dc)) {
totalCostCents += Math.round(dc * 100);
}
});
});
return {
totalCustPrice: (totalCustCents / 100).toFixed(2),
totalDlrCost: (totalCostCents / 100).toFixed(2)
};
}, [roggPreview]);
const roggColumns = [
{ title: "JobNo", dataIndex: "jobNo", key: "jobNo" },
{ title: "OpCode", dataIndex: "opCode", key: "opCode" },
{ title: "BreakOut", dataIndex: "breakOut", key: "breakOut" },
{ title: "ItemType", dataIndex: "itemType", key: "itemType" },
{ title: "ItemDesc", dataIndex: "itemDesc", key: "itemDesc" },
{ title: "CustQty", dataIndex: "custQty", key: "custQty" },
{ title: "CustTxblFlag", dataIndex: "custTxblNtxblFlag", key: "custTxblNtxblFlag" },
{ title: "CustPrice", dataIndex: "custPrice", key: "custPrice" },
{ title: "DlrCost", dataIndex: "dlrCost", key: "dlrCost" }
];
const rolaborColumns = [
{ title: "JobNo", dataIndex: "jobNo", key: "jobNo" },
{ title: "OpCode", dataIndex: "opCode", key: "opCode" },
{ title: "CustPayType", dataIndex: "custPayTypeFlag", key: "custPayTypeFlag" },
{ title: "CustTxblFlag", dataIndex: "custTxblNtxblFlag", key: "custTxblNtxblFlag" },
{ title: "PayType", dataIndex: "payType", key: "payType" },
{ title: "AmtType", dataIndex: "amtType", key: "amtType" },
{ title: "CustPrice", dataIndex: "custPrice", key: "custPrice" },
{ title: "TotalAmt", dataIndex: "totalAmt", key: "totalAmt" }
];
const tabItems = [
{
key: "rogog",
label: "ROGOG Preview",
children: (
<>
<Typography.Paragraph type="secondary" style={{ marginBottom: 8 }}>
OpCode: <strong>{opCode}</strong>. Only centers with RR GOG mapping (rr_gogcode &amp; rr_item_type) are
included. Totals below reflect exactly what will be sent in ROGOG.
</Typography.Paragraph>
<Table
pagination={false}
columns={roggColumns}
rowKey="key"
dataSource={roggRows}
locale={{ emptyText: "No ROGOG lines would be generated." }}
scroll={{ x: true }}
// 👇 visually highlight splits; especially taxable/non-taxable labor segments
rowClassName={(record) => {
if (
record.segmentCount > 1 &&
(record.segmentKind === "laborTaxable" || record.segmentKind === "laborNonTaxable")
) {
return "rr-allocations-tax-split-row";
}
if (record.segmentCount > 1) {
return "rr-allocations-split-row";
}
return "";
}}
summary={() => {
const hasCustTotal = Number(roggTotals.totalCustPrice) !== 0;
const hasCostTotal = Number(roggTotals.totalDlrCost) !== 0;
return (
<Table.Summary.Row>
<Table.Summary.Cell index={0}>
<Typography.Title level={5}>{t("general.labels.totals")}</Typography.Title>
</Table.Summary.Cell>
<Table.Summary.Cell index={1} />
<Table.Summary.Cell index={2} />
<Table.Summary.Cell index={3} />
<Table.Summary.Cell index={4} />
<Table.Summary.Cell index={5} />
<Table.Summary.Cell index={6} />
<Table.Summary.Cell index={7}>{hasCustTotal ? roggTotals.totalCustPrice : null}</Table.Summary.Cell>
<Table.Summary.Cell index={8}>{hasCostTotal ? roggTotals.totalDlrCost : null}</Table.Summary.Cell>
</Table.Summary.Row>
);
}}
/>
</>
)
},
{
key: "rolabor",
label: "ROLABOR Preview",
children: (
<>
<Typography.Paragraph type="secondary" style={{ marginBottom: 8 }}>
This mirrors the shell that would be sent for ROLABOR when all financials are carried in GOG.
</Typography.Paragraph>
<Table
pagination={false}
columns={rolaborColumns}
rowKey="key"
dataSource={rolaborRows}
locale={{ emptyText: "No ROLABOR lines would be generated." }}
scroll={{ x: true }}
/>
</>
)
}
];
return (
<Card
title={title}
extra={
<Button onClick={fetchAllocations} aria-label={t("general.actions.refresh")}>
<SyncOutlined />
</Button>
}
>
{bodyshop.pbs_configuration?.disablebillwip && (
<Alert type="warning" message={t("jobs.labels.dms.disablebillwip")} />
)}
{error && <Alert type="error" style={{ marginTop: 8, marginBottom: 8 }} message={error} />}
<Tabs defaultActiveKey="rogog" items={tabItems} />
</Card>
);
}

View File

@@ -13,7 +13,14 @@ const mapDispatchToProps = () => ({});
export default connect(mapStateToProps, mapDispatchToProps)(DmsLogEvents);
export function DmsLogEvents({ logs, detailsOpen, detailsNonce, isDarkMode, colorizeJson = false }) {
export function DmsLogEvents({
logs,
detailsOpen,
detailsNonce,
isDarkMode,
colorizeJson = false,
showDetails = true
}) {
const [openSet, setOpenSet] = useState(() => new Set());
// Inject JSON highlight styles once (only when colorize is enabled)
@@ -54,8 +61,10 @@ export function DmsLogEvents({ logs, detailsOpen, detailsNonce, isDarkMode, colo
() =>
(logs || []).map((raw, idx) => {
const { level, message, timestamp, meta } = normalizeLog(raw);
const hasMeta = !isEmpty(meta);
const isOpen = openSet.has(idx);
// Only treat meta as "present" when we are allowed to show details
const hasMeta = !isEmpty(meta) && showDetails;
const isOpen = hasMeta && openSet.has(idx);
return {
key: idx,
@@ -101,7 +110,7 @@ export function DmsLogEvents({ logs, detailsOpen, detailsNonce, isDarkMode, colo
)
};
}),
[logs, openSet, colorizeJson]
[logs, openSet, colorizeJson, isDarkMode, showDetails]
);
return <Timeline pending reverse items={items} />;

View File

@@ -26,6 +26,7 @@ import DmsPostForm from "../../components/dms-post-form/dms-post-form.component"
import DmsLogEvents from "../../components/dms-log-events/dms-log-events.component";
import DmsCustomerSelector from "../../components/dms-customer-selector/dms-customer-selector.component";
import DmsAllocationsSummary from "../../components/dms-allocations-summary/dms-allocations-summary.component";
import RrAllocationsSummary from "../../components/dms-allocations-summary/rr-dms-allocations-summary.component";
const mapStateToProps = createStructuredSelector({
bodyshop: selectBodyshop
@@ -66,6 +67,8 @@ const DMS_SOCKET_EVENTS = {
export function DmsContainer({ bodyshop, setBreadcrumbs, setSelectedHeader, insertAuditTrail }) {
const { t } = useTranslation();
const [resetAfterReconnect, setResetAfterReconnect] = useState(false);
const history = useNavigate();
const search = queryString.parse(useLocation().search);
const { jobId } = search;
@@ -82,6 +85,7 @@ export function DmsContainer({ bodyshop, setBreadcrumbs, setSelectedHeader, inse
// Compute a single normalized mode and pick the proper socket
const mode = getDmsMode(bodyshop, Fortellis.treatment); // "rr" | "fortellis" | "cdk" | "pbs" | "none"
const isRrMode = mode === DMS_MAP.reynolds;
const { socket: wsssocket } = useSocket();
const activeSocket = useMemo(() => (isWssMode(mode) ? wsssocket : legacySocket), [mode, wsssocket]);
@@ -134,12 +138,47 @@ export function DmsContainer({ bodyshop, setBreadcrumbs, setSelectedHeader, inse
[mode]
);
const transportLabel = isWssMode(mode) ? "App Socket (WSS)" : "Legacy Socket (WS)";
const transportLabel = isWssMode(mode) ? "(WSS)" : "(WS)";
const bannerMessage = `Posting to ${providerLabel} | ${transportLabel} | ${
isConnected ? "Connected" : "Disconnected"
}`;
const resetKey = useMemo(() => `${mode || "none"}-${jobId || "none"}`, [mode, jobId]);
// 🔄 Hard reset of local + server-side DMS context when the page/job loads
useEffect(() => {
// Clear any local ephemeral state that might be stale
setLogs([]);
setRrOpenRoLimit(false);
setrrValidationPending(false);
if (!activeSocket) return;
const emitReset = () => {
// Generic reset; server can branch on `mode` if needed
activeSocket.emit("dms-reset-context", { jobId, mode });
};
if (activeSocket.connected) {
// WSS usually lands here
emitReset();
return;
}
// Legacy WS: wait for the connect before emitting reset
const handleConnectOnce = () => {
emitReset();
activeSocket.off("connect", handleConnectOnce);
};
activeSocket.on("connect", handleConnectOnce);
return () => {
activeSocket.off("connect", handleConnectOnce);
};
}, [jobId, mode, activeSocket]);
const handleExportFailed = (payload = {}) => {
const { title, friendlyMessage, error: errText, severity, errorCode, vendorStatusCode } = payload;
@@ -148,10 +187,10 @@ export function DmsContainer({ bodyshop, setBreadcrumbs, setSelectedHeader, inse
errText ||
t("dms.errors.exportfailedgeneric", "We couldn't complete the export. Please try again.");
const vendorTitle = title || (mode === DMS_MAP.reynolds ? "Reynolds" : "DMS");
const vendorTitle = title || (isRrMode ? "Reynolds" : "DMS");
const isRrOpenRoLimit =
mode === DMS_MAP.reynolds &&
isRrMode &&
(vendorStatusCode === 507 ||
/MAX_OPEN_ROS/i.test(String(errorCode || "")) ||
/maximum number of open repair orders/i.test(String(msg || "").toLowerCase()));
@@ -187,8 +226,8 @@ export function DmsContainer({ bodyshop, setBreadcrumbs, setSelectedHeader, inse
});
setSelectedHeader("dms");
setBreadcrumbs([
{ link: "/manage/accounting/receivables", label: t("titles.bc.accounting-receivables") },
{ link: "/manage/dms", label: t("titles.bc.dms") }
{ link: "/manage/accounting/receivables", label: t("titles.bc.accounting-receivables") }
// { link: "/manage/dms", label: t("titles.bc.dms") }
]);
}, [t, setBreadcrumbs, setSelectedHeader]);
@@ -207,6 +246,11 @@ export function DmsContainer({ bodyshop, setBreadcrumbs, setSelectedHeader, inse
const onConnect = () => {
setIsConnected(true);
setActiveLogLevel(logLevel);
if (resetAfterReconnect) {
activeSocket.emit("dms-reset-context", { jobId, mode });
setResetAfterReconnect(false);
}
};
const onDisconnect = () => setIsConnected(false);
@@ -218,7 +262,7 @@ export function DmsContainer({ bodyshop, setBreadcrumbs, setSelectedHeader, inse
{
timestamp: new Date(),
level: "warn",
message: `Reconnected to ${mode === DMS_MAP.reynolds ? "RR" : mode === DMS_MAP.fortellis ? "Fortellis" : "DMS"} Export Service`
message: `Reconnected to ${isRrMode ? "RR" : mode === DMS_MAP.fortellis ? "Fortellis" : "DMS"} Export Service`
}
]);
};
@@ -235,22 +279,17 @@ export function DmsContainer({ bodyshop, setBreadcrumbs, setSelectedHeader, inse
activeSocket.on("connect_error", onConnectError);
// Logs
const onLog =
mode === DMS_MAP.reynolds
? (payload = {}) => {
const normalized = {
timestamp: payload.timestamp
? new Date(payload.timestamp)
: payload.ts
? new Date(payload.ts)
: new Date(),
level: (payload.level || "INFO").toUpperCase(),
message: payload.message || payload.msg || "",
meta: payload.meta ?? payload.ctx ?? payload.details ?? null
};
setLogs((prev) => [...prev, normalized]);
}
: (payload) => setLogs((prev) => [...prev, payload]);
const onLog = isRrMode
? (payload = {}) => {
const normalized = {
timestamp: payload.timestamp ? new Date(payload.timestamp) : payload.ts ? new Date(payload.ts) : new Date(),
level: (payload.level || "INFO").toUpperCase(),
message: payload.message || payload.msg || "",
meta: payload.meta ?? payload.ctx ?? payload.details ?? null
};
setLogs((prev) => [...prev, normalized]);
}
: (payload) => setLogs((prev) => [...prev, payload]);
if (channels.log) activeSocket.on(channels.log, onLog);
@@ -308,9 +347,8 @@ export function DmsContainer({ bodyshop, setBreadcrumbs, setSelectedHeader, inse
]);
};
if (mode === DMS_MAP.reynolds && channels.partialResult) activeSocket.on(channels.partialResult, onPartialResult);
if (mode === DMS_MAP.reynolds && channels.validationNeeded)
activeSocket.on(channels.validationNeeded, onValidationRequired);
if (isRrMode && channels.partialResult) activeSocket.on(channels.partialResult, onPartialResult);
if (isRrMode && channels.validationNeeded) activeSocket.on(channels.validationNeeded, onValidationRequired);
return () => {
activeSocket.off("connect", onConnect);
@@ -322,10 +360,8 @@ export function DmsContainer({ bodyshop, setBreadcrumbs, setSelectedHeader, inse
if (channels.exportSuccess) activeSocket.off(channels.exportSuccess, onExportSuccess);
if (channels.exportFailed) activeSocket.off(channels.exportFailed, handleExportFailed);
if (mode === DMS_MAP.reynolds && channels.partialResult)
activeSocket.off(channels.partialResult, onPartialResult);
if (mode === DMS_MAP.reynolds && channels.validationNeeded)
activeSocket.off(channels.validationNeeded, onValidationRequired);
if (isRrMode && channels.partialResult) activeSocket.off(channels.partialResult, onPartialResult);
if (isRrMode && channels.validationNeeded) activeSocket.off(channels.validationNeeded, onValidationRequired);
// Only tear down legacy socket listeners; don't disconnect WSS from here
if (!isWssMode(mode)) {
@@ -358,24 +394,45 @@ export function DmsContainer({ bodyshop, setBreadcrumbs, setSelectedHeader, inse
<AlertComponent style={{ marginBottom: 10 }} message={bannerMessage} type="warning" showIcon closable />
<Row gutter={[16, 16]}>
<Col md={24} lg={10}>
<DmsAllocationsSummary
title={
<span>
<Link
to={`/manage/jobs/${data && data.jobs_by_pk.id}`}
>{`${data?.jobs_by_pk && data.jobs_by_pk.ro_number}`}</Link>
{` | ${OwnerNameDisplayFunction(data.jobs_by_pk)} | ${data.jobs_by_pk.v_model_yr || ""} ${data.jobs_by_pk.v_make_desc || ""} ${data.jobs_by_pk.v_model_desc || ""}`}
</span>
}
socket={activeSocket}
jobId={jobId}
mode={mode}
/>
<Col md={24} lg={10} className="dms-equal-height-col">
{!isRrMode ? (
<DmsAllocationsSummary
key={resetKey}
title={
<span>
<Link
to={`/manage/jobs/${data && data.jobs_by_pk.id}`}
>{`${data?.jobs_by_pk && data.jobs_by_pk.ro_number}`}</Link>
{` | ${OwnerNameDisplayFunction(data.jobs_by_pk)} | ${data.jobs_by_pk.v_model_yr || ""} ${
data.jobs_by_pk.v_make_desc || ""
} ${data.jobs_by_pk.v_model_desc || ""}`}
</span>
}
socket={activeSocket}
jobId={jobId}
mode={mode}
/>
) : (
<RrAllocationsSummary
key={resetKey}
title={
<span>
<Link to={`/manage/jobs/${data && data.jobs_by_pk.id}`}>
{data?.jobs_by_pk && data.jobs_by_pk.ro_number}
</Link>
{` | ${OwnerNameDisplayFunction(data.jobs_by_pk)} | ${
data.jobs_by_pk.v_model_yr || ""
} ${data.jobs_by_pk.v_make_desc || ""} ${data.jobs_by_pk.v_model_desc || ""}`}
</span>
}
socket={activeSocket}
jobId={jobId}
/>
)}
</Col>
<Col md={24} lg={14}>
<DmsPostForm socket={activeSocket} job={data?.jobs_by_pk} logsRef={logsRef} mode={mode} />
<Col md={24} lg={14} className="dms-equal-height-col">
<DmsPostForm key={resetKey} socket={activeSocket} job={data?.jobs_by_pk} logsRef={logsRef} mode={mode} />
</Col>
<DmsCustomerSelector
@@ -397,13 +454,18 @@ export function DmsContainer({ bodyshop, setBreadcrumbs, setSelectedHeader, inse
title={t("jobs.labels.dms.logs")}
extra={
<Space wrap>
<Switch
checked={colorizeJson}
onChange={setColorizeJson}
checkedChildren="Color JSON"
unCheckedChildren="Plain JSON"
/>
<Button onClick={toggleDetailsAll}>{detailsOpen ? "Collapse All" : "Expand All"}</Button>
{isRrMode && (
<>
<Switch
checked={colorizeJson}
onChange={setColorizeJson}
checkedChildren="Color JSON"
unCheckedChildren="Plain JSON"
/>
<Button onClick={toggleDetailsAll}>{detailsOpen ? "Collapse All" : "Expand All"}</Button>
</>
)}
<Select
placeholder="Log Level"
value={logLevel}
@@ -422,11 +484,14 @@ export function DmsContainer({ bodyshop, setBreadcrumbs, setSelectedHeader, inse
<Button
onClick={() => {
setLogs([]);
setResetAfterReconnect(true);
if (isWssMode(mode)) {
setActiveLogLevel(logLevel);
}
activeSocket.disconnect();
activeSocket.connect();
if (activeSocket) {
activeSocket.disconnect();
setTimeout(() => activeSocket.connect(), 100);
}
}}
>
Reconnect
@@ -436,9 +501,12 @@ export function DmsContainer({ bodyshop, setBreadcrumbs, setSelectedHeader, inse
>
<DmsLogEvents
logs={logs}
detailsOpen={detailsOpen}
// Only honour details/colorized JSON in RR mode;
// in other modes DmsLogEvents can render a simple, flat list.
detailsOpen={isRrMode ? detailsOpen : false}
detailsNonce={detailsNonce}
colorizeJson={colorizeJson}
colorizeJson={isRrMode ? colorizeJson : false}
showDetails={isRrMode}
/>
</Card>
</div>

View File

@@ -3223,6 +3223,7 @@
"parts_not_recieved_vendor": "Parts Not Received by Vendor",
"parts_received_not_scheduled": "Parts Received for Jobs Not Scheduled",
"payments_by_date": "Payments by Date",
"payments_by_date_excel": "Payments by Date - Excel",
"payments_by_date_payment": "Payments by Date and Payment Type",
"payments_by_date_type": "Payments by Date and Customer Type",
"production_by_category": "Production by Category",

View File

@@ -3220,6 +3220,7 @@
"parts_not_recieved_vendor": "",
"parts_received_not_scheduled": "",
"payments_by_date": "",
"payments_by_date_excel": "",
"payments_by_date_payment": "",
"payments_by_date_type": "",
"production_by_category": "",

View File

@@ -3220,6 +3220,7 @@
"parts_not_recieved_vendor": "",
"parts_received_not_scheduled": "",
"payments_by_date": "",
"payments_by_date_excel": "",
"payments_by_date_payment": "",
"payments_by_date_type": "",
"production_by_category": "",

View File

@@ -1218,6 +1218,18 @@ export const TemplateList = (type, context) => {
},
group: "customers"
},
payments_by_date_excel: {
title: i18n.t("reportcenter.templates.payments_by_date_excel"),
subject: i18n.t("reportcenter.templates.payments_by_date_excel"),
key: "payments_by_date",
reporttype: "excel",
disabled: false,
rangeFilter: {
object: i18n.t("reportcenter.labels.objects.payments"),
field: i18n.t("payments.fields.date")
},
group: "customers"
},
schedule: {
title: i18n.t("reportcenter.templates.schedule"),
subject: i18n.t("reportcenter.templates.schedule"),

View File

@@ -0,0 +1,4 @@
-- Could not auto-generate a down migration.
-- Please write an appropriate down migration for the SQL below:
-- alter table "public"."media_analytics" add column "unique_documents" numeric
-- null;

View File

@@ -0,0 +1,2 @@
alter table "public"."media_analytics" add column "unique_documents" numeric
null;

View File

@@ -0,0 +1,4 @@
-- Could not auto-generate a down migration.
-- Please write an appropriate down migration for the SQL below:
-- alter table "public"."media_analytics" add column "duplicate_documents" numeric
-- null;

View File

@@ -0,0 +1,2 @@
alter table "public"."media_analytics" add column "duplicate_documents" numeric
null;

View File

@@ -0,0 +1,4 @@
-- Could not auto-generate a down migration.
-- Please write an appropriate down migration for the SQL below:
-- alter table "public"."media_analytics_detail" add column "unique_documents" numeric
-- null;

View File

@@ -0,0 +1,2 @@
alter table "public"."media_analytics_detail" add column "unique_documents" numeric
null;

View File

@@ -0,0 +1,4 @@
-- Could not auto-generate a down migration.
-- Please write an appropriate down migration for the SQL below:
-- alter table "public"."media_analytics_detail" add column "duplicate_documents" numeric
-- null;

View File

@@ -0,0 +1,2 @@
alter table "public"."media_analytics_detail" add column "duplicate_documents" numeric
null;

View File

@@ -0,0 +1 @@
alter table "public"."media_analytics_detail" rename column "unique_document_count" to "unique_documents";

View File

@@ -0,0 +1 @@
alter table "public"."media_analytics_detail" rename column "unique_documents" to "unique_document_count";

View File

@@ -0,0 +1 @@
alter table "public"."media_analytics_detail" rename column "duplicate_count" to "duplicate_documents";

View File

@@ -0,0 +1 @@
alter table "public"."media_analytics_detail" rename column "duplicate_documents" to "duplicate_count";

View File

@@ -219,8 +219,6 @@ const CreateRepairOrderTag = (job, errorCallback) => {
}
const repairCosts = CreateCosts(job);
const jobline = CreateJobLines(job.joblines);
const timeticket = CreateTimeTickets(job.timetickets);
try {
const ret = {
@@ -290,8 +288,100 @@ const CreateRepairOrderTag = (job, errorCallback) => {
(job.date_exported && moment(job.date_exported).tz(job.bodyshop.timezone).format(DateFormat)) || "",
DateVoid: (job.date_void && moment(job.date_void).tz(job.bodyshop.timezone).format(DateFormat)) || ""
},
JobLineDetails: { jobline },
TimeTicketDetails: { timeticket },
JobLineDetails: (function () {
const joblineSource = Array.isArray(job.joblines) ? job.joblines : job.joblines ? [job.joblines] : [];
if (joblineSource.length === 0) return { jobline: [] };
return {
jobline: joblineSource.map((jl = {}) => ({
line_description: jl.line_desc || jl.line_description || "",
oem_part_no: jl.oem_partno || jl.oem_part_no || "",
alt_part_no: jl.alt_partno || jl.alt_part_no || "",
op_code_desc: jl.op_code_desc || "",
part_type: jl.part_type || "",
part_qty: jl.part_qty ?? jl.quantity ?? 0,
part_price: jl.act_price ?? jl.part_price ?? 0,
labor_type: jl.mod_lbr_ty || jl.labor_type || "",
labor_hours: jl.mod_lb_hrs ?? jl.labor_hours ?? 0,
labor_sale: jl.lbr_amt ?? jl.labor_sale ?? 0
}))
};
})(),
BillsDetails: (function () {
const billsSource = Array.isArray(job.bills) ? job.bills : job.bills ? [job.bills] : [];
if (billsSource.length === 0) return { BillDetails: [] };
return {
BillDetails: billsSource.map(
({
billlines = [],
date = "",
is_credit_memo = false,
invoice_number = "",
isinhouse = false,
vendor = {}
} = {}) => ({
BillLines: {
BillLine: billlines.map((bl = {}) => ({
line_description: bl.line_desc || bl.line_description || "",
part_price: bl.actual_price ?? bl.part_price ?? bl.act_price ?? 0,
actual_cost: bl.actual_cost ?? 0,
cost_center: bl.cost_center || "",
deductedfromlbr: bl.deductedfromlbr || false,
part_qty: bl.quantity ?? bl.part_qty ?? 0,
oem_part_no: bl.oem_partno || bl.oem_part_no || "",
alt_part_no: bl.alt_partno || bl.alt_part_no || ""
}))
},
date,
is_credit_memo,
invoice_number,
isinhouse,
vendorName: vendor.name || ""
})
)
};
})(),
JobNotes: (function () {
const notesSource = Array.isArray(job.notes) ? job.notes : job.notes ? [job.notes] : [];
if (notesSource.length === 0) return { JobNote: [] };
return {
JobNote: notesSource.map((note = {}) => ({
created_at: note.created_at || "",
created_by: note.created_by || "",
critical: note.critical || false,
private: note.private || false,
text: note.text || "",
type: note.type || ""
}))
};
})(),
TimeTicketDetails: (function () {
const ticketSource = Array.isArray(job.timetickets)
? job.timetickets
: job.timetickets
? [job.timetickets]
: [];
if (ticketSource.length === 0) return { timeticket: [] };
return {
timeticket: ticketSource.map((ticket = {}) => ({
date: ticket.date || "",
employee:
ticket.employee && ticket.employee.employee_number
? ticket.employee.employee_number
.trim()
.concat(" - ", ticket.employee.first_name.trim(), " ", ticket.employee.last_name.trim())
.trim()
: "",
productive_hrs: ticket.productivehrs ?? 0,
actual_hrs: ticket.actualhrs ?? 0,
cost_center: ticket.cost_center || "",
flat_rate: ticket.flat_rate || false,
rate: ticket.rate ?? 0,
ticket_cost: ticket.flat_rate
? ticket.rate * (ticket.productivehrs || 0)
: ticket.rate * (ticket.actualhrs || 0)
}))
};
})(),
Sales: {
Labour: {
Aluminum: Dinero(job.job_totals.rates.laa.total).toFormat(DineroFormat),
@@ -636,42 +726,3 @@ const CreateCosts = (job) => {
}, 0)
};
};
const CreateJobLines = (joblines) => {
const repairLines = [];
joblines.forEach((jobline) => {
repairLines.push({
line_description: jobline.line_desc,
oem_part_no: jobline.oem_partno,
alt_part_no: jobline.alt_partno,
op_code_desc: jobline.op_code_desc,
part_type: jobline.part_type,
part_qty: jobline.part_qty,
part_price: jobline.act_price,
labor_type: jobline.mod_lbr_ty,
labor_hours: jobline.mod_lb_hrs,
labor_sale: jobline.lbr_amt
});
});
return repairLines;
};
const CreateTimeTickets = (timetickets) => {
const timeTickets = [];
timetickets.forEach((ticket) => {
timeTickets.push({
date: ticket.date,
employee: ticket.employee.employee_number
.trim()
.concat(" - ", ticket.employee.first_name.trim(), " ", ticket.employee.last_name.trim())
.trim(),
productive_hrs: ticket.productivehrs,
actual_hrs: ticket.actualhrs,
cost_center: ticket.cost_center,
flat_rate: ticket.flat_rate,
rate: ticket.rate,
ticket_cost: ticket.flat_rate ? ticket.rate * ticket.productive_hrs : ticket.rate * ticket.actual_hrs
});
});
return timeTickets;
};

View File

@@ -1221,7 +1221,7 @@ query ENTEGRAL_EXPORT($bodyshopid: uuid!) {
}`;
exports.KAIZEN_QUERY = `query KAIZEN_EXPORT($start: timestamptz, $bodyshopid: uuid!, $end: timestamptz) {
bodyshops_by_pk(id: $bodyshopid){
bodyshops_by_pk(id: $bodyshopid) {
id
shopname
address1
@@ -1249,15 +1249,24 @@ exports.KAIZEN_QUERY = `query KAIZEN_EXPORT($start: timestamptz, $bodyshopid: uu
bills {
billlines {
actual_cost
actual_price
cost_center
deductedfromlbr
id
line_desc
quantity
}
date
federal_tax_rate
id
is_credit_memo
invoice_number
isinhouse
local_tax_rate
state_tax_rate
vendor {
name
}
}
created_at
clm_no
@@ -1299,7 +1308,7 @@ exports.KAIZEN_QUERY = `query KAIZEN_EXPORT($start: timestamptz, $bodyshopid: uu
joblines(where: {removed: {_eq: false}}) {
act_price
alt_partno
billlines(order_by: {bill: {date: desc_nulls_last}} limit: 1) {
billlines(order_by: {bill: {date: desc_nulls_last}}, limit: 1) {
actual_cost
actual_price
quantity
@@ -1322,8 +1331,8 @@ exports.KAIZEN_QUERY = `query KAIZEN_EXPORT($start: timestamptz, $bodyshopid: uu
mod_lbr_ty
oem_partno
op_code_desc
parts_order_lines(order_by: {parts_order: {order_date: desc_nulls_last}} limit: 1){
parts_order{
parts_order_lines(order_by: {parts_order: {order_date: desc_nulls_last}}, limit: 1) {
parts_order {
id
order_date
}
@@ -1342,6 +1351,14 @@ exports.KAIZEN_QUERY = `query KAIZEN_EXPORT($start: timestamptz, $bodyshopid: uu
jobid
totalliquidcost
}
notes {
created_at
created_by
critical
private
text
type
}
ownr_addr1
ownr_addr2
ownr_city
@@ -3149,11 +3166,10 @@ exports.DELETE_PHONE_NUMBER_OPT_OUT = `
}
`;
exports.INSERT_MEDIA_ANALYTICS = `
mutation INSERT_MEDIA_ANALYTICS($mediaObject: media_analytics_insert_input!) {
insert_media_analytics_one(object: $mediaObject) {
id
}
}
`
`;

View File

@@ -0,0 +1,923 @@
/**
* THIS IS A COPY of CDKCalculateAllocations, modified to:
* - Only calculate allocations needed for Reynolds & RR exports
* - Keep sales broken down into buckets (parts, taxable labor, non-taxable labor, extras)
* - Add extra logging for easier debugging
*
* Original comments follow.
*/
const { GraphQLClient } = require("graphql-request");
const Dinero = require("dinero.js");
const _ = require("lodash");
const queries = require("../graphql-client/queries");
const CreateRRLogEvent = require("./rr-logger-event");
const InstanceManager = require("../utils/instanceMgr").default;
const { DiscountNotAlreadyCounted } = InstanceManager({
imex: require("../job/job-totals"),
rome: require("../job/job-totals-USA")
});
/**
* ============================
* Helpers / Summarizers
* ============================
*/
const summarizeMoney = (dinero) => {
if (!dinero || typeof dinero.getAmount !== "function") return { cents: null };
return { cents: dinero.getAmount() };
};
const summarizeTaxAllocations = (tax) =>
Object.entries(tax || {}).map(([key, entry]) => ({
key,
sale: summarizeMoney(entry?.sale),
cost: summarizeMoney(entry?.cost)
}));
const summarizeAllocationsArray = (arr) =>
(arr || []).map((a) => ({
center: a.center || a.tax || null,
tax: a.tax || null,
sale: summarizeMoney(a.sale || a.totalSale || Dinero()),
cost: summarizeMoney(a.cost)
}));
/**
* Internal per-center bucket shape for *sales*.
* We keep separate buckets for RR so we can split
* taxable vs non-taxable labor lines later.
*/
function emptyCenterBucket() {
const zero = Dinero();
return {
partsSale: zero, // parts sale
laborTaxableSale: zero, // labor that should be taxed in RR
laborNonTaxableSale: zero, // labor that should NOT be taxed in RR
extrasSale: zero // MAPA/MASH/towing/storage/PAO/etc
};
}
function ensureCenterBucket(hash, center) {
if (!hash[center]) hash[center] = emptyCenterBucket();
return hash[center];
}
/**
* Thin logger wrapper: always uses CreateRRLogEvent,
* with structured data passed via meta arg.
*/
function createDebugLogger(connectionData) {
return (msg, meta, level = "DEBUG") => {
const baseMsg = "rr-calculate-allocations " + msg;
CreateRRLogEvent(connectionData, level, baseMsg, meta !== undefined ? meta : undefined);
};
}
/**
* Query job data for allocations.
*/
async function QueryJobData(connectionData, token, jobid) {
CreateRRLogEvent(connectionData, "DEBUG", "Querying job data for allocations", { jobid });
const client = new GraphQLClient(process.env.GRAPHQL_ENDPOINT, {});
const result = await client.setHeaders({ Authorization: token }).request(queries.GET_CDK_ALLOCATIONS, { id: jobid });
return result.jobs_by_pk;
}
/**
* Build tax allocation object depending on environment (imex vs rome).
* This matches the original logic, just split into its own helper.
*/
function buildTaxAllocations(bodyshop, job) {
return InstanceManager({
executeFunction: true,
deubg: true,
args: [],
imex: () => ({
state: {
center: bodyshop.md_responsibility_centers.taxes.state.name,
sale: Dinero(job.job_totals.totals.state_tax),
cost: Dinero(),
profitCenter: bodyshop.md_responsibility_centers.taxes.state,
costCenter: bodyshop.md_responsibility_centers.taxes.state
},
federal: {
center: bodyshop.md_responsibility_centers.taxes.federal.name,
sale: Dinero(job.job_totals.totals.federal_tax),
cost: Dinero(),
profitCenter: bodyshop.md_responsibility_centers.taxes.federal,
costCenter: bodyshop.md_responsibility_centers.taxes.federal
}
}),
rome: () => ({
tax_ty1: {
center: bodyshop.md_responsibility_centers.taxes.tax_ty1.name,
sale: Dinero(job.job_totals.totals.us_sales_tax_breakdown.ty1Tax),
cost: Dinero(),
profitCenter: bodyshop.md_responsibility_centers.taxes.tax_ty1,
costCenter: bodyshop.md_responsibility_centers.taxes.tax_ty1
},
tax_ty2: {
center: bodyshop.md_responsibility_centers.taxes.tax_ty2.name,
sale: Dinero(job.job_totals.totals.us_sales_tax_breakdown.ty2Tax),
cost: Dinero(),
profitCenter: bodyshop.md_responsibility_centers.taxes.tax_ty2,
costCenter: bodyshop.md_responsibility_centers.taxes.tax_ty2
},
tax_ty3: {
center: bodyshop.md_responsibility_centers.taxes.tax_ty3.name,
sale: Dinero(job.job_totals.totals.us_sales_tax_breakdown.ty3Tax),
cost: Dinero(),
profitCenter: bodyshop.md_responsibility_centers.taxes.tax_ty3,
costCenter: bodyshop.md_responsibility_centers.taxes.tax_ty3
},
tax_ty4: {
center: bodyshop.md_responsibility_centers.taxes.tax_ty4.name,
sale: Dinero(job.job_totals.totals.us_sales_tax_breakdown.ty4Tax),
cost: Dinero(),
profitCenter: bodyshop.md_responsibility_centers.taxes.tax_ty4,
costCenter: bodyshop.md_responsibility_centers.taxes.tax_ty4
},
tax_ty5: {
center: bodyshop.md_responsibility_centers.taxes.tax_ty5.name,
sale: Dinero(job.job_totals.totals.us_sales_tax_breakdown.ty5Tax),
cost: Dinero(),
profitCenter: bodyshop.md_responsibility_centers.taxes.tax_ty5,
costCenter: bodyshop.md_responsibility_centers.taxes.tax_ty5
}
})
});
}
/**
* Decide if a labor line is taxable vs non-taxable for RR.
*/
function isLaborTaxable(line) {
return line.tax_part;
}
/**
* Build profitCenterHash from joblines (parts + labor) and detect MAPA/MASH presence.
* Now stores *buckets* instead of a single Dinero per center.
*/
function buildProfitCenterHash(job, debugLog) {
let hasMapaLine = false;
let hasMashLine = false;
const profitCenterHash = job.joblines.reduce((acc, val) => {
// MAPA line?
if (val.db_ref === "936008") {
if (!hasMapaLine) {
debugLog("Detected existing MAPA line in joblines", {
joblineId: val.id,
db_ref: val.db_ref
});
}
hasMapaLine = true;
}
// MASH line?
if (val.db_ref === "936007") {
if (!hasMashLine) {
debugLog("Detected existing MASH line in joblines", {
joblineId: val.id,
db_ref: val.db_ref
});
}
hasMashLine = true;
}
// Parts
if (val.profitcenter_part) {
const bucket = ensureCenterBucket(acc, val.profitcenter_part);
let amount = Dinero({
amount: Math.round(val.act_price * 100)
}).multiply(val.part_qty || 1);
const hasDiscount = (val.prt_dsmk_m && val.prt_dsmk_m !== 0) || (val.prt_dsmk_p && val.prt_dsmk_p !== 0);
if (hasDiscount && DiscountNotAlreadyCounted(val, job.joblines)) {
const discount = val.prt_dsmk_m
? Dinero({ amount: Math.round(val.prt_dsmk_m * 100) })
: Dinero({
amount: Math.round(val.act_price * 100)
})
.multiply(val.part_qty || 0)
.percentage(Math.abs(val.prt_dsmk_p || 0))
.multiply(val.prt_dsmk_p > 0 ? 1 : -1);
amount = amount.add(discount);
}
bucket.partsSale = bucket.partsSale.add(amount);
}
// Labor
if (val.profitcenter_labor && val.mod_lbr_ty) {
const bucket = ensureCenterBucket(acc, val.profitcenter_labor);
const rateKey = `rate_${val.mod_lbr_ty.toLowerCase()}`;
const rate = job[rateKey];
const laborAmount = Dinero({
amount: Math.round(rate * 100)
}).multiply(val.mod_lb_hrs);
if (isLaborTaxable(val)) {
bucket.laborTaxableSale = bucket.laborTaxableSale.add(laborAmount);
} else {
bucket.laborNonTaxableSale = bucket.laborNonTaxableSale.add(laborAmount);
}
}
return acc;
}, {});
debugLog("profitCenterHash after joblines", {
hasMapaLine,
hasMashLine,
centers: Object.entries(profitCenterHash).map(([center, b]) => ({
center,
parts: summarizeMoney(b.partsSale),
laborTaxable: summarizeMoney(b.laborTaxableSale),
laborNonTaxable: summarizeMoney(b.laborNonTaxableSale),
extras: summarizeMoney(b.extrasSale)
}))
});
return { profitCenterHash, hasMapaLine, hasMashLine };
}
/**
* Build costCenterHash from bills and timetickets.
*/
function buildCostCenterHash(job, selectedDmsAllocationConfig, disablebillwip, debugLog) {
let costCenterHash = {};
// 1) Bills -> costs
debugLog("disablebillwip flag", { disablebillwip });
if (!disablebillwip) {
costCenterHash = job.bills.reduce((billAcc, bill) => {
bill.billlines.forEach((line) => {
const targetCenter = selectedDmsAllocationConfig.costs[line.cost_center];
if (!targetCenter) return;
if (!billAcc[targetCenter]) billAcc[targetCenter] = Dinero();
const lineDinero = Dinero({
amount: Math.round((line.actual_cost || 0) * 100)
})
.multiply(line.quantity)
.multiply(bill.is_credit_memo ? -1 : 1);
billAcc[targetCenter] = billAcc[targetCenter].add(lineDinero);
});
return billAcc;
}, {});
}
debugLog("costCenterHash after bills (pre-timetickets)", {
centers: Object.entries(costCenterHash || {}).map(([center, dinero]) => ({
center,
...summarizeMoney(dinero)
}))
});
// 2) Timetickets -> costs
job.timetickets.forEach((ticket) => {
const effectiveHours =
ticket.employee && ticket.employee.flat_rate ? ticket.productivehrs || 0 : ticket.actualhrs || 0;
const ticketTotal = Dinero({
amount: Math.round(ticket.rate * effectiveHours * 100)
});
const targetCenter = selectedDmsAllocationConfig.costs[ticket.ciecacode];
if (!targetCenter) return;
if (!costCenterHash[targetCenter]) costCenterHash[targetCenter] = Dinero();
costCenterHash[targetCenter] = costCenterHash[targetCenter].add(ticketTotal);
});
debugLog("costCenterHash after timetickets", {
centers: Object.entries(costCenterHash || {}).map(([center, dinero]) => ({
center,
...summarizeMoney(dinero)
}))
});
return costCenterHash;
}
/**
* Add manual MAPA / MASH sales where needed (into extrasSale bucket).
*/
function applyMapaMashManualLines({
job,
selectedDmsAllocationConfig,
bodyshop,
profitCenterHash,
hasMapaLine,
hasMashLine,
debugLog
}) {
// MAPA
if (!hasMapaLine && job.job_totals.rates.mapa.total.amount > 0) {
const mapaAccountName = selectedDmsAllocationConfig.profits.MAPA;
const mapaAccount = bodyshop.md_responsibility_centers.profits.find((c) => c.name === mapaAccountName);
if (mapaAccount) {
debugLog("Adding MAPA Line Manually", {
mapaAccountName,
amount: summarizeMoney(Dinero(job.job_totals.rates.mapa.total))
});
const bucket = ensureCenterBucket(profitCenterHash, mapaAccountName);
bucket.extrasSale = bucket.extrasSale.add(Dinero(job.job_totals.rates.mapa.total));
} else {
debugLog("NO MAPA ACCOUNT FOUND!!", { mapaAccountName });
}
}
// MASH
if (!hasMashLine && job.job_totals.rates.mash.total.amount > 0) {
const mashAccountName = selectedDmsAllocationConfig.profits.MASH;
const mashAccount = bodyshop.md_responsibility_centers.profits.find((c) => c.name === mashAccountName);
if (mashAccount) {
debugLog("Adding MASH Line Manually", {
mashAccountName,
amount: summarizeMoney(Dinero(job.job_totals.rates.mash.total))
});
const bucket = ensureCenterBucket(profitCenterHash, mashAccountName);
bucket.extrasSale = bucket.extrasSale.add(Dinero(job.job_totals.rates.mash.total));
} else {
debugLog("NO MASH ACCOUNT FOUND!!", { mashAccountName });
}
}
return profitCenterHash;
}
/**
* Apply materials costing (MAPA/MASH cost side) when configured.
*/
function applyMaterialsCosting({ job, bodyshop, selectedDmsAllocationConfig, costCenterHash, debugLog }) {
const { cdk_configuration } = bodyshop || {};
if (!cdk_configuration?.sendmaterialscosting) return costCenterHash;
debugLog("sendmaterialscosting enabled", {
sendmaterialscosting: cdk_configuration.sendmaterialscosting,
use_paint_scale_data: job.bodyshop.use_paint_scale_data,
mixdataLength: job.mixdata?.length || 0
});
const percent = cdk_configuration.sendmaterialscosting;
// Paint Mat (MAPA)
const mapaAccountName = selectedDmsAllocationConfig.costs.MAPA;
const mapaAccount = bodyshop.md_responsibility_centers.costs.find((c) => c.name === mapaAccountName);
if (mapaAccount) {
if (!costCenterHash[mapaAccountName]) costCenterHash[mapaAccountName] = Dinero();
if (job.bodyshop.use_paint_scale_data === true) {
if (job.mixdata && job.mixdata.length > 0) {
debugLog("Using mixdata for MAPA cost", {
mapaAccountName,
totalliquidcost: job.mixdata[0] && job.mixdata[0].totalliquidcost
});
costCenterHash[mapaAccountName] = costCenterHash[mapaAccountName].add(
Dinero({
amount: Math.round(((job.mixdata[0] && job.mixdata[0].totalliquidcost) || 0) * 100)
})
);
} else {
debugLog("Using percentage of MAPA total (no mixdata)", { mapaAccountName });
costCenterHash[mapaAccountName] = costCenterHash[mapaAccountName].add(
Dinero(job.job_totals.rates.mapa.total).percentage(percent)
);
}
} else {
debugLog("Using percentage of MAPA total (no paint scale data)", { mapaAccountName });
costCenterHash[mapaAccountName] = costCenterHash[mapaAccountName].add(
Dinero(job.job_totals.rates.mapa.total).percentage(percent)
);
}
} else {
debugLog("NO MAPA ACCOUNT FOUND (costs)!!", { mapaAccountName });
}
// Shop Mat (MASH)
const mashAccountName = selectedDmsAllocationConfig.costs.MASH;
const mashAccount = bodyshop.md_responsibility_centers.costs.find((c) => c.name === mashAccountName);
if (mashAccount) {
debugLog("Adding MASH material costing", { mashAccountName });
if (!costCenterHash[mashAccountName]) costCenterHash[mashAccountName] = Dinero();
costCenterHash[mashAccountName] = costCenterHash[mashAccountName].add(
Dinero(job.job_totals.rates.mash.total).percentage(percent)
);
} else {
debugLog("NO MASH ACCOUNT FOUND (costs)!!", { mashAccountName });
}
return costCenterHash;
}
/**
* Apply non-tax extras (PVRT, towing, storage, PAO).
* Extras go into the extrasSale bucket.
*/
function applyExtras({ job, bodyshop, selectedDmsAllocationConfig, profitCenterHash, taxAllocations, debugLog }) {
const { ca_bc_pvrt } = job;
// BC PVRT -> state tax
if (ca_bc_pvrt) {
debugLog("Adding PVRT to state tax allocation", { ca_bc_pvrt });
taxAllocations.state.sale = taxAllocations.state.sale.add(Dinero({ amount: Math.round((ca_bc_pvrt || 0) * 100) }));
}
// Towing
if (job.towing_payable && job.towing_payable !== 0) {
const towAccountName = selectedDmsAllocationConfig.profits.TOW;
const towAccount = bodyshop.md_responsibility_centers.profits.find((c) => c.name === towAccountName);
if (towAccount) {
debugLog("Adding towing_payable to TOW account", {
towAccountName,
towing_payable: job.towing_payable
});
const bucket = ensureCenterBucket(profitCenterHash, towAccountName);
bucket.extrasSale = bucket.extrasSale.add(
Dinero({
amount: Math.round((job.towing_payable || 0) * 100)
})
);
} else {
debugLog("NO TOW ACCOUNT FOUND!!", { towAccountName });
}
}
// Storage (shares TOW account)
if (job.storage_payable && job.storage_payable !== 0) {
const storageAccountName = selectedDmsAllocationConfig.profits.TOW;
const towAccount = bodyshop.md_responsibility_centers.profits.find((c) => c.name === storageAccountName);
if (towAccount) {
debugLog("Adding storage_payable to TOW account", {
storageAccountName,
storage_payable: job.storage_payable
});
const bucket = ensureCenterBucket(profitCenterHash, storageAccountName);
bucket.extrasSale = bucket.extrasSale.add(
Dinero({
amount: Math.round((job.storage_payable || 0) * 100)
})
);
} else {
debugLog("NO STORAGE/TOW ACCOUNT FOUND!!", { storageAccountName });
}
}
// Bottom line adjustment -> PAO
if (job.adjustment_bottom_line && job.adjustment_bottom_line !== 0) {
const otherAccountName = selectedDmsAllocationConfig.profits.PAO;
const otherAccount = bodyshop.md_responsibility_centers.profits.find((c) => c.name === otherAccountName);
if (otherAccount) {
debugLog("Adding adjustment_bottom_line to PAO", {
otherAccountName,
adjustment_bottom_line: job.adjustment_bottom_line
});
const bucket = ensureCenterBucket(profitCenterHash, otherAccountName);
bucket.extrasSale = bucket.extrasSale.add(
Dinero({
amount: Math.round((job.adjustment_bottom_line || 0) * 100)
})
);
} else {
debugLog("NO PAO ACCOUNT FOUND!!", { otherAccountName });
}
}
return { profitCenterHash, taxAllocations };
}
/**
* Apply Rome-specific profile adjustments (parts + rates).
* These also feed into the *sales* buckets.
*/
/**
* Apply Rome-specific profile adjustments (parts + rates).
* These also feed into the *sales* buckets.
*/
/**
* Apply Rome-specific profile adjustments (parts + rates).
* These also feed into the *sales* buckets.
*/
function applyRomeProfileAdjustments({
job,
bodyshop,
selectedDmsAllocationConfig,
profitCenterHash,
debugLog,
connectionData
}) {
// Only relevant for Rome instances
if (!InstanceManager({ rome: true })) return profitCenterHash;
if (!selectedDmsAllocationConfig || !selectedDmsAllocationConfig.profits) {
debugLog("ROME profile adjustments skipped (no selectedDmsAllocationConfig.profits)");
return profitCenterHash;
}
const partsAdjustments = job?.job_totals?.parts?.adjustments || {};
const rateMap = job?.job_totals?.rates || {};
debugLog("ROME profile adjustments block entered", {
partAdjustmentKeys: Object.keys(partsAdjustments),
rateKeys: Object.keys(rateMap)
});
// Parts adjustments
Object.keys(partsAdjustments).forEach((key) => {
const accountName = selectedDmsAllocationConfig.profits[key];
const otherAccount = bodyshop.md_responsibility_centers.profits.find((c) => c.name === accountName);
if (otherAccount) {
const bucket = ensureCenterBucket(profitCenterHash, accountName);
const adjMoney = Dinero(partsAdjustments[key]);
bucket.extrasSale = bucket.extrasSale.add(adjMoney);
debugLog("Added parts adjustment", {
key,
accountName,
adjustment: summarizeMoney(adjMoney)
});
} else {
CreateRRLogEvent(
connectionData,
"ERROR",
"Error encountered in rr-calculate-allocations. Unable to find parts adjustment account.",
{ accountName, key }
);
debugLog("Missing parts adjustment account", { key, accountName });
}
});
// Labor / materials adjustments (match CDK semantics: check `adjustment`, add `adjustments`)
Object.keys(rateMap).forEach((key) => {
const rate = rateMap[key];
if (!rate || !rate.adjustment) return;
const checkMoney = Dinero(rate.adjustment);
if (checkMoney.isZero()) return;
const accountName = selectedDmsAllocationConfig.profits[key.toUpperCase()];
const otherAccount = bodyshop.md_responsibility_centers.profits.find((c) => c.name === accountName);
if (otherAccount) {
const bucket = ensureCenterBucket(profitCenterHash, accountName);
// Note: we intentionally use `rate.adjustments` here to mirror CDK behaviour
const adjMoney = Dinero(rate.adjustments);
bucket.extrasSale = bucket.extrasSale.add(adjMoney);
debugLog("Added rate adjustment", {
key,
accountName,
adjustment: summarizeMoney(adjMoney)
});
} else {
CreateRRLogEvent(
connectionData,
"ERROR",
"Error encountered in rr-calculate-allocations. Unable to find rate adjustment account.",
{ accountName, key }
);
debugLog("Missing rate adjustment account", { key, accountName });
}
});
return profitCenterHash;
}
/**
* Build job-level profit/cost allocations for each center.
* PUBLIC SHAPE (for RR):
* {
* center,
* partsSale,
* laborTaxableSale,
* laborNonTaxableSale,
* extrasSale,
* totalSale,
* cost,
* profitCenter,
* costCenter
* }
*/
function buildJobAllocations(bodyshop, profitCenterHash, costCenterHash, debugLog) {
const centers = _.union(Object.keys(profitCenterHash), Object.keys(costCenterHash));
const jobAllocations = centers.map((center) => {
const bucket = profitCenterHash[center] || emptyCenterBucket();
const totalSale = bucket.partsSale
.add(bucket.laborTaxableSale)
.add(bucket.laborNonTaxableSale)
.add(bucket.extrasSale);
const profitCenter = bodyshop.md_responsibility_centers.profits.find((c) => c.name === center);
const costCenter = bodyshop.md_responsibility_centers.costs.find((c) => c.name === center);
return {
center,
partsSale: bucket.partsSale,
laborTaxableSale: bucket.laborTaxableSale,
laborNonTaxableSale: bucket.laborNonTaxableSale,
extrasSale: bucket.extrasSale,
totalSale,
cost: costCenterHash[center] || Dinero(),
profitCenter,
costCenter
};
});
debugLog(
"jobAllocations built",
jobAllocations.map((row) => ({
center: row.center,
parts: summarizeMoney(row.partsSale),
laborTaxable: summarizeMoney(row.laborTaxableSale),
laborNonTaxable: summarizeMoney(row.laborNonTaxableSale),
extras: summarizeMoney(row.extrasSale),
totalSale: summarizeMoney(row.totalSale),
cost: summarizeMoney(row.cost)
}))
);
return jobAllocations;
}
/**
* Build tax allocations array from taxAllocations hash.
* Shape is unchanged from original (except extra logging).
*/
function buildTaxAllocArray(taxAllocations, selectedDmsAllocationConfig, debugLog) {
const taxAllocArray = Object.keys(taxAllocations)
.filter((key) => taxAllocations[key].sale.getAmount() > 0 || taxAllocations[key].cost.getAmount() > 0)
.map((key) => {
if (
key === "federal" &&
selectedDmsAllocationConfig.gst_override &&
selectedDmsAllocationConfig.gst_override !== ""
) {
const ret = { ...taxAllocations[key], tax: key };
ret.costCenter.dms_acctnumber = selectedDmsAllocationConfig.gst_override;
ret.profitCenter.dms_acctnumber = selectedDmsAllocationConfig.gst_override;
return ret;
}
return { ...taxAllocations[key], tax: key };
});
debugLog("taxAllocArray built", summarizeAllocationsArray(taxAllocArray));
return taxAllocArray;
}
/**
* Build adjustment allocations (ttl_adjustment + ttl_tax_adjustment).
*/
function buildAdjustmentAllocations(job, bodyshop, debugLog) {
const ttlAdjArray = job.job_totals.totals.ttl_adjustment
? [
{
center: "SUB ADJ",
sale: Dinero(job.job_totals.totals.ttl_adjustment),
cost: Dinero(),
profitCenter: {
name: "SUB ADJ",
accountdesc: "SUB ADJ",
accountitem: "SUB ADJ",
accountname: "SUB ADJ",
dms_acctnumber: bodyshop.md_responsibility_centers.ttl_adjustment.dms_acctnumber
},
costCenter: {}
}
]
: [];
const ttlTaxAdjArray = job.job_totals.totals.ttl_tax_adjustment
? [
{
center: "TAX ADJ",
sale: Dinero(job.job_totals.totals.ttl_tax_adjustment),
cost: Dinero(),
profitCenter: {
name: "TAX ADJ",
accountdesc: "TAX ADJ",
accountitem: "TAX ADJ",
accountname: "TAX ADJ",
dms_acctnumber: bodyshop.md_responsibility_centers.ttl_tax_adjustment.dms_acctnumber
},
costCenter: {}
}
]
: [];
if (ttlAdjArray.length) {
debugLog("ttl_adjustment allocation added", summarizeAllocationsArray(ttlAdjArray));
}
if (ttlTaxAdjArray.length) {
debugLog("ttl_tax_adjustment allocation added", summarizeAllocationsArray(ttlTaxAdjArray));
}
return { ttlAdjArray, ttlTaxAdjArray };
}
/**
* Core allocation calculation RR-only, with bucketed sales.
*
* RETURN SHAPE:
* {
* jobAllocations, // per-center buckets (see buildJobAllocations)
* taxAllocArray, // tax allocations
* ttlAdjArray, // ttl_adjustment allocations
* ttlTaxAdjArray // ttl_tax_adjustment allocations
* }
*/
function calculateAllocations(connectionData, job) {
const { bodyshop } = job;
const debugLog = createDebugLogger(connectionData);
debugLog("ENTER", {
bodyshopId: bodyshop?.id,
bodyshopName: bodyshop?.name,
dms_allocation: job.dms_allocation,
hasBills: Array.isArray(job.bills) ? job.bills.length : 0,
joblines: Array.isArray(job.joblines) ? job.joblines.length : 0,
timetickets: Array.isArray(job.timetickets) ? job.timetickets.length : 0
});
// 1) Tax allocations
let taxAllocations = buildTaxAllocations(bodyshop, job);
debugLog("Initial taxAllocations", summarizeTaxAllocations(taxAllocations));
// 2) Profit centers from job lines + MAPA/MASH detection
const { profitCenterHash: initialProfitHash, hasMapaLine, hasMashLine } = buildProfitCenterHash(job, debugLog);
// 3) DMS allocation config
const selectedDmsAllocationConfig =
bodyshop.md_responsibility_centers.dms_defaults.find((d) => d.name === job.dms_allocation) || null;
CreateRRLogEvent(connectionData, "DEBUG", "Using DMS Allocation for cost export", {
allocationName: selectedDmsAllocationConfig && selectedDmsAllocationConfig.name
});
debugLog("Selected DMS allocation config", {
name: selectedDmsAllocationConfig && selectedDmsAllocationConfig.name
});
// 4) Cost centers from bills and timetickets
const disablebillwip = !!bodyshop?.pbs_configuration?.disablebillwip;
let costCenterHash = buildCostCenterHash(job, selectedDmsAllocationConfig, disablebillwip, debugLog);
// 5) Manual MAPA/MASH sales (when needed)
let profitCenterHash = applyMapaMashManualLines({
job,
selectedDmsAllocationConfig,
bodyshop,
profitCenterHash: initialProfitHash,
hasMapaLine,
hasMashLine,
debugLog
});
// 6) Materials costing (MAPA/MASH cost side)
costCenterHash = applyMaterialsCosting({
job,
bodyshop,
selectedDmsAllocationConfig,
costCenterHash,
debugLog
});
// 7) PVRT / towing / storage / PAO extras
({ profitCenterHash, taxAllocations } = applyExtras({
job,
bodyshop,
selectedDmsAllocationConfig,
profitCenterHash,
taxAllocations,
debugLog
}));
// 8) Rome-only profile-level adjustments
profitCenterHash = applyRomeProfileAdjustments({
job,
bodyshop,
selectedDmsAllocationConfig,
profitCenterHash,
debugLog,
connectionData
});
debugLog("profitCenterHash before jobAllocations build", {
centers: Object.entries(profitCenterHash || {}).map(([center, b]) => ({
center,
parts: summarizeMoney(b.partsSale),
laborTaxable: summarizeMoney(b.laborTaxableSale),
laborNonTaxable: summarizeMoney(b.laborNonTaxableSale),
extras: summarizeMoney(b.extrasSale)
}))
});
debugLog("costCenterHash before jobAllocations build", {
centers: Object.entries(costCenterHash || {}).map(([center, dinero]) => ({
center,
...summarizeMoney(dinero)
}))
});
// 9) Build job-level allocations & tax allocations
const jobAllocations = buildJobAllocations(bodyshop, profitCenterHash, costCenterHash, debugLog);
const taxAllocArray = buildTaxAllocArray(taxAllocations, selectedDmsAllocationConfig, debugLog);
const { ttlAdjArray, ttlTaxAdjArray } = buildAdjustmentAllocations(job, bodyshop, debugLog);
const result = {
jobAllocations,
taxAllocArray,
ttlAdjArray,
ttlTaxAdjArray
};
debugLog("FINAL allocations summary", {
jobAllocationsCount: jobAllocations.length,
taxAllocCount: taxAllocArray.length,
ttlAdjCount: ttlAdjArray.length,
ttlTaxAdjCount: ttlTaxAdjArray.length
});
debugLog("EXIT");
return result;
}
/**
* HTTP route wrapper (kept for compatibility; still logs via RR logger).
* Responds with { data: { jobAllocations, taxAllocArray, ttlAdjArray, ttlTaxAdjArray } }
*/
exports.defaultRoute = async function (req, res) {
try {
CreateRRLogEvent(req, "DEBUG", "Received request to calculate allocations", { jobid: req.body.jobid });
const jobData = await QueryJobData(req, req.BearerToken, req.body.jobid);
const data = calculateAllocations(req, jobData);
return res.status(200).json({ data });
} catch (error) {
CreateRRLogEvent(req, "ERROR", "Error encountered in rr-calculate-allocations.", {
message: error?.message || String(error),
stack: error?.stack
});
res.status(500).json({ error: `Error encountered in rr-calculate-allocations. ${error}` });
}
};
/**
* Socket entry point (what rr-job-export & rr-register-socket-events call).
* Reynolds-only: WSS + RR logger.
*
* Returns the same object as calculateAllocations().
*/
exports.default = async function (socket, jobid) {
try {
const token = `Bearer ${socket.handshake.auth.token}`;
const jobData = await QueryJobData(socket, token, jobid);
return calculateAllocations(socket, jobData);
} catch (error) {
CreateRRLogEvent(socket, "ERROR", "Error encountered in rr-calculate-allocations.", {
message: error?.message || String(error),
stack: error?.stack
});
return null;
}
};

View File

@@ -2,7 +2,7 @@ const { buildRRRepairOrderPayload } = require("./rr-job-helpers");
const { buildClientAndOpts } = require("./rr-lookup");
const CreateRRLogEvent = require("./rr-logger-event");
const { extractRrResponsibilityCenters } = require("./rr-responsibility-centers");
const CdkCalculateAllocations = require("../cdk/cdk-calculate-allocations").default;
const CdkCalculateAllocations = require("./rr-calculate-allocations").default;
/**
* Derive RR status information from response object.
@@ -116,18 +116,25 @@ const exportJobToRR = async (args) => {
// 2) Allocations (sales + cost by center, with rr_* metadata already attached)
try {
allocations = await CdkCalculateAllocations(socket, job.id);
const allocResult = await CdkCalculateAllocations(socket, job.id);
// We only need the per-center job allocations for RO.GOG / ROLABOR.
allocations = Array.isArray(allocResult?.jobAllocations) ? allocResult.jobAllocations : [];
CreateRRLogEvent(socket, "SILLY", "RR allocations resolved", {
hasAllocations: Array.isArray(allocations),
count: Array.isArray(allocations) ? allocations.length : 0
hasAllocations: allocations.length > 0,
count: allocations.length,
taxAllocCount: Array.isArray(allocResult?.taxAllocArray) ? allocResult.taxAllocArray.length : 0,
ttlAdjCount: Array.isArray(allocResult?.ttlAdjArray) ? allocResult.ttlAdjArray.length : 0,
ttlTaxAdjCount: Array.isArray(allocResult?.ttlTaxAdjArray) ? allocResult.ttlTaxAdjArray.length : 0
});
} catch (e) {
CreateRRLogEvent(socket, "ERROR", "Failed to calculate RR allocations", {
message: e?.message,
stack: e?.stack
});
allocations = null; // We still proceed with a header-only RO if this fails.
// Proceed with a header-only RO if allocations fail.
allocations = [];
}
// 3) OpCode (global, but overridable)

View File

@@ -54,7 +54,36 @@ const asN2 = (dineroLike) => {
/**
* Build RO.GOG structure for the reynolds-rome-client `createRepairOrder` payload
* from CDK allocations.
* from allocations.
*
* Supports the new allocation shape:
* {
* center,
* partsSale,
* laborTaxableSale,
* laborNonTaxableSale,
* extrasSale,
* totalSale,
* cost,
* profitCenter,
* costCenter
* }
*
* For each center, we can emit up to 3 GOG *segments*:
* - parts+extras (uses profitCenter.rr_cust_txbl_flag)
* - taxable labor (CustTxblNTxblFlag="T")
* - non-tax labor (CustTxblNTxblFlag="N")
*
* IMPORTANT CHANGE:
* Each segment becomes its OWN JobNo / AllGogOpCodeInfo, with exactly one
* AllGogLineItmInfo inside. This makes the count of:
* - <AllGogOpCodeInfo> (ROGOG)
* - <OpCodeLaborInfo> (ROLABOR)
* match 1:1, and ensures taxable/non-taxable flags line up by JobNo.
*
* We now also attach segmentKind/segmentIndex/segmentCount metadata on each op
* for UI/debug purposes. The XML templates can safely ignore these.
*
* @param {Array} allocations
* @param {Object} opts
* @param {string} opts.opCode - RR OpCode for the job (global, overridable)
@@ -67,45 +96,150 @@ const buildRogogFromAllocations = (allocations, { opCode, payType = "Cust", roNo
const ops = [];
/**
* Normalize various "money-like" shapes to integer cents.
* Supports:
* - Dinero instances (getAmount / toUnit)
* - { cents }
* - { amount, precision }
* - plain numbers (treated as units, e.g. dollars)
*/
const toCents = (value) => {
if (!value) return 0;
if (typeof value.getAmount === "function") {
return value.getAmount();
}
if (typeof value.toUnit === "function") {
const unit = value.toUnit();
return Number.isFinite(unit) ? Math.round(unit * 100) : 0;
}
if (typeof value.cents === "number") {
return value.cents;
}
if (typeof value.amount === "number") {
const precision = typeof value.precision === "number" ? value.precision : 2;
if (precision === 2) return value.amount;
const factor = Math.pow(10, 2 - precision);
return Math.round(value.amount * factor);
}
if (typeof value === "number") {
return Math.round(value * 100);
}
return 0;
};
const asMoneyLike = (amountCents) => ({
amount: amountCents || 0,
precision: 2
});
for (const alloc of allocations) {
const pc = alloc?.profitCenter || {};
const breakOut = pc.rr_gogcode;
const itemType = pc.rr_item_type;
// Only centers that have been configured for RR GOG are included
// Only centers configured for RR GOG are included
if (!breakOut || !itemType) continue;
const saleN2 = asN2(alloc.sale);
const costN2 = asN2(alloc.cost);
const partsCents = toCents(alloc.partsSale);
const extrasCents = toCents(alloc.extrasSale);
const laborTaxableCents = toCents(alloc.laborTaxableSale);
const laborNonTaxableCents = toCents(alloc.laborNonTaxableSale);
const costCents = toCents(alloc.cost);
const itemDesc = pc.accountdesc || pc.accountname || alloc.center || "";
const jobNo = String(ops.length + 1); // 1-based JobNo
// Parts + extras share a single segment
const partsExtrasCents = partsCents + extrasCents;
ops.push({
opCode,
jobNo,
lines: [
{
breakOut,
itemType,
itemDesc,
custQty: "1.0",
// warrQty: "0.0",
// intrQty: "0.0",
custPayTypeFlag: "C",
// warrPayTypeFlag: "W",
// intrPayTypeFlag: "I",
custTxblNtxblFlag: pc.rr_cust_txbl_flag || "T",
// warrTxblNtxblFlag: "N",
// intrTxblNtxblFlag: "N",
amount: {
payType,
amtType: "Unit",
custPrice: saleN2,
dlrCost: costN2
}
const segments = [];
// 1) Parts + extras segment (respect center's default tax flag)
if (partsExtrasCents !== 0) {
segments.push({
kind: "partsExtras",
saleCents: partsExtrasCents,
txFlag: pc.rr_cust_txbl_flag || "N"
});
}
// 2) Taxable labor segment -> "T"
if (laborTaxableCents !== 0) {
segments.push({
kind: "laborTaxable",
saleCents: laborTaxableCents,
txFlag: "T"
});
}
// 3) Non-taxable labor segment -> "N"
if (laborNonTaxableCents !== 0) {
segments.push({
kind: "laborNonTaxable",
saleCents: laborNonTaxableCents,
txFlag: "N"
});
}
if (!segments.length) continue;
// Proportionally split cost across segments based on their sale amounts
const totalCostCents = costCents;
const totalSaleCents = segments.reduce((sum, seg) => sum + seg.saleCents, 0);
let remainingCostCents = totalCostCents;
segments.forEach((seg, idx) => {
let segCost = 0;
if (totalCostCents > 0 && totalSaleCents > 0) {
if (idx === segments.length - 1) {
// Last segment gets the remainder to avoid rounding drift
segCost = remainingCostCents;
} else {
segCost = Math.round((seg.saleCents / totalSaleCents) * totalCostCents);
remainingCostCents -= segCost;
}
]
}
seg.costCents = segCost;
});
const itemDescBase = pc.accountdesc || pc.accountname || alloc.center || "";
const segmentCount = segments.length;
// Each segment becomes its own op / JobNo with a single line
segments.forEach((seg, idx) => {
const jobNo = String(ops.length + 1); // global, 1-based JobNo across all centers/segments
const line = {
breakOut,
itemType,
itemDesc: itemDescBase,
custQty: "1.0",
custPayTypeFlag: "C",
custTxblNtxblFlag: seg.txFlag || "N",
amount: {
payType,
amtType: "Unit",
custPrice: asN2(asMoneyLike(seg.saleCents)),
dlrCost: asN2(asMoneyLike(seg.costCents))
}
};
ops.push({
opCode,
jobNo,
lines: [line], // exactly one AllGogLineItmInfo per AllGogOpCodeInfo
// Extra metadata for UI / debugging
segmentKind: seg.kind,
segmentIndex: idx,
segmentCount
});
});
}
@@ -131,16 +265,18 @@ const buildRolaborFromRogog = (rogg, { payType = "Cust" } = {}) => {
const ops = rogg.ops.map((op) => {
const firstLine = op.lines?.[0] || {};
// Pull tax flag from the GOG line.
// Prefer the property we set in buildRogogFromAllocations (custTxblNTxblFlag),
// but also accept custTxblNtxblFlag in case we ever change naming.
const txFlag = firstLine.custTxblNtxblFlag ?? "N";
const linePayType = firstLine.custPayTypeFlag || "C";
return {
opCode: op.opCode,
jobNo: op.jobNo,
custPayTypeFlag: firstLine.custPayTypeFlag || "C",
// warrPayTypeFlag: firstLine.warrPayTypeFlag || "W",
// intrPayTypeFlag: firstLine.intrPayTypeFlag || "I",
custTxblNtxblFlag: firstLine.custTxblNtxblFlag || "N",
// warrTxblNtxblFlag: firstLine.warrTxblNtxblFlag || "N",
// intrTxblNtxblFlag: firstLine.intrTxblNtxblFlag || "N",
// vlrCode: undefined,
custPayTypeFlag: linePayType,
custTxblNtxblFlag: txFlag,
bill: {
payType,
jobTotalHrs: "0",
@@ -277,24 +413,6 @@ const buildRRRepairOrderPayload = ({
}
}
}
// --- TAX HEADER TEMPORARILY DISABLED ---
// We intentionally do NOT attach payload.tax right now so that the Mustache
// section that renders <TaxCodeInfo> stays false and no TaxCodeInfo is sent.
//
// Keeping this commented-out for future enablement once RR confirms header
// tax handling behaviour.
//
// if (effectiveTaxCode) {
// const taxInfo = buildTaxFromAllocations(allocations, {
// taxCode: effectiveTaxCode,
// payType: "Cust"
// });
//
// if (taxInfo) {
// payload.tax = taxInfo;
// }
// }
}
return payload;
@@ -400,23 +518,49 @@ const normalizeVehicleCandidates = (res) => {
};
/**
* Build a minimal Rolabor structure in the new normalized shape.
*
* Useful for tests or for scenarios where you want a single zero-dollar
* Rolabor op but don't have GOG data. Shape matches payload.rolabor for the
* reynolds-rome-client builders.
*
* @param {Object} opts
* @param {string} opts.opCode
* @param {number|string} [opts.jobNo=1]
* @param {string} [opts.payType="Cust"]
* @returns {null|{ops: Array}}
* Build split labor lines from job allocations.
* @param jobAllocations
* @returns {*[]}
*/
const buildSplitLaborLinesFromAllocations = (jobAllocations) => {
const lines = [];
for (const alloc of jobAllocations || []) {
const { center, laborTaxableSale, laborNonTaxableSale, profitCenter, costCenter } = alloc;
// Taxable labor
if (laborTaxableSale && !laborTaxableSale.isZero()) {
lines.push({
centerName: center,
profitCenter,
costCenter,
amount: laborTaxableSale,
isTaxable: true,
source: "labor"
});
}
// Non-taxable labor
if (laborNonTaxableSale && !laborNonTaxableSale.isZero()) {
lines.push({
centerName: center,
profitCenter,
costCenter,
amount: laborNonTaxableSale,
isTaxable: false,
source: "labor"
});
}
}
return lines;
};
module.exports = {
QueryJobData,
buildRRRepairOrderPayload,
makeCustomerSearchPayloadFromJob,
buildSplitLaborLinesFromAllocations,
makeVehicleSearchPayloadFromJob,
normalizeCustomerCandidates,
normalizeVehicleCandidates,

View File

@@ -45,6 +45,7 @@ const safeMeta = (meta) => {
const CreateRRLogEvent = (socket, level = "INFO", message = "", meta = null) => {
const ts = Date.now();
const lvl = String(level || "INFO").toUpperCase();
const normLevel = lvl.toLowerCase();
const msg = typeof message === "string" ? message : (message?.toString?.() ?? JSON.stringify(message));
const payload = {
@@ -54,12 +55,13 @@ const CreateRRLogEvent = (socket, level = "INFO", message = "", meta = null) =>
meta: safeMeta(meta)
};
// Console
// Central logger (Winston + CloudWatch + S3)
try {
const fn = logger?.logger?.[lvl.toLowerCase()] ?? logger?.logger?.info ?? console.log;
fn(`[RR] ${new Date(ts).toISOString()} | ${lvl} | ${msg}`, payload.meta);
// user = "RR", record = null, meta = payload.meta
logger.log(`[RR] ${msg}`, normLevel, "RR", null, payload.meta);
} catch {
// ignore console failures
// Fallback console
console.log(`[RR] ${new Date(ts).toISOString()} | ${lvl} | ${msg}`, payload.meta);
}
// Socket

View File

@@ -1,8 +1,8 @@
const CreateRRLogEvent = require("./rr-logger-event");
const { rrCombinedSearch, rrGetAdvisors, buildClientAndOpts } = require("./rr-lookup");
const { QueryJobData } = require("./rr-job-helpers");
const { QueryJobData, buildRogogFromAllocations, buildRolaborFromRogog } = require("./rr-job-helpers");
const { exportJobToRR, finalizeRRRepairOrder } = require("./rr-job-export");
const CdkCalculateAllocations = require("../cdk/cdk-calculate-allocations").default;
const RRCalculateAllocations = require("./rr-calculate-allocations").default;
const { createRRCustomer } = require("./rr-customers");
const { ensureRRServiceVehicle } = require("./rr-service-vehicles");
const { classifyRRVendorError } = require("./rr-errors");
@@ -898,10 +898,73 @@ const registerRREvents = ({ socket, redisHelpers }) => {
socket.on("rr-calculate-allocations", async (jobid, cb) => {
try {
CreateRRLogEvent(socket, "DEBUG", "rr-calculate-allocations: begin", { jobid });
const allocations = await CdkCalculateAllocations(socket, jobid);
cb?.(allocations);
socket.emit("rr-calculate-allocations:result", allocations);
CreateRRLogEvent(socket, "DEBUG", "rr-calculate-allocations: success", { items: allocations?.length });
const raw = await RRCalculateAllocations(socket, jobid);
// If the helper returns an explicit error shape, just pass it through.
if (raw && raw.ok === false) {
cb?.(raw);
socket.emit("rr-calculate-allocations:result", raw);
CreateRRLogEvent(socket, "DEBUG", "rr-calculate-allocations: helper returned error", {
jobid,
error: raw.error
});
return;
}
let ack;
let jobAllocations;
if (Array.isArray(raw)) {
// Legacy shape: plain allocations array
jobAllocations = raw;
ack = { jobAllocations: raw };
} else {
ack = raw || {};
jobAllocations = Array.isArray(ack.jobAllocations) ? ack.jobAllocations : [];
}
// Try to derive OpCode from bodyshop; fall back to default
let opCode = "28TOZ";
try {
const { bodyshopId } = await getSessionOrSocket(redisHelpers, socket);
const bodyshop = await getBodyshopForSocket({ bodyshopId, socket });
opCode = bodyshop?.rr_configuration?.baseOpCode || opCode;
} catch (e) {
CreateRRLogEvent(socket, "WARN", "rr-calculate-allocations: bodyshop lookup failed, using default OpCode", {
error: e.message
});
}
let rogg = null;
let rolabor = null;
try {
rogg = buildRogogFromAllocations(jobAllocations, { opCode, payType: "Cust" });
if (rogg) {
rolabor = buildRolaborFromRogog(rogg, { payType: "Cust" });
}
} catch (e) {
CreateRRLogEvent(socket, "WARN", "rr-calculate-allocations: failed to build ROGOG/ROLABOR preview", {
error: e.message
});
}
const enriched = {
...ack,
rogg,
rolabor
};
cb?.(enriched);
socket.emit("rr-calculate-allocations:result", enriched);
CreateRRLogEvent(socket, "DEBUG", "rr-calculate-allocations: success", {
jobid,
jobAllocations: jobAllocations.length,
hasRogg: !!rogg,
hasRolabor: !!rolabor
});
} catch (e) {
CreateRRLogEvent(socket, "ERROR", "rr-calculate-allocations: failed", { error: e.message, jobid });
cb?.({ ok: false, error: e.message });

View File

@@ -1,6 +1,5 @@
const { buildClientAndOpts, rrCombinedSearch } = require("./rr-lookup");
const CreateRRLogEvent = require("./rr-logger-event");
/**
* Pick and normalize VIN from inputs
* @param vin
@@ -29,6 +28,20 @@ const pickCustNo = ({ selectedCustomerNo, custNo, customerNo }) => {
return c != null ? String(c).trim() : "";
};
/**
* Simple length sanitizer for outbound strings
* Returns undefined if value is null/undefined/empty after trim.
*/
const sanitizeLength = (value, maxLen) => {
if (value == null) return undefined;
let s = String(value).trim();
if (!s) return undefined;
if (maxLen && s.length > maxLen) {
s = s.slice(0, maxLen);
}
return s;
};
/**
* Extract owner customer numbers from combined search results
* @param res
@@ -181,65 +194,57 @@ const ensureRRServiceVehicle = async (args = {}) => {
}
} catch (e) {
// Preflight shouldn't be fatal; log and continue to insert (idempotency will still be handled)
CreateRRLogEvent(socket, "WARN", "{SV} VIN preflight lookup failed; continuing to insert", {
CreateRRLogEvent(socket, "warn", "{SV} VIN preflight lookup failed; continuing to insert", {
vin: vinStr,
error: e?.message
});
}
// --- Attempt insert (idempotent) ---
// IMPORTANT: The current RR lib build validates `vehicleServInfo.customerNo`.
// To be future-proof, we also include top-level `customerNo`.
// Vendor says: MODEL DESCRIPTION HAS MAXIMUM LENGTH OF 20
const rawModelDesc = job?.v_model_desc;
const safeModelDesc = sanitizeLength(rawModelDesc, 20);
if (rawModelDesc && safeModelDesc && rawModelDesc.trim() !== safeModelDesc) {
CreateRRLogEvent(socket, "warn", "{SV} Truncated model description to 20 chars", {
original: rawModelDesc,
truncated: safeModelDesc
});
}
const insertPayload = {
// === Core Vehicle Identity (MANDATORY for success) ===
vin: vinStr.toUpperCase(), // "1FDWX34Y28EB01395"
// Required: 2-character make code (from v_make_desc → known mapping)
vehicleMake: deriveMakeCode(job.v_make_desc), // → "FR" for Ford
// Required: 2-digit year (last 2 digits of v_model_yr)
// 2-character make code (from v_make_desc → known mapping)
vehicleMake: deriveMakeCode(job?.v_make_desc), // → "FR" for Ford
year: job?.v_model_yr || undefined,
// Required: Model number — fallback strategy per ERA behavior
// Most Ford trucks use "T" = Truck. Some systems accept actual code.
// CAN BE (P)assenger , (T)ruck, (O)ther
// Model description (RR: max length 20)
modelDesc: safeModelDesc,
// Model number / carline / other optional fields
mdlNo: undefined,
// === Descriptive Fields (highly recommended) ===
modelDesc: job?.v_model_desc?.trim() || undefined, // "F-350 SD"
carline: job?.v_model_desc?.trim() || undefined, // Series line
extClrDesc: job?.v_color?.trim() || undefined, // "Red"
// Optional but helpful
carline: undefined,
extClrDesc: sanitizeLength(job?.v_color, 30), // safe, configurable if vendor complains
accentClr: undefined,
// === VehicleDetail Flags (CRITICAL — cause silent fails or error 303 if missing) ===
aircond: undefined, // "Y", // Nearly all modern vehicles have A/C
pwrstr: undefined, // "Y", // Power steering = yes on 99% of vehicles post-1990
transm: undefined, // "A", // Default to Automatic — change to "M" only if known manual
turbo: undefined, //"N", // 2008 F-350 6.4L Power Stroke has turbo, but field is optional
engineConfig: undefined, //"V8", // or "6.4L Diesel" — optional but nice
trim: undefined, //"XLT", // You don't have this — safe to omit or guess
aircond: undefined,
pwrstr: undefined,
transm: undefined,
turbo: undefined,
engineConfig: undefined,
trim: undefined,
// License plate
licNo: license ? String(license).trim() : undefined,
// === VehicleServInfo (attributes on the element) ===
customerNo: custNoStr, // fallback (some builds read this)
stockId: job.ro_number || undefined, // Use RO as stock# — common pattern
licNo: sanitizeLength(license ? String(license) : undefined, 20),
customerNo: custNoStr,
stockId: sanitizeLength(job?.ro_number, 20), // RO as stock#, truncated for safety
vehicleServInfo: {
customerNo: custNoStr, // REQUIRED — this is what toServiceVehicleView() validates
// Optional but increases success rate
salesmanNo: undefined, // You don't have advisor yet — omit
salesmanNo: undefined,
inServiceDate: undefined,
// Optional — safe to include if you want
productionDate: undefined,
modelMaintCode: undefined,
teamCode: undefined,
// Extended warranty — omit unless you sell contracts
vehExtWarranty: undefined,
// Advisor — omit unless you know who the service advisor is
advisor: undefined
}
};

View File

@@ -67,6 +67,34 @@ const redisSocketEvents = ({ io, redisHelpers, ioHelpers, logger }) => {
// Register Socket Events
const registerSocketEvents = (socket) => {
// DMS reset events (clear per-socket DMS transactional cache)
const registerDmsResetEvents = (socket) => {
socket.on("dms-reset-context", async ({ jobId, mode } = {}, ack) => {
try {
// This clears all transactional session data for this socket
// (RR txEnvelope/JobData/SelectedCustomer/PendingRO/etc, Fortellis, etc.)
await clearSessionTransactionData(socket.id);
createLogEvent(
socket,
"debug",
`DMS reset-context: cleared transactional session data` +
(jobId ? ` (jobId=${jobId})` : "") +
(mode ? ` (mode=${mode})` : "")
);
if (typeof ack === "function") {
ack({ ok: true });
}
} catch (error) {
createLogEvent(socket, "error", `DMS reset-context failed: ${error.message}`);
if (typeof ack === "function") {
ack({ ok: false, error: error.message });
}
}
});
};
// Token Update Events
const registerUpdateEvents = (socket) => {
let latestTokenTimestamp = 0;
@@ -164,7 +192,9 @@ const redisSocketEvents = ({ io, redisHelpers, ioHelpers, logger }) => {
// Optional: clear transactional session
try {
await clearSessionTransactionData(socket.id);
} catch {}
} catch {
//
}
// Leave all rooms except the default room (socket.id)
const rooms = Array.from(socket.rooms).filter((room) => room !== socket.id);
for (const room of rooms) {
@@ -363,6 +393,7 @@ const redisSocketEvents = ({ io, redisHelpers, ioHelpers, logger }) => {
};
// Call Handlers
registerDmsResetEvents(socket);
registerRoomAndBroadcastEvents(socket);
registerUpdateEvents(socket);
registerMessagingEvents(socket);

View File

@@ -115,6 +115,33 @@ function SetLegacyWebsocketHandlers(io) {
socket.on("disconnect", () => {
createLogEvent(socket, "DEBUG", `User disconnected.`);
});
// DMS reset for legacy WS (CDK / PBS)
socket.on("dms-reset-context", ({ jobId, mode } = {}, ack) => {
try {
// Clear any per-socket DMS state that can leak across jobs
socket.selectedCustomerId = null; // CDK / PBS AR
socket.txEnvelope = null; // PBS AP export
socket.apAllocations = null; // PBS AP allocations
createLogEvent(
socket,
"DEBUG",
`DMS reset-context (legacy WS): cleared per-socket state` +
(jobId ? ` (jobId=${jobId})` : "") +
(mode ? ` (mode=${mode})` : "")
);
if (typeof ack === "function") {
ack({ ok: true });
}
} catch (error) {
createLogEvent(socket, "ERROR", `DMS reset-context (legacy WS) failed: ${error.message}`);
if (typeof ack === "function") {
ack({ ok: false, error: error.message });
}
}
});
});
}