Merchant.js 18 KB

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