Merchant.js 18 KB

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