Merchant.js 21 KB

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