controller.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. quantityChange: change in 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. console.log(ingredients);
  35. for(let i = 0; i < ingredients.length; i++){
  36. let isNew = true;
  37. for(let j = 0; j < merchant.inventory.length; j++){
  38. if(merchant.inventory[j].ingredient._id === ingredients[i].id){
  39. if(remove){
  40. merchant.inventory.splice(j, 1);
  41. }else{
  42. merchant.inventory[j].quantity += ingredients[i].quantity;
  43. }
  44. isNew = false;
  45. break;
  46. }
  47. }
  48. if(isNew){
  49. merchant.inventory.push(ingredients[i]);
  50. }
  51. }
  52. homeStrandObj.drawInventoryCheckCard();
  53. ingredientsStrandObj.populateIngredients();
  54. addIngredientsComp.isPopulated = false;
  55. closeSidebar();
  56. }
  57. /*
  58. Updates a recipe in the merchants list of recipes
  59. Can create, edit or remove
  60. Inputs:
  61. recipe: object
  62. _id: id of recipe
  63. name: name of recipe
  64. price: price of recipe
  65. ingredients: list of ingredients
  66. ingredient: id of ingredient
  67. quantity: quantity of ingredient
  68. remove: if true, remove ingredient from inventory
  69. */
  70. let updateRecipes = (recipe, remove = false)=>{
  71. let isNew = true;
  72. let index = 0;
  73. for(let i = 0; i < recipe.ingredients.length; i++){
  74. for(let j = 0; j < merchant.inventory.length; j++){
  75. if(merchant.inventory[j].ingredient._id === recipe.ingredients[i].ingredient){
  76. recipe.ingredients[i].ingredient = merchant.inventory[j].ingredient;
  77. break;
  78. }
  79. }
  80. }
  81. for(let i = 0; i < merchant.recipes.length; i++){
  82. if(recipe._id === merchant.recipes[i]._id){
  83. if(remove){
  84. merchant.recipes.splice(i, 1);
  85. }else{
  86. merchant.recipes[i] = recipe;
  87. index = i;
  88. }
  89. isNew = false;
  90. break;
  91. }
  92. }
  93. if(isNew){
  94. merchant.recipes.push(recipe);
  95. index = merchant.recipes.length - 1;
  96. }
  97. recipeBookStrandObj.populateRecipes();
  98. closeSidebar();
  99. }
  100. /*
  101. Updates an order in the front end
  102. Can create, edit or remove
  103. Inputs:
  104. recipe: object
  105. _id: id of recipe
  106. name: name of recipe
  107. price: price of recipe
  108. ingredients: list of ingredients
  109. ingredient: id of ingredient
  110. quantity: quantity of ingredient
  111. remove: if true, remove ingredient from inventory
  112. */
  113. let updateOrders = (order, remove = false)=>{
  114. let isNew = true;
  115. for(let i = 0; i < orders.length; i++){
  116. if(orders[i]._id === order._id){
  117. if(remove){
  118. orders.splice(i, 1);
  119. }else{
  120. orders[i] = order;
  121. }
  122. isNew = false;
  123. }
  124. }
  125. if(isNew){
  126. orders.push(order);
  127. }
  128. ordersStrandObj.isPopulated = false;
  129. ordersStrandObj.display();
  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 categorizeIngredientsFromDB = (ingredients)=>{
  324. let ingredientsByCategory = [];
  325. for(let i = 0; i < ingredients.length; i++){
  326. let categoryExists = false;
  327. for(let j = 0; j < ingredientsByCategory.length; j++){
  328. if(ingredients[i].category === ingredientsByCategory[j].name){
  329. ingredientsByCategory[j].ingredients.push({
  330. id: ingredients[i]._id,
  331. name: ingredients[i].name,
  332. unit: ingredients[i].unit
  333. });
  334. categoryExists = true;
  335. break;
  336. }
  337. }
  338. if(!categoryExists){
  339. ingredientsByCategory.push({
  340. name: ingredients[i].category,
  341. ingredients: [{
  342. id: ingredients[i]._id,
  343. name: ingredients[i].name,
  344. unit: ingredients[i].unit
  345. }]
  346. });
  347. }
  348. }
  349. return ingredientsByCategory;
  350. }
  351. for(let transaction of transactions){
  352. transaction.date = new Date(transaction.date);
  353. }
  354. homeStrandObj.display();