Merchant.js 18 KB

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