controller.js 12 KB

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