adjust rep source to rlarp.repc, adjust join logic to match current osm definition

This commit is contained in:
Paul Trowbridge 2023-04-05 13:02:53 -04:00
parent 480adef789
commit 12c17b0be6

View File

@ -47,10 +47,10 @@ target AS (select $$replace_request$$::json def)
,('12 - May',12,5,0)
) x(m,s,cal,yr)
)
,SEG AS (
,seg AS (
SELECT
x.GLEC
,x.SEGM
x.GLEC glec
,x.SEGM segm
FROM
(
VALUES
@ -71,19 +71,6 @@ target AS (select $$replace_request$$::json def)
('9TO','Other')
) X(GLEC, SEGM)
)
,REPC AS (
SELECT
LTRIM(RTRIM(C.A9)) RCODE
,c.a30 rname
,LTRIM(RTRIM(C.A9)) || ' - ' || C.A30 REPP
,COALESCE(Q.DIR,'Other') DIRECTOR
FROM
LGDAT.CODE C
LEFT OUTER JOIN RLARP.QRH Q ON
Q.QR = LTRIM(RTRIM(C.A9))
WHERE
C.A2 = 'MM'
)
,copr AS (
SELECT
LTRIM(RTRIM(A9)) AS COMP,
@ -286,9 +273,9 @@ SELECT
,COALESCE(b.terms,bc.bvterm) terms
,b.bill_cust_descr
,b.ship_cust_descr
,(SELECT max(rcode) FROM repc WHERE rname = log.doc->'scenario'->>'quota_rep_descr') dsm
,(SELECT max(rcode) FROM rlarp.repc WHERE repp ~ (log.doc->'scenario'->>'quota_rep_descr')) dsm
,log.doc->'scenario'->>'quota_rep_descr' quota_rep_descr
,(SELECT max(director) FROM repc WHERE rname = log.doc->'scenario'->>'quota_rep_descr') director
,(SELECT max(director) FROM rlarp.repc WHERE repp ~ (log.doc->'scenario'->>'quota_rep_descr')) director
,COALESCE(CASE bc.BVADR6 WHEN '' THEN bc.BVNAME ELSE bc.BVADR6 END,b.bill_cust_descr) billto_group
,COALESCE(CASE sc.BVADR6 WHEN '' THEN sc.BVNAME ELSE sc.BVADR6 END,b.ship_cust_descr) shipto_group
,CASE SUBSTRING(bc.bvclas,2,3)
@ -364,52 +351,20 @@ FROM
BC.BVCUST = rtrim(substring(b.bill_cust_descr,1,8))
LEFT OUTER JOIN LGDAT.CUST SC ON
SC.BVCUST = rtrim(substring(b.ship_cust_descr,1,8))
LEFT OUTER JOIN REPC ON
REPC.RCODE = RTRIM(
--retail items go to currep, or if null go to 90005
CASE WHEN i.glec IN ('1RE','1CU') THEN
CASE WHEN bc.bvctry = 'CAN' THEN
--Rachel Bowman
'50300'
ELSE
--select customers go to select reps
CASE CASE bc.BVADR6 WHEN '' THEN bc.BVNAME ELSE bc.BVADR6 END
------Alecia Latini-------------------------------
WHEN 'DO IT BEST' THEN '90006'
WHEN 'ACE HARDWARE' THEN '90006'
WHEN 'ALDI' THEN '90006'
WHEN 'AMAZON.COM' THEN '90006'
WHEN 'GARDEN RIDGE CORP' THEN '90006' --AKA "At Home"
WHEN 'TRUE VALUE' THEN '90006'
WHEN 'WAYFAIR' THEN '90006'
WHEN 'GRIFFIN' THEN '90006'
WHEN 'WAL-MART' THEN '90006'
------Tony Landino--------------------------------
WHEN 'THE HOME DEPOT' THEN '50802'
WHEN 'FRED MEYER' THEN '50802'
WHEN 'MENARDS' THEN '50802'
WHEN 'KROGER' THEN '50802'
WHEN 'OCEAN STATE JOBBERS' THEN '50802'
WHEN 'AURORA WHOLESALE' THEN '50802'
WHEN 'LEON KORRAL' THEN '50802'
--all other retail goes to Doran Marable-----------
ELSE '50200'
END
END
--minor group b52 goes to dedicated rep
ELSE
CASE WHEN i.MING = 'B52' THEN
'PW'
--gdir, ndir go to bill-to rep
ELSE
CASE WHEN bc.bvclas IN ('GDIR','NDIR') THEN
bc.bvsalm
ELSE
sc.bvsalm
END
LEFT OUTER JOIN rlarp.repc r ON
r.rcode = CASE WHEN i.ming = 'B52' THEN 'PW' ELSE
--if the gl expense code is 1RE use the retail rep assigned to the bill-to customer if available
CASE WHEN COALESCE(seg.segm,'') = 'Retail' AND COALESCE((SELECT currep FROM lgpgm.usrcust where cucust = bc.bvcust),'') <> ''
THEN
(SELECT currep FROM lgpgm.usrcust where cucust = bc.bvcust)
--default logic
ELSE
CASE SUBSTR(bc.bvclas,2,3)
WHEN 'DIS' THEN sc.bvsalm
ELSE bc.bvsalm
END
END
END
)
LEFT OUTER JOIN lgdat.icstm im ON
im.cgpart = b.part
AND im.cgplnt = i.dplt