forecast_api/build/snap_customer.sql

181 lines
6.1 KiB
MySQL
Raw Normal View History

2020-02-12 21:42:47 -05:00
----------------------------SET BILL-TO REP------------------------------------
UPDATE
2023-03-01 16:07:36 -05:00
rlarp.osmf s
2020-02-12 21:42:47 -05:00
SET
2023-03-01 16:07:36 -05:00
bill_rep = c.bvsalm
,bill_class = c.bvclas
,bill_terr = c.bvterr
,bill_ctry = c.bvctry
,bill_prov = c.bvprcd
,bill_post = c.bvpost
,remit_to = c.bvcomp
2023-03-01 16:07:36 -05:00
,account = CASE bvadr6 WHEN '' THEN bvname ELSE bvadr6 END
2020-02-12 21:42:47 -05:00
FROM
2023-03-01 16:07:36 -05:00
lgdat.cust c
2020-02-12 21:42:47 -05:00
WHERE
2023-03-01 16:07:36 -05:00
c.bvcust = s.bill_cust
2020-02-12 21:42:47 -05:00
AND (
2023-03-01 16:07:36 -05:00
COALESCE(s.bill_rep,'') <> c.bvsalm
OR COALESCE(s.bill_class,'') <> c.bvclas
OR COALESCE(s.bill_terr,'') <> c.bvterr
OR COALESCE(bill_ctry,'') <> c.bvctry
OR COALESCE(bill_prov,'') <> c.bvprcd
OR COALESCE(bill_post,'') <> c.bvpost
OR COALESCE(remit_to,'') <> c.bvcomp::text
2020-02-12 21:42:47 -05:00
);
----------------------------SET SHIP-TO REP------------------------------------
UPDATE
2023-03-01 16:07:36 -05:00
rlarp.osmf s
2020-02-12 21:42:47 -05:00
SET
2023-03-01 16:07:36 -05:00
ship_rep = c.bvsalm
,ship_class = c.bvclas
,ship_terr = c.bvterr
,dest_ctry = c.bvctry
,dest_prov = c.bvprcd
,dest_post = c.bvpost
2020-02-12 21:42:47 -05:00
FROM
2023-03-01 16:07:36 -05:00
lgdat.cust c
2020-02-12 21:42:47 -05:00
WHERE
2023-03-01 16:07:36 -05:00
c.bvcust = s.ship_cust
2020-02-12 21:42:47 -05:00
AND (
2023-03-01 16:07:36 -05:00
COALESCE(s.ship_rep,'') <> c.bvsalm
OR COALESCE(s.ship_class,'') <> c.bvclas
OR COALESCE(s.ship_terr,'') <> c.bvterr
OR COALESCE(dest_ctry,'') <> c.bvctry
OR COALESCE(dest_prov,'') <> c.bvprcd
OR COALESCE(dest_post,'') <> c.bvpost
2020-02-12 21:42:47 -05:00
);
----------------------------SET BILLTO GROUP------------------------------------
UPDATE
2023-03-01 16:07:36 -05:00
rlarp.osmf o
2020-02-12 21:42:47 -05:00
SET
ACCOUNT = CASE BVADR6 WHEN '' THEN BVNAME ELSE BVADR6 END
FROM
2023-03-01 16:07:36 -05:00
lgdat.cust c
2020-02-12 21:42:47 -05:00
WHERE
2023-03-01 16:07:36 -05:00
c.bvcust = o.bill_cust
AND coalesce(account,'') <> CASE BVADR6 WHEN '' THEN BVNAME ELSE BVADR6 END;
2020-02-12 21:42:47 -05:00
----------------------------SET SHIPTO GROUP------------------------------------
UPDATE
2023-03-01 16:07:36 -05:00
rlarp.osmf o
2020-02-12 21:42:47 -05:00
SET
SHIPGRP = CASE BVADR6 WHEN '' THEN BVNAME ELSE BVADR6 END
FROM
2023-03-01 16:07:36 -05:00
lgdat.cust c
2020-02-12 21:42:47 -05:00
WHERE
2023-03-01 16:07:36 -05:00
c.bvcust = o.ship_cust
2020-02-12 21:42:47 -05:00
AND CASE BVADR6 WHEN '' THEN BVNAME ELSE BVADR6 END <> COALESCE(O.SHIPGRP,'');
---------------------------SET CHANNEL-----------------------------------------
UPDATE
2023-03-01 16:07:36 -05:00
rlarp.osmf
2020-02-12 21:42:47 -05:00
SET
CHAN = CASE SUBSTRING(BILL_CLASS,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 SUBSTRING(SHIP_CLASS,2,3)
WHEN 'DIS' THEN 'WHS'
ELSE 'DRP'
END
--CASE WHEN RTRIM(SUBSTRING(LTRIM(SC.BVADR7)||SC.BVNAME,1,30)) = RTRIM(SUBSTRING(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,
CHANSUB = CASE SUBSTRING(BILL_CLASS,2,3)
WHEN 'DIS' THEN
--if the ship-to is a different name than the bill-to then it's drop, otherwise it's warehouse
CASE SUBSTRING(SHIP_CLASS,2,3)
WHEN 'DIS' THEN 'WHS'
ELSE CASE SUBSTRING(SHIP_CLASS,1,1) WHEN 'R' THEN 'RDP' ELSE 'DRP' END
END
WHEN 'MAS' THEN 'RMN'
WHEN 'NAT' THEN 'RMN'
ELSE CASE SUBSTRING(SHIP_CLASS,1,1) WHEN 'R' THEN 'RDI' ELSE 'DIR' END
END
WHERE
COALESCE(CHAN,'') <> CASE SUBSTRING(BILL_CLASS,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 SUBSTRING(SHIP_CLASS,2,3)
WHEN 'DIS' THEN 'WHS'
ELSE 'DRP'
END
--CASE WHEN RTRIM(SUBSTRING(LTRIM(SC.BVADR7)||SC.BVNAME,1,30)) = RTRIM(SUBSTRING(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
OR
COALESCE(CHANSUB,'') <> CASE SUBSTRING(BILL_CLASS,2,3)
WHEN 'DIS' THEN
--if the ship-to is a different name than the bill-to then it's drop, otherwise it's warehouse
CASE SUBSTRING(SHIP_CLASS,2,3)
WHEN 'DIS' THEN 'WHS'
ELSE CASE SUBSTRING(SHIP_CLASS,1,1) WHEN 'R' THEN 'RDP' ELSE 'DRP' END
END
WHEN 'MAS' THEN 'RMN'
WHEN 'NAT' THEN 'RMN'
ELSE CASE SUBSTRING(SHIP_CLASS,1,1) WHEN 'R' THEN 'RDI' ELSE 'DIR' END
END;
---------------------------SET QUOTA REP---------------------------------------
UPDATE
2023-03-01 16:07:36 -05:00
rlarp.osmf s
2020-02-12 21:42:47 -05:00
SET
2023-03-01 16:07:36 -05:00
dsm = cr.quota_rep
2020-02-12 21:42:47 -05:00
FROM
(
SELECT DISTINCT
2023-03-01 16:07:36 -05:00
version,
COALESCE(glec,'') glec,
ming,
bill_cust,
ship_cust,
2020-02-12 21:42:47 -05:00
------------quota rep column--------------
CASE WHEN COALESCE(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(glec,'') = '1RE' AND COALESCE(cu.currep,'') <> '' THEN
cu.currep
--default logic
2020-02-12 21:42:47 -05:00
ELSE
CASE SUBSTR(bill_class,2,3)
WHEN 'DIS' THEN
ship_rep
2020-02-12 21:42:47 -05:00
ELSE
bill_rep
2020-02-12 21:42:47 -05:00
END
END
END QUOTA_REP
2020-02-12 21:42:47 -05:00
FROM
2023-03-01 16:07:36 -05:00
rlarp.osmf s
LEFT OUTER JOIN lgdat.cust ON
bvcust = bill_cust
LEFT OUTER JOIN lgpgm.usrcust cu ON
cu.cucust = s.bill_cust
2020-02-12 21:42:47 -05:00
WHERE
version = 'ACTUALS'
2020-02-12 21:42:47 -05:00
) CR
WHERE
2023-03-01 16:07:36 -05:00
cr.version = s.version
AND cr.glec = COALESCE(s.glec,'')
AND cr.ming = s.ming
AND cr.bill_cust = s.bill_cust
AND cr.ship_cust = s.ship_cust
AND COALESCE(s.dsm,'') <> cr.quota_rep;