Merchant.js 21 KB

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