Merchant.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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. controller.createBanner("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE", "error");
  165. })
  166. .finally(()=>{
  167. loader.style.display = "none";
  168. });
  169. }
  170. get name(){
  171. return this._name;
  172. }
  173. set name(name){
  174. this._name = name;
  175. }
  176. get email(){
  177. return this._email;
  178. }
  179. set email(email){
  180. this._email = email;
  181. }
  182. get pos(){
  183. return this._pos;
  184. }
  185. get inventory(){
  186. return this._inventory;
  187. }
  188. get address(){
  189. return this._address;
  190. }
  191. set address(address){
  192. this._address = address;
  193. }
  194. /*
  195. ingredient: [{
  196. ingredient: {
  197. _id: String,
  198. name: String,
  199. category: String,
  200. unitType: String,
  201. specialUnit: String || undefined,
  202. unitSize: Number || undefined
  203. }
  204. quantity: Number
  205. defaultUnit: String
  206. }]
  207. */
  208. addIngredients(ingredients){
  209. for(let i = 0; i < ingredients.length; i++){
  210. let ingredient = ingredients[i].ingredient;
  211. let quantity = ingredients[i].quantity;
  212. let defaultUnit = ingredients[i].defaultUnit;
  213. const createdIngredient = new Ingredient(
  214. ingredient._id,
  215. ingredient.name,
  216. ingredient.category,
  217. ingredient.unitType,
  218. defaultUnit,
  219. this,
  220. ingredient.unitSize,
  221. );
  222. createdIngredient.replaceIngredients(ingredient.ingredients);
  223. const merchantIngredient = new MerchantIngredient(createdIngredient, quantity, this);
  224. this._inventory.push(merchantIngredient);
  225. }
  226. }
  227. removeIngredient(ingredient){
  228. const index = this._inventory.indexOf(ingredient);
  229. if(index === undefined) return false;
  230. this._inventory.splice(index, 1);
  231. }
  232. getIngredient(id){
  233. for(let i = 0; i < this._inventory.length; i++){
  234. if(this._inventory[i].ingredient.id === id) return this._inventory[i];
  235. }
  236. }
  237. get recipes(){
  238. return this._recipes;
  239. }
  240. getRecipe(id){
  241. for(let i = 0; i < this._recipes.length; i++){
  242. if(this._recipes[i].id === id) return this._recipes[i];
  243. }
  244. }
  245. /*
  246. recipes: [{
  247. _id: String
  248. name: String
  249. price: Number
  250. ingredients: [{
  251. ingredient: String (id)
  252. quantity: Number
  253. }]
  254. }]
  255. */
  256. addRecipes(recipes){
  257. for(let i = 0; i < recipes.length; i++){
  258. this._recipes.push(new Recipe(
  259. recipes[i]._id,
  260. recipes[i].name,
  261. recipes[i].price,
  262. recipes[i].ingredients,
  263. this,
  264. recipes[i].hidden
  265. ));
  266. }
  267. }
  268. removeRecipe(recipe){
  269. const index = this._recipes.indexOf(recipe);
  270. if(index === undefined){
  271. return false;
  272. }
  273. this._recipes.splice(index, 1);
  274. state.updateRecipes();
  275. }
  276. get transactions(){
  277. return this._transactions;
  278. }
  279. getTransactions(from = 0, to = new Date()){
  280. if(merchant._transactions.length <= 0){
  281. return [];
  282. }
  283. if(from === 0) from = this._transactions[this._transactions.length-1].date;
  284. const {start, end} = this.getTransactionIndices(from, to);
  285. return this._transactions.slice(start, end + 1);
  286. }
  287. /*
  288. transactions: [{
  289. _id: String,
  290. date: String (date)
  291. recipes: [{
  292. recipe: String (id)
  293. quantity: Number
  294. }]
  295. }]
  296. */
  297. addTransactions(transactions, isNew = false){
  298. for(let i = 0; i < transactions.length; i++){
  299. let transaction = new Transaction(
  300. transactions[i]._id,
  301. transactions[i].date,
  302. transactions[i].recipes,
  303. this
  304. );
  305. this._transactions.push(transaction);
  306. if(isNew === true){
  307. for(let j = 0; j < transaction.recipes.length; j++){
  308. let recipe = transaction.recipes[j].recipe;
  309. for(let k = 0; k < recipe.ingredients.length; k++){
  310. let ingredient = recipe.ingredients[k].ingredient;
  311. let quantity = transaction.recipes[j].quantity * recipe.ingredients[k].quantity;
  312. this.getIngredient(ingredient.id).updateQuantity(-quantity);
  313. }
  314. }
  315. }
  316. }
  317. this.transactions.sort((a, b) => (a.date > b.date) ? 1 : -1);
  318. }
  319. removeTransaction(transaction){
  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. this._transactions.splice(this._transactions.indexOf(transaction), 1);
  329. state.updateTransactions();
  330. }
  331. get orders(){
  332. return this._orders;
  333. }
  334. /*
  335. orders: [{
  336. _id: String,
  337. name: String,
  338. date: String (date)
  339. taxes: Number
  340. fees: Number
  341. ingredients: [{
  342. ingredient: String (id),
  343. pricePerUnit: Number
  344. quantity: Number
  345. }]
  346. }]
  347. */
  348. addOrders(orders, isNew = false){
  349. for(let i = 0; i < orders.length; i++){
  350. let order = new Order(
  351. orders[i]._id,
  352. orders[i].name,
  353. orders[i].date,
  354. orders[i].taxes,
  355. orders[i].fees,
  356. orders[i].ingredients,
  357. this
  358. );
  359. this._orders.push(order);
  360. if(isNew === true){
  361. for(let j = 0; j < order.ingredients.length; j++){
  362. this.getIngredient(order.ingredients[j].ingredient.id).updateQuantity(order.ingredients[j].quantity);
  363. }
  364. }
  365. }
  366. }
  367. removeOrder(order){
  368. const index = this._orders.indexOf(order);
  369. if(index === undefined){
  370. return false;
  371. }
  372. this._orders.splice(index, 1);
  373. for(let i = 0; i < order.ingredients.length; i++){
  374. for(let j = 0; j < this._inventory.length; j++){
  375. if(order.ingredients[i].ingredient === this._inventory[j].ingredient){
  376. this._inventory[j].updateQuantity(-order.ingredients[i].quantity);
  377. break;
  378. }
  379. }
  380. }
  381. }
  382. get units(){
  383. return this._units;
  384. }
  385. get owner(){
  386. return this._owner;
  387. }
  388. getRevenue(from, to = new Date()){
  389. const {start, end} = this.getTransactionIndices(from, to);
  390. let total = 0;
  391. for(let i = start; i <= end; i++){
  392. for(let j = 0; j < this._transactions[i].recipes.length; j++){
  393. for(let k = 0; k < this.recipes.length; k++){
  394. if(this._transactions[i].recipes[j].recipe === this.recipes[k]){
  395. total += this._transactions[i].recipes[j].quantity * this.recipes[k].price;
  396. }
  397. }
  398. }
  399. }
  400. return total;
  401. }
  402. /*
  403. Gets the quantity of each ingredient sold between two dates (dateRange)
  404. Inputs:
  405. dateRange: list containing a start date and an end date
  406. Return:
  407. [{
  408. ingredient: Ingredient object,
  409. quantity: quantity of ingredient sold in default unit
  410. }]
  411. */
  412. getIngredientsSold(from, to = new Date()){
  413. let recipes = this.getRecipesSold(from, to);
  414. let ingredientList = [];
  415. for(let i = 0; i < recipes.length; i++){
  416. for(let j = 0; j < recipes[i].recipe.ingredients.length; j++){
  417. let exists = false;
  418. for(let k = 0; k < ingredientList.length; k++){
  419. if(ingredientList[k].ingredient === recipes[i].recipe.ingredients[j].ingredient){
  420. exists = true;
  421. ingredientList[k].quantity += recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity;
  422. break;
  423. }
  424. }
  425. if(!exists){
  426. ingredientList.push({
  427. ingredient: recipes[i].recipe.ingredients[j].ingredient,
  428. quantity: recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity
  429. });
  430. }
  431. }
  432. }
  433. return ingredientList;
  434. }
  435. /*
  436. Gets the number of recipes sold between two dates (dateRange)
  437. Inputs:
  438. dateRange: array containing a start date and an end date
  439. Return:
  440. [{
  441. recipe: a recipe object
  442. quantity: quantity of the recipe sold
  443. }]
  444. */
  445. getRecipesSold(from = 0, to = new Date()){
  446. if(from === 0) from = this._transactions[0].date;
  447. const {start, end} = this.getTransactionIndices(from, to);
  448. let recipeList = [];
  449. for(let i = start; i <= end; i++){
  450. for(let j = 0; j < this._transactions[i].recipes.length; j++){
  451. let exists = false;
  452. for(let k = 0; k < recipeList.length; k++){
  453. if(recipeList[k].recipe === this._transactions[i].recipes[j].recipe){
  454. exists = true;
  455. recipeList[k].quantity += this._transactions[i].recipes[j].quantity;
  456. break;
  457. }
  458. }
  459. if(!exists){
  460. recipeList.push({
  461. recipe: this._transactions[i].recipes[j].recipe,
  462. quantity: this._transactions[i].recipes[j].quantity
  463. });
  464. }
  465. }
  466. }
  467. return recipeList;
  468. }
  469. /*
  470. Groups all of the merchant's ingredients by their category
  471. Return: [{
  472. name: category name,
  473. ingredients: [MerchantIngredient Object]
  474. }]
  475. */
  476. categorizeIngredients(){
  477. let ingredientsByCategory = [];
  478. for(let i = 0; i < this._inventory.length; i++){
  479. let categoryExists = false;
  480. for(let j = 0; j < ingredientsByCategory.length; j++){
  481. if(this._inventory[i].ingredient.category === ingredientsByCategory[j].name){
  482. ingredientsByCategory[j].ingredients.push(this._inventory[i]);
  483. categoryExists = true;
  484. break;
  485. }
  486. }
  487. if(!categoryExists){
  488. ingredientsByCategory.push({
  489. name: this._inventory[i].ingredient.category,
  490. ingredients: [this._inventory[i]]
  491. });
  492. }
  493. }
  494. return ingredientsByCategory;
  495. }
  496. getTransactionIndices(from, to){
  497. let start, end;
  498. for(let i = this._transactions.length - 1; i >= 0; i--){
  499. if(this._transactions[i].date >= from){
  500. end = i;
  501. break;
  502. }
  503. }
  504. for(let i = 0; i < this._transactions.length; i++){
  505. if(this._transactions[i].date < to){
  506. start = i;
  507. break;
  508. }
  509. }
  510. if(end === undefined) return false;
  511. return {start: start, end: end};
  512. }
  513. }
  514. module.exports = Merchant;