Merchant.js 21 KB

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