Merchant.js 19 KB

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