Răsfoiți Sursa

Update the individual ingredient analytics to display accurately.

Lee Morgan 5 ani în urmă
părinte
comite
c229214838

+ 1 - 1
views/dashboardPage/js/classes/Ingredient.js

@@ -155,7 +155,7 @@ class Ingredient{
     calculateRecipeMultiplier(recipeQuantity, ingredientQuantity){
         let unitToBase = controller.baseUnit(ingredientQuantity, this._unit);
 
-        return  recipeQuantity / unitToBase;
+        return unitToBase / recipeQuantity;
     }
 }
 

+ 1 - 1
views/dashboardPage/js/classes/Merchant.js

@@ -62,7 +62,7 @@ class MerchantIngredient{
         const {start, end} = this._parent.getTransactionIndices(from, to);
 
         for(let i = start; i < end; i++){
-            total += this._parent.transactions[i].getIngredientQuantityBase(this._ingredient);
+            total += this._parent.transactions[i].getIngredientQuantity(this._ingredient);
         }
 
         return total;

+ 19 - 14
views/dashboardPage/js/classes/Recipe.js

@@ -6,6 +6,7 @@ class RecipeIngredient{
         this._ingredient = ingredient;
         this._quantity = quantity;
         this._unit = unit;
+        //Quantity * baseUnitMultiplier will give you the quantity converted to the base unit of the ingredient (gram/meter/liter)
         this._baseUnitMultiplier = baseUnitMultiplier;
     }
 
@@ -21,6 +22,10 @@ class RecipeIngredient{
         this_quantity = controller.baseUnit(quantity, this._ingredient.unit);
     }
 
+    get baseUnitMultiplier(){
+        return this._baseUnitMultiplier;
+    }
+
     getQuantityDisplay(){
         return `${this._quantity.toFixed(2)} ${this._unit.toUpperCase()}`;
     }
@@ -52,8 +57,8 @@ class Recipe{
         this._parent = parent;
         this._hidden = hidden;
         this._ingredients = [];
+        //Ingredient totals is the total amount of each ingredient within the recipe, converted to ingredient base unit
         this._ingredientTotals = {};
-        this._ingredientTotalsBase = {};
 
         for(let i = 0; i < ingredients.length; i++){
             const ingredient = parent.getIngredient(ingredients[i].ingredient);
@@ -120,14 +125,20 @@ class Recipe{
         return this._ingredientTotals;
     }
 
-    getIngredientTotal(id){
+    //Returns the quantity of a single ingredient with the recipe.
+    //Returns the quantity converted to the base unit of the ingredient
+    getIngredientTotal(id, isDisplay = false){
+        if(isDisplay === true){
+            for(let i = 0; i < this._ingredients.length; i++){
+                if(this._ingredients[i].ingredient.id === id){
+                    return (this._ingredientTotals[id] === undefined) ? 0 : controller.displayUnit(this._ingredientTotals[id], this._ingredients[i].ingredient.unit);
+                }
+                break;
+            }
+        }
         return (this._ingredientTotals[id] === undefined) ? 0 : this._ingredientTotals[id];
     }
 
-    getIngredientTotalBase(id){
-        return (this._ingredientTotalsBase[id] === undefined) ? 0 : this._ingredientTotalsBase[id];
-    }
-
     addIngredient(ingredient, quantity, unit, baseUnitMultiplier){
         let recipeIngredient = new RecipeIngredient(ingredient, quantity, unit, baseUnitMultiplier);
         this._ingredients.push(recipeIngredient);
@@ -150,15 +161,9 @@ class Recipe{
             }
 
             if(this._ingredientTotals[ingredient.id] === undefined){
-                this._ingredientTotals[ingredient.id] = multiplier;
-            }else{
-                this._ingredientTotals[ingredient.id] += multiplier;
-            }
-
-            if(this._ingredientTotalsBase[ingredient.id] === undefined){
-                this._ingredientTotalsBase[ingredient.id] = multiplier * recipeIngredient._baseUnitMultiplier;
+                this._ingredientTotals[ingredient.id] = multiplier * recipeIngredient.baseUnitMultiplier;
             }else{
-                this._ingredientTotalsBase[ingredient.id] += multiplier * recipeIngredient._baseUnitMultiplier;
+                this._ingredientTotals[ingredient.id] += multiplier * recipeIngredient.baseUnitMultiplier;
             }
         }
 

+ 2 - 10
views/dashboardPage/js/classes/Transaction.js

@@ -43,19 +43,11 @@ class Transaction{
     /*
     Gets the quantity for a given ingredient
     */
-    getIngredientQuantity(ingredient){
+    getIngredientQuantity(ingredient, isDisplay = false){
         let total = 0;
-        for(let i = 0; i < this._recipes.length; i++){
-            total += this._recipes[i].recipe.getIngredientTotal(ingredient.id) * this._recipes[i].quantity;
-        }
-
-        return total;
-    }
 
-    getIngredientQuantityBase(ingredient){
-        let total = 0;
         for(let i = 0; i < this._recipes.length; i++){
-            total += this._recipes[i].recipe.getIngredientTotalBase(ingredient.id) * this._recipes[i].quantity;
+            total += this._recipes[i].recipe.getIngredientTotal(ingredient.id, isDisplay) * this._recipes[i].quantity;
         }
 
         return total;

+ 24 - 0
views/dashboardPage/js/dashboard.js

@@ -399,6 +399,30 @@ controller = {
             case "ft": return quantity / 3.2808;
             default: return quantity;
         }
+    },
+
+    displayUnit(quantity, unit){
+        switch(unit){
+            case "g": return quantity;
+            case "kg": return quantity / 1000;
+            case "oz":  return quantity / 28.3495; 
+            case "lb":  return quantity / 453.5924;
+            case "ml": return quantity * 1000; 
+            case "l": return quantity;
+            case "tsp": return quantity * 202.8842; 
+            case "tbsp": return quantity * 67.6278; 
+            case "ozfl": return quantity * 33.8141; 
+            case "cup": return quantity * 4.1667; 
+            case "pt": return quantity * 2.1134; 
+            case "qt": return quantity * 1.0567; 
+            case "gal": return quantity / 3.7854;
+            case "mm": return quantity * 1000; 
+            case "cm": return quantity * 100; 
+            case "m": return quantity;
+            case "in": return quantity * 39.3701; 
+            case "ft": return quantity * 3.2808;
+            default: return quantity;
+        }
     }
 }
 

+ 2 - 2
views/dashboardPage/js/strands/analytics.js

@@ -184,7 +184,7 @@ let analytics = {
             let sum = 0;
             for(let j = 0; j < this.transactionsByDate[i].transactions.length; j++){
                 let transaction = this.transactionsByDate[i].transactions[j];
-                sum += transaction.getIngredientQuantityBase(this.ingredient);
+                sum += transaction.getIngredientQuantity(this.ingredient, true);
             }
             
             quantities.push(sum);
@@ -264,7 +264,7 @@ let analytics = {
                 let transaction = this.transactionsByDate[i].transactions[j];
 
                 for(let k = 0; k < this.category.ingredients.length; k++){
-                    total += transaction.getIngredientQuantityBase(this.category.ingredients[k].ingredient);
+                    total += transaction.getIngredientQuantity(this.category.ingredients[k].ingredient, true);
                 }
             }
             quantities.push(total);