home.js 7.2 KB

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