From a4454f825a74b4e51dfad94b0da5b74b8fa23246 Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Sat, 31 Aug 2019 14:08:15 +0200 Subject: [PATCH] Fixes/changes for pull request #349 --- changelog/52_UNRELEASED_2019-xx-xx.md | 1 + services/StockService.php | 39 +++++++++++++++------------ 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/changelog/52_UNRELEASED_2019-xx-xx.md b/changelog/52_UNRELEASED_2019-xx-xx.md index 4f074d10..185bd44e 100644 --- a/changelog/52_UNRELEASED_2019-xx-xx.md +++ b/changelog/52_UNRELEASED_2019-xx-xx.md @@ -10,6 +10,7 @@ - Fixed that the Userfield type "Preset list" had always the caption "Product group" instead of the configured one (thanks @oncleben31) - Userfields of type "checkbox" are rendered as a checkmark in tables when checked (instead of "1" as till now) - API improvements + - New endpoint `/stock/shoppinglist/add-product` to add a product to a shopping list (thanks @Forceu) - New endpoint `/stock/shoppinglist/remove-product` to remove a product from a shopping list (thanks @Forceu) - When adding a product (through `stock/product/{productId}/add` or `stock/product/{productId}/inventory`) with omitted best before date and if the given product has "Default best before days" set, the best before date is calculated based on that (so far always today was used which is still the case when no date is supplied and also the product has no "Default best before days set) (thanks @Forceu) - Field `stock_amount` of endpoint `/stock/products/{productId}ยด now returns `0` instead of `null` when the given product is not in stock (thanks @Forceu) diff --git a/services/StockService.php b/services/StockService.php index 68dee04f..9bd792fb 100644 --- a/services/StockService.php +++ b/services/StockService.php @@ -556,23 +556,28 @@ class StockService extends BaseService throw new \Exception('Shopping list does not exist'); } - $alreadyExistingEntry = $this->Database->shopping_list()->where('product_id', $productId->id)->fetch(); - if ($alreadyExistingEntry) // Update - { - $alreadyExistingEntry->update(array( - 'amount' => ($alreadyExistingEntry->amount + $amount), - 'shopping_list_id' => $listId - )); - } - else // Insert - { - $shoppinglistRow = $this->Database->shopping_list()->createRow(array( - 'product_id' => $productId->id, - 'amount' => $amount, - 'shopping_list_id' => $listId - )); - $shoppinglistRow->save(); - } + if (!$this->ProductExists($productId)) + { + throw new \Exception('Product does not exist'); + } + + $alreadyExistingEntry = $this->Database->shopping_list()->where('product_id = :1 AND shopping_list_id = :2', $productId, $listId)->fetch(); + if ($alreadyExistingEntry) // Update + { + $alreadyExistingEntry->update(array( + 'amount' => ($alreadyExistingEntry->amount + $amount), + 'shopping_list_id' => $listId + )); + } + else // Insert + { + $shoppinglistRow = $this->Database->shopping_list()->createRow(array( + 'product_id' => $productId->id, + 'amount' => $amount, + 'shopping_list_id' => $listId + )); + $shoppinglistRow->save(); + } } private function ProductExists($productId)