move files around

This commit is contained in:
Paul Trowbridge 2025-08-07 23:16:18 -04:00
parent 92e25c004e
commit 045e050b0e
48 changed files with 77 additions and 1121 deletions

View File

@ -1,13 +0,0 @@
-- FAnalysis.PRICING.lastprice definition
-- Drop table
-- DROP TABLE FAnalysis.PRICING.lastprice;
CREATE TABLE pricing.lastprice (
customer varchar(255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
mold varchar(255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
part_stats nvarchar(MAX) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
);
CREATE UNIQUE NONCLUSTERED INDEX lastprice_cust_mold ON FAnalysis.PRICING.lastprice (customer ASC, mold ASC) ;

View File

@ -1,59 +0,0 @@
--------------------------------------------------------------------------------
-- Step 1: Rebuild last price history at sales matrix refresh time
--------------------------------------------------------------------------------
DELETE FROM pricing.lastprice;
WITH srt AS (
SELECT
customer,
mold,
part,
version,
qty,
ROUND(sales_usd / qty, 5) AS price,
odate,
oseas,
ordnum,
quoten,
ROW_NUMBER() OVER (
PARTITION BY customer, mold, part, version
ORDER BY odate DESC
) AS rn
FROM rlarp.osm_stack
WHERE
--quotes can't be integrated until we have datasegment or correct part code
version IN ('Actual'/*,'Quotes'*/) AND
customer IS NOT NULL AND
fs_line = '41010' AND
calc_status <> 'CANCELLED' AND
qty <> 0 AND
mold <> ''
),
json_rows AS (
SELECT
customer,
mold,
part,
version,
CONCAT(
'"', part, '":',
(
SELECT version, qty, price, odate, ordnum, quoten
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
)
) AS part_json
FROM srt
WHERE rn = 1
)
,onerow AS (
SELECT
customer,
mold,
CONCAT('{', STRING_AGG(part_json, ','), '}') AS part_stats
FROM json_rows
GROUP BY customer, mold
)
INSERT INTO pricing.lastprice SELECT * FROM onerow;
--CREATE UNIQUE INDEX lastprice_cust_mold ON pricing.lastprice(customer, mold);

View File

@ -1,26 +0,0 @@
minimal setup to run a single pricing call:
```sql
EXEC pricing.single_price_call
@bill = 'GRIF0001',
@ship = 'GRIF0001',
@part = 'XNS0T1G3G18B096',
@stlc = 'XNS0T1G3',
@v1ds = 'v1:T..PLT..',
@vol = 9600;
```
make sure pricing schema is setup
Target Prices
----------------------
1. create target table: `target_prices.ms.sql`
2. populate targets: `target_prices_copy.ms.sql`
Price History
----------------------
1. create history table: `lastprice.ms.sql`
2. populate history: `make_hist.ms.sql`
Proc Definitions
----------------------
1. create proc: `single_price_call.ms.sql`

View File

@ -1,162 +0,0 @@
CREATE OR ALTER PROCEDURE pricing.single_price_call
@bill VARCHAR(100),
@ship VARCHAR(100),
@part VARCHAR(100),
@stlc VARCHAR(100),
@v1ds VARCHAR(100),
@vol NUMERIC(18,6)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @phist NVARCHAR(MAX);
-- Declare table variable for the input row
DECLARE @queue TABLE (
bill VARCHAR(100),
ship VARCHAR(100),
part VARCHAR(100),
stlc VARCHAR(100),
v1ds VARCHAR(100),
vol NUMERIC(18,6),
chan VARCHAR(50),
cust VARCHAR(100),
tier VARCHAR(50),
pltq NUMERIC(18,6),
price NUMERIC(18,6),
expl NVARCHAR(MAX),
hist NVARCHAR(MAX),
LAST nvarchar(max)
);
--------------------------------------------------------------------------------
-- Step 1: Insert input row into queue
--------------------------------------------------------------------------------
INSERT INTO @queue (bill, ship, part, stlc, v1ds, vol)
VALUES (@bill, @ship, @part, @stlc, @v1ds, @vol);
--------------------------------------------------------------------------------
-- Step 2: Enrich the row with chan, tier, cust, pltq
--------------------------------------------------------------------------------
UPDATE q
SET
chan =
CASE SUBSTRING(bc.cclass, 2, 3)
WHEN 'DIS' THEN
CASE SUBSTRING(sc.cclass, 2, 3)
WHEN 'DIS' THEN 'WHS'
ELSE 'DRP'
END
ELSE 'DIR'
END,
tier =
CASE SUBSTRING(bc.cclass, 2, 3)
WHEN 'DIR' THEN bc.tier
ELSE ISNULL(sc.tier, bc.tier)
END,
cust =
CASE SUBSTRING(bc.cclass, 2, 3)
WHEN 'DIS' THEN
CASE SUBSTRING(sc.cclass, 2, 3)
WHEN 'DIS' THEN bc.dba
ELSE sc.dba
END
ELSE q.bill
END,
pltq = i.mpck
FROM @queue q
LEFT JOIN rlarp.cust bc ON bc.code = q.bill
LEFT JOIN rlarp.cust sc ON sc.code = q.ship
LEFT JOIN CMSInterfaceIn.[CMS.CUSLG].itemm i ON i.item = q.part;
--------------------------------------------------------------------------------
-- Step 3: Get last price info directly into @queue columns
--------------------------------------------------------------------------------
UPDATE q
SET
q.hist = (
SELECT TOP 1
j.qty,
j.price,
j.odate,
j.ordnum,
j.quoten
FROM pricing.lastprice lp
OUTER APPLY OPENJSON(lp.part_stats) AS p
OUTER APPLY OPENJSON(p.value)
WITH (
qty NUMERIC(20,5),
price NUMERIC(20,5),
odate DATE,
ordnum INT,
quoten INT
) AS j
WHERE
lp.customer = q.cust
AND lp.mold = SUBSTRING(q.part,1,8)
AND p.[key] COLLATE SQL_Latin1_General_CP1_CI_AS = q.part
ORDER BY j.odate DESC
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
),
q.last = (
SELECT TOP 1
CAST(ROUND(j.price, 5) AS NVARCHAR(50)) -- must be string to store in NVARCHAR column
FROM pricing.lastprice lp
OUTER APPLY OPENJSON(lp.part_stats) AS p
OUTER APPLY OPENJSON(p.value)
WITH (
qty NUMERIC(20,5),
price NUMERIC(20,5),
odate DATE,
ordnum INT,
quoten INT
) AS j
WHERE
lp.customer = q.cust
AND lp.mold = SUBSTRING(q.part,1,8)
AND p.[key] COLLATE SQL_Latin1_General_CP1_CI_AS = q.part
ORDER BY j.odate DESC
)
FROM @queue q;
--------------------------------------------------------------------------------
-- Step 4: Apply pricing and embed price history + last price from queue columns
--------------------------------------------------------------------------------
UPDATE q
SET
price = tp.price,
expl = (
SELECT
'target price' AS [source],
tp.price AS [target_price],
CAST(q.last AS NUMERIC(20,5)) AS [last_price],
FLOOR(q.vol / NULLIF(q.pltq, 0)) AS [calculated_pallets],
ROUND(q.vol / NULLIF(q.pltq, 0), 5) AS [exact_pallets],
CONCAT(tp.lower_bound, '-', ISNULL(CAST(tp.upper_bound AS VARCHAR), '')) AS [volume range],
q.cust AS [customer],
q.chan AS [channel],
q.tier AS [tier],
JSON_QUERY(tp.math) AS [target math],
JSON_QUERY(q.hist) AS [price history]
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
)
FROM @queue q
INNER JOIN pricing.target_prices tp ON
q.stlc = tp.stlc
AND q.v1ds = tp.ds
AND q.chan = tp.chan
AND q.tier = tp.tier
AND FLOOR(q.vol / NULLIF(q.pltq, 0)) >= tp.lower_bound
AND (
tp.upper_bound IS NULL OR FLOOR(q.vol / NULLIF(q.pltq, 0)) < tp.upper_bound
);
--------------------------------------------------------------------------------
-- Step Last: Return just the enriched row
--------------------------------------------------------------------------------
SELECT price, expl FROM @queue;
END;

View File

@ -1,18 +0,0 @@
DROP TABLE pricing.target_prices;
CREATE TABLE pricing.target_prices (
stlc nvarchar(8) NOT NULL,
ds nvarchar(20) NOT NULL,
chan nvarchar(3) NOT NULL,
tier nvarchar(1) NOT NULL,
vol nvarchar(20) NOT NULL,
lower_bound int NOT NULL,
upper_bound int NULL,
price numeric(28,6) NOT NULL,
math nvarchar(MAX) NULL
);
ALTER TABLE pricing.target_prices
ADD CONSTRAINT uq_target_prices_unique_combo
UNIQUE (stlc, ds, chan, tier, vol, lower_bound);

View File

@ -1,23 +0,0 @@
DELETE FROM pricing.target_prices;
INSERT INTO
pricing.target_prices
SELECT
stlc,
ds,
chan,
tier,
vol,
-- Extract lower bound: text between '[' and ','
TRY_CAST(SUBSTRING(vol, 2, CHARINDEX(',', vol) - 2) AS INT) AS lower_bound,
-- Extract upper bound: text between ',' and ')'
CASE
WHEN RIGHT(vol, 2) = ',)' THEN NULL
ELSE TRY_CAST(SUBSTRING(vol, CHARINDEX(',', vol) + 1, LEN(vol) - CHARINDEX(',', vol) - 1) AS INT)
END AS upper_bound,
price,
math
FROM
usmidsap02.ubm.pricequote.target_prices_view;
--SELECT COUNT(*) FROM pricing.target_prices

View File

@ -1,60 +0,0 @@
{
"details": [
{
"label": "History",
"details": [
{
"label": "Last Sale: 2025-06-12",
"value": 0.10120,
"type": "currency",
"note": "Ord# 1008338"
}
]
},
{
"label": "List",
"details": [
{
"label": "List:GUAU",
"value": 0.11000,
"type": "currency",
"note": "U.AEA.DI"
}
]
},
{
"label": "Target Support",
"details": [
{
"label": "Anchor:XNS0T1G3",
"value": 0.08000,
"type": "currency",
"note": "+"
},
{
"label": "Channel:WHS",
"value": 0.20000,
"type": "percentage",
"note": "x"
},
{
"label": "Volume:1-8",
"value": 0.10000,
"type": "percentage",
"note": "x"
}
]
},
{
"label": "Guidance",
"details": [
{
"label": "Last Price Capped",
"value": 0.10120,
"type": "currency",
"note": "Last price 0.10120 capped at 0.0000000000000%"
}
]
}
]
}

View File

@ -1,280 +0,0 @@
{
"v1:B..CSE..": [
{
"version": "Actual",
"part": "XNS0T1G2G18C140",
"qty": 8960.00,
"price": 0.092910,
"odate": "2016-02-10",
"ordnum": 784718,
"quoten": 28971,
"flags": []
},
{
"version": "Quotes",
"part": "XNS0T1G2G18C140",
"qty": 9100.00,
"price": 0.092910,
"odate": "2016-02-10",
"ordnum": 0,
"quoten": 29543,
"flags": []
}
],
"v1:T..CSE..": [
{
"version": "Quotes",
"part": "XNS0T1G2X03C140",
"qty": 6020.00,
"price": 0.092910,
"odate": "2016-02-10",
"ordnum": 0,
"quoten": 29543,
"flags": []
},
{
"version": "Actual",
"part": "XNS0T1G2X03C140",
"qty": 6020.00,
"price": 0.092910,
"odate": "2016-02-09",
"ordnum": 784655,
"flags": []
}
],
"v1:B..PLT..": [
{
"version": "Actual",
"part": "XNS0T1G3G18B096",
"qty": 9600.00,
"price": 0.126130,
"odate": "2024-10-31",
"ordnum": 992442,
"quoten": 79774,
"flags": [
"most_recent_sale",
"largest_volume_sale"
]
},
{
"version": "Quotes",
"part": "XNS0T1G3G18B096",
"qty": 38400.00,
"price": 0.126130,
"odate": "2024-10-29",
"ordnum": 0,
"quoten": 79774,
"flags": [
"most_recent_quote",
"largest_volume_quote"
]
},
{
"version": "Actual",
"part": "XNS0T1G3G18B096",
"qty": 9600.00,
"price": 0.126130,
"odate": "2024-08-02",
"ordnum": 985899,
"flags": []
},
{
"version": "Actual",
"part": "XNS0T1G3G18B096",
"qty": 38400.00,
"price": 0.128250,
"odate": "2023-12-04",
"ordnum": 969894,
"quoten": 75273,
"flags": []
},
{
"version": "Quotes",
"part": "XNS0T1G3G18B096",
"qty": 38400.00,
"price": 0.128250,
"odate": "2023-12-04",
"ordnum": 0,
"quoten": 75273,
"flags": []
},
{
"version": "Quotes",
"part": "XNS0T1G3G18B096",
"qty": 19200.00,
"price": 0.135000,
"odate": "2023-02-13",
"ordnum": 0,
"quoten": 73602,
"flags": []
},
{
"version": "Quotes",
"part": "XNS0T1G3G18B096",
"qty": 38400.00,
"price": 0.145020,
"odate": "2022-01-26",
"ordnum": 0,
"quoten": 69130,
"flags": []
},
{
"version": "Actual",
"part": "XNS0T1G3G18B096",
"qty": 28800.00,
"price": 0.125480,
"odate": "2021-07-16",
"ordnum": 922664,
"quoten": 64686,
"flags": []
},
{
"version": "Quotes",
"part": "XNS0T1G3G18B096",
"qty": 38400.00,
"price": 0.125480,
"odate": "2021-07-08",
"ordnum": 0,
"quoten": 64686,
"flags": []
},
{
"version": "Actual",
"part": "XNS0T1G3G18B096",
"qty": 38400.00,
"price": 0.100390,
"odate": "2020-11-12",
"ordnum": 905400,
"flags": []
},
{
"version": "Quotes",
"part": "XNS0T1G3G18B096",
"qty": 38400.00,
"price": 0.100390,
"odate": "2020-08-13",
"ordnum": 0,
"quoten": 58215,
"flags": []
},
{
"version": "Quotes",
"part": "XNS0T1G3G18B096",
"qty": 28800.00,
"price": 0.100390,
"odate": "2020-06-11",
"ordnum": 0,
"quoten": 57254,
"flags": []
},
{
"version": "Actual",
"part": "XNS0T1G3G18B096",
"qty": 28800.00,
"price": 0.100390,
"odate": "2020-06-10",
"ordnum": 890715,
"quoten": 57254,
"flags": []
},
{
"version": "Actual",
"part": "XNS0T1G3G18B096",
"qty": 28800.00,
"price": 0.098910,
"odate": "2019-07-23",
"ordnum": 870300,
"quoten": 51460,
"flags": []
},
{
"version": "Quotes",
"part": "XNS0T1G3G18B096",
"qty": 28800.00,
"price": 0.098910,
"odate": "2019-07-18",
"ordnum": 0,
"quoten": 51460,
"flags": []
},
{
"version": "Actual",
"part": "XNS0T1G3G18B096",
"qty": 19200.00,
"price": 0.095290,
"odate": "2018-10-09",
"ordnum": 853112,
"quoten": 44775,
"flags": []
},
{
"version": "Actual",
"part": "XNS0T1G3G18B096",
"qty": 9600.00,
"price": 0.095290,
"odate": "2018-10-09",
"ordnum": 853112,
"quoten": 44775,
"flags": []
},
{
"version": "Quotes",
"part": "XNS0T1G3G18B096",
"qty": 9600.00,
"price": 0.095290,
"odate": "2018-08-21",
"ordnum": 0,
"quoten": 44775,
"flags": []
},
{
"version": "Quotes",
"part": "XNS0T1G3G18B096",
"qty": 38400.00,
"price": 0.094160,
"odate": "2017-10-13",
"ordnum": 0,
"quoten": 38844,
"flags": []
},
{
"version": "Actual",
"part": "XNS0T1G2G18B079",
"qty": 47790.00,
"price": 0.092910,
"odate": "2016-06-10",
"ordnum": 794232,
"flags": []
},
{
"version": "Quotes",
"part": "XNS0T1G2G18B079",
"qty": 47790.00,
"price": 0.092910,
"odate": "2016-02-10",
"ordnum": 0,
"quoten": 29543,
"flags": []
},
{
"version": "Quotes",
"part": "XNS0T1G2G18B079",
"qty": 47790.00,
"price": 0.091200,
"odate": "2015-12-17",
"ordnum": 0,
"quoten": 28971,
"flags": []
},
{
"version": "Quotes",
"part": "XNS0T1G2G18B079",
"qty": 47790.00,
"price": 0.091200,
"odate": "2015-09-21",
"ordnum": 0,
"quoten": 27873,
"flags": []
}
]
}

View File

@ -1,63 +0,0 @@
{
"mrs": {
"version": "Actual",
"datasegment": "v1:T..CSE..",
"part": "XNS0T1G3X18C140",
"qty": 4200.00,
"price": 0.146300,
"odate": "2025-08-01",
"ordnum": 1011917,
"flag": "mrs"
},
"mrq": {
"version": "Quotes",
"datasegment": "v1:B..CSE..",
"part": "XNS0T1G3G18C140",
"qty": 7560.00,
"price": 0.115800,
"odate": "2024-12-10",
"ordnum": 0,
"quoten": 81983,
"flag": "mrq"
},
"lvs": {
"version": "Actual",
"datasegment": "v1:B..PLT..",
"part": "XNS0T1G3G18B096",
"qty": 28800.00,
"price": 0.098360,
"odate": "2025-05-23",
"ordnum": 1006860,
"flag": "lvs"
},
"lvq": {
"version": "Quotes",
"datasegment": "v1:B..CSE..",
"part": "XNS0T1G3G18C140",
"qty": 7560.00,
"price": 0.115800,
"odate": "2024-12-10",
"ordnum": 0,
"quoten": 81983,
"flag": "lvq"
},
"v1:B..CSE..": {
"dss": {
"version": "Actual",
"datasegment": "v1:B..CSE..",
"part": "XNS0T1G3G18C140",
"qty": 2520.00,
"price": 0.106540,
"odate": "2025-07-17",
"ordnum": 1010952
},
"dsq": {
"version": "Quotes",
"datasegment": "v1:B..CSE..",
"part": "XNS0T1G3G18C140",
"qty": 2520.00,
"price": 0.106540,
"odate": "2025-07-17",
"quoten": 81983
}}
}

View File

@ -1,109 +0,0 @@
CREATE OR ALTER PROCEDURE pricing.process_queue
AS
BEGIN
SET NOCOUNT ON;
--------------------------------------------------------------------------------
-- Step 1: Insert input row into real queue table
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Step 2: Enrich the row with chan, tier, cust, pltq
--------------------------------------------------------------------------------
UPDATE q
SET
q.chan =
CASE SUBSTRING(bc.cclass, 2, 3)
WHEN 'DIS' THEN
CASE SUBSTRING(sc.cclass, 2, 3)
WHEN 'DIS' THEN 'WHS'
ELSE 'DRP'
END
ELSE 'DIR'
END,
q.tier =
CASE SUBSTRING(bc.cclass, 2, 3)
WHEN 'DIR' THEN bc.tier
ELSE ISNULL(sc.tier, bc.tier)
END,
q.cust =
CASE SUBSTRING(bc.cclass, 2, 3)
WHEN 'DIS' THEN
CASE SUBSTRING(sc.cclass, 2, 3)
WHEN 'DIS' THEN q.ship
ELSE q.ship
END
ELSE q.bill
END,
q.pltq = i.mpck
FROM
pricing.price_queue q
LEFT JOIN rlarp.cust bc ON bc.code = q.bill
LEFT JOIN rlarp.cust sc ON sc.code = q.ship
LEFT JOIN CMSInterfaceIn.[CMS.CUSLG].itemm i ON i.item = q.part;
--------------------------------------------------------------------------------
-- Step 3: Apply pricing from target_prices
--------------------------------------------------------------------------------
DECLARE @updated TABLE (
id BIGINT,
bill VARCHAR(100),
ship VARCHAR(100),
part VARCHAR(100),
stlc VARCHAR(100),
v1ds VARCHAR(100),
vol NUMERIC(18,6),
chan VARCHAR(50),
cust VARCHAR(100),
tier VARCHAR(50),
pltq NUMERIC(18,6),
price NUMERIC(18,6),
expl NVARCHAR(MAX)
);
UPDATE q
SET
q.price = tp.price,
q.expl = (
SELECT
'target price' AS [source],
FLOOR(q.vol / NULLIF(q.pltq, 0)) AS [calculated_pallets],
ROUND(q.vol / NULLIF(q.pltq, 0), 5) AS [exact_pallets],
CONCAT(tp.lower_bound, '-', ISNULL(CAST(tp.upper_bound AS VARCHAR), '')) AS [volume range],
q.cust AS [customer],
q.chan AS [channel],
q.tier AS [tier],
JSON_QUERY(tp.math) AS [target math] -- important if math is JSON
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
)
OUTPUT
inserted.id,
inserted.bill,
inserted.ship,
inserted.part,
inserted.stlc,
inserted.v1ds,
inserted.vol,
inserted.chan,
inserted.cust,
inserted.tier,
inserted.pltq,
inserted.price,
inserted.expl
INTO @updated
FROM pricing.price_queue q
INNER JOIN pricing.target_prices tp ON
q.stlc = tp.stlc
AND q.v1ds = tp.ds
AND q.chan = tp.chan
AND q.tier = tp.tier
AND FLOOR(q.vol / NULLIF(q.pltq, 0)) >= tp.lower_bound
AND (
tp.upper_bound IS NULL OR FLOOR(q.vol / NULLIF(q.pltq, 0)) < tp.upper_bound
);
--------------------------------------------------------------------------------
-- Step 4: Return just the enriched row
--------------------------------------------------------------------------------
SELECT * FROM @updated;
END;

View File

@ -1,69 +0,0 @@
CREATE OR REPLACE FUNCTION pricequote.process_queue()
RETURNS SETOF pricequote.price_queue
LANGUAGE plpgsql
AS $$
BEGIN
--------------------------------------------------------------------------------
-- Step 1: Set channel, tier, and pallet quantity
--------------------------------------------------------------------------------
UPDATE pricequote.price_queue s
SET
chan = cr.chan,
tier = cr.tier,
pltq = cr.mpck
FROM (
SELECT
q.bill,
q.ship,
q.part,
i.mpck,
CASE SUBSTRING(bc.cclass, 2, 3)
WHEN 'DIS' THEN
CASE SUBSTRING(sc.cclass, 2, 3)
WHEN 'DIS' THEN 'WHS'
ELSE 'DRP'
END
ELSE 'DIR'
END AS chan,
CASE SUBSTRING(bc.cclass, 2, 3)
WHEN 'DIR' THEN bc.tier
ELSE COALESCE(sc.tier, bc.tier)
END AS tier
FROM pricequote.price_queue q
LEFT JOIN rlarp.cust bc ON bc.code = q.bill
LEFT JOIN rlarp.cust sc ON sc.code = q.ship
LEFT JOIN "CMS.CUSLG".itemm i ON i.item = q.part
) cr
WHERE
cr.bill = s.bill AND
COALESCE(cr.ship, '') = COALESCE(s.ship, '') AND
cr.part = s.part;
--------------------------------------------------------------------------------
-- Step 2: Set price and explanation from target_prices, return touched rows
--------------------------------------------------------------------------------
RETURN QUERY
WITH updated AS (
UPDATE pricequote.price_queue q
SET
price = tp.price,
expl = jsonb_build_object(
'source', 'target price',
'calculated_pallets', FLOOR(q.vol / NULLIF(q.pltq, 0))::INT,
'exact_pallets', ROUND(q.vol / NULLIF(q.pltq, 0), 5),
'volume range', tp.vol
)
FROM pricequote.target_prices tp
WHERE
q.stlc = tp.stlc
AND q.v1ds = tp.ds
AND q.chan = tp.chan
AND q.tier = tp.tier
AND tp.vol @> FLOOR(q.vol / NULLIF(q.pltq, 0))::INT
RETURNING q.*
)
SELECT * FROM updated;
END;
$$;

View File

@ -1,69 +0,0 @@
CREATE OR REPLACE PROCEDURE pricequote.process_queue_proc()
LANGUAGE plpgsql
AS $$
BEGIN
--------------------------------------------------------------------------------
-- Step 1: Set channel, tier, and pallet quantity
--------------------------------------------------------------------------------
UPDATE pricequote.price_queue s
SET
chan = cr.chan,
tier = cr.tier,
pltq = cr.mpck,
cust = cr.cust
FROM (
SELECT
q.bill,
q.ship,
q.part,
i.mpck,
CASE SUBSTRING(bc.cclass, 2, 3)
WHEN 'DIS' THEN
CASE SUBSTRING(sc.cclass, 2, 3)
WHEN 'DIS' THEN 'WHS'
ELSE 'DRP'
END
ELSE 'DIR'
END AS chan,
CASE SUBSTRING(bc.cclass, 2, 3)
WHEN 'DIR' THEN bc.tier
ELSE COALESCE(sc.tier, bc.tier)
END AS tier,
CASE SUBSTRING(bc.cclass, 2, 3)
WHEN 'DIR' THEN bc.dba
ELSE COALESCE(sc.dba, bc.dba)
END AS cust
FROM pricequote.price_queue q
LEFT JOIN rlarp.cust bc ON bc.code = q.bill
LEFT JOIN rlarp.cust sc ON sc.code = q.ship
LEFT JOIN "CMS.CUSLG".itemm i ON i.item = q.part
) cr
WHERE
cr.bill = s.bill AND
COALESCE(cr.ship, '') = COALESCE(s.ship, '') AND
cr.part = s.part;
--------------------------------------------------------------------------------
-- Step 2: Set price and explanation from target_prices
--------------------------------------------------------------------------------
UPDATE pricequote.price_queue q
SET
price = tp.price,
expl = jsonb_build_object(
'source', 'target price',
'calculated_pallets', FLOOR(q.vol / NULLIF(q.pltq, 0))::INT,
'exact_pallets', ROUND(q.vol / NULLIF(q.pltq, 0), 5),
'volume range', tp.vol
)
FROM pricequote.target_prices tp
WHERE
q.stlc = tp.stlc
AND q.v1ds = tp.ds
AND q.chan = tp.chan
AND q.tier = tp.tier
AND tp.vol @> FLOOR(q.vol / NULLIF(q.pltq, 0))::INT;
END;
$$;

View File

@ -1,27 +0,0 @@
CREATE OR ALTER FUNCTION dbo.LEAST_NUMERIC205(
@a NUMERIC(20,5),
@b NUMERIC(20,5)
)
RETURNS NUMERIC(20,5)
AS
BEGIN
RETURN CASE
WHEN @a IS NULL THEN @b
WHEN @b IS NULL THEN @a
WHEN @a < @b THEN @a ELSE @b
END
END
CREATE OR ALTER FUNCTION dbo.GREATEST_NUMERIC205(
@a NUMERIC(20,5),
@b NUMERIC(20,5)
)
RETURNS NUMERIC(20,5)
AS
BEGIN
RETURN CASE
WHEN @a IS NULL THEN @b
WHEN @b IS NULL THEN @a
WHEN @a > @b THEN @a ELSE @b
END
END

View File

@ -1,3 +1,37 @@
-- This function returns the least of two NUMERIC(20,5) values.
CREATE OR ALTER FUNCTION dbo.LEAST_NUMERIC205(
@a NUMERIC(20,5),
@b NUMERIC(20,5)
)
RETURNS NUMERIC(20,5)
AS
BEGIN
RETURN CASE
WHEN @a IS NULL THEN @b
WHEN @b IS NULL THEN @a
WHEN @a < @b THEN @a ELSE @b
END
END
GO
-- This function returns the greatest of two NUMERIC(20,5) values.
CREATE OR ALTER FUNCTION dbo.GREATEST_NUMERIC205(
@a NUMERIC(20,5),
@b NUMERIC(20,5)
)
RETURNS NUMERIC(20,5)
AS
BEGIN
RETURN CASE
WHEN @a IS NULL THEN @b
WHEN @b IS NULL THEN @a
WHEN @a > @b THEN @a ELSE @b
END
END
GO
-- This function implements the guidance logic for pricing based on target, last, and list prices.
CREATE OR ALTER FUNCTION pricing.guidance_logic ( CREATE OR ALTER FUNCTION pricing.guidance_logic (
@target_price NUMERIC(20,5), @target_price NUMERIC(20,5),
@last_price NUMERIC(20,5), @last_price NUMERIC(20,5),
@ -82,3 +116,4 @@ BEGIN
INSERT INTO @result VALUES (@price, @reason); INSERT INTO @result VALUES (@price, @reason);
RETURN; RETURN;
END END
GO

View File

@ -1,75 +0,0 @@
{
"details": [
{
"label": "Model Inputs",
"details": [
{
"label": "Base Cost",
"value": 1.22446,
"type": "currency"
}
]
},
{
"label": "Peer Target",
"details": [
{
"label": "Peer Median Margin",
"value": 36.873,
"type": "percent",
"note": "DIR|102 - HANGING POTS|110 - INJECTION|Tier 2"
}
]
},
{
"label": "Target Support",
"details": [
{
"label": "Tier 1 Truckload",
"value": 80,
"type": "currency",
"note": "reviewed floor price"
},
{
"label": "Warehouse",
"value": 1.2,
"type": "percent"
}
]
},
{
"label": "Factor Adjustment",
"details": [
{
"label": "Package UOM",
"value": -0.01,
"type": "percent"
},
{
"label": "# of Pallets",
"value": 0.02,
"type": "percent",
"note": "0.03"
},
{
"label": "Urgency",
"value": 0.03,
"type": "percent",
"note": "Top Priority"
},
{
"label": "State Adder/Subtractor",
"value": -0.02,
"type": "percent",
"note": "OH"
},
{
"label": "Branding",
"value": 0,
"type": "currency"
}
]
}
]
}

View File

@ -1,15 +0,0 @@
SELECT
lq.*
,t.*
FROM
rlarp.live_quotes lq
OUTER APPLY pricing.fn_single_price_call(
lq.billto,
lq.shipto,
lq.part,
substring(lq.part,1,8),
lq.v1ds,
lq.units_each
) t
--WHERE
-- qid = 112794

View File

@ -1,23 +0,0 @@
DELETE FROM pricing.target_prices;
INSERT INTO
pricing.target_prices
SELECT
stlc,
ds,
chan,
tier,
vol,
-- Extract lower bound: text between '[' and ','
TRY_CAST(SUBSTRING(vol, 2, CHARINDEX(',', vol) - 2) AS INT) AS lower_bound,
-- Extract upper bound: text between ',' and ')'
CASE
WHEN RIGHT(vol, 2) = ',)' THEN NULL
ELSE TRY_CAST(SUBSTRING(vol, CHARINDEX(',', vol) + 1, LEN(vol) - CHARINDEX(',', vol) - 1) AS INT)
END AS upper_bound,
price,
math
FROM
usmidsap02.ubm.pricequote.target_prices_view;
--SELECT COUNT(*) FROM pricing.target_prices

View File

@ -1,28 +1,23 @@
{ {
"details": [ "details": [
{ {
"label": "History", "label": "Model Inputs",
"details": [ "details": [
{ {
"label": "Last Sale", "label": "Base Cost",
"value": 0.081, "value": 1.22446,
"type": "currency"
},
{
"label": "Last Quote",
"value": 0.079,
"type": "currency" "type": "currency"
} }
] ]
}, },
{ {
"label": "List", "label": "Peer Target",
"details": [ "details": [
{ {
"label": "Code", "label": "Peer Median Margin",
"value": "GUAU", "value": 36.873,
"type": "text", "type": "percent",
"note": "Griffin East - US A List" "note": "DIR|102 - HANGING POTS|110 - INJECTION|Tier 2"
} }
] ]
}, },
@ -30,34 +25,51 @@
"label": "Target Support", "label": "Target Support",
"details": [ "details": [
{ {
"label": "Anchor", "label": "Tier 1 Truckload",
"value": 0.080, "value": 80,
"type": "currency", "type": "currency",
"note": "Tier 1 Truckload Black" "note": "reviewed floor price"
}, },
{ {
"label": "Color :L", "label": "Warehouse",
"value": 0.10, "value": 1.2,
"type": "percent", "type": "percent"
"note": " x "
},
{
"label": "Tier 2",
"value": 0.02,
"type": "percent",
"note": " x "
} }
] ]
}, },
{ {
"label": "Guidance", "label": "Factor Adjustment",
"details": [ "details": [
{ {
"label": "Last Price Capped", "label": "Package UOM",
"value": 0.080, "value": -0.01,
"type": "Last price 0.079 capped at 1%" "type": "percent"
},
{
"label": "# of Pallets",
"value": 0.02,
"type": "percent",
"note": "0.03"
},
{
"label": "Urgency",
"value": 0.03,
"type": "percent",
"note": "Top Priority"
},
{
"label": "State Adder/Subtractor",
"value": -0.02,
"type": "percent",
"note": "OH"
},
{
"label": "Branding",
"value": 0,
"type": "currency"
} }
] ]
} }
] ]
} }