diff --git a/index.js b/index.js index 5931139..502cef8 100644 --- a/index.js +++ b/index.js @@ -121,9 +121,9 @@ server.get('/scale', bodyParser.json(), function(req, res) { console.log(JSON.stringify(req.body)); //parse the where clause into the main sql statement //sql = sql.replace(new RegExp("where_clause", 'g'), w) - sql = sql.replace(new RegExp("app_pincr", 'g'), app_pincr); + //sql = sql.replace(new RegExp("app_pincr", 'g'), app_pincr); sql = sql.replace(new RegExp("app_req", 'g'), app_req); - sql = sql.replace(new RegExp("app_vincr", 'g'), app_vincr); + //sql = sql.replace(new RegExp("app_vincr", 'g'), app_vincr); sql = sql.replace(new RegExp("app_where", 'g'), w); //execute the sql and send the result args.push(req.body.app_baseline_from_date); diff --git a/routes/scale/gen_scale.sql b/routes/scale/gen_scale.sql index 1d9eb97..ade8420 100644 --- a/routes/scale/gen_scale.sql +++ b/routes/scale/gen_scale.sql @@ -77,7 +77,14 @@ WHERE SELECT ---------$$app_req$$ will hold the request body-------------------- $$WITH -req AS (SELECT $$||'$$app_req$$::jsonb)'||$$ +req AS (SELECT $$||'$$app_req$$::jsonb j)'||$$ +,target AS ( + SELECT + (req.j->>'vincr')::numeric vincr --volume + ,(req.j->>'pincr')::numeric pincr --price + FROM + req +) -----this block is supposed to test for new products that might not be in baseline etc------- ,test AS ( SELECT @@ -98,9 +105,9 @@ WHERE ), vscale AS ( SELECT - app_vincr AS target_increment + (SELECT vincr FROM target) AS target_increment ,sum($$||_units_col||') AS units'||$$ - ,app_vincr/sum($$||_units_col||$$) AS factor + ,(SELECT vincr FROM target)/sum($$||_units_col||$$) AS factor FROM basemix ) @@ -113,19 +120,19 @@ FROM ) ,pscale AS ( SELECT - app_pincr AS target_increment + (SELECT pincr FROM target) AS target_increment ,sum($$||_value_col||') AS value'||$$ ,CASE WHEN (SELECT sum($$||_value_col||$$) FROM volume) = 0 THEN --if the base value is -0- scaling will not work, need to generate price, factor goes to -0- 0 ELSE --if the target dollar value still does not match the target increment, make this adjustment - ((SELECT pincr::numeric FROM target)-(SELECT sum($$||_value_col||$$) FROM volume))/(SELECT sum($$||_value_col||$$) FROM volume) + ((SELECT pincr FROM target)-(SELECT sum($$||_value_col||$$) FROM volume))/(SELECT sum($$||_value_col||$$) FROM volume) END factor ,CASE WHEN (SELECT sum($$||_value_col||$$) FROM volume) = 0 THEN CASE WHEN ((SELECT pincr::numeric FROM target) - (SELECT sum($$||_value_col||$$) 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($$||_value_col||$$) FROM volume))/(SELECT sum(units) FROM volume) + ((SELECT pincr::numeric FROM target) - (SELECT sum($$||_value_col||$$) FROM volume))/(SELECT sum($$||_units_col||$$) FROM volume) ELSE 0 END @@ -138,6 +145,11 @@ FROM ,pricing AS ( SELECT $$||_clist_prc||$$ +FROM + volume o + CROSS JOIN pscale +WHERE + pscale.factor <> 0 or pscale.mod_price <> 0 ) INSERT INTO fc.live diff --git a/routes/scale/generate_route_sql.sh b/routes/scale/generate_route_sql.sh index cc6149e..e53dd62 100755 --- a/routes/scale/generate_route_sql.sh +++ b/routes/scale/generate_route_sql.sh @@ -1,4 +1,4 @@ # execure the sql for scale which builds the sql and inserts into a table -$PGD -f routes/scale/gen_scale.sql +$PGD -f gen_scale.sql # pull the sql out of the table and write it to route directory -$PGD -c "SELECT t FROM fc.sql WHERE cmd = 'scale'" -t -A -o routes/scale/scale_final.sql +$PGD -c "SELECT t FROM fc.sql WHERE cmd = 'scale'" -t -A -o scale.sql diff --git a/routes/scale/scale.sql b/routes/scale/scale.sql index 59c646c..dccc721 100644 --- a/routes/scale/scale.sql +++ b/routes/scale/scale.sql @@ -1,5 +1,12 @@ WITH -req AS (SELECT $$app_req$$::jsonb) +req AS (SELECT $$app_req$$::jsonb j) +,target AS ( + SELECT + (req.j->>'vincr')::numeric vincr --volume + ,(req.j->>'pincr')::numeric pincr --price + FROM + req +) -----this block is supposed to test for new products that might not be in baseline etc------- ,test AS ( SELECT @@ -120,9 +127,9 @@ WHERE ), vscale AS ( SELECT - app_vincr AS target_increment + (SELECT vincr FROM target) AS target_increment ,sum(fb_qty) AS units - ,app_vincr/sum(fb_qty) AS factor + ,(SELECT vincr FROM target)/sum(fb_qty) AS factor FROM basemix ) @@ -235,19 +242,19 @@ FROM ) ,pscale AS ( SELECT - app_pincr AS target_increment + (SELECT pincr FROM target) AS target_increment ,sum(fb_val_loc) AS value ,CASE WHEN (SELECT sum(fb_val_loc) FROM volume) = 0 THEN --if the base value is -0- scaling will not work, need to generate price, factor goes to -0- 0 ELSE --if the target dollar value still does not match the target increment, make this adjustment - ((SELECT pincr::numeric FROM target)-(SELECT sum(fb_val_loc) FROM volume))/(SELECT sum(fb_val_loc) FROM volume) + ((SELECT pincr FROM target)-(SELECT sum(fb_val_loc) FROM volume))/(SELECT sum(fb_val_loc) FROM volume) END factor ,CASE WHEN (SELECT sum(fb_val_loc) FROM volume) = 0 THEN CASE WHEN ((SELECT pincr::numeric FROM target) - (SELECT sum(fb_val_loc) 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) FROM volume))/(SELECT sum(units) FROM volume) + ((SELECT pincr::numeric FROM target) - (SELECT sum(fb_val_loc) FROM volume))/(SELECT sum(fb_qty) FROM volume) ELSE 0 END @@ -360,6 +367,11 @@ SELECT ,o.rseas ,o.sdate ,o.sseas +FROM + volume o + CROSS JOIN pscale +WHERE + pscale.factor <> 0 or pscale.mod_price <> 0 ) INSERT INTO fc.live