controller.js 6.0 KB

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