Selaa lähdekoodia

Change displaying of quantities to the proper amounts where needed.

Lee Morgan 5 vuotta sitten
vanhempi
sitoutus
dac2b0b2e5

+ 17 - 0
views/dashboardPage/js/classes/Recipe.js

@@ -24,6 +24,10 @@ class RecipeIngredient{
     getQuantityDisplay(){
         return `${this._quantity.toFixed(2)} ${this._unit.toUpperCase()}`;
     }
+
+    getQuantityAsBase(){
+        return this._quantity * this._baseUnitMultiplier;
+    }
 }
 
 /*
@@ -34,6 +38,8 @@ price = price of recipe in cents
 ingredients = [{
     ingredient: Ingredient Object,
     quantity: quantity of the ingredient within the recipe (stored as base unit, i.e grams)
+    unit: String
+    baseUnitmultiplier: Number
 }]
 parent = merchant that it belongs to
 */
@@ -47,6 +53,7 @@ class Recipe{
         this._hidden = hidden;
         this._ingredients = [];
         this._ingredientTotals = {};
+        this._ingredientTotalsBase = {};
 
         for(let i = 0; i < ingredients.length; i++){
             const ingredient = parent.getIngredient(ingredients[i].ingredient);
@@ -117,6 +124,10 @@ class Recipe{
         return (this._ingredientTotals[id] === undefined) ? 0 : this._ingredientTotals[id];
     }
 
+    getIngredientTotalBase(id){
+        return (this._ingredientTotalsBase[i] === undefined) ? 0 : this._ingredientTotals[id];
+    }
+
     addIngredient(ingredient, quantity, unit, baseUnitMultiplier){
         let recipeIngredient = new RecipeIngredient(ingredient, quantity, unit, baseUnitMultiplier);
         this._ingredients.push(recipeIngredient);
@@ -142,6 +153,12 @@ class Recipe{
             }else{
                 this._ingredientTotals[ingredient.id] += multiplier;
             }
+
+            if(this._ingredientTotalsBase[ingredient.id] === undefined){
+                this._ingredientTotalsBase[ingredient.id] = multiplier * this._baseUnitMultiplier;
+            }else{
+                this._ingredientTotalsBase[ingredient.id] += multiplier * this._baseUnitMultiplier;
+            }
         }
 
         for(let i = 0; i < this._ingredients.length; i++){

+ 9 - 0
views/dashboardPage/js/classes/Transaction.js

@@ -52,6 +52,15 @@ class Transaction{
         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;
+        }
+
+        return total;
+    }
+
     /*
     Gets the quantity for a given recipe
     */

+ 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.getIngredientQuantity(this.ingredient);
+                sum += transaction.getIngredientQuantityBase(this.ingredient);
             }
             
             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.getIngredientQuantity(this.category.ingredients[k].ingredient);
+                    total += transaction.getIngredientQuantityBase(this.category.ingredients[k].ingredient);
                 }
             }
             quantities.push(total);