Debug metric calculations and reset scripts (still broken)
This commit is contained in:
@@ -79,8 +79,7 @@ CREATE TABLE IF NOT EXISTS vendor_metrics (
|
||||
order_fill_rate DECIMAL(5,2),
|
||||
total_orders INT,
|
||||
total_late_orders INT,
|
||||
PRIMARY KEY (vendor),
|
||||
FOREIGN KEY (vendor) REFERENCES products(vendor) ON DELETE CASCADE
|
||||
PRIMARY KEY (vendor)
|
||||
);
|
||||
|
||||
-- Re-enable foreign key checks
|
||||
@@ -88,4 +87,28 @@ SET FOREIGN_KEY_CHECKS = 1;
|
||||
|
||||
-- Create optimized indexes for metrics calculations
|
||||
CREATE INDEX idx_orders_metrics ON orders (product_id, date, canceled, quantity, price);
|
||||
CREATE INDEX idx_purchase_orders_metrics ON purchase_orders (product_id, date, status, ordered, received);
|
||||
CREATE INDEX idx_purchase_orders_metrics ON purchase_orders (product_id, date, status, ordered, received);
|
||||
|
||||
-- Create view for inventory health (after all tables are created)
|
||||
CREATE OR REPLACE VIEW inventory_health AS
|
||||
SELECT
|
||||
p.product_id,
|
||||
p.SKU,
|
||||
p.title,
|
||||
p.stock_quantity,
|
||||
COALESCE(pm.daily_sales_avg, 0) as daily_sales_avg,
|
||||
COALESCE(pm.days_of_inventory, 0) as days_of_inventory,
|
||||
COALESCE(pm.reorder_point, 0) as reorder_point,
|
||||
COALESCE(pm.safety_stock, 0) as safety_stock,
|
||||
CASE
|
||||
WHEN p.stock_quantity <= COALESCE(pm.safety_stock, 0) THEN 'Critical'
|
||||
WHEN p.stock_quantity <= COALESCE(pm.reorder_point, 0) THEN 'Reorder'
|
||||
WHEN p.stock_quantity > (COALESCE(pm.daily_sales_avg, 0) * 90) THEN 'Overstocked'
|
||||
ELSE 'Healthy'
|
||||
END as stock_status
|
||||
FROM
|
||||
products p
|
||||
LEFT JOIN
|
||||
product_metrics pm ON p.product_id = pm.product_id
|
||||
WHERE
|
||||
p.managing_stock = true;
|
||||
@@ -117,28 +117,4 @@ LEFT JOIN
|
||||
WHERE
|
||||
o.canceled = false
|
||||
GROUP BY
|
||||
p.product_id, p.SKU, p.title;
|
||||
|
||||
-- Create view for inventory health
|
||||
CREATE OR REPLACE VIEW inventory_health AS
|
||||
SELECT
|
||||
p.product_id,
|
||||
p.SKU,
|
||||
p.title,
|
||||
p.stock_quantity,
|
||||
pm.daily_sales_avg,
|
||||
pm.days_of_inventory,
|
||||
pm.reorder_point,
|
||||
pm.safety_stock,
|
||||
CASE
|
||||
WHEN p.stock_quantity <= pm.safety_stock THEN 'Critical'
|
||||
WHEN p.stock_quantity <= pm.reorder_point THEN 'Reorder'
|
||||
WHEN p.stock_quantity > (pm.daily_sales_avg * 90) THEN 'Overstocked'
|
||||
ELSE 'Healthy'
|
||||
END as stock_status
|
||||
FROM
|
||||
products p
|
||||
LEFT JOIN
|
||||
product_metrics pm ON p.product_id = pm.product_id
|
||||
WHERE
|
||||
p.managing_stock = true;
|
||||
p.product_id, p.SKU, p.title;
|
||||
Reference in New Issue
Block a user