home.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. row.classList = "clickableRow";
  93. row.onclick = ()=>{window.graphObj.display("ingredient", ingredient)};
  94. ingredientsBody.appendChild(row);
  95. let name = document.createElement("td");
  96. name.innerText = `${ingredient.name} (${ingredient.unit})`;
  97. row.appendChild(name);
  98. let used = document.createElement("td");
  99. used.innerText = ingredient.quantity;
  100. row.appendChild(used);
  101. let remaining = document.createElement("td");
  102. remaining.innerText = ingredient.quantityRemaining;
  103. row.appendChild(remaining);
  104. }
  105. //Populate recipes table
  106. let recipesBody = document.querySelector("#recipesData tbody");
  107. while(recipesBody.children.length > 0){
  108. recipesBody.removeChild(recipesBody.firstChild);
  109. }
  110. for(let recipe of recipes){
  111. let row = document.createElement("tr");
  112. recipesBody.appendChild(row);
  113. let name = document.createElement("td");
  114. name.innerText = recipe.name;
  115. row.appendChild(name);
  116. let quantity = document.createElement("td");
  117. quantity.innerText = recipe.quantity;
  118. row.appendChild(quantity);
  119. let revenue = document.createElement("td");
  120. revenue.innerText = `$${(recipe.quantity * recipe.price).toFixed(2)}`;
  121. row.appendChild(revenue);
  122. }
  123. //Populate purchases table
  124. let purchasesBody = document.querySelector("#purchasesData tbody");
  125. while(purchasesBody.children.length > 0){
  126. purchasesBody.removeChild(purchasesBody.firstChild);
  127. }
  128. for(let ingredient of purchaseIngredients){
  129. let row = document.createElement("tr");
  130. purchasesBody.appendChild(row);
  131. let name = document.createElement("td");
  132. name.innerText = `${ingredient.name} (${ingredient.unit})`;
  133. row.appendChild(name);
  134. let amount = document.createElement("td");
  135. amount.innerText = ingredient.amount;
  136. row.appendChild(amount);
  137. }
  138. //Populate totals
  139. document.querySelector("#revenueTotal").innerText = `$${this.revenueTotal.toFixed(2)}`;
  140. document.querySelector("#soldTotal").innerText = this.recipeTotal;
  141. },
  142. newDates: function(){
  143. let from = document.querySelector("#from").value;
  144. let to = new Date(document.querySelector("#to").value);
  145. if(from === "" || to === ""){
  146. banner.createError("Invalid date");
  147. return;
  148. }else{
  149. from = new Date(from);
  150. to = new Date(to);
  151. }
  152. if(validator.transaction.date(from, to)){
  153. let startIndex = 0;
  154. let endIndex = 0;
  155. for(let i = 0; i < data.transactions.length; i++){
  156. if(from < new Date(data.transactions[i].date)){
  157. startIndex = i;
  158. break;
  159. }
  160. }
  161. for(let i = 0; i < data.transactions.length; i++){
  162. if(to < new Date(data.transactions[i].date)){
  163. endIndex = i;
  164. break;
  165. }
  166. }
  167. this.populate(data.transactions.slice(startIndex, endIndex));
  168. }
  169. }
  170. }