BEGIN;
with 
logl AS (
    INSERT INTO 
        rlarp.osm_log(doc) 
    SELECT
        jsonb_build_object(
            'stamp',current_timestamp
            ,'user','Trowbridge, Paul'
            ,'source','script'
            ,'tag','remove other'
            ,'message','remove budget for quota reps rolling into director Other'
            ,'type','remove other'
            ,'version','b21'
        ) doc RETURNING *
)
,targ as (
    select 
        p.fspr
        ,p.plnt
        ,p.promo
        ,p.terms
        ,p.bill_cust_descr
        ,p.ship_cust_descr
        ,p.dsm
        ,p.quota_rep_descr
        ,p.director
        ,p.billto_group
        ,p.shipto_group
        ,p.chan
        ,p.chansub
        ,p.chan_retail     
        ,p.part
        ,p.part_descr
        ,p.part_group
        ,p.branding
        ,p.majg_descr
        ,p.ming_descr
        ,p.majs_descr
        ,p.mins_descr
        ,p.segm
        ,p.substance
        ,p.fs_line
        ,p.r_currency
        ,p.r_rate
        ,p.c_currency
        ,p.c_rate
        ,-p.units     units
        ,-p.value_loc value_loc
        ,-p.value_usd value_usd
        ,-p.cost_loc  cost_loc
        ,-p.cost_usd  cost_usd
        ,p.calc_status
        ,p.flag
        ,p.order_date
        ,p.order_month
        ,p.order_season
        ,p.request_date
        ,p.request_month
        ,p.request_season
        ,p.ship_date
        ,p.ship_month
        ,p.ship_season
        ,'b21' AS version
        ,'adj volume' iter
        ,logl.id logid
        ,logl.doc->>'tag' tag
        ,logl.doc->>'message' "comment"
        ,logl.doc->>'type' module
    from 
        rlarp.osm_pool p
        CROSS JOIN logl
    where 
        director = 'Other' 
        and (order_season >= 2021 or ship_season >= 2021)
)
INSERT INTO rlarp.osm_pool SELECT * FROM targ;

COMMIT;
