Compare commits

..

No commits in common. "budget" and "master" have entirely different histories.

6 changed files with 62 additions and 133 deletions

13
api.ts
View File

@ -20,7 +20,6 @@ const client = new Client({
,user: user ,user: user
,password:password ,password:password
,database:database ,database:database
,applicationName: "account status"
}); });
await client.connect(); await client.connect();
@ -29,13 +28,11 @@ await client.connect();
const query = await Deno.readTextFile("sql/write_note.sql"); const query = await Deno.readTextFile("sql/write_note.sql");
// Define a route to retrieve values from the database using parameters // Define a route to retrieve values from the database using parameters
router.get('/sales_walk/write_note/:bill_cust/:ship_cust/:bucket/:attainment/:notes', async (ctx) => { router.get('/sales_walk/write_note/:ship_cust/:bucket/:notes', async (ctx) => {
const bucket = ctx.params.bucket; // Extract the bucket parameter from the route const bucket = ctx.params.bucket; // Extract the bucket parameter from the route
const attainment= ctx.params.attainment; // Extract the bucket parameter from the route
const notes = ctx.params.notes; // Extract the bucket parameter from the route const notes = ctx.params.notes; // Extract the bucket parameter from the route
const ship_cust = ctx.params.ship_cust; // Extract the ship_cust parameter from the route const ship_cust = ctx.params.ship_cust; // Extract the ship_cust parameter from the route
const bill_cust = ctx.params.bill_cust; // Extract the ship_cust parameter from the route
//console.log(bucket) //console.log(bucket)
//console.log(ship_cust) //console.log(ship_cust)
@ -77,8 +74,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 , fcnotes} = body; // Destructure the needed values from the JSON const { ship_cust, bucket, notes } = body; // Destructure the needed values from the JSON
const result = await client.queryObject({args: [bill_cust, ship_cust, bucket, attainment, notes, fcnotes], text: query} ); const result = await client.queryObject({args: [ship_cust, bucket, notes], text: query} );
} }
} catch (error) { } catch (error) {
@ -94,6 +91,6 @@ app.use(router.routes());
app.use(router.allowedMethods()); app.use(router.allowedMethods());
// Start the server // Start the server
console.log('Server is running on http://localhost:8086'); console.log('Server is running on http://localhost:8085');
await app.listen({ port: 8086 }); await app.listen({ port: 8085 });

View File

@ -1,10 +1,10 @@
[Unit] [Unit]
Description=Deno API Sales Walk v2 Description=Deno API Sales Walk
After=network.target After=network.target
[Service] [Service]
ExecStart=/home/ptrowbridge/.deno/bin/deno run --allow-all /opt/sales_bridge_2/api.ts ExecStart=/home/ptrowbridge/.deno/bin/deno run --allow-all /opt/sales_bridge/api.ts
WorkingDirectory=/opt/sales_bridge_2 WorkingDirectory=/opt/sales_bridge
Restart=always Restart=always
User=ptrowbridge User=ptrowbridge

View File

@ -1,24 +0,0 @@
SELECT
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
(sw.dsm = ?)
ORDER BY
sw.shipments_2023 desc,
shipments_2024 + open_orders_2024 desc,
quotes desc

View File

@ -83,7 +83,7 @@ SELECT
,case when coalesce(qty,0) = 0 then 0 else pounds/qty end ,case when coalesce(qty,0) = 0 then 0 else pounds/qty end
,pallets ,pallets
,plcd ,plcd
,TRIM(fs_line) ,fs_line
,'US' ,'US'
,1 ,1
,'US' ,'US'

View File

@ -8,24 +8,20 @@ sales_walk_seg
CREATE TABLE IF NOT EXISTS rlarp.walk_r1 ( CREATE TABLE IF NOT EXISTS rlarp.walk (
bill_cust text ship_cust text PRIMARY KEY
,ship_cust text
,bucket text ,bucket text
,attainment numeric
,notes text ,notes text
,fcnotes text
,PRIMARY KEY (bill_cust, ship_cust)
); );
GRANT ALL ON TABLE rlarp.walk_r1 TO PUBLIC; GRANT ALL ON TABLE rlarp.walk TO PUBLIC;
DROP VIEW IF EXISTS rlarp.sales_walk_r1_seg; DROP VIEW IF EXISTS rlarp.sales_walk;
DROP VIEW IF EXISTS rlarp.sales_walk_r1; DROP VIEW IF EXISTS rlarp.sales_walk_seg;
------------------------------------------sales walk agg--------------------------------------------------- ------------------------------------------sales walk agg---------------------------------------------------
DROP MATERIALIZED VIEW IF EXISTS rlarp.sales_walk_r1_agg ; DROP MATERIALIZED VIEW IF EXISTS rlarp.sales_walk_agg ;
CREATE MATERIALIZED VIEW rlarp.sales_walk_r1_agg AS CREATE MATERIALIZED VIEW rlarp.sales_walk_agg AS
WITH WITH
act as ( act as (
SELECT SELECT
@ -35,7 +31,7 @@ SELECT
--,os.glec --,os.glec
--,m.biggroup --,m.biggroup
--,os.priceg --,os.priceg
,COALESCE(sum(pounds) filter (WHERE sseas = 2023 AND version = 'Actual' AND SUBSTRING(smon,1,2) <= '12'),0) "Actual 2023" ,COALESCE(sum(pounds) filter (WHERE sseas = 2023 AND version = 'Actual' AND SUBSTRING(smon,1,2) <= '08'),0) "Actual 2023"
,COALESCE(sum(pounds) filter (WHERE sseas = 2024 AND version = 'Actual'),0) "Actual 2024" ,COALESCE(sum(pounds) filter (WHERE sseas = 2024 AND version = 'Actual'),0) "Actual 2024"
,COALESCE(sum(pounds) filter (WHERE version = 'Actual' AND ostatus LIKE 'Open%' AND rseas <= 2024),0) "Open Ord" ,COALESCE(sum(pounds) filter (WHERE version = 'Actual' AND ostatus LIKE 'Open%' AND rseas <= 2024),0) "Open Ord"
,COALESCE(sum(pounds) filter (WHERE version = 'Quotes'),0) "Quotes" ,COALESCE(sum(pounds) filter (WHERE version = 'Quotes'),0) "Quotes"
@ -73,44 +69,27 @@ GROUP BY
bill_dba bill_dba
,ship_dba ,ship_dba
,dsm ,dsm
,sum(sales_usd) sales ,sum(sales_usd)
,sum(pounds) pounds ,sum(pounds)
--string_agg(distinct mold,', ') --string_agg(distinct mold,', ')
FROM FROM b
rlarp.osm_stack left outer join rlarp.molds m ON
m.stlc = b.mold
WHERE WHERE
version = 'Budget' oseas = 2024
AND oseas = '2024' AND version = 'Forecats'
GROUP BY GROUP BY
bill_dba version,
,ship_dba oseas,
,dsm customer
) )
,agg AS ( ,agg AS (
SELECT SELECT
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
,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
TRIM(bgt.bill_dba) = act.bill_dba
AND TRIM(bgt.ship_dba) = act.ship_dba
AND TRIM(bgt.dsm ) = act.dsm
GROUP BY
COALESCE(act.bill_dba,TRIM(bgt.bill_dba))
,COALESCE(act.ship_dba,TRIM(bgt.ship_dba))
,COALESCE(act.dsm ,TRIM(bgt.dsm ))
) )
SELECT * FROM agg; SELECT * FROM agg
------------------------------------------sales walk------------------------------------------------------- ------------------------------------------sales walk-------------------------------------------------------
CREATE OR REPLACE VIEW rlarp.sales_walk_r1 AS CREATE OR REPLACE VIEW rlarp.sales_walk AS
SELECT SELECT
bill_dba bill_dba
,ship_dba ,ship_dba
@ -118,14 +97,13 @@ SELECT
--,glec --,glec
--,biggroup --,biggroup
--,priceg --,priceg
,"Budget 2024" budget_2024
,"Actual 2023" shipments_2023 ,"Actual 2023" shipments_2023
,"Actual 2024" shipments_2024 ,"Actual 2024" shipments_2024
,"Open Ord" open_orders_2024 ,"Open Ord" open_orders_2024
,"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 'Not Quoted' 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 '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'
@ -134,37 +112,30 @@ 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,'None') bucket
,COALESCE(w.attainment,0) attainment ,COALESCE(w.notes,'-') notes
,COALESCE(w.notes,a.notes,'-') notes
,COALESCE(w.fcnotes,'-') fcnotes
FROM FROM
rlarp.sales_walk_r1_agg agg rlarp.sales_walk_agg agg
LEFT OUTER JOIN rlarp.walk a ON LEFT OUTER JOIN rlarp.walk w ON
a.ship_cust = agg.ship_dba
LEFT OUTER JOIN rlarp.walk_r1 w ON
w.ship_cust = agg.ship_dba w.ship_cust = agg.ship_dba
AND w.bill_cust = agg.bill_dba
WHERE WHERE
"Budget 2024" <> 0 "Actual 2023" <> 0
OR "Actual 2023" <> 0
OR ("Actual 2024") <> 0 OR ("Actual 2024") <> 0
OR ("Open Ord") <> 0 OR ("Open Ord") <> 0
OR ("Quotes") <> 0 OR ("Quotes") <> 0
ORDER BY ORDER BY
dsm dsm
,"Budget 2024" + "Actual 2024" + "Actual 2023" + "Open Ord" + "Quotes" desc; ,"Actual 2024" + "Actual 2023" + "Open Ord" + "Quotes" desc;
GRANT SELECT ON rlarp.sales_walk_r1 TO PUBLIC; GRANT SELECT ON rlarp.sales_walk TO PUBLIC;
--,biggroup --,biggroup
------------------------------------------sales walk seg agg----------------------------------------------- ------------------------------------------sales walk seg agg-----------------------------------------------
DROP MATERIALIZED VIEW IF EXISTS rlarp.sales_walk_r1_seg_agg; DROP MATERIALIZED VIEW IF EXISTS rlarp.sales_walk_seg_agg;
CREATE MATERIALIZED VIEW rlarp.sales_walk_r1_seg_agg AS CREATE MATERIALIZED VIEW rlarp.sales_walk_seg_agg AS
WITH WITH
SEG AS ( SEG AS (
SELECT SELECT
@ -200,8 +171,7 @@ SELECT
,s.segm ,s.segm
,m.biggroup ,m.biggroup
--,os.priceg --,os.priceg
,COALESCE(sum(pounds) filter (WHERE oseas = 2024 AND version = 'Budget'),0) "Budget 2024" ,COALESCE(sum(pounds) filter (WHERE sseas = 2023 AND version = 'Actual' AND SUBSTRING(smon,1,2) <= '08'),0) "Actual 2023"
,COALESCE(sum(pounds) filter (WHERE sseas = 2023 AND version = 'Actual' AND SUBSTRING(smon,1,2) <= '12'),0) "Actual 2023"
,COALESCE(sum(pounds) filter (WHERE sseas = 2024 AND version = 'Actual'),0) "Actual 2024" ,COALESCE(sum(pounds) filter (WHERE sseas = 2024 AND version = 'Actual'),0) "Actual 2024"
,COALESCE(sum(pounds) filter (WHERE version = 'Actual' AND ostatus LIKE 'Open%' AND rseas <= 2024),0) "Open Ord" ,COALESCE(sum(pounds) filter (WHERE version = 'Actual' AND ostatus LIKE 'Open%' AND rseas <= 2024),0) "Open Ord"
,COALESCE(sum(pounds) filter (WHERE version = 'Quotes'),0) "Quotes" ,COALESCE(sum(pounds) filter (WHERE version = 'Quotes'),0) "Quotes"
@ -223,7 +193,6 @@ WHERE
AND odate >= '2023-05-01'::date AND odate >= '2023-05-01'::date
) )
OR (ostatus LIKE 'Open%') OR (ostatus LIKE 'Open%')
OR (version = 'Budget')
) )
AND calc_status <> 'CANCELED' AND calc_status <> 'CANCELED'
AND substring(os.glec,1,1) <= '2' AND substring(os.glec,1,1) <= '2'
@ -242,7 +211,7 @@ GROUP BY
SELECT * FROM agg; SELECT * FROM agg;
------------------------------------------sales walk seg--------------------------------------------------- ------------------------------------------sales walk seg---------------------------------------------------
CREATE OR REPLACE VIEW rlarp.sales_walk_r1_seg AS CREATE OR REPLACE VIEW rlarp.sales_walk_seg AS
SELECT SELECT
agg.bill_dba agg.bill_dba
,agg.ship_dba ,agg.ship_dba
@ -252,15 +221,13 @@ SELECT
,agg.segm ,agg.segm
,agg.biggroup ,agg.biggroup
--,priceg --,priceg
,"Budget 2024" budget_2024
,"Actual 2023" shipments_2023 ,"Actual 2023" shipments_2023
,"Actual 2024" shipments_2024 ,"Actual 2024" shipments_2024
,"Open Ord" open_orders_2024 ,"Open Ord" open_orders_2024
,"Quotes" quotes ,"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 THEN CASE
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 'No Activity'
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'
@ -269,35 +236,27 @@ 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,'None') bucket
,COALESCE(w.attainment,0) attainment ,COALESCE(w.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_seg_agg agg
LEFT OUTER JOIN rlarp.walk a ON LEFT OUTER JOIN rlarp.walk w ON
a.ship_cust = agg.ship_dba
LEFT OUTER JOIN rlarp.walk_r1 w ON
w.ship_cust = agg.ship_dba w.ship_cust = agg.ship_dba
AND w.bill_cust = agg.bill_dba LEFT OUTER JOIN rlarp.sales_walk sw ON
LEFT OUTER JOIN rlarp.sales_walk_r1 sw ON
sw.bill_dba = agg.bill_dba sw.bill_dba = agg.bill_dba
AND sw.ship_dba = agg.ship_dba AND sw.ship_dba = agg.ship_dba
AND sw.dsm = agg.dsm AND sw.dsm = agg.dsm
WHERE WHERE
"Budget 2024" <> 0 "Actual 2023" <> 0
OR "Actual 2023" <> 0
OR ("Actual 2024") <> 0 OR ("Actual 2024") <> 0
OR ("Open Ord") <> 0 OR ("Open Ord") <> 0
OR ("Quotes") <> 0 OR ("Quotes") <> 0
ORDER BY ORDER BY
dsm dsm
,"Budget 2024" + "Actual 2024" + "Actual 2023" + "Open Ord" + "Quotes" desc; ,"Actual 2024" + "Actual 2023" + "Open Ord" + "Quotes" desc;
GRANT SELECT ON rlarp.sales_walk_r1_seg TO PUBLIC; GRANT SELECT ON rlarp.sales_walk_seg TO PUBLIC;
--,biggroup --,biggroup

View File

@ -1,14 +1,11 @@
MERGE INTO MERGE INTO
rlarp.walk_r1 w rlarp.walk w
USING USING
( SELECT $1 bill_cust, $2 ship_cust, $3 bucket, $4 attainment, $5 notes, $6 fcnotes) as i ON ( SELECT $1 ship_cust, $2 bucket, $3 notes) as i ON
i.ship_cust = w.ship_cust i.ship_cust = w.ship_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
,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, fcnotes) INSERT (ship_cust, bucket, notes)
VALUES (bill_cust,i.ship_cust, i.bucket, i.attainment::numeric, i.notes , i.fcnotes) VALUES (i.ship_cust, i.bucket, i.notes )