Merchant.js 20 KB

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