Merchant.js 19 KB

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