Fixed product average price/last price/price history chart handling regarding empty/0 prices (fixes #2106)

This commit is contained in:
Bernd Bestel
2023-01-13 13:19:22 +01:00
parent 995df64054
commit efc7b999bb
2 changed files with 74 additions and 0 deletions

View File

@@ -26,9 +26,11 @@
- Defaults to disabled, so no changed behavior when not configured - Defaults to disabled, so no changed behavior when not configured
- Added a new option "Reprint stock entry label" on the stock entry edit page (will print the correspondind stock entry label on save) - Added a new option "Reprint stock entry label" on the stock entry edit page (will print the correspondind stock entry label on save)
- This option will be automatically set on changing the entry's due date - This option will be automatically set on changing the entry's due date
- Changed that for the product's average and last price (and for the price history chart) stock transactions with an empty or `0` price are ignored
- Fixed that hiding the "Purchased date" column (table options) on the stock entries page didn't work - Fixed that hiding the "Purchased date" column (table options) on the stock entries page didn't work
- Fixed that sorting by the "Value" column on the stock overview page didn't work - Fixed that sorting by the "Value" column on the stock overview page didn't work
- Fixed that the consumed amount was wrong, when consuming multiple substituted subproducts at once and when multiple/different conversion factors were involved - Fixed that the consumed amount was wrong, when consuming multiple substituted subproducts at once and when multiple/different conversion factors were involved
- Fixed that for a product's average price, only currently in-stock items were considered, not already consumed ones
### Shopping list ### Shopping list

72
migrations/0213.sql Normal file
View File

@@ -0,0 +1,72 @@
DROP VIEW products_average_price;
CREATE VIEW products_average_price
AS
SELECT
1 AS id, -- Dummy, LessQL needs an id column
s.product_id,
SUM(s.amount * s.price) / SUM(s.amount) as price
FROM stock_log s
WHERE s.transaction_type IN ('purchase', 'inventory-correction', 'stock-edit-new')
AND IFNULL(s.price, 0) > 0
AND IFNULL(s.amount, 0) > 0
AND undone = 0
GROUP BY s.product_id;
DROP VIEW products_last_purchased;
CREATE VIEW products_last_purchased
AS
select
1 AS id, -- Dummy, LessQL needs an id column
sl.product_id,
sl.amount,
sl.best_before_date,
sl.purchased_date,
(SELECT price FROM stock_log WHERE product_id = sl.product_id AND transaction_type IN ('purchase', 'stock-edit-new', 'inventory-correction') AND IFNULL(price, 0) > 0 AND IFNULL(amount, 0) > 0 AND undone = 0 ORDER BY purchased_date DESC LIMIT 1) AS price,
sl.location_id,
sl.shopping_location_id
FROM stock_log sl
JOIN (
SELECT
s1.product_id,
MAX(s1.id) max_stock_id
FROM stock_log s1
JOIN (
SELECT
s.product_id,
MAX(s.purchased_date) max_purchased_date
FROM stock_log s
WHERE undone = 0
AND transaction_type in ('purchase', 'stock-edit-new', 'inventory-correction')
GROUP BY s.product_id) sp2
ON s1.product_id = sp2.product_id
AND s1.purchased_date = sp2.max_purchased_date
WHERE undone = 0
AND transaction_type in ('purchase', 'stock-edit-new', 'inventory-correction')
GROUP BY s1.product_id) sp3
ON sl.product_id = sp3.product_id
AND sl.id = sp3.max_stock_id;
DROP VIEW product_price_history;
CREATE VIEW product_price_history
AS
SELECT
sl.product_id AS id, -- Dummy, LessQL needs an id column
sl.product_id,
sl.price,
sl.purchased_date,
sl.shopping_location_id
FROM stock_log sl
WHERE sl.transaction_type IN ('purchase', 'inventory-correction', 'stock-edit-new')
AND sl.undone = 0
AND IFNULL(sl.price, 0) > 0
AND IFNULL(sl.amount, 0) > 0
AND sl.id NOT IN (
-- These are edited purchase and inventory-correction rows
SELECT sl_origin.id
FROM stock_log sl_origin
JOIN stock_log sl_edit
ON sl_origin.stock_id = sl_edit.stock_id
AND sl_edit.transaction_type = 'stock-edit-new'
AND sl_edit.id > sl_origin.id
WHERE sl_origin.transaction_type IN ('purchase', 'inventory-correction')
);