home.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. window.homeObj = {
  2. display: function(){
  3. clearScreen();
  4. document.querySelector("#homeStrand").style.display = "flex";
  5. //Fill in month
  6. let months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  7. document.querySelector("#month").innerText = `Month of ${months[new Date().getMonth()]}`;
  8. this.populate();
  9. },
  10. populate: function(){
  11. //Create object to store number of recipes sold
  12. let recipes = [];
  13. for(let recipe of data.merchant.recipes){
  14. recipes.push({
  15. id: recipe._id,
  16. name: recipe.name,
  17. quantity: 0,
  18. ingredients: recipe.ingredients,
  19. price: recipe.price / 100
  20. });
  21. }
  22. //Create object to store amount of ingredients sold
  23. let soldIngredients = [];
  24. for(let item of data.merchant.inventory){
  25. soldIngredients.push({
  26. id: item.ingredient._id,
  27. name: item.ingredient.name,
  28. quantity: 0,
  29. quantityRemaining: item.quantity,
  30. unit: item.ingredient.unit
  31. });
  32. }
  33. //Create object for each merchant ingredient
  34. let purchaseIngredients = [];
  35. for(let item of data.merchant.inventory){
  36. purchaseIngredients.push({
  37. id: item.ingredient._id,
  38. name: item.ingredient.name,
  39. amount: 0,
  40. unit: item.ingredient.unit
  41. });
  42. }
  43. //Populate number of recipes sold
  44. let revenueTotal = 0;
  45. let recipeTotal = 0;
  46. for(let transaction of data.transactions){
  47. for(let transactionRecipe of transaction.recipes){
  48. for(let recipeCounter of recipes){
  49. console.log("recipecounter");
  50. if(transactionRecipe === recipeCounter.id){
  51. console.log(transactionRecipe);
  52. recipeCounter.quantity++;
  53. recipeTotal++;
  54. revenueTotal += (recipeCounter.price * recipeCounter.quantity);
  55. break;
  56. }
  57. }
  58. }
  59. }
  60. //Populate amount of ingredients sold
  61. for(let recipe of recipes){
  62. for(let recipeIngredient of recipe.ingredients){
  63. for(let newIngredient of soldIngredients){
  64. if(newIngredient.id === recipeIngredient.ingredient){
  65. newIngredient.quantity += (recipeIngredient.quantity * recipe.quantity);
  66. break;
  67. }
  68. }
  69. }
  70. }
  71. //Populate amount of ingredients purchased
  72. for(let purchase of data.purchases){
  73. for(let newPurchaseIngredient of purchase.ingredients){
  74. for(let newIngredient of purchaseIngredients){
  75. if(newIngredient.id === newPurchaseIngredient.ingredient){
  76. newIngredient.amount += newPurchaseIngredient.quantity;
  77. break;
  78. }
  79. }
  80. }
  81. }
  82. //Populate Ingredients table
  83. let ingredientsBody = document.querySelector("#ingredientsData tbody");
  84. for(let ingredient of soldIngredients){
  85. let row = document.createElement("tr");
  86. ingredientsBody.appendChild(row);
  87. let name = document.createElement("td");
  88. name.innerText = `${ingredient.name} (${ingredient.unit})`;
  89. row.appendChild(name);
  90. let used = document.createElement("td");
  91. used.innerText = ingredient.quantity;
  92. row.appendChild(used);
  93. let remaining = document.createElement("td");
  94. remaining.innerText = ingredient.quantityRemaining;
  95. row.appendChild(remaining);
  96. }
  97. //Populate recipes table
  98. let recipesBody = document.querySelector("#recipesData tbody");
  99. for(let recipe of recipes){
  100. let row = document.createElement("tr");
  101. recipesBody.appendChild(row);
  102. let name = document.createElement("td");
  103. name.innerText = recipe.name;
  104. row.appendChild(name);
  105. let quantity = document.createElement("td");
  106. quantity.innerText = recipe.quantity;
  107. row.appendChild(quantity);
  108. let revenue = document.createElement("td");
  109. revenue.innerText = `$${recipe.quantity * recipe.price}`;
  110. row.appendChild(revenue);
  111. }
  112. //Populate purchases table
  113. let purchasesBody = document.querySelector("#purchasesData tbody");
  114. for(let ingredient of purchaseIngredients){
  115. let row = document.createElement("tr");
  116. purchasesBody.appendChild(row);
  117. let name = document.createElement("td");
  118. name.innerText = `${ingredient.name} (${ingredient.unit})`;
  119. row.appendChild(name);
  120. let amount = document.createElement("td");
  121. amount.innerText = ingredient.amount;
  122. row.appendChild(amount);
  123. }
  124. //Populate totals
  125. document.querySelector("#revenueTotal").innerText = `$${revenueTotal.toFixed(2)}`;
  126. document.querySelector("#soldTotal").innerText = recipeTotal;
  127. }
  128. }