mirror of
https://github.com/grocy/grocy.git
synced 2025-04-29 17:45:39 +00:00
added button to add expired products to shoppinglist (#1026)
* added button to add expired products to shoppinglist * Localizations are managed via Transifex Revert "added button to add expired products to shoppinglist" This reverts commit ad1ab5d6a0f4bd1dbc88dbb0bda67713d4038014. * Revert unnecessary change * Reuse existing function (GetExpiringProducts) to get expired products Co-authored-by: Mario Klug <mario.klug@sourcefactory.at> Co-authored-by: Bernd Bestel <bernd@berrnd.de>
This commit is contained in:
parent
49588508bb
commit
617b25bda8
@ -31,6 +31,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 = $request->getParsedBody();
|
||||
|
||||
$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)
|
||||
{
|
||||
User::checkPermission($request, User::PERMISSION_STOCK_PURCHASE);
|
||||
|
@ -2356,6 +2356,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/GenericErrorResponse"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/stock/shoppinglist/clear": {
|
||||
"post": {
|
||||
"summary": "Removes all items from the given shopping list",
|
||||
|
@ -1909,3 +1909,6 @@ msgstr ""
|
||||
|
||||
msgid "Journal summary for this product"
|
||||
msgstr ""
|
||||
|
||||
msgid "Add expired products"
|
||||
msgstr ""
|
||||
|
@ -155,6 +155,20 @@ $(document).on('click', '#add-products-below-min-stock-amount', function(e)
|
||||
);
|
||||
});
|
||||
|
||||
$(document).on('click', '#add-expired-products', function(e)
|
||||
{
|
||||
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);
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
$(document).on('click', '#clear-shopping-list', function(e)
|
||||
{
|
||||
bootbox.confirm({
|
||||
|
@ -212,6 +212,7 @@ $app->group('/api', function (RouteCollectorProxy $group) {
|
||||
if (GROCY_FEATURE_FLAG_SHOPPINGLIST)
|
||||
{
|
||||
$group->post('/stock/shoppinglist/add-missing-products', '\Grocy\Controllers\StockApiController:AddMissingProductsToShoppingList');
|
||||
$group->post('/stock/shoppinglist/add-expired-products', '\Grocy\Controllers\StockApiController:AddExpiredProductsToShoppingList');
|
||||
$group->post('/stock/shoppinglist/clear', '\Grocy\Controllers\StockApiController:ClearShoppingList');
|
||||
$group->post('/stock/shoppinglist/add-product', '\Grocy\Controllers\StockApiController:AddProductToShoppingList');
|
||||
$group->post('/stock/shoppinglist/remove-product', '\Grocy\Controllers\StockApiController:RemoveProductFromShoppingList');
|
||||
|
@ -52,6 +52,33 @@ class StockService extends BaseService
|
||||
}
|
||||
}
|
||||
|
||||
public function AddExpiredProductsToShoppingList($listId = 1)
|
||||
{
|
||||
if (!$this->ShoppingListExists($listId))
|
||||
{
|
||||
throw new \Exception('Shopping list does not exist');
|
||||
}
|
||||
|
||||
$expiredProducts = $this->GetExpiringProducts(-1);
|
||||
|
||||
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, $quFactorPurchaseToStock, $locationId = null, $shoppingLocationId = null, &$transactionId = null)
|
||||
{
|
||||
if (!$this->ProductExists($productId))
|
||||
|
@ -96,6 +96,11 @@
|
||||
href="#">
|
||||
{{ $__t('Add products that are below defined min. stock amount') }}
|
||||
</a>
|
||||
<a id="add-expired-products"
|
||||
class="btn btn-outline-primary btn-sm mb-1 responsive-button"
|
||||
href="#">
|
||||
{{ $__t('Add expired products') }}
|
||||
</a>
|
||||
</div>
|
||||
<div class="col-xs-12 col-md-5">
|
||||
<div class="input-group mb-3">
|
||||
|
Loading…
x
Reference in New Issue
Block a user