543 lines
15 KiB
JavaScript
543 lines
15 KiB
JavaScript
const client = require("../graphql-client/graphql-client").client;
|
|
const { GET_JOB_BY_PK } = require("../graphql-client/queries");
|
|
|
|
/**
|
|
* Remove all non-digit characters from a string.
|
|
* @param s
|
|
* @returns {string}
|
|
*/
|
|
const digitsOnly = (s) => String(s || "").replace(/\D/g, "");
|
|
|
|
/**
|
|
* Pick job ID from various possible locations.
|
|
* @param ctx
|
|
* @param explicitId
|
|
* @returns {*|null}
|
|
*/
|
|
const pickJobId = (ctx, explicitId) =>
|
|
explicitId || ctx?.job?.id || ctx?.payload?.job?.id || ctx?.payload?.jobId || ctx?.jobId || null;
|
|
|
|
/**
|
|
* Safely get VIN from job object.
|
|
* @param job
|
|
* @returns {*|string|null}
|
|
*/
|
|
const safeVin = (job) => (job?.v_vin && String(job.v_vin).trim()) || null;
|
|
|
|
/**
|
|
* Extract blocks array from combined search result.
|
|
* @param res
|
|
* @returns {any[]|*[]}
|
|
*/
|
|
const blocksFromCombinedSearchResult = (res) => {
|
|
const data = Array.isArray(res?.data) ? res.data : Array.isArray(res) ? res : [];
|
|
return Array.isArray(data) ? data : [];
|
|
};
|
|
|
|
/**
|
|
* Convert a Dinero.js object or number into an "N2" string ("123.45").
|
|
* @param value
|
|
* @returns {string}
|
|
*/
|
|
const asN2 = (dineroLike) => {
|
|
if (!dineroLike) return "0.00";
|
|
|
|
// Handle Dinero v1/v2-ish or raw objects
|
|
if (typeof dineroLike.toUnit === "function") {
|
|
return dineroLike.toUnit().toFixed(2);
|
|
}
|
|
|
|
const precision = dineroLike.precision ?? 2;
|
|
const amount = (dineroLike.amount ?? 0) / Math.pow(10, precision);
|
|
return amount.toFixed(2);
|
|
};
|
|
|
|
/**
|
|
* Build RO.GOG structure for the reynolds-rome-client `createRepairOrder` payload
|
|
* 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)
|
|
* @param {string} [opts.payType="Cust"] - PayType (always "Cust" per Marc)
|
|
* @param {string} [opts.roNo] - Optional RoNo to echo on <Rogog RoNo="">
|
|
* @returns {null|{roNo?: string, ops: Array}}
|
|
*/
|
|
const buildRogogFromAllocations = (allocations, { opCode, payType = "Cust", roNo } = {}) => {
|
|
if (!Array.isArray(allocations) || !allocations.length || !opCode) return null;
|
|
|
|
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 configured for RR GOG are included
|
|
if (!breakOut || !itemType) continue;
|
|
|
|
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;
|
|
|
|
// Parts + extras share a single segment
|
|
const partsExtrasSale = addMoney(partsSale, extrasSale);
|
|
|
|
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
|
|
});
|
|
});
|
|
}
|
|
|
|
if (!ops.length) return null;
|
|
|
|
return {
|
|
roNo,
|
|
ops
|
|
};
|
|
};
|
|
|
|
/**
|
|
* Build RO.ROLABOR structure for the reynolds-rome-client `createRepairOrder` payload
|
|
* from an already-built RO.GOG structure.
|
|
* @param {Object} rogg - result of buildRogogFromAllocations
|
|
* @param {Object} opts
|
|
* @param {string} [opts.payType="Cust"]
|
|
* @returns {null|{ops: Array}}
|
|
*/
|
|
const buildRolaborFromRogog = (rogg, { payType = "Cust" } = {}) => {
|
|
if (!rogg || !Array.isArray(rogg.ops)) return null;
|
|
|
|
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: linePayType,
|
|
// This is the property the Mustache template uses for <CustTxblNTxblFlag>
|
|
custTxblNtxblFlag: txFlag,
|
|
bill: {
|
|
payType,
|
|
jobTotalHrs: "0",
|
|
billTime: "0",
|
|
billRate: "0"
|
|
},
|
|
amount: {
|
|
payType,
|
|
amtType: "Job",
|
|
custPrice: "0",
|
|
totalAmt: "0"
|
|
}
|
|
};
|
|
});
|
|
|
|
if (!ops.length) return null;
|
|
|
|
return { ops };
|
|
};
|
|
|
|
/**
|
|
* Query job data by ID from GraphQL API.
|
|
* @param ctx
|
|
* @param jobId
|
|
* @returns {Promise<*>}
|
|
* @constructor
|
|
*/
|
|
const QueryJobData = async (ctx = {}, jobId) => {
|
|
if (ctx?.job) return ctx.job;
|
|
if (ctx?.payload?.job) return ctx.payload.job;
|
|
|
|
const id = pickJobId(ctx, jobId);
|
|
if (!id) throw new Error("QueryJobData: jobId required (none found in ctx or args)");
|
|
|
|
try {
|
|
const res = await client.request(GET_JOB_BY_PK, { id });
|
|
const job = res?.jobs_by_pk;
|
|
if (!job) throw new Error(`Job ${id} not found`);
|
|
return job;
|
|
} catch (e) {
|
|
const msg = e?.response?.errors?.[0]?.message || e.message || "unknown";
|
|
throw new Error(`QueryJobData failed: ${msg}`);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Build Repair Order payload for RR from job and customer data.
|
|
* @param {Object} args
|
|
* @param job
|
|
* @param selectedCustomer
|
|
* @param advisorNo
|
|
* @param story
|
|
* @param makeOverride
|
|
* @param bodyshop
|
|
* @param allocations
|
|
* @param {string} [opCode] - RR OpCode for this RO (global default / override)
|
|
* @param {string} [taxCode] - RR tax code for header tax (e.g. state/prov code)
|
|
* @returns {Object}
|
|
*/
|
|
const buildRRRepairOrderPayload = ({
|
|
job,
|
|
selectedCustomer,
|
|
advisorNo,
|
|
story,
|
|
makeOverride,
|
|
allocations,
|
|
opCode
|
|
// taxCode
|
|
} = {}) => {
|
|
const customerNo = selectedCustomer?.customerNo
|
|
? String(selectedCustomer.customerNo).trim()
|
|
: selectedCustomer?.custNo
|
|
? String(selectedCustomer.custNo).trim()
|
|
: null;
|
|
|
|
if (!customerNo) throw new Error("No RR customer selected (customerNo/custNo missing)");
|
|
|
|
const adv = advisorNo != null && String(advisorNo).trim() !== "" ? String(advisorNo).trim() : null;
|
|
if (!adv) throw new Error("advisorNo is required for RR export");
|
|
|
|
const vinRaw = job?.v_vin;
|
|
const vin =
|
|
typeof vinRaw === "string"
|
|
? vinRaw
|
|
.replace(/[^A-Za-z0-9]/g, "")
|
|
.toUpperCase()
|
|
.slice(0, 17) || undefined
|
|
: undefined;
|
|
|
|
// Use ro_number when present; fallback to job.id
|
|
const ro = job?.ro_number != null ? job.ro_number : job?.id != null ? job.id : null;
|
|
if (ro == null) throw new Error("Missing repair order identifier (ro_number/id)");
|
|
|
|
const roStr = String(ro);
|
|
|
|
// Base payload shape expected by reynolds-rome-client (buildCreateRepairOrder)
|
|
const payload = {
|
|
outsdRoNo: roStr,
|
|
departmentType: "B",
|
|
vin,
|
|
customerNo: String(customerNo),
|
|
advisorNo: adv,
|
|
mileageIn: job.kmin
|
|
};
|
|
|
|
if (story) {
|
|
payload.roComment = String(story).trim();
|
|
}
|
|
|
|
if (makeOverride) {
|
|
// Passed through so the template can override DMS Make if needed
|
|
payload.makeOverride = String(makeOverride).trim();
|
|
}
|
|
|
|
const haveAllocations = Array.isArray(allocations) && allocations.length > 0;
|
|
|
|
if (haveAllocations) {
|
|
const effectiveOpCode = (opCode && String(opCode).trim()) || null;
|
|
// const effectiveTaxCode = (taxCode && String(taxCode).trim()) || null;
|
|
|
|
if (effectiveOpCode) {
|
|
// Build RO.GOG and RO.LABOR in the new normalized shape
|
|
const rogg = buildRogogFromAllocations(allocations, {
|
|
opCode: effectiveOpCode,
|
|
payType: "Cust"
|
|
});
|
|
|
|
if (rogg) {
|
|
payload.rogg = rogg;
|
|
|
|
const rolabor = buildRolaborFromRogog(rogg, { payType: "Cust" });
|
|
if (rolabor) {
|
|
payload.rolabor = rolabor;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return payload;
|
|
};
|
|
|
|
/**
|
|
* Make vehicle search payload from job data
|
|
* @param job
|
|
* @returns {{kind: string, license: string}|null|{kind: string, vin: *|string}}
|
|
*/
|
|
const makeVehicleSearchPayloadFromJob = (job) => {
|
|
const vin = safeVin(job);
|
|
if (vin) return { kind: "vin", vin };
|
|
|
|
const plate = job?.plate_no;
|
|
if (plate) return { kind: "license", license: String(plate).trim() };
|
|
|
|
return null;
|
|
};
|
|
|
|
/**
|
|
* Make customer search payload from job data
|
|
* @param job
|
|
* @returns {{kind: string, vin: *|string}|{kind: string, name: {name: string}}|{kind: string, phone: string}|null}
|
|
*/
|
|
const makeCustomerSearchPayloadFromJob = (job) => {
|
|
const phone = job?.ownr_ph1;
|
|
const d = digitsOnly(phone);
|
|
if (d.length >= 7) return { kind: "phone", phone: d };
|
|
|
|
const lastName = job?.ownr_ln;
|
|
const company = job?.ownr_co_nm;
|
|
const lnOrCompany = lastName || company;
|
|
if (lnOrCompany) return { kind: "name", name: { name: String(lnOrCompany).trim() } };
|
|
|
|
const vin = safeVin(job);
|
|
if (vin) return { kind: "vin", vin };
|
|
|
|
return null;
|
|
};
|
|
|
|
/**
|
|
* Normalize customer candidates from combined search result.
|
|
* @param res
|
|
* @returns {*[]}
|
|
*/
|
|
const normalizeCustomerCandidates = (res) => {
|
|
const blocks = blocksFromCombinedSearchResult(res);
|
|
const out = [];
|
|
for (const blk of blocks) {
|
|
const serv = Array.isArray(blk?.ServVehicle) ? blk.ServVehicle : [];
|
|
const custNos = serv.map((sv) => sv?.VehicleServInfo?.CustomerNo).filter(Boolean);
|
|
|
|
const nci = blk?.NameContactId;
|
|
const ind = nci?.NameId?.IndName;
|
|
const bus = nci?.NameId?.BusName;
|
|
const personal = [ind?.FName, ind?.LName].filter(Boolean).join(" ").trim();
|
|
const company = bus?.CompanyName;
|
|
const name = (personal || company || "").trim();
|
|
|
|
for (const custNo of custNos) {
|
|
out.push({ custNo, name: name || `Customer ${custNo}`, _blk: blk });
|
|
}
|
|
}
|
|
const seen = new Set();
|
|
return out.filter((c) => {
|
|
if (!c.custNo || seen.has(c.custNo)) return false;
|
|
seen.add(c.custNo);
|
|
return true;
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Normalize vehicle candidates from combined search result.
|
|
* @param res
|
|
* @returns {*[]}
|
|
*/
|
|
const normalizeVehicleCandidates = (res) => {
|
|
const blocks = blocksFromCombinedSearchResult(res);
|
|
const out = [];
|
|
for (const blk of blocks) {
|
|
const serv = Array.isArray(blk?.ServVehicle) ? blk.ServVehicle : [];
|
|
for (const sv of serv) {
|
|
const v = sv?.Vehicle || {};
|
|
const vin = v?.Vin || v?.VIN || v?.vin;
|
|
|
|
if (!vin) continue;
|
|
|
|
const year = v?.VehicleYr || v?.ModelYear || v?.Year;
|
|
const make = v?.VehicleMake || v?.MakeName || v?.Make;
|
|
const model = v?.MdlNo || v?.ModelDesc || v?.Model;
|
|
|
|
const label = [year, make, model, vin].filter(Boolean).join(" ");
|
|
out.push({ vin, year, make, model, label, _blk: blk });
|
|
}
|
|
}
|
|
const seen = new Set();
|
|
return out.filter((v) => {
|
|
if (!v.vin || seen.has(v.vin)) return false;
|
|
seen.add(v.vin);
|
|
return true;
|
|
});
|
|
};
|
|
|
|
/**
|
|
* 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,
|
|
buildRogogFromAllocations,
|
|
buildRolaborFromRogog
|
|
};
|