Merchant.js 19 KB

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