mirror of
https://github.com/grocy/grocy.git
synced 2026-04-07 05:16:15 +02:00
migrations/0103 add value and factor_puchase_amount to stock_current and stock_current_location_content
This commit is contained in:
parent
2bb792af99
commit
495659c3f9
|
|
@ -13,7 +13,70 @@ UPDATE stock_log
|
|||
CREATE TABLE product_barcodes (
|
||||
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
|
||||
product_id INT NOT NULL,
|
||||
barcode TEXT NOT NULL,
|
||||
barcode TEXT NOT NULL UNIQUE,
|
||||
qu_factor_purchase_to_stock REAL NOT NULL DEFAULT 1,
|
||||
shopping_location_id INTEGER
|
||||
);
|
||||
|
||||
DROP VIEW stock_current_location_content;
|
||||
CREATE VIEW stock_current_location_content
|
||||
AS
|
||||
SELECT
|
||||
IFNULL(s.location_id, p.location_id) AS location_id,
|
||||
s.product_id,
|
||||
SUM(s.amount) AS amount,
|
||||
SUM(s.amount / s.qu_factor_purchase_to_stock) as factor_purchase_amount,
|
||||
ROUND(SUM(IFNULL(s.price, 0) * (s.amount / s.qu_factor_purchase_to_stock)),2) AS value,
|
||||
MIN(s.best_before_date) AS best_before_date,
|
||||
IFNULL((SELECT SUM(amount) FROM stock WHERE product_id = s.product_id AND location_id = s.location_id AND open = 1), 0) AS amount_opened
|
||||
FROM stock s
|
||||
JOIN products p
|
||||
ON s.product_id = p.id
|
||||
GROUP BY IFNULL(s.location_id, p.location_id), s.product_id;
|
||||
|
||||
DROP VIEW stock_current;
|
||||
CREATE VIEW stock_current
|
||||
AS
|
||||
SELECT
|
||||
pr.parent_product_id AS product_id,
|
||||
IFNULL((SELECT SUM(amount) FROM stock WHERE product_id = pr.parent_product_id), 0) AS amount,
|
||||
IFNULL((SELECT SUM(amount / qu_factor_purchase_to_stock) FROM stock WHERE product_id = pr.parent_product_id), 0) as factor_purchase_amount,
|
||||
SUM(s.amount) * IFNULL(qucr.factor, 1) AS amount_aggregated,
|
||||
IFNULL((SELECT SUM(IFNULL(price,0) * (amount / qu_factor_purchase_to_stock)) FROM stock WHERE product_id = pr.parent_product_id), 0) AS value,
|
||||
MIN(s.best_before_date) AS best_before_date,
|
||||
IFNULL((SELECT SUM(amount) FROM stock WHERE product_id = pr.parent_product_id AND open = 1), 0) AS amount_opened,
|
||||
IFNULL((SELECT SUM(amount) FROM stock WHERE product_id IN (SELECT sub_product_id FROM products_resolved WHERE parent_product_id = pr.parent_product_id) AND open = 1), 0) * IFNULL(qucr.factor, 1) AS amount_opened_aggregated,
|
||||
CASE WHEN p_sub.parent_product_id IS NOT NULL THEN 1 ELSE 0 END AS is_aggregated_amount
|
||||
FROM products_resolved pr
|
||||
JOIN stock s
|
||||
ON pr.sub_product_id = s.product_id
|
||||
JOIN products p_parent
|
||||
ON pr.parent_product_id = p_parent.id
|
||||
JOIN products p_sub
|
||||
ON pr.sub_product_id = p_sub.id
|
||||
LEFT JOIN quantity_unit_conversions_resolved qucr
|
||||
ON pr.sub_product_id = qucr.product_id
|
||||
AND p_sub.qu_id_stock = qucr.from_qu_id
|
||||
AND p_parent.qu_id_stock = qucr.to_qu_id
|
||||
GROUP BY pr.parent_product_id
|
||||
HAVING SUM(s.amount) > 0
|
||||
|
||||
UNION
|
||||
|
||||
-- This is the same as above but sub products not rolled up (no QU conversion and column is_aggregated_amount = 0 here)
|
||||
SELECT
|
||||
pr.sub_product_id AS product_id,
|
||||
SUM(s.amount) AS amount,
|
||||
SUM(s.amount / s.qu_factor_purchase_to_stock) as factor_purchase_amount,
|
||||
SUM(s.amount) AS amount_aggregated,
|
||||
SUM(IFNULL(s.price, 0) * (s.amount / s.qu_factor_purchase_to_stock)) AS value,
|
||||
MIN(s.best_before_date) AS best_before_date,
|
||||
IFNULL((SELECT SUM(amount) FROM stock WHERE product_id = s.product_id AND open = 1), 0) AS amount_opened,
|
||||
IFNULL((SELECT SUM(amount) FROM stock WHERE product_id = s.product_id AND open = 1), 0) AS amount_opened_aggregated,
|
||||
0 AS is_aggregated_amount
|
||||
FROM products_resolved pr
|
||||
JOIN stock s
|
||||
ON pr.sub_product_id = s.product_id
|
||||
WHERE pr.parent_product_id != pr.sub_product_id
|
||||
GROUP BY pr.sub_product_id
|
||||
HAVING SUM(s.amount) > 0;
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ class StockService extends BaseService
|
|||
$missingProductsView = 'stock_missing_products';
|
||||
}
|
||||
|
||||
$sql = 'SELECT * FROM stock_current WHERE best_before_date IS NOT NULL UNION SELECT id, 0, 0, null, 0, 0, 0 FROM ' . $missingProductsView . ' WHERE id NOT IN (SELECT product_id FROM stock_current)';
|
||||
$sql = 'SELECT * FROM stock_current WHERE best_before_date IS NOT NULL UNION SELECT id, 0, 0, 0, 0, null, 0, 0, 0 FROM ' . $missingProductsView . ' WHERE id NOT IN (SELECT product_id FROM stock_current)';
|
||||
}
|
||||
$currentStockMapped = $this->getDatabaseService()->ExecuteDbQuery($sql)->fetchAll(\PDO::FETCH_GROUP|\PDO::FETCH_OBJ);
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user