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