controller.js 9.3 KB

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