Merchant.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. class MerchantIngredient{
  2. constructor(ingredient, quantity){
  3. this._quantity = quantity;
  4. this._ingredient = ingredient;
  5. }
  6. get ingredient(){
  7. return this._ingredient;
  8. }
  9. get quantity(){
  10. switch(this._ingredient.unit){
  11. case "g":return this._quantity;
  12. case "kg": return this._quantity / 1000;
  13. case "oz": return this._quantity / 28.3495;
  14. case "lb": return this._quantity / 453.5924;
  15. case "ml": return this._quantity * 1000;
  16. case "l": return this._quantity;
  17. case "tsp": return this._quantity * 202.8842;
  18. case "tbsp": return this._quantity * 67.6278;
  19. case "ozfl": return this._quantity * 33.8141;
  20. case "cup": return this._quantity * 4.1667;
  21. case "pt": return this._quantity * 2.1134;
  22. case "qt": return this._quantity * 1.0567;
  23. case "gal": return this._quantity / 3.7854;
  24. case "mm": return this._quantity * 1000;
  25. case "cm": return this._quantity * 100;
  26. case "m": return this._quantity;
  27. case "in": return this._quantity * 39.3701;
  28. case "ft": return this._quantity * 3.2808;
  29. default: return this._quantity;
  30. }
  31. }
  32. updateQuantity(quantity){
  33. this._quantity += this.convertToBase(quantity);
  34. }
  35. convertToBase(quantity){
  36. switch(this._ingredient.unit){
  37. case "g": return quantity;
  38. case "kg": return quantity * 1000;
  39. case "oz": return quantity * 28.3495;
  40. case "lb": return quantity * 453.5924;
  41. case "ml": return quantity / 1000;
  42. case "l": return quantity;
  43. case "tsp": return quantity / 202.8842;
  44. case "tbsp": return quantity / 67.6278;
  45. case "ozfl": return quantity / 33.8141;
  46. case "cup": return quantity / 4.1667;
  47. case "pt": return quantity / 2.1134;
  48. case "qt": return quantity / 1.0567;
  49. case "gal": return quantity * 3.7854;
  50. case "mm": return quantity / 1000;
  51. case "cm": return quantity / 100;
  52. case "m": return quantity;
  53. case "in": return quantity / 39.3701;
  54. case "ft": return quantity / 3.2808;
  55. default: return quantity;
  56. }
  57. }
  58. getQuantityDisplay(){
  59. return `${this.quantity.toFixed(2)} ${this._ingredient.unit.toUpperCase()}`;
  60. }
  61. }
  62. class Merchant{
  63. constructor(oldMerchant, transactions, modules){
  64. this._modules = modules;
  65. this._name = oldMerchant.name;
  66. this._pos = oldMerchant.pos;
  67. this._ingredients = [];
  68. this._recipes = [];
  69. this._transactions = [];
  70. this._orders = [];
  71. this._units = {
  72. mass: ["g", "kg", "oz", "lb"],
  73. volume: ["ml", "l", "tsp", "tbsp", "ozfl", "cup", "pt", "qt", "gal"],
  74. length: ["mm", "cm", "m", "in", "ft"],
  75. other: ["each", "bottle"]
  76. }
  77. //populate ingredients
  78. for(let i = 0; i < oldMerchant.inventory.length; i++){
  79. const ingredient = new modules.Ingredient(
  80. oldMerchant.inventory[i].ingredient._id,
  81. oldMerchant.inventory[i].ingredient.name,
  82. oldMerchant.inventory[i].ingredient.category,
  83. oldMerchant.inventory[i].ingredient.unitType,
  84. oldMerchant.inventory[i].defaultUnit,
  85. this,
  86. oldMerchant.inventory[i].ingredient.specialUnit,
  87. oldMerchant.inventory[i].ingredient.unitSize
  88. );
  89. const merchantIngredient = new MerchantIngredient(
  90. ingredient,
  91. oldMerchant.inventory[i].quantity,
  92. );
  93. this._ingredients.push(merchantIngredient);
  94. }
  95. //populate recipes
  96. for(let i = 0; i < oldMerchant.recipes.length; i++){
  97. let ingredients = [];
  98. for(let j = 0; j < oldMerchant.recipes[i].ingredients.length; j++){
  99. const ingredient = oldMerchant.recipes[i].ingredients[j];
  100. for(let k = 0; k < this._ingredients.length; k++){
  101. if(ingredient.ingredient === this._ingredients[k].ingredient.id){
  102. ingredients.push({
  103. ingredient: this._ingredients[k].ingredient.id,
  104. quantity: ingredient.quantity
  105. });
  106. break;
  107. }
  108. }
  109. }
  110. this._recipes.push(new this._modules.Recipe(
  111. oldMerchant.recipes[i]._id,
  112. oldMerchant.recipes[i].name,
  113. oldMerchant.recipes[i].price,
  114. ingredients,
  115. this
  116. ));
  117. }
  118. //populate transactions
  119. for(let i = 0; i < transactions.length; i++){
  120. this._transactions.push(new modules.Transaction(
  121. transactions[i]._id,
  122. transactions[i].date,
  123. transactions[i].recipes,
  124. this
  125. ));
  126. }
  127. }
  128. get modules(){
  129. return this._modules;
  130. }
  131. get name(){
  132. return this._name;
  133. }
  134. set name(name){
  135. if(this.isSanitaryString(name)){
  136. this._name = name;
  137. }
  138. return false;
  139. }
  140. get pos(){
  141. return this._pos;
  142. }
  143. get ingredients(){
  144. return this._ingredients;
  145. }
  146. /*
  147. ingredient: {
  148. _id: String,
  149. name: String,
  150. category: String,
  151. unitType: String,
  152. specialUnit: String || undefined,
  153. unitSize: Number || undefined
  154. }
  155. quantity: Number
  156. defaultUnit: String
  157. */
  158. addIngredient(ingredient, quantity, defaultUnit){
  159. const createdIngredient = new this._modules.Ingredient(
  160. ingredient._id,
  161. ingredient.name,
  162. ingredient.category,
  163. ingredient.unitType,
  164. defaultUnit,
  165. this,
  166. ingredient.specialUnit,
  167. ingredient.unitSize
  168. )
  169. const merchantIngredient = new MerchantIngredient(createdIngredient, quantity);
  170. this._ingredients.push(merchantIngredient);
  171. this._modules.home.isPopulated = false;
  172. this._modules.ingredients.isPopulated = false;
  173. }
  174. removeIngredient(ingredient){
  175. const index = this._ingredients.indexOf(ingredient);
  176. if(index === undefined){
  177. return false;
  178. }
  179. this._ingredients.splice(index, 1);
  180. this._modules.home.isPopulated = false;
  181. this._modules.ingredients.isPopulated = false;
  182. }
  183. getIngredient(id){
  184. for(let i = 0; i < this._ingredients.length; i++){
  185. if(this._ingredients[i].ingredient.id === id){
  186. return this._ingredients[i];
  187. }
  188. }
  189. }
  190. get recipes(){
  191. return this._recipes;
  192. }
  193. addRecipe(id, name, price, ingredients){
  194. let recipe = new this._modules.Recipe(id, name, price, ingredients, this);
  195. this._recipes.push(recipe);
  196. this._modules.recipeBook.isPopulated = false;
  197. }
  198. removeRecipe(recipe){
  199. const index = this._recipes.indexOf(recipe);
  200. if(index === undefined){
  201. return false;
  202. }
  203. this._recipes.splice(index, 1);
  204. this._modules.recipeBook.isPopulated = false;
  205. }
  206. /*
  207. recipe = {
  208. name: required,
  209. price: required,
  210. ingredients: [{
  211. ingredient: id of ingredient,
  212. quantity: quantity of ingredient
  213. }]
  214. }
  215. */
  216. updateRecipe(recipe){
  217. for(let i = 0; i < this._recipes.length; i++){
  218. if(this._recipes[i].id === recipe._id){
  219. this._recipes[i].name = recipe.name;
  220. this._recipes[i].price = recipe.price;
  221. this._recipes[i].removeIngredients();
  222. for(let j = 0; j < recipe.ingredients.length; j++){
  223. for(let k = 0; k < this._ingredients.length; k++){
  224. if(this._ingredients[k].ingredient.id === recipe.ingredients[j].ingredient){
  225. this._recipes[i].addIngredient(
  226. this._ingredients[k].ingredient,
  227. recipe.ingredients[j].quantity
  228. );
  229. break;
  230. }
  231. }
  232. }
  233. break;
  234. }
  235. }
  236. this._modules.recipeBook.isPopulated = false;
  237. }
  238. get transactions(){
  239. return this._transactions;
  240. }
  241. getTransactions(from = 0, to = new Date()){
  242. if(merchant._transactions.length <= 0){
  243. return [];
  244. }
  245. if(from === 0){
  246. from = this._transactions[this._transactions.length-1].date;
  247. }
  248. const {start, end} = this.getTransactionIndices(from, to);
  249. return this._transactions.slice(start, end + 1);
  250. }
  251. addTransaction(transaction){
  252. transaction = new this._modules.Transaction(
  253. transaction._id,
  254. transaction.date,
  255. transaction.recipes,
  256. this
  257. );
  258. this._transactions.push(transaction);
  259. this._transactions.sort((a, b)=>{
  260. if(a.date > b.date){
  261. return -1;
  262. }
  263. return 1;
  264. });
  265. let ingredients = {};
  266. for(let i = 0; i < transaction.recipes.length; i++){
  267. const recipe = transaction.recipes[i];
  268. for(let j = 0; j < recipe.recipe.ingredients.length; j++){
  269. const ingredient = recipe.recipe.ingredients[j];
  270. if(ingredients[ingredient.ingredient.id]){
  271. ingredients[ingredient.ingredient.id] += recipe.quantity * ingredient.quantity;
  272. }else{
  273. ingredients[ingredient.ingredient.id] = recipe.quantity * ingredient.quantity;
  274. }
  275. }
  276. }
  277. const keys = Object.keys(ingredients);
  278. for(let i = 0; i < keys.length; i++){
  279. for(let j = 0; j < this._ingredients.length; j++){
  280. if(keys[i] === this._ingredients[j].ingredient.id){
  281. this._ingredients[j].updateQuantity(-ingredients[keys[i]]);
  282. }
  283. }
  284. }
  285. this._modules.home.isPopulated = false;
  286. this._modules.ingredients.isPopulated = false;
  287. this._modules.analytics.newData = true;
  288. }
  289. removeTransaction(transaction){
  290. const index = this._transactions.indexOf(transaction);
  291. if(index === undefined){
  292. return false;
  293. }
  294. this._transactions.splice(index, 1);
  295. let ingredients = {};
  296. for(let i = 0; i < transaction.recipes.length; i++){
  297. const recipe = transaction.recipes[i];
  298. for(let j = 0; j < recipe.recipe.ingredients.length; j++){
  299. const ingredient = recipe.recipe.ingredients[j];
  300. if(ingredients[ingredient.ingredient.id]){
  301. ingredients[ingredient.ingredient.id] += ingredient.quantity * recipe.quantity;
  302. }else{
  303. ingredients[ingredient.ingredient.id] = ingredient.quantity * recipe.quantity;
  304. }
  305. }
  306. }
  307. const keys = Object.keys(ingredients);
  308. for(let i = 0; i < keys.length; i++){
  309. for(let j = 0; j < this._ingredients.length; j++){
  310. if(keys[i] === this._ingredients[j].ingredient.id){
  311. this._ingredients[j].updateQuantity(ingredients[keys[i]]);
  312. break;
  313. }
  314. }
  315. }
  316. this._modules.home.isPopulated = false;
  317. this._modules.ingredients.isPopulated = false;
  318. this._modules.analytics.newData = true;
  319. }
  320. get orders(){
  321. return this._orders;
  322. }
  323. addOrder(data, isNew = false){
  324. let order = new this._modules.Order(
  325. data._id,
  326. data.name,
  327. data.date,
  328. data.taxes,
  329. data.fees,
  330. data.ingredients,
  331. this
  332. );
  333. this._orders.push(order);
  334. if(isNew){
  335. for(let i = 0; i < order.ingredients.length; i++){
  336. for(let j = 0; j < this._ingredients.length; j++){
  337. if(order.ingredients[i].ingredient === this._ingredients[j].ingredient){
  338. this._ingredients[j].updateQuantity(order.ingredients[i].quantity);
  339. break;
  340. }
  341. }
  342. }
  343. }
  344. this._modules.ingredients.isPopulated = false;
  345. this._modules.orders.isPopulated = false;
  346. }
  347. setOrders(orders){
  348. this._orders = orders
  349. }
  350. removeOrder(order){
  351. const index = this._orders.indexOf(order);
  352. if(index === undefined){
  353. return false;
  354. }
  355. this._orders.splice(index, 1);
  356. for(let i = 0; i < order.ingredients.length; i++){
  357. for(let j = 0; j < this._ingredients.length; j++){
  358. if(order.ingredients[i].ingredient === this._ingredients[j].ingredient){
  359. this._ingredients[j].updateQuantity(-order.ingredients[i].quantity);
  360. break;
  361. }
  362. }
  363. }
  364. this._modules.ingredients.isPopulated = false;
  365. this._modules.orders.isPopulated = false;
  366. }
  367. get units(){
  368. return this._units;
  369. }
  370. getRevenue(from, to = new Date()){
  371. if(from === 0){
  372. from = this._transactions[0].date;
  373. }
  374. const {start, end} = this.getTransactionIndices(from, to);
  375. let total = 0;
  376. for(let i = start; i <= end; i++){
  377. for(let j = 0; j < this._transactions[i].recipes.length; j++){
  378. for(let k = 0; k < this.recipes.length; k++){
  379. if(this._transactions[i].recipes[j].recipe === this.recipes[k]){
  380. total += this._transactions[i].recipes[j].quantity * this.recipes[k].price;
  381. }
  382. }
  383. }
  384. }
  385. return total / 100;
  386. }
  387. /*
  388. Gets the quantity of each ingredient sold between two dates (dateRange)
  389. Inputs:
  390. dateRange: list containing a start date and an end date
  391. Return:
  392. [{
  393. ingredient: Ingredient object,
  394. quantity: quantity of ingredient sold in default unit
  395. }]
  396. */
  397. getIngredientsSold(from = 0, to = new Date()){
  398. if(from === 0){
  399. from = this._ingredients[0].date;
  400. }
  401. let recipes = this.getRecipesSold(from, to);
  402. let ingredientList = [];
  403. for(let i = 0; i < recipes.length; i++){
  404. for(let j = 0; j < recipes[i].recipe.ingredients.length; j++){
  405. let exists = false;
  406. for(let k = 0; k < ingredientList.length; k++){
  407. if(ingredientList[k].ingredient === recipes[i].recipe.ingredients[j].ingredient){
  408. exists = true;
  409. ingredientList[k].quantity += recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity;
  410. break;
  411. }
  412. }
  413. if(!exists){
  414. ingredientList.push({
  415. ingredient: recipes[i].recipe.ingredients[j].ingredient,
  416. quantity: recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity
  417. });
  418. }
  419. }
  420. }
  421. return ingredientList;
  422. }
  423. /*
  424. Gets the quantity of a single ingredient sold between two dates
  425. Inputs:
  426. ingredient = MerchantIngredient object to find
  427. from = start Date
  428. to = end Date
  429. return: quantity sold in default unit
  430. */
  431. getSingleIngredientSold(ingredient, from = 0, to = new Date()){
  432. if(from === 0){
  433. from = this._transactions[0].date;
  434. }
  435. const {start, end} = this.getTransactionIndices(from, to);
  436. let total = 0;
  437. for(let i = start; i < end; i++){
  438. for(let j = 0; j < this._transactions[i].recipes.length; j++){
  439. for(let k = 0; k < this._transactions[i].recipes[j].recipe.ingredients.length; k++){
  440. if(this._transactions[i].recipes[j].recipe.ingredients[k].ingredient === ingredient.ingredient){
  441. total += this._transactions[i].recipes[j].recipe.ingredients[k].quantity;
  442. break;
  443. }
  444. }
  445. }
  446. }
  447. return total;
  448. }
  449. /*
  450. Gets the number of recipes sold between two dates (dateRange)
  451. Inputs:
  452. dateRange: array containing a start date and an end date
  453. Return:
  454. [{
  455. recipe: a recipe object
  456. quantity: quantity of the recipe sold
  457. }]
  458. */
  459. getRecipesSold(from = 0, to = new Date()){
  460. if(from === 0){
  461. from = this._transactions[0].date;
  462. }
  463. const {start, end} = this.getTransactionIndices(from, to);
  464. let recipeList = [];
  465. for(let i = start; i <= end; i++){
  466. for(let j = 0; j < this._transactions[i].recipes.length; j++){
  467. let exists = false;
  468. for(let k = 0; k < recipeList.length; k++){
  469. if(recipeList[k].recipe === this._transactions[i].recipes[j].recipe){
  470. exists = true;
  471. recipeList[k].quantity += this._transactions[i].recipes[j].quantity;
  472. break;
  473. }
  474. }
  475. if(!exists){
  476. recipeList.push({
  477. recipe: this._transactions[i].recipes[j].recipe,
  478. quantity: this._transactions[i].recipes[j].quantity
  479. });
  480. }
  481. }
  482. }
  483. return recipeList;
  484. }
  485. /*
  486. Groups all of the merchant's ingredients by their category
  487. Return: [{
  488. name: category name,
  489. ingredients: [MerchantIngredient Object]
  490. }]
  491. */
  492. categorizeIngredients(){
  493. let ingredientsByCategory = [];
  494. for(let i = 0; i < this.ingredients.length; i++){
  495. let categoryExists = false;
  496. for(let j = 0; j < ingredientsByCategory.length; j++){
  497. if(this.ingredients[i].ingredient.category === ingredientsByCategory[j].name){
  498. ingredientsByCategory[j].ingredients.push(this.ingredients[i]);
  499. categoryExists = true;
  500. break;
  501. }
  502. }
  503. if(!categoryExists){
  504. ingredientsByCategory.push({
  505. name: this.ingredients[i].ingredient.category,
  506. ingredients: [this.ingredients[i]]
  507. });
  508. }
  509. }
  510. return ingredientsByCategory;
  511. }
  512. unitizeIngredients(){
  513. let ingredientsByUnit = [];
  514. for(let i = 0; i < this.ingredients.length; i++){
  515. let unitExists = false;
  516. const innerIngredient = this.ingredients[i].ingredient;
  517. for(let j = 0; j < ingredientsByUnit.length; j++){
  518. if(innerIngredient.unit === ingredientsByUnit[j].name || innerIngredient.specialUnit === ingredientsByUnit[j].name){
  519. ingredientsByUnit[j].ingredients.push(this.ingredients[i]);
  520. unitExists = true;
  521. break;
  522. }
  523. }
  524. if(!unitExists){
  525. let unit = "";
  526. unit = innerIngredient.unit;
  527. ingredientsByUnit.push({
  528. name: unit,
  529. ingredients: [this.ingredients[i]]
  530. });
  531. }
  532. }
  533. return ingredientsByUnit;
  534. }
  535. getRecipesForIngredient(ingredient){
  536. let recipes = [];
  537. for(let i = 0; i < this._recipes.length; i++){
  538. for(let j = 0; j < this._recipes[i].ingredients.length; j++){
  539. if(this._recipes[i].ingredients[j].ingredient === ingredient){
  540. recipes.push(this._recipes[i]);
  541. break;
  542. }
  543. }
  544. }
  545. return recipes;
  546. }
  547. getTransactionIndices(from, to){
  548. let start, end;
  549. for(let i = this._transactions.length - 1; i >= 0; i--){
  550. if(this._transactions[i].date >= from){
  551. end = i;
  552. break;
  553. }
  554. }
  555. for(let i = 0; i < this._transactions.length; i++){
  556. if(this._transactions[i].date < to){
  557. start = i;
  558. break;
  559. }
  560. }
  561. if(end === undefined){
  562. return false;
  563. }
  564. return {start: start, end: end};
  565. }
  566. isSanitaryString(str){
  567. let disallowed = ["\\", "<", ">", "$", "{", "}", "(", ")"];
  568. for(let i = 0; i < disallowed.length; i++){
  569. if(str.includes(disallowed[i])){
  570. return false;
  571. }
  572. }
  573. return true;
  574. }
  575. }
  576. module.exports = Merchant;