Adapt shopping list add expired products for #851

This commit is contained in:
Bernd Bestel
2020-11-15 22:38:21 +01:00
parent 1bbd7787d8
commit 7b4edf3147
8 changed files with 110 additions and 9 deletions

View File

@@ -77,7 +77,7 @@
### Shopping list improvements ### Shopping list improvements
- Decimal amounts are now allowed (for any product, rounded by two decimal places) - Decimal amounts are now allowed (for any product, rounded by two decimal places)
- Added a button to add all currently in-stock but expired products to the shopping list (thanks @m-byte) - Added a button to add all currently in-stock but overdue and expired products to the shopping list (thanks @m-byte)
- Improved that when `FEATURE_FLAG_STOCK` is disabled, all product/stock related inputs and buttons are now hidden on the shopping list page (thanks @fipwmaqzufheoxq92ebc) - Improved that when `FEATURE_FLAG_STOCK` is disabled, all product/stock related inputs and buttons are now hidden on the shopping list page (thanks @fipwmaqzufheoxq92ebc)
- Fixed that "Add products that are below defined min. stock amount" always rounded up the missing amount to an integral number, this now allows decimal numbers - Fixed that "Add products that are below defined min. stock amount" always rounded up the missing amount to an integral number, this now allows decimal numbers
@@ -190,6 +190,7 @@
- `<value>` is the value to search for - `<value>` is the value to search for
- New endpoints `/stock/journal` and `/stock/journal/summary` to get the stock journal (thanks @fipwmaqzufheoxq92ebc) - New endpoints `/stock/journal` and `/stock/journal/summary` to get the stock journal (thanks @fipwmaqzufheoxq92ebc)
- New endpoint `/stock/shoppinglist/add-overdue-products` to add all currently in-stock but overdue products to a shopping list (thanks @m-byte) - New endpoint `/stock/shoppinglist/add-overdue-products` to add all currently in-stock but overdue products to a shopping list (thanks @m-byte)
- New endpoint `/stock/shoppinglist/add-expired-products` to add all currently in-stock but expired products to a shopping list
- New endpoints GET/POST/PUT `/users/{userId}/permissions` for the new user permissions feature mentioned above - New endpoints GET/POST/PUT `/users/{userId}/permissions` for the new user permissions feature mentioned above
- Performance improvements of the `/stock/products/*` endpoints (thanks @fipwmaqzufheoxq92ebc) - Performance improvements of the `/stock/products/*` endpoints (thanks @fipwmaqzufheoxq92ebc)
- Fixed that the endpoint `/objects/{entity}/{objectId}` always returned successfully, even when the given object not exists (now returns `404` when the object is not found) (thanks @fipwmaqzufheoxq92ebc) - Fixed that the endpoint `/objects/{entity}/{objectId}` always returned successfully, even when the given object not exists (now returns `404` when the object is not found) (thanks @fipwmaqzufheoxq92ebc)

View File

@@ -55,6 +55,30 @@ class StockApiController extends BaseApiController
} }
} }
public function AddExpiredProductsToShoppingList(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
{
User::checkPermission($request, User::PERMISSION_SHOPPINGLIST_ITEMS_ADD);
try
{
$requestBody = $this->GetParsedAndFilteredRequestBody($request);
$listId = 1;
if (array_key_exists('list_id', $requestBody) && !empty($requestBody['list_id']) && is_numeric($requestBody['list_id']))
{
$listId = intval($requestBody['list_id']);
}
$this->getStockService()->AddExpiredProductsToShoppingList($listId);
return $this->EmptyApiResponse($response);
}
catch (\Exception $ex)
{
return $this->GenericErrorResponse($response, $ex->getMessage());
}
}
public function AddProduct(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args) public function AddProduct(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
{ {
User::checkPermission($request, User::PERMISSION_STOCK_PURCHASE); User::checkPermission($request, User::PERMISSION_STOCK_PURCHASE);

View File

@@ -2572,6 +2572,48 @@
} }
} }
}, },
"/stock/shoppinglist/add-expired-products": {
"post": {
"summary": "Adds expired products to the given shopping list",
"tags": [
"Stock"
],
"requestBody": {
"required": false,
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"list_id": {
"type": "integer",
"description": "The shopping list to use, when omitted, the default shopping list (with id 1) is used"
}
},
"example": {
"list_id": 2
}
}
}
}
},
"responses": {
"204": {
"description": "The operation was successful"
},
"400": {
"description": "The operation was not successful (possible errors are: Not existing shopping list)",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error400"
}
}
}
}
}
}
},
"/stock/shoppinglist/clear": { "/stock/shoppinglist/clear": {
"post": { "post": {
"summary": "Removes all items from the given shopping list", "summary": "Removes all items from the given shopping list",

View File

@@ -1798,9 +1798,6 @@ msgstr ""
msgid "Journal summary for this product" msgid "Journal summary for this product"
msgstr "" msgstr ""
msgid "Add expired products"
msgstr ""
msgid "Consume exact amount" msgid "Consume exact amount"
msgstr "" msgstr ""
@@ -1981,3 +1978,6 @@ msgstr ""
msgid "Due soon days" msgid "Due soon days"
msgstr "" msgstr ""
msgid "Add overdue/expired products"
msgstr ""

View File

@@ -174,12 +174,21 @@ $(document).on('click', '#add-products-below-min-stock-amount', function(e)
); );
}); });
$(document).on('click', '#add-overdue-products', function(e) $(document).on('click', '#add-overdue-expired-products', function(e)
{ {
Grocy.Api.Post('stock/shoppinglist/add-overdue-products', { "list_id": $("#selected-shopping-list").val() }, Grocy.Api.Post('stock/shoppinglist/add-overdue-products', { "list_id": $("#selected-shopping-list").val() },
function(result) function(result)
{ {
window.location.href = U('/shoppinglist?list=' + $("#selected-shopping-list").val()); Grocy.Api.Post('stock/shoppinglist/add-expired-products', { "list_id": $("#selected-shopping-list").val() },
function(result)
{
window.location.href = U('/shoppinglist?list=' + $("#selected-shopping-list").val());
},
function(xhr)
{
console.error(xhr);
}
);
}, },
function(xhr) function(xhr)
{ {

View File

@@ -211,6 +211,7 @@ $app->group('/api', function (RouteCollectorProxy $group) {
{ {
$group->post('/stock/shoppinglist/add-missing-products', '\Grocy\Controllers\StockApiController:AddMissingProductsToShoppingList'); $group->post('/stock/shoppinglist/add-missing-products', '\Grocy\Controllers\StockApiController:AddMissingProductsToShoppingList');
$group->post('/stock/shoppinglist/add-overdue-products', '\Grocy\Controllers\StockApiController:AddOverdueProductsToShoppingList'); $group->post('/stock/shoppinglist/add-overdue-products', '\Grocy\Controllers\StockApiController:AddOverdueProductsToShoppingList');
$group->post('/stock/shoppinglist/add-expired-products', '\Grocy\Controllers\StockApiController:AddExpiredProductsToShoppingList');
$group->post('/stock/shoppinglist/clear', '\Grocy\Controllers\StockApiController:ClearShoppingList'); $group->post('/stock/shoppinglist/clear', '\Grocy\Controllers\StockApiController:ClearShoppingList');
$group->post('/stock/shoppinglist/add-product', '\Grocy\Controllers\StockApiController:AddProductToShoppingList'); $group->post('/stock/shoppinglist/add-product', '\Grocy\Controllers\StockApiController:AddProductToShoppingList');
$group->post('/stock/shoppinglist/remove-product', '\Grocy\Controllers\StockApiController:RemoveProductFromShoppingList'); $group->post('/stock/shoppinglist/remove-product', '\Grocy\Controllers\StockApiController:RemoveProductFromShoppingList');

View File

@@ -60,7 +60,6 @@ class StockService extends BaseService
} }
$overdueProducts = $this->GetDueProducts(-1); $overdueProducts = $this->GetDueProducts(-1);
foreach ($overdueProducts as $overdueProduct) foreach ($overdueProducts as $overdueProduct)
{ {
$product = $this->getDatabase()->products()->where('id', $overdueProduct->product_id)->fetch(); $product = $this->getDatabase()->products()->where('id', $overdueProduct->product_id)->fetch();
@@ -78,6 +77,31 @@ class StockService extends BaseService
} }
} }
public function AddExpiredProductsToShoppingList($listId = 1)
{
if (!$this->ShoppingListExists($listId))
{
throw new \Exception('Shopping list does not exist');
}
$expiredProducts = $this->GetExpiredProducts();
foreach ($expiredProducts as $expiredProduct)
{
$product = $this->getDatabase()->products()->where('id', $expiredProduct->product_id)->fetch();
$alreadyExistingEntry = $this->getDatabase()->shopping_list()->where('product_id', $expiredProduct->product_id)->fetch();
if (!$alreadyExistingEntry)
{
$shoppinglistRow = $this->getDatabase()->shopping_list()->createRow([
'product_id' => $expiredProduct->product_id,
'amount' => 1,
'shopping_list_id' => $listId
]);
$shoppinglistRow->save();
}
}
}
public function AddProduct(int $productId, float $amount, $bestBeforeDate, $transactionType, $purchasedDate, $price, $locationId = null, $shoppingLocationId = null, &$transactionId = null) public function AddProduct(int $productId, float $amount, $bestBeforeDate, $transactionType, $purchasedDate, $price, $locationId = null, $shoppingLocationId = null, &$transactionId = null)
{ {
if (!$this->ProductExists($productId)) if (!$this->ProductExists($productId))

View File

@@ -115,10 +115,10 @@
href="#"> href="#">
{{ $__t('Add products that are below defined min. stock amount') }} {{ $__t('Add products that are below defined min. stock amount') }}
</a> </a>
<a id="add-overdue-products" <a id="add-overdue-expired-products"
class="btn btn-outline-primary btn-sm mb-1 responsive-button @if(!GROCY_FEATURE_FLAG_STOCK) d-none @endif" class="btn btn-outline-primary btn-sm mb-1 responsive-button @if(!GROCY_FEATURE_FLAG_STOCK) d-none @endif"
href="#"> href="#">
{{ $__t('Add overdue products') }} {{ $__t('Add overdue/expired products') }}
</a> </a>
</div> </div>
</div> </div>