Merchant.js 17 KB

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