Merchant.js 21 KB

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