controller.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 a single ingredient sold between two dates (dateRange)
  162. Input:
  163. dateRange: array containing two elements, start and end indices
  164. id: id of the ingredient to calculate
  165. Return: (int) Quantity of recipes sold
  166. */
  167. let ingredientSold = (dateRange, id)=>{
  168. let recipes = recipesSold(dateRange);
  169. let total = 0;
  170. let checkRecipes = [];
  171. let quantities = [];
  172. for(let i = 0; i < merchant.recipes.length; i++){
  173. for(let j = 0; j < merchant.recipes[i].ingredients.length; j++){
  174. if(merchant.recipes[i].ingredients[j].ingredient._id === id){
  175. checkRecipes.push(merchant.recipes[i]._id);
  176. quantities.push(merchant.recipes[i].ingredients[j].quantity);
  177. break;
  178. }
  179. }
  180. }
  181. for(let i = 0; i < recipes.length; i++){
  182. for(let i = 0; i < checkRecipes.length; i++){
  183. if(checkRecipes[i] === recipes[i].id){
  184. total += recipes[i].quantity * quantities[i];
  185. break;
  186. }
  187. }
  188. }
  189. return total;
  190. }
  191. let unitizeIngredients = (ingredients)=>{
  192. let ingredientsByUnit = [];
  193. for(let i = 0; i < ingredients.length; i++){
  194. let unitExists = false;
  195. for(let j = 0; j < ingredientsByUnit.length; j++){
  196. if(ingredients[i].ingredient.unit === ingredientsByUnit[j].name){
  197. ingredientsByUnit[j].ingredients.push({
  198. id: ingredients[i].ingredient._id,
  199. name: ingredients[i].ingredient.name,
  200. quantity: ingredients[i].quantity,
  201. unit: ingredients[i].ingredient.unit
  202. });
  203. unitExists = true;
  204. break;
  205. }
  206. }
  207. if(!unitExists){
  208. ingredientsByUnit.push({
  209. name: ingredients[i].ingredient.unit,
  210. ingredients: [{
  211. id: ingredients[i].ingredient._id,
  212. name: ingredients[i].ingredient.name,
  213. quantity: ingredients[i].quantity,
  214. unit: ingredients[i].ingredient.unit
  215. }]
  216. })
  217. }
  218. }
  219. return ingredientsByUnit;
  220. }
  221. let categorizeIngredientsFromDB = (ingredients)=>{
  222. let ingredientsByCategory = [];
  223. for(let i = 0; i < ingredients.length; i++){
  224. let categoryExists = false;
  225. for(let j = 0; j < ingredientsByCategory.length; j++){
  226. if(ingredients[i].category === ingredientsByCategory[j].name){
  227. ingredientsByCategory[j].ingredients.push({
  228. id: ingredients[i]._id,
  229. name: ingredients[i].name,
  230. unit: ingredients[i].unit
  231. });
  232. categoryExists = true;
  233. break;
  234. }
  235. }
  236. if(!categoryExists){
  237. ingredientsByCategory.push({
  238. name: ingredients[i].category,
  239. ingredients: [{
  240. id: ingredients[i]._id,
  241. name: ingredients[i].name,
  242. unit: ingredients[i].unit
  243. }]
  244. });
  245. }
  246. }
  247. return ingredientsByCategory;
  248. }
  249. let recipesForIngredient = (ingredientId)=>{
  250. let recipes = [];
  251. for(let i = 0; i < merchant.recipes.length; i++){
  252. for(let j = 0; j < merchant.recipes[i].ingredients.length; j++){
  253. if(merchant.recipes[i].ingredients[j].ingredient._id === ingredientId){
  254. recipes.push(merchant.recipes[i]);
  255. break;
  256. }
  257. }
  258. }
  259. return recipes;
  260. }
  261. homeStrandObj.display();