feature/IO-3357-Reynolds-and-Reynolds-DMS-API-Integration / RRScratch2 / Checkpoint

This commit is contained in:
Dave
2025-11-24 17:21:33 -05:00
parent b2184a2d11
commit ae7d150a6c
8 changed files with 1052 additions and 225 deletions

View File

@@ -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
});