data.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. //Populate number of recipes sold
  22. for(let transaction of transactions){
  23. for(let transactionRecipe of transaction.recipes){
  24. for(let recipeCounter of recipes){
  25. if(transactionRecipe === recipeCounter.id){
  26. recipeCounter.quantity++;
  27. }
  28. }
  29. }
  30. }
  31. //Create object to store amount of ingredients sold
  32. let ingredients = [];
  33. for(let item of merchant.inventory){
  34. ingredients.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. //Populate amount of ingredients sold
  43. for(let recipe of recipes){
  44. for(let recipeIngredient of recipe.ingredients){
  45. for(let newIngredient of ingredients){
  46. if(newIngredient.id === recipeIngredient.ingredient._id){
  47. newIngredient.quantity += (recipeIngredient.quantity * recipe.quantity);
  48. break;
  49. }
  50. }
  51. }
  52. }
  53. //Populate Ingredients table
  54. let ingredientsBody = document.querySelector("#ingredientsData tbody");
  55. for(let ingredient of ingredients){
  56. let row = document.createElement("tr");
  57. ingredientsBody.appendChild(row);
  58. let name = document.createElement("td");
  59. name.innerText = `${ingredient.name} (${ingredient.unit})`;
  60. row.appendChild(name);
  61. let used = document.createElement("td");
  62. used.innerText = ingredient.quantity;
  63. row.appendChild(used);
  64. let remaining = document.createElement("td");
  65. remaining.innerText = ingredient.quantityRemaining;
  66. row.appendChild(remaining);
  67. }
  68. //Populate recipes table
  69. let recipesBody = document.querySelector("#recipesData tbody");
  70. for(let recipe of recipes){
  71. let row = document.createElement("tr");
  72. recipesBody.appendChild(row);
  73. let name = document.createElement("td");
  74. name.innerText = recipe.name;
  75. row.appendChild(name);
  76. let quantity = document.createElement("td");
  77. quantity.innerText = recipe.quantity;
  78. row.appendChild(quantity);
  79. let revenue = document.createElement("td");
  80. revenue.innerText = `$${recipe.quantity * recipe.price}`;
  81. row.appendChild(revenue);
  82. }
  83. },
  84. populatePurchases: function(purchases){
  85. //Create object for each merchant ingredient
  86. let ingredients = [];
  87. for(let item of merchant.inventory){
  88. ingredients.push({
  89. id: item.ingredient._id,
  90. name: item.ingredient.name,
  91. amount: 0,
  92. unit: item.ingredient.unit
  93. });
  94. }
  95. //Populate amount of ingredients purchased
  96. for(let purchase of purchases){
  97. for(let purchaseIngredient of purchase.ingredients){
  98. for(let newIngredient of ingredients){
  99. if(newIngredient.id === purchaseIngredient.ingredient){
  100. newIngredient.amount += purchaseIngredient.quantity;
  101. break;
  102. }
  103. }
  104. }
  105. }
  106. //Populate purchases table
  107. let purchasesBody = document.querySelector("#purchasesData tbody");
  108. for(let ingredient of ingredients){
  109. let row = document.createElement("tr");
  110. purchasesBody.appendChild(row);
  111. let name = document.createElement("td");
  112. name.innerText = `${ingredient.name} (${ingredient.unit})`;
  113. row.appendChild(name);
  114. let amount = document.createElement("td");
  115. amount.innerText = ingredient.amount;
  116. row.appendChild(amount);
  117. }
  118. }
  119. }