Merchant.js 19 KB

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