Merchant.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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. recipeBookStrandObj.populateRecipes();
  241. closeSidebar();
  242. }
  243. /*
  244. Updates a list of orders in the merchants list of orders
  245. Create/edit/remove
  246. orders = [Order object]
  247. remove = will remove order when true
  248. */
  249. editOrders(orders, remove = false){
  250. for(let i = 0; i < orders.length; i++){
  251. let isNew = true;
  252. for(let j = 0; j < this.orders.length; j++){
  253. if(orders[i] === this.orders[j]){
  254. if(remove){
  255. this.orders.splice(j, 1);
  256. }else{
  257. this.orders[j] = orders[i];
  258. }
  259. isNew = false;
  260. break;
  261. }
  262. }
  263. if(isNew){
  264. this.orders.push(orders[i]);
  265. }
  266. }
  267. ordersStrandObj.populate();
  268. closeSidebar();
  269. }
  270. editTransactions(transaction, remove = false){
  271. let isNew = true;
  272. for(let i = 0; i < this.transactions.length; i++){
  273. if(this.transactions[i] === transaction){
  274. if(remove){
  275. this.transactions.splice(i, 1);
  276. }
  277. isNew = false;
  278. break;
  279. }
  280. }
  281. if(isNew){
  282. this.transactions.push(transaction);
  283. this.transactions.sort((a, b) => a.date > b.date ? 1 : -1);
  284. }
  285. transactionsStrandObj.isPopulated = false;
  286. transactionsStrandObj.display();
  287. closeSidebar();
  288. }
  289. /*
  290. Gets the indices of two dates from transactions
  291. Inputs
  292. from: starting date
  293. to: ending date (default to now)
  294. Output
  295. Array containing starting index and ending index
  296. Note: Will return false if it cannot find both necessary dates
  297. */
  298. transactionIndices(from, to = new Date()){
  299. let indices = [];
  300. for(let i = 0; i < this.transactions.length; i++){
  301. if(this.transactions[i].date > from){
  302. indices.push(i);
  303. break;
  304. }
  305. }
  306. for(let i = this.transactions.length - 1; i >=0; i--){
  307. if(this.transactions[i].date < to){
  308. indices.push(i);
  309. break;
  310. }
  311. }
  312. if(indices.length < 2){
  313. return false;
  314. }
  315. return indices;
  316. }
  317. revenue(indices){
  318. let total = 0;
  319. for(let i = indices[0]; i <= indices[1]; i++){
  320. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  321. for(let k = 0; k < this.recipes.length; k++){
  322. if(this.transactions[i].recipes[j].recipe === this.recipes[k]){
  323. total += this.transactions[i].recipes[j].quantity * this.recipes[k].price;
  324. }
  325. }
  326. }
  327. }
  328. return total / 100;
  329. }
  330. /*
  331. Gets the quantity of each ingredient sold between two dates (dateRange)
  332. Inputs
  333. dateRange: list containing a start date and an end date
  334. Return:
  335. [{
  336. ingredient: Ingredient object,
  337. quantity: quantity of ingredient sold
  338. }]
  339. */
  340. ingredientsSold(dateRange){
  341. if(!dateRange){
  342. return false;
  343. }
  344. let recipes = this.recipesSold(dateRange);
  345. let ingredientList = [];
  346. for(let i = 0; i < recipes.length; i++){
  347. for(let j = 0; j < recipes[i].recipe.ingredients.length; j++){
  348. let exists = false;
  349. for(let k = 0; k < ingredientList.length; k++){
  350. if(ingredientList[k].ingredient === recipes[i].recipe.ingredients[j].ingredient){
  351. exists = true;
  352. ingredientList[k].quantity += recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity;
  353. break;
  354. }
  355. }
  356. if(!exists){
  357. ingredientList.push({
  358. ingredient: recipes[i].recipe.ingredients[j].ingredient,
  359. quantity: recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity
  360. });
  361. }
  362. }
  363. }
  364. return ingredientList;
  365. }
  366. singleIngredientSold(dateRange, ingredient){
  367. let total = 0;
  368. for(let i = dateRange[0]; i < dateRange[1]; i++){
  369. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  370. for(let k = 0; k < this.transactions[i].recipes[j].recipe.ingredients.length; k++){
  371. if(this.transactions[i].recipes[j].recipe.ingredients[k].ingredient === ingredient.ingredient){
  372. total += this.transactions[i].recipes[j].recipe.ingredients[k].quantity;
  373. break;
  374. }
  375. }
  376. }
  377. }
  378. return total;
  379. }
  380. /*
  381. Gets the number of recipes sold between two dates (dateRange)
  382. Inputs:
  383. dateRange: array containing a start date and an end date
  384. Return:
  385. [{
  386. recipe: a recipe object
  387. quantity: quantity of the recipe sold
  388. }]
  389. */
  390. recipesSold(dateRange){
  391. let recipeList = [];
  392. for(let i = dateRange[0]; i <= dateRange[1]; i++){
  393. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  394. let exists = false;
  395. for(let k = 0; k < recipeList.length; k++){
  396. if(recipeList[k].recipe === this.transactions[i].recipes[j].recipe){
  397. exists = true;
  398. recipeList[k].quantity += this.transactions[i].recipes[j].quantity;
  399. break;
  400. }
  401. }
  402. if(!exists){
  403. recipeList.push({
  404. recipe: this.transactions[i].recipes[j].recipe,
  405. quantity: this.transactions[i].recipes[j].quantity
  406. });
  407. }
  408. }
  409. }
  410. return recipeList;
  411. }
  412. /*
  413. Create revenue data for graphing
  414. Input:
  415. dateRange: [start index, end index] (this.transactionIndices)
  416. Return:
  417. [total revenue for each day]
  418. */
  419. graphDailyRevenue(dateRange){
  420. if(!dateRange){
  421. return false;
  422. }
  423. let dataList = new Array(30).fill(0);
  424. let currentDate = this.transactions[dateRange[0]].date;
  425. let arrayIndex = 0;
  426. for(let i = dateRange[0]; i <= dateRange[1]; i++){
  427. if(this.transactions[i].date.getDate() !== currentDate.getDate()){
  428. currentDate = this.transactions[i].date;
  429. arrayIndex++;
  430. }
  431. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  432. dataList[arrayIndex] += (this.transactions[i].recipes[j].recipe.price / 100) * this.transactions[i].recipes[j].quantity;
  433. }
  434. }
  435. return dataList;
  436. }
  437. /*
  438. Groups all of the merchant's ingredients by their category
  439. Return: [{
  440. name: category name,
  441. ingredients: [Ingredient Object]
  442. }]
  443. */
  444. categorizeIngredients(){
  445. let ingredientsByCategory = [];
  446. for(let i = 0; i < this.ingredients.length; i++){
  447. let categoryExists = false;
  448. for(let j = 0; j < ingredientsByCategory.length; j++){
  449. if(this.ingredients[i].ingredient.category === ingredientsByCategory[j].name){
  450. ingredientsByCategory[j].ingredients.push(this.ingredients[i]);
  451. categoryExists = true;
  452. break;
  453. }
  454. }
  455. if(!categoryExists){
  456. ingredientsByCategory.push({
  457. name: this.ingredients[i].ingredient.category,
  458. ingredients: [this.ingredients[i]]
  459. });
  460. }
  461. }
  462. return ingredientsByCategory;
  463. }
  464. unitizeIngredients(){
  465. let ingredientsByUnit = [];
  466. for(let i = 0; i < this.ingredients.length; i++){
  467. let unitExists = false;
  468. for(let j = 0; j < ingredientsByUnit.length; j++){
  469. if(this.ingredients[i].ingredient.unit === ingredientsByUnit[j].name){
  470. ingredientsByUnit[j].ingredients.push(this.ingredients[i]);
  471. unitExists = true;
  472. break;
  473. }
  474. }
  475. if(!unitExists){
  476. ingredientsByUnit.push({
  477. name: this.ingredients[i].ingredient.unit,
  478. ingredients: [this.ingredients[i]]
  479. });
  480. }
  481. }
  482. return ingredientsByUnit;
  483. }
  484. getRecipesForIngredient(ingredient){
  485. let recipes = [];
  486. for(let i = 0; i < this.recipes.length; i++){
  487. for(let j = 0; j < this.recipes[i].ingredients.length; j++){
  488. if(this.recipes[i].ingredients[j].ingredient === ingredient){
  489. recipes.push(this.recipes[i]);
  490. }
  491. }
  492. }
  493. return recipes;
  494. }
  495. }
  496. let convertToMain = (unit, quantity)=>{
  497. let converted = 0;
  498. if(merchant.units.mass.includes(unit)){
  499. switch(unit){
  500. case "g": break;
  501. case "kg": converted = quantity * 1000; break;
  502. case "oz": converted = quantity * 28.3495; break;
  503. case "lb": converted = quantity * 453.5924; break;
  504. }
  505. }else if(merchant.units.volume.includes(unit)){
  506. switch(unit){
  507. case "ml": converted = quantity / 1000; break;
  508. case "l": break;
  509. case "tsp": converted = quantity / 202.8842; break;
  510. case "tbsp": converted = quantity / 67.6278; break;
  511. case "ozfl": converted = quantity / 33.8141; break;
  512. case "cup": converted = quantity / 4.1667; break;
  513. case "pt": converted = quantity / 2.1134; break;
  514. case "qt": converted = quantity / 1.0567; break;
  515. case "gal": converted = quantity * 3.7854; break;
  516. }
  517. }else if(merchant.units.length.includes(unit)){
  518. switch(unit){
  519. case "mm": converted = quantity / 1000; break;
  520. case "cm": converted = quantity / 100; break;
  521. case "m": break;
  522. case "in": converted = quantity / 39.3701; break;
  523. case "ft": converted = quantity / 3.2808; break;
  524. }
  525. }else{
  526. converted = quantity;
  527. }
  528. return converted;
  529. }