home.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. // if(i === 0){
  57. // console.log(transaction);
  58. // i++;
  59. // }
  60. for(let recipe of transaction.recipes){
  61. for(let newRecipe of recipes){
  62. if(recipe.recipe === newRecipe.id){
  63. newRecipe.quantity += recipe.quantity;
  64. this.recipeTotal += recipe.quantity;
  65. this.revenueTotal += (newRecipe.price * recipe.quantity);
  66. break;
  67. }
  68. }
  69. }
  70. }
  71. //Populate amount of ingredients sold
  72. for(let recipe of recipes){
  73. for(let recipeIngredient of recipe.ingredients){
  74. for(let newIngredient of soldIngredients){
  75. if(newIngredient.id === recipeIngredient.ingredient){
  76. newIngredient.quantity += (recipeIngredient.quantity * recipe.quantity);
  77. break;
  78. }
  79. }
  80. }
  81. }
  82. //Populate amount of ingredients purchased
  83. for(let purchase of data.purchases){
  84. for(let newPurchaseIngredient of purchase.ingredients){
  85. for(let newIngredient of purchaseIngredients){
  86. if(newIngredient.id === newPurchaseIngredient.ingredient){
  87. newIngredient.amount += newPurchaseIngredient.quantity;
  88. break;
  89. }
  90. }
  91. }
  92. }
  93. //Populate Ingredients table
  94. let ingredientsBody = document.querySelector("#ingredientsData tbody");
  95. while(ingredientsBody.children.length > 0){
  96. ingredientsBody.removeChild(ingredientsBody.firstChild);
  97. }
  98. for(let ingredient of soldIngredients){
  99. let row = document.createElement("tr");
  100. ingredientsBody.appendChild(row);
  101. row.classList = "clickableRow";
  102. row.onclick = ()=>{window.ingredientObj.display(ingredient)};
  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("#homeFrom").value);
  152. let to = new Date(document.querySelector("#homeTo").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. from.setMinutes(from.getMinutes() + from.getTimezoneOffset());
  160. to.setMinutes(to.getMinutes() + to.getTimezoneOffset());
  161. }
  162. if(validator.transaction.date(from, to)){
  163. window.fetchData(from, to, ()=>{
  164. let startIndex = 0;
  165. let endIndex = data.transactions.length;
  166. for(let i = 0; i < data.transactions.length; i++){
  167. if(from < new Date(data.transactions[i].date)){
  168. startIndex = i;
  169. break;
  170. }
  171. }
  172. for(let i = 0; i < data.transactions.length; i++){
  173. if(to < new Date(data.transactions[i].date)){
  174. endIndex = i;
  175. break;
  176. }
  177. }
  178. let newData = data.transactions.slice(startIndex, endIndex);
  179. this.populate(newData);
  180. });
  181. }
  182. }
  183. }