Merchant.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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,
  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(recipe){
  200. this._recipes.push(recipe);
  201. this._modules.recipeBook.isPopulated = false;
  202. }
  203. removeRecipe(recipe){
  204. const index = this._recipes.indexOf(recipe);
  205. if(index === undefined){
  206. return false;
  207. }
  208. this._recipes.splice(index, 1);
  209. this._modules.recipeBook.isPopulated = false;
  210. }
  211. /*
  212. recipe = {
  213. name: required,
  214. price: required,
  215. ingredients: [{
  216. ingredient: id of ingredient,
  217. quantity: quantity of ingredient
  218. }]
  219. }
  220. */
  221. updateRecipe(recipe){
  222. for(let i = 0; i < this._recipes.length; i++){
  223. if(this._recipes[i].id === recipe._id){
  224. this._recipes[i].name = recipe.name;
  225. this._recipes[i].price = recipe.price;
  226. this._recipes[i].removeIngredients();
  227. for(let j = 0; j < recipe.ingredients.length; j++){
  228. for(let k = 0; k < this._ingredients.length; k++){
  229. if(this._ingredients[k].ingredient.id === recipe.ingredients[j].ingredient){
  230. this._recipes[i].addIngredient(
  231. this._ingredients[k].ingredient,
  232. recipe.ingredients[j].quantity
  233. );
  234. break;
  235. }
  236. }
  237. }
  238. break;
  239. }
  240. }
  241. this._modules.recipeBook.isPopulated = false;
  242. }
  243. getTransactions(from = 0, to = new Date()){
  244. if(merchant._transactions.length <= 0){
  245. return [];
  246. }
  247. if(from === 0){
  248. from = this._transactions[this._transactions.length-1].date;
  249. }
  250. const {start, end} = this.getTransactionIndices(from, to);
  251. return this._transactions.slice(start, end + 1);
  252. }
  253. addTransaction(transaction){
  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. addOrder(order, isNew = false){
  320. this._orders.push(order);
  321. if(isNew){
  322. for(let i = 0; i < order.ingredients.length; i++){
  323. for(let j = 0; j < this._ingredients.length; j++){
  324. if(order.ingredients[i].ingredient === this._ingredients[j].ingredient){
  325. this._ingredients[j].updateQuantity(order.ingredients[i].quantity);
  326. break;
  327. }
  328. }
  329. }
  330. }
  331. this._modules.ingredients.isPopulated = false;
  332. this._modules.orders.isPopulated = false;
  333. }
  334. setOrders(orders){
  335. this._orders = orders
  336. }
  337. removeOrder(order){
  338. const index = this._orders.indexOf(order);
  339. if(index === undefined){
  340. return false;
  341. }
  342. this._orders.splice(index, 1);
  343. for(let i = 0; i < order.ingredients.length; i++){
  344. for(let j = 0; j < this._ingredients.length; j++){
  345. if(order.ingredients[i].ingredient === this._ingredients[j].ingredient){
  346. this._ingredients[j].updateQuantity(-order.ingredients[i].quantity);
  347. break;
  348. }
  349. }
  350. }
  351. this._modules.ingredients.isPopulated = false;
  352. this._modules.orders.isPopulated = false;
  353. }
  354. get units(){
  355. return this._units;
  356. }
  357. getRevenue(from, to = new Date()){
  358. if(from === 0){
  359. from = this._transactions[0].date;
  360. }
  361. const {start, end} = this.getTransactionIndices(from, to);
  362. let total = 0;
  363. for(let i = start; i <= end; i++){
  364. for(let j = 0; j < this._transactions[i].recipes.length; j++){
  365. for(let k = 0; k < this.recipes.length; k++){
  366. if(this._transactions[i].recipes[j].recipe === this.recipes[k]){
  367. total += this._transactions[i].recipes[j].quantity * this.recipes[k].price;
  368. }
  369. }
  370. }
  371. }
  372. return total / 100;
  373. }
  374. /*
  375. Gets the quantity of each ingredient sold between two dates (dateRange)
  376. Inputs:
  377. dateRange: list containing a start date and an end date
  378. Return:
  379. [{
  380. ingredient: Ingredient object,
  381. quantity: quantity of ingredient sold in default unit
  382. }]
  383. */
  384. getIngredientsSold(from = 0, to = new Date()){
  385. if(from = 0){
  386. from = this._ingredients[0].date;
  387. }
  388. let recipes = this.getRecipesSold(from, to);
  389. let ingredientList = [];
  390. for(let i = 0; i < recipes.length; i++){
  391. for(let j = 0; j < recipes[i].recipe.ingredients.length; j++){
  392. let exists = false;
  393. for(let k = 0; k < ingredientList.length; k++){
  394. if(ingredientList[k].ingredient === recipes[i].recipe.ingredients[j].ingredient){
  395. exists = true;
  396. ingredientList[k].quantity += recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity;
  397. }
  398. }
  399. if(!exists){
  400. ingredientList.push({
  401. ingredient: recipes[i].recipe.ingredients[j].ingredient,
  402. quantity: recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity
  403. });
  404. }
  405. }
  406. }
  407. return ingredientList;
  408. }
  409. /*
  410. Gets the quantity of a single ingredient sold between two dates
  411. Inputs:
  412. ingredient = MerchantIngredient object to find
  413. from = start Date
  414. to = end Date
  415. return: quantity sold in default unit
  416. */
  417. getSingleIngredientSold(ingredient, from = 0, to = new Date()){
  418. if(from === 0){
  419. from = this._transactions[0].date;
  420. }
  421. const {start, end} = this.getTransactionIndices(from, to);
  422. let total = 0;
  423. for(let i = start; i < end; i++){
  424. for(let j = 0; j < this._transactions[i].recipes.length; j++){
  425. for(let k = 0; k < this._transactions[i].recipes[j].recipe.ingredients.length; k++){
  426. if(this._transactions[i].recipes[j].recipe.ingredients[k].ingredient === ingredient.ingredient){
  427. total += this._transactions[i].recipes[j].recipe.ingredients[k].quantity;
  428. break;
  429. }
  430. }
  431. }
  432. }
  433. return total;
  434. }
  435. /*
  436. Gets the number of recipes sold between two dates (dateRange)
  437. Inputs:
  438. dateRange: array containing a start date and an end date
  439. Return:
  440. [{
  441. recipe: a recipe object
  442. quantity: quantity of the recipe sold
  443. }]
  444. */
  445. getRecipesSold(from = 0, to = new Date()){
  446. if(from = 0){
  447. from = this._transactions[0].date;
  448. }
  449. const {start, end} = this.getTransactionIndices(from, to);
  450. let recipeList = [];
  451. for(let i = start; i <= end; i++){
  452. for(let j = 0; j < this._transactions[i].recipes.length; j++){
  453. let exists = false;
  454. for(let k = 0; k < recipeList.length; k++){
  455. if(recipeList[k].recipe === this._transactions[i].recipes[j].recipe){
  456. exists = true;
  457. recipeList[k].quantity += this._transactions[i].recipes[j].quantity;
  458. break;
  459. }
  460. }
  461. if(!exists){
  462. recipeList.push({
  463. recipe: this._transactions[i].recipes[j].recipe,
  464. quantity: this._transactions[i].recipes[j].quantity
  465. });
  466. }
  467. }
  468. }
  469. return recipeList;
  470. }
  471. /*
  472. Groups all of the merchant's ingredients by their category
  473. Return: [{
  474. name: category name,
  475. ingredients: [MerchantIngredient Object]
  476. }]
  477. */
  478. categorizeIngredients(){
  479. let ingredientsByCategory = [];
  480. for(let i = 0; i < this.ingredients.length; i++){
  481. let categoryExists = false;
  482. for(let j = 0; j < ingredientsByCategory.length; j++){
  483. if(this.ingredients[i].ingredient.category === ingredientsByCategory[j].name){
  484. ingredientsByCategory[j].ingredients.push(this.ingredients[i]);
  485. categoryExists = true;
  486. break;
  487. }
  488. }
  489. if(!categoryExists){
  490. ingredientsByCategory.push({
  491. name: this.ingredients[i].ingredient.category,
  492. ingredients: [this.ingredients[i]]
  493. });
  494. }
  495. }
  496. return ingredientsByCategory;
  497. }
  498. unitizeIngredients(){
  499. let ingredientsByUnit = [];
  500. for(let i = 0; i < this.ingredients.length; i++){
  501. let unitExists = false;
  502. const innerIngredient = this.ingredients[i].ingredient;
  503. for(let j = 0; j < ingredientsByUnit.length; j++){
  504. if(innerIngredient.unit === ingredientsByUnit[j].name || innerIngredient.specialUnit === ingredientsByUnit[j].name){
  505. ingredientsByUnit[j].ingredients.push(this.ingredients[i]);
  506. unitExists = true;
  507. break;
  508. }
  509. }
  510. if(!unitExists){
  511. let unit = "";
  512. if(innerIngredient.specialUnit === "bottle"){
  513. unit = "bottle";
  514. }else{
  515. unit = innerIngredient.unit;
  516. }
  517. ingredientsByUnit.push({
  518. name: unit,
  519. ingredients: [this.ingredients[i]]
  520. });
  521. }
  522. }
  523. return ingredientsByUnit;
  524. }
  525. getRecipesForIngredient(ingredient){
  526. let recipes = [];
  527. for(let i = 0; i < this._recipes.length; i++){
  528. for(let j = 0; j < this._recipes[i].ingredients.length; j++){
  529. if(this._recipes[i].ingredients[j].ingredient === ingredient){
  530. recipes.push(this._recipes[i]);
  531. break;
  532. }
  533. }
  534. }
  535. return recipes;
  536. }
  537. getTransactionIndices(from, to){
  538. let start, end;
  539. to.setDate(to.getDate() + 1);
  540. for(let i = this._transactions.length - 1; i >= 0; i--){
  541. if(this._transactions[i].date >= from){
  542. start = i;
  543. break;
  544. }
  545. }
  546. for(let i = 0; i < this._transactions.length; i++){
  547. if(this._transactions[i].date < to){
  548. end = i;
  549. break;
  550. }
  551. }
  552. if(start === undefined){
  553. return false;
  554. }
  555. //these are switched due to the order of the transactions in the merchant
  556. return {start: end, end: start};
  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;