controller.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. class Ingredient{
  2. constructor(id, name, category, unitType, unit, parent){
  3. this.id = id;
  4. this.name = name;
  5. this.category = category;
  6. this.unitType = unitType;
  7. this.unit = unit;
  8. this.parent = parent;
  9. }
  10. convert(quantity){
  11. if(this.unitType === "mass"){
  12. switch(this.unit){
  13. case "g": break;
  14. case "kg": quantity /= 1000; break;
  15. case "oz": quantity /= 28.3495; break;
  16. case "lb": quantity /= 453.5924; break;
  17. }
  18. }else if(this.unitType === "volume"){
  19. switch(this.unit){
  20. case "ml": quantity *= 1000; break;
  21. case "l": break;
  22. case "tsp": quantity *= 202.8842; break;
  23. case "tbsp": quantity *= 67.6278; break;
  24. case "ozfl": quantity *= 33.8141; break;
  25. case "cup": quantity *= 4.1667; break;
  26. case "pt": quantity *= 2.1134; break;
  27. case "qt": quantity *= 1.0567; break;
  28. case "gal": quantity /= 3.7854; break;
  29. }
  30. }else if(this.unitType === "length"){
  31. switch(this.unit){
  32. case "mm": quantity *= 1000; break;
  33. case "cm": quantity *= 100; break;
  34. case "m": break;
  35. case "in": quantity *= 39.3701; break;
  36. case "ft": quantity *= 3.2808; break;
  37. }
  38. }
  39. return quantity;
  40. }
  41. }
  42. class Recipe{
  43. constructor(id, name, price, ingredients, parent){
  44. this.id = id;
  45. this.name = name;
  46. this.price = price;
  47. this.parent = parent;
  48. this.ingredients = [];
  49. for(let i = 0; i < ingredients.length; i++){
  50. for(let j = 0; j < parent.ingredients.length; j++){
  51. if(ingredients[i].ingredient === parent.ingredients[j].ingredient.id){
  52. this.ingredients.push({
  53. ingredient: parent.ingredients[j].ingredient,
  54. quantity: ingredients[i].quantity
  55. });
  56. break;
  57. }
  58. }
  59. }
  60. }
  61. }
  62. class Transaction{
  63. constructor(id, date, recipes, parent){
  64. this.id = id;
  65. this.parent = parent;
  66. this.date = new Date(date);
  67. this.recipes = [];
  68. for(let i = 0; i < recipes.length; i++){
  69. for(let j = 0; j < parent.recipes.length; j++){
  70. if(recipes[i].recipe === parent.recipes[j].id){
  71. this.recipes.push({
  72. recipe: parent.recipes[j],
  73. quantity: recipes[i].quantity
  74. });
  75. break;
  76. }
  77. }
  78. }
  79. }
  80. }
  81. class Order{
  82. constructor(id, name, date, ingredients, parent){
  83. this.id = id;
  84. this.name = name;
  85. this.date = new Date(date);
  86. this.ingredients = [];
  87. this.parent = parent;
  88. for(let i = 0; i < ingredients.length; i++){
  89. for(let j = 0; j < parent.ingredients.length; j++){
  90. if(ingredients[i].ingredient === parent.ingredients[j].ingredient.id){
  91. this.ingredients.push({
  92. ingredient: parent.ingredients[j].ingredient,
  93. quantity: ingredients[i].quantity,
  94. price: ingredients[i].price
  95. });
  96. }
  97. }
  98. }
  99. }
  100. convertPrice(unitType, unit, price){
  101. if(unitType === "mass"){
  102. switch(unit){
  103. case "g": break;
  104. case "kg": price *= 1000; break;
  105. case "oz": price *= 28.3495; break;
  106. case "lb": price *= 453.5924; break;
  107. }
  108. }else if(unitType === "volume"){
  109. switch(unit){
  110. case "ml": price /= 1000; break;
  111. case "l": break;
  112. case "tsp": price /= 202.8842; break;
  113. case "tbsp": price /= 67.6278; break;
  114. case "ozfl": price /= 33.8141; break;
  115. case "cup": price /= 4.1667; break;
  116. case "pt": price /= 2.1134; break;
  117. case "qt": price /= 1.0567; break;
  118. case "gal": price *= 3.7854; break;
  119. }
  120. }else if(unitType === "length"){
  121. switch(unit){
  122. case "mm": price /= 1000; break;
  123. case "cm": price /= 100; break;
  124. case "m": break;
  125. case "in": price /= 39.3701; break;
  126. case "ft": price /= 3.2808; break;
  127. }
  128. }
  129. return price;
  130. }
  131. }
  132. class Merchant{
  133. constructor(oldMerchant, transactions){
  134. this.name = oldMerchant.name;
  135. this.pos = oldMerchant.pos;
  136. this.ingredients = [];
  137. this.recipes = [];
  138. this.transactions = [];
  139. this.orders = [];
  140. this.units = {
  141. mass: ["g", "kg", "oz", "lb"],
  142. volume: ["ml", "l", "tsp", "tbsp", "ozfl", "cup", "pt", "qt", "gal"],
  143. length: ["mm", "cm", "m", "in", "foot"]
  144. }
  145. for(let i = 0; i < oldMerchant.inventory.length; i++){
  146. this.ingredients.push({
  147. ingredient: new Ingredient(
  148. oldMerchant.inventory[i].ingredient._id,
  149. oldMerchant.inventory[i].ingredient.name,
  150. oldMerchant.inventory[i].ingredient.category,
  151. oldMerchant.inventory[i].ingredient.unitType,
  152. oldMerchant.inventory[i].defaultUnit,
  153. this
  154. ),
  155. quantity: oldMerchant.inventory[i].quantity
  156. });
  157. }
  158. for(let i = 0; i < oldMerchant.recipes.length; i++){
  159. this.recipes.push(new Recipe(
  160. oldMerchant.recipes[i]._id,
  161. oldMerchant.recipes[i].name,
  162. oldMerchant.recipes[i].price,
  163. oldMerchant.recipes[i].ingredients,
  164. this
  165. ));
  166. }
  167. for(let i = 0; i < transactions.length; i++){
  168. this.transactions.push(new Transaction(
  169. transactions[i]._id,
  170. transactions[i].date,
  171. transactions[i].recipes,
  172. this
  173. ));
  174. }
  175. }
  176. /*
  177. Updates all specified item in the merchant's inventory and updates the page
  178. If ingredient doesn't exist, add it
  179. ingredients = {
  180. ingredient: Ingredient object,
  181. quantity: new quantity,
  182. defaultUnit: the default unit to be displayed
  183. }
  184. remove = set true if removing
  185. isOrder = set true if this is coming from an order
  186. */
  187. editIngredients(ingredients, remove = false, isOrder = false){
  188. for(let i = 0; i < ingredients.length; i++){
  189. let isNew = true;
  190. for(let j = 0; j < merchant.ingredients.length; j++){
  191. if(merchant.ingredients[j].ingredient === ingredients[i].ingredient){
  192. if(remove){
  193. merchant.ingredients.splice(j, 1);
  194. }else if(!remove && isOrder){
  195. merchant.ingredients[j].quantity += ingredients[i].quantity;
  196. }else{
  197. merchant.ingredients[j].quantity = ingredients[i].quantity;
  198. }
  199. isNew = false;
  200. break;
  201. }
  202. }
  203. if(isNew){
  204. merchant.ingredients.push({
  205. ingredient: ingredients[i].ingredient,
  206. quantity: parseFloat(ingredients[i].quantity),
  207. defaultUnit: ingredients[i].defaultUnit
  208. });
  209. }
  210. }
  211. homeStrandObj.drawInventoryCheckCard();
  212. ingredientsStrandObj.populateByProperty("category");
  213. addIngredientsComp.isPopulated = false;
  214. closeSidebar();
  215. }
  216. /*
  217. Updates a recipe in the merchants list of recipes
  218. Can create, edit or remove
  219. recipe = [Recipe object]
  220. remove = will remove recipe when true
  221. */
  222. editRecipes(recipes, remove = false){
  223. let isNew = true;
  224. for(let i = 0; i < recipes.length; i++){
  225. for(let j = 0; j < this.recipes.length; j++){
  226. if(recipes[i] === this.recipes[j]){
  227. if(remove){
  228. this.recipes.splice(j, 1);
  229. }else{
  230. this.recipes[j] = recipes[i];
  231. }
  232. isNew = false;
  233. break;
  234. }
  235. }
  236. if(isNew){
  237. merchant.recipes.push(recipes[i]);
  238. }
  239. }
  240. transactionsStrandObj.isPopulated = false;
  241. recipeBookStrandObj.populateRecipes();
  242. closeSidebar();
  243. }
  244. /*
  245. Updates a list of orders in the merchants list of orders
  246. Create/edit/remove
  247. orders = [Order object]
  248. remove = will remove order when true
  249. */
  250. editOrders(orders, remove = false){
  251. for(let i = 0; i < orders.length; i++){
  252. let isNew = true;
  253. for(let j = 0; j < this.orders.length; j++){
  254. if(orders[i] === this.orders[j]){
  255. if(remove){
  256. this.orders.splice(j, 1);
  257. }else{
  258. this.orders[j] = orders[i];
  259. }
  260. isNew = false;
  261. break;
  262. }
  263. }
  264. if(isNew){
  265. this.orders.push(orders[i]);
  266. }
  267. }
  268. ordersStrandObj.populate();
  269. closeSidebar();
  270. }
  271. editTransactions(transaction, remove = false){
  272. let isNew = true;
  273. for(let i = 0; i < this.transactions.length; i++){
  274. if(this.transactions[i] === transaction){
  275. if(remove){
  276. this.transactions.splice(i, 1);
  277. }
  278. isNew = false;
  279. break;
  280. }
  281. }
  282. if(isNew){
  283. this.transactions.push(transaction);
  284. this.transactions.sort((a, b) => a.date > b.date ? 1 : -1);
  285. }
  286. transactionsStrandObj.isPopulated = false;
  287. transactionsStrandObj.display();
  288. closeSidebar();
  289. }
  290. /*
  291. Gets the indices of two dates from transactions
  292. Inputs
  293. from: starting date
  294. to: ending date (default to now)
  295. Output
  296. Array containing starting index and ending index
  297. Note: Will return false if it cannot find both necessary dates
  298. */
  299. transactionIndices(from, to = new Date()){
  300. let indices = [];
  301. for(let i = 0; i < this.transactions.length; i++){
  302. if(this.transactions[i].date > from){
  303. indices.push(i);
  304. break;
  305. }
  306. }
  307. for(let i = this.transactions.length - 1; i >=0; i--){
  308. if(this.transactions[i].date < to){
  309. indices.push(i);
  310. break;
  311. }
  312. }
  313. if(indices.length < 2){
  314. return false;
  315. }
  316. return indices;
  317. }
  318. revenue(indices){
  319. let total = 0;
  320. for(let i = indices[0]; i <= indices[1]; i++){
  321. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  322. for(let k = 0; k < this.recipes.length; k++){
  323. if(this.transactions[i].recipes[j].recipe === this.recipes[k]){
  324. total += this.transactions[i].recipes[j].quantity * this.recipes[k].price;
  325. }
  326. }
  327. }
  328. }
  329. return total / 100;
  330. }
  331. /*
  332. Gets the quantity of each ingredient sold between two dates (dateRange)
  333. Inputs
  334. dateRange: list containing a start date and an end date
  335. Return:
  336. [{
  337. ingredient: Ingredient object,
  338. quantity: quantity of ingredient sold
  339. }]
  340. */
  341. ingredientsSold(dateRange){
  342. if(!dateRange){
  343. return false;
  344. }
  345. let recipes = this.recipesSold(dateRange);
  346. let ingredientList = [];
  347. for(let i = 0; i < recipes.length; i++){
  348. for(let j = 0; j < recipes[i].recipe.ingredients.length; j++){
  349. let exists = false;
  350. for(let k = 0; k < ingredientList.length; k++){
  351. if(ingredientList[k].ingredient === recipes[i].recipe.ingredients[j].ingredient){
  352. exists = true;
  353. ingredientList[k].quantity += recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity;
  354. break;
  355. }
  356. }
  357. if(!exists){
  358. ingredientList.push({
  359. ingredient: recipes[i].recipe.ingredients[j].ingredient,
  360. quantity: recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity
  361. });
  362. }
  363. }
  364. }
  365. return ingredientList;
  366. }
  367. singleIngredientSold(dateRange, ingredient){
  368. let total = 0;
  369. for(let i = dateRange[0]; i < dateRange[1]; i++){
  370. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  371. for(let k = 0; k < this.transactions[i].recipes[j].recipe.ingredients.length; k++){
  372. if(this.transactions[i].recipes[j].recipe.ingredients[k].ingredient === ingredient.ingredient){
  373. total += this.transactions[i].recipes[j].recipe.ingredients[k].quantity;
  374. break;
  375. }
  376. }
  377. }
  378. }
  379. return total;
  380. }
  381. /*
  382. Gets the number of recipes sold between two dates (dateRange)
  383. Inputs:
  384. dateRange: array containing a start date and an end date
  385. Return:
  386. [{
  387. recipe: a recipe object
  388. quantity: quantity of the recipe sold
  389. }]
  390. */
  391. recipesSold(dateRange){
  392. let recipeList = [];
  393. for(let i = dateRange[0]; i <= dateRange[1]; i++){
  394. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  395. let exists = false;
  396. for(let k = 0; k < recipeList.length; k++){
  397. if(recipeList[k].recipe === this.transactions[i].recipes[j].recipe){
  398. exists = true;
  399. recipeList[k].quantity += this.transactions[i].recipes[j].quantity;
  400. break;
  401. }
  402. }
  403. if(!exists){
  404. recipeList.push({
  405. recipe: this.transactions[i].recipes[j].recipe,
  406. quantity: this.transactions[i].recipes[j].quantity
  407. });
  408. }
  409. }
  410. }
  411. return recipeList;
  412. }
  413. /*
  414. Create revenue data for graphing
  415. Input:
  416. dateRange: [start index, end index] (this.transactionIndices)
  417. Return:
  418. [total revenue for each day]
  419. */
  420. graphDailyRevenue(dateRange){
  421. if(!dateRange){
  422. return false;
  423. }
  424. let dataList = new Array(30).fill(0);
  425. let currentDate = this.transactions[dateRange[0]].date;
  426. let arrayIndex = 0;
  427. for(let i = dateRange[0]; i <= dateRange[1]; i++){
  428. if(this.transactions[i].date.getDate() !== currentDate.getDate()){
  429. currentDate = this.transactions[i].date;
  430. arrayIndex++;
  431. }
  432. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  433. dataList[arrayIndex] += (this.transactions[i].recipes[j].recipe.price / 100) * this.transactions[i].recipes[j].quantity;
  434. }
  435. }
  436. return dataList;
  437. }
  438. /*
  439. Groups all of the merchant's ingredients by their category
  440. Return: [{
  441. name: category name,
  442. ingredients: [Ingredient Object]
  443. }]
  444. */
  445. categorizeIngredients(){
  446. let ingredientsByCategory = [];
  447. for(let i = 0; i < this.ingredients.length; i++){
  448. let categoryExists = false;
  449. for(let j = 0; j < ingredientsByCategory.length; j++){
  450. if(this.ingredients[i].ingredient.category === ingredientsByCategory[j].name){
  451. ingredientsByCategory[j].ingredients.push(this.ingredients[i]);
  452. categoryExists = true;
  453. break;
  454. }
  455. }
  456. if(!categoryExists){
  457. ingredientsByCategory.push({
  458. name: this.ingredients[i].ingredient.category,
  459. ingredients: [this.ingredients[i]]
  460. });
  461. }
  462. }
  463. return ingredientsByCategory;
  464. }
  465. unitizeIngredients(){
  466. let ingredientsByUnit = [];
  467. for(let i = 0; i < this.ingredients.length; i++){
  468. let unitExists = false;
  469. for(let j = 0; j < ingredientsByUnit.length; j++){
  470. if(this.ingredients[i].ingredient.unit === ingredientsByUnit[j].name){
  471. ingredientsByUnit[j].ingredients.push(this.ingredients[i]);
  472. unitExists = true;
  473. break;
  474. }
  475. }
  476. if(!unitExists){
  477. ingredientsByUnit.push({
  478. name: this.ingredients[i].ingredient.unit,
  479. ingredients: [this.ingredients[i]]
  480. });
  481. }
  482. }
  483. return ingredientsByUnit;
  484. }
  485. getRecipesForIngredient(ingredient){
  486. let recipes = [];
  487. for(let i = 0; i < this.recipes.length; i++){
  488. for(let j = 0; j < this.recipes[i].ingredients.length; j++){
  489. if(this.recipes[i].ingredients[j].ingredient === ingredient){
  490. recipes.push(this.recipes[i]);
  491. }
  492. }
  493. }
  494. return recipes;
  495. }
  496. }
  497. let convertToMain = (unit, quantity)=>{
  498. let converted = 0;
  499. if(merchant.units.mass.includes(unit)){
  500. switch(unit){
  501. case "g": converted = quantity; break;
  502. case "kg": converted = quantity * 1000; break;
  503. case "oz": converted = quantity * 28.3495; break;
  504. case "lb": converted = quantity * 453.5924; break;
  505. }
  506. }else if(merchant.units.volume.includes(unit)){
  507. switch(unit){
  508. case "ml": converted = quantity / 1000; break;
  509. case "l": converted = quantity; break;
  510. case "tsp": converted = quantity / 202.8842; break;
  511. case "tbsp": converted = quantity / 67.6278; break;
  512. case "ozfl": converted = quantity / 33.8141; break;
  513. case "cup": converted = quantity / 4.1667; break;
  514. case "pt": converted = quantity / 2.1134; break;
  515. case "qt": converted = quantity / 1.0567; break;
  516. case "gal": converted = quantity * 3.7854; break;
  517. }
  518. }else if(merchant.units.length.includes(unit)){
  519. switch(unit){
  520. case "mm": converted = quantity / 1000; break;
  521. case "cm": converted = quantity / 100; break;
  522. case "m": converted = quantity; break;
  523. case "in": converted = quantity / 39.3701; break;
  524. case "ft": converted = quantity / 3.2808; break;
  525. }
  526. }else{
  527. converted = quantity;
  528. }
  529. return converted;
  530. }
  531. /*
  532. Switches to a different strand
  533. Input:
  534. name: name of the strand. Must end with "Strand"
  535. */
  536. let changeStrand = (name)=>{
  537. closeSidebar();
  538. for(let strand of document.querySelectorAll(".strand")){
  539. strand.style.display = "none";
  540. }
  541. let buttons = document.querySelectorAll(".menuButton");
  542. for(let i = 0; i < buttons.length - 1; i++){
  543. buttons[i].classList = "menuButton";
  544. buttons[i].onclick = ()=>{changeStrand(`${buttons[i].id.slice(0, buttons[i].id.indexOf("Btn"))}Strand`)};
  545. }
  546. let activeButton = document.querySelector(`#${name.slice(0, name.indexOf("Strand"))}Btn`);
  547. activeButton.classList = "menuButton active";
  548. activeButton.onclick = undefined;
  549. document.querySelector(`#${name}`).style.display = "flex";
  550. window[`${name}Obj`].display();
  551. if(window.screen.availWidth <= 1000){
  552. closeMenu();
  553. }
  554. }
  555. //Close any open sidebar
  556. let closeSidebar = ()=>{
  557. let sidebar = document.querySelector("#sidebarDiv");
  558. for(let i = 0; i < sidebar.children.length; i++){
  559. sidebar.children[i].style.display = "none";
  560. }
  561. sidebar.classList = "sidebarHide";
  562. if(window.screen.availWidth <= 1000){
  563. document.querySelector(".contentBlock").style.display = "flex";
  564. document.getElementById("mobileMenuSelector").style.display = "block";
  565. document.getElementById("sidebarCloser").style.display = "none";
  566. }
  567. }
  568. /*
  569. Open a specific sidebar
  570. Input:
  571. sidebar: the outermost element of the sidebar (must contain class sidebar)
  572. */
  573. let openSidebar = (sidebar)=>{
  574. document.querySelector("#sidebarDiv").classList = "sidebar";
  575. let sideBars = document.querySelector("#sidebarDiv").children;
  576. for(let i = 0; i < sideBars.length; i++){
  577. sideBars[i].style.display = "none";
  578. }
  579. sidebar.style.display = "flex";
  580. if(window.screen.availWidth <= 1000){
  581. document.querySelector(".contentBlock").style.display = "none";
  582. document.getElementById("mobileMenuSelector").style.display = "none";
  583. document.getElementById("sidebarCloser").style.display = "block";
  584. }
  585. }
  586. let changeMenu = ()=>{
  587. let menu = document.querySelector(".menu");
  588. let buttons = document.querySelectorAll(".menuButton");
  589. if(!menu.classList.contains("menuMinimized")){
  590. menu.classList = "menu menuMinimized";
  591. for(let button of buttons){
  592. button.children[1].style.display = "none";
  593. }
  594. document.querySelector("#max").style.display = "none";
  595. document.querySelector("#min").style.display = "flex";
  596. }else if(menu.classList.contains("menuMinimized")){
  597. menu.classList = "menu";
  598. for(let button of buttons){
  599. button.children[1].style.display = "block";
  600. }
  601. setTimeout(()=>{
  602. document.querySelector("#max").style.display = "flex";
  603. document.querySelector("#min").style.display = "none";
  604. }, 150);
  605. }
  606. }
  607. let openMenu = ()=>{
  608. document.getElementById("menu").style.display = "flex";
  609. document.querySelector(".contentBlock").style.display = "none";
  610. document.getElementById("mobileMenuSelector").onclick = ()=>{closeMenu()};
  611. }
  612. let closeMenu = ()=>{
  613. document.getElementById("menu").style.display = "none";
  614. document.querySelector(".contentBlock").style.display = "flex";
  615. document.getElementById("mobileMenuSelector").onclick = ()=>{openMenu()};
  616. }
  617. if(window.screen.availWidth > 1000 && window.screen.availWidth <= 1400){
  618. changeMenu();
  619. document.getElementById("menuShifter2").style.display = "none";
  620. }