From 1df3ff1adb2f4435f2196098905c83bde4b253d8 Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Tue, 24 Oct 2023 16:22:00 -0400 Subject: [PATCH] add forecast notes --- api.ts | 4 ++-- sql/excel_query.sql | 29 +++++++++++++++-------------- sql/schema.pg.sql | 32 +++++++++++++++++++------------- sql/write_note.sql | 7 ++++--- 4 files changed, 40 insertions(+), 32 deletions(-) diff --git a/api.ts b/api.ts index f5294da..88c4f41 100644 --- a/api.ts +++ b/api.ts @@ -76,8 +76,8 @@ router.post('/sales_walk/flag_cust', async (ctx) => { if (ctx.request.hasBody) { const body = JSON.parse(bodyText); console.log("Body JSON:", body); - const { bill_cust, ship_cust, bucket, attainment, notes } = body; // Destructure the needed values from the JSON - const result = await client.queryObject({args: [bill_cust, ship_cust, bucket, attainment, notes], text: query} ); + const { bill_cust, ship_cust, bucket, attainment, notes , fcnotes} = body; // Destructure the needed values from the JSON + const result = await client.queryObject({args: [bill_cust, ship_cust, bucket, attainment, notes, fcnotes], text: query} ); } } catch (error) { diff --git a/sql/excel_query.sql b/sql/excel_query.sql index 4c33415..aea1893 100644 --- a/sql/excel_query.sql +++ b/sql/excel_query.sql @@ -1,18 +1,19 @@ SELECT - sw.bill_dba, - sw.ship_dba, - sw.dsm, - sw.budget_2024, - sw.shipments_2023, - sw.shipments_2024, - sw.open_orders_2024, - sw.quotes, - --(shipments_2024 + open_orders_2024) - shipments_2023 yoy_delta, - (shipments_2024 + open_orders_2024) - budget_2024 yoy_delta, - sw.flag, - sw.bucket, - sw.attainment, - sw.notes + sw.bill_dba "Bill-To", + sw.ship_dba "Ship-To", + sw.dsm "DSM", + sw.budget_2024 "2024 Budget", + sw.shipments_2023 "2023 Shipments", + sw.shipments_2024 "2024 Shipments", + sw.open_orders_2024 "Open Orders", + sw.quotes "Quotes", + (shipments_2024 + open_orders_2024) - budget_2024 "Budget Delta", + (shipments_2024 + open_orders_2024) - shipments_2023 "YoY Delta", + sw.flag "Flag", + sw.bucket "Bucket", + sw.attainment "'25 Fcst vs '24 Bdgt - %", + sw.notes "YoY Notes", + sw.fcnotes "'25 Forecast Notes" FROM rlarp.sales_walk_r1 sw WHERE diff --git a/sql/schema.pg.sql b/sql/schema.pg.sql index 8f5c469..3e289e3 100644 --- a/sql/schema.pg.sql +++ b/sql/schema.pg.sql @@ -9,11 +9,12 @@ sales_walk_seg CREATE TABLE IF NOT EXISTS rlarp.walk_r1 ( - bill_cust text - ,ship_cust text - ,bucket text + bill_cust text + ,ship_cust text + ,bucket text ,attainment numeric - ,notes text + ,notes text + ,fcnotes text ,PRIMARY KEY (bill_cust, ship_cust) ); @@ -90,11 +91,11 @@ GROUP BY COALESCE(act.bill_dba,TRIM(bgt.bill_dba)) bill_dba ,COALESCE(act.ship_dba,TRIM(bgt.ship_dba)) ship_dba ,COALESCE(act.dsm ,TRIM(bgt.dsm )) dsm - ,SUM(COALESCE(bgt.pounds ,0)) "Budget 2024" - ,SUM(COALESCE(act."Actual 2023",0)) "Actual 2023" - ,SUM(COALESCE(act."Actual 2024",0)) "Actual 2024" - ,SUM(COALESCE(act."Open Ord" ,0)) "Open Ord" - ,SUM(COALESCE(act."Quotes" ,0)) "Quotes" + ,COALESCE(SUM(bgt.pounds ),0) "Budget 2024" + ,COALESCE(SUM(act."Actual 2023"),0) "Actual 2023" + ,COALESCE(SUM(act."Actual 2024"),0) "Actual 2024" + ,COALESCE(SUM(act."Open Ord" ),0) "Open Ord" + ,COALESCE(SUM(act."Quotes" ),0) "Quotes" FROM act FULL OUTER JOIN bgt ON @@ -124,7 +125,7 @@ SELECT ,"Quotes" quotes ,CASE WHEN "Actual 2023" > 0 THEN CASE - WHEN "Actual 2024" = 0 AND "Open Ord" = 0 AND "Quotes" = 0 THEN 'No Activity' + WHEN "Actual 2024" = 0 AND "Open Ord" = 0 AND "Quotes" = 0 THEN 'Not Quoted' WHEN "Actual 2024" = 0 AND "Open Ord" = 0 AND "Quotes" > 0 THEN 'Quoted Only' WHEN ("Actual 2024" + "Open Ord") < "Actual 2023" THEN 'Reduced' WHEN ("Actual 2024" + "Open Ord") >= "Actual 2023" THEN 'Increased' @@ -133,11 +134,13 @@ SELECT CASE WHEN "Actual 2024" = 0 AND "Open Ord" = 0 AND "Quotes" > 0 THEN 'Incremental Quoted' WHEN "Actual 2024" > 0 OR "Open Ord" > 0 THEN 'Incremental Won' + WHEN "Actual 2024" = 0 AND "Open Ord" = 0 AND "Quotes" = 0 THEN 'Not Quoted' END END flag ,COALESCE(w.bucket,a.bucket,'None') bucket ,COALESCE(w.attainment,0) attainment ,COALESCE(w.notes,a.notes,'-') notes + ,COALESCE(w.fcnotes,'-') fcnotes FROM rlarp.sales_walk_r1_agg agg LEFT OUTER JOIN rlarp.walk a ON @@ -255,9 +258,9 @@ SELECT ,"Open Ord" open_orders_2024 ,"Quotes" quotes ,"Budget 2024" * COALESCE(w.attainment,0) available_to_win - ,CASE WHEN "Actual 2023" > 0 + ,CASE WHEN COALESCE("Actual 2023",0) > 0 THEN CASE - WHEN "Actual 2024" = 0 AND "Open Ord" = 0 AND "Quotes" = 0 THEN 'No Activity' + WHEN "Actual 2024" = 0 AND "Open Ord" = 0 AND "Quotes" = 0 THEN 'Not Quoted' WHEN "Actual 2024" = 0 AND "Open Ord" = 0 AND "Quotes" > 0 THEN 'Quoted Only' WHEN ("Actual 2024" + "Open Ord") < "Actual 2023" THEN 'Reduced' WHEN ("Actual 2024" + "Open Ord") >= "Actual 2023" THEN 'Increased' @@ -266,12 +269,15 @@ SELECT CASE WHEN "Actual 2024" = 0 AND "Open Ord" = 0 AND "Quotes" > 0 THEN 'Incremental Quoted' WHEN "Actual 2024" > 0 OR "Open Ord" > 0 THEN 'Incremental Won' + WHEN "Actual 2024" = 0 AND "Open Ord" = 0 AND "Quotes" = 0 THEN 'Not Quoted' + ELSE 'Not Quoted' END END flag ,COALESCE(w.bucket,a.bucket,'None') bucket ,COALESCE(w.attainment,0) attainment ,COALESCE(w.notes,a.notes,'-') notes - ,sw.flag flag_cust + ,COALESCE(w.fcnotes,'-') fcnotes + ,COALESCE(sw.flag,'Not Quoted') flag_cust FROM rlarp.sales_walk_r1_seg_agg agg LEFT OUTER JOIN rlarp.walk a ON diff --git a/sql/write_note.sql b/sql/write_note.sql index 8d918ad..6fbb95f 100644 --- a/sql/write_note.sql +++ b/sql/write_note.sql @@ -1,13 +1,14 @@ MERGE INTO rlarp.walk_r1 w USING - ( SELECT $1 bill_cust, $2 ship_cust, $3 bucket, $4 attainment, $5 notes) as i ON + ( SELECT $1 bill_cust, $2 ship_cust, $3 bucket, $4 attainment, $5 notes, $6 fcnotes) as i ON i.ship_cust = w.ship_cust AND i.bill_cust = w.bill_cust WHEN MATCHED THEN UPDATE SET bucket = i.bucket ,attainment = i.attainment::numeric ,notes = i.notes + ,fcnotes = i.fcnotes WHEN NOT MATCHED THEN - INSERT (bill_cust, ship_cust, bucket, attainment, notes) - VALUES (bill_cust,i.ship_cust, i.bucket, i.attainment::numeric, i.notes ) + INSERT (bill_cust, ship_cust, bucket, attainment, notes, fcnotes) + VALUES (bill_cust,i.ship_cust, i.bucket, i.attainment::numeric, i.notes , i.fcnotes)