From ec0347bda85f24cce8f3022719180a60d42f0aac Mon Sep 17 00:00:00 2001 From: Todd Rogers Date: Wed, 5 Apr 2017 20:53:03 -0400 Subject: [PATCH] when combining ingredients take into account the # of servings --- Model/ShoppingList.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Model/ShoppingList.php b/Model/ShoppingList.php index 12b8ba8..dc23126 100644 --- a/Model/ShoppingList.php +++ b/Model/ShoppingList.php @@ -111,7 +111,7 @@ public function getAllIngredients($listId, $userId) { 'ShoppingListRecipe' => array( 'fields' => array('servings'), 'Recipe' => array( - 'fields' => array('name'), + 'fields' => array('name', 'serving_size'), 'IngredientMapping' => array( 'fields' => array('quantity'), 'Unit' => array( @@ -138,12 +138,14 @@ public function combineIngredients($list) { $ingredients = array(); foreach ($list['ShoppingListIngredient'] as $item) { - $ingredients = $this->combineIngredient($ingredients, $item); + //TODO: pass on servings + $ingredients = $this->combineIngredient($ingredients, $item, 1); } foreach ($list['ShoppingListRecipe'] as $recipeInList) { $recipeDetail = $recipeInList['Recipe']; + $scaling = $recipeInList['servings'] / $recipeDetail['serving_size']; foreach ($recipeDetail['IngredientMapping'] as $mapping) { - $ingredients = $this->combineIngredient($ingredients, $mapping); + $ingredients = $this->combineIngredient($ingredients, $mapping, $scaling); } } @@ -168,7 +170,7 @@ public function clearList($userId) { $this->ShoppingListRecipe->deleteAll(array('ShoppingListRecipe.user_id' => $userId), false); } - private function combineIngredient($list, $ingredient) { + private function combineIngredient($list, $ingredient, $scaling) { $id = $ingredient['ingredient_id']; $unitId = $ingredient['unit_id']; $quantity = $ingredient['quantity']; @@ -178,14 +180,14 @@ private function combineIngredient($list, $ingredient) { if (isset($list[$id])) { foreach ($list[$id] as $item) { if ($item->unitId == $unitId) { - $item->quantity += $quantity; + $item->quantity += $quantity * $scaling; } } } else { $this->ListItem->id = $id; $this->ListItem->name = $name; $this->ListItem->unitId = $unitId; - $this->ListItem->quantity = $quantity; + $this->ListItem->quantity = $quantity * $scaling; $this->ListItem->unitName = $unitName; $this->ListItem->locationId = $locationId; $this->ListItem->removed = false;