Compare commits

..

No commits in common. "c5532cd2ddc17c4e82816b858776a4f84cc074b1" and "37fead59ffa9818f9c41cc179d2036ec6aa559a5" have entirely different histories.

3 changed files with 76 additions and 167 deletions

View File

@ -1,91 +0,0 @@
CREATE OR ALTER FUNCTION pricing.approval_logic(
@target numeric(20,5),
@last_norm numeric(20,5),
@list_eff numeric(20,5),
@last_date date,
@floor_pct numeric(10,5) = 0.05,
@cap_last_pct numeric(10,5) = 1.00,
@cap_list_pct numeric(10,5) = 1.00
)
RETURNS @ret TABLE (
guidance_price numeric(20,5),
guidance_reason nvarchar(4000)
)
AS
BEGIN
DECLARE
@base_price numeric(20,5),
@after_floor numeric(20,5),
@after_cap_last numeric(20,5),
@final_price numeric(20,5),
@reason nvarchar(4000) = N'';
-- no target → early return
IF @target IS NULL
BEGIN
INSERT INTO @ret VALUES (NULL, N'No target price available');
RETURN;
END;
-- start from target
SET @base_price = @target;
-- Step 1: floor vs last_norm (only if it raises price)
SET @after_floor = @base_price;
IF @last_norm IS NOT NULL AND @floor_pct > 0
SET @after_floor = ROUND(
CASE WHEN @base_price >= @last_norm*(1-@floor_pct)
THEN @base_price
ELSE @last_norm*(1-@floor_pct)
END, 5);
-- Step 2: cap vs last_norm (only if it lowers price)
SET @after_cap_last = @after_floor;
IF @last_norm IS NOT NULL
SET @after_cap_last = ROUND(
CASE WHEN @after_floor <= @last_norm*@cap_last_pct
THEN @after_floor
ELSE @last_norm*@cap_last_pct
END, 5);
-- Step 3: cap vs list_eff (only if it lowers price)
SET @final_price = @after_cap_last;
IF @list_eff IS NOT NULL
SET @final_price = ROUND(
CASE WHEN @after_cap_last <= @list_eff*@cap_list_pct
THEN @after_cap_last
ELSE @list_eff*@cap_list_pct
END, 5);
-- Build explanation
IF @last_norm IS NULL AND @list_eff IS NULL
BEGIN
SET @reason = N'No prior sale or list; using target price';
END
ELSE
BEGIN
SET @reason = N'Using target price';
-- mention floor only if it raised the price
IF @last_norm IS NOT NULL AND @floor_pct > 0 AND @after_floor > @base_price
SET @reason += N', floored to ' + FORMAT(@floor_pct*100, '0.##') + N'% below last price';
-- mention last cap only if it lowered the price
IF @last_norm IS NOT NULL AND @after_cap_last < @after_floor
SET @reason += CASE WHEN @cap_last_pct = 1
THEN N', capped to not exceed last price'
ELSE N', capped to ' + FORMAT(@cap_last_pct*100,'0.##') + N'% of last price'
END;
-- mention list cap only if it lowered the price
IF @list_eff IS NOT NULL AND @final_price < @after_cap_last
SET @reason += CASE WHEN @cap_list_pct = 1
THEN N', capped to not exceed list price'
ELSE N', capped to ' + FORMAT(@cap_list_pct*100,'0.##') + N'% of list price'
END;
END;
INSERT INTO @ret VALUES (@final_price, @reason);
RETURN;
END;

View File

@ -1,81 +1,83 @@
-- CREATE OR ALTER FUNCTION pricing.guidance_logic;
CREATE OR ALTER FUNCTION pricing.guidance_logic( CREATE OR ALTER FUNCTION pricing.guidance_logic(
@target numeric(20,5), @target numeric(20,5),
@last_norm numeric(20,5), @last_norm numeric(20,5),
@list_eff numeric(20,5), @list_eff numeric(20,5),
@last_date date, @last_date date,
@floor_pct numeric(10,5) = 0.95, @floor_pct numeric(10,5) = 0.05, -- e.g., 5%
@cap_last_pct numeric(10,5) = 1.00, @cap_last_pct numeric(10,5) = 1.00, -- e.g., 100%
@cap_list_pct numeric(10,5) = 1.00 @cap_list_pct numeric(10,5) = 1.00
) )
RETURNS @ret TABLE ( RETURNS TABLE
guidance_price numeric(20,5),
guidance_reason nvarchar(4000)
)
AS AS
BEGIN RETURN
DECLARE WITH params AS (
@base_price numeric(20,5), -- starting point (target if available, else last_norm, else list_eff) SELECT
@after_floor numeric(20,5), -- base but limited to x% lower than last price @target AS base_price,
@after_cap_last numeric(20,5), -- previous step but limited to x% higher than last price @last_norm AS last_norm,
@final_price numeric(20,5), -- previous step but limited to x% higher than list price @list_eff AS list_eff,
@reason nvarchar(4000) = N''; -- logic source of price @floor_pct AS floor_pct,
@cap_last_pct AS cap_last_pct,
-- Early exit if nothing to work with -- HARD CEILING at list: clamp to <= 1.00 regardless of input
IF @target IS NULL AND @last_norm IS NULL AND @list_eff IS NULL CASE WHEN @cap_list_pct IS NULL OR @cap_list_pct > 1 THEN 1.00 ELSE @cap_list_pct END AS eff_list_cap
BEGIN ),
INSERT INTO @ret VALUES (NULL, N'No target, last, or list available'); step_floor AS (
RETURN; SELECT
END; base_price, last_norm, list_eff, floor_pct, cap_last_pct, eff_list_cap,
CAST(
-- Pick starting base price CASE
SET @base_price = COALESCE(@target, @last_norm, @list_eff); WHEN base_price IS NULL OR last_norm IS NULL OR floor_pct <= 0
THEN base_price
-- Step 1: use base price unless it's more than x% below last price ELSE ROUND(IIF(base_price >= last_norm*(1-floor_pct), base_price, last_norm*(1-floor_pct)), 5)
SET @after_floor = @base_price; END
IF @last_norm IS NOT NULL AS numeric(20,5)) AS after_floor
SET @after_floor = ROUND( FROM params
CASE WHEN @base_price >= @last_norm*@floor_pct ),
THEN @base_price step_cap_last AS (
ELSE @last_norm*@floor_pct SELECT
END, 5); base_price, last_norm, list_eff, floor_pct, cap_last_pct, eff_list_cap, after_floor,
CAST(
-- Step 2: use price from previous step but don't allow it to be x% above last price CASE
SET @after_cap_last = @after_floor; WHEN after_floor IS NULL OR last_norm IS NULL
IF @last_norm IS NOT NULL THEN after_floor
SET @after_cap_last = ROUND( ELSE ROUND(IIF(after_floor <= last_norm*cap_last_pct, after_floor, last_norm*cap_last_pct), 5)
CASE WHEN @after_floor <= @last_norm*@cap_last_pct END
THEN @after_floor AS numeric(20,5)) AS after_cap_last
ELSE @last_norm*@cap_last_pct FROM step_floor
END, 5); ),
step_cap_list AS (
-- Step 3: use price from last step, but don't allow it to be more than x% above list price SELECT
SET @final_price = @after_cap_last; base_price, last_norm, list_eff, floor_pct, cap_last_pct, eff_list_cap, after_floor, after_cap_last,
IF @list_eff IS NOT NULL CAST(
SET @final_price = ROUND( CASE
CASE WHEN @after_cap_last <= @list_eff*@cap_list_pct WHEN after_cap_last IS NULL OR list_eff IS NULL
THEN @after_cap_last THEN after_cap_last
ELSE @list_eff*@cap_list_pct ELSE ROUND(IIF(after_cap_last <= list_eff*eff_list_cap, after_cap_last, list_eff*eff_list_cap), 5)
END, 5); END
AS numeric(20,5)) AS final_price
-- Reason text FROM step_cap_last
SET @reason = )
CASE SELECT
WHEN @target IS NOT NULL THEN N'Using target price' final_price AS guidance_price,
WHEN @last_norm IS NOT NULL THEN N'Using last price as base' CASE
WHEN @list_eff IS NOT NULL THEN N'Using list price as base' WHEN @target IS NULL THEN 'No target price available'
END; WHEN @last_norm IS NULL AND @list_eff IS NULL THEN 'No prior sale or list; using target price'
ELSE
IF @last_norm IS NOT NULL AND @after_floor > @base_price CONCAT(
SET @reason = N'Last price drop limit'; 'Using target price',
-- show floor only if it raised price
IF @last_norm IS NOT NULL AND @after_cap_last < @after_floor CASE WHEN last_norm IS NOT NULL AND @floor_pct > 0 AND after_floor > base_price
SET @reason = N'Last price increase limit'; THEN CONCAT(', floored to ', FORMAT(@floor_pct*100,'0.##'), '% below last price') ELSE '' END,
-- show last cap only if it lowered price
IF @list_eff IS NOT NULL AND @final_price < @after_cap_last CASE WHEN last_norm IS NOT NULL AND after_cap_last < after_floor
SET @reason = N'List price ceiling'; THEN CASE WHEN @cap_last_pct = 1
THEN ', capped to not exceed last price'
INSERT INTO @ret VALUES (@final_price, @reason); ELSE CONCAT(', capped to ', FORMAT(@cap_last_pct*100,'0.##'), '% of last price')
RETURN; END
END; ELSE '' END,
-- show list cap only if it lowered price (always “not exceed” because eff_list_cap<=1)
CASE WHEN list_eff IS NOT NULL AND final_price < after_cap_last
THEN ', capped to not exceed list price'
ELSE '' END
)
END AS guidance_reason
FROM step_cap_list;

View File

@ -136,8 +136,6 @@ BEGIN
------------step 6 compute guidance------------ ------------step 6 compute guidance------------
guidance_price NUMERIC(20,5), guidance_price NUMERIC(20,5),
guidance_reason NVARCHAR(MAX), guidance_reason NVARCHAR(MAX),
shown_price NUMERIC(20,5),
shown_logic NVARCHAR(MAX),
------------step 7 build json------------------ ------------step 7 build json------------------
expl NVARCHAR(MAX), expl NVARCHAR(MAX),
ui_json NVARCHAR(MAX) ui_json NVARCHAR(MAX)
@ -386,7 +384,7 @@ BEGIN
TRY_CAST(q.listprice_eff AS NUMERIC(20,5)), TRY_CAST(q.listprice_eff AS NUMERIC(20,5)),
TRY_CAST(q.last_date AS DATE), TRY_CAST(q.last_date AS DATE),
--allowable price drop percent --allowable price drop percent
.95, .05,
--cap on last price --cap on last price
1.0, 1.0,
--cap on list percent --cap on list percent