home.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. window.homeObj = {
  2. recipeTotal: 0,
  3. revenueTotal: 0,
  4. dateFrom: "",
  5. dateTo: "",
  6. display: function(){
  7. clearScreen();
  8. document.querySelector("#homeStrand").style.display = "flex";
  9. //Fill in month
  10. let months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  11. document.querySelector("#month").innerText = `Month of ${months[new Date().getMonth()]}`;
  12. document.querySelector("#to").valueAsDate = new Date();
  13. this.populate(data.transactions);
  14. },
  15. populate: function(transactions){
  16. this.recipeTotal = 0;
  17. this.revenueTotal = 0;
  18. //Create object to store number of recipes sold
  19. let recipes = [];
  20. for(let recipe of data.merchant.recipes){
  21. recipes.push({
  22. id: recipe._id,
  23. name: recipe.name,
  24. quantity: 0,
  25. ingredients: recipe.ingredients,
  26. price: recipe.price / 100
  27. });
  28. }
  29. //Create object to store amount of ingredients sold
  30. let soldIngredients = [];
  31. for(let item of data.merchant.inventory){
  32. soldIngredients.push({
  33. id: item.ingredient._id,
  34. name: item.ingredient.name,
  35. quantity: 0,
  36. quantityRemaining: item.quantity,
  37. unit: item.ingredient.unit
  38. });
  39. }
  40. //Create object for each merchant ingredient
  41. let purchaseIngredients = [];
  42. for(let item of data.merchant.inventory){
  43. purchaseIngredients.push({
  44. id: item.ingredient._id,
  45. name: item.ingredient.name,
  46. amount: 0,
  47. unit: item.ingredient.unit
  48. });
  49. }
  50. //Populate number of recipes sold
  51. for(let transaction of transactions){
  52. for(let recipe of transaction.recipes){
  53. for(let newRecipe of recipes){
  54. if(recipe.recipe === newRecipe.id){
  55. newRecipe.quantity += recipe.quantity;
  56. this.recipeTotal += recipe.quantity;
  57. this.revenueTotal += (newRecipe.price * recipe.quantity);
  58. break;
  59. }
  60. }
  61. }
  62. }
  63. //Populate amount of ingredients sold
  64. for(let recipe of recipes){
  65. for(let recipeIngredient of recipe.ingredients){
  66. for(let newIngredient of soldIngredients){
  67. if(newIngredient.id === recipeIngredient.ingredient){
  68. newIngredient.quantity += (recipeIngredient.quantity * recipe.quantity);
  69. break;
  70. }
  71. }
  72. }
  73. }
  74. //Populate amount of ingredients purchased
  75. for(let purchase of data.purchases){
  76. for(let newPurchaseIngredient of purchase.ingredients){
  77. for(let newIngredient of purchaseIngredients){
  78. if(newIngredient.id === newPurchaseIngredient.ingredient){
  79. newIngredient.amount += newPurchaseIngredient.quantity;
  80. break;
  81. }
  82. }
  83. }
  84. }
  85. //Populate Ingredients table
  86. let ingredientsBody = document.querySelector("#ingredientsData tbody");
  87. while(ingredientsBody.children.length > 0){
  88. ingredientsBody.removeChild(ingredientsBody.firstChild);
  89. }
  90. for(let ingredient of soldIngredients){
  91. let row = document.createElement("tr");
  92. ingredientsBody.appendChild(row);
  93. let name = document.createElement("td");
  94. name.innerText = `${ingredient.name} (${ingredient.unit})`;
  95. row.appendChild(name);
  96. let used = document.createElement("td");
  97. used.innerText = ingredient.quantity;
  98. row.appendChild(used);
  99. let remaining = document.createElement("td");
  100. remaining.innerText = ingredient.quantityRemaining;
  101. row.appendChild(remaining);
  102. }
  103. //Populate recipes table
  104. let recipesBody = document.querySelector("#recipesData tbody");
  105. while(recipesBody.children.length > 0){
  106. recipesBody.removeChild(recipesBody.firstChild);
  107. }
  108. for(let recipe of recipes){
  109. let row = document.createElement("tr");
  110. recipesBody.appendChild(row);
  111. let name = document.createElement("td");
  112. name.innerText = recipe.name;
  113. row.appendChild(name);
  114. let quantity = document.createElement("td");
  115. quantity.innerText = recipe.quantity;
  116. row.appendChild(quantity);
  117. let revenue = document.createElement("td");
  118. revenue.innerText = `$${(recipe.quantity * recipe.price).toFixed(2)}`;
  119. row.appendChild(revenue);
  120. }
  121. //Populate purchases table
  122. let purchasesBody = document.querySelector("#purchasesData tbody");
  123. while(purchasesBody.children.length > 0){
  124. purchasesBody.removeChild(purchasesBody.firstChild);
  125. }
  126. for(let ingredient of purchaseIngredients){
  127. let row = document.createElement("tr");
  128. purchasesBody.appendChild(row);
  129. let name = document.createElement("td");
  130. name.innerText = `${ingredient.name} (${ingredient.unit})`;
  131. row.appendChild(name);
  132. let amount = document.createElement("td");
  133. amount.innerText = ingredient.amount;
  134. row.appendChild(amount);
  135. }
  136. //Populate totals
  137. document.querySelector("#revenueTotal").innerText = `$${this.revenueTotal.toFixed(2)}`;
  138. document.querySelector("#soldTotal").innerText = this.recipeTotal;
  139. },
  140. newDates: function(){
  141. let from = document.querySelector("#from").value;
  142. let to = new Date(document.querySelector("#to").value);
  143. if(from === "" || to === ""){
  144. banner.createError("Invalid date");
  145. return;
  146. }else{
  147. from = new Date(from);
  148. to = new Date(to);
  149. }
  150. if(validator.transaction.date(from, to)){
  151. let startIndex = 0;
  152. let endIndex = 0;
  153. for(let i = 0; i < data.transactions.length; i++){
  154. if(from < new Date(data.transactions[i].date)){
  155. startIndex = i;
  156. break;
  157. }
  158. }
  159. for(let i = 0; i < data.transactions.length; i++){
  160. if(to < new Date(data.transactions[i].date)){
  161. endIndex = i;
  162. break;
  163. }
  164. }
  165. this.populate(data.transactions.slice(startIndex, endIndex));
  166. }
  167. }
  168. }