Add daily stock lot sellout forecast

This commit is contained in:
gitea_admin committed 2026-09-06 08:55:10 +02:00
1 parent 9a1192ae81
commit 1639960ab6
9 files changed
+440 -3

No files matched your search

@@ -0,0 +1,50 @@
BEGIN;
ALTER TABLE stock_lot
ADD COLUMN IF NOT EXISTS sellout_forecast_status TEXT NOT NULL DEFAULT 'no_demand',
ADD COLUMN IF NOT EXISTS sellout_forecast_confidence TEXT NOT NULL DEFAULT 'none',
ADD COLUMN IF NOT EXISTS sellout_forecast_daily_demand NUMERIC(14, 6) NOT NULL DEFAULT 0,
ADD COLUMN IF NOT EXISTS sellout_forecast_calculated_at TIMESTAMP;
DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'chk_stock_lot_forecast_status') THEN
ALTER TABLE stock_lot ADD CONSTRAINT chk_stock_lot_forecast_status
CHECK (sellout_forecast_status IN ('forecast', 'depleted', 'no_demand'));
END IF;
IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'chk_stock_lot_forecast_confidence') THEN
ALTER TABLE stock_lot ADD CONSTRAINT chk_stock_lot_forecast_confidence
CHECK (sellout_forecast_confidence IN ('high', 'medium', 'low', 'none'));
END IF;
END;
$$;
CREATE TABLE IF NOT EXISTS stock_lot_forecast_history (
id BIGSERIAL PRIMARY KEY,
stock_lot_id BIGINT NOT NULL REFERENCES stock_lot(id) ON DELETE RESTRICT,
product_id BIGINT NOT NULL REFERENCES product(id) ON DELETE RESTRICT,
calculated_at TIMESTAMP NOT NULL DEFAULT NOW(),
as_of_date DATE NOT NULL,
physical_qty NUMERIC(14, 4) NOT NULL,
weighted_daily_demand NUMERIC(14, 6) NOT NULL DEFAULT 0,
sales_units NUMERIC(14, 4) NOT NULL DEFAULT 0,
sales_days INTEGER NOT NULL DEFAULT 0,
observed_span_days INTEGER NOT NULL DEFAULT 0,
lookback_days INTEGER NOT NULL,
half_life_days INTEGER NOT NULL,
forecast_date DATE,
forecast_status TEXT NOT NULL,
confidence TEXT NOT NULL,
error_message TEXT,
CONSTRAINT chk_stock_lot_forecast_history_status
CHECK (forecast_status IN ('forecast', 'depleted', 'no_demand')),
CONSTRAINT chk_stock_lot_forecast_history_confidence
CHECK (confidence IN ('high', 'medium', 'low', 'none'))
);
CREATE INDEX IF NOT EXISTS idx_stock_lot_forecast_history_lot_date
ON stock_lot_forecast_history(stock_lot_id, calculated_at DESC);
CREATE INDEX IF NOT EXISTS idx_stock_lot_forecast_history_product_date
ON stock_lot_forecast_history(product_id, calculated_at DESC);
COMMIT;