Skip to content

Commit 0816359

Browse files
committed
Some refinements for grocy#501
1 parent 3a36bda commit 0816359

File tree

9 files changed

+52
-55
lines changed

9 files changed

+52
-55
lines changed

localization/en/stock_transaction_types.po

+3
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,6 @@ msgstr "Stock entry edited (old values)"
3535

3636
msgid "stock-edit-new"
3737
msgstr "Stock entry edited (new values)"
38+
39+
msgid "self-production"
40+
msgstr "Self-production"

localization/stock_transaction_types.pot

+3
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,6 @@ msgstr ""
3535

3636
msgid "stock-edit-new"
3737
msgstr ""
38+
39+
msgid "self-production"
40+
msgstr ""

localization/strings.pot

+9
Original file line numberDiff line numberDiff line change
@@ -1618,3 +1618,12 @@ msgstr ""
16181618

16191619
msgid "A purchased date is required"
16201620
msgstr ""
1621+
1622+
msgid "When a product is selected, one unit (per serving in purchase quantity unit) will be added to stock on consuming this recipe"
1623+
msgstr ""
1624+
1625+
msgid "Produces product"
1626+
msgstr ""
1627+
1628+
msgid "This booking cannot be undone"
1629+
msgstr ""

migrations/0095.sql

+16-16
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,22 @@ ADD correlation_id TEXT;
2020
ALTER TABLE stock_log
2121
ADD transaction_id TEXT;
2222

23+
ALTER TABLE stock_log
24+
ADD stock_row_id INTEGER;
25+
26+
DROP VIEW stock_current_locations;
27+
CREATE VIEW stock_current_locations
28+
AS
29+
SELECT
30+
1 AS id, -- Dummy, LessQL needs an id column
31+
s.product_id,
32+
s.location_id AS location_id,
33+
l.name AS location_name
34+
FROM stock s
35+
JOIN locations l
36+
ON s.location_id = l.id
37+
GROUP BY s.product_id, s.location_id, l.name;
38+
2339
ALTER TABLE recipes
2440
ADD product_id INTEGER;
2541

@@ -38,19 +54,3 @@ FROM recipes r
3854
LEFT JOIN recipes_pos_resolved rpr
3955
ON r.id = rpr.recipe_id
4056
GROUP BY r.id;
41-
42-
ALTER TABLE stock_log
43-
ADD stock_row_id INTEGER;
44-
45-
DROP VIEW stock_current_locations;
46-
CREATE VIEW stock_current_locations
47-
AS
48-
SELECT
49-
1 AS id, -- Dummy, LessQL needs an id column
50-
s.product_id,
51-
s.location_id AS location_id,
52-
l.name AS location_name
53-
FROM stock s
54-
JOIN locations l
55-
ON s.location_id = l.id
56-
GROUP BY s.product_id, s.location_id, l.name;

public/viewjs/recipeform.js

+1-16
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
$(document).ready(function() {
2-
Grocy.Components.ProductPicker.GetPicker().trigger('change');
3-
} );
4-
5-
$('#save-recipe-button').on('click', function(e)
1+
$('#save-recipe-button').on('click', function(e)
62
{
73
e.preventDefault();
84

@@ -351,15 +347,4 @@ $(window).on("message", function(e)
351347
{
352348
window.location.href = U('/recipe/' + Grocy.EditObjectId);
353349
}
354-
355-
});
356-
357-
Grocy.Components.ProductPicker.GetPicker().on('change', function(e)
358-
{
359-
var productId = $(e.target).val();
360-
361-
if (productId)
362-
{
363-
Grocy.Components.ProductCard.Refresh(productId);
364-
}
365350
});

services/RecipesService.php

+7-9
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,6 @@ public function ConsumeRecipe($recipeId)
6767
throw new \Exception('Recipe does not exist');
6868
}
6969

70-
$recipeRow = $this->Database->recipes()->where('id = :1', $recipeId)->fetch();
71-
if ($recipeRow->product_id != null)
72-
{
73-
$recipeResolvedRow = $this->Database->recipes_resolved()->where('recipe_id = :1', $recipeId)->fetch();
74-
$price = number_format($recipeResolvedRow->costs, 2);
75-
$bookingAmount = floatval($recipeRow->desired_servings);
76-
$this->StockService->AddProduct($recipeRow->product_id, $bookingAmount, null, StockService::TRANSACTION_TYPE_PURCHASE, date('Y-m-d'), $price);
77-
}
78-
7970
$recipePositions = $this->Database->recipes_pos_resolved()->where('recipe_id', $recipeId)->fetchAll();
8071
foreach ($recipePositions as $recipePosition)
8172
{
@@ -84,6 +75,13 @@ public function ConsumeRecipe($recipeId)
8475
$this->StockService->ConsumeProduct($recipePosition->product_id, $recipePosition->recipe_amount, false, StockService::TRANSACTION_TYPE_CONSUME, 'default', $recipeId);
8576
}
8677
}
78+
79+
$recipeRow = $this->Database->recipes()->where('id = :1', $recipeId)->fetch();
80+
if (!empty($recipeRow->product_id))
81+
{
82+
$recipeResolvedRow = $this->Database->recipes_resolved()->where('recipe_id = :1', $recipeId)->fetch();
83+
$this->StockService->AddProduct($recipeRow->product_id, floatval($recipeRow->desired_servings), null, StockService::TRANSACTION_TYPE_SELF_PRODUCTION, date('Y-m-d'), floatval($recipeResolvedRow->costs));
84+
}
8785
}
8886

8987
private function RecipeExists($recipeId)

services/StockService.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class StockService extends BaseService
1212
const TRANSACTION_TYPE_STOCK_EDIT_NEW = 'stock-edit-new';
1313
const TRANSACTION_TYPE_STOCK_EDIT_OLD = 'stock-edit-old';
1414
const TRANSACTION_TYPE_PRODUCT_OPENED = 'product-opened';
15+
const TRANSACTION_TYPE_SELF_PRODUCTION = 'self-production';
1516

1617
public function GetCurrentStock($includeNotInStockButMissingProducts = false)
1718
{
@@ -242,7 +243,7 @@ public function AddProduct(int $productId, float $amount, $bestBeforeDate, $tran
242243
}
243244
}
244245

245-
if ($transactionType === self::TRANSACTION_TYPE_PURCHASE || $transactionType === self::TRANSACTION_TYPE_INVENTORY_CORRECTION)
246+
if ($transactionType === self::TRANSACTION_TYPE_PURCHASE || $transactionType === self::TRANSACTION_TYPE_INVENTORY_CORRECTION || $transactionType == self::TRANSACTION_TYPE_SELF_PRODUCTION)
246247
{
247248
if ($transactionId === null)
248249
{

views/recipeform.blade.php

+10-12
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,7 @@
6161
'value' => $value,
6262
'invalidFeedback' => $__t('This cannot be lower than %s', '1'),
6363
'hint' => $__t('The ingredients listed here result in this amount of servings')
64-
))
65-
66-
@include('components.productpicker', array(
67-
'products' => $products,
68-
'isRequired' => false,
69-
'label' => 'Creates Product'
70-
))
64+
))
7165

7266
<div class="form-group">
7367
<div class="form-check">
@@ -89,6 +83,15 @@
8983
</div>
9084
</div>
9185

86+
@include('components.productpicker', array(
87+
'products' => $products,
88+
'isRequired' => false,
89+
'label' => 'Produces product',
90+
'prefillById' => $recipe->product_id,
91+
'disallowAllProductWorkflows' => true,
92+
'hint' => $__t('When a product is selected, one unit (per serving in purchase quantity unit) will be added to stock on consuming this recipe')
93+
))
94+
9295
@include('components.userfieldsform', array(
9396
'userfields' => $userfields,
9497
'entity' => 'recipes'
@@ -222,11 +225,6 @@
222225
@endif
223226
</div>
224227
</div>
225-
<div class="row mt-5">
226-
<div class="col">
227-
@include('components.productcard')
228-
</div>
229-
</div>
230228
</div>
231229

232230
</div>

views/recipes.blade.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@
137137
<a class="btn btn-sm btn-outline-primary py-0 recipe-order-missing-button hide-when-embedded hide-on-fullscreen-card @if(FindObjectInArrayByPropertyValue($recipesResolved, 'recipe_id', $selectedRecipe->id)->need_fulfilled_with_shopping_list == 1) disabled @endif" href="#" data-toggle="tooltip" title="{{ $__t('Put missing products on shopping list') }}" data-recipe-id="{{ $selectedRecipe->id }}" data-recipe-name="{{ $selectedRecipe->name }}">
138138
<i class="fas fa-cart-plus"></i>
139139
</a>&nbsp;&nbsp;
140-
<a id="selectedRecipeEditButton" class="btn btn-sm btn-outline-info hide-when-embedded hide-on-fullscreen-card py-0" href="{{ $U('/recipe/') }}{{ $selectedRecipe->id }}?product={{ $selectedRecipe->product_id }}">
140+
<a id="selectedRecipeEditButton" class="btn btn-sm btn-outline-info hide-when-embedded hide-on-fullscreen-card py-0" href="{{ $U('/recipe/') }}{{ $selectedRecipe->id }}">
141141
<i class="fas fa-edit"></i>
142142
</a>
143143
<a id="selectedRecipeDeleteButton" class="btn btn-sm btn-outline-danger hide-when-embedded hide-on-fullscreen-card py-0" href="#" data-recipe-id="{{ $selectedRecipe->id }}" data-recipe-name="{{ $selectedRecipe->name }}">

0 commit comments

Comments
 (0)