home.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. if(!this.isPopulated){
  11. this.populate(data.transactions);
  12. let date = new Date(new Date().getFullYear(), new Date().getMonth(), 1);
  13. document.querySelector("#homeFrom").valueAsDate = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 12);
  14. document.querySelector("#homeTo").valueAsDate = new Date();
  15. this.isPopulated = true;
  16. }
  17. },
  18. populate: function(transactions){
  19. this.recipeTotal = 0;
  20. this.revenueTotal = 0;
  21. //Create object to store number of recipes sold
  22. let recipes = [];
  23. for(let recipe of data.merchant.recipes){
  24. recipes.push({
  25. id: recipe._id,
  26. name: recipe.name,
  27. quantity: 0,
  28. ingredients: recipe.ingredients,
  29. price: recipe.price / 100
  30. });
  31. }
  32. //Create object to store amount of ingredients sold
  33. let soldIngredients = [];
  34. for(let item of data.merchant.inventory){
  35. soldIngredients.push({
  36. id: item.ingredient._id,
  37. name: item.ingredient.name,
  38. quantity: 0,
  39. quantityRemaining: item.quantity,
  40. unit: item.ingredient.unit
  41. });
  42. }
  43. //Create object for each merchant ingredient
  44. let purchaseIngredients = [];
  45. for(let item of data.merchant.inventory){
  46. purchaseIngredients.push({
  47. id: item.ingredient._id,
  48. name: item.ingredient.name,
  49. amount: 0,
  50. unit: item.ingredient.unit
  51. });
  52. }
  53. //Populate number of recipes sold
  54. let i = 0;
  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. row.classList = "clickableRow";
  118. row.onclick = ()=>{window.recipeObj.display(recipe)};
  119. let name = document.createElement("td");
  120. name.innerText = recipe.name;
  121. row.appendChild(name);
  122. let quantity = document.createElement("td");
  123. quantity.innerText = recipe.quantity;
  124. row.appendChild(quantity);
  125. let revenue = document.createElement("td");
  126. revenue.innerText = `$${(recipe.quantity * recipe.price).toFixed(2)}`;
  127. row.appendChild(revenue);
  128. }
  129. //Populate purchases table
  130. let purchasesBody = document.querySelector("#purchasesData tbody");
  131. while(purchasesBody.children.length > 0){
  132. purchasesBody.removeChild(purchasesBody.firstChild);
  133. }
  134. for(let ingredient of purchaseIngredients){
  135. let row = document.createElement("tr");
  136. purchasesBody.appendChild(row);
  137. let name = document.createElement("td");
  138. name.innerText = `${ingredient.name} (${ingredient.unit})`;
  139. row.appendChild(name);
  140. let amount = document.createElement("td");
  141. amount.innerText = ingredient.amount;
  142. row.appendChild(amount);
  143. }
  144. //Populate totals
  145. document.querySelector("#revenueTotal").innerText = `$${this.revenueTotal.toFixed(2)}`;
  146. document.querySelector("#soldTotal").innerText = this.recipeTotal;
  147. },
  148. newDates: function(){
  149. let from = document.querySelector("#homeFrom").value;
  150. let to = document.querySelector("#homeTo").value;
  151. if(from === "" || to === ""){
  152. banner.createError("Invalid date");
  153. return;
  154. }else{
  155. from = new Date(from);
  156. to = new Date(to);
  157. from.setMinutes(from.getMinutes() + from.getTimezoneOffset());
  158. to.setMinutes(to.getMinutes() + to.getTimezoneOffset());
  159. }
  160. if(validator.transaction.date(from, to)){
  161. window.fetchData(from, to, ()=>{
  162. let startIndex = 0;
  163. let endIndex = data.transactions.length;
  164. for(let i = 0; i < data.transactions.length; i++){
  165. if(from < data.transactions[i].date){
  166. startIndex = i;
  167. break;
  168. }
  169. }
  170. for(let i = 0; i < data.transactions.length; i++){
  171. if(to < data.transactions[i].date){
  172. endIndex = i;
  173. break;
  174. }
  175. }
  176. let newData = data.transactions.slice(startIndex, endIndex);
  177. this.populate(newData);
  178. });
  179. }
  180. }
  181. }