diff --git a/apply_guidance.ts b/apply_guidance.ts index a9528d9..f6be241 100644 --- a/apply_guidance.ts +++ b/apply_guidance.ts @@ -1,5 +1,21 @@ export function apply_guidance(doc: any) { + function sortObjectKeys(obj) { + // If the object is not an actual object or is an array, return it as is + if (typeof obj !== 'object' || obj === null || Array.isArray(obj)) { + return obj; + } + + // Create a new object and sort the keys + const sortedObj = {}; + Object.keys(obj).sort().forEach(key => { + // Recursively apply the function for nested objects + sortedObj[key] = sortObjectKeys(obj[key]); + }); + + return sortedObj; + } + function getAdjValue(number) { const data = [ {f: 2.001, t: 1000, snap: 3, adj: 0 }, @@ -76,9 +92,10 @@ export function apply_guidance(doc: any) { const inflationFactor = iidx[inflation]; const list = doc.pricing?.list && doc.product?.itemrel === "2" ? doc.pricing?.list : null; const listUSD = list ? list * fxrate :null; + const stlc = doc.inputs.stlc; // ------------------calculate price adders------------------------------------------------------ - let ltp = qty < pltq ? 0.15 : null; + let ltp = stlc.includes("SDD") || stlc.includes("HZP") ? 0 : (qty < pltq ? 0.15 : null); let anchor_sd = priceBand ? ((bridgedPrice - targetPrice) / priceBand).toFixed(2) : 0 let optimization = getAdjValue(anchor_sd); let custAdder = (ltp ?? 0) + optimization + inflationFactor; @@ -103,7 +120,7 @@ export function apply_guidance(doc: any) { let targPrice = targetPrice ? r5(targetPrice * (1 + markAdder)) : null; let targReason = `Target price ${pp(targetPrice)}${markAddReason}`; let listPrice = listUSD; - let listReason = fxrate === 1 ? "" : `list ${pp(list)} CAD ${pp(listUSD)} USD` + let listReason = fxrate === 1 ? `list ${pp(list)}` : `list ${pp(list)} CAD ${pp(listUSD)} USD`; let prices = { cust: [custPrice, custReason, "cust", ceiling(custPrice,snap)], @@ -123,5 +140,6 @@ export function apply_guidance(doc: any) { ,optimization } doc.guidance = guidance; - return doc; + //return doc; + return sortObjectKeys(doc); } diff --git a/sql/apply_guidance.pg.sql b/sql/apply_guidance.pg.sql index 599a41b..6b6ed6f 100644 --- a/sql/apply_guidance.pg.sql +++ b/sql/apply_guidance.pg.sql @@ -79,9 +79,10 @@ AS $function$ const inflationFactor = iidx ? iidx[inflation] : 0; const list = doc.pricing?.list && doc.product?.itemrel === "2" ? doc.pricing?.list : null; const listUSD = list ? list * fxrate :null; + const stlc = doc.inputs.stlc; // ------------------calculate price adders------------------------------------------------------ - let ltp = qty < pltq ? 0.15 : null; + let ltp = stlc.includes("SDD") || stlc.includes("HZP") ? 0 : (qty < pltq ? 0.15 : null); let anchor_sd = priceBand ? ((bridgedPrice - targetPrice) / priceBand).toFixed(2) : 0 let optimization = getAdjValue(anchor_sd); let custAdder = (ltp ?? 0) + optimization + inflationFactor; @@ -106,7 +107,7 @@ AS $function$ let targPrice = targetPrice ? r5(targetPrice * (1 + markAdder)) : null; let targReason = `Target price ${pp(targetPrice)}${markAddReason}`; let listPrice = listUSD; - let listReason = fxrate === 1 ? "" : `list ${pp(list)} CAD ${pp(listUSD)} USD` + let listReason = fxrate === 1 ? `list ${pp(list)}` : `list ${pp(list)} CAD ${pp(listUSD)} USD`; let prices = { cust: [custPrice, custReason, "cust", ceiling(custPrice,snap)],