Merchant.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  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.quantity += ingredients[keys[i]];
  305. }
  306. }
  307. }
  308. this._modules.home.isPopulated = false;
  309. this._modules.ingredients.isPopulated = false;
  310. this._modules.analytics.newData = true;
  311. }
  312. get orders(){
  313. return this._orders;
  314. }
  315. addOrder(order, isNew = false){
  316. this._orders.push(order);
  317. if(isNew){
  318. for(let i = 0; i < order.ingredients.length; i++){
  319. for(let j = 0; j < this._ingredients.length; j++){
  320. if(order.ingredients[i].ingredient === this._ingredients[j].ingredient){
  321. this._ingredients[j].updateQuantity(order.ingredients[i].quantity);
  322. break;
  323. }
  324. }
  325. }
  326. }
  327. this._modules.ingredients.isPopulated = false;
  328. this._modules.orders.isPopulated = false;
  329. }
  330. setOrders(orders){
  331. this._orders = orders
  332. }
  333. removeOrder(order){
  334. const index = this._orders.indexOf(order);
  335. if(index === undefined){
  336. return false;
  337. }
  338. this._orders.splice(index, 1);
  339. for(let i = 0; i < order.ingredients.length; i++){
  340. for(let j = 0; j < this._ingredients.length; j++){
  341. if(order.ingredients[i].ingredient === this._ingredients[j].ingredient){
  342. this._ingredients[j].updateQuantity(-order.ingredients[i].quantity);
  343. break;
  344. }
  345. }
  346. }
  347. this._modules.ingredients.isPopulated = false;
  348. this._modules.orders.isPopulated = false;
  349. }
  350. get units(){
  351. return this._units;
  352. }
  353. getRevenue(from, to = new Date()){
  354. if(from === 0){
  355. from = this._transactions[0].date;
  356. }
  357. const {start, end} = this.getTransactionIndices(from, to);
  358. let total = 0;
  359. for(let i = start; i <= end; i++){
  360. for(let j = 0; j < this._transactions[i].recipes.length; j++){
  361. for(let k = 0; k < this.recipes.length; k++){
  362. if(this._transactions[i].recipes[j].recipe === this.recipes[k]){
  363. total += this._transactions[i].recipes[j].quantity * this.recipes[k].price;
  364. }
  365. }
  366. }
  367. }
  368. return total / 100;
  369. }
  370. /*
  371. Gets the quantity of each ingredient sold between two dates (dateRange)
  372. Inputs:
  373. dateRange: list containing a start date and an end date
  374. Return:
  375. [{
  376. ingredient: Ingredient object,
  377. quantity: quantity of ingredient sold in default unit
  378. }]
  379. */
  380. getIngredientsSold(from = 0, to = new Date()){
  381. if(from = 0){
  382. from = this._ingredients[0].date;
  383. }
  384. let recipes = this.getRecipesSold(from, to);
  385. let ingredientList = [];
  386. for(let i = 0; i < recipes.length; i++){
  387. for(let j = 0; j < recipes[i].recipe.ingredients.length; j++){
  388. let exists = false;
  389. for(let k = 0; k < ingredientList.length; k++){
  390. if(ingredientList[k].ingredient === recipes[i].recipe.ingredients[j].ingredient){
  391. exists = true;
  392. ingredientList[k].quantity += recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity;
  393. }
  394. }
  395. if(!exists){
  396. ingredientList.push({
  397. ingredient: recipes[i].recipe.ingredients[j].ingredient,
  398. quantity: recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity
  399. });
  400. }
  401. }
  402. }
  403. return ingredientList;
  404. }
  405. /*
  406. Gets the quantity of a single ingredient sold between two dates
  407. Inputs:
  408. ingredient = MerchantIngredient object to find
  409. from = start Date
  410. to = end Date
  411. return: quantity sold in default unit
  412. */
  413. getSingleIngredientSold(ingredient, from = 0, to = new Date()){
  414. if(from === 0){
  415. from = this._transactions[0].date;
  416. }
  417. const {start, end} = this.getTransactionIndices(from, to);
  418. let total = 0;
  419. for(let i = start; i < end; i++){
  420. for(let j = 0; j < this._transactions[i].recipes.length; j++){
  421. for(let k = 0; k < this._transactions[i].recipes[j].recipe.ingredients.length; k++){
  422. if(this._transactions[i].recipes[j].recipe.ingredients[k].ingredient === ingredient.ingredient){
  423. total += this._transactions[i].recipes[j].recipe.ingredients[k].quantity;
  424. break;
  425. }
  426. }
  427. }
  428. }
  429. return total;
  430. }
  431. /*
  432. Gets the number of recipes sold between two dates (dateRange)
  433. Inputs:
  434. dateRange: array containing a start date and an end date
  435. Return:
  436. [{
  437. recipe: a recipe object
  438. quantity: quantity of the recipe sold
  439. }]
  440. */
  441. getRecipesSold(from = 0, to = new Date()){
  442. if(from = 0){
  443. from = this._transactions[0].date;
  444. }
  445. const {start, end} = this.getTransactionIndices(from, to);
  446. let recipeList = [];
  447. for(let i = start; i <= end; i++){
  448. for(let j = 0; j < this._transactions[i].recipes.length; j++){
  449. let exists = false;
  450. for(let k = 0; k < recipeList.length; k++){
  451. if(recipeList[k].recipe === this._transactions[i].recipes[j].recipe){
  452. exists = true;
  453. recipeList[k].quantity += this._transactions[i].recipes[j].quantity;
  454. break;
  455. }
  456. }
  457. if(!exists){
  458. recipeList.push({
  459. recipe: this._transactions[i].recipes[j].recipe,
  460. quantity: this._transactions[i].recipes[j].quantity
  461. });
  462. }
  463. }
  464. }
  465. return recipeList;
  466. }
  467. /*
  468. Groups all of the merchant's ingredients by their category
  469. Return: [{
  470. name: category name,
  471. ingredients: [MerchantIngredient Object]
  472. }]
  473. */
  474. categorizeIngredients(){
  475. let ingredientsByCategory = [];
  476. for(let i = 0; i < this.ingredients.length; i++){
  477. let categoryExists = false;
  478. for(let j = 0; j < ingredientsByCategory.length; j++){
  479. if(this.ingredients[i].ingredient.category === ingredientsByCategory[j].name){
  480. ingredientsByCategory[j].ingredients.push(this.ingredients[i]);
  481. categoryExists = true;
  482. break;
  483. }
  484. }
  485. if(!categoryExists){
  486. ingredientsByCategory.push({
  487. name: this.ingredients[i].ingredient.category,
  488. ingredients: [this.ingredients[i]]
  489. });
  490. }
  491. }
  492. return ingredientsByCategory;
  493. }
  494. unitizeIngredients(){
  495. let ingredientsByUnit = [];
  496. for(let i = 0; i < this.ingredients.length; i++){
  497. let unitExists = false;
  498. const innerIngredient = this.ingredients[i].ingredient;
  499. for(let j = 0; j < ingredientsByUnit.length; j++){
  500. if(innerIngredient.unit === ingredientsByUnit[j].name || innerIngredient.specialUnit === ingredientsByUnit[j].name){
  501. ingredientsByUnit[j].ingredients.push(this.ingredients[i]);
  502. unitExists = true;
  503. break;
  504. }
  505. }
  506. if(!unitExists){
  507. let unit = "";
  508. if(innerIngredient.specialUnit === "bottle"){
  509. unit = "bottle";
  510. }else{
  511. unit = innerIngredient.unit;
  512. }
  513. ingredientsByUnit.push({
  514. name: unit,
  515. ingredients: [this.ingredients[i]]
  516. });
  517. }
  518. }
  519. return ingredientsByUnit;
  520. }
  521. getRecipesForIngredient(ingredient){
  522. let recipes = [];
  523. for(let i = 0; i < this._recipes.length; i++){
  524. for(let j = 0; j < this._recipes[i].ingredients.length; j++){
  525. if(this._recipes[i].ingredients[j].ingredient === ingredient){
  526. recipes.push(this._recipes[i]);
  527. break;
  528. }
  529. }
  530. }
  531. return recipes;
  532. }
  533. getTransactionIndices(from, to){
  534. let start, end;
  535. to.setDate(to.getDate() + 1);
  536. for(let i = this._transactions.length - 1; i >= 0; i--){
  537. if(this._transactions[i].date >= from){
  538. start = i;
  539. break;
  540. }
  541. }
  542. for(let i = 0; i < this._transactions.length; i++){
  543. if(this._transactions[i].date < to){
  544. end = i;
  545. break;
  546. }
  547. }
  548. if(start === undefined){
  549. return false;
  550. }
  551. //these are switched due to the order of the transactions in the merchant
  552. return {start: end, end: start};
  553. }
  554. isSanitaryString(str){
  555. let disallowed = ["\\", "<", ">", "$", "{", "}", "(", ")"];
  556. for(let i = 0; i < disallowed.length; i++){
  557. if(str.includes(disallowed[i])){
  558. return false;
  559. }
  560. }
  561. return true;
  562. }
  563. }
  564. module.exports = Merchant;