diff --git a/route_sql/addmonth_vd.sql b/route_sql/addmonth_vd.sql index 712bd88..939a5a7 100644 --- a/route_sql/addmonth_vd.sql +++ b/route_sql/addmonth_vd.sql @@ -11,7 +11,10 @@ target AS (select target_increment incr) SELECT sum(fb_qty) tot ,sum(fb_qty) FILTER (WHERE iter = 'copy') base - ,sum(fb_qty) FILTER (WHERE iterdef->>'type' = 'new basket') newpart + ,COALESCE(sum(fb_qty) FILTER (WHERE iterdef->>'type' = 'new basket'),0) newpart + ,sum(fb_val_loc *r_rate) totsales + ,sum(fb_val_loc *r_rate) FILTER (WHERE iter = 'copy') basesales + ,COALESCE(sum(fb_val_loc *r_rate) FILTER (WHERE iterdef->>'type' = 'new basket'),0) newpartsales FROM rlarp.osm_fcpool WHERE @@ -40,6 +43,19 @@ target AS (select target_increment incr) ELSE 'scale all' END flag + ,CASE WHEN totsales = 0 THEN + CASE WHEN basesales = 0 THEN + CASE WHEN newpartsales = 0 THEN + 'no price' + ELSE + 'scale new part' + END + ELSE + 'scale copy' + END + ELSE + 'scale all' + END flagsales FROM testv ) @@ -92,7 +108,7 @@ target AS (select target_increment incr) ,orderdate ,requestdate ,shipdate - ,sum(value_usd) value_usd + ,sum(CASE (SELECT flagsales FROM flagv) WHEN 'no price'THEN 1.0 ELSE value_usd END) value_usd FROM rlarp.osm_fcpool LEFT OUTER JOIN mseq ON @@ -118,7 +134,7 @@ target AS (select target_increment incr) ,requestdate ,shipdate HAVING - sum(value_usd) <> 0 + sum(CASE (SELECT flagsales FROM flagv) WHEN 'no price'THEN 1.0 ELSE value_usd END) <> 0 ) --select * from alldates ,dom AS ( diff --git a/route_sql/addmonth_vupd.sql b/route_sql/addmonth_vupd.sql index a876885..035a3d2 100644 --- a/route_sql/addmonth_vupd.sql +++ b/route_sql/addmonth_vupd.sql @@ -3,6 +3,58 @@ WITH the volume must be expressed in terms of units, since that is what it will be scaling */ target AS (select target_volume vincr, target_price pincr) +,testv AS ( + SELECT + sum(fb_qty) tot + ,sum(fb_qty) FILTER (WHERE iter = 'copy') base + ,COALESCE(sum(fb_qty) FILTER (WHERE iterdef->>'type' = 'new basket'),0) newpart + ,sum(fb_val_loc *r_rate) totsales + ,sum(fb_val_loc *r_rate) FILTER (WHERE iter = 'copy') basesales + ,COALESCE(sum(fb_val_loc *r_rate) FILTER (WHERE iterdef->>'type' = 'new basket'),0) newpartsales + FROM + rlarp.osm_fcpool + WHERE + -----------------scenario---------------------------- + where_clause + -----------------additional params------------------- + AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments + AND adj_orderdate <= adj_shipdate + +) +,flagv AS ( + SELECT + tot + ,base + ,newpart + ,CASE WHEN tot = 0 THEN + CASE WHEN base = 0 THEN + CASE WHEN newpart = 0 THEN + 'unclean data. tested -> does not exist' + ELSE + 'scale new part' + END + ELSE + 'scale copy' + END + ELSE + 'scale all' + END flag + ,CASE WHEN totsales = 0 THEN + CASE WHEN basesales = 0 THEN + CASE WHEN newpartsales = 0 THEN + 'no price' + ELSE + 'scale new part' + END + ELSE + 'scale copy' + END + ELSE + 'scale all' + END flagsales + FROM + testv +) ,GLD AS ( SELECT N1COMP COMP @@ -52,7 +104,7 @@ target AS (select target_volume vincr, target_price pincr) ,orderdate ,requestdate ,shipdate - ,sum(value_usd) value_usd + ,sum(CASE (SELECT flagsales FROM flagv) WHEN 'no price'THEN 1.0 ELSE value_usd END) value_usd FROM rlarp.osm_fcpool LEFT OUTER JOIN mseq ON @@ -61,6 +113,11 @@ target AS (select target_volume vincr, target_price pincr) -----------------scenario---------------------------- where_clause -----------------additional params------------------- + AND CASE (SELECT flag FROM flagv) + WHEN 'scale all' THEN true + WHEN 'scale copy' THEN iter = 'copy' + WHEN 'scale new part' THEN iterdef->>'type' = 'new basket' + END AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments AND adj_orderdate <= adj_shipdate GROUP BY @@ -72,7 +129,7 @@ target AS (select target_volume vincr, target_price pincr) ,requestdate ,shipdate HAVING - sum(value_usd) <> 0 + sum(CASE (SELECT flagsales FROM flagv) WHEN 'no price'THEN 1.0 ELSE value_usd END) <> 0 ) --select * from alldates ,dom AS ( @@ -245,6 +302,11 @@ SELECT -----------------scenario---------------------------- where_clause -----------------additional params------------------- + AND CASE (SELECT flag FROM flagv) + WHEN 'scale all' THEN true + WHEN 'scale copy' THEN iter = 'copy' + WHEN 'scale new part' THEN iterdef->>'type' = 'new basket' + END AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments AND adj_orderdate <= adj_shipdate GROUP BY @@ -445,8 +507,21 @@ WHERE SELECT (SELECT pincr::numeric FROM target) incr ,(SELECT sum(fb_val_loc * r_rate) FROM volume) base - --(target_sales - current_state)/current state = adjustment factor - ,((SELECT pincr::numeric FROM target) - (SELECT sum(fb_val_loc * r_rate) FROM volume))/(SELECT sum(fb_val_loc * r_rate) FROM volume) factor + ,CASE WHEN (SELECT sum(fb_val_loc * r_rate) FROM volume) = 0 THEN + 0 + ELSE + ((SELECT pincr::numeric FROM target) - (SELECT sum(fb_val_loc * r_rate) FROM volume))/(SELECT sum(fb_val_loc * r_rate) FROM volume) + END factor + ,CASE WHEN (SELECT sum(fb_val_loc * r_rate) FROM volume) = 0 THEN + CASE WHEN ((SELECT pincr::numeric FROM target) - (SELECT sum(fb_val_loc * r_rate) FROM volume)) <> 0 THEN + --if the base value is -0- but the target value hasn't been achieved, derive a price to apply + ((SELECT pincr::numeric FROM target) - (SELECT sum(fb_val_loc * r_rate) FROM volume))/(SELECT sum(fb_qty) FROM volume) + ELSE + 0 + END + ELSE + 0 + END mod_price ) ,price AS ( SELECT @@ -552,14 +627,16 @@ SELECT ,b.mod_chansub ,b.quota_rep_descr ,b.director_descr - ,b.fb_val_loc*p.factor value_loc - ,b.fb_val_loc*p.factor*r_rate value_usd + ,(CASE WHEN p.factor = 0 THEN b.fb_qty * p.mod_price/b.r_rate ELSE b.fb_val_loc*p.factor END)::numeric value_loc + ,(CASE WHEN p.factor = 0 THEN b.fb_qty * p.mod_price ELSE b.fb_val_loc*p.factor END)::numeric value_usd ,0 cost_loc ,0 cost_usd ,0 units FROM volume b CROSS JOIN pscale p + WHERE + p.factor <> 0 or p.mod_price <> 0 ) , ins AS ( INSERT INTO rlarp.osm_fcpool (SELECT * FROM price UNION ALL SELECT * FROM volume) RETURNING *