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