diff --git a/api.ts b/api.ts index 7f94305..dea54c7 100644 --- a/api.ts +++ b/api.ts @@ -34,18 +34,27 @@ const query = await Deno.readTextFile("sql/get.pg.sql"); const query_dseg = await Deno.readTextFile("sql/get_dseg.pg.sql"); // exact scenario for existing codes -router.get('/code_price/:billcode/:shipcode/:partcode/:qty', async (ctx) => { +router.get('/code/:billcode/:shipcode/:partcode/:qty', async (ctx) => { const { billcode, shipcode, partcode, qty } = ctx.params; const result = await client.queryObject({args: [billcode, shipcode, partcode, qty], text: query} ); ctx.response.body = apply_guidance(result.rows[0]["doc"]); }); +// specific customres codes but generic style code and data segment to accomodate custom colors and branding +router.get('/dseg/:billcode/:shipcode/:stlc/:dseg/:qty', async (ctx) => { + const { billcode, shipcode, stlc, dseg, qty } = ctx.params; + const result = await client.queryObject({args: [billcode, shipcode, stlc, dseg, qty], text: query_dseg} ); + ctx.response.body = apply_guidance(result.rows[0]["doc"]); +}); + // specific customres codes but generic style code and data segment to accomodate custom colors and branding router.get('/dseg_price/:billcode/:shipcode/:stlc/:dseg/:qty', async (ctx) => { const { billcode, shipcode, stlc, dseg, qty } = ctx.params; const result = await client.queryObject({args: [billcode, shipcode, stlc, dseg, qty], text: query_dseg} ); - ctx.response.body = apply_guidance(result.rows[0]["doc"]); + const ag = apply_guidance(result.rows[0]["doc"]); + ctx.response.body = ag.guidance.FinalReason; }); + app.use(router.routes()); app.use(router.allowedMethods()); diff --git a/apply_guidance.ts b/apply_guidance.ts index a329708..f72a25a 100644 --- a/apply_guidance.ts +++ b/apply_guidance.ts @@ -1,6 +1,21 @@ export function apply_guidance(doc: any) { + function getAdjValue(number) { + const data = [ + {f: 2.001, t: 1000, snap: 3, adj: 0 }, + {f: 1.001, t: 2, snap: 2, adj: 0 }, + {f: 0.1, t: 1, snap: 1, adj: 0 }, + {f: 0, t: 0.1, snap: 0, adj: 0 }, + {f: -1, t: -0.00001, snap: -1, adj: 0.05}, + {f: -2, t: -0.999999, snap: -2, adj: 0.05}, + {f: -1000, t: -2.001, snap: -3, adj: 0.10}, + ]; + const match = data.find(row => number >= row.f && number <= row.t); + return match ? match.adj : null; + } + const targetPrice = doc.pricing?.v1tp ?? doc.pricing?.v0tp; + const priceBand = doc.pricing?.v1stdv ?? doc.pricing?.v0stdv; const earlyPrice = doc.hist?.cust?.early_price; const earlySeason = doc.hist?.cust?.early_season; const bridgePremium = doc.pricing?.bridgePremium ?? 1.00000; @@ -25,6 +40,14 @@ export function apply_guidance(doc: any) { const inflation = Math.max(...Object.keys(iidx).map(Number)); const inflationFactor = iidx[inflation] + 1; const list = doc.pricing?.list && doc.product?.itemrel === "2" ? doc.pricing?.list : null; + + //-------set basic customer pricing-------------- + custPrice = Number((earlyPrice * bridgePremium).toFixed(5)); + anchorPrice = custPrice; + let anchor_sd = priceBand ? ((anchorPrice - targetPrice) / priceBand).toFixed(2) : 0 + let optimization = getAdjValue(anchor_sd); + + // ------if there is not target price just exit--------------- if (!targetPrice) { anchorSource = "No target pricing setup"; @@ -33,14 +56,12 @@ export function apply_guidance(doc: any) { // if there is no customer anchor price use target if (earlyPrice) { // translate alternate product history to current product quoted - custPrice = Number((earlyPrice * bridgePremium).toFixed(5)); - anchorPrice = custPrice; // --------if the price needs bridged, add the details to the description-------- if (bridgePremium === 1) { - anchorSource = earlySeason + ' Customer Price ' + earlyPrice; + anchorSource = earlySeason + ' Price ' + earlyPrice; custSource = anchorSource; } else { - anchorSource = earlySeason + ' Similar (' + altHist + ') Customer Price ' + earlyPrice + ' x ' + bridgePremium + ' = ' + anchorPrice; + anchorSource = earlySeason + ' Similar (' + altHist + ') Price ' + earlyPrice + ' x ' + bridgePremium + ' = ' + anchorPrice; custSource = anchorSource; } // --------after the early price is translated see if target is still less------- diff --git a/sql/get_guidance_dseg.pg.sql b/sql/get_guidance_dseg.pg.sql index a1b0a9b..fad11e6 100644 --- a/sql/get_guidance_dseg.pg.sql +++ b/sql/get_guidance_dseg.pg.sql @@ -64,13 +64,13 @@ BEGIN ,item ,idxk ,prefer - ,(SELECT unti FROM "CMS.CUSLG".itemm WHERE item = best.item) + ,pltq INTO _mold ,_item ,_iidx ,_itemr - ,_unti + ,_pltq FROM ( SELECT @@ -79,6 +79,7 @@ BEGIN ,i.stlc ,i.v1ds ,i.v0ds + ,i.pltq ,jsonb_strip_nulls(jsonb_build_object('assc',CASE WHEN i.assc <> '' THEN i.assc ELSE null::text END,'majg',i.majg::int,'coltier',i.coltier)) idxk ,CASE WHEN i.v1ds = _v1ds THEN 2 ELSE CASE WHEN i.v0ds = _v0ds THEN 1 ELSE 0 END END prefer FROM @@ -92,6 +93,7 @@ BEGIN ,i.stlc ,i.v1ds ,i.v0ds + ,i.pltq ,jsonb_strip_nulls(jsonb_build_object('assc',CASE WHEN i.assc <> '' THEN i.assc ELSE null::text END,'majg',i.majg::int,'coltier',i.coltier)) ,CASE WHEN i.v1ds = _v1ds THEN 2 ELSE CASE WHEN i.v0ds = _v0ds THEN 1 ELSE 0 END END ) best @@ -100,35 +102,17 @@ BEGIN LIMIT 1; _product := jsonb_build_object( - 'mold',_mold - ,'item',_item - ,'itemrel',_itemr - ,'iidx',_iidx - ,'unti',_unti + 'product' + ,jsonb_build_object( + 'mold',_mold + ,'item',_item + ,'itemrel',_itemr + ,'iidx',_iidx + ,'pltq',_pltq + ) ); --RAISE NOTICE 'item data %', jsonb_pretty(_product||_input); - ----------------pallet quantity----------------------------- - SELECT - ROUND(uom.nm/uom.dm,1) pltq - INTO - _pltq - FROM - ( - SELECT - jsonb_agg(row_to_json(d)::jsonb) jdoc - FROM - ( - select distinct - _item partn - ,'PLT' fu - ,_unti tu - ) d - ) c - JOIN LATERAL rlarp.uom_array(c.jdoc) uom ON TRUE; - - _product := jsonb_build_object('product',_product||jsonb_build_object('pltq',_pltq)); - ----------------channel------------------------------------- SELECT rlarp.channel_code(_bill, _ship) INTO _chan;