Merchant.js 18 KB

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