controller.js 11 KB

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