Merchant.js 19 KB

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