Merchant.js 18 KB

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