Merchant.js 21 KB

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