Merchant.js 18 KB

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