Merchant.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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.addIngredients(ingredients[i].ingredient.ingredients);
  231. }
  232. }
  233. getIngredient(id){
  234. for(let i = 0; i < this.inventory.length; i++){
  235. if(this.inventory[i].ingredient.id === id) return this.inventory[i];
  236. }
  237. }
  238. /*
  239. Groups all of the merchant's ingredients by their category
  240. Return: [{
  241. name: category name,
  242. ingredients: [MerchantIngredient Object]
  243. }]
  244. */
  245. categorizeIngredients(){
  246. let ingredientsByCategory = [];
  247. for(let i = 0; i < this.inventory.length; i++){
  248. let categoryExists = false;
  249. for(let j = 0; j < ingredientsByCategory.length; j++){
  250. if(this.inventory[i].ingredient.category === ingredientsByCategory[j].name){
  251. ingredientsByCategory[j].ingredients.push(this.inventory[i]);
  252. categoryExists = true;
  253. break;
  254. }
  255. }
  256. if(!categoryExists){
  257. ingredientsByCategory.push({
  258. name: this.inventory[i].ingredient.category,
  259. ingredients: [this.inventory[i]]
  260. });
  261. }
  262. }
  263. return ingredientsByCategory;
  264. }
  265. getRecipe(id){
  266. for(let i = 0; i < this.recipes.length; i++){
  267. if(this.recipes[i].id === id) return this.recipes[i];
  268. }
  269. return new Recipe(
  270. "",
  271. "Deleted Recipe",
  272. "",
  273. 0,
  274. [],
  275. undefined,
  276. true
  277. );
  278. }
  279. /*
  280. recipes: [{
  281. _id: String
  282. name: String
  283. price: Number
  284. ingredients: [{
  285. ingredient: String (id)
  286. quantity: Number
  287. }]
  288. }]
  289. */
  290. addRecipes(recipes){
  291. for(let i = 0; i < recipes.length; i++){
  292. let newRecipe = new Recipe(
  293. recipes[i]._id,
  294. recipes[i].name,
  295. recipes[i].category,
  296. recipes[i].price,
  297. recipes[i].ingredients,
  298. this,
  299. recipes[i].hidden
  300. );
  301. newRecipe.calculateIngredientTotals();
  302. this.recipes.push(newRecipe);
  303. }
  304. }
  305. /*
  306. Updates a single recipe
  307. recipe: Recipe
  308. updates: Object
  309. */
  310. updateRecipe(recipe, updates){
  311. recipe.name = updates.name;
  312. recipe.category = updates.category;
  313. recipe.hidden = updates.category;
  314. recipe.price = updates.price;
  315. recipe.clearIngredients();
  316. for(let i = 0; i < updates.ingredients.length; i++){
  317. newIngredient = this.getIngredient(updates.ingredients[i].ingredient);
  318. recipe.addIngredient(
  319. newIngredient.ingredient,
  320. updates.ingredients[i].quantity,
  321. updates.ingredients[i].unit,
  322. updates.ingredients[i].baseUnitMultiplier
  323. );
  324. }
  325. recipe.calculateIngredientTotals();
  326. }
  327. removeRecipe(recipe){
  328. const index = this.recipes.indexOf(recipe);
  329. if(index === undefined) return false;
  330. this.recipes.splice(index, 1);
  331. }
  332. /*
  333. Groups recipes by their categories
  334. return: [{
  335. name: String,
  336. recipes: [Recipe]
  337. }]
  338. */
  339. categorizeRecipes(){
  340. let categories = [];
  341. for(let i = 0; i < this.recipes.length; i++){
  342. let exists = false;
  343. for(let j = 0; j < categories.length; j++){
  344. if(this.recipes[i].category === categories[j].name){
  345. categories[j].recipes.push(this.recipes[i]);
  346. exists = true;
  347. break;
  348. }
  349. }
  350. if(exists === false){
  351. categories.push({
  352. name: this.recipes[i].category,
  353. recipes: [this.recipes[i]]
  354. });
  355. }
  356. }
  357. return categories;
  358. }
  359. getTransactions(from, to){
  360. if(merchant.transactions.length <= 0) return [];
  361. const {start, end} = this.getTransactionIndices(from, to);
  362. return this.transactions.slice(start, end);
  363. }
  364. /*
  365. transactions: [{
  366. _id: String,
  367. date: String (date)
  368. recipes: [{
  369. recipe: String (id)
  370. quantity: Number
  371. }]
  372. }]
  373. */
  374. addTransactions(transactions, isNew = false){
  375. for(let i = 0; i < transactions.length; i++){
  376. let transaction = new Transaction(
  377. transactions[i]._id,
  378. transactions[i].date,
  379. transactions[i].recipes,
  380. this
  381. );
  382. this.transactions.push(transaction);
  383. if(isNew === true){
  384. for(let j = 0; j < transaction.recipes.length; j++){
  385. let recipe = transaction.recipes[j].recipe;
  386. for(let k = 0; k < recipe.ingredients.length; k++){
  387. let ingredient = recipe.ingredients[k].ingredient;
  388. let quantity = transaction.recipes[j].quantity * recipe.ingredients[k]._quantity;
  389. this.getIngredient(ingredient.id).updateQuantity(-quantity);
  390. }
  391. }
  392. }
  393. }
  394. this.transactions.sort((a, b) => (a.date > b.date) ? 1 : -1);
  395. }
  396. removeTransaction(transaction){
  397. for(let j = 0; j < transaction.recipes.length; j++){
  398. let recipe = transaction.recipes[j].recipe;
  399. for(let k = 0; k < recipe.ingredients.length; k++){
  400. let ingredient = recipe.ingredients[k].ingredient;
  401. let quantity = transaction.recipes[j].quantity * recipe.ingredients[k].quantity;
  402. this.getIngredient(ingredient.id).updateQuantity(quantity);
  403. }
  404. }
  405. this.transactions.splice(this.transactions.indexOf(transaction), 1);
  406. state.updateTransactions();
  407. }
  408. /*
  409. orders: [{
  410. _id: String,
  411. name: String,
  412. date: String (date)
  413. taxes: Number
  414. fees: Number
  415. ingredients: [{
  416. ingredient: String (id),
  417. pricePerUnit: Number
  418. quantity: Number
  419. }]
  420. }]
  421. */
  422. addOrders(orders, isNew = false){
  423. for(let i = 0; i < orders.length; i++){
  424. let order = new Order(
  425. orders[i]._id,
  426. orders[i].name,
  427. orders[i].date,
  428. orders[i].taxes,
  429. orders[i].fees,
  430. orders[i].ingredients,
  431. this
  432. );
  433. this.orders.push(order);
  434. if(isNew === true){
  435. for(let j = 0; j < order.ingredients.length; j++){
  436. this.getIngredient(order.ingredients[j].ingredient.id).updateQuantity(order.ingredients[j].quantity);
  437. }
  438. }
  439. }
  440. }
  441. removeOrder(order){
  442. const index = this.orders.indexOf(order);
  443. if(index === undefined){
  444. return false;
  445. }
  446. this.orders.splice(index, 1);
  447. for(let i = 0; i < order.ingredients.length; i++){
  448. for(let j = 0; j < this.inventory.length; j++){
  449. if(order.ingredients[i].ingredient === this.inventory[j].ingredient){
  450. this.inventory[j].updateQuantity(-order.ingredients[i].quantity);
  451. break;
  452. }
  453. }
  454. }
  455. }
  456. /*
  457. Gets the quantity of each ingredient sold between two dates (dateRange)
  458. Inputs:
  459. dateRange: list containing a start date and an end date
  460. Return:
  461. [{
  462. ingredient: Ingredient object,
  463. quantity: quantity of ingredient sold in default unit
  464. }]
  465. */
  466. getIngredientsSold(from, to = new Date()){
  467. let recipes = this.getRecipesSold(from, to);
  468. let ingredientList = [];
  469. for(let i = 0; i < recipes.length; i++){
  470. for(let j = 0; j < recipes[i].recipe.ingredients.length; j++){
  471. let exists = false;
  472. for(let k = 0; k < ingredientList.length; k++){
  473. if(ingredientList[k].ingredient === recipes[i].recipe.ingredients[j].ingredient){
  474. exists = true;
  475. ingredientList[k].quantity += recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity;
  476. break;
  477. }
  478. }
  479. if(!exists){
  480. ingredientList.push({
  481. ingredient: recipes[i].recipe.ingredients[j].ingredient,
  482. quantity: recipes[i].quantity * recipes[i].recipe.ingredients[j].quantity
  483. });
  484. }
  485. }
  486. }
  487. return ingredientList;
  488. }
  489. /*
  490. Gets the number of recipes sold between two dates (dateRange)
  491. Inputs:
  492. dateRange: array containing a start date and an end date
  493. Return:
  494. [{
  495. recipe: a recipe object
  496. quantity: quantity of the recipe sold
  497. }]
  498. */
  499. getRecipesSold(from = 0, to = new Date()){
  500. if(from === 0) from = this.transactions[0].date;
  501. const {start, end} = this.getTransactionIndices(from, to);
  502. let recipeList = [];
  503. for(let i = start; i < end; i++){
  504. for(let j = 0; j < this.transactions[i].recipes.length; j++){
  505. let exists = false;
  506. for(let k = 0; k < recipeList.length; k++){
  507. if(recipeList[k].recipe === this.transactions[i].recipes[j].recipe){
  508. exists = true;
  509. recipeList[k].quantity += this.transactions[i].recipes[j].quantity;
  510. break;
  511. }
  512. }
  513. if(!exists){
  514. recipeList.push({
  515. recipe: this.transactions[i].recipes[j].recipe,
  516. quantity: this.transactions[i].recipes[j].quantity
  517. });
  518. }
  519. }
  520. }
  521. return recipeList;
  522. }
  523. getTransactionIndices(from, to){
  524. let start = 0;
  525. let end = 0;
  526. if(
  527. this.transactions.length === 0 ||
  528. from > this.transactions[0].date ||
  529. to >= this.transactions[this.transactions.length-1].date
  530. ){
  531. for(let i = this.transactions.length - 1; i >= 0; i--){
  532. if(this.transactions[i].date > from){
  533. end = i + 1;
  534. break;
  535. }
  536. }
  537. for(let i = 0; i < this.transactions.length; i++){
  538. if(this.transactions[i].date <= to){
  539. start = i;
  540. break;
  541. }
  542. }
  543. }
  544. return {start: start, end: end};
  545. }
  546. }
  547. module.exports = Merchant;