Merchant.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. class Ingredient{
  2. constructor(id, name, category, unit){
  3. this.id = id;
  4. this.name = name;
  5. this.category = category;
  6. this.unit = unit;
  7. }
  8. }
  9. class Recipe{
  10. constructor(id, name, price, ingredients, parent){
  11. this.id = id;
  12. this.name = name;
  13. this.price = price;
  14. this.parent = parent;
  15. this.ingredients = [];
  16. for(let i = 0; i < ingredients.length; i++){
  17. for(let j = 0; j < parent.ingredients.length; j++){
  18. if(ingredients[i].ingredient === parent.ingredients[j].ingredient.id){
  19. this.ingredients.push({
  20. ingredient: parent.ingredients[j].ingredient,
  21. quantity: ingredients[i].quantity
  22. });
  23. break;
  24. }
  25. }
  26. }
  27. }
  28. }
  29. class Transaction{
  30. constructor(id, date, recipes, parent){
  31. this.id = id;
  32. this.parent = parent;
  33. this.date = new Date(date);
  34. this.recipes = [];
  35. for(let i = 0; i < recipes.length; i++){
  36. for(let j = 0; j < parent.recipes.length; j++){
  37. if(recipes[i].recipe === parent.recipes[j].id){
  38. this.recipes.push({
  39. recipe: parent.recipes[j],
  40. quantity: recipes[i].quantity
  41. });
  42. break;
  43. }
  44. }
  45. }
  46. }
  47. }
  48. class Order{
  49. constructor(id, name, date, ingredients, parent){
  50. this.id = id;
  51. this.name = name;
  52. this.date = new Date(date);
  53. this.ingredients = [];
  54. this.parent = parent;
  55. for(let i = 0; i < ingredients.length; i++){
  56. for(let j = 0; j < parent.ingredients.length; j++){
  57. if(ingredients[i].ingredient === parent.ingredients[j].ingredient.id){
  58. this.ingredients.push({
  59. ingredient: parent.ingredients[j].ingredient,
  60. quantity: ingredients[i].quantity,
  61. price: ingredients[i].price
  62. });
  63. }
  64. }
  65. }
  66. }
  67. }
  68. class Merchant{
  69. constructor(oldMerchant, transactions){
  70. this.name = oldMerchant.name;
  71. this.pos = oldMerchant.pos;
  72. this.ingredients = [];
  73. this.recipes = [];
  74. this.transactions = [];
  75. this.orders = [];
  76. for(let i = 0; i < oldMerchant.inventory.length; i++){
  77. this.ingredients.push({
  78. ingredient: new Ingredient(
  79. oldMerchant.inventory[i].ingredient._id,
  80. oldMerchant.inventory[i].ingredient.name,
  81. oldMerchant.inventory[i].ingredient.category,
  82. oldMerchant.inventory[i].ingredient.unit,
  83. ),
  84. quantity: oldMerchant.inventory[i].quantity
  85. });
  86. }
  87. for(let i = 0; i < oldMerchant.recipes.length; i++){
  88. this.recipes.push(new Recipe(
  89. oldMerchant.recipes[i]._id,
  90. oldMerchant.recipes[i].name,
  91. oldMerchant.recipes[i].price,
  92. oldMerchant.recipes[i].ingredients,
  93. this
  94. ));
  95. }
  96. for(let i = 0; i < transactions.length; i++){
  97. this.transactions.push(new Transaction(
  98. transactions[i]._id,
  99. transactions[i].date,
  100. transactions[i].recipes,
  101. this
  102. ));
  103. }
  104. }
  105. /*
  106. Updates all specified item in the merchant's inventory and updates the page
  107. If ingredient doesn't exist, add it
  108. ingredients = {
  109. ingredient: Ingredient object,
  110. quantity: new quantity
  111. }
  112. remove = set true if removing
  113. isOrder = set true if this is coming from an order
  114. */
  115. editIngredients(ingredients, remove = false, isOrder = false){
  116. for(let i = 0; i < ingredients.length; i++){
  117. let isNew = true;
  118. for(let j = 0; j < merchant.ingredients.length; j++){
  119. if(merchant.ingredients[j].ingredient === ingredients[i].ingredient){
  120. if(remove){
  121. merchant.ingredients.splice(j, 1);
  122. }else if(!remove && isOrder){
  123. merchant.ingredients[j].quantity += ingredients[i].quantity;
  124. }else{
  125. merchant.ingredients[j].quantity = ingredients[i].quantity;
  126. }
  127. isNew = false;
  128. break;
  129. }
  130. }
  131. if(isNew){
  132. merchant.ingredients.push({
  133. ingredient: ingredients[i].ingredient,
  134. quantity: parseFloat(ingredients[i].quantity)
  135. });
  136. }
  137. }
  138. homeStrandObj.drawInventoryCheckCard();
  139. ingredientsStrandObj.populateByProperty("category");
  140. addIngredientsComp.isPopulated = false;
  141. closeSidebar();
  142. }
  143. /*
  144. Updates a recipe in the merchants list of recipes
  145. Can create, edit or remove
  146. recipe = [Recipe object]
  147. remove = will remove recipe when true
  148. */
  149. editRecipes(recipes, remove = false){
  150. let isNew = true;
  151. for(let i = 0; i < recipes.length; i++){
  152. for(let j = 0; j < this.recipes.length; j++){
  153. if(recipes[i] === this.recipes[j]){
  154. if(remove){
  155. this.recipes.splice(j, 1);
  156. }else{
  157. this.recipes[j] = recipes[i];
  158. }
  159. isNew = false;
  160. break;
  161. }
  162. }
  163. if(isNew){
  164. merchant.recipes.push(recipes[i]);
  165. }
  166. }
  167. recipeBookStrandObj.populateRecipes();
  168. closeSidebar();
  169. }
  170. /*
  171. Updates a list of orders in the merchants list of orders
  172. Create/edit/remove
  173. orders = [Order object]
  174. remove = will remove order when true
  175. */
  176. editOrders(orders, remove = false){
  177. for(let i = 0; i < orders.length; i++){
  178. let isNew = true;
  179. for(let j = 0; j < this.orders.length; j++){
  180. if(orders[i] === this.orders[j]){
  181. if(remove){
  182. this.orders.splice(j, 1);
  183. }else{
  184. this.orders[j] = orders[i];
  185. }
  186. isNew = false;
  187. break;
  188. }
  189. }
  190. if(isNew){
  191. this.orders.push(orders[i]);
  192. }
  193. }
  194. ordersStrandObj.populate();
  195. closeSidebar();
  196. }
  197. editTransactions(transaction, remove = false){
  198. let isNew = true;
  199. for(let i = 0; i < this.transactions.length; i++){
  200. if(this.transactions[i] === transaction){
  201. if(remove){
  202. this.transactions.splice(i, 1);
  203. }
  204. isNew = false;
  205. break;
  206. }
  207. }
  208. if(isNew){
  209. this.transactions.push(transaction);
  210. this.transactions.sort((a, b) => a.date > b.date ? 1 : -1);
  211. }
  212. transactionsStrandObj.isPopulated = false;
  213. transactionsStrandObj.display();
  214. closeSidebar();
  215. }
  216. /*
  217. Gets the indices of two dates from transactions
  218. Inputs
  219. from: starting date
  220. to: ending date (default to now)
  221. Output
  222. Array containing starting index and ending index
  223. Note: Will return false if it cannot find both necessary dates
  224. */
  225. transactionIndices(from, to = new Date()){
  226. let indices = [];
  227. for(let i = 0; i < this.transactions.length; i++){
  228. if(this.transactions[i].date > from){
  229. indices.push(i);
  230. break;
  231. }
  232. }
  233. for(let i = this.transactions.length - 1; i >=0; i--){
  234. if(this.transactions[i].date < to){
  235. indices.push(i);
  236. break;
  237. }
  238. }
  239. if(indices.length < 2){
  240. return false;
  241. }
  242. return indices;
  243. }
  244. revenue(indices){
  245. let total = 0;
  246. for(let i = indices[0]; i <= indices[1]; i++){
  247. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  248. for(let k = 0; k < this.recipes.length; k++){
  249. if(this.transactions[i].recipes[j].recipe === this.recipes[k]){
  250. total += this.transactions[i].recipes[j].quantity * this.recipes[k].price;
  251. }
  252. }
  253. }
  254. }
  255. return total / 100;
  256. }
  257. /*
  258. Gets the quantity of each ingredient sold between two dates (dateRange)
  259. Inputs
  260. dateRange: list containing a start date and an end date
  261. Return:
  262. [{
  263. ingredient: Ingredient object,
  264. quantity: quantity of ingredient sold
  265. }]
  266. */
  267. ingredientsSold(dateRange){
  268. if(!dateRange){
  269. return false;
  270. }
  271. let recipes = this.recipesSold(dateRange);
  272. let ingredientList = [];
  273. for(let i = 0; i < recipes.length; i++){
  274. for(let j = 0; j < recipes[i].recipe.ingredients.length; j++){
  275. let exists = false;
  276. for(let k = 0; k < ingredientList.length; k++){
  277. if(ingredientList[k].ingredient === recipes[i].recipe.ingredients[j].ingredient){
  278. exists = true;
  279. ingredientList[k].quantity += recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity;
  280. break;
  281. }
  282. }
  283. if(!exists){
  284. ingredientList.push({
  285. ingredient: recipes[i].recipe.ingredients[j].ingredient,
  286. quantity: recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity
  287. });
  288. }
  289. }
  290. }
  291. return ingredientList;
  292. }
  293. singleIngredientSold(dateRange, ingredient){
  294. let total = 0;
  295. for(let i = dateRange[0]; i < dateRange[1]; i++){
  296. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  297. for(let k = 0; k < this.transactions[i].recipes[j].recipe.ingredients.length; k++){
  298. if(this.transactions[i].recipes[j].recipe.ingredients[k].ingredient === ingredient.ingredient){
  299. total += this.transactions[i].recipes[j].recipe.ingredients[k].quantity;
  300. break;
  301. }
  302. }
  303. }
  304. }
  305. return total;
  306. }
  307. /*
  308. Gets the number of recipes sold between two dates (dateRange)
  309. Inputs:
  310. dateRange: array containing a start date and an end date
  311. Return:
  312. [{
  313. recipe: a recipe object
  314. quantity: quantity of the recipe sold
  315. }]
  316. */
  317. recipesSold(dateRange){
  318. let recipeList = [];
  319. for(let i = dateRange[0]; i <= dateRange[1]; i++){
  320. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  321. let exists = false;
  322. for(let k = 0; k < recipeList.length; k++){
  323. if(recipeList[k].recipe === this.transactions[i].recipes[j].recipe){
  324. exists = true;
  325. recipeList[k].quantity += this.transactions[i].recipes[j].quantity;
  326. break;
  327. }
  328. }
  329. if(!exists){
  330. recipeList.push({
  331. recipe: this.transactions[i].recipes[j].recipe,
  332. quantity: this.transactions[i].recipes[j].quantity
  333. });
  334. }
  335. }
  336. }
  337. return recipeList;
  338. }
  339. /*
  340. Create revenue data for graphing
  341. Input:
  342. dateRange: [start index, end index] (this.transactionIndices)
  343. Return:
  344. [total revenue for each day]
  345. */
  346. graphDailyRevenue(dateRange){
  347. if(!dateRange){
  348. return false;
  349. }
  350. let dataList = new Array(30).fill(0);
  351. let currentDate = this.transactions[dateRange[0]].date;
  352. let arrayIndex = 0;
  353. for(let i = dateRange[0]; i <= dateRange[1]; i++){
  354. if(this.transactions[i].date.getDate() !== currentDate.getDate()){
  355. currentDate = this.transactions[i].date;
  356. arrayIndex++;
  357. }
  358. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  359. dataList[arrayIndex] += (this.transactions[i].recipes[j].recipe.price / 100) * this.transactions[i].recipes[j].quantity;
  360. }
  361. }
  362. return dataList;
  363. }
  364. /*
  365. Groups all of the merchant's ingredients by their category
  366. Return: [{
  367. name: category name,
  368. ingredients: [Ingredient Object]
  369. }]
  370. */
  371. categorizeIngredients(){
  372. let ingredientsByCategory = [];
  373. for(let i = 0; i < this.ingredients.length; i++){
  374. let categoryExists = false;
  375. for(let j = 0; j < ingredientsByCategory.length; j++){
  376. if(this.ingredients[i].ingredient.category === ingredientsByCategory[j].name){
  377. ingredientsByCategory[j].ingredients.push(this.ingredients[i]);
  378. categoryExists = true;
  379. break;
  380. }
  381. }
  382. if(!categoryExists){
  383. ingredientsByCategory.push({
  384. name: this.ingredients[i].ingredient.category,
  385. ingredients: [this.ingredients[i]]
  386. });
  387. }
  388. }
  389. return ingredientsByCategory;
  390. }
  391. unitizeIngredients(){
  392. let ingredientsByUnit = [];
  393. for(let i = 0; i < this.ingredients.length; i++){
  394. let unitExists = false;
  395. for(let j = 0; j < ingredientsByUnit.length; j++){
  396. if(this.ingredients[i].ingredient.unit === ingredientsByUnit[j].name){
  397. ingredientsByUnit[j].ingredients.push(this.ingredients[i]);
  398. unitExists = true;
  399. break;
  400. }
  401. }
  402. if(!unitExists){
  403. ingredientsByUnit.push({
  404. name: this.ingredients[i].ingredient.unit,
  405. ingredients: [this.ingredients[i]]
  406. });
  407. }
  408. }
  409. return ingredientsByUnit;
  410. }
  411. getRecipesForIngredient(ingredient){
  412. let recipes = [];
  413. for(let i = 0; i < this.recipes.length; i++){
  414. for(let j = 0; j < this.recipes[i].ingredients.length; j++){
  415. if(this.recipes[i].ingredients[j].ingredient === ingredient){
  416. recipes.push(this.recipes[i]);
  417. }
  418. }
  419. }
  420. return recipes;
  421. }
  422. }
  423. let convertMass = (quantity, from, to)=>{
  424. //change to g
  425. let converted = 0;
  426. switch(from){
  427. case "g":
  428. converted = quantity;
  429. break;
  430. case "kg":
  431. converted = quantity * 1000;
  432. break;
  433. case "oz":
  434. converted = quantity * 28.3495;
  435. break;
  436. case "lb":
  437. converted = quantity * 453.5924;
  438. break;
  439. }
  440. //change to end
  441. switch(to){
  442. case "g":
  443. break;
  444. case "kg":
  445. converted = converted / 1000;
  446. break;
  447. case "oz":
  448. converted = converted / 28.3495;
  449. break;
  450. case "lb":
  451. converted = converted / 453.5924;
  452. break;
  453. }
  454. return converted;
  455. }
  456. let convertVolume = (quantity, from, to)=>{
  457. //change to l
  458. let converted = 0;
  459. switch(from){
  460. case "ml": converted = quantity / 1000; break;
  461. case "l": converted = quantity; break;
  462. case "tsp": converted = quantity / 202.8842; break;
  463. case "tbsp": converted = quantity / 67.6278; break;
  464. case "ozfl": converted = quantity / 33.8141; break;
  465. case "cup": converted = quantity / 4.1667; break;
  466. case "pt": converted = quantity / 2.1134; break;
  467. case "qt": converted = quantity / 1.0567; break;
  468. case "gal": converted = quantity * 3.7854; break;
  469. }
  470. //change to end
  471. switch(to){
  472. case "ml": converted *= 1000; break;
  473. case "l": break;
  474. case "tsp": converted *= 202.8842; break;
  475. case "tbsp": converted *= 67.6278; break;
  476. case "ozfl": converted *= 33.8141; break;
  477. case "cup": converted *= 4.1667; break;
  478. case "pt": converted *= 2.1134; break;
  479. case "qt": converted *= 1.0567; break;
  480. case "gal": converted /= 3.7854; break;
  481. }
  482. return converted;
  483. }
  484. let convertLength = (quantity, from, to)=>{
  485. //change to m
  486. let converted = 0;
  487. switch(from){
  488. case "mm": converted = quantity / 1000; break;
  489. case "cm": converted = quantity / 100; break;
  490. case "m": converted = quantity; break;
  491. case "in": converted = quantity / 39.3701; break;
  492. case "ft": converted = quantity / 3.2808; break;
  493. }
  494. //change to end
  495. switch(to){
  496. case "mm": converted *= 1000; break;
  497. case "cm": converted *= 100; break;
  498. case "m": break;
  499. case "in": converted *= 39.3701; break;
  500. case "ft": converted *= 3.2808; break;
  501. }
  502. return converted;
  503. }