Skip to content

Commit

Permalink
when combining ingredients take into account the # of servings
Browse files Browse the repository at this point in the history
  • Loading branch information
Todd Rogers committed Apr 6, 2017
1 parent 023525c commit ec0347b
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions Model/ShoppingList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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);
}
}

Expand All @@ -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'];
Expand All @@ -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;
Expand Down

0 comments on commit ec0347b

Please sign in to comment.