Merchant.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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. //TODO: remove this, just for testing purposes
  234. clearTransactions(){
  235. this._transactions = [];
  236. }
  237. getTransactions(from = 0, to = new Date()){
  238. if(merchant._transactions.length <= 0){
  239. return [];
  240. }
  241. if(from === 0){
  242. from = this._transactions[this._transactions.length-1].date;
  243. }
  244. const {start, end} = this.getTransactionIndices(from, to);
  245. return this._transactions.slice(start, end + 1);
  246. }
  247. addTransaction(transaction){
  248. transaction = new this._modules.Transaction(
  249. transaction._id,
  250. transaction.date,
  251. transaction.recipes,
  252. this
  253. );
  254. this._transactions.push(transaction);
  255. this._transactions.sort((a, b)=>{
  256. if(a.date > b.date){
  257. return -1;
  258. }
  259. return 1;
  260. });
  261. let ingredients = {};
  262. for(let i = 0; i < transaction.recipes.length; i++){
  263. const recipe = transaction.recipes[i];
  264. for(let j = 0; j < recipe.recipe.ingredients.length; j++){
  265. const ingredient = recipe.recipe.ingredients[j];
  266. if(ingredients[ingredient.ingredient.id]){
  267. ingredients[ingredient.ingredient.id] += recipe.quantity * ingredient.quantity;
  268. }else{
  269. ingredients[ingredient.ingredient.id] = recipe.quantity * ingredient.quantity;
  270. }
  271. }
  272. }
  273. const keys = Object.keys(ingredients);
  274. for(let i = 0; i < keys.length; i++){
  275. for(let j = 0; j < this._ingredients.length; j++){
  276. if(keys[i] === this._ingredients[j].ingredient.id){
  277. this._ingredients[j].updateQuantity(-ingredients[keys[i]]);
  278. }
  279. }
  280. }
  281. this._modules.home.isPopulated = false;
  282. this._modules.ingredients.isPopulated = false;
  283. this._modules.analytics.newData = true;
  284. }
  285. removeTransaction(transaction){
  286. const index = this._transactions.indexOf(transaction);
  287. if(index === undefined){
  288. return false;
  289. }
  290. this._transactions.splice(index, 1);
  291. let ingredients = {};
  292. for(let i = 0; i < transaction.recipes.length; i++){
  293. const recipe = transaction.recipes[i];
  294. for(let j = 0; j < recipe.recipe.ingredients.length; j++){
  295. const ingredient = recipe.recipe.ingredients[j];
  296. if(ingredients[ingredient.ingredient.id]){
  297. ingredients[ingredient.ingredient.id] += ingredient.quantity * recipe.quantity;
  298. }else{
  299. ingredients[ingredient.ingredient.id] = ingredient.quantity * recipe.quantity;
  300. }
  301. }
  302. }
  303. const keys = Object.keys(ingredients);
  304. for(let i = 0; i < keys.length; i++){
  305. for(let j = 0; j < this._ingredients.length; j++){
  306. if(keys[i] === this._ingredients[j].ingredient.id){
  307. this._ingredients[j].updateQuantity(ingredients[keys[i]]);
  308. break;
  309. }
  310. }
  311. }
  312. this._modules.home.isPopulated = false;
  313. this._modules.ingredients.isPopulated = false;
  314. this._modules.analytics.newData = true;
  315. }
  316. get orders(){
  317. return this._orders;
  318. }
  319. clearOrders(){
  320. this._orders = [];
  321. }
  322. addOrder(data, isNew = false){
  323. let order = new this._modules.Order(
  324. data._id,
  325. data.name,
  326. data.date,
  327. data.taxes,
  328. data.fees,
  329. data.ingredients,
  330. this
  331. );
  332. this._orders.push(order);
  333. if(isNew){
  334. for(let i = 0; i < order.ingredients.length; i++){
  335. for(let j = 0; j < this._ingredients.length; j++){
  336. if(order.ingredients[i].ingredient === this._ingredients[j].ingredient){
  337. this._ingredients[j].updateQuantity(order.ingredients[i].quantity);
  338. break;
  339. }
  340. }
  341. }
  342. }
  343. this._modules.ingredients.isPopulated = false;
  344. this._modules.orders.isPopulated = false;
  345. }
  346. removeOrder(order){
  347. const index = this._orders.indexOf(order);
  348. if(index === undefined){
  349. return false;
  350. }
  351. this._orders.splice(index, 1);
  352. for(let i = 0; i < order.ingredients.length; i++){
  353. for(let j = 0; j < this._ingredients.length; j++){
  354. if(order.ingredients[i].ingredient === this._ingredients[j].ingredient){
  355. this._ingredients[j].updateQuantity(-order.ingredients[i].quantity);
  356. break;
  357. }
  358. }
  359. }
  360. this._modules.ingredients.isPopulated = false;
  361. this._modules.orders.isPopulated = false;
  362. }
  363. get units(){
  364. return this._units;
  365. }
  366. getRevenue(from, to = new Date()){
  367. if(from === 0){
  368. from = this._transactions[0].date;
  369. }
  370. const {start, end} = this.getTransactionIndices(from, to);
  371. let total = 0;
  372. for(let i = start; i <= end; i++){
  373. for(let j = 0; j < this._transactions[i].recipes.length; j++){
  374. for(let k = 0; k < this.recipes.length; k++){
  375. if(this._transactions[i].recipes[j].recipe === this.recipes[k]){
  376. total += this._transactions[i].recipes[j].quantity * this.recipes[k].price;
  377. }
  378. }
  379. }
  380. }
  381. return total / 100;
  382. }
  383. /*
  384. Gets the quantity of each ingredient sold between two dates (dateRange)
  385. Inputs:
  386. dateRange: list containing a start date and an end date
  387. Return:
  388. [{
  389. ingredient: Ingredient object,
  390. quantity: quantity of ingredient sold in default unit
  391. }]
  392. */
  393. getIngredientsSold(from, to = new Date()){
  394. let recipes = this.getRecipesSold(from, to);
  395. let ingredientList = [];
  396. for(let i = 0; i < recipes.length; i++){
  397. for(let j = 0; j < recipes[i].recipe.ingredients.length; j++){
  398. let exists = false;
  399. for(let k = 0; k < ingredientList.length; k++){
  400. if(ingredientList[k].ingredient === recipes[i].recipe.ingredients[j].ingredient){
  401. exists = true;
  402. ingredientList[k].quantity += recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity;
  403. break;
  404. }
  405. }
  406. if(!exists){
  407. ingredientList.push({
  408. ingredient: recipes[i].recipe.ingredients[j].ingredient,
  409. quantity: recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity
  410. });
  411. }
  412. }
  413. }
  414. return ingredientList;
  415. }
  416. /*
  417. Gets the quantity of a single ingredient sold between two dates
  418. Inputs:
  419. ingredient = MerchantIngredient object to find
  420. from = start Date
  421. to = end Date
  422. return: quantity sold in default unit
  423. */
  424. getSingleIngredientSold(ingredient, from, to = new Date()){
  425. const {start, end} = this.getTransactionIndices(from, to);
  426. let total = 0;
  427. for(let i = start; i < end; i++){
  428. for(let j = 0; j < this._transactions[i].recipes.length; j++){
  429. for(let k = 0; k < this._transactions[i].recipes[j].recipe.ingredients.length; k++){
  430. if(this._transactions[i].recipes[j].recipe.ingredients[k].ingredient === ingredient.ingredient){
  431. total += this._transactions[i].recipes[j].recipe.ingredients[k].quantity;
  432. break;
  433. }
  434. }
  435. }
  436. }
  437. return total;
  438. }
  439. /*
  440. Gets the number of recipes sold between two dates (dateRange)
  441. Inputs:
  442. dateRange: array containing a start date and an end date
  443. Return:
  444. [{
  445. recipe: a recipe object
  446. quantity: quantity of the recipe sold
  447. }]
  448. */
  449. getRecipesSold(from = 0, to = new Date()){
  450. if(from === 0){
  451. from = this._transactions[0].date;
  452. }
  453. const {start, end} = this.getTransactionIndices(from, to);
  454. let recipeList = [];
  455. for(let i = start; i <= end; i++){
  456. for(let j = 0; j < this._transactions[i].recipes.length; j++){
  457. let exists = false;
  458. for(let k = 0; k < recipeList.length; k++){
  459. if(recipeList[k].recipe === this._transactions[i].recipes[j].recipe){
  460. exists = true;
  461. recipeList[k].quantity += this._transactions[i].recipes[j].quantity;
  462. break;
  463. }
  464. }
  465. if(!exists){
  466. recipeList.push({
  467. recipe: this._transactions[i].recipes[j].recipe,
  468. quantity: this._transactions[i].recipes[j].quantity
  469. });
  470. }
  471. }
  472. }
  473. return recipeList;
  474. }
  475. /*
  476. Groups all of the merchant's ingredients by their category
  477. Return: [{
  478. name: category name,
  479. ingredients: [MerchantIngredient Object]
  480. }]
  481. */
  482. categorizeIngredients(){
  483. let ingredientsByCategory = [];
  484. for(let i = 0; i < this.ingredients.length; i++){
  485. let categoryExists = false;
  486. for(let j = 0; j < ingredientsByCategory.length; j++){
  487. if(this.ingredients[i].ingredient.category === ingredientsByCategory[j].name){
  488. ingredientsByCategory[j].ingredients.push(this.ingredients[i]);
  489. categoryExists = true;
  490. break;
  491. }
  492. }
  493. if(!categoryExists){
  494. ingredientsByCategory.push({
  495. name: this.ingredients[i].ingredient.category,
  496. ingredients: [this.ingredients[i]]
  497. });
  498. }
  499. }
  500. return ingredientsByCategory;
  501. }
  502. getRecipesForIngredient(ingredient){
  503. let recipes = [];
  504. for(let i = 0; i < this._recipes.length; i++){
  505. for(let j = 0; j < this._recipes[i].ingredients.length; j++){
  506. if(this._recipes[i].ingredients[j].ingredient === ingredient){
  507. recipes.push(this._recipes[i]);
  508. break;
  509. }
  510. }
  511. }
  512. return recipes;
  513. }
  514. getTransactionIndices(from, to){
  515. let start, end;
  516. for(let i = this._transactions.length - 1; i >= 0; i--){
  517. if(this._transactions[i].date >= from){
  518. end = i;
  519. break;
  520. }
  521. }
  522. for(let i = 0; i < this._transactions.length; i++){
  523. if(this._transactions[i].date < to){
  524. start = i;
  525. break;
  526. }
  527. }
  528. if(end === undefined){
  529. return false;
  530. }
  531. return {start: start, end: end};
  532. }
  533. isSanitaryString(str){
  534. let disallowed = ["\\", "<", ">", "$", "{", "}", "(", ")"];
  535. for(let i = 0; i < disallowed.length; i++){
  536. if(str.includes(disallowed[i])){
  537. return false;
  538. }
  539. }
  540. return true;
  541. }
  542. }
  543. module.exports = Merchant;