Merchant.js 20 KB

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