Browse Source

Ingredient details works with classes

Lee Morgan 6 years ago
parent
commit
b2d05c9c65

+ 20 - 18
views/dashboardPage/Merchant.js

@@ -285,6 +285,23 @@ class Merchant{
         return ingredientList;
     }
 
+    singleIngredientSold(dateRange, ingredient){
+        let total = 0;
+
+        for(let i = dateRange[0]; i < dateRange[1]; i++){
+            for(let j = 0; j < this.transactions[i].recipes.length; j++){
+                for(let k = 0; k < this.transactions[i].recipes[j].recipe.ingredients.length; k++){
+                    if(this.transactions[i].recipes[j].recipe.ingredients[k].ingredient === ingredient.ingredient){
+                        total += this.transactions[i].recipes[j].recipe.ingredients[k].quantity;
+                        break;
+                    }
+                }
+            }
+        }
+
+        return total;
+    }
+
     /*
     Gets the number of recipes sold between two dates (dateRange)
     Inputs:
@@ -354,12 +371,7 @@ class Merchant{
     Groups all of the merchant's ingredients by their category
     Return: [{
         name: category name,
-        ingredients: [{
-            id: id of ingredient,
-            name: name of ingredient,
-            quantity: merchant's quantity of this ingredient,
-            unit: measurement unit
-        }]
+        ingredients: [Ingredient Object]
     }]
     */
     categorizeIngredients(){
@@ -369,12 +381,7 @@ class Merchant{
             let categoryExists = false;
             for(let j = 0; j < ingredientsByCategory.length; j++){
                 if(this.ingredients[i].ingredient.category === ingredientsByCategory[j].name){
-                    ingredientsByCategory[j].ingredients.push({
-                        id: this.ingredients[i].ingredient._id,
-                        name: this.ingredients[i].ingredient.name,
-                        quantity: this.ingredients[i].quantity,
-                        unit: this.ingredients[i].ingredient.unit
-                    });
+                    ingredientsByCategory[j].ingredients.push(this.ingredients[i]);
 
                     categoryExists = true;
                     break;
@@ -384,12 +391,7 @@ class Merchant{
             if(!categoryExists){
                 ingredientsByCategory.push({
                     name: this.ingredients[i].ingredient.category,
-                    ingredients: [{
-                        id: this.ingredients[i].ingredient._id,
-                        name: this.ingredients[i].ingredient.name,
-                        quantity: this.ingredients[i].quantity,
-                        unit: this.ingredients[i].ingredient.unit
-                    }]
+                    ingredients: [this.ingredients[i]]
                 });
             }
         }

+ 13 - 5
views/dashboardPage/components/components.js

@@ -548,24 +548,32 @@ let ingredientDetailsComp = {
         openSidebar(sidebar);
 
         document.querySelector("#ingredientDetails p").innerText = category.name;
-        document.querySelector("#ingredientDetails h1").innerText = ingredient.name;
-        document.querySelector("#ingredientStock").innerText = `${ingredient.quantity} ${ingredient.unit}`;
-        document.querySelector("#ingredientInput").placeholder = `${ingredient.quantity} ${ingredient.unit}`;
+        document.querySelector("#ingredientDetails h1").innerText = ingredient.ingredient.name;
+        document.querySelector("#ingredientStock").innerText = `${ingredient.quantity} ${ingredient.ingredient.unit}`;
+        document.querySelector("#ingredientInput").placeholder = `${ingredient.quantity} ${ingredient.ingredient.unit}`;
 
         let quantities = [];
         let now = new Date();
+        console.time("Single Ingredient Sold");
         for(let i = 1; i < 31; i++){
             let endDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i)
             let startDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i - 1);
-            quantities.push(merchant.ingredientsSold(merchant.transactionIndices(startDay, endDay), ingredient.id));
+            let indices = merchant.transactionIndices(startDay, endDay);
+
+            if(indices === false){
+                quantities.push(0);
+            }else{
+                quantities.push(merchant.singleIngredientSold(indices, ingredient));
+            }
         }
+        console.timeEnd("Single Ingredient Sold");
 
         let sum = 0;
         for(let quantity of quantities){
             sum += quantity;
         }
 
-        document.querySelector("#dailyUse").innerText = `${(sum/quantities.length).toFixed(2)} ${ingredient.unit}`;
+        document.querySelector("#dailyUse").innerText = `${(sum/quantities.length).toFixed(2)} ${ingredient.ingredient.unit}`;
 
         let ul = document.querySelector("#ingredientRecipeList");
         let recipes = recipesForIngredient(ingredient.id);

+ 4 - 4
views/dashboardPage/ingredients.js

@@ -38,11 +38,11 @@ window.ingredientsStrandObj = {
                 let ingredient = categories[i].ingredients[j];
                 let ingredientDiv = ingredientTemplate.cloneNode(true);
 
-                ingredientDiv.children[0].innerText = ingredient.name;
-                ingredientDiv.children[2].innerText = `${ingredient.quantity} ${ingredient.unit}`;
+                ingredientDiv.children[0].innerText = ingredient.ingredient.name;
+                ingredientDiv.children[2].innerText = `${ingredient.quantity} ${ingredient.ingredient.unit}`;
                 ingredientDiv.onclick = ()=>{ingredientDetailsComp.display(ingredient, categories[i])};
-                ingredientDiv._name = ingredient.name.toLowerCase();
-                ingredientDiv._unit = ingredient.unit.toLowerCase();
+                ingredientDiv._name = ingredient.ingredient.name.toLowerCase();
+                ingredientDiv._unit = ingredient.ingredient.unit.toLowerCase();
 
                 categoryDiv.children[1].appendChild(ingredientDiv);
                 this.ingredients.push(ingredientDiv);