Merchant.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  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, to = new Date()){
  390. let recipes = this.getRecipesSold(from, to);
  391. let ingredientList = [];
  392. for(let i = 0; i < recipes.length; i++){
  393. for(let j = 0; j < recipes[i].recipe.ingredients.length; j++){
  394. let exists = false;
  395. for(let k = 0; k < ingredientList.length; k++){
  396. if(ingredientList[k].ingredient === recipes[i].recipe.ingredients[j].ingredient){
  397. exists = true;
  398. ingredientList[k].quantity += recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity;
  399. break;
  400. }
  401. }
  402. if(!exists){
  403. ingredientList.push({
  404. ingredient: recipes[i].recipe.ingredients[j].ingredient,
  405. quantity: recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity
  406. });
  407. }
  408. }
  409. }
  410. return ingredientList;
  411. }
  412. /*
  413. Gets the quantity of a single ingredient sold between two dates
  414. Inputs:
  415. ingredient = MerchantIngredient object to find
  416. from = start Date
  417. to = end Date
  418. return: quantity sold in default unit
  419. */
  420. getSingleIngredientSold(ingredient, from = 0, to = new Date()){
  421. if(from === 0){
  422. from = this._transactions[0].date;
  423. }
  424. const {start, end} = this.getTransactionIndices(from, to);
  425. let total = 0;
  426. for(let i = start; i < end; i++){
  427. for(let j = 0; j < this._transactions[i].recipes.length; j++){
  428. for(let k = 0; k < this._transactions[i].recipes[j].recipe.ingredients.length; k++){
  429. if(this._transactions[i].recipes[j].recipe.ingredients[k].ingredient === ingredient.ingredient){
  430. total += this._transactions[i].recipes[j].recipe.ingredients[k].quantity;
  431. break;
  432. }
  433. }
  434. }
  435. }
  436. return total;
  437. }
  438. /*
  439. Gets the number of recipes sold between two dates (dateRange)
  440. Inputs:
  441. dateRange: array containing a start date and an end date
  442. Return:
  443. [{
  444. recipe: a recipe object
  445. quantity: quantity of the recipe sold
  446. }]
  447. */
  448. getRecipesSold(from = 0, to = new Date()){
  449. if(from === 0){
  450. from = this._transactions[0].date;
  451. }
  452. const {start, end} = this.getTransactionIndices(from, to);
  453. let recipeList = [];
  454. for(let i = start; i <= end; i++){
  455. for(let j = 0; j < this._transactions[i].recipes.length; j++){
  456. let exists = false;
  457. for(let k = 0; k < recipeList.length; k++){
  458. if(recipeList[k].recipe === this._transactions[i].recipes[j].recipe){
  459. exists = true;
  460. recipeList[k].quantity += this._transactions[i].recipes[j].quantity;
  461. break;
  462. }
  463. }
  464. if(!exists){
  465. recipeList.push({
  466. recipe: this._transactions[i].recipes[j].recipe,
  467. quantity: this._transactions[i].recipes[j].quantity
  468. });
  469. }
  470. }
  471. }
  472. return recipeList;
  473. }
  474. /*
  475. Groups all of the merchant's ingredients by their category
  476. Return: [{
  477. name: category name,
  478. ingredients: [MerchantIngredient Object]
  479. }]
  480. */
  481. categorizeIngredients(){
  482. let ingredientsByCategory = [];
  483. for(let i = 0; i < this.ingredients.length; i++){
  484. let categoryExists = false;
  485. for(let j = 0; j < ingredientsByCategory.length; j++){
  486. if(this.ingredients[i].ingredient.category === ingredientsByCategory[j].name){
  487. ingredientsByCategory[j].ingredients.push(this.ingredients[i]);
  488. categoryExists = true;
  489. break;
  490. }
  491. }
  492. if(!categoryExists){
  493. ingredientsByCategory.push({
  494. name: this.ingredients[i].ingredient.category,
  495. ingredients: [this.ingredients[i]]
  496. });
  497. }
  498. }
  499. return ingredientsByCategory;
  500. }
  501. unitizeIngredients(){
  502. let ingredientsByUnit = [];
  503. for(let i = 0; i < this.ingredients.length; i++){
  504. let unitExists = false;
  505. const innerIngredient = this.ingredients[i].ingredient;
  506. for(let j = 0; j < ingredientsByUnit.length; j++){
  507. if(innerIngredient.unit === ingredientsByUnit[j].name || innerIngredient.specialUnit === ingredientsByUnit[j].name){
  508. ingredientsByUnit[j].ingredients.push(this.ingredients[i]);
  509. unitExists = true;
  510. break;
  511. }
  512. }
  513. if(!unitExists){
  514. let unit = "";
  515. unit = innerIngredient.unit;
  516. ingredientsByUnit.push({
  517. name: unit,
  518. ingredients: [this.ingredients[i]]
  519. });
  520. }
  521. }
  522. return ingredientsByUnit;
  523. }
  524. getRecipesForIngredient(ingredient){
  525. let recipes = [];
  526. for(let i = 0; i < this._recipes.length; i++){
  527. for(let j = 0; j < this._recipes[i].ingredients.length; j++){
  528. if(this._recipes[i].ingredients[j].ingredient === ingredient){
  529. recipes.push(this._recipes[i]);
  530. break;
  531. }
  532. }
  533. }
  534. return recipes;
  535. }
  536. getTransactionIndices(from, to){
  537. let start, end;
  538. for(let i = this._transactions.length - 1; i >= 0; i--){
  539. if(this._transactions[i].date >= from){
  540. end = i;
  541. break;
  542. }
  543. }
  544. for(let i = 0; i < this._transactions.length; i++){
  545. if(this._transactions[i].date < to){
  546. start = i;
  547. break;
  548. }
  549. }
  550. if(end === undefined){
  551. return false;
  552. }
  553. return {start: start, end: end};
  554. }
  555. isSanitaryString(str){
  556. let disallowed = ["\\", "<", ">", "$", "{", "}", "(", ")"];
  557. for(let i = 0; i < disallowed.length; i++){
  558. if(str.includes(disallowed[i])){
  559. return false;
  560. }
  561. }
  562. return true;
  563. }
  564. }
  565. module.exports = Merchant;