home.js 7.1 KB

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