----------------------------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---------------------------------------



MERGE INTO 
    rlarp.osmf AS s
USING (
    SELECT DISTINCT
         s.bill_cust
        ,s.ship_cust
        ,s.glec
        ,CASE substring(bc.cclass, 2, 3) 
            WHEN 'DIR' THEN bc.default_rep
            ELSE COALESCE(sc.default_rep, bc.default_rep)
        END AS default_rep
        ,CASE substring(bc.cclass, 2, 3) 
            WHEN 'DIR' THEN bc.tier
            ELSE COALESCE(sc.tier, bc.tier)
        END AS customer_tier
        ,CASE 
            WHEN COALESCE(s.glec, '') = '1RE' AND COALESCE(bc.retail_rep, '') <> '' 
            THEN bc.retail_rep 
            ELSE NULL 
        END AS retail_rep
    FROM rlarp.osmf s
    LEFT JOIN rlarp.cust bc ON bc.code = s.bill_cust
    LEFT JOIN rlarp.cust sc ON sc.code = s.ship_cust
    WHERE glec IS NOT NULL
) AS cr
ON 
    cr.bill_cust = s.bill_cust AND
    COALESCE(cr.ship_cust, '') = COALESCE(s.ship_cust, '') AND
    s.glec = cr.glec
WHEN MATCHED THEN
UPDATE SET
    dsm             = COALESCE(cr.retail_rep, cr.default_rep);

