controller.js 4.2 KB

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