Merchant.js 20 KB

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