Merchant.js 17 KB

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