fix profit value after each order 2

This commit is contained in:
2026-07-18 17:37:41 +03:30
parent b66cd5e8f9
commit 285a159762
8 changed files with 512 additions and 25 deletions
@@ -0,0 +1,28 @@
-- Backfill helpers for orders (run manually after migrate; test on one row first).
-- 1) product_id from first related transaction that has product_id
UPDATE orders o
INNER JOIN (
SELECT order_id, MIN(product_id) AS product_id
FROM transactions
WHERE order_id IS NOT NULL
AND product_id IS NOT NULL
AND product_id > 0
GROUP BY order_id
) t ON t.order_id = o.id
SET o.product_id = t.product_id
WHERE o.product_id IS NULL;
-- 2) Recalc fee / total_fee / total_price_original from stored prices
-- Example dry-run for one order:
-- SELECT id, fee, total_fee,
-- ABS(price - price_original) AS fee_new,
-- ABS(amount * (price - price_original)) AS total_fee_new
-- FROM orders WHERE id = ?;
UPDATE orders
SET
fee = ABS(price - price_original),
total_fee = ABS(amount * (price - price_original)),
total_price_original = amount * price_original
WHERE price_original IS NOT NULL
AND amount IS NOT NULL;