| 
									
										
										
										
											2019-09-26 10:36:49 +02:00
										 |  |  | ALTER TABLE products
 | 
					
						
							| 
									
										
										
										
											2019-09-26 12:45:35 +02:00
										 |  |  | ADD cumulate_min_stock_amount_of_sub_products TINYINT DEFAULT 0;
 | 
					
						
							| 
									
										
										
										
											2019-09-26 10:36:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-26 13:13:49 +02:00
										 |  |  | CREATE VIEW products_view
 | 
					
						
							|  |  |  | AS
 | 
					
						
							|  |  |  | SELECT *, CASE WHEN (SELECT 1 FROM products WHERE parent_product_id = p.id) NOTNULL THEN 1 ELSE 0 END AS has_sub_products
 | 
					
						
							|  |  |  | FROM products p;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-26 10:36:49 +02:00
										 |  |  | DROP VIEW stock_missing_products;
 | 
					
						
							|  |  |  | CREATE VIEW stock_missing_products
 | 
					
						
							|  |  |  | AS
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-26 13:13:49 +02:00
										 |  |  | -- Products WITHOUT sub products where the amount of the sub products SHOULD NOT be cumulated
 | 
					
						
							| 
									
										
										
										
											2019-09-26 10:36:49 +02:00
										 |  |  | SELECT
 | 
					
						
							|  |  |  | 	p.id,
 | 
					
						
							|  |  |  | 	MAX(p.name) AS name,
 | 
					
						
							|  |  |  | 	p.min_stock_amount - IFNULL(SUM(s.amount), 0) AS amount_missing,
 | 
					
						
							|  |  |  | 	CASE WHEN IFNULL(SUM(s.amount), 0) > 0 THEN 1 ELSE 0 END AS is_partly_in_stock
 | 
					
						
							| 
									
										
										
										
											2019-09-26 13:13:49 +02:00
										 |  |  | FROM products_view p
 | 
					
						
							| 
									
										
										
										
											2019-09-26 10:36:49 +02:00
										 |  |  | LEFT JOIN stock_current s
 | 
					
						
							|  |  |  | 	ON p.id = s.product_id
 | 
					
						
							|  |  |  | WHERE p.min_stock_amount != 0
 | 
					
						
							| 
									
										
										
										
											2019-09-26 12:45:35 +02:00
										 |  |  | 	AND p.cumulate_min_stock_amount_of_sub_products = 0
 | 
					
						
							| 
									
										
										
										
											2019-09-26 13:13:49 +02:00
										 |  |  | 	AND p.has_sub_products = 0
 | 
					
						
							|  |  |  | 	AND p.parent_product_id IS NULL
 | 
					
						
							| 
									
										
										
										
											2019-09-26 10:36:49 +02:00
										 |  |  | GROUP BY p.id
 | 
					
						
							|  |  |  | HAVING IFNULL(SUM(s.amount), 0) < p.min_stock_amount
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | UNION
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-26 13:13:49 +02:00
										 |  |  | -- Parent products WITH sub products where the amount of the sub products SHOULD be cumulated
 | 
					
						
							| 
									
										
										
										
											2019-09-26 10:36:49 +02:00
										 |  |  | SELECT
 | 
					
						
							|  |  |  | 	p.id,
 | 
					
						
							|  |  |  | 	MAX(p.name) AS name,
 | 
					
						
							| 
									
										
										
										
											2019-09-26 20:56:19 +02:00
										 |  |  | 	SUM(sub_p.min_stock_amount) - IFNULL(SUM(s.amount_aggregated), 0) AS amount_missing,
 | 
					
						
							| 
									
										
										
										
											2019-09-26 10:36:49 +02:00
										 |  |  | 	CASE WHEN IFNULL(SUM(s.amount), 0) > 0 THEN 1 ELSE 0 END AS is_partly_in_stock
 | 
					
						
							| 
									
										
										
										
											2019-09-26 13:13:49 +02:00
										 |  |  | FROM products_view p
 | 
					
						
							| 
									
										
										
										
											2019-09-26 10:36:49 +02:00
										 |  |  | JOIN products_resolved pr
 | 
					
						
							|  |  |  | 	ON p.id = pr.parent_product_id
 | 
					
						
							|  |  |  | JOIN products sub_p
 | 
					
						
							|  |  |  | 	ON pr.sub_product_id = sub_p.id
 | 
					
						
							|  |  |  | LEFT JOIN stock_current s
 | 
					
						
							|  |  |  | 	ON pr.sub_product_id = s.product_id
 | 
					
						
							|  |  |  | WHERE sub_p.min_stock_amount != 0
 | 
					
						
							| 
									
										
										
										
											2019-09-26 12:45:35 +02:00
										 |  |  | 	AND p.cumulate_min_stock_amount_of_sub_products = 1
 | 
					
						
							| 
									
										
										
										
											2019-09-26 10:36:49 +02:00
										 |  |  | GROUP BY p.id
 | 
					
						
							| 
									
										
										
										
											2019-09-26 20:56:19 +02:00
										 |  |  | HAVING IFNULL(SUM(s.amount_aggregated), 0) < SUM(sub_p.min_stock_amount)
 | 
					
						
							| 
									
										
										
										
											2019-09-26 10:36:49 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | UNION
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-26 12:43:00 +02:00
										 |  |  | -- Sub products where the amount SHOULD NOT be cumulated into the parent product
 | 
					
						
							| 
									
										
										
										
											2019-09-26 10:36:49 +02:00
										 |  |  | SELECT
 | 
					
						
							|  |  |  | 	sub_p.id,
 | 
					
						
							|  |  |  | 	MAX(sub_p.name) AS name,
 | 
					
						
							|  |  |  | 	SUM(sub_p.min_stock_amount) - IFNULL(SUM(s.amount), 0) AS amount_missing,
 | 
					
						
							|  |  |  | 	CASE WHEN IFNULL(SUM(s.amount), 0) > 0 THEN 1 ELSE 0 END AS is_partly_in_stock
 | 
					
						
							| 
									
										
										
										
											2019-09-26 12:43:00 +02:00
										 |  |  | FROM products p
 | 
					
						
							| 
									
										
										
										
											2019-09-26 10:36:49 +02:00
										 |  |  | JOIN products_resolved pr
 | 
					
						
							|  |  |  | 	ON p.id = pr.parent_product_id
 | 
					
						
							|  |  |  | JOIN products sub_p
 | 
					
						
							|  |  |  | 	ON pr.sub_product_id = sub_p.id
 | 
					
						
							|  |  |  | LEFT JOIN stock_current s
 | 
					
						
							|  |  |  | 	ON pr.sub_product_id = s.product_id
 | 
					
						
							|  |  |  | WHERE sub_p.min_stock_amount != 0
 | 
					
						
							| 
									
										
										
										
											2019-09-26 12:45:35 +02:00
										 |  |  | 	AND p.cumulate_min_stock_amount_of_sub_products = 0
 | 
					
						
							| 
									
										
										
										
											2019-09-26 10:36:49 +02:00
										 |  |  | GROUP BY sub_p.id
 | 
					
						
							|  |  |  | HAVING IFNULL(SUM(s.amount), 0) < sub_p.min_stock_amount;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | DROP VIEW stock_missing_products_including_opened;
 | 
					
						
							|  |  |  | CREATE VIEW stock_missing_products_including_opened
 | 
					
						
							|  |  |  | AS
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-26 12:43:00 +02:00
										 |  |  | /* This is basically the same view as stock_missing_products, but the column "amount_missing" includes opened amounts */
 | 
					
						
							| 
									
										
										
										
											2019-09-26 10:36:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-26 13:13:49 +02:00
										 |  |  | -- Products WITHOUT sub products where the amount of the sub products SHOULD NOT be cumulated
 | 
					
						
							| 
									
										
										
										
											2019-09-26 10:36:49 +02:00
										 |  |  | SELECT
 | 
					
						
							|  |  |  | 	p.id,
 | 
					
						
							|  |  |  | 	MAX(p.name) AS name,
 | 
					
						
							|  |  |  | 	p.min_stock_amount - (IFNULL(SUM(s.amount), 0) - IFNULL(SUM(s.amount_opened), 0)) AS amount_missing,
 | 
					
						
							|  |  |  | 	CASE WHEN IFNULL(SUM(s.amount), 0) > 0 THEN 1 ELSE 0 END AS is_partly_in_stock
 | 
					
						
							| 
									
										
										
										
											2019-09-26 13:13:49 +02:00
										 |  |  | FROM products_view p
 | 
					
						
							| 
									
										
										
										
											2019-09-26 10:36:49 +02:00
										 |  |  | LEFT JOIN stock_current s
 | 
					
						
							|  |  |  | 	ON p.id = s.product_id
 | 
					
						
							|  |  |  | WHERE p.min_stock_amount != 0
 | 
					
						
							| 
									
										
										
										
											2019-09-26 12:45:35 +02:00
										 |  |  | 	AND p.cumulate_min_stock_amount_of_sub_products = 0
 | 
					
						
							| 
									
										
										
										
											2019-09-26 13:13:49 +02:00
										 |  |  | 	AND p.has_sub_products = 0
 | 
					
						
							|  |  |  | 	AND p.parent_product_id IS NULL
 | 
					
						
							| 
									
										
										
										
											2019-09-26 10:36:49 +02:00
										 |  |  | GROUP BY p.id
 | 
					
						
							|  |  |  | HAVING IFNULL(SUM(s.amount), 0) < p.min_stock_amount
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | UNION
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-26 13:13:49 +02:00
										 |  |  | -- Parent products WITH sub products where the amount of the sub products SHOULD be cumulated
 | 
					
						
							| 
									
										
										
										
											2019-09-26 10:36:49 +02:00
										 |  |  | SELECT
 | 
					
						
							|  |  |  | 	p.id,
 | 
					
						
							|  |  |  | 	MAX(p.name) AS name,
 | 
					
						
							| 
									
										
										
										
											2019-09-26 20:56:19 +02:00
										 |  |  | 	SUM(sub_p.min_stock_amount) - (IFNULL(SUM(s.amount_aggregated), 0) - IFNULL(SUM(s.amount_opened), 0)) AS amount_missing,
 | 
					
						
							| 
									
										
										
										
											2019-09-26 10:36:49 +02:00
										 |  |  | 	CASE WHEN IFNULL(SUM(s.amount), 0) > 0 THEN 1 ELSE 0 END AS is_partly_in_stock
 | 
					
						
							| 
									
										
										
										
											2019-09-26 13:13:49 +02:00
										 |  |  | FROM products_view p
 | 
					
						
							| 
									
										
										
										
											2019-09-26 10:36:49 +02:00
										 |  |  | JOIN products_resolved pr
 | 
					
						
							|  |  |  | 	ON p.id = pr.parent_product_id
 | 
					
						
							|  |  |  | JOIN products sub_p
 | 
					
						
							|  |  |  | 	ON pr.sub_product_id = sub_p.id
 | 
					
						
							|  |  |  | LEFT JOIN stock_current s
 | 
					
						
							|  |  |  | 	ON pr.sub_product_id = s.product_id
 | 
					
						
							|  |  |  | WHERE sub_p.min_stock_amount != 0
 | 
					
						
							| 
									
										
										
										
											2019-09-26 12:45:35 +02:00
										 |  |  | 	AND p.cumulate_min_stock_amount_of_sub_products = 1
 | 
					
						
							| 
									
										
										
										
											2019-09-26 10:36:49 +02:00
										 |  |  | GROUP BY p.id
 | 
					
						
							| 
									
										
										
										
											2019-09-26 20:56:19 +02:00
										 |  |  | HAVING IFNULL(SUM(s.amount_aggregated), 0) < SUM(sub_p.min_stock_amount)
 | 
					
						
							| 
									
										
										
										
											2019-09-26 10:36:49 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | UNION
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-26 12:43:00 +02:00
										 |  |  | -- Sub products where the amount SHOULD NOT be cumulated into the parent product
 | 
					
						
							| 
									
										
										
										
											2019-09-26 10:36:49 +02:00
										 |  |  | SELECT
 | 
					
						
							|  |  |  | 	sub_p.id,
 | 
					
						
							|  |  |  | 	MAX(sub_p.name) AS name,
 | 
					
						
							|  |  |  | 	SUM(sub_p.min_stock_amount) - (IFNULL(SUM(s.amount), 0) - IFNULL(SUM(s.amount_opened), 0)) AS amount_missing,
 | 
					
						
							|  |  |  | 	CASE WHEN IFNULL(SUM(s.amount), 0) > 0 THEN 1 ELSE 0 END AS is_partly_in_stock
 | 
					
						
							| 
									
										
										
										
											2019-09-26 12:43:00 +02:00
										 |  |  | FROM products p
 | 
					
						
							| 
									
										
										
										
											2019-09-26 10:36:49 +02:00
										 |  |  | JOIN products_resolved pr
 | 
					
						
							|  |  |  | 	ON p.id = pr.parent_product_id
 | 
					
						
							|  |  |  | JOIN products sub_p
 | 
					
						
							|  |  |  | 	ON pr.sub_product_id = sub_p.id
 | 
					
						
							|  |  |  | LEFT JOIN stock_current s
 | 
					
						
							|  |  |  | 	ON pr.sub_product_id = s.product_id
 | 
					
						
							|  |  |  | WHERE sub_p.min_stock_amount != 0
 | 
					
						
							| 
									
										
										
										
											2019-09-26 12:45:35 +02:00
										 |  |  | 	AND p.cumulate_min_stock_amount_of_sub_products = 0
 | 
					
						
							| 
									
										
										
										
											2019-09-26 10:36:49 +02:00
										 |  |  | GROUP BY sub_p.id
 | 
					
						
							|  |  |  | HAVING IFNULL(SUM(s.amount), 0) < sub_p.min_stock_amount;
 |