Merchant.js 20 KB

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