home.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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;
  15. }
  16. },
  17. populate: function(transactions){
  18. console.log(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. for(let transaction of transactions){
  55. for(let recipe of transaction.recipes){
  56. for(let newRecipe of recipes){
  57. if(recipe.recipe === newRecipe.id){
  58. newRecipe.quantity += recipe.quantity;
  59. this.recipeTotal += recipe.quantity;
  60. this.revenueTotal += (newRecipe.price * recipe.quantity);
  61. break;
  62. }
  63. }
  64. }
  65. }
  66. //Populate amount of ingredients sold
  67. for(let recipe of recipes){
  68. for(let recipeIngredient of recipe.ingredients){
  69. for(let newIngredient of soldIngredients){
  70. if(newIngredient.id === recipeIngredient.ingredient){
  71. newIngredient.quantity += (recipeIngredient.quantity * recipe.quantity);
  72. break;
  73. }
  74. }
  75. }
  76. }
  77. //Populate amount of ingredients purchased
  78. for(let purchase of data.purchases){
  79. for(let newPurchaseIngredient of purchase.ingredients){
  80. for(let newIngredient of purchaseIngredients){
  81. if(newIngredient.id === newPurchaseIngredient.ingredient){
  82. newIngredient.amount += newPurchaseIngredient.quantity;
  83. break;
  84. }
  85. }
  86. }
  87. }
  88. //Populate Ingredients table
  89. let ingredientsBody = document.querySelector("#ingredientsData tbody");
  90. while(ingredientsBody.children.length > 0){
  91. ingredientsBody.removeChild(ingredientsBody.firstChild);
  92. }
  93. for(let ingredient of soldIngredients){
  94. let row = document.createElement("tr");
  95. ingredientsBody.appendChild(row);
  96. row.classList = "clickableRow";
  97. row.onclick = ()=>{window.ingredientObj.display(ingredient)};
  98. let name = document.createElement("td");
  99. name.innerText = `${ingredient.name} (${ingredient.unit})`;
  100. row.appendChild(name);
  101. let used = document.createElement("td");
  102. used.innerText = ingredient.quantity;
  103. row.appendChild(used);
  104. let remaining = document.createElement("td");
  105. remaining.innerText = ingredient.quantityRemaining;
  106. row.appendChild(remaining);
  107. }
  108. //Populate recipes table
  109. let recipesBody = document.querySelector("#recipesData tbody");
  110. while(recipesBody.children.length > 0){
  111. recipesBody.removeChild(recipesBody.firstChild);
  112. }
  113. for(let recipe of recipes){
  114. let row = document.createElement("tr");
  115. recipesBody.appendChild(row);
  116. let name = document.createElement("td");
  117. name.innerText = recipe.name;
  118. row.appendChild(name);
  119. let quantity = document.createElement("td");
  120. quantity.innerText = recipe.quantity;
  121. row.appendChild(quantity);
  122. let revenue = document.createElement("td");
  123. revenue.innerText = `$${(recipe.quantity * recipe.price).toFixed(2)}`;
  124. row.appendChild(revenue);
  125. }
  126. //Populate purchases table
  127. let purchasesBody = document.querySelector("#purchasesData tbody");
  128. while(purchasesBody.children.length > 0){
  129. purchasesBody.removeChild(purchasesBody.firstChild);
  130. }
  131. for(let ingredient of purchaseIngredients){
  132. let row = document.createElement("tr");
  133. purchasesBody.appendChild(row);
  134. let name = document.createElement("td");
  135. name.innerText = `${ingredient.name} (${ingredient.unit})`;
  136. row.appendChild(name);
  137. let amount = document.createElement("td");
  138. amount.innerText = ingredient.amount;
  139. row.appendChild(amount);
  140. }
  141. //Populate totals
  142. document.querySelector("#revenueTotal").innerText = `$${this.revenueTotal.toFixed(2)}`;
  143. document.querySelector("#soldTotal").innerText = this.recipeTotal;
  144. },
  145. newDates: function(){
  146. let from = new Date(document.querySelector("#homeFrom").value);
  147. let to = new Date(document.querySelector("#homeTo").value);
  148. if(from === "" || to === ""){
  149. banner.createError("Invalid date");
  150. return;
  151. }else{
  152. from = new Date(from);
  153. to = new Date(to);
  154. }
  155. if(validator.transaction.date(from, to)){
  156. window.fetchData(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. }
  175. }