Merchant.js 21 KB

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