From 833ec511c1880337cec9573db60982ff238c26b0 Mon Sep 17 00:00:00 2001 From: fleetside72 Date: Wed, 3 Apr 2019 03:00:38 -0400 Subject: [PATCH] handle price change on no base sales --- route_sql/scale_pd.sql | 24 +++++++++++--- route_sql/scale_vupd.sql | 67 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 82 insertions(+), 9 deletions(-) diff --git a/route_sql/scale_pd.sql b/route_sql/scale_pd.sql index 0c6110e..88af892 100644 --- a/route_sql/scale_pd.sql +++ b/route_sql/scale_pd.sql @@ -193,8 +193,22 @@ SELECT ,scale AS ( SELECT (SELECT incr::numeric FROM target) incr - ,(SELECT sum(fb_val_loc *r_rate) FROM basemix) base - ,(SELECT incr::numeric FROM target)/(SELECT sum(fb_val_loc *r_rate) FROM basemix) factor + ,(SELECT sum(fb_val_loc * r_rate) FROM basemix) base + ,CASE WHEN (SELECT sum(fb_val_loc * r_rate) FROM basemix) = 0 THEN + 0 + ELSE + ((SELECT incr::numeric FROM target) - (SELECT sum(fb_val_loc * r_rate) FROM basemix))/(SELECT sum(fb_val_loc * r_rate) FROM basemix) + END factor + ,CASE WHEN (SELECT sum(fb_val_loc * r_rate) FROM basemix) = 0 THEN + CASE WHEN ((SELECT incr::numeric FROM target) - (SELECT sum(fb_val_loc * r_rate) FROM basemix)) <> 0 THEN + --if the base value is -0- but the target value hasn't been achieved, derive a price to apply + ((SELECT incr::numeric FROM target) - (SELECT sum(fb_val_loc * r_rate) FROM basemix))/(SELECT sum(fb_qty) FROM basemix) + ELSE + 0 + END + ELSE + 0 + END mod_price ) ,final AS ( SELECT @@ -301,14 +315,16 @@ SELECT ,b.mod_chansub ,b.quota_rep_descr ,b.director_descr - ,(b.fb_val_loc*s.factor)::numeric value_loc - ,(b.fb_val_loc*s.factor*r_rate)::numeric value_usd + ,(CASE WHEN s.factor = 0 THEN b.fb_qty * s.mod_price/b.r_rate ELSE b.fb_val_loc*s.factor END)::numeric value_loc + ,(CASE WHEN s.factor = 0 THEN b.fb_qty * s.mod_price ELSE b.fb_val_loc*s.factor END)::numeric value_usd ,0 cost_loc ,0 cost_usd ,0 units FROM basemix b CROSS JOIN scale s +WHERE + s.factor <> 0 or s.mod_price <> 0 ) --select sum(value_usd), count(*) from final , ins AS ( diff --git a/route_sql/scale_vupd.sql b/route_sql/scale_vupd.sql index 16e73cc..4559bf2 100644 --- a/route_sql/scale_vupd.sql +++ b/route_sql/scale_vupd.sql @@ -1,5 +1,41 @@ WITH target AS (select target_vol vincr, target_prc pincr) +,testv AS ( + SELECT + sum(fb_qty) tot + ,sum(fb_qty) FILTER (WHERE iter = 'copy') base + ,sum(fb_qty) FILTER (WHERE iterdef->>'type' = 'new basket') newpart + 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 + FROM + testv +) ,basemix AS ( SELECT plnt ---master data @@ -76,8 +112,8 @@ target AS (select target_vol vincr, target_prc pincr) ,adj_orderdate --history ,adj_requestdate --history ,adj_shipdate --history - ,'b20' "version" --calculated - ,'adjustment' iter --calculated + ,null::text "version" --calculated + ,null::text iter --calculated ---------------ui columns------------------------- ,order_season ,order_month @@ -114,6 +150,11 @@ target AS (select target_vol vincr, target_prc 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 @@ -315,7 +356,21 @@ target AS (select target_vol vincr, target_prc pincr) SELECT (SELECT pincr::numeric FROM target) incr ,(SELECT sum(fb_val_loc * r_rate) FROM volume) base - ,((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 ) --select * from pscale ,pricing AS ( @@ -423,14 +478,16 @@ target AS (select target_vol vincr, target_prc pincr) ,b.mod_chansub ,b.quota_rep_descr ,b.director_descr - ,(b.fb_val_loc*s.factor)::numeric value_loc - ,(b.fb_val_loc*s.factor*r_rate)::numeric value_usd + ,(CASE WHEN s.factor = 0 THEN b.fb_qty * s.mod_price/b.r_rate ELSE b.fb_val_loc*s.factor END)::numeric value_loc + ,(CASE WHEN s.factor = 0 THEN b.fb_qty * s.mod_price ELSE b.fb_val_loc*s.factor END)::numeric value_usd ,0 cost_loc ,0 cost_usd ,0 units FROM volume b CROSS JOIN pscale s + WHERE + s.factor <> 0 or s.mod_price <> 0 ) --select sum(value_usd), sum(fb_qty) from pricing , ins AS (