Merchant.js 21 KB

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