Merchant.js 21 KB

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