Merchant.js 20 KB

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