Merchant.js 21 KB

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