home.js 5.3 KB

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