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

UPDATE
    rlarp.osmf s
SET
    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
    ,account = CASE bvadr6 WHEN '' THEN bvname ELSE bvadr6 END
FROM
    lgdat.cust c
WHERE
    c.bvcust = s.bill_cust
    AND (
           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
    );
    
----------------------------SET SHIP-TO REP------------------------------------

UPDATE
    rlarp.osmf s
SET
    ship_rep = c.bvsalm
    ,ship_class = c.bvclas
    ,ship_terr = c.bvterr
    ,dest_ctry = c.bvctry 
    ,dest_prov = c.bvprcd
    ,dest_post = c.bvpost 
FROM
    lgdat.cust c
WHERE
    c.bvcust = s.ship_cust
    AND (
           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 
    );

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

UPDATE
    rlarp.osmf o
SET
    ACCOUNT = CASE BVADR6 WHEN '' THEN BVNAME ELSE BVADR6 END
FROM
    lgdat.cust c
WHERE
    c.bvcust = o.bill_cust
    AND coalesce(account,'') <> CASE BVADR6 WHEN '' THEN BVNAME ELSE BVADR6 END;
    


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

UPDATE
    rlarp.osmf o
SET
    SHIPGRP = 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 <> COALESCE(O.SHIPGRP,'');


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


UPDATE
    rlarp.osmf
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
    rlarp.osmf s
SET
    dsm = cr.quota_rep
FROM
(
    SELECT DISTINCT
        version,
        COALESCE(glec,'') glec,
        ming,
        bill_cust,
        ship_cust,
        ------------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
            ELSE
                CASE SUBSTR(bill_class,2,3)
                    WHEN 'DIS' THEN
                        ship_rep
                    ELSE
                        bill_rep
                END
            END
        END QUOTA_REP
    FROM 
        rlarp.osmf s
        LEFT OUTER JOIN lgdat.cust ON
            bvcust = bill_cust
        LEFT OUTER JOIN lgpgm.usrcust cu ON
            cu.cucust = s.bill_cust
    WHERE
        version = 'ACTUALS'
    ) CR
WHERE
        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;
