add forecast notes

This commit is contained in:
Paul Trowbridge 2023-10-24 16:22:00 -04:00
parent 26ad456d52
commit 1df3ff1adb
4 changed files with 40 additions and 32 deletions

4
api.ts
View File

@ -76,8 +76,8 @@ router.post('/sales_walk/flag_cust', async (ctx) => {
if (ctx.request.hasBody) { if (ctx.request.hasBody) {
const body = JSON.parse(bodyText); const body = JSON.parse(bodyText);
console.log("Body JSON:", body); console.log("Body JSON:", body);
const { bill_cust, ship_cust, bucket, attainment, notes } = body; // Destructure the needed values from the JSON 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], text: query} ); const result = await client.queryObject({args: [bill_cust, ship_cust, bucket, attainment, notes, fcnotes], text: query} );
} }
} catch (error) { } catch (error) {

View File

@ -1,18 +1,19 @@
SELECT SELECT
sw.bill_dba, sw.bill_dba "Bill-To",
sw.ship_dba, sw.ship_dba "Ship-To",
sw.dsm, sw.dsm "DSM",
sw.budget_2024, sw.budget_2024 "2024 Budget",
sw.shipments_2023, sw.shipments_2023 "2023 Shipments",
sw.shipments_2024, sw.shipments_2024 "2024 Shipments",
sw.open_orders_2024, sw.open_orders_2024 "Open Orders",
sw.quotes, sw.quotes "Quotes",
--(shipments_2024 + open_orders_2024) - shipments_2023 yoy_delta, (shipments_2024 + open_orders_2024) - budget_2024 "Budget Delta",
(shipments_2024 + open_orders_2024) - budget_2024 yoy_delta, (shipments_2024 + open_orders_2024) - shipments_2023 "YoY Delta",
sw.flag, sw.flag "Flag",
sw.bucket, sw.bucket "Bucket",
sw.attainment, sw.attainment "'25 Fcst vs '24 Bdgt - %",
sw.notes sw.notes "YoY Notes",
sw.fcnotes "'25 Forecast Notes"
FROM FROM
rlarp.sales_walk_r1 sw rlarp.sales_walk_r1 sw
WHERE WHERE

View File

@ -14,6 +14,7 @@ CREATE TABLE IF NOT EXISTS rlarp.walk_r1 (
,bucket text ,bucket text
,attainment numeric ,attainment numeric
,notes text ,notes text
,fcnotes text
,PRIMARY KEY (bill_cust, ship_cust) ,PRIMARY KEY (bill_cust, ship_cust)
); );
@ -90,11 +91,11 @@ GROUP BY
COALESCE(act.bill_dba,TRIM(bgt.bill_dba)) bill_dba COALESCE(act.bill_dba,TRIM(bgt.bill_dba)) bill_dba
,COALESCE(act.ship_dba,TRIM(bgt.ship_dba)) ship_dba ,COALESCE(act.ship_dba,TRIM(bgt.ship_dba)) ship_dba
,COALESCE(act.dsm ,TRIM(bgt.dsm )) dsm ,COALESCE(act.dsm ,TRIM(bgt.dsm )) dsm
,SUM(COALESCE(bgt.pounds ,0)) "Budget 2024" ,COALESCE(SUM(bgt.pounds ),0) "Budget 2024"
,SUM(COALESCE(act."Actual 2023",0)) "Actual 2023" ,COALESCE(SUM(act."Actual 2023"),0) "Actual 2023"
,SUM(COALESCE(act."Actual 2024",0)) "Actual 2024" ,COALESCE(SUM(act."Actual 2024"),0) "Actual 2024"
,SUM(COALESCE(act."Open Ord" ,0)) "Open Ord" ,COALESCE(SUM(act."Open Ord" ),0) "Open Ord"
,SUM(COALESCE(act."Quotes" ,0)) "Quotes" ,COALESCE(SUM(act."Quotes" ),0) "Quotes"
FROM FROM
act act
FULL OUTER JOIN bgt ON FULL OUTER JOIN bgt ON
@ -124,7 +125,7 @@ SELECT
,"Quotes" quotes ,"Quotes" quotes
,CASE WHEN "Actual 2023" > 0 ,CASE WHEN "Actual 2023" > 0
THEN CASE 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" = 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 'Reduced'
WHEN ("Actual 2024" + "Open Ord") >= "Actual 2023" THEN 'Increased' WHEN ("Actual 2024" + "Open Ord") >= "Actual 2023" THEN 'Increased'
@ -133,11 +134,13 @@ SELECT
CASE CASE
WHEN "Actual 2024" = 0 AND "Open Ord" = 0 AND "Quotes" > 0 THEN 'Incremental Quoted' 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 OR "Open Ord" > 0 THEN 'Incremental Won'
WHEN "Actual 2024" = 0 AND "Open Ord" = 0 AND "Quotes" = 0 THEN 'Not Quoted'
END END
END flag END flag
,COALESCE(w.bucket,a.bucket,'None') bucket ,COALESCE(w.bucket,a.bucket,'None') bucket
,COALESCE(w.attainment,0) attainment ,COALESCE(w.attainment,0) attainment
,COALESCE(w.notes,a.notes,'-') notes ,COALESCE(w.notes,a.notes,'-') notes
,COALESCE(w.fcnotes,'-') fcnotes
FROM FROM
rlarp.sales_walk_r1_agg agg rlarp.sales_walk_r1_agg agg
LEFT OUTER JOIN rlarp.walk a ON LEFT OUTER JOIN rlarp.walk a ON
@ -255,9 +258,9 @@ SELECT
,"Open Ord" open_orders_2024 ,"Open Ord" open_orders_2024
,"Quotes" quotes ,"Quotes" quotes
,"Budget 2024" * COALESCE(w.attainment,0) available_to_win ,"Budget 2024" * COALESCE(w.attainment,0) available_to_win
,CASE WHEN "Actual 2023" > 0 ,CASE WHEN COALESCE("Actual 2023",0) > 0
THEN CASE 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" = 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 'Reduced'
WHEN ("Actual 2024" + "Open Ord") >= "Actual 2023" THEN 'Increased' WHEN ("Actual 2024" + "Open Ord") >= "Actual 2023" THEN 'Increased'
@ -266,12 +269,15 @@ SELECT
CASE CASE
WHEN "Actual 2024" = 0 AND "Open Ord" = 0 AND "Quotes" > 0 THEN 'Incremental Quoted' 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 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
END flag END flag
,COALESCE(w.bucket,a.bucket,'None') bucket ,COALESCE(w.bucket,a.bucket,'None') bucket
,COALESCE(w.attainment,0) attainment ,COALESCE(w.attainment,0) attainment
,COALESCE(w.notes,a.notes,'-') notes ,COALESCE(w.notes,a.notes,'-') notes
,sw.flag flag_cust ,COALESCE(w.fcnotes,'-') fcnotes
,COALESCE(sw.flag,'Not Quoted') flag_cust
FROM FROM
rlarp.sales_walk_r1_seg_agg agg rlarp.sales_walk_r1_seg_agg agg
LEFT OUTER JOIN rlarp.walk a ON LEFT OUTER JOIN rlarp.walk a ON

View File

@ -1,13 +1,14 @@
MERGE INTO MERGE INTO
rlarp.walk_r1 w rlarp.walk_r1 w
USING 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 i.ship_cust = w.ship_cust
AND i.bill_cust = w.bill_cust AND i.bill_cust = w.bill_cust
WHEN MATCHED THEN UPDATE SET WHEN MATCHED THEN UPDATE SET
bucket = i.bucket bucket = i.bucket
,attainment = i.attainment::numeric ,attainment = i.attainment::numeric
,notes = i.notes ,notes = i.notes
,fcnotes = i.fcnotes
WHEN NOT MATCHED THEN WHEN NOT MATCHED THEN
INSERT (bill_cust, ship_cust, bucket, attainment, notes) INSERT (bill_cust, ship_cust, bucket, attainment, notes, fcnotes)
VALUES (bill_cust,i.ship_cust, i.bucket, i.attainment::numeric, i.notes ) VALUES (bill_cust,i.ship_cust, i.bucket, i.attainment::numeric, i.notes , i.fcnotes)