controller.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. //Updates all specified item in the merchant's inventory and updates the page
  17. //If ingredient doesn't exist, add it
  18. //Inputs:
  19. // Array of objects
  20. // id: id of ingredient
  21. // quantity: updated quantity (optional)
  22. // name: name of ingredient (only for new ingredient)
  23. // category: category of ingredient (only for new ingredient)
  24. // unit: unit of measurement (only for new ingredient)
  25. // remove: if true, remove ingredient from inventory
  26. let updateInventory = (ingredients, remove = false)=>{
  27. for(let i = 0; i < ingredients.length; i++){
  28. let isNew = true;
  29. for(let j = 0; j < merchant.inventory.length; j++){
  30. if(merchant.inventory[j].ingredient._id === ingredients[i].id){
  31. if(remove){
  32. merchant.inventory.splice(i, 1);
  33. }else{
  34. merchant.inventory[j].quantity = ingredients[i].quantity;
  35. }
  36. isNew = false;
  37. break;
  38. }
  39. }
  40. if(isNew){
  41. merchant.inventory.push(ingredients[i]);
  42. }
  43. }
  44. homeStrandObj.drawInventoryCheckCard();
  45. ingredientsStrandObj.populateIngredients();
  46. }
  47. let closeSidebar = ()=>{
  48. let sidebar = document.querySelector("#sidebarDiv");
  49. for(let i = 0; i < sidebar.children.length; i++){
  50. sidebar.children[i].style.display = "none";
  51. }
  52. sidebar.classList = "sidebarHide";
  53. }
  54. let openSidebar = (sidebar)=>{
  55. document.querySelector("#sidebarDiv").classList = "sidebar";
  56. let sideBars = document.querySelector("#sidebarDiv").children;
  57. for(let i = 0; i < sideBars.length; i++){
  58. sideBars[i].style.display = "none";
  59. }
  60. sidebar.style.display = "flex";
  61. }
  62. //Gets the indices of two dates from transactions
  63. //Inputs
  64. // from: starting date
  65. // to: ending date (default to now)
  66. //Output
  67. // Array containing starting index and ending index
  68. //Note: Will return false if it cannot find both necessary dates
  69. let dateIndices = (from, to = new Date())=>{
  70. let indices = [];
  71. for(let i = 0; i < transactions.length; i++){
  72. if(transactions[i].date > from){
  73. indices.push(i);
  74. break;
  75. }
  76. }
  77. for(let i = transactions.length - 1; i >=0; i--){
  78. if(transactions[i].date < to){
  79. indices.push(i);
  80. break;
  81. }
  82. }
  83. if(indices.length < 2){
  84. return false;
  85. }
  86. return indices;
  87. }
  88. //Gets the quantity of each ingredient sold between two dates (dateRange)
  89. //Inputs
  90. // dateRange: list containing a start date and an end date
  91. //Output
  92. // List of objects
  93. // id: id of specific ingredient
  94. // quantity: quantity sold of that ingredient
  95. // name: name of the ingredient
  96. let ingredientsSold = (dateRange)=>{
  97. if(!dateRange){
  98. return false;
  99. }
  100. let recipes = recipesSold(dateRange);
  101. let ingredientList = [];
  102. for(let i = 0; i < recipes.length; i++){
  103. for(let j = 0; j < merchant.recipes.length; j++){
  104. for(let k = 0; k < merchant.recipes[j].ingredients.length; k++){
  105. let exists = false;
  106. for(let l = 0; l < ingredientList.length; l++){
  107. if(ingredientList[l].id === merchant.recipes[j].ingredients[k].ingredient._id){
  108. exists = true;
  109. ingredientList[l].quantity += merchant.recipes[j].ingredients[k].quantity * recipes[i].quantity;
  110. break;
  111. }
  112. }
  113. if(!exists){
  114. ingredientList.push({
  115. id: merchant.recipes[j].ingredients[k].ingredient._id,
  116. quantity: merchant.recipes[j].ingredients[k].quantity * recipes[i].quantity,
  117. name: merchant.recipes[j].ingredients[k].ingredient.name,
  118. unit: merchant.recipes[j].ingredients[k].ingredient.unit
  119. })
  120. }
  121. }
  122. }
  123. }
  124. return ingredientList;
  125. }
  126. //Gets the quantity of a single ingredient sold between two dates (dateRange)
  127. let ingredientSold = (dateRange, id)=>{
  128. let recipes = recipesSold(dateRange);
  129. let total = 0;
  130. let checkRecipes = [];
  131. let quantities = [];
  132. for(let i = 0; i < merchant.recipes.length; i++){
  133. for(let j = 0; j < merchant.recipes[i].ingredients.length; j++){
  134. if(merchant.recipes[i].ingredients[j].ingredient._id === id){
  135. checkRecipes.push(merchant.recipes[i]._id);
  136. quantities.push(merchant.recipes[i].ingredients[j].quantity);
  137. break;
  138. }
  139. }
  140. }
  141. for(let i = 0; i < recipes.length; i++){
  142. for(let i = 0; i < checkRecipes.length; i++){
  143. if(checkRecipes[i] === recipes[i].id){
  144. total += recipes[i].quantity * quantities[i];
  145. break;
  146. }
  147. }
  148. }
  149. return total;
  150. }
  151. //Gets the number of recipes sold between two dates (dateRange)
  152. //Inputs
  153. // dateRange: array containing a start date and an end date
  154. //Output
  155. // List of objects
  156. // id: id of specific recipe
  157. // quantity: quantity sold of that recipe
  158. let recipesSold = (dateRange)=>{
  159. let recipeList = [];
  160. for(let i = dateRange[0]; i <= dateRange[1]; i++){
  161. for(let j = 0; j < transactions[i].recipes.length; j++){
  162. let exists = false;
  163. for(let k = 0; k < recipeList.length; k++){
  164. if(recipeList[k].id === transactions[i].recipes[j].recipe){
  165. exists = true;
  166. recipeList[k].quantity += transactions[i].recipes[j].quantity;
  167. break;
  168. }
  169. }
  170. if(!exists){
  171. recipeList.push({
  172. id: transactions[i].recipes[j].recipe,
  173. quantity: transactions[i].recipes[j].quantity
  174. })
  175. }
  176. }
  177. }
  178. return recipeList;
  179. }
  180. let categorizeIngredients = ()=>{
  181. let ingredientsByCategory = [];
  182. for(let i = 0; i < merchant.inventory.length; i++){
  183. let categoryExists = false;
  184. for(let j = 0; j < ingredientsByCategory.length; j++){
  185. if(merchant.inventory[i].ingredient.category === ingredientsByCategory[j].name){
  186. ingredientsByCategory[j].ingredients.push({
  187. id: merchant.inventory[i].ingredient._id,
  188. name: merchant.inventory[i].ingredient.name,
  189. quantity: merchant.inventory[i].quantity,
  190. unit: merchant.inventory[i].ingredient.unit
  191. });
  192. categoryExists = true;
  193. break;
  194. }
  195. }
  196. if(!categoryExists){
  197. ingredientsByCategory.push({
  198. name: merchant.inventory[i].ingredient.category,
  199. ingredients: [{
  200. id: merchant.inventory[i].ingredient._id,
  201. name: merchant.inventory[i].ingredient.name,
  202. quantity: merchant.inventory[i].quantity,
  203. unit: merchant.inventory[i].ingredient.unit
  204. }]
  205. });
  206. }
  207. }
  208. return ingredientsByCategory;
  209. }
  210. for(let transaction of transactions){
  211. transaction.date = new Date(transaction.date);
  212. }
  213. homeStrandObj.display();