diff --git a/sql/live_quotes_review.pg.sql b/sql/live_quotes_review.pg.sql index f8b1729..11a9034 100644 --- a/sql/live_quotes_review.pg.sql +++ b/sql/live_quotes_review.pg.sql @@ -14,7 +14,7 @@ lq AS MATERIALIZED ( SELECT lq.* ,pricing->'product'->>'mold' part_group - ,substring(pricing->'customer'->>'chan',1,1) qchan + -- ,substring(pricing->'customer'->>'chan',1,1) qchan ,pricing->'customer'->>'cust' qcust ,pricing->'product'->>'itemrel' item_fit ,(pricing->'product'->>'pltq')::numeric pltq diff --git a/sql/quote_review.pg.sql b/sql/quote_review.pg.sql index b4b9dfa..7507034 100644 --- a/sql/quote_review.pg.sql +++ b/sql/quote_review.pg.sql @@ -14,7 +14,19 @@ lq AS MATERIALIZED ( SELECT lq.* ,pricing->'product'->>'mold' part_group - ,substring(pricing->'customer'->>'chan',1,1) qchan + CASE SUBSTR(bc.cclass,2,3) + --if the bill to class is ditsributor, then it's either warehouse or drop + WHEN 'DIS' THEN + --if the ship-to is a different name than the bill-to then it's drop, otherwise it's warehouse + CASE SUBSTR(sc.cclass,2,3) + WHEN 'DIS' THEN 'WHS' + ELSE 'DRP' + END + --CASE WHEN RTRIM(SUBSTR(LTRIM(SC.BVADR7)||SC.BVNAME,1,30)) = RTRIM(SUBSTR(LTRIM(BC.BVADR7)||BC.BVNAME,1,30)) THEN 'DIS' ELSE 'DRP' END + --everything else does not involve a distributor and is considered direct + ELSE 'DIR' + END qchan, + ,CASE osm.chan WHEN 'DRP' THEN rtrim(osm.shipgrp) ELSE rtrim(osm.account) END customer ,pricing->'customer'->>'cust' qcust ,pricing->'product'->>'itemrel' item_fit ,(pricing->'product'->>'pltq')::numeric pltq @@ -28,6 +40,10 @@ lq AS MATERIALIZED ( ,jsonb_pretty(pricing) pricing FROM lq + LEFT OUTER JOIN rlarp.cust bc ON + bc.cust = lq.billto + LEFT OUTER JOIN rlarp.cust sc ON + bc.cust = lq.shipto LEFT JOIN LATERAL rlarp.guidance_r1( rlarp.get_guidance_dseg(lq.billto,lq.shipto,substring(lq.part,1,8),lq.v1ds,lq.units_each,2025) ) pricing ON TRUE