Merchant.js 18 KB

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