Merchant.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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].price,
  126. ingredients,
  127. this,
  128. recipes[i].hidden
  129. );
  130. newRecipe.calculateIngredientTotals();
  131. this._recipes.push(newRecipe);
  132. }
  133. //populate transactions
  134. for(let i = 0; i < transactions.length; i++){
  135. this._transactions.push(new Transaction(
  136. transactions[i]._id,
  137. transactions[i].date,
  138. transactions[i].recipes,
  139. this
  140. ));
  141. }
  142. //populate orders
  143. let from = new Date();
  144. from.setDate(from.getDate() - 30);
  145. let data = {
  146. from: from,
  147. to: new Date(),
  148. ingredients: []
  149. };
  150. let loader = document.getElementById("loaderContainer");
  151. loader.style.display = "flex";
  152. fetch("/orders/get", {
  153. method: "post",
  154. headers: {
  155. "Content-Type": "application/json"
  156. },
  157. body: JSON.stringify(data)
  158. })
  159. .then(response => response.json())
  160. .then((response)=>{
  161. if(typeof(response) === "string"){
  162. controller.createBanner(response, "error");
  163. }else{
  164. this.addOrders(response);
  165. state.updateOrders(this._orders);
  166. }
  167. })
  168. .catch((err)=>{
  169. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  170. })
  171. .finally(()=>{
  172. loader.style.display = "none";
  173. });
  174. }
  175. get name(){
  176. return this._name;
  177. }
  178. set name(name){
  179. this._name = name;
  180. }
  181. get email(){
  182. return this._email;
  183. }
  184. set email(email){
  185. this._email = email;
  186. }
  187. get pos(){
  188. return this._pos;
  189. }
  190. get inventory(){
  191. return this._inventory;
  192. }
  193. get address(){
  194. return this._address;
  195. }
  196. set address(address){
  197. this._address = address;
  198. }
  199. /*
  200. ingredient: [{
  201. ingredient: {
  202. _id: String,
  203. name: String,
  204. category: String,
  205. unitType: String,
  206. specialUnit: String || undefined,
  207. unitSize: Number || undefined
  208. }
  209. quantity: Number
  210. defaultUnit: String
  211. }]
  212. */
  213. addIngredients(ingredients){
  214. for(let i = 0; i < ingredients.length; i++){
  215. let ingredient = ingredients[i].ingredient;
  216. let quantity = ingredients[i].quantity;
  217. let defaultUnit = ingredients[i].defaultUnit;
  218. const createdIngredient = new Ingredient(
  219. ingredient._id,
  220. ingredient.name,
  221. ingredient.category,
  222. ingredient.unitType,
  223. defaultUnit,
  224. this,
  225. ingredient.unitSize,
  226. );
  227. createdIngredient.replaceIngredients(ingredient.ingredients);
  228. const merchantIngredient = new MerchantIngredient(createdIngredient, quantity, this);
  229. this._inventory.push(merchantIngredient);
  230. }
  231. }
  232. removeIngredient(ingredient){
  233. const index = this._inventory.indexOf(ingredient);
  234. if(index === undefined) return false;
  235. this._inventory.splice(index, 1);
  236. }
  237. updateIngredients(ingredients){
  238. for(let i = 0; i < ingredients.length; i++){
  239. let inventoryItem = this.getIngredient(ingredients[i].ingredient._id);
  240. inventoryItem.quantity = ingredients[i].quantity;
  241. inventoryItem.ingredient.id = ingredients[i].ingredient._id;
  242. inventoryItem.ingredient.name = ingredients[i].ingredient.name;
  243. inventoryItem.ingredient.unitType = ingredients[i].ingredient.unitType;
  244. inventoryItem.ingredient.unit = ingredients[i].defaultUnit;
  245. inventoryItem.ingredient.unitSize = ingredients[i].ingredient.unitSize;
  246. inventoryItem.ingredient.addIngredients(ingredients[i].ingredient.ingredients);
  247. }
  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. let newRecipe = new Recipe(
  276. recipes[i]._id,
  277. recipes[i].name,
  278. recipes[i].price,
  279. recipes[i].ingredients,
  280. this,
  281. recipes[i].hidden
  282. );
  283. newRecipe.calculateIngredientTotals();
  284. this._recipes.push(newRecipe);
  285. }
  286. }
  287. removeRecipe(recipe){
  288. const index = this._recipes.indexOf(recipe);
  289. if(index === undefined) return false;
  290. this._recipes.splice(index, 1);
  291. state.updateRecipes();
  292. }
  293. get transactions(){
  294. return this._transactions;
  295. }
  296. getTransactions(from, to){
  297. if(merchant._transactions.length <= 0) return [];
  298. const {start, end} = this.getTransactionIndices(from, to);
  299. return this._transactions.slice(start, end);
  300. }
  301. /*
  302. transactions: [{
  303. _id: String,
  304. date: String (date)
  305. recipes: [{
  306. recipe: String (id)
  307. quantity: Number
  308. }]
  309. }]
  310. */
  311. addTransactions(transactions, isNew = false){
  312. for(let i = 0; i < transactions.length; i++){
  313. let transaction = new Transaction(
  314. transactions[i]._id,
  315. transactions[i].date,
  316. transactions[i].recipes,
  317. this
  318. );
  319. this._transactions.push(transaction);
  320. if(isNew === true){
  321. for(let j = 0; j < transaction.recipes.length; j++){
  322. let recipe = transaction.recipes[j].recipe;
  323. for(let k = 0; k < recipe.ingredients.length; k++){
  324. let ingredient = recipe.ingredients[k].ingredient;
  325. let quantity = transaction.recipes[j].quantity * recipe.ingredients[k].quantity;
  326. this.getIngredient(ingredient.id).updateQuantity(-quantity);
  327. }
  328. }
  329. }
  330. }
  331. this.transactions.sort((a, b) => (a.date > b.date) ? 1 : -1);
  332. }
  333. removeTransaction(transaction){
  334. for(let j = 0; j < transaction.recipes.length; j++){
  335. let recipe = transaction.recipes[j].recipe;
  336. for(let k = 0; k < recipe.ingredients.length; k++){
  337. let ingredient = recipe.ingredients[k].ingredient;
  338. let quantity = transaction.recipes[j].quantity * recipe.ingredients[k].quantity;
  339. this.getIngredient(ingredient.id).updateQuantity(quantity);
  340. }
  341. }
  342. this._transactions.splice(this._transactions.indexOf(transaction), 1);
  343. state.updateTransactions();
  344. }
  345. get orders(){
  346. return this._orders;
  347. }
  348. /*
  349. orders: [{
  350. _id: String,
  351. name: String,
  352. date: String (date)
  353. taxes: Number
  354. fees: Number
  355. ingredients: [{
  356. ingredient: String (id),
  357. pricePerUnit: Number
  358. quantity: Number
  359. }]
  360. }]
  361. */
  362. addOrders(orders, isNew = false){
  363. for(let i = 0; i < orders.length; i++){
  364. let order = new Order(
  365. orders[i]._id,
  366. orders[i].name,
  367. orders[i].date,
  368. orders[i].taxes,
  369. orders[i].fees,
  370. orders[i].ingredients,
  371. this
  372. );
  373. this._orders.push(order);
  374. if(isNew === true){
  375. for(let j = 0; j < order.ingredients.length; j++){
  376. this.getIngredient(order.ingredients[j].ingredient.id).updateQuantity(order.ingredients[j].quantity);
  377. }
  378. }
  379. }
  380. }
  381. removeOrder(order){
  382. const index = this._orders.indexOf(order);
  383. if(index === undefined){
  384. return false;
  385. }
  386. this._orders.splice(index, 1);
  387. for(let i = 0; i < order.ingredients.length; i++){
  388. for(let j = 0; j < this._inventory.length; j++){
  389. if(order.ingredients[i].ingredient === this._inventory[j].ingredient){
  390. this._inventory[j].updateQuantity(-order.ingredients[i].quantity);
  391. break;
  392. }
  393. }
  394. }
  395. }
  396. get units(){
  397. return this._units;
  398. }
  399. get owner(){
  400. return this._owner;
  401. }
  402. getRevenue(from, to = new Date()){
  403. const {start, end} = this.getTransactionIndices(from, to);
  404. let total = 0;
  405. for(let i = start; i < end; i++){
  406. for(let j = 0; j < this._transactions[i].recipes.length; j++){
  407. for(let k = 0; k < this.recipes.length; k++){
  408. if(this._transactions[i].recipes[j].recipe === this.recipes[k]){
  409. total += this._transactions[i].recipes[j].quantity * this.recipes[k].price;
  410. }
  411. }
  412. }
  413. }
  414. return total;
  415. }
  416. /*
  417. Gets the quantity of each ingredient sold between two dates (dateRange)
  418. Inputs:
  419. dateRange: list containing a start date and an end date
  420. Return:
  421. [{
  422. ingredient: Ingredient object,
  423. quantity: quantity of ingredient sold in default unit
  424. }]
  425. */
  426. getIngredientsSold(from, to = new Date()){
  427. let recipes = this.getRecipesSold(from, to);
  428. let ingredientList = [];
  429. for(let i = 0; i < recipes.length; i++){
  430. for(let j = 0; j < recipes[i].recipe.ingredients.length; j++){
  431. let exists = false;
  432. for(let k = 0; k < ingredientList.length; k++){
  433. if(ingredientList[k].ingredient === recipes[i].recipe.ingredients[j].ingredient){
  434. exists = true;
  435. ingredientList[k].quantity += recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity;
  436. break;
  437. }
  438. }
  439. if(!exists){
  440. ingredientList.push({
  441. ingredient: recipes[i].recipe.ingredients[j].ingredient,
  442. quantity: recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity
  443. });
  444. }
  445. }
  446. }
  447. return ingredientList;
  448. }
  449. /*
  450. Gets the number of recipes sold between two dates (dateRange)
  451. Inputs:
  452. dateRange: array containing a start date and an end date
  453. Return:
  454. [{
  455. recipe: a recipe object
  456. quantity: quantity of the recipe sold
  457. }]
  458. */
  459. getRecipesSold(from = 0, to = new Date()){
  460. if(from === 0) from = this._transactions[0].date;
  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 = 0;
  512. let end = 0;
  513. if(
  514. this._transactions.length === 0 ||
  515. from > this._transactions[0].date ||
  516. to >= this._transactions[this._transactions.length-1].date
  517. ){
  518. for(let i = this._transactions.length - 1; i >= 0; i--){
  519. if(this._transactions[i].date > from){
  520. end = i + 1;
  521. break;
  522. }
  523. }
  524. for(let i = 0; i < this._transactions.length; i++){
  525. if(this._transactions[i].date <= to){
  526. start = i;
  527. break;
  528. }
  529. }
  530. }
  531. return {start: start, end: end};
  532. }
  533. }
  534. module.exports = Merchant;