Merchant.js 19 KB

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