home.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. this.populateRecipesObject(recipes);
  47. //Populate amount of ingredients sold
  48. for(let recipe of recipes){
  49. for(let recipeIngredient of recipe.ingredients){
  50. for(let newIngredient of soldIngredients){
  51. if(newIngredient.id === recipeIngredient.ingredient){
  52. newIngredient.quantity += (recipeIngredient.quantity * recipe.quantity);
  53. break;
  54. }
  55. }
  56. }
  57. }
  58. //Populate amount of ingredients purchased
  59. for(let purchase of data.purchases){
  60. for(let newPurchaseIngredient of purchase.ingredients){
  61. for(let newIngredient of purchaseIngredients){
  62. if(newIngredient.id === newPurchaseIngredient.ingredient){
  63. newIngredient.amount += newPurchaseIngredient.quantity;
  64. break;
  65. }
  66. }
  67. }
  68. }
  69. //Populate Ingredients table
  70. let ingredientsBody = document.querySelector("#ingredientsData tbody");
  71. for(let ingredient of soldIngredients){
  72. let row = document.createElement("tr");
  73. ingredientsBody.appendChild(row);
  74. let name = document.createElement("td");
  75. name.innerText = `${ingredient.name} (${ingredient.unit})`;
  76. row.appendChild(name);
  77. let used = document.createElement("td");
  78. used.innerText = ingredient.quantity;
  79. row.appendChild(used);
  80. let remaining = document.createElement("td");
  81. remaining.innerText = ingredient.quantityRemaining;
  82. row.appendChild(remaining);
  83. }
  84. //Populate recipes table
  85. let recipesBody = document.querySelector("#recipesData tbody");
  86. for(let recipe of recipes){
  87. let row = document.createElement("tr");
  88. recipesBody.appendChild(row);
  89. let name = document.createElement("td");
  90. name.innerText = recipe.name;
  91. row.appendChild(name);
  92. let quantity = document.createElement("td");
  93. quantity.innerText = recipe.quantity;
  94. row.appendChild(quantity);
  95. let revenue = document.createElement("td");
  96. revenue.innerText = `$${(recipe.quantity * recipe.price).toFixed(2)}`;
  97. row.appendChild(revenue);
  98. }
  99. //Populate purchases table
  100. let purchasesBody = document.querySelector("#purchasesData tbody");
  101. for(let ingredient of purchaseIngredients){
  102. let row = document.createElement("tr");
  103. purchasesBody.appendChild(row);
  104. let name = document.createElement("td");
  105. name.innerText = `${ingredient.name} (${ingredient.unit})`;
  106. row.appendChild(name);
  107. let amount = document.createElement("td");
  108. amount.innerText = ingredient.amount;
  109. row.appendChild(amount);
  110. }
  111. //Populate totals
  112. document.querySelector("#revenueTotal").innerText = `$${this.revenueTotal.toFixed(2)}`;
  113. document.querySelector("#soldTotal").innerText = this.recipeTotal;
  114. },
  115. populateRecipesObject: function(recipes){
  116. if(data.merchant.pos === "clover"){
  117. for(let transaction of data.transactions){
  118. for(let transactionRecipe of transaction.recipes){
  119. for(let recipeCounter of recipes){
  120. if(transactionRecipe === recipeCounter.id){
  121. recipeCounter.quantity++;
  122. this.recipeTotal++;
  123. this.revenueTotal += recipeCounter.price;
  124. break;
  125. }
  126. }
  127. }
  128. }
  129. }else{
  130. for(let transaction of data.transactions){
  131. for(let recipe of transaction.recipes){
  132. for(let newRecipe of recipes){
  133. if(recipe.recipe === newRecipe.id){
  134. newRecipe.quantity += recipe.quantity;
  135. this.recipeTotal += recipe.quantity;
  136. this.revenueTotal += (newRecipe.price * recipe.quantity);
  137. break;
  138. }
  139. }
  140. }
  141. }
  142. }
  143. }
  144. }