Merchant.js 20 KB

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