Merchant.js 17 KB

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