controller.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. let changeStrand = (name)=>{
  2. for(let strand of document.querySelectorAll(".strand")){
  3. strand.style.display = "none";
  4. }
  5. for(let button of document.querySelectorAll(".menu > button")){
  6. button.classList = "";
  7. button.onclick = ()=>{changeStrand(`${button.id.slice(0, button.id.indexOf("Btn"))}Strand`)};
  8. }
  9. let activeButton = document.querySelector(`#${name.slice(0, name.indexOf("Strand"))}Btn`);
  10. activeButton.classList = "active";
  11. activeButton.onclick = undefined;
  12. closeSidebar();
  13. document.querySelector(`#${name}`).style.display = "flex";
  14. window[`${name}Obj`].display();
  15. }
  16. let closeSidebar = ()=>{
  17. let sidebar = document.querySelector(".sidebar");
  18. if(sidebar){
  19. sidebar.classList = "sidebarHide";
  20. }
  21. }
  22. //Gets the indices of two dates from transactions
  23. //Inputs
  24. // from: starting date
  25. // to: ending date (default to now)
  26. //Output
  27. // Array containing starting index and ending index
  28. //Note: Will return false if it cannot find both necessary dates
  29. let dateIndices = (from, to = new Date())=>{
  30. let indices = [];
  31. for(let i = 0; i < transactions.length; i++){
  32. if(transactions[i].date > from){
  33. indices.push(i);
  34. break;
  35. }
  36. }
  37. for(let i = transactions.length - 1; i >=0; i--){
  38. if(transactions[i].date < to){
  39. indices.push(i);
  40. break;
  41. }
  42. }
  43. if(indices.length < 2){
  44. return false;
  45. }
  46. return indices;
  47. }
  48. //Gets the quantity of each ingredient sold between two dates (dateRange)
  49. //Inputs
  50. // dateRange: list containing a start date and an end date
  51. //Output
  52. // List of objects
  53. // id: id of specific ingredient
  54. // quantity: quantity sold of that ingredient
  55. // name: name of the ingredient
  56. let ingredientsSold = (dateRange)=>{
  57. if(!dateRange){
  58. return false;
  59. }
  60. let recipes = recipesSold(dateRange);
  61. let ingredientList = [];
  62. for(let recipe of recipes){
  63. for(let merchRecipe of merchant.recipes){
  64. for(let ingredient of merchRecipe.ingredients){
  65. let exists = false;
  66. for(let item of ingredientList){
  67. if(item.id === ingredient.ingredient._id){
  68. exists = true;
  69. item.quantity += ingredient.quantity * recipe.quantity;
  70. break;
  71. }
  72. }
  73. if(!exists){
  74. ingredientList.push({
  75. id: ingredient.ingredient._id,
  76. quantity: ingredient.quantity * recipe.quantity,
  77. name: ingredient.ingredient.name,
  78. unit: ingredient.ingredient.unit
  79. })
  80. }
  81. }
  82. }
  83. }
  84. return ingredientList;
  85. }
  86. //Gets the quantity of a single ingredient sold between two dates (dateRange)
  87. let ingredientSold = (dateRange, id)=>{
  88. let recipes = recipesSold(dateRange);
  89. let total = 0;
  90. let checkRecipes = [];
  91. let quantities = [];
  92. for(let merchRecipe of merchant.recipes){
  93. for(let merchIngredient of merchRecipe.ingredients){
  94. if(merchIngredient.ingredient._id === id){
  95. checkRecipes.push(merchRecipe._id);
  96. quantities.push(merchIngredient.quantity);
  97. break;
  98. }
  99. }
  100. }
  101. for(let recipe of recipes){
  102. for(let i = 0; i < checkRecipes.length; i++){
  103. if(checkRecipes[i] === recipe.id){
  104. total += recipe.quantity * quantities[i];
  105. break;
  106. }
  107. }
  108. }
  109. return total;
  110. }
  111. //Gets the number of recipes sold between two dates (dateRange)
  112. //Inputs
  113. // dateRange: array containing a start date and an end date
  114. //Output
  115. // List of objects
  116. // id: id of specific recipe
  117. // quantity: quantity sold of that recipe
  118. let recipesSold = (dateRange)=>{
  119. let recipeList = [];
  120. for(let i = dateRange[0]; i <= dateRange[1]; i++){
  121. for(let recipe of transactions[i].recipes){
  122. let exists = false;
  123. for(let item of recipeList){
  124. if(item.id === recipe.recipe){
  125. exists = true;
  126. item.quantity += recipe.quantity;
  127. break;
  128. }
  129. }
  130. if(!exists){
  131. recipeList.push({
  132. id: recipe.recipe,
  133. quantity: recipe.quantity
  134. })
  135. }
  136. }
  137. }
  138. return recipeList;
  139. }
  140. let categorizeIngredients = ()=>{
  141. let ingredientsByCategory = [];
  142. for(let item of merchant.inventory){
  143. let categoryExists = false;
  144. for(let category of ingredientsByCategory){
  145. if(item.ingredient.category === category.name){
  146. category.ingredients.push({
  147. id: item.ingredient._id,
  148. name: item.ingredient.name,
  149. quantity: item.quantity,
  150. unit: item.ingredient.unit
  151. });
  152. categoryExists = true;
  153. break;
  154. }
  155. }
  156. if(!categoryExists){
  157. ingredientsByCategory.push({
  158. name: item.ingredient.category,
  159. ingredients: [{
  160. id: item.ingredient._id,
  161. name: item.ingredient.name,
  162. quantity: item.quantity,
  163. unit: item.ingredient.unit
  164. }]
  165. });
  166. }
  167. }
  168. return ingredientsByCategory;
  169. }
  170. for(let transaction of transactions){
  171. transaction.date = new Date(transaction.date);
  172. }
  173. homeStrandObj.display();