From ef9f28d1547afea6ab2a6ff67ec5526eaac96925 Mon Sep 17 00:00:00 2001
From: kriddles <54413450+kriddles@users.noreply.github.com>
Date: Wed, 22 Jan 2020 14:08:49 -0600
Subject: [PATCH] 421 (#507)
* refactor to be stock/entry/{entryId}
* Allow recipeform add productworkflow
* on document ready stockdetail filter based on ProductPicker
* openDate fixes for undo
* RefreshStockDetailRow refresh location-id
---
controllers/StockApiController.php | 7 +-
grocy.openapi.json | 163 +++++++++++++++--------------
public/viewjs/stockdetail.js | 8 +-
public/viewjs/stockedit.js | 5 +-
routes.php | 4 +-
services/StockService.php | 18 +++-
views/recipeform.blade.php | 1 -
views/stockdetail.blade.php | 2 +-
8 files changed, 114 insertions(+), 94 deletions(-)
diff --git a/controllers/StockApiController.php b/controllers/StockApiController.php
index 2f1b682e..371c86d7 100644
--- a/controllers/StockApiController.php
+++ b/controllers/StockApiController.php
@@ -124,11 +124,6 @@ class StockApiController extends BaseApiController
throw new \Exception('Request body could not be parsed (probably invalid JSON format or missing/wrong Content-Type header)');
}
- if (!array_key_exists('id', $requestBody))
- {
- throw new \Exception('A stock row id is required');
- }
-
if (!array_key_exists('amount', $requestBody))
{
throw new \Exception('An amount is required');
@@ -152,7 +147,7 @@ class StockApiController extends BaseApiController
$locationId = $requestBody['location_id'];
}
- $bookingId = $this->StockService->EditStockEntry($requestBody['id'], $requestBody['amount'], $bestBeforeDate, $locationId, $price, $requestBody['open']);
+ $bookingId = $this->StockService->EditStockEntry($args['entryId'], $requestBody['amount'], $bestBeforeDate, $locationId, $price, $requestBody['open']);
return $this->ApiResponse($this->Database->stock_log($bookingId));
}
catch (\Exception $ex)
diff --git a/grocy.openapi.json b/grocy.openapi.json
index 66214e02..bd701c4b 100644
--- a/grocy.openapi.json
+++ b/grocy.openapi.json
@@ -1050,86 +1050,9 @@
}
}
}
- },
- "put": {
- "summary": "Edits the stock entry",
- "tags": [
- "Stock"
- ],
- "requestBody": {
- "required": true,
- "content": {
- "application/json": {
- "schema": {
- "type": "object",
- "properties": {
- "id": {
- "type": "number",
- "format": "number",
- "description": "The stock table id"
- },
- "amount": {
- "type": "number",
- "format": "number",
- "description": "The amount to add - please note that when tare weight handling for the product is enabled, this needs to be the amount including the container weight (gross), the amount to be posted will be automatically calculated based on what is in stock and the defined tare weight"
- },
- "best_before_date": {
- "type": "string",
- "format": "date",
- "description": "The best before date of the product to add, when omitted, the current date is used"
- },
- "location_id": {
- "type": "number",
- "format": "integer",
- "description": "If omitted, the default location of the product is used"
- },
- "price": {
- "type": "number",
- "format": "number",
- "description": "The price per purchase quantity unit in configured currency"
- },
- "open": {
- "type": "boolean",
- "description": "If the stock entry was already opened or not"
- }
- },
- "example": {
- "id": 2,
- "amount": 1,
- "best_before_date": "2019-01-19",
- "location_id": 2,
- "price": "1.99",
- "open": false
- }
- }
- }
- }
- },
- "responses": {
- "200": {
- "description": "The operation was successful",
- "content": {
- "application/json": {
- "schema": {
- "$ref": "#/components/schemas/StockLogEntry"
- }
- }
- }
- },
- "400": {
- "description": "The operation was not successful (possible errors are: Not existing product, invalid transaction type)",
- "content": {
- "application/json": {
- "schema": {
- "$ref": "#/components/schemas/GenericErrorResponse"
- }
- }
- }
- }
- }
}
},
- "/stock/{entryId}/entry": {
+ "/stock/entry/{entryId}": {
"get": {
"summary": "Returns details of the given stock",
"tags": [
@@ -1168,6 +1091,90 @@
}
}
}
+ },
+ "put": {
+ "summary": "Edits the stock entry",
+ "tags": [
+ "Stock"
+ ],
+ "parameters": [
+ {
+ "in": "path",
+ "name": "entryId",
+ "required": true,
+ "description": "A valid stock row id",
+ "schema": {
+ "type": "integer"
+ }
+ }
+ ],
+ "requestBody": {
+ "required": true,
+ "content": {
+ "application/json": {
+ "schema": {
+ "type": "object",
+ "properties": {
+ "amount": {
+ "type": "number",
+ "format": "number",
+ "description": "The amount to add - please note that when tare weight handling for the product is enabled, this needs to be the amount including the container weight (gross), the amount to be posted will be automatically calculated based on what is in stock and the defined tare weight"
+ },
+ "best_before_date": {
+ "type": "string",
+ "format": "date",
+ "description": "The best before date of the product to add, when omitted, the current date is used"
+ },
+ "price": {
+ "type": "number",
+ "format": "number",
+ "description": "The price per purchase quantity unit in configured currency"
+ },
+ "open": {
+ "type": "boolean",
+ "description": "If the stock entry was already opened or not"
+ },
+ "location_id": {
+ "type": "number",
+ "format": "integer",
+ "description": "If omitted, the default location of the product is used"
+ }
+ },
+ "example": {
+ "id": "2",
+ "amount": "1",
+ "best_before_date": "2021-07-19",
+ "purchased_date": "2020-01-01",
+ "price": "22.03",
+ "open": false,
+ "location_id": "4"
+ }
+ }
+ }
+ }
+ },
+ "responses": {
+ "200": {
+ "description": "The operation was successful",
+ "content": {
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/StockEntry"
+ }
+ }
+ }
+ },
+ "400": {
+ "description": "The operation was not successful (possible errors are: Not existing product, invalid transaction type)",
+ "content": {
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/GenericErrorResponse"
+ }
+ }
+ }
+ }
+ }
}
},
"/stock/volatile": {
diff --git a/public/viewjs/stockdetail.js b/public/viewjs/stockdetail.js
index 0772bd4a..13d4b093 100644
--- a/public/viewjs/stockdetail.js
+++ b/public/viewjs/stockdetail.js
@@ -19,7 +19,6 @@ $.fn.dataTable.ext.search.push(function(settings, data, dataIndex)
return false;
});
-Grocy.Components.ProductPicker.GetPicker().trigger('change');
Grocy.Components.ProductPicker.GetPicker().on('change', function(e)
{
@@ -116,7 +115,7 @@ $(document).on("click", ".stock-name-cell", function(e)
function RefreshStockDetailRow(stockRowId)
{
- Grocy.Api.Get("stock/" + stockRowId + "/entry",
+ Grocy.Api.Get("stock/entry/" + stockRowId,
function(result)
{
var stockRow = $('#stock-' + stockRowId + '-row');
@@ -161,6 +160,8 @@ function RefreshStockDetailRow(stockRowId)
});
$('#stock-' + stockRowId + '-best-before-date-timeago').attr('datetime', result.best_before_date + ' 23:59:59');
+ $(".stock-consume-button").attr('data-location-id',result.location_id);
+
var locationName = "";
Grocy.Api.Get("objects/locations/" + result.location_id,
function(locationResult)
@@ -175,6 +176,7 @@ function RefreshStockDetailRow(stockRowId)
$('#stock-' + stockRowId + '-location').parent().effect('highlight', { }, 500);
$('#stock-' + stockRowId + '-location').fadeOut(500, function()
{
+ $(this).attr('data-location-id',result.location_id);
$(this).text(locationName).fadeIn(500);
});
@@ -231,6 +233,8 @@ $(window).on("message", function(e)
}
});
+Grocy.Components.ProductPicker.GetPicker().trigger('change');
+
function UndoStockBookingEntry(bookingId, stockRowId)
{
Grocy.Api.Post('stock/bookings/' + bookingId.toString() + '/undo', { },
diff --git a/public/viewjs/stockedit.js b/public/viewjs/stockedit.js
index ef1790d7..e7da8594 100644
--- a/public/viewjs/stockedit.js
+++ b/public/viewjs/stockedit.js
@@ -26,9 +26,8 @@
jsonData.open = $("#open").is(":checked");
var stockRowId = GetUriParam('stockRowId');
- jsonData.id = stockRowId;
- Grocy.Api.Put("stock", jsonData,
+ Grocy.Api.Put("stock/entry/" + stockRowId, jsonData,
function(result)
{
var successMessage = __t('Stock entry successfully updated') + '
' + __t("Undo") + '';
@@ -84,7 +83,7 @@ if (Grocy.Components.DateTimePicker)
}
var stockRowId = GetUriParam('stockRowId');
-Grocy.Api.Get("stock/" + stockRowId + "/entry",
+Grocy.Api.Get("stock/entry/" + stockRowId,
function (stockEntry)
{
Grocy.Components.LocationPicker.SetId(stockEntry.location_id);
diff --git a/routes.php b/routes.php
index 3032605b..967814ab 100644
--- a/routes.php
+++ b/routes.php
@@ -161,8 +161,8 @@ $app->group('/api', function()
if (GROCY_FEATURE_FLAG_STOCK)
{
$this->get('/stock', '\Grocy\Controllers\StockApiController:CurrentStock');
- $this->get('/stock/{entryId}/entry', '\Grocy\Controllers\StockApiController:StockEntry');
- $this->put('/stock', '\Grocy\Controllers\StockApiController:EditStockEntry');
+ $this->get('/stock/entry/{entryId}', '\Grocy\Controllers\StockApiController:StockEntry');
+ $this->put('/stock/entry/{entryId}', '\Grocy\Controllers\StockApiController:EditStockEntry');
$this->get('/stock/volatile', '\Grocy\Controllers\StockApiController:CurrentVolatileStock');
$this->get('/stock/products/{productId}', '\Grocy\Controllers\StockApiController:ProductDetails');
$this->get('/stock/products/{productId}/entries', '\Grocy\Controllers\StockApiController:ProductStockEntries');
diff --git a/services/StockService.php b/services/StockService.php
index c4b43faf..be55daeb 100644
--- a/services/StockService.php
+++ b/services/StockService.php
@@ -591,11 +591,19 @@ class StockService extends BaseService
));
$logOldRowForStockUpdate->save();
+ $openDate = $stockRow->opened_date;
+ if ($open && $openDate == null) {
+ $openDate = date('Y-m-d');
+ } else if (!$open) {
+ $openDate = null;
+ }
+
$stockRow->update(array(
'amount' => $amount,
'price' => $price,
'best_before_date' => $bestBeforeDate,
'location_id' => $locationId,
+ 'opened_date' => $openDate,
'open' => $open
));
@@ -1071,12 +1079,20 @@ class StockService extends BaseService
throw new \Exception('Booking does not exist or was already undone');
}
+ $openDate = $logRow->opened_date;
+ $open = true;
+ if ($openDate == null) {
+ $open = false;
+ }
+
$stockRow->update(array(
'amount' => $logRow->amount,
'best_before_date' => $logRow->best_before_date,
'purchased_date' => $logRow->purchased_date,
'price' => $logRow->price,
- 'location_id' => $logRow->location_id
+ 'location_id' => $logRow->location_id,
+ 'open' => $open,
+ 'opened_date' => $openDate
));
// Update log entry
diff --git a/views/recipeform.blade.php b/views/recipeform.blade.php
index 75b0efd3..6dba08ca 100644
--- a/views/recipeform.blade.php
+++ b/views/recipeform.blade.php
@@ -88,7 +88,6 @@
'isRequired' => false,
'label' => 'Produces product',
'prefillById' => $recipe->product_id,
- 'disallowAllProductWorkflows' => true,
'hint' => $__t('When a product is selected, one unit (per serving in purchase quantity unit) will be added to stock on consuming this recipe')
))
diff --git a/views/stockdetail.blade.php b/views/stockdetail.blade.php
index 1b11535b..fe3e3560 100644
--- a/views/stockdetail.blade.php
+++ b/views/stockdetail.blade.php
@@ -64,10 +64,10 @@
data-stockrow-id="{{ $currentStockEntry->id }}">
+ @endif
- @endif