From aaa62afb5d8f546d8ed619d8e7e13baeab9c4aec Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Mon, 20 Nov 2023 13:53:43 -0500 Subject: [PATCH] vault backup: 2023-11-20 13:53:43 --- sql/apply_guidance.pg.sql | 5 ++- sql/livequotes.pg.sql | 74 +++++++++++++++++++++++++++++++++------ sql/livequotes_dev.pg.sql | 59 +++++++++++++++++++++++++++++++ sql/price_pool.sql | 9 ++--- 4 files changed, 130 insertions(+), 17 deletions(-) create mode 100644 sql/livequotes_dev.pg.sql diff --git a/sql/apply_guidance.pg.sql b/sql/apply_guidance.pg.sql index 71cd69e..d36d51f 100644 --- a/sql/apply_guidance.pg.sql +++ b/sql/apply_guidance.pg.sql @@ -77,9 +77,8 @@ AS $function$ const fxrate = doc.customer?.fxrate ?? 1.0; const qty = doc.inputs?.qty; const pltq = doc.product?.pltq; - console.log(iidx); - const inflation = Math.max(...Object.keys(iidx).map(Number)); - const inflationFactor = iidx[inflation]; + const inflation = iidx ? Math.max(...Object.keys(iidx).map(Number)) : null; + 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; diff --git a/sql/livequotes.pg.sql b/sql/livequotes.pg.sql index 3a99280..cb79ea7 100644 --- a/sql/livequotes.pg.sql +++ b/sql/livequotes.pg.sql @@ -1,10 +1,64 @@ -SELECT - lq.* - ,pricing->'guidance'->'finalPrice'->>'Price' - ,pricing->'guidance'->'finalPrice'->>'Reason' -FROM - pricequote.live_quotes lq - JOIN LATERAL rlarp.guidance_r1( - rlarp.get_guidance_dseg(lq.billto,lq.shipto,substring(lq.part,1,8),lq.v1ds,lq.units_each,2024) - ) pricing ON TRUE -LIMIT 1000 +DROP VIEW rlarp.live_quotes_review; +CREATE OR REPLACE VIEW rlarp.live_quotes_review AS +WITH +lq AS MATERIALIZED ( + SELECT + lq.* + ,substring(lq.part,1,8) mold + FROM + pricequote.live_quotes lq +) +,lqg AS ( + SELECT + lq.* + ,m.part_group + ,i.pltq + ,(pricing->'guidance'->'finalPrice'->>'Price')::numeric guidance + ,pricing->'guidance'->'finalPrice'->>'Reason' reason + ,jsonb_pretty(pricing) pricing + FROM + lq + JOIN LATERAL rlarp.guidance_r1( + rlarp.get_guidance_dseg(lq.billto,lq.shipto,substring(lq.part,1,8),lq.v1ds,lq.units_each,2024) + ) pricing ON TRUE + LEFT OUTER JOIN rlarp.molds m ON + m.stlc = lq.mold + LEFT OUTER JOIN "CMS.CUSLG".itemmv i ON + i.item = lq.part + WHERE + lq.qstat ~ 'Submitted' +) +,hist AS ( + SELECT + g.* + ,gset.chan + --,gset.mold moldh + ,gset.v1ds v1dsh + ,gset.cust + ,gset.vers + ,je.k + ,seas.* + FROM + lqg g + LEFT OUTER JOIN rlarp.price_pool_dev p ON + p.gset @> jsonb_build_object('mold',g.part_group) + AND p.gset ? 'cust' + AND p.gset ? 'v1ds' + LEFT JOIN LATERAL jsonb_to_record(p.gset) AS gset( + chan text + ,mold text + ,v1ds text + ,v0ds text + ,cust text + ,vers text + --,nurs text + --,ghse text + ) ON TRUE + LEFT JOIN LATERAL jsonb_each(p.season) je(k,v) on true + LEFT JOIN Lateral jsonb_to_record(je.v) as seas( + units numeric + ,sales_usd numeric + ,price_usd numeric + ) ON TRUE +) +SELECT * FROM hist diff --git a/sql/livequotes_dev.pg.sql b/sql/livequotes_dev.pg.sql new file mode 100644 index 0000000..54a4107 --- /dev/null +++ b/sql/livequotes_dev.pg.sql @@ -0,0 +1,59 @@ +WITH +lq AS MATERIALIZED ( + SELECT + lq.* + ,substring(lq.part,1,8) mold + FROM + pricequote.live_quotes lq +) +,lqg AS ( + SELECT + lq.* + ,m.part_group + ,(pricing->'guidance'->'finalPrice'->>'Price')::numeric guidance + ,pricing->'guidance'->'finalPrice'->>'Reason' reason + ,jsonb_pretty(pricing) pricing + FROM + lq + JOIN LATERAL rlarp.guidance_r1( + rlarp.get_guidance_dseg(lq.billto,lq.shipto,substring(lq.part,1,8),lq.v1ds,lq.units_each,2024) + ) pricing ON TRUE + LEFT OUTER JOIN rlarp.molds m ON + m.stlc = lq.mold + WHERE + lq.qstat ~ 'Submitted' +) +,hist AS ( + SELECT + g.* + ,gset.chan + --,gset.mold moldh + ,gset.v1ds v1dsh + ,gset.cust + ,gset.vers + ,je.k + ,seas.* + FROM + lqg g + LEFT OUTER JOIN rlarp.price_pool_dev p ON + p.gset @> jsonb_build_object('mold',g.part_group) + AND p.gset ? 'cust' + AND p.gset ? 'v1ds' + LEFT JOIN LATERAL jsonb_to_record(p.gset) AS gset( + chan text + ,mold text + ,v1ds text + ,v0ds text + ,cust text + ,vers text + --,nurs text + --,ghse text + ) ON TRUE + LEFT JOIN LATERAL jsonb_each(p.season) je(k,v) on true + LEFT JOIN Lateral jsonb_to_record(je.v) as seas( + units numeric + ,sales_usd numeric + ,price_usd numeric + ) ON TRUE +) +SELECT * FROM hist WHERE qid = 73059 diff --git a/sql/price_pool.sql b/sql/price_pool.sql index 34e06c4..40717fc 100644 --- a/sql/price_pool.sql +++ b/sql/price_pool.sql @@ -1,9 +1,10 @@ --CREATE OR REPLACE PROCEDURE rlarp.price_pool() --LANGUAGE plpgsql AS --$func$ -BEGIN; -DROP TABLE IF EXISTS rlarp.price_pool_dev; -CREATE TABLE IF NOT EXISTS rlarp.price_pool_dev AS ( +--BEGIN; +--DROP TABLE IF EXISTS rlarp.price_pool_dev; +--CREATE TABLE IF NOT EXISTS rlarp.price_pool_dev AS ( +CREATE MATERIALIZED VIEW rlarp.price_pool_dev AS ( WITH agg AS ( SELECT @@ -171,4 +172,4 @@ CREATE TABLE IF NOT EXISTS rlarp.price_pool_dev AS ( ) WITH DATA; create index ppd_gset on rlarp.price_pool_dev using gin (gset); -END; +--END;