controller.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*
  2. Switches 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. quantityChange: change in quantity (if not removing)
  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.populateByProperty("category");
  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. /*
  100. Updates an order in the front end
  101. Can create, edit or remove
  102. order = {
  103. _id: id of recipe,
  104. name: name of recipe,
  105. price: price of recipe,
  106. ingredients: [
  107. ingredient: id of ingredient,
  108. quantity: quantity of ingredient
  109. ]
  110. }
  111. */
  112. let updateOrders = (order, remove = false)=>{
  113. let isNew = true;
  114. for(let i = 0; i < orders.length; i++){
  115. if(orders[i]._id === order._id){
  116. if(remove){
  117. orders.splice(i, 1);
  118. }else{
  119. orders[i] = order;
  120. }
  121. isNew = false;
  122. }
  123. }
  124. if(isNew){
  125. orders.push(order);
  126. }
  127. ordersStrandObj.isPopulated = false;
  128. ordersStrandObj.display();
  129. closeSidebar();
  130. }
  131. //Close any open sidebar
  132. let closeSidebar = ()=>{
  133. let sidebar = document.querySelector("#sidebarDiv");
  134. for(let i = 0; i < sidebar.children.length; i++){
  135. sidebar.children[i].style.display = "none";
  136. }
  137. sidebar.classList = "sidebarHide";
  138. }
  139. /*
  140. Open a specific sidebar
  141. Input:
  142. sidebar: the outermost element of the sidebar (must contain class sidebar)
  143. */
  144. let openSidebar = (sidebar)=>{
  145. document.querySelector("#sidebarDiv").classList = "sidebar";
  146. let sideBars = document.querySelector("#sidebarDiv").children;
  147. for(let i = 0; i < sideBars.length; i++){
  148. sideBars[i].style.display = "none";
  149. }
  150. sidebar.style.display = "flex";
  151. }
  152. /*
  153. Gets the indices of two dates from transactions
  154. Inputs
  155. from: starting date
  156. to: ending date (default to now)
  157. Output
  158. Array containing starting index and ending index
  159. Note: Will return false if it cannot find both necessary dates
  160. */
  161. let dateIndices = (from, to = new Date())=>{
  162. let indices = [];
  163. for(let i = 0; i < transactions.length; i++){
  164. if(transactions[i].date > from){
  165. indices.push(i);
  166. break;
  167. }
  168. }
  169. for(let i = transactions.length - 1; i >=0; i--){
  170. if(transactions[i].date < to){
  171. indices.push(i);
  172. break;
  173. }
  174. }
  175. if(indices.length < 2){
  176. return false;
  177. }
  178. return indices;
  179. }
  180. /*
  181. Gets the quantity of each ingredient sold between two dates (dateRange)
  182. Inputs
  183. dateRange: list containing a start date and an end date
  184. Output
  185. List of objects
  186. id: id of specific ingredient
  187. quantity: quantity sold of that ingredient
  188. name: name of the ingredient
  189. */
  190. let ingredientsSold = (dateRange)=>{
  191. if(!dateRange){
  192. return false;
  193. }
  194. let recipes = recipesSold(dateRange);
  195. let ingredientList = [];
  196. for(let i = 0; i < recipes.length; i++){
  197. for(let j = 0; j < merchant.recipes.length; j++){
  198. for(let k = 0; k < merchant.recipes[j].ingredients.length; k++){
  199. let exists = false;
  200. for(let l = 0; l < ingredientList.length; l++){
  201. if(ingredientList[l].id === merchant.recipes[j].ingredients[k].ingredient._id){
  202. exists = true;
  203. ingredientList[l].quantity += merchant.recipes[j].ingredients[k].quantity * recipes[i].quantity;
  204. break;
  205. }
  206. }
  207. if(!exists){
  208. ingredientList.push({
  209. id: merchant.recipes[j].ingredients[k].ingredient._id,
  210. quantity: merchant.recipes[j].ingredients[k].quantity * recipes[i].quantity,
  211. name: merchant.recipes[j].ingredients[k].ingredient.name,
  212. unit: merchant.recipes[j].ingredients[k].ingredient.unit
  213. })
  214. }
  215. }
  216. }
  217. }
  218. return ingredientList;
  219. }
  220. /*
  221. Gets the quantity of a single ingredient sold between two dates (dateRange)
  222. Input:
  223. dateRange: array containing two elements, start and end indices
  224. id: id of the ingredient to calculate
  225. Return: (int) Quantity of recipes sold
  226. */
  227. let ingredientSold = (dateRange, id)=>{
  228. let recipes = recipesSold(dateRange);
  229. let total = 0;
  230. let checkRecipes = [];
  231. let quantities = [];
  232. for(let i = 0; i < merchant.recipes.length; i++){
  233. for(let j = 0; j < merchant.recipes[i].ingredients.length; j++){
  234. if(merchant.recipes[i].ingredients[j].ingredient._id === id){
  235. checkRecipes.push(merchant.recipes[i]._id);
  236. quantities.push(merchant.recipes[i].ingredients[j].quantity);
  237. break;
  238. }
  239. }
  240. }
  241. for(let i = 0; i < recipes.length; i++){
  242. for(let i = 0; i < checkRecipes.length; i++){
  243. if(checkRecipes[i] === recipes[i].id){
  244. total += recipes[i].quantity * quantities[i];
  245. break;
  246. }
  247. }
  248. }
  249. return total;
  250. }
  251. /*
  252. Gets the number of recipes sold between two dates (dateRange)
  253. Inputs:
  254. dateRange: array containing a start date and an end date
  255. Return:
  256. List of objects
  257. id: id of specific recipe
  258. quantity: quantity sold of that recipe
  259. */
  260. let recipesSold = (dateRange)=>{
  261. let recipeList = [];
  262. for(let i = dateRange[0]; i <= dateRange[1]; i++){
  263. for(let j = 0; j < transactions[i].recipes.length; j++){
  264. let exists = false;
  265. for(let k = 0; k < recipeList.length; k++){
  266. if(recipeList[k].id === transactions[i].recipes[j].recipe){
  267. exists = true;
  268. recipeList[k].quantity += transactions[i].recipes[j].quantity;
  269. break;
  270. }
  271. }
  272. if(!exists){
  273. recipeList.push({
  274. id: transactions[i].recipes[j].recipe,
  275. quantity: transactions[i].recipes[j].quantity
  276. })
  277. }
  278. }
  279. }
  280. return recipeList;
  281. }
  282. /*
  283. Groups all of the merchant's ingredients by their category
  284. Return:
  285. Array of objects
  286. name: Category name
  287. ingredients: Array of objects
  288. id: Id of ingredient
  289. name: Name of ingredient
  290. quantity: Merchant's quantity of this ingredient
  291. unit: Measurement unit
  292. */
  293. let categorizeIngredients = (ingredients)=>{
  294. let ingredientsByCategory = [];
  295. for(let i = 0; i < ingredients.length; i++){
  296. let categoryExists = false;
  297. for(let j = 0; j < ingredientsByCategory.length; j++){
  298. if(ingredients[i].ingredient.category === ingredientsByCategory[j].name){
  299. ingredientsByCategory[j].ingredients.push({
  300. id: ingredients[i].ingredient._id,
  301. name: ingredients[i].ingredient.name,
  302. quantity: ingredients[i].quantity,
  303. unit: ingredients[i].ingredient.unit
  304. });
  305. categoryExists = true;
  306. break;
  307. }
  308. }
  309. if(!categoryExists){
  310. ingredientsByCategory.push({
  311. name: ingredients[i].ingredient.category,
  312. ingredients: [{
  313. id: ingredients[i].ingredient._id,
  314. name: ingredients[i].ingredient.name,
  315. quantity: ingredients[i].quantity,
  316. unit: ingredients[i].ingredient.unit
  317. }]
  318. });
  319. }
  320. }
  321. return ingredientsByCategory;
  322. }
  323. let unitizeIngredients = (ingredients)=>{
  324. let ingredientsByUnit = [];
  325. for(let i = 0; i < ingredients.length; i++){
  326. let unitExists = false;
  327. for(let j = 0; j < ingredientsByUnit.length; j++){
  328. if(ingredients[i].ingredient.unit === ingredientsByUnit[j].name){
  329. ingredientsByUnit[j].ingredients.push({
  330. id: ingredients[i].ingredient._id,
  331. name: ingredients[i].ingredient.name,
  332. quantity: ingredients[i].quantity,
  333. unit: ingredients[i].ingredient.unit
  334. });
  335. unitExists = true;
  336. break;
  337. }
  338. }
  339. if(!unitExists){
  340. ingredientsByUnit.push({
  341. name: ingredients[i].ingredient.unit,
  342. ingredients: [{
  343. id: ingredients[i].ingredient._id,
  344. name: ingredients[i].ingredient.name,
  345. quantity: ingredients[i].quantity,
  346. unit: ingredients[i].ingredient.unit
  347. }]
  348. })
  349. }
  350. }
  351. return ingredientsByUnit;
  352. }
  353. let categorizeIngredientsFromDB = (ingredients)=>{
  354. let ingredientsByCategory = [];
  355. for(let i = 0; i < ingredients.length; i++){
  356. let categoryExists = false;
  357. for(let j = 0; j < ingredientsByCategory.length; j++){
  358. if(ingredients[i].category === ingredientsByCategory[j].name){
  359. ingredientsByCategory[j].ingredients.push({
  360. id: ingredients[i]._id,
  361. name: ingredients[i].name,
  362. unit: ingredients[i].unit
  363. });
  364. categoryExists = true;
  365. break;
  366. }
  367. }
  368. if(!categoryExists){
  369. ingredientsByCategory.push({
  370. name: ingredients[i].category,
  371. ingredients: [{
  372. id: ingredients[i]._id,
  373. name: ingredients[i].name,
  374. unit: ingredients[i].unit
  375. }]
  376. });
  377. }
  378. }
  379. return ingredientsByCategory;
  380. }
  381. let recipesForIngredient = (ingredientId)=>{
  382. let recipes = [];
  383. for(let i = 0; i < merchant.recipes.length; i++){
  384. for(let j = 0; j < merchant.recipes[i].ingredients.length; j++){
  385. if(merchant.recipes[i].ingredients[j].ingredient._id === ingredientId){
  386. recipes.push(merchant.recipes[i]);
  387. break;
  388. }
  389. }
  390. }
  391. return recipes;
  392. }
  393. for(let transaction of transactions){
  394. transaction.date = new Date(transaction.date);
  395. }
  396. homeStrandObj.display();