diff --git a/migrations/0095.sql b/migrations/0095.sql index dfd01779..94931be1 100644 --- a/migrations/0095.sql +++ b/migrations/0095.sql @@ -20,6 +20,25 @@ ADD correlation_id TEXT; ALTER TABLE stock_log ADD transaction_id TEXT; +ALTER TABLE recipes +ADD product_id INTEGER; + +DROP VIEW recipes_resolved; +CREATE VIEW recipes_resolved +AS +SELECT + 1 AS id, -- Dummy, LessQL needs an id column + r.id AS recipe_id, + IFNULL(MIN(rpr.need_fulfilled), 1) AS need_fulfilled, + IFNULL(MIN(rpr.need_fulfilled_with_shopping_list), 1) AS need_fulfilled_with_shopping_list, ( + SELECT COUNT(*) FROM recipes_pos_resolved WHERE recipe_id = r.id AND need_fulfilled = 0) AS missing_products_count, + IFNULL(SUM(rpr.costs), 0) AS costs, + IFNULL(SUM(rpr.calories), 0) AS calories +FROM recipes r +LEFT JOIN recipes_pos_resolved rpr + ON r.id = rpr.recipe_id +GROUP BY r.id; + ALTER TABLE stock_log ADD stock_row_id INTEGER; diff --git a/public/viewjs/recipeform.js b/public/viewjs/recipeform.js index 78cfdb40..fcd50bd6 100644 --- a/public/viewjs/recipeform.js +++ b/public/viewjs/recipeform.js @@ -1,4 +1,8 @@ -$('#save-recipe-button').on('click', function(e) +$(document).ready(function() { + Grocy.Components.ProductPicker.GetPicker().trigger('change'); +} ); + +$('#save-recipe-button').on('click', function(e) { e.preventDefault(); @@ -349,3 +353,13 @@ $(window).on("message", function(e) } }); + +Grocy.Components.ProductPicker.GetPicker().on('change', function(e) +{ + var productId = $(e.target).val(); + + if (productId) + { + Grocy.Components.ProductCard.Refresh(productId); + } +}); diff --git a/services/RecipesService.php b/services/RecipesService.php index 966e1f5c..85d1a9c8 100644 --- a/services/RecipesService.php +++ b/services/RecipesService.php @@ -67,6 +67,15 @@ class RecipesService extends BaseService throw new \Exception('Recipe does not exist'); } + $recipeRow = $this->Database->recipes()->where('id = :1', $recipeId)->fetch(); + if ($recipeRow->product_id != null) + { + $recipeResolvedRow = $this->Database->recipes_resolved()->where('recipe_id = :1', $recipeId)->fetch(); + $price = number_format($recipeResolvedRow->costs, 2); + $bookingAmount = floatval($recipeRow->desired_servings); + $this->StockService->AddProduct($recipeRow->product_id, $bookingAmount, null, StockService::TRANSACTION_TYPE_PURCHASE, date('Y-m-d'), $price); + } + $recipePositions = $this->Database->recipes_pos_resolved()->where('recipe_id', $recipeId)->fetchAll(); foreach ($recipePositions as $recipePosition) { diff --git a/views/recipeform.blade.php b/views/recipeform.blade.php index 4e68b275..f7c3b782 100644 --- a/views/recipeform.blade.php +++ b/views/recipeform.blade.php @@ -63,6 +63,12 @@ 'hint' => $__t('The ingredients listed here result in this amount of servings') )) + @include('components.productpicker', array( + 'products' => $products, + 'isRequired' => false, + 'label' => 'Creates Product' + )) +