Merchant.js 21 KB

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