Merchant.js 21 KB

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