Make sure that the views products_average_price and products_oldest_stock_unit_price always returns a row per product, also for not in stock items (references #801)

Otherwise there are errors when getting product details for currently not in stock items...
This commit is contained in:
Bernd Bestel 2020-08-22 10:06:37 +02:00
parent ffec1134a3
commit 939b98e470
No known key found for this signature in database
GPG Key ID: 71BD34C0D4891300

View File

@ -39,25 +39,30 @@ CREATE VIEW products_average_price
AS AS
SELECT SELECT
1 AS id, -- Dummy, LessQL needs an id column 1 AS id, -- Dummy, LessQL needs an id column
s.product_id, p.id AS product_id,
round(sum(s.amount * s.price) / sum(s.amount), 2) as price CASE WHEN s.product_id IS NOT NULL THEN round(sum(s.amount * s.price) / sum(s.amount), 2) ELSE NULL END as price
FROM stock s FROM products p
GROUP BY s.product_id; LEFT JOIN stock s
ON p.id = s.product_id
GROUP BY p.id;
CREATE VIEW products_oldest_stock_unit_price CREATE VIEW products_oldest_stock_unit_price
AS AS
-- Find oldest best_before_date then oldest purchased_date then make sure to return one stock row using max -- Find oldest best_before_date then oldest purchased_date then make sure to return one stock row using max
SELECT SELECT
1 AS id, -- Dummy, LessQL needs an id column 1 AS id, -- Dummy, LessQL needs an id column
sw.product_id, p.id AS product_id,
sw.amount, s.amount,
sw.best_before_date, s.best_before_date,
sw.purchased_date, s.purchased_date,
sw.price, sw.qu_factor_purchase_to_stock, s.price,
sw.location_id, s.qu_factor_purchase_to_stock,
sw.shopping_location_id s.location_id,
FROM stock sw s.shopping_location_id
JOIN ( FROM products p
LEFT JOIN stock s
ON p.id = s.product_id
LEFT JOIN (
SELECT SELECT
s1.product_id, s1.product_id,
MIN(s1.id) min_stock_id MIN(s1.id) min_stock_id
@ -81,8 +86,8 @@ SELECT
AND s1.best_before_date = sp2.oldest_date AND s1.best_before_date = sp2.oldest_date
AND s1.purchased_date = sp2.min_purchased_date AND s1.purchased_date = sp2.min_purchased_date
GROUP BY s1.product_id) sp3 GROUP BY s1.product_id) sp3
ON sw.product_id = sp3.product_id ON s.product_id = sp3.product_id
AND sw.id = sp3.min_stock_id; AND s.id = sp3.min_stock_id;
DROP VIEW recipes_pos_resolved; DROP VIEW recipes_pos_resolved;
CREATE VIEW recipes_pos_resolved CREATE VIEW recipes_pos_resolved