controller.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. Changes to a different strand
  3. Input:
  4. name: name of the strand. Must end with "Strand"
  5. */
  6. let changeStrand = (name)=>{
  7. closeSidebar();
  8. for(let strand of document.querySelectorAll(".strand")){
  9. strand.style.display = "none";
  10. }
  11. for(let button of document.querySelectorAll(".menu > button")){
  12. button.classList = "";
  13. button.onclick = ()=>{changeStrand(`${button.id.slice(0, button.id.indexOf("Btn"))}Strand`)};
  14. }
  15. let activeButton = document.querySelector(`#${name.slice(0, name.indexOf("Strand"))}Btn`);
  16. activeButton.classList = "active";
  17. activeButton.onclick = undefined;
  18. document.querySelector(`#${name}`).style.display = "flex";
  19. window[`${name}Obj`].display();
  20. }
  21. /*
  22. Updates all specified item in the merchant's inventory and updates the page
  23. If ingredient doesn't exist, add it
  24. Inputs:
  25. Array of objects
  26. id: id of ingredient
  27. quantity: updated quantity (optional)
  28. name: name of ingredient (only for new ingredient)
  29. category: category of ingredient (only for new ingredient)
  30. unit: unit of measurement (only for new ingredient)
  31. remove: if true, remove ingredient from inventory
  32. */
  33. let updateInventory = (ingredients, remove = false)=>{
  34. for(let i = 0; i < ingredients.length; i++){
  35. let isNew = true;
  36. for(let j = 0; j < merchant.inventory.length; j++){
  37. if(merchant.inventory[j].ingredient._id === ingredients[i].id){
  38. if(remove){
  39. merchant.inventory.splice(i, 1);
  40. }else{
  41. merchant.inventory[j].quantity = ingredients[i].quantity;
  42. }
  43. isNew = false;
  44. break;
  45. }
  46. }
  47. if(isNew){
  48. merchant.inventory.push(ingredients[i]);
  49. }
  50. }
  51. homeStrandObj.drawInventoryCheckCard();
  52. ingredientsStrandObj.populateIngredients();
  53. }
  54. //Close any open sidebar
  55. let closeSidebar = ()=>{
  56. let sidebar = document.querySelector("#sidebarDiv");
  57. for(let i = 0; i < sidebar.children.length; i++){
  58. sidebar.children[i].style.display = "none";
  59. }
  60. sidebar.classList = "sidebarHide";
  61. }
  62. /*
  63. Open a specific sidebar
  64. Input:
  65. sidebar: the outermost element of the sidebar (must contain class sidebar)
  66. */
  67. let openSidebar = (sidebar)=>{
  68. console.log(sidebar);
  69. document.querySelector("#sidebarDiv").classList = "sidebar";
  70. let sideBars = document.querySelector("#sidebarDiv").children;
  71. for(let i = 0; i < sideBars.length; i++){
  72. sideBars[i].style.display = "none";
  73. }
  74. sidebar.style.display = "flex";
  75. }
  76. /*
  77. Gets the indices of two dates from transactions
  78. Inputs
  79. from: starting date
  80. to: ending date (default to now)
  81. Output
  82. Array containing starting index and ending index
  83. Note: Will return false if it cannot find both necessary dates
  84. */
  85. let dateIndices = (from, to = new Date())=>{
  86. let indices = [];
  87. for(let i = 0; i < transactions.length; i++){
  88. if(transactions[i].date > from){
  89. indices.push(i);
  90. break;
  91. }
  92. }
  93. for(let i = transactions.length - 1; i >=0; i--){
  94. if(transactions[i].date < to){
  95. indices.push(i);
  96. break;
  97. }
  98. }
  99. if(indices.length < 2){
  100. return false;
  101. }
  102. return indices;
  103. }
  104. /*
  105. Gets the quantity of each ingredient sold between two dates (dateRange)
  106. Inputs
  107. dateRange: list containing a start date and an end date
  108. Output
  109. List of objects
  110. id: id of specific ingredient
  111. quantity: quantity sold of that ingredient
  112. name: name of the ingredient
  113. */
  114. let ingredientsSold = (dateRange)=>{
  115. if(!dateRange){
  116. return false;
  117. }
  118. let recipes = recipesSold(dateRange);
  119. let ingredientList = [];
  120. for(let i = 0; i < recipes.length; i++){
  121. for(let j = 0; j < merchant.recipes.length; j++){
  122. for(let k = 0; k < merchant.recipes[j].ingredients.length; k++){
  123. let exists = false;
  124. for(let l = 0; l < ingredientList.length; l++){
  125. if(ingredientList[l].id === merchant.recipes[j].ingredients[k].ingredient._id){
  126. exists = true;
  127. ingredientList[l].quantity += merchant.recipes[j].ingredients[k].quantity * recipes[i].quantity;
  128. break;
  129. }
  130. }
  131. if(!exists){
  132. ingredientList.push({
  133. id: merchant.recipes[j].ingredients[k].ingredient._id,
  134. quantity: merchant.recipes[j].ingredients[k].quantity * recipes[i].quantity,
  135. name: merchant.recipes[j].ingredients[k].ingredient.name,
  136. unit: merchant.recipes[j].ingredients[k].ingredient.unit
  137. })
  138. }
  139. }
  140. }
  141. }
  142. return ingredientList;
  143. }
  144. /*
  145. Gets the quantity of a single ingredient sold between two dates (dateRange)
  146. Input:
  147. dateRange: array containing two elements, start and end indices
  148. id: id of the ingredient to calculate
  149. Return: (int) Quantity of recipes sold
  150. */
  151. let ingredientSold = (dateRange, id)=>{
  152. let recipes = recipesSold(dateRange);
  153. let total = 0;
  154. let checkRecipes = [];
  155. let quantities = [];
  156. for(let i = 0; i < merchant.recipes.length; i++){
  157. for(let j = 0; j < merchant.recipes[i].ingredients.length; j++){
  158. if(merchant.recipes[i].ingredients[j].ingredient._id === id){
  159. checkRecipes.push(merchant.recipes[i]._id);
  160. quantities.push(merchant.recipes[i].ingredients[j].quantity);
  161. break;
  162. }
  163. }
  164. }
  165. for(let i = 0; i < recipes.length; i++){
  166. for(let i = 0; i < checkRecipes.length; i++){
  167. if(checkRecipes[i] === recipes[i].id){
  168. total += recipes[i].quantity * quantities[i];
  169. break;
  170. }
  171. }
  172. }
  173. return total;
  174. }
  175. /*
  176. Gets the number of recipes sold between two dates (dateRange)
  177. Inputs:
  178. dateRange: array containing a start date and an end date
  179. Return:
  180. List of objects
  181. id: id of specific recipe
  182. quantity: quantity sold of that recipe
  183. */
  184. let recipesSold = (dateRange)=>{
  185. let recipeList = [];
  186. for(let i = dateRange[0]; i <= dateRange[1]; i++){
  187. for(let j = 0; j < transactions[i].recipes.length; j++){
  188. let exists = false;
  189. for(let k = 0; k < recipeList.length; k++){
  190. if(recipeList[k].id === transactions[i].recipes[j].recipe){
  191. exists = true;
  192. recipeList[k].quantity += transactions[i].recipes[j].quantity;
  193. break;
  194. }
  195. }
  196. if(!exists){
  197. recipeList.push({
  198. id: transactions[i].recipes[j].recipe,
  199. quantity: transactions[i].recipes[j].quantity
  200. })
  201. }
  202. }
  203. }
  204. return recipeList;
  205. }
  206. /*
  207. Groups all of the merchant's ingredients by their category
  208. Return:
  209. Array of objects
  210. ingredients: Array of objects
  211. id: Id of ingredient
  212. name: Name of ingredient
  213. quantity: Merchant's quantity of this ingredient
  214. unit: Measurement unit
  215. name: Category name
  216. */
  217. let categorizeIngredients = ()=>{
  218. let ingredientsByCategory = [];
  219. for(let i = 0; i < merchant.inventory.length; i++){
  220. let categoryExists = false;
  221. for(let j = 0; j < ingredientsByCategory.length; j++){
  222. if(merchant.inventory[i].ingredient.category === ingredientsByCategory[j].name){
  223. ingredientsByCategory[j].ingredients.push({
  224. id: merchant.inventory[i].ingredient._id,
  225. name: merchant.inventory[i].ingredient.name,
  226. quantity: merchant.inventory[i].quantity,
  227. unit: merchant.inventory[i].ingredient.unit
  228. });
  229. categoryExists = true;
  230. break;
  231. }
  232. }
  233. if(!categoryExists){
  234. ingredientsByCategory.push({
  235. name: merchant.inventory[i].ingredient.category,
  236. ingredients: [{
  237. id: merchant.inventory[i].ingredient._id,
  238. name: merchant.inventory[i].ingredient.name,
  239. quantity: merchant.inventory[i].quantity,
  240. unit: merchant.inventory[i].ingredient.unit
  241. }]
  242. });
  243. }
  244. }
  245. console.log(ingredientsByCategory);
  246. return ingredientsByCategory;
  247. }
  248. for(let transaction of transactions){
  249. transaction.date = new Date(transaction.date);
  250. }
  251. homeStrandObj.display();