Merchant.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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. set email(email){
  135. this._email = email;
  136. }
  137. get pos(){
  138. return this._pos;
  139. }
  140. get ingredients(){
  141. return this._ingredients;
  142. }
  143. /*
  144. ingredient: {
  145. _id: String,
  146. name: String,
  147. category: String,
  148. unitType: String,
  149. specialUnit: String || undefined,
  150. unitSize: Number || undefined
  151. }
  152. quantity: Number
  153. defaultUnit: String
  154. */
  155. addIngredient(ingredient, quantity, defaultUnit){
  156. const createdIngredient = new this._modules.Ingredient(
  157. ingredient._id,
  158. ingredient.name,
  159. ingredient.category,
  160. ingredient.unitType,
  161. defaultUnit,
  162. this,
  163. ingredient.unitSize
  164. );
  165. const merchantIngredient = new MerchantIngredient(createdIngredient, quantity);
  166. this._ingredients.push(merchantIngredient);
  167. this._modules.home.isPopulated = false;
  168. this._modules.ingredients.isPopulated = false;
  169. }
  170. removeIngredient(ingredient){
  171. const index = this._ingredients.indexOf(ingredient);
  172. if(index === undefined){
  173. return false;
  174. }
  175. this._ingredients.splice(index, 1);
  176. this._modules.home.isPopulated = false;
  177. this._modules.ingredients.isPopulated = false;
  178. }
  179. getIngredient(id){
  180. for(let i = 0; i < this._ingredients.length; i++){
  181. if(this._ingredients[i].ingredient.id === id){
  182. return this._ingredients[i];
  183. }
  184. }
  185. }
  186. get recipes(){
  187. return this._recipes;
  188. }
  189. addRecipe(id, name, price, ingredients){
  190. let recipe = new this._modules.Recipe(id, name, price, ingredients, this);
  191. this._recipes.push(recipe);
  192. this._modules.recipeBook.isPopulated = false;
  193. }
  194. removeRecipe(recipe){
  195. const index = this._recipes.indexOf(recipe);
  196. if(index === undefined){
  197. return false;
  198. }
  199. this._recipes.splice(index, 1);
  200. this._modules.recipeBook.isPopulated = false;
  201. }
  202. /*
  203. recipe = {
  204. name: required,
  205. price: required,
  206. ingredients: [{
  207. ingredient: id of ingredient,
  208. quantity: quantity of ingredient
  209. }]
  210. }
  211. */
  212. updateRecipe(recipe){
  213. for(let i = 0; i < this._recipes.length; i++){
  214. if(this._recipes[i].id === recipe._id){
  215. this._recipes[i].name = recipe.name;
  216. this._recipes[i].price = recipe.price;
  217. this._recipes[i].removeIngredients();
  218. for(let j = 0; j < recipe.ingredients.length; j++){
  219. for(let k = 0; k < this._ingredients.length; k++){
  220. if(this._ingredients[k].ingredient.id === recipe.ingredients[j].ingredient){
  221. this._recipes[i].addIngredient(
  222. this._ingredients[k].ingredient,
  223. recipe.ingredients[j].quantity
  224. );
  225. break;
  226. }
  227. }
  228. }
  229. break;
  230. }
  231. }
  232. this._modules.recipeBook.isPopulated = false;
  233. }
  234. get transactions(){
  235. return this._transactions;
  236. }
  237. getTransactions(from = 0, to = new Date()){
  238. if(merchant._transactions.length <= 0){
  239. return [];
  240. }
  241. if(from === 0){
  242. from = this._transactions[this._transactions.length-1].date;
  243. }
  244. const {start, end} = this.getTransactionIndices(from, to);
  245. return this._transactions.slice(start, end + 1);
  246. }
  247. addTransaction(transaction){
  248. transaction = new this._modules.Transaction(
  249. transaction._id,
  250. transaction.date,
  251. transaction.recipes,
  252. this
  253. );
  254. this._transactions.push(transaction);
  255. this._transactions.sort((a, b)=>{
  256. if(a.date > b.date){
  257. return -1;
  258. }
  259. return 1;
  260. });
  261. let ingredients = {};
  262. for(let i = 0; i < transaction.recipes.length; i++){
  263. const recipe = transaction.recipes[i];
  264. for(let j = 0; j < recipe.recipe.ingredients.length; j++){
  265. const ingredient = recipe.recipe.ingredients[j];
  266. if(ingredients[ingredient.ingredient.id]){
  267. ingredients[ingredient.ingredient.id] += recipe.quantity * ingredient.quantity;
  268. }else{
  269. ingredients[ingredient.ingredient.id] = recipe.quantity * ingredient.quantity;
  270. }
  271. }
  272. }
  273. const keys = Object.keys(ingredients);
  274. for(let i = 0; i < keys.length; i++){
  275. for(let j = 0; j < this._ingredients.length; j++){
  276. if(keys[i] === this._ingredients[j].ingredient.id){
  277. this._ingredients[j].updateQuantity(-ingredients[keys[i]]);
  278. }
  279. }
  280. }
  281. this._modules.home.isPopulated = false;
  282. this._modules.ingredients.isPopulated = false;
  283. this._modules.analytics.newData = true;
  284. }
  285. removeTransaction(transaction){
  286. const index = this._transactions.indexOf(transaction);
  287. if(index === undefined){
  288. return false;
  289. }
  290. this._transactions.splice(index, 1);
  291. let ingredients = {};
  292. for(let i = 0; i < transaction.recipes.length; i++){
  293. const recipe = transaction.recipes[i];
  294. for(let j = 0; j < recipe.recipe.ingredients.length; j++){
  295. const ingredient = recipe.recipe.ingredients[j];
  296. if(ingredients[ingredient.ingredient.id]){
  297. ingredients[ingredient.ingredient.id] += ingredient.quantity * recipe.quantity;
  298. }else{
  299. ingredients[ingredient.ingredient.id] = ingredient.quantity * recipe.quantity;
  300. }
  301. }
  302. }
  303. const keys = Object.keys(ingredients);
  304. for(let i = 0; i < keys.length; i++){
  305. for(let j = 0; j < this._ingredients.length; j++){
  306. if(keys[i] === this._ingredients[j].ingredient.id){
  307. this._ingredients[j].updateQuantity(ingredients[keys[i]]);
  308. break;
  309. }
  310. }
  311. }
  312. this._modules.home.isPopulated = false;
  313. this._modules.ingredients.isPopulated = false;
  314. this._modules.analytics.newData = true;
  315. }
  316. get orders(){
  317. return this._orders;
  318. }
  319. clearOrders(){
  320. this._orders = [];
  321. }
  322. addOrder(data, isNew = false){
  323. let order = new this._modules.Order(
  324. data._id,
  325. data.name,
  326. data.date,
  327. data.taxes,
  328. data.fees,
  329. data.ingredients,
  330. this
  331. );
  332. this._orders.push(order);
  333. if(isNew){
  334. for(let i = 0; i < order.ingredients.length; i++){
  335. for(let j = 0; j < this._ingredients.length; j++){
  336. if(order.ingredients[i].ingredient === this._ingredients[j].ingredient){
  337. this._ingredients[j].updateQuantity(order.ingredients[i].quantity);
  338. break;
  339. }
  340. }
  341. }
  342. }
  343. this._modules.ingredients.isPopulated = false;
  344. this._modules.orders.isPopulated = false;
  345. }
  346. removeOrder(order){
  347. const index = this._orders.indexOf(order);
  348. if(index === undefined){
  349. return false;
  350. }
  351. this._orders.splice(index, 1);
  352. for(let i = 0; i < order.ingredients.length; i++){
  353. for(let j = 0; j < this._ingredients.length; j++){
  354. if(order.ingredients[i].ingredient === this._ingredients[j].ingredient){
  355. this._ingredients[j].updateQuantity(-order.ingredients[i].quantity);
  356. break;
  357. }
  358. }
  359. }
  360. this._modules.ingredients.isPopulated = false;
  361. this._modules.orders.isPopulated = false;
  362. }
  363. get units(){
  364. return this._units;
  365. }
  366. getRevenue(from, to = new Date()){
  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, to = new Date()){
  391. let recipes = this.getRecipesSold(from, to);
  392. let ingredientList = [];
  393. for(let i = 0; i < recipes.length; i++){
  394. for(let j = 0; j < recipes[i].recipe.ingredients.length; j++){
  395. let exists = false;
  396. for(let k = 0; k < ingredientList.length; k++){
  397. if(ingredientList[k].ingredient === recipes[i].recipe.ingredients[j].ingredient){
  398. exists = true;
  399. ingredientList[k].quantity += recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity;
  400. break;
  401. }
  402. }
  403. if(!exists){
  404. ingredientList.push({
  405. ingredient: recipes[i].recipe.ingredients[j].ingredient,
  406. quantity: recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity
  407. });
  408. }
  409. }
  410. }
  411. return ingredientList;
  412. }
  413. /*
  414. Gets the quantity of a single ingredient sold between two dates
  415. Inputs:
  416. ingredient = MerchantIngredient object to find
  417. from = start Date
  418. to = end Date
  419. return: quantity sold in default unit
  420. */
  421. getSingleIngredientSold(ingredient, from, to = new Date()){
  422. const {start, end} = this.getTransactionIndices(from, to);
  423. let total = 0;
  424. for(let i = start; i < end; i++){
  425. for(let j = 0; j < this._transactions[i].recipes.length; j++){
  426. for(let k = 0; k < this._transactions[i].recipes[j].recipe.ingredients.length; k++){
  427. if(this._transactions[i].recipes[j].recipe.ingredients[k].ingredient === ingredient.ingredient){
  428. total += this._transactions[i].recipes[j].recipe.ingredients[k].quantity;
  429. break;
  430. }
  431. }
  432. }
  433. }
  434. return total;
  435. }
  436. /*
  437. Gets the number of recipes sold between two dates (dateRange)
  438. Inputs:
  439. dateRange: array containing a start date and an end date
  440. Return:
  441. [{
  442. recipe: a recipe object
  443. quantity: quantity of the recipe sold
  444. }]
  445. */
  446. getRecipesSold(from = 0, to = new Date()){
  447. if(from === 0){
  448. from = this._transactions[0].date;
  449. }
  450. const {start, end} = this.getTransactionIndices(from, to);
  451. let recipeList = [];
  452. for(let i = start; i <= end; i++){
  453. for(let j = 0; j < this._transactions[i].recipes.length; j++){
  454. let exists = false;
  455. for(let k = 0; k < recipeList.length; k++){
  456. if(recipeList[k].recipe === this._transactions[i].recipes[j].recipe){
  457. exists = true;
  458. recipeList[k].quantity += this._transactions[i].recipes[j].quantity;
  459. break;
  460. }
  461. }
  462. if(!exists){
  463. recipeList.push({
  464. recipe: this._transactions[i].recipes[j].recipe,
  465. quantity: this._transactions[i].recipes[j].quantity
  466. });
  467. }
  468. }
  469. }
  470. return recipeList;
  471. }
  472. /*
  473. Groups all of the merchant's ingredients by their category
  474. Return: [{
  475. name: category name,
  476. ingredients: [MerchantIngredient Object]
  477. }]
  478. */
  479. categorizeIngredients(){
  480. let ingredientsByCategory = [];
  481. for(let i = 0; i < this.ingredients.length; i++){
  482. let categoryExists = false;
  483. for(let j = 0; j < ingredientsByCategory.length; j++){
  484. if(this.ingredients[i].ingredient.category === ingredientsByCategory[j].name){
  485. ingredientsByCategory[j].ingredients.push(this.ingredients[i]);
  486. categoryExists = true;
  487. break;
  488. }
  489. }
  490. if(!categoryExists){
  491. ingredientsByCategory.push({
  492. name: this.ingredients[i].ingredient.category,
  493. ingredients: [this.ingredients[i]]
  494. });
  495. }
  496. }
  497. return ingredientsByCategory;
  498. }
  499. getRecipesForIngredient(ingredient){
  500. let recipes = [];
  501. for(let i = 0; i < this._recipes.length; i++){
  502. for(let j = 0; j < this._recipes[i].ingredients.length; j++){
  503. if(this._recipes[i].ingredients[j].ingredient === ingredient){
  504. recipes.push(this._recipes[i]);
  505. break;
  506. }
  507. }
  508. }
  509. return recipes;
  510. }
  511. getTransactionIndices(from, to){
  512. let start, end;
  513. for(let i = this._transactions.length - 1; i >= 0; i--){
  514. if(this._transactions[i].date >= from){
  515. end = i;
  516. break;
  517. }
  518. }
  519. for(let i = 0; i < this._transactions.length; i++){
  520. if(this._transactions[i].date < to){
  521. start = i;
  522. break;
  523. }
  524. }
  525. if(end === undefined){
  526. return false;
  527. }
  528. return {start: start, end: end};
  529. }
  530. }
  531. module.exports = Merchant;