controller.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. addIngredientsComp.isPopulated = false;
  54. closeSidebar();
  55. }
  56. //Close any open sidebar
  57. let closeSidebar = ()=>{
  58. let sidebar = document.querySelector("#sidebarDiv");
  59. for(let i = 0; i < sidebar.children.length; i++){
  60. sidebar.children[i].style.display = "none";
  61. }
  62. sidebar.classList = "sidebarHide";
  63. }
  64. /*
  65. Open a specific sidebar
  66. Input:
  67. sidebar: the outermost element of the sidebar (must contain class sidebar)
  68. */
  69. let openSidebar = (sidebar)=>{
  70. document.querySelector("#sidebarDiv").classList = "sidebar";
  71. let sideBars = document.querySelector("#sidebarDiv").children;
  72. for(let i = 0; i < sideBars.length; i++){
  73. sideBars[i].style.display = "none";
  74. }
  75. sidebar.style.display = "flex";
  76. }
  77. /*
  78. Gets the indices of two dates from transactions
  79. Inputs
  80. from: starting date
  81. to: ending date (default to now)
  82. Output
  83. Array containing starting index and ending index
  84. Note: Will return false if it cannot find both necessary dates
  85. */
  86. let dateIndices = (from, to = new Date())=>{
  87. let indices = [];
  88. for(let i = 0; i < transactions.length; i++){
  89. if(transactions[i].date > from){
  90. indices.push(i);
  91. break;
  92. }
  93. }
  94. for(let i = transactions.length - 1; i >=0; i--){
  95. if(transactions[i].date < to){
  96. indices.push(i);
  97. break;
  98. }
  99. }
  100. if(indices.length < 2){
  101. return false;
  102. }
  103. return indices;
  104. }
  105. /*
  106. Gets the quantity of each ingredient sold between two dates (dateRange)
  107. Inputs
  108. dateRange: list containing a start date and an end date
  109. Output
  110. List of objects
  111. id: id of specific ingredient
  112. quantity: quantity sold of that ingredient
  113. name: name of the ingredient
  114. */
  115. let ingredientsSold = (dateRange)=>{
  116. if(!dateRange){
  117. return false;
  118. }
  119. let recipes = recipesSold(dateRange);
  120. let ingredientList = [];
  121. for(let i = 0; i < recipes.length; i++){
  122. for(let j = 0; j < merchant.recipes.length; j++){
  123. for(let k = 0; k < merchant.recipes[j].ingredients.length; k++){
  124. let exists = false;
  125. for(let l = 0; l < ingredientList.length; l++){
  126. if(ingredientList[l].id === merchant.recipes[j].ingredients[k].ingredient._id){
  127. exists = true;
  128. ingredientList[l].quantity += merchant.recipes[j].ingredients[k].quantity * recipes[i].quantity;
  129. break;
  130. }
  131. }
  132. if(!exists){
  133. ingredientList.push({
  134. id: merchant.recipes[j].ingredients[k].ingredient._id,
  135. quantity: merchant.recipes[j].ingredients[k].quantity * recipes[i].quantity,
  136. name: merchant.recipes[j].ingredients[k].ingredient.name,
  137. unit: merchant.recipes[j].ingredients[k].ingredient.unit
  138. })
  139. }
  140. }
  141. }
  142. }
  143. return ingredientList;
  144. }
  145. /*
  146. Gets the quantity of a single ingredient sold between two dates (dateRange)
  147. Input:
  148. dateRange: array containing two elements, start and end indices
  149. id: id of the ingredient to calculate
  150. Return: (int) Quantity of recipes sold
  151. */
  152. let ingredientSold = (dateRange, id)=>{
  153. let recipes = recipesSold(dateRange);
  154. let total = 0;
  155. let checkRecipes = [];
  156. let quantities = [];
  157. for(let i = 0; i < merchant.recipes.length; i++){
  158. for(let j = 0; j < merchant.recipes[i].ingredients.length; j++){
  159. if(merchant.recipes[i].ingredients[j].ingredient._id === id){
  160. checkRecipes.push(merchant.recipes[i]._id);
  161. quantities.push(merchant.recipes[i].ingredients[j].quantity);
  162. break;
  163. }
  164. }
  165. }
  166. for(let i = 0; i < recipes.length; i++){
  167. for(let i = 0; i < checkRecipes.length; i++){
  168. if(checkRecipes[i] === recipes[i].id){
  169. total += recipes[i].quantity * quantities[i];
  170. break;
  171. }
  172. }
  173. }
  174. return total;
  175. }
  176. /*
  177. Gets the number of recipes sold between two dates (dateRange)
  178. Inputs:
  179. dateRange: array containing a start date and an end date
  180. Return:
  181. List of objects
  182. id: id of specific recipe
  183. quantity: quantity sold of that recipe
  184. */
  185. let recipesSold = (dateRange)=>{
  186. let recipeList = [];
  187. for(let i = dateRange[0]; i <= dateRange[1]; i++){
  188. for(let j = 0; j < transactions[i].recipes.length; j++){
  189. let exists = false;
  190. for(let k = 0; k < recipeList.length; k++){
  191. if(recipeList[k].id === transactions[i].recipes[j].recipe){
  192. exists = true;
  193. recipeList[k].quantity += transactions[i].recipes[j].quantity;
  194. break;
  195. }
  196. }
  197. if(!exists){
  198. recipeList.push({
  199. id: transactions[i].recipes[j].recipe,
  200. quantity: transactions[i].recipes[j].quantity
  201. })
  202. }
  203. }
  204. }
  205. return recipeList;
  206. }
  207. /*
  208. Groups all of the merchant's ingredients by their category
  209. Return:
  210. Array of objects
  211. ingredients: Array of objects
  212. id: Id of ingredient
  213. name: Name of ingredient
  214. quantity: Merchant's quantity of this ingredient
  215. unit: Measurement unit
  216. name: Category name
  217. */
  218. let categorizeIngredients = (ingredients)=>{
  219. let ingredientsByCategory = [];
  220. for(let i = 0; i < ingredients.length; i++){
  221. let categoryExists = false;
  222. for(let j = 0; j < ingredientsByCategory.length; j++){
  223. if(ingredients[i].ingredient.category === ingredientsByCategory[j].name){
  224. ingredientsByCategory[j].ingredients.push({
  225. id: ingredients[i].ingredient._id,
  226. name: ingredients[i].ingredient.name,
  227. quantity: ingredients[i].quantity,
  228. unit: ingredients[i].ingredient.unit
  229. });
  230. categoryExists = true;
  231. break;
  232. }
  233. }
  234. if(!categoryExists){
  235. ingredientsByCategory.push({
  236. name: ingredients[i].ingredient.category,
  237. ingredients: [{
  238. id: ingredients[i].ingredient._id,
  239. name: ingredients[i].ingredient.name,
  240. quantity: ingredients[i].quantity,
  241. unit: ingredients[i].ingredient.unit
  242. }]
  243. });
  244. }
  245. }
  246. return ingredientsByCategory;
  247. }
  248. let categorizeIngredientsFromDB = (ingredients)=>{
  249. let ingredientsByCategory = [];
  250. for(let i = 0; i < ingredients.length; i++){
  251. let categoryExists = false;
  252. for(let j = 0; j < ingredientsByCategory.length; j++){
  253. if(ingredients[i].category === ingredientsByCategory[j].name){
  254. ingredientsByCategory[j].ingredients.push({
  255. id: ingredients[i]._id,
  256. name: ingredients[i].name,
  257. unit: ingredients[i].unit
  258. });
  259. categoryExists = true;
  260. break;
  261. }
  262. }
  263. if(!categoryExists){
  264. ingredientsByCategory.push({
  265. name: ingredients[i].category,
  266. ingredients: [{
  267. id: ingredients[i]._id,
  268. name: ingredients[i].name,
  269. unit: ingredients[i].unit
  270. }]
  271. });
  272. }
  273. }
  274. return ingredientsByCategory;
  275. }
  276. for(let transaction of transactions){
  277. transaction.date = new Date(transaction.date);
  278. }
  279. homeStrandObj.display();