Optionally remove only done shopping list items (/stock/shoppinglist/clear API endpoint, closes #1730)

This commit is contained in:
Bernd Bestel 2022-01-06 14:07:29 +01:00
parent 0b36d02aa1
commit 3a6f04f770
No known key found for this signature in database
GPG Key ID: 71BD34C0D4891300
4 changed files with 23 additions and 5 deletions

View File

@ -7,3 +7,4 @@
- Fixed that the barcode lookup for the "Stock by-barcode" API endpoints was case sensitive - Fixed that the barcode lookup for the "Stock by-barcode" API endpoints was case sensitive
- Fixed that the logout button/menu was missing when using external authentication (e.g. LDAP) - Fixed that the logout button/menu was missing when using external authentication (e.g. LDAP)
- Fixed that the consume page/dialog wasn't properly initialized when opening it from the stock entries page - Fixed that the consume page/dialog wasn't properly initialized when opening it from the stock entries page
- The API endpoint `/stock/shoppinglist/clear` has now a new optional request body parameter `done_only` (to only remove done items from the given shopping list, defaults to `false`)

View File

@ -233,13 +233,18 @@ class StockApiController extends BaseApiController
$requestBody = $this->GetParsedAndFilteredRequestBody($request); $requestBody = $this->GetParsedAndFilteredRequestBody($request);
$listId = 1; $listId = 1;
if (array_key_exists('list_id', $requestBody) && !empty($requestBody['list_id']) && is_numeric($requestBody['list_id'])) if (array_key_exists('list_id', $requestBody) && !empty($requestBody['list_id']) && is_numeric($requestBody['list_id']))
{ {
$listId = intval($requestBody['list_id']); $listId = intval($requestBody['list_id']);
} }
$this->getStockService()->ClearShoppingList($listId); $doneOnly = false;
if (array_key_exists('done_only', $requestBody) && filter_var($requestBody['done_only'], FILTER_VALIDATE_BOOLEAN) !== false)
{
$doneOnly = boolval($requestBody['done_only']);
}
$this->getStockService()->ClearShoppingList($listId, $doneOnly);
return $this->EmptyApiResponse($response); return $this->EmptyApiResponse($response);
} }
catch (\Exception $ex) catch (\Exception $ex)

View File

@ -2961,10 +2961,15 @@
"list_id": { "list_id": {
"type": "integer", "type": "integer",
"description": "The shopping list id to clear, when omitted, the default shopping list (with id 1) is used" "description": "The shopping list id to clear, when omitted, the default shopping list (with id 1) is used"
},
"done_only": {
"type": "boolean",
"description": "When `true`, only done items will be removed (defaults to `false` when ommited)"
} }
}, },
"example": { "example": {
"list_id": 2 "list_id": 2,
"done_only": false
} }
} }
} }

View File

@ -336,15 +336,22 @@ class StockService extends BaseService
} }
} }
public function ClearShoppingList($listId = 1) public function ClearShoppingList($listId = 1, $doneOnly = false)
{ {
if (!$this->ShoppingListExists($listId)) if (!$this->ShoppingListExists($listId))
{ {
throw new \Exception('Shopping list does not exist'); throw new \Exception('Shopping list does not exist');
} }
if ($doneOnly)
{
$this->getDatabase()->shopping_list()->where('shopping_list_id = :1 AND IFNULL(done, 0) = 1', $listId)->delete();
}
else
{
$this->getDatabase()->shopping_list()->where('shopping_list_id = :1', $listId)->delete(); $this->getDatabase()->shopping_list()->where('shopping_list_id = :1', $listId)->delete();
} }
}
public function ConsumeProduct(int $productId, float $amount, bool $spoiled, $transactionType, $specificStockEntryId = 'default', $recipeId = null, $locationId = null, &$transactionId = null, $allowSubproductSubstitution = false, $consumeExactAmount = false) public function ConsumeProduct(int $productId, float $amount, bool $spoiled, $transactionType, $specificStockEntryId = 'default', $recipeId = null, $locationId = null, &$transactionId = null, $allowSubproductSubstitution = false, $consumeExactAmount = false)
{ {