----------------------------SET BILL-TO REP------------------------------------

UPDATE
    public.b S
SET
    BILL_REP = C.BVSALM
    ,BILL_CLASS = C.BVCLAS
FROM
    LGDAT.CUST C
WHERE
    C.BVCUST = S.BILL_CUST
    AND (
        COALESCE(S.BILL_REP,'') <> C.BVSALM 
        OR COALESCE(S.BILL_CLASS,'') <> C.BVCLAS 
    );
    
----------------------------SET SHIP-TO REP------------------------------------

UPDATE
    public.b S
SET
    SHIP_REP = C.BVSALM
    ,SHIP_CLASS = C.BVCLAS
FROM
    LGDAT.CUST C
WHERE
    C.BVCUST = S.SHIP_CUST
    AND (
        COALESCE(S.SHIP_REP,'') <> C.BVSALM 
        OR COALESCE(S.SHIP_CLASS,'') <> C.BVCLAS 
    );

----------------------------SET BILLTO GROUP------------------------------------

UPDATE
    public.b O
SET
    BILL_DBA = CASE BVADR6 WHEN '' THEN BVNAME ELSE BVADR6 END
FROM
    LGDAT.CUST C
WHERE
    C.BVCUST = O.BILL_CUST
    AND CASE BVADR6 WHEN '' THEN BVNAME ELSE BVADR6 END <> O.ACCOUNT;


----------------------------SET SHIPTO GROUP------------------------------------

UPDATE
    public.b O
SET
    SHIP_DBA = CASE BVADR6 WHEN '' THEN BVNAME ELSE BVADR6 END
FROM
    LGDAT.CUST C
WHERE
    C.BVCUST = O.SHIP_CUST
    AND CASE BVADR6 WHEN '' THEN BVNAME ELSE BVADR6 END <> O.SHIPGRP;


---------------------------SET GEOGRAPHY----------------------------------------

UPDATE
    public.b o
SET
    geo = t.geo 
FROM 
    rlarp.ffterr t
WHERE
    t.CTRY = o.DEST_CTRY 
    AND t.PROV = o.DEST_PROV 
    AND o.GEO <> t.GEO;


---------------------------SET CHANNEL-----------------------------------------


UPDATE
    public.b
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
    public.b S
SET
    DSM = CR.QUOTA_REP
FROM
(
    SELECT DISTINCT
        VERSION,
        COALESCE(GLEC,'') GLEC,
        SUBSTRING(mingd,1,3),
        BILL_CUST,
        SHIP_CUST,
        ------------quota rep column--------------
        CASE WHEN COALESCE(SUBSTRING(mingd,1,3),'') = '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
            ELSE
                CASE SUBSTR(bill_class,2,3)
                    WHEN 'DIS' THEN
                        ship_rep
                    ELSE
                        bill_rep
                END
            END
        END QUOTA_REP
    FROM 
        public.b S
        LEFT OUTER JOIN LGDAT.CUST ON
            BVCUST = BILL_CUST
        LEFT OUTER JOIN lgpgm.usrcust cu ON
            cu.cucust = s.bill_cust
    ) CR
WHERE
    CR.VERSION = S.VERSION
    AND CR.GLEC = COALESCE(S.GLEC,'')
    AND CR.SUBSTRING(mingd,1,3) = S.SUBSTRING(mingd,1,3)
    AND CR.BILL_CUST = S.BILL_CUST 
    AND CR.SHIP_CUST = S.SHIP_CUST
    AND COALESCE(S.DSM,'') <> CR.QUOTA_REP;
