فهرست منبع

Update the Recipe.getIngredientTotal function to work cross-unit types.
Next need to ensure that new transactions calculate correctly with cross-unit recipes.

Lee Morgan 4 سال پیش
والد
کامیت
aae28513e7
1فایلهای تغییر یافته به همراه13 افزوده شده و 1 حذف شده
  1. 13 1
      views/dashboardPage/js/classes/Recipe.js

+ 13 - 1
views/dashboardPage/js/classes/Recipe.js

@@ -75,7 +75,18 @@ class Recipe{
     getIngredientTotal(id){
         for(let i = 0; i < this.ingredients.length; i++){
             if(this.ingredients[i].ingredient.id === id){
-                return (this.ingredientTotals[id] === undefined) ? 0 : this.ingredientTotals[id] * controller.unitMultiplier(controller.toBase(this.ingredients[i].ingredient.unit), this.ingredients[i].ingredient.unit);
+                if(this.ingredientTotals[id] === undefined) break;
+                let ingredientTotal = this.ingredientTotals[id];
+                let toIngredientBase = 0;
+
+                switch(controller.getBaseUnit(this.ingredients[i].unit)){
+                    case "g": toIngredientBase = this.ingredients[i].ingredient.convert.toMass; break;
+                    case "l": toIngredientBase = this.ingredients[i].ingredient.convert.toVolume; break;
+                    case "m": toIngredientBase = this.ingredients[i].ingredient.convert.toLength; break;
+                }
+
+                return ingredientTotal / toIngredientBase;
+                // return (this.ingredientTotals[id] === undefined) ? 0 : this.ingredientTotals[id] * controller.unitMultiplier(controller.toBase(this.ingredients[i].ingredient.unit), this.ingredients[i].ingredient.unit);
             }
         }
 
@@ -90,6 +101,7 @@ class Recipe{
         analytics.isPopulated = false;
     }
 
+    //Ingredient totals are stored as base of the recipeIngredient unit
     calculateIngredientTotals(){
         this.ingredientTotals = {};