Merchant.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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) return false;
  275. this._recipes.splice(index, 1);
  276. state.updateRecipes();
  277. }
  278. get transactions(){
  279. return this._transactions;
  280. }
  281. getTransactions(from, to){
  282. if(merchant._transactions.length <= 0) return [];
  283. const {start, end} = this.getTransactionIndices(from, to);
  284. return this._transactions.slice(start, end);
  285. }
  286. /*
  287. transactions: [{
  288. _id: String,
  289. date: String (date)
  290. recipes: [{
  291. recipe: String (id)
  292. quantity: Number
  293. }]
  294. }]
  295. */
  296. addTransactions(transactions, isNew = false){
  297. for(let i = 0; i < transactions.length; i++){
  298. let transaction = new Transaction(
  299. transactions[i]._id,
  300. transactions[i].date,
  301. transactions[i].recipes,
  302. this
  303. );
  304. this._transactions.push(transaction);
  305. if(isNew === true){
  306. for(let j = 0; j < transaction.recipes.length; j++){
  307. let recipe = transaction.recipes[j].recipe;
  308. for(let k = 0; k < recipe.ingredients.length; k++){
  309. let ingredient = recipe.ingredients[k].ingredient;
  310. let quantity = transaction.recipes[j].quantity * recipe.ingredients[k].quantity;
  311. this.getIngredient(ingredient.id).updateQuantity(-quantity);
  312. }
  313. }
  314. }
  315. }
  316. this.transactions.sort((a, b) => (a.date > b.date) ? 1 : -1);
  317. }
  318. removeTransaction(transaction){
  319. for(let j = 0; j < transaction.recipes.length; j++){
  320. let recipe = transaction.recipes[j].recipe;
  321. for(let k = 0; k < recipe.ingredients.length; k++){
  322. let ingredient = recipe.ingredients[k].ingredient;
  323. let quantity = transaction.recipes[j].quantity * recipe.ingredients[k].quantity;
  324. this.getIngredient(ingredient.id).updateQuantity(quantity);
  325. }
  326. }
  327. this._transactions.splice(this._transactions.indexOf(transaction), 1);
  328. state.updateTransactions();
  329. }
  330. get orders(){
  331. return this._orders;
  332. }
  333. /*
  334. orders: [{
  335. _id: String,
  336. name: String,
  337. date: String (date)
  338. taxes: Number
  339. fees: Number
  340. ingredients: [{
  341. ingredient: String (id),
  342. pricePerUnit: Number
  343. quantity: Number
  344. }]
  345. }]
  346. */
  347. addOrders(orders, isNew = false){
  348. for(let i = 0; i < orders.length; i++){
  349. let order = new Order(
  350. orders[i]._id,
  351. orders[i].name,
  352. orders[i].date,
  353. orders[i].taxes,
  354. orders[i].fees,
  355. orders[i].ingredients,
  356. this
  357. );
  358. this._orders.push(order);
  359. if(isNew === true){
  360. for(let j = 0; j < order.ingredients.length; j++){
  361. this.getIngredient(order.ingredients[j].ingredient.id).updateQuantity(order.ingredients[j].quantity);
  362. }
  363. }
  364. }
  365. }
  366. removeOrder(order){
  367. const index = this._orders.indexOf(order);
  368. if(index === undefined){
  369. return false;
  370. }
  371. this._orders.splice(index, 1);
  372. for(let i = 0; i < order.ingredients.length; i++){
  373. for(let j = 0; j < this._inventory.length; j++){
  374. if(order.ingredients[i].ingredient === this._inventory[j].ingredient){
  375. this._inventory[j].updateQuantity(-order.ingredients[i].quantity);
  376. break;
  377. }
  378. }
  379. }
  380. }
  381. get units(){
  382. return this._units;
  383. }
  384. get owner(){
  385. return this._owner;
  386. }
  387. getRevenue(from, to = new Date()){
  388. const {start, end} = this.getTransactionIndices(from, to);
  389. let total = 0;
  390. for(let i = start; i < end; i++){
  391. for(let j = 0; j < this._transactions[i].recipes.length; j++){
  392. for(let k = 0; k < this.recipes.length; k++){
  393. if(this._transactions[i].recipes[j].recipe === this.recipes[k]){
  394. total += this._transactions[i].recipes[j].quantity * this.recipes[k].price;
  395. }
  396. }
  397. }
  398. }
  399. return total;
  400. }
  401. /*
  402. Gets the quantity of each ingredient sold between two dates (dateRange)
  403. Inputs:
  404. dateRange: list containing a start date and an end date
  405. Return:
  406. [{
  407. ingredient: Ingredient object,
  408. quantity: quantity of ingredient sold in default unit
  409. }]
  410. */
  411. getIngredientsSold(from, to = new Date()){
  412. let recipes = this.getRecipesSold(from, to);
  413. let ingredientList = [];
  414. for(let i = 0; i < recipes.length; i++){
  415. for(let j = 0; j < recipes[i].recipe.ingredients.length; j++){
  416. let exists = false;
  417. for(let k = 0; k < ingredientList.length; k++){
  418. if(ingredientList[k].ingredient === recipes[i].recipe.ingredients[j].ingredient){
  419. exists = true;
  420. ingredientList[k].quantity += recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity;
  421. break;
  422. }
  423. }
  424. if(!exists){
  425. ingredientList.push({
  426. ingredient: recipes[i].recipe.ingredients[j].ingredient,
  427. quantity: recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity
  428. });
  429. }
  430. }
  431. }
  432. return ingredientList;
  433. }
  434. /*
  435. Gets the number of recipes sold between two dates (dateRange)
  436. Inputs:
  437. dateRange: array containing a start date and an end date
  438. Return:
  439. [{
  440. recipe: a recipe object
  441. quantity: quantity of the recipe sold
  442. }]
  443. */
  444. getRecipesSold(from = 0, to = new Date()){
  445. if(from === 0) from = this._transactions[0].date;
  446. const {start, end} = this.getTransactionIndices(from, to);
  447. let recipeList = [];
  448. for(let i = start; i < end; i++){
  449. for(let j = 0; j < this._transactions[i].recipes.length; j++){
  450. let exists = false;
  451. for(let k = 0; k < recipeList.length; k++){
  452. if(recipeList[k].recipe === this._transactions[i].recipes[j].recipe){
  453. exists = true;
  454. recipeList[k].quantity += this._transactions[i].recipes[j].quantity;
  455. break;
  456. }
  457. }
  458. if(!exists){
  459. recipeList.push({
  460. recipe: this._transactions[i].recipes[j].recipe,
  461. quantity: this._transactions[i].recipes[j].quantity
  462. });
  463. }
  464. }
  465. }
  466. return recipeList;
  467. }
  468. /*
  469. Groups all of the merchant's ingredients by their category
  470. Return: [{
  471. name: category name,
  472. ingredients: [MerchantIngredient Object]
  473. }]
  474. */
  475. categorizeIngredients(){
  476. let ingredientsByCategory = [];
  477. for(let i = 0; i < this._inventory.length; i++){
  478. let categoryExists = false;
  479. for(let j = 0; j < ingredientsByCategory.length; j++){
  480. if(this._inventory[i].ingredient.category === ingredientsByCategory[j].name){
  481. ingredientsByCategory[j].ingredients.push(this._inventory[i]);
  482. categoryExists = true;
  483. break;
  484. }
  485. }
  486. if(!categoryExists){
  487. ingredientsByCategory.push({
  488. name: this._inventory[i].ingredient.category,
  489. ingredients: [this._inventory[i]]
  490. });
  491. }
  492. }
  493. return ingredientsByCategory;
  494. }
  495. getTransactionIndices(from, to){
  496. let start = 0;
  497. let end = 0;
  498. if(
  499. this._transactions.length === 0 ||
  500. from > this._transactions[0].date ||
  501. to >= this._transactions[this._transactions.length-1].date
  502. ){
  503. for(let i = this._transactions.length - 1; i >= 0; i--){
  504. if(this._transactions[i].date > from){
  505. end = i + 1;
  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. }
  516. return {start: start, end: end};
  517. }
  518. }
  519. module.exports = Merchant;