Merchant.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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. ingredientsStrand.populateByProperty();
  193. analyticsStrand.populateButtons();
  194. }
  195. removeIngredient(ingredient){
  196. const index = this._ingredients.indexOf(ingredient);
  197. if(index === undefined){
  198. return false;
  199. }
  200. this._ingredients.splice(index, 1);
  201. homeStrand.drawInventoryCheckCard();
  202. ingredientsStrand.populateByProperty();
  203. analyticsStrand.populateButtons();
  204. }
  205. getIngredient(id){
  206. for(let i = 0; i < this._ingredients.length; i++){
  207. if(this._ingredients[i].ingredient.id === id){
  208. return this._ingredients[i];
  209. }
  210. }
  211. }
  212. get recipes(){
  213. return this._recipes;
  214. }
  215. getRecipe(id){
  216. for(let i = 0; i < this._recipes.length; i++){
  217. if(this._recipes[i].id === id){
  218. return this._recipes[i];
  219. }
  220. }
  221. }
  222. /*
  223. recipes: [{
  224. _id: String
  225. name: String
  226. price: Number
  227. ingredients: [{
  228. ingredient: String (id)
  229. quantity: Number
  230. }]
  231. }]
  232. */
  233. addRecipes(recipes){
  234. for(let i = 0; i < recipes.length; i++){
  235. this._recipes.push(new Recipe(
  236. recipes[i]._id,
  237. recipes[i].name,
  238. recipes[i].price,
  239. recipes[i].ingredients,
  240. this
  241. ));
  242. }
  243. recipeBookStrand.populateRecipes();
  244. analyticsStrand.populateButtons();
  245. }
  246. removeRecipe(recipe){
  247. const index = this._recipes.indexOf(recipe);
  248. if(index === undefined){
  249. return false;
  250. }
  251. this._recipes.splice(index, 1);
  252. recipeBookStrand.populateRecipes();
  253. analyticsStrand.populateButtons();
  254. }
  255. get transactions(){
  256. return this._transactions;
  257. }
  258. getTransactions(from = 0, to = new Date()){
  259. if(merchant._transactions.length <= 0){
  260. return [];
  261. }
  262. if(from === 0){
  263. from = this._transactions[this._transactions.length-1].date;
  264. }
  265. const {start, end} = this.getTransactionIndices(from, to);
  266. return this._transactions.slice(start, end + 1);
  267. }
  268. /*
  269. transactions: [{
  270. _id: String,
  271. date: String (date)
  272. recipes: [{
  273. recipe: String (id)
  274. quantity: Number
  275. }]
  276. }]
  277. */
  278. addTransactions(transactions, isNew = false){
  279. for(let i = 0; i < transactions.length; i++){
  280. let transaction = new Transaction(
  281. transactions[i]._id,
  282. transactions[i].date,
  283. transactions[i].recipes,
  284. this
  285. );
  286. this._transactions.push(transaction);
  287. if(isNew === true){
  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. }
  297. }
  298. this.transactions.sort((a, b) => (a.date > b.date) ? 1 : -1);
  299. homeStrand.isPopulated = false;
  300. ingredientsStrand.populateByProperty();
  301. analyticsStrand.displayIngredient();
  302. analyticsStrand.displayRecipe();
  303. }
  304. removeTransaction(transaction){
  305. for(let j = 0; j < transaction.recipes.length; j++){
  306. let recipe = transaction.recipes[j].recipe;
  307. for(let k = 0; k < recipe.ingredients.length; k++){
  308. let ingredient = recipe.ingredients[k].ingredient;
  309. let quantity = transaction.recipes[j].quantity * recipe.ingredients[k].quantity;
  310. this.getIngredient(ingredient.id).updateQuantity(quantity);
  311. }
  312. }
  313. this._transactions.splice(this._transactions.indexOf(transaction), 1);
  314. homeStrand.isPopulated = false;
  315. ingredientsStrand.populateByProperty();
  316. analyticsStrand.displayIngredient();
  317. analyticsStrand.displayRecipe();
  318. }
  319. get orders(){
  320. return this._orders;
  321. }
  322. clearOrders(){
  323. this._orders = [];
  324. }
  325. /*
  326. orders: [{
  327. _id: String,
  328. name: String,
  329. date: String (date)
  330. taxes: Number
  331. fees: Number
  332. ingredients: [{
  333. ingredient: String (id),
  334. pricePerUnit: Number
  335. quantity: Number
  336. }]
  337. }]
  338. */
  339. addOrders(orders, isNew = false){
  340. for(let i = 0; i < orders.length; i++){
  341. let order = new Order(
  342. orders[i]._id,
  343. orders[i].name,
  344. orders[i].date,
  345. orders[i].taxes,
  346. orders[i].fees,
  347. orders[i].ingredients,
  348. this
  349. );
  350. this._orders.push(order);
  351. if(isNew === true){
  352. for(let j = 0; j < order.ingredients.length; j++){
  353. this.getIngredient(order.ingredients[j].ingredient.id).updateQuantity(order.ingredients[j].quantity);
  354. }
  355. }
  356. }
  357. ingredientsStrand.populateByProperty();
  358. ordersStrand.displayOrders();
  359. }
  360. removeOrder(order){
  361. const index = this._orders.indexOf(order);
  362. if(index === undefined){
  363. return false;
  364. }
  365. this._orders.splice(index, 1);
  366. for(let i = 0; i < order.ingredients.length; i++){
  367. for(let j = 0; j < this._ingredients.length; j++){
  368. if(order.ingredients[i].ingredient === this._ingredients[j].ingredient){
  369. this._ingredients[j].updateQuantity(-order.ingredients[i].quantity);
  370. break;
  371. }
  372. }
  373. }
  374. ingredientsStrand.isPopulated = false;
  375. ordersStrand.isPopulated = false;
  376. }
  377. get units(){
  378. return this._units;
  379. }
  380. getRevenue(from, to = new Date()){
  381. const {start, end} = this.getTransactionIndices(from, to);
  382. let total = 0;
  383. for(let i = start; i <= end; i++){
  384. for(let j = 0; j < this._transactions[i].recipes.length; j++){
  385. for(let k = 0; k < this.recipes.length; k++){
  386. if(this._transactions[i].recipes[j].recipe === this.recipes[k]){
  387. total += this._transactions[i].recipes[j].quantity * this.recipes[k].price;
  388. }
  389. }
  390. }
  391. }
  392. return total;
  393. }
  394. /*
  395. Gets the quantity of each ingredient sold between two dates (dateRange)
  396. Inputs:
  397. dateRange: list containing a start date and an end date
  398. Return:
  399. [{
  400. ingredient: Ingredient object,
  401. quantity: quantity of ingredient sold in default unit
  402. }]
  403. */
  404. getIngredientsSold(from, to = new Date()){
  405. let recipes = this.getRecipesSold(from, to);
  406. let ingredientList = [];
  407. for(let i = 0; i < recipes.length; i++){
  408. for(let j = 0; j < recipes[i].recipe.ingredients.length; j++){
  409. let exists = false;
  410. for(let k = 0; k < ingredientList.length; k++){
  411. if(ingredientList[k].ingredient === recipes[i].recipe.ingredients[j].ingredient){
  412. exists = true;
  413. ingredientList[k].quantity += recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity;
  414. break;
  415. }
  416. }
  417. if(!exists){
  418. ingredientList.push({
  419. ingredient: recipes[i].recipe.ingredients[j].ingredient,
  420. quantity: recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity
  421. });
  422. }
  423. }
  424. }
  425. return ingredientList;
  426. }
  427. /*
  428. Gets the quantity of a single ingredient sold between two dates
  429. Inputs:
  430. ingredient = MerchantIngredient object to find
  431. from = start Date
  432. to = end Date
  433. return: quantity sold in default unit
  434. */
  435. getSingleIngredientSold(ingredient, from, to = new Date()){
  436. const {start, end} = this.getTransactionIndices(from, to);
  437. let total = 0;
  438. for(let i = start; i < end; i++){
  439. for(let j = 0; j < this._transactions[i].recipes.length; j++){
  440. for(let k = 0; k < this._transactions[i].recipes[j].recipe.ingredients.length; k++){
  441. if(this._transactions[i].recipes[j].recipe.ingredients[k].ingredient === ingredient.ingredient){
  442. total += this._transactions[i].recipes[j].recipe.ingredients[k].quantity;
  443. break;
  444. }
  445. }
  446. }
  447. }
  448. return total;
  449. }
  450. /*
  451. Gets the number of recipes sold between two dates (dateRange)
  452. Inputs:
  453. dateRange: array containing a start date and an end date
  454. Return:
  455. [{
  456. recipe: a recipe object
  457. quantity: quantity of the recipe sold
  458. }]
  459. */
  460. getRecipesSold(from = 0, to = new Date()){
  461. if(from === 0){
  462. from = this._transactions[0].date;
  463. }
  464. const {start, end} = this.getTransactionIndices(from, to);
  465. let recipeList = [];
  466. for(let i = start; i <= end; i++){
  467. for(let j = 0; j < this._transactions[i].recipes.length; j++){
  468. let exists = false;
  469. for(let k = 0; k < recipeList.length; k++){
  470. if(recipeList[k].recipe === this._transactions[i].recipes[j].recipe){
  471. exists = true;
  472. recipeList[k].quantity += this._transactions[i].recipes[j].quantity;
  473. break;
  474. }
  475. }
  476. if(!exists){
  477. recipeList.push({
  478. recipe: this._transactions[i].recipes[j].recipe,
  479. quantity: this._transactions[i].recipes[j].quantity
  480. });
  481. }
  482. }
  483. }
  484. return recipeList;
  485. }
  486. /*
  487. Groups all of the merchant's ingredients by their category
  488. Return: [{
  489. name: category name,
  490. ingredients: [MerchantIngredient Object]
  491. }]
  492. */
  493. categorizeIngredients(){
  494. let ingredientsByCategory = [];
  495. for(let i = 0; i < this.ingredients.length; i++){
  496. let categoryExists = false;
  497. for(let j = 0; j < ingredientsByCategory.length; j++){
  498. if(this.ingredients[i].ingredient.category === ingredientsByCategory[j].name){
  499. ingredientsByCategory[j].ingredients.push(this.ingredients[i]);
  500. categoryExists = true;
  501. break;
  502. }
  503. }
  504. if(!categoryExists){
  505. ingredientsByCategory.push({
  506. name: this.ingredients[i].ingredient.category,
  507. ingredients: [this.ingredients[i]]
  508. });
  509. }
  510. }
  511. return ingredientsByCategory;
  512. }
  513. getRecipesForIngredient(ingredient){
  514. let recipes = [];
  515. for(let i = 0; i < this._recipes.length; i++){
  516. for(let j = 0; j < this._recipes[i].ingredients.length; j++){
  517. if(this._recipes[i].ingredients[j].ingredient === ingredient){
  518. recipes.push(this._recipes[i]);
  519. break;
  520. }
  521. }
  522. }
  523. return recipes;
  524. }
  525. getTransactionIndices(from, to){
  526. let start, end;
  527. for(let i = this._transactions.length - 1; i >= 0; i--){
  528. if(this._transactions[i].date >= from){
  529. end = i;
  530. break;
  531. }
  532. }
  533. for(let i = 0; i < this._transactions.length; i++){
  534. if(this._transactions[i].date < to){
  535. start = i;
  536. break;
  537. }
  538. }
  539. if(end === undefined){
  540. return false;
  541. }
  542. return {start: start, end: end};
  543. }
  544. }
  545. module.exports = Merchant;