Merchant.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  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. set quantity(quantity){
  15. this._quantity = quantity;
  16. }
  17. get quantity(){
  18. let convertMultiplier = 1;
  19. switch(controller.getUnitType(this._ingredient.unit)){
  20. case "mass":
  21. convertMultiplier = this._ingredient.convert.toMass;
  22. break;
  23. case "volume":
  24. convertMultiplier = this._ingredient.convert.toVolume;
  25. break;
  26. case "length":
  27. convertMultiplier = this._ingredient.convert.toLength;
  28. break;
  29. case "bottle":
  30. return this._quantity * this._ingredient.convert.toBottle;
  31. }
  32. return this._quantity * controller.unitMultiplier(controller.getBaseUnit(this._ingredient.unit), this._ingredient.unit) * convertMultiplier;
  33. }
  34. /*
  35. Takes in quantity and unit of that quantity and subtracts from the quantity on the ingredient
  36. quantity: Number
  37. unit: String
  38. */
  39. updateQuantity(quantity, unit){
  40. quantity *= controller.unitMultiplier(unit, controller.getBaseUnit(unit))
  41. switch(controller.getUnitType(this._ingredient.unit)){
  42. case "mass": quantity /= this._ingredient.convert.toMass; break;
  43. case "volume": quantity /= this._ingredient.convert.toVolume; break;
  44. case "length": quantity /= this._ingredient.convert.toLength; break;
  45. }
  46. this._quantity += quantity;
  47. }
  48. getQuantityDisplay(){
  49. return `${this.quantity.toFixed(2)} ${this._ingredient.unit.toUpperCase()}`;
  50. }
  51. /*
  52. Gets the quantity of a single ingredient sold between two dates
  53. Inputs:
  54. from = start Date
  55. to = end Date
  56. return: quantity sold in default unit
  57. */
  58. getSoldQuantity(from, to){
  59. let total = 0;
  60. const {start, end} = this._parent.getTransactionIndices(from, to);
  61. for(let i = start; i < end; i++){
  62. total += this._parent.transactions[i].getIngredientQuantity(this._ingredient);
  63. }
  64. return total;
  65. }
  66. }
  67. class Merchant{
  68. constructor(
  69. name,
  70. pos,
  71. ingredients,
  72. recipes,
  73. transactions,
  74. address,
  75. owner,
  76. id
  77. ){
  78. this._name = name;
  79. this._pos = pos;
  80. this._inventory = [];
  81. this._recipes = [];
  82. this._transactions = [];
  83. this._orders = [];
  84. this._address = address;
  85. this._owner = {
  86. id: owner._id,
  87. email: owner.email,
  88. merchants: owner.merchants,
  89. name: owner.name
  90. };
  91. this.id = id;
  92. //populate ingredients
  93. for(let i = 0; i < ingredients.length; i++){
  94. const ingredient = new Ingredient(
  95. ingredients[i].ingredient._id,
  96. ingredients[i].ingredient.name,
  97. ingredients[i].ingredient.category,
  98. ingredients[i].ingredient.unit,
  99. ingredients[i].ingredient.altUnit,
  100. ingredients[i].ingredient.ingredients,
  101. ingredients[i].ingredient.convert,
  102. this
  103. );
  104. const merchantIngredient = new MerchantIngredient(
  105. ingredient,
  106. ingredients[i].quantity,
  107. this
  108. );
  109. this._inventory.push(merchantIngredient);
  110. }
  111. //populate recipes
  112. for(let i = 0; i < recipes.length; i++){
  113. let ingredients = [];
  114. for(let j = 0; j < recipes[i].ingredients.length; j++){
  115. const ingredient = recipes[i].ingredients[j];
  116. for(let k = 0; k < this._inventory.length; k++){
  117. if(ingredient.ingredient === this._inventory[k].ingredient.id){
  118. ingredients.push({
  119. ingredient: this._inventory[k].ingredient.id,
  120. quantity: ingredient.quantity,
  121. unit: ingredient.unit,
  122. baseUnitMultiplier: ingredient.baseUnitMultiplier
  123. });
  124. break;
  125. }
  126. }
  127. }
  128. let newRecipe = new Recipe(
  129. recipes[i]._id,
  130. recipes[i].name,
  131. recipes[i].category,
  132. recipes[i].price,
  133. ingredients,
  134. this,
  135. recipes[i].hidden
  136. );
  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. specialUnit: String || undefined,
  212. }
  213. quantity: Number
  214. defaultUnit: String
  215. }]
  216. */
  217. addIngredients(ingredients){
  218. for(let i = 0; i < ingredients.length; i++){
  219. let ingredient = ingredients[i].ingredient;
  220. let quantity = ingredients[i].quantity;
  221. let unit = ingredients[i].ingredient.unit;
  222. const createdIngredient = new Ingredient(
  223. ingredient._id,
  224. ingredient.name,
  225. ingredient.category,
  226. unit,
  227. ingredients[i].ingredient.altUnit,
  228. ingredient.ingredients,
  229. ingredient.convert,
  230. this
  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.unit = ingredients[i].ingredient.unit;
  248. inventoryItem.ingredient.addIngredients(ingredients[i].ingredient.ingredients);
  249. }
  250. }
  251. getIngredient(id){
  252. for(let i = 0; i < this._inventory.length; i++){
  253. if(this._inventory[i].ingredient.id === id) return this._inventory[i];
  254. }
  255. }
  256. /*
  257. Groups all of the merchant's ingredients by their category
  258. Return: [{
  259. name: category name,
  260. ingredients: [MerchantIngredient Object]
  261. }]
  262. */
  263. categorizeIngredients(){
  264. let ingredientsByCategory = [];
  265. for(let i = 0; i < this._inventory.length; i++){
  266. let categoryExists = false;
  267. for(let j = 0; j < ingredientsByCategory.length; j++){
  268. if(this._inventory[i].ingredient.category === ingredientsByCategory[j].name){
  269. ingredientsByCategory[j].ingredients.push(this._inventory[i]);
  270. categoryExists = true;
  271. break;
  272. }
  273. }
  274. if(!categoryExists){
  275. ingredientsByCategory.push({
  276. name: this._inventory[i].ingredient.category,
  277. ingredients: [this._inventory[i]]
  278. });
  279. }
  280. }
  281. return ingredientsByCategory;
  282. }
  283. get recipes(){
  284. return this._recipes;
  285. }
  286. getRecipe(id){
  287. for(let i = 0; i < this._recipes.length; i++){
  288. if(this._recipes[i].id === id) return this._recipes[i];
  289. }
  290. return new Recipe(
  291. "",
  292. "Deleted Recipe",
  293. "",
  294. 0,
  295. [],
  296. undefined,
  297. true
  298. );
  299. }
  300. /*
  301. recipes: [{
  302. _id: String
  303. name: String
  304. price: Number
  305. ingredients: [{
  306. ingredient: String (id)
  307. quantity: Number
  308. }]
  309. }]
  310. */
  311. addRecipes(recipes){
  312. for(let i = 0; i < recipes.length; i++){
  313. let newRecipe = new Recipe(
  314. recipes[i]._id,
  315. recipes[i].name,
  316. recipes[i].category,
  317. recipes[i].price,
  318. recipes[i].ingredients,
  319. this,
  320. recipes[i].hidden
  321. );
  322. newRecipe.calculateIngredientTotals();
  323. this._recipes.push(newRecipe);
  324. }
  325. }
  326. /*
  327. Updates a single recipe
  328. recipe: Recipe
  329. updates: Object
  330. */
  331. updateRecipe(recipe, updates){
  332. recipe.name = updates.name;
  333. recipe.category = updates.category;
  334. recipe.hidden = updates.category;
  335. recipe.price = updates.price;
  336. recipe.clearIngredients();
  337. for(let i = 0; i < updates.ingredients.length; i++){
  338. newIngredient = this.getIngredient(updates.ingredients[i].ingredient);
  339. recipe.addIngredient(
  340. newIngredient.ingredient,
  341. updates.ingredients[i].quantity,
  342. updates.ingredients[i].unit,
  343. updates.ingredients[i].baseUnitMultiplier
  344. );
  345. }
  346. recipe.calculateIngredientTotals();
  347. }
  348. removeRecipe(recipe){
  349. const index = this._recipes.indexOf(recipe);
  350. if(index === undefined) return false;
  351. this._recipes.splice(index, 1);
  352. }
  353. /*
  354. Groups recipes by their categories
  355. return: [{
  356. name: String,
  357. recipes: [Recipe]
  358. }]
  359. */
  360. categorizeRecipes(){
  361. let categories = [];
  362. for(let i = 0; i < this._recipes.length; i++){
  363. let exists = false;
  364. for(let j = 0; j < categories.length; j++){
  365. if(this._recipes[i].category === categories[j].name){
  366. categories[j].recipes.push(this._recipes[i]);
  367. exists = true;
  368. break;
  369. }
  370. }
  371. if(exists === false){
  372. categories.push({
  373. name: this._recipes[i].category,
  374. recipes: [this._recipes[i]]
  375. });
  376. }
  377. }
  378. return categories;
  379. }
  380. get transactions(){
  381. return this._transactions;
  382. }
  383. getTransactions(from, to){
  384. if(merchant._transactions.length <= 0) return [];
  385. const {start, end} = this.getTransactionIndices(from, to);
  386. return this._transactions.slice(start, end);
  387. }
  388. /*
  389. transactions: [{
  390. _id: String,
  391. date: String (date)
  392. recipes: [{
  393. recipe: String (id)
  394. quantity: Number
  395. }]
  396. }]
  397. */
  398. addTransactions(transactions, isNew = false){
  399. for(let i = 0; i < transactions.length; i++){
  400. let transaction = new Transaction(
  401. transactions[i]._id,
  402. transactions[i].date,
  403. transactions[i].recipes,
  404. this
  405. );
  406. this._transactions.push(transaction);
  407. if(isNew === true){
  408. for(let j = 0; j < transaction.recipes.length; j++){
  409. let recipe = transaction.recipes[j].recipe;
  410. for(let k = 0; k < recipe.ingredients.length; k++){
  411. let ingredient = recipe.ingredients[k].ingredient;
  412. let quantity = transaction.recipes[j].quantity * recipe.ingredients[k].quantity;
  413. this.getIngredient(ingredient.id).updateQuantity(-quantity);
  414. }
  415. }
  416. }
  417. }
  418. this.transactions.sort((a, b) => (a.date > b.date) ? 1 : -1);
  419. }
  420. removeTransaction(transaction){
  421. for(let j = 0; j < transaction.recipes.length; j++){
  422. let recipe = transaction.recipes[j].recipe;
  423. for(let k = 0; k < recipe.ingredients.length; k++){
  424. let ingredient = recipe.ingredients[k].ingredient;
  425. let quantity = transaction.recipes[j].quantity * recipe.ingredients[k].quantity;
  426. this.getIngredient(ingredient.id).updateQuantity(quantity);
  427. }
  428. }
  429. this._transactions.splice(this._transactions.indexOf(transaction), 1);
  430. state.updateTransactions();
  431. }
  432. get orders(){
  433. return this._orders;
  434. }
  435. /*
  436. orders: [{
  437. _id: String,
  438. name: String,
  439. date: String (date)
  440. taxes: Number
  441. fees: Number
  442. ingredients: [{
  443. ingredient: String (id),
  444. pricePerUnit: Number
  445. quantity: Number
  446. }]
  447. }]
  448. */
  449. addOrders(orders, isNew = false){
  450. for(let i = 0; i < orders.length; i++){
  451. let order = new Order(
  452. orders[i]._id,
  453. orders[i].name,
  454. orders[i].date,
  455. orders[i].taxes,
  456. orders[i].fees,
  457. orders[i].ingredients,
  458. this
  459. );
  460. this._orders.push(order);
  461. if(isNew === true){
  462. for(let j = 0; j < order.ingredients.length; j++){
  463. this.getIngredient(order.ingredients[j].ingredient.id).updateQuantity(order.ingredients[j].quantity);
  464. }
  465. }
  466. }
  467. }
  468. removeOrder(order){
  469. const index = this._orders.indexOf(order);
  470. if(index === undefined){
  471. return false;
  472. }
  473. this._orders.splice(index, 1);
  474. for(let i = 0; i < order.ingredients.length; i++){
  475. for(let j = 0; j < this._inventory.length; j++){
  476. if(order.ingredients[i].ingredient === this._inventory[j].ingredient){
  477. this._inventory[j].updateQuantity(-order.ingredients[i].quantity);
  478. break;
  479. }
  480. }
  481. }
  482. }
  483. get units(){
  484. return this._units;
  485. }
  486. get owner(){
  487. return this._owner;
  488. }
  489. getRevenue(from, to = new Date()){
  490. const {start, end} = this.getTransactionIndices(from, to);
  491. let total = 0;
  492. for(let i = start; i < end; i++){
  493. for(let j = 0; j < this._transactions[i].recipes.length; j++){
  494. for(let k = 0; k < this.recipes.length; k++){
  495. if(this._transactions[i].recipes[j].recipe === this.recipes[k]){
  496. total += this._transactions[i].recipes[j].quantity * this.recipes[k].price;
  497. }
  498. }
  499. }
  500. }
  501. return total;
  502. }
  503. /*
  504. Gets the quantity of each ingredient sold between two dates (dateRange)
  505. Inputs:
  506. dateRange: list containing a start date and an end date
  507. Return:
  508. [{
  509. ingredient: Ingredient object,
  510. quantity: quantity of ingredient sold in default unit
  511. }]
  512. */
  513. getIngredientsSold(from, to = new Date()){
  514. let recipes = this.getRecipesSold(from, to);
  515. let ingredientList = [];
  516. for(let i = 0; i < recipes.length; i++){
  517. for(let j = 0; j < recipes[i].recipe.ingredients.length; j++){
  518. let exists = false;
  519. for(let k = 0; k < ingredientList.length; k++){
  520. if(ingredientList[k].ingredient === recipes[i].recipe.ingredients[j].ingredient){
  521. exists = true;
  522. ingredientList[k].quantity += recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity;
  523. break;
  524. }
  525. }
  526. if(!exists){
  527. ingredientList.push({
  528. ingredient: recipes[i].recipe.ingredients[j].ingredient,
  529. quantity: recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity
  530. });
  531. }
  532. }
  533. }
  534. return ingredientList;
  535. }
  536. /*
  537. Gets the number of recipes sold between two dates (dateRange)
  538. Inputs:
  539. dateRange: array containing a start date and an end date
  540. Return:
  541. [{
  542. recipe: a recipe object
  543. quantity: quantity of the recipe sold
  544. }]
  545. */
  546. getRecipesSold(from = 0, to = new Date()){
  547. if(from === 0) from = this._transactions[0].date;
  548. const {start, end} = this.getTransactionIndices(from, to);
  549. let recipeList = [];
  550. for(let i = start; i < end; i++){
  551. for(let j = 0; j < this._transactions[i].recipes.length; j++){
  552. let exists = false;
  553. for(let k = 0; k < recipeList.length; k++){
  554. if(recipeList[k].recipe === this._transactions[i].recipes[j].recipe){
  555. exists = true;
  556. recipeList[k].quantity += this._transactions[i].recipes[j].quantity;
  557. break;
  558. }
  559. }
  560. if(!exists){
  561. recipeList.push({
  562. recipe: this._transactions[i].recipes[j].recipe,
  563. quantity: this._transactions[i].recipes[j].quantity
  564. });
  565. }
  566. }
  567. }
  568. return recipeList;
  569. }
  570. getTransactionIndices(from, to){
  571. let start = 0;
  572. let end = 0;
  573. if(
  574. this._transactions.length === 0 ||
  575. from > this._transactions[0].date ||
  576. to >= this._transactions[this._transactions.length-1].date
  577. ){
  578. for(let i = this._transactions.length - 1; i >= 0; i--){
  579. if(this._transactions[i].date > from){
  580. end = i + 1;
  581. break;
  582. }
  583. }
  584. for(let i = 0; i < this._transactions.length; i++){
  585. if(this._transactions[i].date <= to){
  586. start = i;
  587. break;
  588. }
  589. }
  590. }
  591. return {start: start, end: end};
  592. }
  593. }
  594. module.exports = Merchant;