WITH
/*
the volume must be expressed in terms of units, since that is what it will be scaling
*/
target AS (select $${"parts":[{"part":"KEP015P0X56B316","qty":31680,"price":"0.058"},{"part":"KEP015P0X56B316PYMLI","qty":31680,"price":"0.111"},{"part":"KEP015P0X19B316","qty":63360,"price":"0.058"},{"part":"KEP015P0X19B316PYMGL","qty":63360,"price":"0.111"},{"part":"KET10P15G18B030","qty":12168,"price":"0.519"},{"part":"KEP010P0X19B524","qty":366912,"price":"0.039"},{"part":"KEP010P0X19B262PYMLE","qty":366912,"price":"0.095"},{"part":"KEP010P0X19B524","qty":2044224,"price":"0.039"},{"part":"KEP010P0X19B262PYMLE","qty":2044224,"price":"0.095"},{"part":"KET12P10A10B027","qty":201188,"price":"0.54"},{"part":"KET10P15A10B030","qty":12168,"price":"0.595"},{"part":"KET12P10G18B027","qty":201188,"price":"0.473"}],"dates":[{"bill_cust_descr":"COST0017 - COSTA FARMS LLC-GOULDS FL","ship_cust_descr":"COST0046 - COSTA LAYMAN, LLC","promo":"NONE","terms":"7N","r_currency":"US","order_date":"2025-07-01","ship_date":"2025-09-16","fc_mix":"0.0118","baseline_mix":"0.0118"},{"bill_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","ship_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","promo":"NONE","terms":"1G","r_currency":"US","order_date":"2025-07-15","ship_date":"2025-07-18","fc_mix":"0.00944","baseline_mix":"0.00944"},{"bill_cust_descr":"COST0017 - COSTA FARMS LLC-GOULDS FL","ship_cust_descr":"COST0002 - COSTA CAROLINA-LEICESTER NC","promo":"NONE","terms":"7N","r_currency":"US","order_date":"2025-07-17","ship_date":"2025-08-30","fc_mix":"0.00448","baseline_mix":"0.00448"},{"bill_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","ship_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","promo":"NONE","terms":"1G","r_currency":"US","order_date":"2025-07-29","ship_date":"2025-08-07","fc_mix":"0.03103","baseline_mix":"0.03103"},{"bill_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","ship_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","promo":"NONE","terms":"1G","r_currency":"US","order_date":"2025-07-29","ship_date":"2025-08-12","fc_mix":"0.00877","baseline_mix":"0.00877"},{"bill_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","ship_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","promo":"NONE","terms":"1G","r_currency":"US","order_date":"2025-07-29","ship_date":"2025-09-03","fc_mix":"0.01887","baseline_mix":"0.01887"},{"bill_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","ship_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","promo":"NONE","terms":"1G","r_currency":"US","order_date":"2025-07-29","ship_date":"2025-09-13","fc_mix":"0.06421","baseline_mix":"0.06421"},{"bill_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","ship_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","promo":"NONE","terms":"1G","r_currency":"US","order_date":"2025-07-29","ship_date":"2025-09-26","fc_mix":"0.06667","baseline_mix":"0.06667"},{"bill_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","ship_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","promo":"NONE","terms":"1G","r_currency":"US","order_date":"2025-07-29","ship_date":"2025-10-09","fc_mix":"0.0127","baseline_mix":"0.0127"},{"bill_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","ship_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","promo":"NONE","terms":"1G","r_currency":"US","order_date":"2025-07-29","ship_date":"2025-10-16","fc_mix":"0.00317","baseline_mix":"0.00317"},{"bill_cust_descr":"COST0017 - COSTA FARMS LLC-GOULDS FL","ship_cust_descr":"COST0015 - COSTA FARMS-FOLIAGE DIV (CNF)","promo":"NONE","terms":"7N","r_currency":"US","order_date":"2025-08-08","ship_date":"2025-08-27","fc_mix":"0.02595","baseline_mix":"0.02595"},{"bill_cust_descr":"COST0017 - COSTA FARMS LLC-GOULDS FL","ship_cust_descr":"COST0015 - COSTA FARMS-FOLIAGE DIV (CNF)","promo":"NONE","terms":"7N","r_currency":"US","order_date":"2025-08-08","ship_date":"2025-09-06","fc_mix":"0.02595","baseline_mix":"0.02595"},{"bill_cust_descr":"COST0017 - COSTA FARMS LLC-GOULDS FL","ship_cust_descr":"COST0015 - COSTA FARMS-FOLIAGE DIV (CNF)","promo":"NONE","terms":"7N","r_currency":"US","order_date":"2025-08-12","ship_date":"2025-10-18","fc_mix":"0.03775","baseline_mix":"0.03775"},{"bill_cust_descr":"COST0017 - COSTA FARMS LLC-GOULDS FL","ship_cust_descr":"COST0015 - COSTA FARMS-FOLIAGE DIV (CNF)","promo":"NONE","terms":"7N","r_currency":"US","order_date":"2025-08-12","ship_date":"2025-11-06","fc_mix":"0.0236","baseline_mix":"0.0236"},{"bill_cust_descr":"COST0017 - COSTA FARMS LLC-GOULDS FL","ship_cust_descr":"COST0015 - COSTA FARMS-FOLIAGE DIV (CNF)","promo":"NONE","terms":"7N","r_currency":"US","order_date":"2025-08-12","ship_date":"2025-12-06","fc_mix":"0.03304","baseline_mix":"0.03304"},{"bill_cust_descr":"COST0017 - COSTA FARMS LLC-GOULDS FL","ship_cust_descr":"COST0015 - COSTA FARMS-FOLIAGE DIV (CNF)","promo":"NONE","terms":"7N","r_currency":"US","order_date":"2025-08-12","ship_date":"2025-12-13","fc_mix":"0.00472","baseline_mix":"0.00472"},{"bill_cust_descr":"COST0017 - COSTA FARMS LLC-GOULDS FL","ship_cust_descr":"COST0007 - COSTA COLOR-MIAMI FL","promo":"NONE","terms":"7N","r_currency":"US","order_date":"2025-09-06","ship_date":"2025-12-13","fc_mix":"0.00555","baseline_mix":"0.00555"},{"bill_cust_descr":"COST0017 - COSTA FARMS LLC-GOULDS FL","ship_cust_descr":"COST0007 - COSTA COLOR-MIAMI FL","promo":"NONE","terms":"7N","r_currency":"US","order_date":"2025-09-06","ship_date":"2025-12-30","fc_mix":"0.00388","baseline_mix":"0.00388"},{"bill_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","ship_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","promo":"NONE","terms":"1G","r_currency":"US","order_date":"2025-10-14","ship_date":"2025-10-29","fc_mix":"0.00239","baseline_mix":"0.00239"},{"bill_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","ship_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","promo":"NONE","terms":"1G","r_currency":"US","order_date":"2025-10-29","ship_date":"2025-11-18","fc_mix":"0.00439","baseline_mix":"0.00439"},{"bill_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","ship_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","promo":"NONE","terms":"1G","r_currency":"US","order_date":"2025-10-29","ship_date":"2025-11-19","fc_mix":"0.00239","baseline_mix":"0.00239"},{"bill_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","ship_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","promo":"NONE","terms":"1G","r_currency":"US","order_date":"2025-10-29","ship_date":"2025-12-10","fc_mix":"0.00272","baseline_mix":"0.00272"},{"bill_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","ship_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","promo":"NONE","terms":"1G","r_currency":"US","order_date":"2025-10-29","ship_date":"2026-01-21","fc_mix":"0.00815","baseline_mix":"0.00815"},{"bill_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","ship_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","promo":"NONE","terms":"1G","r_currency":"US","order_date":"2025-10-29","ship_date":"2026-02-05","fc_mix":"0.00952","baseline_mix":"0.00952"},{"bill_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","ship_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","promo":"NONE","terms":"1G","r_currency":"US","order_date":"2025-10-29","ship_date":"2026-02-10","fc_mix":"0.00855","baseline_mix":"0.00855"},{"bill_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","ship_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","promo":"NONE","terms":"1G","r_currency":"US","order_date":"2025-10-29","ship_date":"2026-02-21","fc_mix":"0.02717","baseline_mix":"0.02717"},{"bill_cust_descr":"COST0017 - COSTA FARMS LLC-GOULDS FL","ship_cust_descr":"COST0015 - COSTA FARMS-FOLIAGE DIV (CNF)","promo":"NONE","terms":"7N","r_currency":"US","order_date":"2025-11-19","ship_date":"2025-11-25","fc_mix":"0.02989","baseline_mix":"0.02989"},{"bill_cust_descr":"COST0017 - COSTA FARMS LLC-GOULDS FL","ship_cust_descr":"COST0015 - COSTA FARMS-FOLIAGE DIV (CNF)","promo":"NONE","terms":"7N","r_currency":"US","order_date":"2025-11-19","ship_date":"2025-12-06","fc_mix":"0.00272","baseline_mix":"0.00272"},{"bill_cust_descr":"COST0017 - COSTA FARMS LLC-GOULDS FL","ship_cust_descr":"COST0007 - COSTA COLOR-MIAMI FL","promo":"NONE","terms":"7N","r_currency":"US","order_date":"2025-11-25","ship_date":"2025-11-27","fc_mix":"0.02174","baseline_mix":"0.02174"},{"bill_cust_descr":"COST0017 - COSTA FARMS LLC-GOULDS FL","ship_cust_descr":"COST0046 - COSTA LAYMAN, LLC","promo":"NONE","terms":"7N","r_currency":"US","order_date":"2025-11-27","ship_date":"2026-01-27","fc_mix":"0.05191","baseline_mix":"0.05191"},{"bill_cust_descr":"COST0017 - COSTA FARMS LLC-GOULDS FL","ship_cust_descr":"COST0046 - COSTA LAYMAN, LLC","promo":"NONE","terms":"7N","r_currency":"US","order_date":"2025-11-27","ship_date":"2026-01-29","fc_mix":"0.02595","baseline_mix":"0.02595"},{"bill_cust_descr":"COST0017 - COSTA FARMS LLC-GOULDS FL","ship_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","promo":"NONE","terms":30,"r_currency":"US","order_date":"2026-01-03","ship_date":"2026-01-07","fc_mix":"0.00052","baseline_mix":"0.00052"},{"bill_cust_descr":"COST0017 - COSTA FARMS LLC-GOULDS FL","ship_cust_descr":"COST0007 - COSTA COLOR-MIAMI FL","promo":"NONE","terms":"7N","r_currency":"US","order_date":"2026-01-28","ship_date":"2026-02-05","fc_mix":"0.00086","baseline_mix":"0.00086"},{"bill_cust_descr":"COST0017 - COSTA FARMS LLC-GOULDS FL","ship_cust_descr":"COST0002 - COSTA CAROLINA-LEICESTER NC","promo":"NONE","terms":"7N","r_currency":"US","order_date":"2026-01-28","ship_date":"2026-02-18","fc_mix":"0.00472","baseline_mix":"0.00472"},{"bill_cust_descr":"COST0017 - COSTA FARMS LLC-GOULDS FL","ship_cust_descr":"COST0046 - COSTA LAYMAN, LLC","promo":"NONE","terms":"7N","r_currency":"US","order_date":"2026-02-06","ship_date":"2026-03-05","fc_mix":"0.00708","baseline_mix":"0.00708"},{"bill_cust_descr":"COST0017 - COSTA FARMS LLC-GOULDS FL","ship_cust_descr":"COST0046 - COSTA LAYMAN, LLC","promo":"NONE","terms":"7N","r_currency":"US","order_date":"2026-02-06","ship_date":"2026-03-06","fc_mix":"0.0118","baseline_mix":"0.0118"},{"bill_cust_descr":"COST0017 - COSTA FARMS LLC-GOULDS FL","ship_cust_descr":"COST0007 - COSTA COLOR-MIAMI FL","promo":"NONE","terms":"7N","r_currency":"US","order_date":"2026-02-12","ship_date":"2026-02-17","fc_mix":"0.00272","baseline_mix":"0.00272"},{"bill_cust_descr":"COST0017 - COSTA FARMS LLC-GOULDS FL","ship_cust_descr":"COST0002 - COSTA CAROLINA-LEICESTER NC","promo":"NONE","terms":"7N","r_currency":"US","order_date":"2026-02-12","ship_date":"2026-02-18","fc_mix":"0.00272","baseline_mix":"0.00272"},{"bill_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","ship_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","promo":"NONE","terms":"1G","r_currency":"US","order_date":"2026-02-25","ship_date":"2026-02-28","fc_mix":"0.00946","baseline_mix":"0.00946"},{"bill_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","ship_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","promo":"NONE","terms":"1G","r_currency":"US","order_date":"2026-02-25","ship_date":"2026-03-14","fc_mix":"0.02869","baseline_mix":"0.02869"},{"bill_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","ship_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","promo":"NONE","terms":"1G","r_currency":"US","order_date":"2026-02-28","ship_date":"2026-03-17","fc_mix":"0.00708","baseline_mix":"0.00708"},{"bill_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","ship_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","promo":"NONE","terms":"1G","r_currency":"US","order_date":"2026-03-03","ship_date":"2026-03-10","fc_mix":"0.00236","baseline_mix":"0.00236"},{"bill_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","ship_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","promo":"NONE","terms":"1G","r_currency":"US","order_date":"2026-03-03","ship_date":"2026-03-24","fc_mix":"0.00317","baseline_mix":"0.00317"},{"bill_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","ship_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","promo":"NONE","terms":"1G","r_currency":"US","order_date":"2026-03-03","ship_date":"2026-03-27","fc_mix":"0.00635","baseline_mix":"0.00635"},{"bill_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","ship_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","promo":"NONE","terms":"1G","r_currency":"US","order_date":"2026-03-03","ship_date":"2026-04-01","fc_mix":"0.02989","baseline_mix":"0.02989"},{"bill_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","ship_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","promo":"NONE","terms":"1G","r_currency":"US","order_date":"2026-03-03","ship_date":"2026-04-14","fc_mix":"0.00239","baseline_mix":"0.00239"},{"bill_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","ship_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","promo":"NONE","terms":"1G","r_currency":"US","order_date":"2026-03-03","ship_date":"2026-04-21","fc_mix":"0.00779","baseline_mix":"0.00779"},{"bill_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","ship_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","promo":"NONE","terms":"1G","r_currency":"US","order_date":"2026-03-03","ship_date":"2026-04-22","fc_mix":"0.00815","baseline_mix":"0.00815"},{"bill_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","ship_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","promo":"NONE","terms":"1G","r_currency":"US","order_date":"2026-03-10","ship_date":"2026-04-28","fc_mix":"0.00895","baseline_mix":"0.00895"},{"bill_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","ship_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","promo":"NONE","terms":"1G","r_currency":"US","order_date":"2026-03-10","ship_date":"2026-04-29","fc_mix":"0.02994","baseline_mix":"0.02994"},{"bill_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","ship_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","promo":"NONE","terms":"1G","r_currency":"US","order_date":"2026-03-10","ship_date":"2026-05-05","fc_mix":"0.00026","baseline_mix":"0.00026"},{"bill_cust_descr":"COST0017 - COSTA FARMS LLC-GOULDS FL","ship_cust_descr":"COST0002 - COSTA CAROLINA-LEICESTER NC","promo":"NONE","terms":"7N","r_currency":"US","order_date":"2026-03-25","ship_date":"2026-05-05","fc_mix":"0.00435","baseline_mix":"0.00435"},{"bill_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","ship_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","promo":"NONE","terms":"1G","r_currency":"US","order_date":"2026-04-16","ship_date":"2026-04-17","fc_mix":"0.00185","baseline_mix":"0.00185"},{"bill_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","ship_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","promo":"NONE","terms":"1G","r_currency":"US","order_date":"2026-04-16","ship_date":"2026-05-01","fc_mix":"0.00013","baseline_mix":"0.00013"},{"bill_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","ship_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","promo":"NONE","terms":"1G","r_currency":"US","order_date":"2026-04-19","ship_date":"2026-05-02","fc_mix":"0.00944","baseline_mix":"0.00944"},{"bill_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","ship_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","promo":"NONE","terms":"1G","r_currency":"US","order_date":"2026-04-19","ship_date":"2026-06-03","fc_mix":"0.03067","baseline_mix":"0.03067"},{"bill_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","ship_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","promo":"NONE","terms":"1G","r_currency":"US","order_date":"2026-04-19","ship_date":"2026-06-11","fc_mix":"0.01902","baseline_mix":"0.01902"},{"bill_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","ship_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","promo":"NONE","terms":"1G","r_currency":"US","order_date":"2026-04-19","ship_date":"2026-06-13","fc_mix":"0.05398","baseline_mix":"0.05398"},{"bill_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","ship_cust_descr":"BATT0001 - BATTLEFIELD FARMS - COSTA","promo":"NONE","terms":"1G","r_currency":"US","order_date":"2026-04-19","ship_date":"2026-06-17","fc_mix":"0.00239","baseline_mix":"0.00239"},{"bill_cust_descr":"COST0017 - COSTA FARMS LLC-GOULDS FL","ship_cust_descr":"COST0046 - COSTA LAYMAN, LLC","promo":"NONE","terms":"7N","r_currency":"US","order_date":"2026-05-07","ship_date":"2026-07-30","fc_mix":"0.05191","baseline_mix":"0.05191"},{"bill_cust_descr":"COST0017 - COSTA FARMS LLC-GOULDS FL","ship_cust_descr":"COST0046 - COSTA LAYMAN, LLC","promo":"NONE","terms":"7N","r_currency":"US","order_date":"2026-05-07","ship_date":"2026-08-05","fc_mix":"0.00236","baseline_mix":"0.00236"},{"bill_cust_descr":"COST0017 - COSTA FARMS LLC-GOULDS FL","ship_cust_descr":"DELR0001 - COSTA DELRAY-VENUS","promo":"NONE","terms":"7N","r_currency":"US","order_date":"2026-05-30","ship_date":"2026-05-31","fc_mix":"0.05991","baseline_mix":"0.05991"}],"scenario":{"segm":["Greenhouse","Nursery","Other","Sustainable",""],"quota_rep_descr":"13028 - RICHARD MEULE","shipto_group":"COSTA","majg_descr":"320 - 320 - COEX"}}$$::json def)
,seg AS (
    SELECT
        x.GLEC glec
        ,x.SEGM segm
    FROM
        (
            VALUES
            ('1SU','Sustainable'),
            ('1CU','Sustainable'),
            ('1GR','Greenhouse'),
            ('1NU','Nursery'),
            ('1RE','Retail'),
            ('2WI','Greenhouse'),
            ('3BM','Other'),
            ('3CO','Other'),
            ('3PE','Other'),
            ('3PP','Other'),
            ('4CO','Other'),
            ('4RA','Other'),
            ('9MI','Other'),
            ('9SA','Other'),
            ('9TO','Other')
        ) X(GLEC, SEGM)
)
,copr AS (
    SELECT
        LTRIM(RTRIM(A9)) AS COMP,
        A30 AS DESCR,
        SUBSTR(C.A249,242,2) CURR,
        SUBSTR(C.A249,32,4) AS GL,
        SUBSTR(C.A249,190,4) AS AR,
        SUBSTR(C.A249,182,4) AS AP,
        SUBSTR(C.A249,198,4) AS FA,
        SUBSTR(C.A249,238,4) AS IC ,
        SUBSTR(D.A249,9,5) CONS_FUNC
    FROM
        LGDAT.CODE
        LEFT OUTER JOIN LGDAT.NAME C ON
            'C0000'||LTRIM(RTRIM(A9)) = C.A7
        LEFT OUTER JOIN LGDAT.NAME D ON
            'D0000'||LTRIM(RTRIM(A9)) = D.A7
    WHERE
        A2 = 'AA' OR A2 IS NULL
)
,parts AS (
    SELECT 
        jr.*
    FROM 
        target
        JOIN LATERAL json_array_elements(def->'parts') as ae(e) ON true
        JOIN LATERAL json_to_record(ae.e) as jr(part text, qty numeric, price numeric, pounds numeric) ON true
)
-- SELECT * FROM parts
,dates AS (
    SELECT 
        jr.*
        ,def->'scenario'->>'quota_rep_descr' quota_rep_descr
    FROM 
        target
        JOIN LATERAL json_array_elements(def->'dates') as ae(e) ON true
        JOIN LATERAL json_to_record(ae.e) as jr(
             bill_cust_descr	text
            ,ship_cust_descr	text
            ,promo	            text
            ,terms	            text
            ,r_currency	        text
            ,order_date	        date
            ,ship_date	        date
            ,fc_mix	            numeric
            ,baseline_mix       numeric
        ) ON true
)
-- SELECT * FROM dates
,basemix AS (
    SELECT
         od.fspr
        ,d.promo
        ,d.terms
        ,d.r_currency
        ,d.bill_cust_descr
        ,d.ship_cust_descr
        ,d.quota_rep_descr
        ,p.part
        ,round(d.fc_mix * p.qty,0) units
        ,round(d.fc_mix * p.qty,0)* p.price value_usd --assume that target dollars are USD
        ,d.order_date
        ,sd.sortmo order_month
        ,sd.ssyr order_season
        ,d.ship_date request_date
        ,sd.sortmo request_month
        ,sd.ssyr request_season
        ,d.ship_date
        ,sd.sortmo ship_month
        ,sd.ssyr ship_season
    FROM
        dates d
        CROSS JOIN parts p
        LEFT OUTER JOIN rlarp.gld sd ON 
            d.ship_date <@ sd.drange
        LEFT OUTER JOIN rlarp.gld od ON 
            d.order_date <@ od.drange
)
,log AS (
    INSERT INTO rlarp.osm_log(doc) SELECT $$replace_iterdef$$::jsonb doc RETURNING *
)
-- SELECT * FROM basemix
,final AS (
SELECT
    b.fspr
    ,i.dplt plnt
    ,b.promo
    ,b.terms
    ,b.bill_cust_descr
    ,b.ship_cust_descr
    ,r.rcode
    ,b.quota_rep_descr
    ,r.director
    ,b.bill_cust_descr billto_group
    ,b.ship_cust_descr shipto_group
    ,CASE SUBSTRING(bc.cclass,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(sc.cclass,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 chan
    ,CASE SUBSTRING(bc.cclass,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(sc.cclass,2,3)
                WHEN 'DIS' THEN 'WHS'
                ELSE CASE SUBSTRING(sc.cclass,1,1) WHEN 'R' THEN 'RDP' ELSE 'DRP' END
            END
        WHEN 'MAS' THEN 'RMN'
        WHEN 'NAT' THEN 'RMN'
        ELSE CASE SUBSTRING(sc.cclass,1,1) WHEN 'R' THEN 'RDI' ELSE 'DIR' END
    END chansub
    ,null::text chan_retail
    ,b.part
    ,b.part || ' - ' || i.descr
    ,i.partgroup
    ,i.branding
    ,i.majgd majg_descr
    ,i.mingd ming_descr
    ,i.majsd majs_descr
    ,i.minsd mins_descr
    ,seg.segm
    ,CASE WHEN i.majg = '610' THEN 'Fiber' ELSE 'Plastic' END substance
    ,'41010' fs_line          --master data
    ,b.r_currency          --history cust mix
    ,rx.rate r_rate          --master data
    ,pl.curr c_currency          --master data
    ,cx.rate c_rate          --master data
    ,round(b.units                                                            ,2) units
    ,round((b.value_usd / COALESCE(rx.rate,1))::numeric                       ,2) value_loc --b.value is denominated in USD, need to apply currency to get to local, assume 1 if using a fake customer
    ,round(b.value_usd                                                        ,2) value_usd --b.value is already denominated in usd
    ,round((i.futstd*b.units)::numeric        ,2) cost_loc
    ,round((i.futstdus*b.units)::numeric,2) cost_usd
    ,'CLOSED' calc_status
    ,'SHIPMENT' flag
    ,b.order_date
    ,b.order_month
    ,b.order_season
    ,b.request_date
    ,b.request_month
    ,b.request_season
    ,b.ship_date
    ,b.ship_month
    ,b.ship_season
    ,'replace_version' "version"
    ,'replace_source'||' volume'  iter
    ,log.id
    ,COALESCE(log.doc->>'tag','') "tag"
    ,log.doc->>'message' "comment"
    ,log.doc->>'type' module
    ,round(b.units * i.nwht * CASE i.nwun WHEN 'KG' THEN 2.2046 ELSE 1 END, 2) pounds
FROM 
    basemix b
    LEFT OUTER JOIN rlarp.repc r ON 
        r.repp = b.quota_rep_descr
    LEFT OUTER JOIN "CMS.CUSLG".itemm i ON 
        i.item = b.part
    LEFT OUTER JOIN rlarp.cust bc ON 
        bc.code = rtrim(substring(b.bill_cust_descr,1,8))
    LEFT OUTER JOIN rlarp.cust sc ON 
        sc.code = rtrim(substring(b.bill_cust_descr,1,8))
    LEFT OUTER JOIN seg ON 
        seg.glec = i.glec
    LEFT OUTER JOIN rlarp.plpr pl ON 
        pl.plnt = i.dplt
    LEFT OUTER JOIN rlarp.ffcret cx ON
        cx.perd = pl.gl AND
        cx.rtyp = 'MA' AND
        cx.fcur = pl.curr AND
        cx.tcur = 'US'
    LEFT OUTER JOIN rlarp.ffcret rx ON
        rx.perd = pl.gl AND
        rx.rtyp = 'MA' AND
        rx.fcur = b.r_currency AND
        rx.tcur = 'US'
)
select * from final
