feature/IO-3357-Reynolds-and-Reynolds-DMS-API-Integration / RRScratch2 / Checkpoint
This commit is contained in:
@@ -0,0 +1,521 @@
|
||||
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 Dinero from "dinero.js";
|
||||
|
||||
import { selectBodyshop } from "../../redux/user/user.selectors";
|
||||
|
||||
const mapStateToProps = createStructuredSelector({
|
||||
bodyshop: selectBodyshop
|
||||
});
|
||||
const mapDispatchToProps = () => ({});
|
||||
|
||||
export default connect(mapStateToProps, mapDispatchToProps)(RrAllocationsSummary);
|
||||
|
||||
function normalizeDineroJson(d) {
|
||||
if (!d) return null;
|
||||
|
||||
// If it's already a Dinero instance, leave it to the caller
|
||||
if (typeof d.toUnit === "function") return d;
|
||||
|
||||
// New server shape: { cents: 54144, currency?: "USD" }
|
||||
if (typeof d.cents === "number") {
|
||||
return {
|
||||
amount: d.cents,
|
||||
precision: 2,
|
||||
currency: d.currency || "USD"
|
||||
};
|
||||
}
|
||||
|
||||
// Classic Dinero JSON: { amount, precision, currency }
|
||||
if (typeof d.amount === "number") {
|
||||
return {
|
||||
amount: d.amount,
|
||||
precision: d.precision ?? 2,
|
||||
currency: d.currency || "USD"
|
||||
};
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a Dinero-like object into an "N2" string ("123.45").
|
||||
* Works with real Dinero instances or plain JSON objects
|
||||
* that have { amount, precision }.
|
||||
*/
|
||||
function dineroToN2(dineroLike) {
|
||||
if (!dineroLike) return "0.00";
|
||||
|
||||
// If it's an actual Dinero instance
|
||||
if (typeof dineroLike.toUnit === "function") {
|
||||
return dineroLike.toUnit().toFixed(2);
|
||||
}
|
||||
|
||||
const normalized = normalizeDineroJson(dineroLike);
|
||||
if (!normalized) return "0.00";
|
||||
|
||||
const { amount, precision = 2 } = normalized;
|
||||
const value = amount / Math.pow(10, precision);
|
||||
return value.toFixed(2);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a minimal ROGOG preview from job allocation rows.
|
||||
* Mirrors the backend buildRogogFromAllocations logic:
|
||||
* - parts+extras segment
|
||||
* - taxable labor segment
|
||||
* - non-taxable labor segment
|
||||
* Each segment becomes its *own* JobNo with a single GOG line.
|
||||
*/
|
||||
function buildRogogPreviewFromJobRows(jobRows, opCode) {
|
||||
if (!Array.isArray(jobRows) || !jobRows.length || !opCode) return null;
|
||||
|
||||
const ops = [];
|
||||
|
||||
const toDinero = (d) => {
|
||||
if (!d) return Dinero();
|
||||
if (typeof d.toUnit === "function") return d;
|
||||
|
||||
const normalized = normalizeDineroJson(d);
|
||||
return normalized ? Dinero(normalized) : Dinero();
|
||||
};
|
||||
|
||||
const cents = (d) => toDinero(d).getAmount();
|
||||
|
||||
const segmentLabelMap = {
|
||||
partsExtras: "Parts/Extras",
|
||||
laborTaxable: "Taxable Labor",
|
||||
laborNonTaxable: "Non-Taxable Labor"
|
||||
};
|
||||
|
||||
for (const row of jobRows) {
|
||||
const pc = row.profitCenter || {};
|
||||
const breakOut = pc.rr_gogcode;
|
||||
const itemType = pc.rr_item_type;
|
||||
|
||||
// Only centers configured for RR GOG should appear
|
||||
if (!breakOut || !itemType) continue;
|
||||
|
||||
// Bucketed sales (Dinero-like objects coming from the backend)
|
||||
const partsSale = toDinero(row.partsSale);
|
||||
const extrasSale = toDinero(row.extrasSale);
|
||||
const laborTaxableSale = toDinero(row.laborTaxableSale);
|
||||
const laborNonTaxableSale = toDinero(row.laborNonTaxableSale);
|
||||
const costMoney = toDinero(row.cost);
|
||||
|
||||
const partsExtrasSale = partsSale.add(extrasSale);
|
||||
|
||||
const segments = [];
|
||||
|
||||
// 1) Parts + extras segment (uses center's default tax flag)
|
||||
if (!partsExtrasSale.isZero()) {
|
||||
segments.push({
|
||||
kind: "partsExtras",
|
||||
sale: partsExtrasSale,
|
||||
txFlag: pc.rr_cust_txbl_flag || "T"
|
||||
});
|
||||
}
|
||||
|
||||
// 2) Taxable labor -> always "T"
|
||||
if (!laborTaxableSale.isZero()) {
|
||||
segments.push({
|
||||
kind: "laborTaxable",
|
||||
sale: laborTaxableSale,
|
||||
txFlag: "T"
|
||||
});
|
||||
}
|
||||
|
||||
// 3) Non-taxable labor -> always "N"
|
||||
if (!laborNonTaxableSale.isZero()) {
|
||||
segments.push({
|
||||
kind: "laborNonTaxable",
|
||||
sale: laborNonTaxableSale,
|
||||
txFlag: "N"
|
||||
});
|
||||
}
|
||||
|
||||
if (!segments.length) continue;
|
||||
|
||||
// Proportionally split cost across segments (same logic as backend)
|
||||
const totalCostCents = cents(costMoney);
|
||||
const totalSaleCents = segments.reduce((sum, seg) => sum + cents(seg.sale), 0);
|
||||
|
||||
let remainingCostCents = totalCostCents;
|
||||
|
||||
segments.forEach((seg, idx) => {
|
||||
let costCents = 0;
|
||||
|
||||
if (totalCostCents > 0 && totalSaleCents > 0) {
|
||||
if (idx === segments.length - 1) {
|
||||
// Last segment gets the remainder to avoid rounding drift
|
||||
costCents = remainingCostCents;
|
||||
} else {
|
||||
const segSaleCents = cents(seg.sale);
|
||||
costCents = Math.round((segSaleCents / totalSaleCents) * totalCostCents);
|
||||
remainingCostCents -= costCents;
|
||||
}
|
||||
}
|
||||
|
||||
seg.costCents = costCents;
|
||||
});
|
||||
|
||||
const itemDescBase = pc.accountdesc || pc.accountname || row.center || "";
|
||||
|
||||
// 🔑 Each segment becomes its own JobNo with a single GOG line
|
||||
segments.forEach((seg, segIndex) => {
|
||||
const jobNo = String(ops.length + 1); // 1-based, global across all centers
|
||||
const segmentCount = segments.length;
|
||||
const segmentKind = seg.kind;
|
||||
const segmentLabel = segmentLabelMap[segmentKind] || segmentKind;
|
||||
|
||||
// If there is a split, annotate the description so it’s obvious which segment this is
|
||||
const displayDesc = segmentCount > 1 ? `${itemDescBase} (${segmentLabel})` : itemDescBase;
|
||||
|
||||
const saleN2 = dineroToN2(seg.sale);
|
||||
const costN2 = dineroToN2({
|
||||
amount: seg.costCents || 0,
|
||||
precision: 2
|
||||
});
|
||||
|
||||
ops.push({
|
||||
opCode,
|
||||
jobNo,
|
||||
segmentKind,
|
||||
segmentIndex: segIndex,
|
||||
segmentCount,
|
||||
lines: [
|
||||
{
|
||||
breakOut,
|
||||
itemType,
|
||||
itemDesc: displayDesc,
|
||||
custQty: "1.0",
|
||||
custPayTypeFlag: "C",
|
||||
// canonical property name used on the server
|
||||
custTxblNTxblFlag: seg.txFlag || "T",
|
||||
// legacy alias used by the table
|
||||
custTxblNtxblFlag: seg.txFlag || "T",
|
||||
amount: {
|
||||
payType: "Cust",
|
||||
amtType: "Unit",
|
||||
custPrice: saleN2,
|
||||
dlrCost: costN2
|
||||
}
|
||||
}
|
||||
]
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
if (!ops.length) return null;
|
||||
|
||||
return {
|
||||
roNo: null, // preview only
|
||||
ops
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a minimal ROLABOR preview from a ROGOG preview.
|
||||
* Mirrors server-side buildRolaborFromRogog.
|
||||
*/
|
||||
function buildRolaborPreviewFromRogog(rogg) {
|
||||
if (!rogg || !Array.isArray(rogg.ops)) return null;
|
||||
|
||||
const ops = rogg.ops.map((op) => {
|
||||
const firstLine = op.lines?.[0] || {};
|
||||
|
||||
// Prefer the server-side property name, but fall back to legacy
|
||||
const txFlag = firstLine.custTxblNTxblFlag ?? firstLine.custTxblNtxblFlag ?? "N";
|
||||
const payFlag = firstLine.custPayTypeFlag || "C";
|
||||
|
||||
return {
|
||||
opCode: op.opCode,
|
||||
jobNo: op.jobNo,
|
||||
custPayTypeFlag: payFlag,
|
||||
// this is what the table uses
|
||||
custTxblNtxblFlag: txFlag,
|
||||
bill: {
|
||||
payType: "Cust",
|
||||
jobTotalHrs: "0",
|
||||
billTime: "0",
|
||||
billRate: "0"
|
||||
},
|
||||
amount: {
|
||||
payType: "Cust",
|
||||
amtType: "Job",
|
||||
custPrice: "0",
|
||||
totalAmt: "0"
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
if (!ops.length) return null;
|
||||
return { ops };
|
||||
}
|
||||
|
||||
/**
|
||||
* RR-specific DMS Allocations Summary
|
||||
* Focused on what we actually send to RR:
|
||||
* - ROGOG (split by taxable / non-taxable segments)
|
||||
* - ROLABOR shell
|
||||
*/
|
||||
export function RrAllocationsSummary({ socket, bodyshop, jobId, title }) {
|
||||
const { t } = useTranslation();
|
||||
const [jobRows, setJobRows] = useState([]);
|
||||
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) {
|
||||
setJobRows([]);
|
||||
setError(ack.error || t("dms.labels.allocations_error"));
|
||||
if (socket) {
|
||||
socket.allocationsSummary = [];
|
||||
socket.rrAllocationsRaw = ack;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const jobAllocRows = normalizeJobAllocations(ack);
|
||||
|
||||
setJobRows(jobAllocRows);
|
||||
setError(null);
|
||||
|
||||
if (socket) {
|
||||
socket.allocationsSummary = jobAllocRows;
|
||||
socket.rrAllocationsRaw = ack;
|
||||
}
|
||||
});
|
||||
} catch {
|
||||
setJobRows([]);
|
||||
setError(t("dms.labels.allocations_error"));
|
||||
if (socket) {
|
||||
socket.allocationsSummary = [];
|
||||
}
|
||||
}
|
||||
}, [socket, jobId, t]);
|
||||
|
||||
useEffect(() => {
|
||||
fetchAllocations();
|
||||
}, [fetchAllocations]);
|
||||
|
||||
const opCode = bodyshop?.rr_configuration?.baseOpCode || "28TOZ";
|
||||
|
||||
const roggPreview = useMemo(() => buildRogogPreviewFromJobRows(jobRows, opCode), [jobRows, opCode]);
|
||||
const rolaborPreview = useMemo(() => buildRolaborPreviewFromRogog(roggPreview), [roggPreview]);
|
||||
|
||||
const roggRows = useMemo(() => {
|
||||
if (!roggPreview || !Array.isArray(roggPreview.ops)) return [];
|
||||
const rows = [];
|
||||
roggPreview.ops.forEach((op) => {
|
||||
(op.lines || []).forEach((line, idx) => {
|
||||
rows.push({
|
||||
key: `${op.jobNo}-${idx}`,
|
||||
opCode: op.opCode,
|
||||
jobNo: op.jobNo,
|
||||
breakOut: line.breakOut,
|
||||
itemType: line.itemType,
|
||||
itemDesc: line.itemDesc,
|
||||
custQty: line.custQty,
|
||||
custPayTypeFlag: line.custPayTypeFlag,
|
||||
custTxblNtxblFlag: line.custTxblNtxblFlag,
|
||||
custPrice: line.amount?.custPrice,
|
||||
dlrCost: line.amount?.dlrCost,
|
||||
// segment metadata for visual styling
|
||||
segmentKind: op.segmentKind,
|
||||
segmentCount: op.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 & 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={() => (
|
||||
<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}>{roggTotals.totalCustPrice}</Table.Summary.Cell>
|
||||
<Table.Summary.Cell index={8}>{roggTotals.totalDlrCost}</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>
|
||||
);
|
||||
}
|
||||
@@ -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} />;
|
||||
|
||||
@@ -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
|
||||
@@ -82,6 +83,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,7 +136,7 @@ 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"
|
||||
@@ -148,10 +150,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 +189,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]);
|
||||
|
||||
@@ -218,7 +220,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 +237,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 +305,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 +318,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)) {
|
||||
@@ -359,19 +353,38 @@ export function DmsContainer({ bodyshop, setBreadcrumbs, setSelectedHeader, inse
|
||||
|
||||
<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}
|
||||
/>
|
||||
{!isRrMode ? (
|
||||
<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}
|
||||
/>
|
||||
) : (
|
||||
<RrAllocationsSummary
|
||||
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}>
|
||||
@@ -397,13 +410,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}
|
||||
@@ -425,8 +443,10 @@ export function DmsContainer({ bodyshop, setBreadcrumbs, setSelectedHeader, inse
|
||||
if (isWssMode(mode)) {
|
||||
setActiveLogLevel(logLevel);
|
||||
}
|
||||
activeSocket.disconnect();
|
||||
activeSocket.connect();
|
||||
if (activeSocket) {
|
||||
activeSocket.disconnect();
|
||||
activeSocket.connect();
|
||||
}
|
||||
}}
|
||||
>
|
||||
Reconnect
|
||||
@@ -436,9 +456,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>
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
// server/rr/rr-calculate-allocations.js
|
||||
|
||||
const { GraphQLClient } = require("graphql-request");
|
||||
const Dinero = require("dinero.js");
|
||||
const _ = require("lodash");
|
||||
@@ -12,19 +14,16 @@ const { DiscountNotAlreadyCounted } = InstanceManager({
|
||||
});
|
||||
|
||||
/**
|
||||
* Dinero helpers for safe, compact logging.
|
||||
* ============================
|
||||
* Helpers / Summarizers
|
||||
* ============================
|
||||
*/
|
||||
|
||||
const summarizeMoney = (dinero) => {
|
||||
if (!dinero || typeof dinero.getAmount !== "function") return { cents: null };
|
||||
return { cents: dinero.getAmount() };
|
||||
};
|
||||
|
||||
const summarizeHash = (hash) =>
|
||||
Object.entries(hash || {}).map(([center, dinero]) => ({
|
||||
center,
|
||||
...summarizeMoney(dinero)
|
||||
}));
|
||||
|
||||
const summarizeTaxAllocations = (tax) =>
|
||||
Object.entries(tax || {}).map(([key, entry]) => ({
|
||||
key,
|
||||
@@ -36,18 +35,37 @@ const summarizeAllocationsArray = (arr) =>
|
||||
(arr || []).map((a) => ({
|
||||
center: a.center || a.tax || null,
|
||||
tax: a.tax || null,
|
||||
sale: summarizeMoney(a.sale),
|
||||
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 = `[CdkCalculateAllocations] ${msg}`;
|
||||
|
||||
const baseMsg = "rr-calculate-allocations " + msg;
|
||||
CreateRRLogEvent(connectionData, level, baseMsg, meta !== undefined ? meta : undefined);
|
||||
};
|
||||
}
|
||||
@@ -66,6 +84,7 @@ async function QueryJobData(connectionData, token, jobid) {
|
||||
|
||||
/**
|
||||
* 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({
|
||||
@@ -128,8 +147,16 @@ function buildTaxAllocations(bodyshop, job) {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
@@ -160,16 +187,16 @@ function buildProfitCenterHash(job, debugLog) {
|
||||
|
||||
// Parts
|
||||
if (val.profitcenter_part) {
|
||||
if (!acc[val.profitcenter_part]) acc[val.profitcenter_part] = Dinero();
|
||||
const bucket = ensureCenterBucket(acc, val.profitcenter_part);
|
||||
|
||||
let dineroAmount = Dinero({
|
||||
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 moneyDiscount = val.prt_dsmk_m
|
||||
const discount = val.prt_dsmk_m
|
||||
? Dinero({ amount: Math.round(val.prt_dsmk_m * 100) })
|
||||
: Dinero({
|
||||
amount: Math.round(val.act_price * 100)
|
||||
@@ -178,24 +205,28 @@ function buildProfitCenterHash(job, debugLog) {
|
||||
.percentage(Math.abs(val.prt_dsmk_p || 0))
|
||||
.multiply(val.prt_dsmk_p > 0 ? 1 : -1);
|
||||
|
||||
dineroAmount = dineroAmount.add(moneyDiscount);
|
||||
amount = amount.add(discount);
|
||||
}
|
||||
|
||||
acc[val.profitcenter_part] = acc[val.profitcenter_part].add(dineroAmount);
|
||||
bucket.partsSale = bucket.partsSale.add(amount);
|
||||
}
|
||||
|
||||
// Labor
|
||||
if (val.profitcenter_labor && val.mod_lbr_ty) {
|
||||
if (!acc[val.profitcenter_labor]) acc[val.profitcenter_labor] = Dinero();
|
||||
const bucket = ensureCenterBucket(acc, val.profitcenter_labor);
|
||||
|
||||
const rateKey = `rate_${val.mod_lbr_ty.toLowerCase()}`;
|
||||
const rate = job[rateKey];
|
||||
|
||||
acc[val.profitcenter_labor] = acc[val.profitcenter_labor].add(
|
||||
Dinero({
|
||||
amount: Math.round(rate * 100)
|
||||
}).multiply(val.mod_lb_hrs)
|
||||
);
|
||||
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;
|
||||
@@ -204,7 +235,13 @@ function buildProfitCenterHash(job, debugLog) {
|
||||
debugLog("profitCenterHash after joblines", {
|
||||
hasMapaLine,
|
||||
hasMashLine,
|
||||
centers: summarizeHash(profitCenterHash)
|
||||
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 };
|
||||
@@ -240,7 +277,10 @@ function buildCostCenterHash(job, selectedDmsAllocationConfig, disablebillwip, d
|
||||
}
|
||||
|
||||
debugLog("costCenterHash after bills (pre-timetickets)", {
|
||||
centers: summarizeHash(costCenterHash)
|
||||
centers: Object.entries(costCenterHash || {}).map(([center, dinero]) => ({
|
||||
center,
|
||||
...summarizeMoney(dinero)
|
||||
}))
|
||||
});
|
||||
|
||||
// 2) Timetickets -> costs
|
||||
@@ -260,14 +300,17 @@ function buildCostCenterHash(job, selectedDmsAllocationConfig, disablebillwip, d
|
||||
});
|
||||
|
||||
debugLog("costCenterHash after timetickets", {
|
||||
centers: summarizeHash(costCenterHash)
|
||||
centers: Object.entries(costCenterHash || {}).map(([center, dinero]) => ({
|
||||
center,
|
||||
...summarizeMoney(dinero)
|
||||
}))
|
||||
});
|
||||
|
||||
return costCenterHash;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add manual MAPA / MASH sales where needed.
|
||||
* Add manual MAPA / MASH sales where needed (into extrasSale bucket).
|
||||
*/
|
||||
function applyMapaMashManualLines({
|
||||
job,
|
||||
@@ -289,10 +332,8 @@ function applyMapaMashManualLines({
|
||||
amount: summarizeMoney(Dinero(job.job_totals.rates.mapa.total))
|
||||
});
|
||||
|
||||
if (!profitCenterHash[mapaAccountName]) profitCenterHash[mapaAccountName] = Dinero();
|
||||
profitCenterHash[mapaAccountName] = profitCenterHash[mapaAccountName].add(
|
||||
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 });
|
||||
}
|
||||
@@ -309,10 +350,8 @@ function applyMapaMashManualLines({
|
||||
amount: summarizeMoney(Dinero(job.job_totals.rates.mash.total))
|
||||
});
|
||||
|
||||
if (!profitCenterHash[mashAccountName]) profitCenterHash[mashAccountName] = Dinero();
|
||||
profitCenterHash[mashAccountName] = profitCenterHash[mashAccountName].add(
|
||||
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 });
|
||||
}
|
||||
@@ -345,7 +384,7 @@ function applyMaterialsCosting({ job, bodyshop, selectedDmsAllocationConfig, cos
|
||||
if (!costCenterHash[mapaAccountName]) costCenterHash[mapaAccountName] = Dinero();
|
||||
|
||||
if (job.bodyshop.use_paint_scale_data === true) {
|
||||
if (job.mixdata.length > 0) {
|
||||
if (job.mixdata && job.mixdata.length > 0) {
|
||||
debugLog("Using mixdata for MAPA cost", {
|
||||
mapaAccountName,
|
||||
totalliquidcost: job.mixdata[0] && job.mixdata[0].totalliquidcost
|
||||
@@ -394,6 +433,7 @@ function applyMaterialsCosting({ job, bodyshop, selectedDmsAllocationConfig, cos
|
||||
|
||||
/**
|
||||
* 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;
|
||||
@@ -416,9 +456,8 @@ function applyExtras({ job, bodyshop, selectedDmsAllocationConfig, profitCenterH
|
||||
towing_payable: job.towing_payable
|
||||
});
|
||||
|
||||
if (!profitCenterHash[towAccountName]) profitCenterHash[towAccountName] = Dinero();
|
||||
|
||||
profitCenterHash[towAccountName] = profitCenterHash[towAccountName].add(
|
||||
const bucket = ensureCenterBucket(profitCenterHash, towAccountName);
|
||||
bucket.extrasSale = bucket.extrasSale.add(
|
||||
Dinero({
|
||||
amount: Math.round((job.towing_payable || 0) * 100)
|
||||
})
|
||||
@@ -439,9 +478,8 @@ function applyExtras({ job, bodyshop, selectedDmsAllocationConfig, profitCenterH
|
||||
storage_payable: job.storage_payable
|
||||
});
|
||||
|
||||
if (!profitCenterHash[storageAccountName]) profitCenterHash[storageAccountName] = Dinero();
|
||||
|
||||
profitCenterHash[storageAccountName] = profitCenterHash[storageAccountName].add(
|
||||
const bucket = ensureCenterBucket(profitCenterHash, storageAccountName);
|
||||
bucket.extrasSale = bucket.extrasSale.add(
|
||||
Dinero({
|
||||
amount: Math.round((job.storage_payable || 0) * 100)
|
||||
})
|
||||
@@ -462,9 +500,8 @@ function applyExtras({ job, bodyshop, selectedDmsAllocationConfig, profitCenterH
|
||||
adjustment_bottom_line: job.adjustment_bottom_line
|
||||
});
|
||||
|
||||
if (!profitCenterHash[otherAccountName]) profitCenterHash[otherAccountName] = Dinero();
|
||||
|
||||
profitCenterHash[otherAccountName] = profitCenterHash[otherAccountName].add(
|
||||
const bucket = ensureCenterBucket(profitCenterHash, otherAccountName);
|
||||
bucket.extrasSale = bucket.extrasSale.add(
|
||||
Dinero({
|
||||
amount: Math.round((job.adjustment_bottom_line || 0) * 100)
|
||||
})
|
||||
@@ -479,6 +516,15 @@ function applyExtras({ job, bodyshop, selectedDmsAllocationConfig, profitCenterH
|
||||
|
||||
/**
|
||||
* 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,
|
||||
@@ -488,33 +534,43 @@ function applyRomeProfileAdjustments({
|
||||
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(job.job_totals.parts.adjustments || {}),
|
||||
rateKeys: Object.keys(job.job_totals.rates || {})
|
||||
partAdjustmentKeys: Object.keys(partsAdjustments),
|
||||
rateKeys: Object.keys(rateMap)
|
||||
});
|
||||
|
||||
// Parts adjustments
|
||||
Object.keys(job.job_totals.parts.adjustments).forEach((key) => {
|
||||
Object.keys(partsAdjustments).forEach((key) => {
|
||||
const accountName = selectedDmsAllocationConfig.profits[key];
|
||||
const otherAccount = bodyshop.md_responsibility_centers.profits.find((c) => c.name === accountName);
|
||||
|
||||
if (otherAccount) {
|
||||
if (!profitCenterHash[accountName]) profitCenterHash[accountName] = Dinero();
|
||||
const bucket = ensureCenterBucket(profitCenterHash, accountName);
|
||||
|
||||
profitCenterHash[accountName] = profitCenterHash[accountName].add(Dinero(job.job_totals.parts.adjustments[key]));
|
||||
const adjMoney = Dinero(partsAdjustments[key]);
|
||||
bucket.extrasSale = bucket.extrasSale.add(adjMoney);
|
||||
|
||||
debugLog("Added parts adjustment", {
|
||||
key,
|
||||
accountName,
|
||||
adjustment: summarizeMoney(Dinero(job.job_totals.parts.adjustments[key]))
|
||||
adjustment: summarizeMoney(adjMoney)
|
||||
});
|
||||
} else {
|
||||
CreateRRLogEvent(
|
||||
connectionData,
|
||||
"ERROR",
|
||||
"Error encountered in CdkCalculateAllocations. Unable to find parts adjustment account.",
|
||||
"Error encountered in rr-calculate-allocations. Unable to find parts adjustment account.",
|
||||
{ accountName, key }
|
||||
);
|
||||
debugLog("Missing parts adjustment account", { key, accountName });
|
||||
@@ -522,26 +578,30 @@ function applyRomeProfileAdjustments({
|
||||
});
|
||||
|
||||
// Labor / materials adjustments
|
||||
Object.keys(job.job_totals.rates).forEach((key) => {
|
||||
const rate = job.job_totals.rates[key];
|
||||
Object.keys(rateMap).forEach((key) => {
|
||||
const rate = rateMap[key];
|
||||
if (!rate || !rate.adjustment) return;
|
||||
|
||||
if (Dinero(rate.adjustment).isZero()) return;
|
||||
const adjMoney = Dinero(rate.adjustment);
|
||||
if (adjMoney.isZero()) return;
|
||||
|
||||
const accountName = selectedDmsAllocationConfig.profits[key.toUpperCase()];
|
||||
const otherAccount = bodyshop.md_responsibility_centers.profits.find((c) => c.name === accountName);
|
||||
|
||||
if (otherAccount) {
|
||||
if (!profitCenterHash[accountName]) profitCenterHash[accountName] = Dinero();
|
||||
const bucket = ensureCenterBucket(profitCenterHash, accountName);
|
||||
bucket.extrasSale = bucket.extrasSale.add(adjMoney);
|
||||
|
||||
profitCenterHash[accountName] = profitCenterHash[accountName].add(Dinero(job.job_totals.rates[key].adjustments));
|
||||
|
||||
debugLog("Added rate adjustment", { key, accountName });
|
||||
debugLog("Added rate adjustment", {
|
||||
key,
|
||||
accountName,
|
||||
adjustment: summarizeMoney(adjMoney)
|
||||
});
|
||||
} else {
|
||||
CreateRRLogEvent(
|
||||
connectionData,
|
||||
"ERROR",
|
||||
"Error encountered in CdkCalculateAllocations. Unable to find rate adjustment account.",
|
||||
"Error encountered in rr-calculate-allocations. Unable to find rate adjustment account.",
|
||||
{ accountName, key }
|
||||
);
|
||||
debugLog("Missing rate adjustment account", { key, accountName });
|
||||
@@ -553,30 +613,68 @@ function applyRomeProfileAdjustments({
|
||||
|
||||
/**
|
||||
* 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((key) => {
|
||||
const profitCenter = bodyshop.md_responsibility_centers.profits.find((c) => c.name === key);
|
||||
const costCenter = bodyshop.md_responsibility_centers.costs.find((c) => c.name === key);
|
||||
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: key,
|
||||
sale: profitCenterHash[key] || Dinero(),
|
||||
cost: costCenterHash[key] || Dinero(),
|
||||
center,
|
||||
|
||||
partsSale: bucket.partsSale,
|
||||
laborTaxableSale: bucket.laborTaxableSale,
|
||||
laborNonTaxableSale: bucket.laborNonTaxableSale,
|
||||
extrasSale: bucket.extrasSale,
|
||||
totalSale,
|
||||
|
||||
cost: costCenterHash[center] || Dinero(),
|
||||
|
||||
profitCenter,
|
||||
costCenter
|
||||
};
|
||||
});
|
||||
|
||||
debugLog("jobAllocations built", summarizeAllocationsArray(jobAllocations));
|
||||
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)
|
||||
@@ -650,7 +748,15 @@ function buildAdjustmentAllocations(job, bodyshop, debugLog) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Core allocation calculation – Reynolds-only, Reynolds-logging only.
|
||||
* 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;
|
||||
@@ -728,10 +834,19 @@ function calculateAllocations(connectionData, job) {
|
||||
});
|
||||
|
||||
debugLog("profitCenterHash before jobAllocations build", {
|
||||
centers: summarizeHash(profitCenterHash)
|
||||
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: summarizeHash(costCenterHash)
|
||||
centers: Object.entries(costCenterHash || {}).map(([center, dinero]) => ({
|
||||
center,
|
||||
...summarizeMoney(dinero)
|
||||
}))
|
||||
});
|
||||
|
||||
// 9) Build job-level allocations & tax allocations
|
||||
@@ -739,20 +854,27 @@ function calculateAllocations(connectionData, job) {
|
||||
const taxAllocArray = buildTaxAllocArray(taxAllocations, selectedDmsAllocationConfig, debugLog);
|
||||
const { ttlAdjArray, ttlTaxAdjArray } = buildAdjustmentAllocations(job, bodyshop, debugLog);
|
||||
|
||||
// 10) Final combined array
|
||||
const allocations = [...jobAllocations, ...taxAllocArray, ...ttlAdjArray, ...ttlTaxAdjArray];
|
||||
const result = {
|
||||
jobAllocations,
|
||||
taxAllocArray,
|
||||
ttlAdjArray,
|
||||
ttlTaxAdjArray
|
||||
};
|
||||
|
||||
debugLog("FINAL allocations summary", {
|
||||
count: allocations.length,
|
||||
allocations: summarizeAllocationsArray(allocations)
|
||||
jobAllocationsCount: jobAllocations.length,
|
||||
taxAllocCount: taxAllocArray.length,
|
||||
ttlAdjCount: ttlAdjArray.length,
|
||||
ttlTaxAdjCount: ttlTaxAdjArray.length
|
||||
});
|
||||
debugLog("EXIT");
|
||||
|
||||
return allocations;
|
||||
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 {
|
||||
@@ -762,17 +884,19 @@ exports.defaultRoute = async function (req, res) {
|
||||
const data = calculateAllocations(req, jobData);
|
||||
return res.status(200).json({ data });
|
||||
} catch (error) {
|
||||
CreateRRLogEvent(req, "ERROR", "Error encountered in CdkCalculateAllocations.", {
|
||||
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 CdkCalculateAllocations. ${error}` });
|
||||
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 {
|
||||
@@ -780,7 +904,7 @@ exports.default = async function (socket, jobid) {
|
||||
const jobData = await QueryJobData(socket, token, jobid);
|
||||
return calculateAllocations(socket, jobData);
|
||||
} catch (error) {
|
||||
CreateRRLogEvent(socket, "ERROR", "Error encountered in CdkCalculateAllocations.", {
|
||||
CreateRRLogEvent(socket, "ERROR", "Error encountered in rr-calculate-allocations.", {
|
||||
message: error?.message || String(error),
|
||||
stack: error?.stack
|
||||
});
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -54,7 +54,33 @@ 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.
|
||||
*
|
||||
* @param {Array} allocations
|
||||
* @param {Object} opts
|
||||
* @param {string} opts.opCode - RR OpCode for the job (global, overridable)
|
||||
@@ -67,45 +93,125 @@ const buildRogogFromAllocations = (allocations, { opCode, payType = "Cust", roNo
|
||||
|
||||
const ops = [];
|
||||
|
||||
const cents = (money) => {
|
||||
if (!money) return 0;
|
||||
if (typeof money.getAmount === "function") return money.getAmount();
|
||||
if (typeof money === "object" && typeof money.amount === "number") return money.amount;
|
||||
return 0;
|
||||
};
|
||||
|
||||
const asMoneyLike = (amountCents) => ({
|
||||
amount: amountCents || 0,
|
||||
precision: 2
|
||||
});
|
||||
|
||||
const addMoney = (...ms) => {
|
||||
let acc = null;
|
||||
for (const m of ms) {
|
||||
if (!m) continue;
|
||||
if (!acc) acc = m;
|
||||
else if (typeof acc.add === "function") acc = acc.add(m);
|
||||
}
|
||||
return acc;
|
||||
};
|
||||
|
||||
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 partsSale = alloc.partsSale || null;
|
||||
const extrasSale = alloc.extrasSale || null;
|
||||
const laborTaxableSale = alloc.laborTaxableSale || null;
|
||||
const laborNonTaxableSale = alloc.laborNonTaxableSale || null;
|
||||
const costMoney = alloc.cost || null;
|
||||
|
||||
const itemDesc = pc.accountdesc || pc.accountname || alloc.center || "";
|
||||
const jobNo = String(ops.length + 1); // 1-based JobNo
|
||||
// Parts + extras share a single segment
|
||||
const partsExtrasSale = addMoney(partsSale, extrasSale);
|
||||
|
||||
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 (partsExtrasSale && typeof partsExtrasSale.isZero === "function" && !partsExtrasSale.isZero()) {
|
||||
segments.push({
|
||||
kind: "partsExtras",
|
||||
sale: partsExtrasSale,
|
||||
txFlag: pc.rr_cust_txbl_flag || "T"
|
||||
});
|
||||
}
|
||||
|
||||
// 2) Taxable labor segment -> "T"
|
||||
if (laborTaxableSale && typeof laborTaxableSale.isZero === "function" && !laborTaxableSale.isZero()) {
|
||||
segments.push({
|
||||
kind: "laborTaxable",
|
||||
sale: laborTaxableSale,
|
||||
txFlag: "T"
|
||||
});
|
||||
}
|
||||
|
||||
// 3) Non-taxable labor segment -> "N"
|
||||
if (laborNonTaxableSale && typeof laborNonTaxableSale.isZero === "function" && !laborNonTaxableSale.isZero()) {
|
||||
segments.push({
|
||||
kind: "laborNonTaxable",
|
||||
sale: laborNonTaxableSale,
|
||||
txFlag: "N"
|
||||
});
|
||||
}
|
||||
|
||||
if (!segments.length) continue;
|
||||
|
||||
// Proportionally split cost across segments based on their sale amounts
|
||||
const totalCostCents = cents(costMoney);
|
||||
const totalSaleCents = segments.reduce((sum, seg) => sum + cents(seg.sale), 0);
|
||||
|
||||
let remainingCostCents = totalCostCents;
|
||||
|
||||
segments.forEach((seg, idx) => {
|
||||
let costCents = 0;
|
||||
|
||||
if (totalCostCents > 0 && totalSaleCents > 0) {
|
||||
if (idx === segments.length - 1) {
|
||||
// Last segment gets the remainder to avoid rounding drift
|
||||
costCents = remainingCostCents;
|
||||
} else {
|
||||
const segSaleCents = cents(seg.sale);
|
||||
costCents = Math.round((segSaleCents / totalSaleCents) * totalCostCents);
|
||||
remainingCostCents -= costCents;
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
seg.costCents = costCents;
|
||||
});
|
||||
|
||||
const itemDescBase = pc.accountdesc || pc.accountname || alloc.center || "";
|
||||
|
||||
// NEW: each segment becomes its own op / JobNo with a single line
|
||||
segments.forEach((seg) => {
|
||||
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 || "T",
|
||||
amount: {
|
||||
payType,
|
||||
amtType: "Unit",
|
||||
custPrice: asN2(seg.sale),
|
||||
dlrCost: asN2(asMoneyLike(seg.costCents))
|
||||
}
|
||||
};
|
||||
|
||||
ops.push({
|
||||
opCode,
|
||||
jobNo,
|
||||
lines: [line] // exactly one AllGogLineItmInfo per AllGogOpCodeInfo
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -131,16 +237,19 @@ 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 ?? 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,
|
||||
// This is the property the Mustache template uses for <CustTxblNTxblFlag>
|
||||
custTxblNtxblFlag: txFlag,
|
||||
bill: {
|
||||
payType,
|
||||
jobTotalHrs: "0",
|
||||
@@ -277,24 +386,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 +491,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,
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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,41 +194,52 @@ 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
|
||||
});
|
||||
}
|
||||
|
||||
// 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 = {
|
||||
vin: vinStr.toUpperCase(), // "1FDWX34Y28EB01395"
|
||||
|
||||
// 2-character make code (from v_make_desc → known mapping)
|
||||
vehicleMake: deriveMakeCode(job.v_make_desc), // → "FR" for Ford
|
||||
vehicleMake: deriveMakeCode(job?.v_make_desc), // → "FR" for Ford
|
||||
year: job?.v_model_yr || undefined,
|
||||
// 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,
|
||||
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"
|
||||
carline: undefined,
|
||||
extClrDesc: sanitizeLength(job?.v_color, 30), // safe, configurable if vendor complains
|
||||
accentClr: undefined,
|
||||
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,
|
||||
licNo: sanitizeLength(license ? String(license) : undefined, 20),
|
||||
|
||||
customerNo: custNoStr,
|
||||
stockId: job.ro_number || undefined, // Use RO as stock# — common pattern
|
||||
stockId: sanitizeLength(job?.ro_number, 20), // RO as stock#, truncated for safety
|
||||
vehicleServInfo: {
|
||||
customerNo: custNoStr, // REQUIRED — this is what toServiceVehicleView() validates
|
||||
salesmanNo: undefined, // You don't have advisor yet — omit
|
||||
salesmanNo: undefined,
|
||||
inServiceDate: undefined,
|
||||
productionDate: undefined,
|
||||
modelMaintCode: undefined,
|
||||
|
||||
Reference in New Issue
Block a user