Merchant.js 18 KB

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