controller.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. let dateIndices = (from, to = new Date())=>{
  29. let indices = [];
  30. for(let i = 0; i < transactions.length; i++){
  31. if(transactions[i].date > from){
  32. indices[0] = i;
  33. break;
  34. }
  35. }
  36. for(let i = transactions.length - 1; i >=0; i--){
  37. if(transactions[i].date < to){
  38. indices[1] = i;
  39. break;
  40. }
  41. }
  42. return indices;
  43. }
  44. //Gets the quantity of each ingredient sold between two dates (dateRange)
  45. //Inputs
  46. // dateRange: list containing a start date and an end date
  47. //Output
  48. // List of objects
  49. // id: id of specific ingredient
  50. // quantity: quantity sold of that ingredient
  51. // name: name of the ingredient
  52. let ingredientsSold = (dateRange)=>{
  53. let recipes = recipesSold(dateRange);
  54. let ingredientList = [];
  55. for(let recipe of recipes){
  56. for(let merchRecipe of merchant.recipes){
  57. for(let ingredient of merchRecipe.ingredients){
  58. let exists = false;
  59. for(let item of ingredientList){
  60. if(item.id === ingredient.ingredient._id){
  61. exists = true;
  62. item.quantity += ingredient.quantity * recipe.quantity;
  63. break;
  64. }
  65. }
  66. if(!exists){
  67. ingredientList.push({
  68. id: ingredient.ingredient._id,
  69. quantity: ingredient.quantity * recipe.quantity,
  70. name: ingredient.ingredient.name,
  71. unit: ingredient.ingredient.unit
  72. })
  73. }
  74. }
  75. }
  76. }
  77. return ingredientList;
  78. }
  79. //Gets the quantity of a single ingredient sold between two dates (dateRange)
  80. let ingredientSold = (dateRange, id)=>{
  81. let recipes = recipesSold(dateRange);
  82. let total = 0;
  83. let checkRecipes = [];
  84. let quantities = [];
  85. for(let merchRecipe of merchant.recipes){
  86. for(let merchIngredient of merchRecipe.ingredients){
  87. if(merchIngredient.ingredient._id === id){
  88. checkRecipes.push(merchRecipe._id);
  89. quantities.push(merchIngredient.quantity);
  90. break;
  91. }
  92. }
  93. }
  94. for(let recipe of recipes){
  95. for(let i = 0; i < checkRecipes.length; i++){
  96. if(checkRecipes[i] === recipe.id){
  97. total += recipe.quantity * quantities[i];
  98. break;
  99. }
  100. }
  101. }
  102. return total;
  103. }
  104. //Gets the number of recipes sold between two dates (dateRange)
  105. //Inputs
  106. // dateRange: array containing a start date and an end date
  107. //Output
  108. // List of objects
  109. // id: id of specific recipe
  110. // quantity: quantity sold of that recipe
  111. let recipesSold = (dateRange)=>{
  112. let recipeList = [];
  113. for(let i = dateRange[0]; i <= dateRange[1]; i++){
  114. for(let recipe of transactions[i].recipes){
  115. let exists = false;
  116. for(let item of recipeList){
  117. if(item.id === recipe.recipe){
  118. exists = true;
  119. item.quantity += recipe.quantity;
  120. break;
  121. }
  122. }
  123. if(!exists){
  124. recipeList.push({
  125. id: recipe.recipe,
  126. quantity: recipe.quantity
  127. })
  128. }
  129. }
  130. }
  131. return recipeList;
  132. }
  133. for(let transaction of transactions){
  134. transaction.date = new Date(transaction.date);
  135. }
  136. homeStrandObj.display();