data.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. window.dataObj = {
  2. display: function(){
  3. clearScreen();
  4. document.querySelector("#dataStrand").style.display = "flex";
  5. //Fill in month
  6. let months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  7. document.querySelector("#month").innerText = `Month of ${months[new Date().getMonth()]}`;
  8. },
  9. populate: function(transactions){
  10. //Create object to store number of recipes sold
  11. let recipes = [];
  12. for(let recipe of merchant.recipes){
  13. recipes.push({
  14. id: recipe._id,
  15. name: recipe.name,
  16. quantity: 0,
  17. ingredients: recipe.ingredients,
  18. price: recipe.price / 100
  19. });
  20. }
  21. console.log(transactions);
  22. //Populate number of recipes sold
  23. let revenueTotal = 0;
  24. let recipeTotal = 0;
  25. for(let transaction of transactions){
  26. for(let transactionRecipe of transaction.recipes){
  27. for(let recipeCounter of recipes){
  28. if(transactionRecipe === recipeCounter.id){
  29. recipeCounter.quantity++;
  30. recipeTotal++;
  31. revenueTotal += (recipeCounter.price * recipeCounter.quantity);
  32. break;
  33. }
  34. }
  35. }
  36. }
  37. //Create object to store amount of ingredients sold
  38. let ingredients = [];
  39. for(let item of merchant.inventory){
  40. ingredients.push({
  41. id: item.ingredient._id,
  42. name: item.ingredient.name,
  43. quantity: 0,
  44. quantityRemaining: item.quantity,
  45. unit: item.ingredient.unit
  46. });
  47. }
  48. //Populate amount of ingredients sold
  49. for(let recipe of recipes){
  50. for(let recipeIngredient of recipe.ingredients){
  51. for(let newIngredient of ingredients){
  52. if(newIngredient.id === recipeIngredient.ingredient._id){
  53. newIngredient.quantity += (recipeIngredient.quantity * recipe.quantity);
  54. break;
  55. }
  56. }
  57. }
  58. }
  59. //Populate Ingredients table
  60. let ingredientsBody = document.querySelector("#ingredientsData tbody");
  61. for(let ingredient of ingredients){
  62. let row = document.createElement("tr");
  63. ingredientsBody.appendChild(row);
  64. let name = document.createElement("td");
  65. name.innerText = `${ingredient.name} (${ingredient.unit})`;
  66. row.appendChild(name);
  67. let used = document.createElement("td");
  68. used.innerText = ingredient.quantity;
  69. row.appendChild(used);
  70. let remaining = document.createElement("td");
  71. remaining.innerText = ingredient.quantityRemaining;
  72. row.appendChild(remaining);
  73. }
  74. //Populate recipes table
  75. let recipesBody = document.querySelector("#recipesData tbody");
  76. for(let recipe of recipes){
  77. let row = document.createElement("tr");
  78. recipesBody.appendChild(row);
  79. let name = document.createElement("td");
  80. name.innerText = recipe.name;
  81. row.appendChild(name);
  82. let quantity = document.createElement("td");
  83. quantity.innerText = recipe.quantity;
  84. row.appendChild(quantity);
  85. let revenue = document.createElement("td");
  86. revenue.innerText = `$${recipe.quantity * recipe.price}`;
  87. row.appendChild(revenue);
  88. }
  89. //Populate totals
  90. document.querySelector("#revenueTotal").innerText = `$${revenueTotal.toFixed(2)}`;
  91. document.querySelector("#soldTotal").innerText = recipeTotal;
  92. },
  93. populatePurchases: function(purchases){
  94. //Create object for each merchant ingredient
  95. let ingredients = [];
  96. for(let item of merchant.inventory){
  97. ingredients.push({
  98. id: item.ingredient._id,
  99. name: item.ingredient.name,
  100. amount: 0,
  101. unit: item.ingredient.unit
  102. });
  103. }
  104. //Populate amount of ingredients purchased
  105. for(let purchase of purchases){
  106. for(let purchaseIngredient of purchase.ingredients){
  107. for(let newIngredient of ingredients){
  108. if(newIngredient.id === purchaseIngredient.ingredient){
  109. newIngredient.amount += purchaseIngredient.quantity;
  110. break;
  111. }
  112. }
  113. }
  114. }
  115. //Populate purchases table
  116. let purchasesBody = document.querySelector("#purchasesData tbody");
  117. for(let ingredient of ingredients){
  118. let row = document.createElement("tr");
  119. purchasesBody.appendChild(row);
  120. let name = document.createElement("td");
  121. name.innerText = `${ingredient.name} (${ingredient.unit})`;
  122. row.appendChild(name);
  123. let amount = document.createElement("td");
  124. amount.innerText = ingredient.amount;
  125. row.appendChild(amount);
  126. }
  127. }
  128. }