Merchant.js 20 KB

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