From 597abe236dd2cc9e13bd4f5698c3465dffa09a7d Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Wed, 4 Jan 2023 22:54:12 +0100 Subject: [PATCH] AddMissingProductsToShoppingList also on adding/editing products (references #2094) --- controllers/GenericEntityApiController.php | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/controllers/GenericEntityApiController.php b/controllers/GenericEntityApiController.php index 82cf2fa9..36974c77 100644 --- a/controllers/GenericEntityApiController.php +++ b/controllers/GenericEntityApiController.php @@ -48,10 +48,16 @@ class GenericEntityApiController extends BaseApiController $newRow = $this->getDatabase()->{$args['entity']}()->createRow($requestBody); $newRow->save(); - $success = $newRow->isClean(); + $newObjectId = $this->getDatabase()->lastInsertId(); + + // TODO: This should be better done somehow in StockService + if ($args['entity'] == 'products' && boolval($this->getUsersService()->GetUserSetting(GROCY_USER_ID, 'shopping_list_auto_add_below_min_stock_amount'))) + { + $this->getStockService()->AddMissingProductsToShoppingList(intval($this->getUsersService()->GetUserSetting(GROCY_USER_ID, 'shopping_list_auto_add_below_min_stock_amount_list_id'))); + } return $this->ApiResponse($response, [ - 'created_object_id' => $this->getDatabase()->lastInsertId() + 'created_object_id' => $newObjectId ]); } catch (\Exception $ex) @@ -102,7 +108,6 @@ class GenericEntityApiController extends BaseApiController } $row->delete(); - $success = $row->isClean(); return $this->EmptyApiResponse($response); } @@ -158,7 +163,12 @@ class GenericEntityApiController extends BaseApiController } $row->update($requestBody); - $success = $row->isClean(); + + // TODO: This should be better done somehow in StockService + if ($args['entity'] == 'products' && boolval($this->getUsersService()->GetUserSetting(GROCY_USER_ID, 'shopping_list_auto_add_below_min_stock_amount'))) + { + $this->getStockService()->AddMissingProductsToShoppingList(intval($this->getUsersService()->GetUserSetting(GROCY_USER_ID, 'shopping_list_auto_add_below_min_stock_amount_list_id'))); + } return $this->EmptyApiResponse($response); }