Lee Morgan 6 лет назад
Родитель
Сommit
3dbab6dc84
2 измененных файлов с 55 добавлено и 53 удалено
  1. 37 37
      views/dashboardPage/controller.js
  2. 18 16
      views/dashboardPage/home.js

+ 37 - 37
views/dashboardPage/controller.js

@@ -84,24 +84,24 @@ let ingredientsSold = (dateRange)=>{
     let recipes = recipesSold(dateRange);
     let ingredientList = [];
 
-    for(let recipe of recipes){
-        for(let merchRecipe of merchant.recipes){
-            for(let ingredient of merchRecipe.ingredients){
+    for(let i = 0; i < recipes.length; i++){
+        for(let j = 0; j < merchant.recipes.length; j++){
+            for(let k = 0; k < merchant.recipes[j].ingredients.length; k++){
                 let exists = false;
-                for(let item of ingredientList){
-                    if(item.id === ingredient.ingredient._id){
+                for(let l = 0; l < ingredientList.length; l++){
+                    if(ingredientList[l].id === merchant.recipes[j].ingredients[k].ingredient._id){
                         exists = true;
-                        item.quantity += ingredient.quantity * recipe.quantity;
+                        ingredientList[l].quantity += merchant.recipes[j].ingredients[k].quantity * recipes[i].quantity;
                         break;
                     }
                 }
 
                 if(!exists){
                     ingredientList.push({
-                        id: ingredient.ingredient._id,
-                        quantity: ingredient.quantity * recipe.quantity,
-                        name: ingredient.ingredient.name,
-                        unit: ingredient.ingredient.unit
+                        id: merchant.recipes[j].ingredients[k].ingredient._id,
+                        quantity: merchant.recipes[j].ingredients[k].quantity * recipes[i].quantity,
+                        name: merchant.recipes[j].ingredients[k].ingredient.name,
+                        unit: merchant.recipes[j].ingredients[k].ingredient.unit
                     })
                 }
             }
@@ -118,20 +118,20 @@ let ingredientSold = (dateRange,  id)=>{
 
     let checkRecipes = [];
     let quantities = [];
-    for(let merchRecipe of merchant.recipes){
-        for(let merchIngredient of merchRecipe.ingredients){
-            if(merchIngredient.ingredient._id === id){
-                checkRecipes.push(merchRecipe._id);
-                quantities.push(merchIngredient.quantity);
+    for(let i = 0; i < merchant.recipes.length; i++){
+        for(let j = 0; j < merchant.recipes[i].ingredients.length; j++){
+            if(merchant.recipes[i].ingredients[j].ingredient._id === id){
+                checkRecipes.push(merchant.recipes[i]._id);
+                quantities.push(merchant.recipes[i].ingredients[j].quantity);
                 break;
             }
         }
     }
 
-    for(let recipe of recipes){
+    for(let i = 0; i < recipes.length; i++){
         for(let i = 0; i < checkRecipes.length; i++){
-            if(checkRecipes[i] === recipe.id){
-                total += recipe.quantity * quantities[i];
+            if(checkRecipes[i] === recipes[i].id){
+                total += recipes[i].quantity * quantities[i];
                 break;
             }
         }
@@ -151,20 +151,20 @@ let recipesSold = (dateRange)=>{
     let recipeList = [];
 
     for(let i = dateRange[0]; i <= dateRange[1]; i++){
-        for(let recipe of transactions[i].recipes){
+        for(let j = 0; j < transactions[i].recipes.length; j++){
             let exists = false;
-            for(let item of recipeList){
-                if(item.id === recipe.recipe){
+            for(let k = 0; k < recipeList.length; k++){
+                if(recipeList[k].id === transactions[i].recipes[j].recipe){
                     exists = true;
-                    item.quantity += recipe.quantity;
+                    recipeList[k].quantity += transactions[i].recipes[j].quantity;
                     break;
                 }
             }
 
             if(!exists){
                 recipeList.push({
-                    id: recipe.recipe,
-                    quantity: recipe.quantity
+                    id: transactions[i].recipes[j].recipe,
+                    quantity: transactions[i].recipes[j].quantity
                 })
             }
         }
@@ -176,15 +176,15 @@ let recipesSold = (dateRange)=>{
 let categorizeIngredients = ()=>{
     let ingredientsByCategory = [];
 
-    for(let item of merchant.inventory){
+    for(let i = 0; i < merchant.inventory.length; i++){
         let categoryExists = false;
-        for(let category of ingredientsByCategory){
-            if(item.ingredient.category === category.name){
-                category.ingredients.push({
-                    id: item.ingredient._id,
-                    name: item.ingredient.name,
-                    quantity: item.quantity,
-                    unit: item.ingredient.unit
+        for(let j = 0; j < ingredientsByCategory.length; j++){
+            if(merchant.inventory[i].ingredient.category === ingredientsByCategory[j].name){
+                ingredientsByCategory[j].ingredients.push({
+                    id: merchant.inventory[i].ingredient._id,
+                    name: merchant.inventory[i].ingredient.name,
+                    quantity: merchant.inventory[i].quantity,
+                    unit: merchant.inventory[i].ingredient.unit
                 });
 
                 categoryExists = true;
@@ -194,12 +194,12 @@ let categorizeIngredients = ()=>{
 
         if(!categoryExists){
             ingredientsByCategory.push({
-                name: item.ingredient.category,
+                name: merchant.inventory[i].ingredient.category,
                 ingredients: [{
-                    id: item.ingredient._id,
-                    name: item.ingredient.name,
-                    quantity: item.quantity,
-                    unit: item.ingredient.unit
+                    id: merchant.inventory[i].ingredient._id,
+                    name: merchant.inventory[i].ingredient.name,
+                    quantity: merchant.inventory[i].quantity,
+                    unit: merchant.inventory[i].ingredient.unit
                 }]
             });
         }

+ 18 - 16
views/dashboardPage/home.js

@@ -3,6 +3,7 @@ window.homeStrandObj = {
     graph: {},
 
     display: function(){
+        console.time("Load main");
         if(!this.isPopulated){
             let today = new Date();
             let firstOfMonth = new Date(today.getFullYear(), today.getMonth(), 1);
@@ -68,23 +69,23 @@ window.homeStrandObj = {
             while(ul.children.length > 0){
                 ul.removeChild(ul.firstChild);
             }
-            for(let rand of rands){
+            for(let i = 0; i < rands.length; i++){
                 let li = document.createElement("li");
                 li.classList = "flexRow";
-                li.ingredientIndex = rand;
+                li.ingredientIndex = rands[i];
                 ul.appendChild(li);
 
                 let name = document.createElement("p");
-                name.innerText = merchant.inventory[rand].ingredient.name;
+                name.innerText = merchant.inventory[rands[i]].ingredient.name;
                 li.appendChild(name);
 
                 let input = document.createElement("input");
                 input.type = "number";
-                input.value = merchant.inventory[rand].quantity;
+                input.value = merchant.inventory[rands[i]].quantity;
                 li.appendChild(input);
 
                 let label = document.createElement("p");
-                label.innerText = merchant.inventory[rand].ingredient.unit;
+                label.innerText = merchant.inventory[rands[i]].ingredient.unit;
                 li.appendChild(label);
             }
 
@@ -129,16 +130,17 @@ window.homeStrandObj = {
 
             this.isPopulated = true;
         }
+        console.timeEnd("Load main");
     },
 
     calculateRevenue: function(indices){
         let total = 0;
 
         for(let i = indices[0]; i <= indices[1]; i++){
-            for(let recipe of transactions[i].recipes){
-                for(let merchRecipe of merchant.recipes){
-                    if(recipe.recipe === merchRecipe._id){
-                        total += recipe.quantity * merchRecipe.price;
+            for(let j = 0; j < transactions[i].recipes.length; j++){
+                for(let k = 0; k < merchant.recipes.length; k++){
+                    if(transactions[i].recipes[j].recipe === merchant.recipes[k]._id){
+                        total += transactions[i].recipes[j].quantity * merchant.recipes[k].price;
                     }
                 }
             }
@@ -161,10 +163,10 @@ window.homeStrandObj = {
                 currentDate = transactions[i].date;
                 arrayIndex++;
             }
-            for(let recipe of transactions[i].recipes){
+            for(let j = 0; j < transactions[i].recipes.length; j++){
                 for(let merchRecipe of merchant.recipes){
-                    if(recipe.recipe === merchRecipe._id){
-                        dataList[arrayIndex] = parseFloat((dataList[arrayIndex] + (recipe.quantity * merchRecipe.price) / 100).toFixed(2));
+                    if(transactions[i].recipes[j].recipe === merchRecipe._id){
+                        dataList[arrayIndex] = parseFloat((dataList[arrayIndex] + (transactions[i].recipes[j].quantity * merchRecipe.price) / 100).toFixed(2));
                         break;
                     }
                 }
@@ -179,11 +181,11 @@ window.homeStrandObj = {
 
         let changes = [];
 
-        for(let li of lis){
-            if(li.children[1].value >= 0){
-                let merchIngredient = merchant.inventory[li.ingredientIndex];
+        for(let i = 0; i < lis.length; i++){
+            if(lis[i].children[1].value >= 0){
+                let merchIngredient = merchant.inventory[lis[i].ingredientIndex];
 
-                let value = parseInt(li.children[1].value);
+                let value = parseInt(lis[i].children[1].value);
 
                 if(value !== merchIngredient.quantity){
                     changes.push({