sidebars.js 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  1. let recipeDetailsComp = {
  2. recipe: {},
  3. display: function(recipe){
  4. this.recipe = recipe;
  5. openSidebar(document.querySelector("#recipeDetails"));
  6. document.querySelector("#recipeName").style.display = "block";
  7. document.querySelector("#recipeNameIn").style.display = "none";
  8. document.querySelector("#recipeDetails h1").innerText = recipe.name;
  9. let ingredientList = document.querySelector("#recipeIngredientList");
  10. while(ingredientList.children.length > 0){
  11. ingredientList.removeChild(ingredientList.firstChild);
  12. }
  13. let template = document.querySelector("#recipeIngredient").content.children[0];
  14. for(let i = 0; i < recipe.ingredients.length; i++){
  15. ingredientDiv = template.cloneNode(true);
  16. ingredientDiv.children[0].innerText = recipe.ingredients[i].ingredient.name;
  17. ingredientDiv.children[2].innerText = `${recipe.ingredients[i].quantity} ${recipe.ingredients[i].ingredient.unit}`;
  18. ingredientDiv.ingredient = recipe.ingredients[i].ingredient;
  19. ingredientDiv.name = recipe.ingredients[i].ingredient.name;
  20. ingredientList.appendChild(ingredientDiv);
  21. }
  22. document.querySelector("#addRecIng").style.display = "none";
  23. let price = document.querySelector("#recipePrice");
  24. price.children[1].style.display = "block";
  25. price.children[2].style.display = "none";
  26. price.children[1].innerText = `$${(recipe.price / 100).toFixed(2)}`;
  27. document.querySelector("#recipeUpdate").style.display = "none";
  28. },
  29. edit: function(){
  30. let ingredientDivs = document.querySelector("#recipeIngredientList");
  31. if(merchant.pos === "none"){
  32. let name = document.querySelector("#recipeName");
  33. let nameIn = document.querySelector("#recipeNameIn");
  34. name.style.display = "none";
  35. nameIn.style.display = "block";
  36. nameIn.value = this.recipe.name;
  37. let price = document.querySelector("#recipePrice");
  38. price.children[1].style.display = "none";
  39. price.children[2].style.display = "block";
  40. price.children[2].value = parseFloat((this.recipe.price / 100).toFixed(2));
  41. }
  42. for(let i = 0; i < ingredientDivs.children.length; i++){
  43. let div = ingredientDivs.children[i];
  44. div.children[2].innerText = this.recipe.ingredients[i].ingredient.unit;
  45. div.children[1].style.display = "block";
  46. div.children[1].value = parseFloat(this.recipe.ingredients[i].quantity);
  47. div.children[3].style.display = "block";
  48. div.children[3].onclick = ()=>{div.parentElement.removeChild(div)};
  49. }
  50. document.querySelector("#addRecIng").style.display = "flex";
  51. document.querySelector("#recipeUpdate").style.display = "flex";
  52. },
  53. update: function(){
  54. this.recipe.name = document.querySelector("#recipeNameIn").value || this.recipe.name;
  55. this.recipe.price = Math.round((document.querySelector("#recipePrice").children[2].value * 100)) || this.recipe.price;
  56. this.recipe.ingredients = [];
  57. let divs = document.querySelector("#recipeIngredientList").children;
  58. for(let i = 0; i < divs.length; i++){
  59. if(divs[i].name === "new"){
  60. let select = divs[i].children[0];
  61. this.recipe.ingredients.push({
  62. ingredient: select.options[select.selectedIndex].ingredient,
  63. quantity: divs[i].children[1].value
  64. });
  65. }else{
  66. this.recipe.ingredients.push({
  67. ingredient: divs[i].ingredient,
  68. quantity: divs[i].children[1].value
  69. });
  70. }
  71. }
  72. let data = {
  73. id: this.recipe.id,
  74. name: this.recipe.name,
  75. price: this.recipe.price,
  76. ingredients: []
  77. }
  78. for(let i = 0; i < this.recipe.ingredients.length; i++){
  79. data.ingredients.push({
  80. ingredient: this.recipe.ingredients[i].ingredient.id,
  81. quantity: this.recipe.ingredients[i].quantity
  82. });
  83. }
  84. let loader = document.getElementById("loaderContainer");
  85. loader.style.display = "flex";
  86. fetch("/recipe/update", {
  87. method: "PUT",
  88. headers: {
  89. "Content-Type": "application/json;charset=utf-8"
  90. },
  91. body: JSON.stringify(data)
  92. })
  93. .then((response) => response.json())
  94. .then((response)=>{
  95. if(typeof(response) === "string"){
  96. banner.createError(response);
  97. }else{
  98. merchant.editRecipe(this.recipe);
  99. banner.createNotification("Recipe successfully updated");
  100. }
  101. })
  102. .catch((err)=>{
  103. banner.createError("Something went wrong. Please refresh the page");
  104. })
  105. .finally(()=>{
  106. loader.style.display = "none";
  107. });
  108. },
  109. remove: function(){
  110. fetch(`/merchant/recipes/remove/${this.recipe.id}`, {
  111. method: "DELETE"
  112. })
  113. .then((response) => response.json())
  114. .then((response)=>{
  115. if(typeof(response) === "string"){
  116. banner.createError(response);
  117. }else{
  118. merchant.editRecipe(this.recipe, true);
  119. banner.createNotification("Recipe removed");
  120. }
  121. })
  122. .catch((err)=>{
  123. banner.createError("Something went wrong. Try refreshing the page");
  124. });
  125. },
  126. displayAddIngredient: function(){
  127. let template = document.querySelector("#addRecIngredient").content.children[0].cloneNode(true);
  128. template.name = "new";
  129. document.querySelector("#recipeIngredientList").appendChild(template);
  130. let categories = merchant.categorizeIngredients();
  131. for(let i = 0; i < categories.length; i++){
  132. let optGroup = document.createElement("optgroup");
  133. optGroup.label = categories[i].name;
  134. template.children[0].appendChild(optGroup);
  135. for(let j = 0; j < categories[i].ingredients.length; j++){
  136. let option = document.createElement("option");
  137. option.innerText = `${categories[i].ingredients[j].ingredient.name} (${categories[i].ingredients[j].ingredient.unit})`;
  138. option.ingredient = categories[i].ingredients[j].ingredient;
  139. optGroup.appendChild(option);
  140. }
  141. }
  142. }
  143. }
  144. let newOrderComp = {
  145. isPopulated: false,
  146. unused: [],
  147. display: function(){
  148. if(!this.isPopulated){
  149. let categories = merchant.categorizeIngredients();
  150. let categoriesList = document.querySelector("#newOrderCategories");
  151. let template = document.querySelector("#addIngredientsCategory").content.children[0];
  152. let ingredientTemplate = document.querySelector("#addIngredientsIngredient").content.children[0];
  153. for(let i = 0; i < categories.length; i++){
  154. let category = template.cloneNode(true);
  155. category.children[0].children[0].innerText = categories[i].name;
  156. category.children[0].children[1].onclick = ()=>{addIngredientsComp.toggleAddIngredient(category)};
  157. category.children[0].children[1].children[1].style.display = "none";
  158. category.children[1].style.display = "none";
  159. categoriesList.appendChild(category);
  160. for(let j = 0; j < categories[i].ingredients.length; j++){
  161. let ingredientDiv = ingredientTemplate.cloneNode(true);
  162. ingredientDiv.children[0].innerText = categories[i].ingredients[j].ingredient.name;
  163. ingredientDiv.children[1].onclick = ()=>{this.addOne(ingredientDiv, category.children[1])};
  164. ingredientDiv.ingredient = categories[i].ingredients[j].ingredient;
  165. this.unused.push(categories[i].ingredients[j]);
  166. category.children[1].appendChild(ingredientDiv);
  167. }
  168. }
  169. this.isPopulated = true;
  170. }
  171. openSidebar(document.querySelector("#newOrder"));
  172. },
  173. addOne: function(ingredientDiv, container){
  174. for(let i = 0; i < this.unused.length; i++){
  175. if(this.unused[i] === ingredientDiv){
  176. this.unused.splice(i, 1);
  177. break;
  178. }
  179. }
  180. let quantityInput = document.createElement("input");
  181. quantityInput.type = "number";
  182. quantityInput.placeholder = ingredientDiv.ingredient.unit;
  183. quantityInput.min = "0";
  184. quantityInput.step = "0.01";
  185. ingredientDiv.insertBefore(quantityInput, ingredientDiv.children[1]);
  186. let priceInput = document.createElement("input");
  187. priceInput.type = "number";
  188. priceInput.placeholder = "Price Per Unit";
  189. priceInput.min = "0";
  190. priceInput.step = "0.01";
  191. ingredientDiv.insertBefore(priceInput, ingredientDiv.children[2]);
  192. ingredientDiv.children[3].innerText = "-";
  193. ingredientDiv.children[3].onclick = ()=>{this.removeOne(ingredientDiv, container)};
  194. container.removeChild(ingredientDiv);
  195. document.getElementById("newOrderAdded").appendChild(ingredientDiv);
  196. },
  197. removeOne: function(ingredientDiv, container){
  198. this.unused.push(ingredientDiv.ingredient);
  199. ingredientDiv.removeChild(ingredientDiv.children[1]);
  200. ingredientDiv.removeChild(ingredientDiv.children[1]);
  201. ingredientDiv.children[1].innerText = "+";
  202. ingredientDiv.children[1].onclick = ()=>{this.addOne(ingredientDiv, container)};
  203. ingredientDiv.parentElement.removeChild(ingredientDiv);
  204. container.appendChild(ingredientDiv);
  205. },
  206. submit: function(){
  207. let categoriesList = document.getElementById("newOrderAdded");
  208. let ingredients = [];
  209. for(let i = 0; i < categoriesList.children.length; i++){
  210. let quantity = categoriesList.children[i].children[1].value;
  211. let price = categoriesList.children[i].children[2].value;
  212. if(quantity !== "" && price !== ""){
  213. ingredients.push({
  214. ingredient: categoriesList.children[i].ingredient.id,
  215. quantity: parseFloat(quantity),
  216. price: parseInt(price * 100)
  217. });
  218. }
  219. }
  220. let data = {
  221. name: document.getElementById("orderName").value,
  222. date: document.getElementById("orderDate").value,
  223. ingredients: ingredients
  224. }
  225. let loader = document.getElementById("loaderContainer");
  226. loader.style.display = "flex";
  227. fetch("/order", {
  228. method: "POST",
  229. headers: {
  230. "Content-Type": "application/json;charset=utf-8"
  231. },
  232. body: JSON.stringify(data)
  233. })
  234. .then(response => response.json())
  235. .then((response)=>{
  236. if(typeof(response) === "string"){
  237. banner.createError(response);
  238. }else{
  239. let order = new Order(
  240. response._id,
  241. response.name,
  242. response.date,
  243. response.ingredients,
  244. merchant
  245. )
  246. merchant.editOrders([order]);
  247. merchant.editIngredients(order.ingredients, false, true);
  248. banner.createNotification("New order created");
  249. }
  250. })
  251. .catch((err)=>{
  252. banner.createError("Something went wrong. Try refreshing the page");
  253. })
  254. .finally(()=>{
  255. loader.style.display = "none";
  256. });
  257. },
  258. }
  259. let newIngredientComp = {
  260. display: function(){
  261. openSidebar(document.querySelector("#newIngredient"));
  262. document.querySelector("#newIngName").value = "";
  263. document.querySelector("#newIngCategory").value = "";
  264. document.querySelector("#newIngQuantity").value = 0;
  265. document.querySelector("#newIngUnit").value = ""
  266. },
  267. submit: function(){
  268. let newIngredient = {
  269. ingredient: {
  270. name: document.querySelector("#newIngName").value,
  271. category: document.querySelector("#newIngCategory").value,
  272. unit: document.querySelector("#newIngUnit").value
  273. },
  274. quantity: document.querySelector("#newIngQuantity").value
  275. }
  276. let loader = document.getElementById("loaderContainer");
  277. loader.style.display = "flex";
  278. // if(validator.ingredient(newIngredient)){
  279. fetch("/ingredients/create", {
  280. method: "POST",
  281. headers: {
  282. "Content-Type": "application/json;charset=utf-8"
  283. },
  284. body: JSON.stringify(newIngredient)
  285. })
  286. .then((response) => response.json())
  287. .then((response)=>{
  288. if(typeof(response) === "string"){
  289. banner.createError(response);
  290. }else{
  291. merchant.editIngredients([{
  292. ingredient: new Ingredient(
  293. response.ingredient._id,
  294. response.ingredient.name,
  295. response.ingredient.category,
  296. response.ingredient.unit
  297. ),
  298. quantity: response.quantity
  299. }]);
  300. banner.createNotification("Ingredient successfully created");
  301. }
  302. })
  303. .catch((err)=>{
  304. banner.createError("Something went wrong. Try refreshing the page");
  305. })
  306. .finally(()=>{
  307. loader.style.display = "none";
  308. });
  309. // }
  310. }
  311. }
  312. let orderDetailsComp = {
  313. display: function(order){
  314. openSidebar(document.querySelector("#orderDetails"));
  315. document.querySelector("#removeOrderBtn").onclick = ()=>{this.remove(order)};
  316. document.querySelector("#orderDetails h1").innerText = order.name;
  317. document.querySelector("#orderDetails h3").innerText = order.date.toLocaleDateString("en-US");
  318. let ingredientList = document.querySelector("#orderIngredients");
  319. while(ingredientList.children.length > 0){
  320. ingredientList.removeChild(ingredientList.firstChild);
  321. }
  322. let template = document.querySelector("#orderIngredient").content.children[0];
  323. let grandTotal = 0;
  324. for(let i = 0; i < order.ingredients.length; i++){
  325. let ingredient = template.cloneNode(true);
  326. let price = (order.ingredients[i].quantity * order.ingredients[i].price) / 100;
  327. grandTotal += price;
  328. ingredient.children[0].innerText = order.ingredients[i].ingredient.name;
  329. ingredient.children[1].innerText = `${order.ingredients[i].quantity} x $${(order.ingredients[i].price / 100).toFixed(2)}`;
  330. ingredient.children[2].innerText = `$${price.toFixed(2)}`;
  331. ingredientList.appendChild(ingredient);
  332. }
  333. document.querySelector("#orderTotalPrice p").innerText = `$${grandTotal.toFixed(2)}`;
  334. },
  335. remove: function(order){
  336. let loader = document.getElementById("loaderContainer");
  337. loader.style.display = "flex";
  338. fetch(`/order/${order.id}`, {
  339. method: "DELETE",
  340. headers: {
  341. "Content-Type": "application/json;charset=utf-8"
  342. }
  343. })
  344. .then((response) => response.json())
  345. .then((response)=>{
  346. if(typeof(response) === "string"){
  347. banner.createError(response);
  348. }else{
  349. merchant.editOrders([order], true);
  350. banner.createNotification("Order successfully removed");
  351. }
  352. })
  353. .catch((err)=>{
  354. banner.createError("Something went wrong, try refreshing the page");
  355. })
  356. .finally(()=>{
  357. loader.style.display = "none";
  358. });
  359. }
  360. }
  361. let addIngredientsComp = {
  362. isPopulated: false,
  363. fakeMerchant: {},
  364. chosenIngredients: [],
  365. display: function(){
  366. let sidebar = document.querySelector("#addIngredients");
  367. if(!this.isPopulated){
  368. let loader = document.getElementById("loaderContainer");
  369. loader.style.display = "flex";
  370. fetch("/ingredients")
  371. .then((response) => response.json())
  372. .then((response)=>{
  373. if(typeof(response) === "string"){
  374. banner.createError(response);
  375. }else{
  376. for(let i = 0; i < merchant.ingredients.length; i++){
  377. for(let j = 0; j < response.length; j++){
  378. if(merchant.ingredients[i].ingredient.id === response[j]._id){
  379. response.splice(j, 1);
  380. break;
  381. }
  382. }
  383. }
  384. for(let i = 0; i < response.length; i++){
  385. response[i] = {ingredient: response[i]}
  386. }
  387. this.fakeMerchant = new Merchant(
  388. {
  389. name: "none",
  390. inventory: response,
  391. recipes: [],
  392. },
  393. []
  394. );
  395. this.populateAddIngredients();
  396. }
  397. })
  398. .catch((err)=>{
  399. banner.createError("Unable to retrieve data");
  400. })
  401. .finally(()=>{
  402. loader.style.display = "none";
  403. });
  404. this.isPopulated = true;
  405. }
  406. openSidebar(sidebar);
  407. },
  408. populateAddIngredients: function(){
  409. let addIngredientsDiv = document.getElementById("addIngredientList");
  410. let categoryTemplate = document.getElementById("addIngredientsCategory");
  411. let ingredientTemplate = document.getElementById("addIngredientsIngredient");
  412. let categories = this.fakeMerchant.categorizeIngredients();
  413. while(addIngredientsDiv.children.length > 0){
  414. addIngredientsDiv.removeChild(addIngredientsDiv.firstChild);
  415. }
  416. for(let i = 0; i < categories.length; i++){
  417. let categoryDiv = categoryTemplate.content.children[0].cloneNode(true);
  418. categoryDiv.children[0].children[0].innerText = categories[i].name;
  419. categoryDiv.children[0].children[1].onclick = ()=>{addIngredientsComp.toggleAddIngredient(categoryDiv)};
  420. categoryDiv.children[1].style.display = "none";
  421. categoryDiv.children[0].children[1].children[1].style.display = "none";
  422. addIngredientsDiv.appendChild(categoryDiv);
  423. for(let j = 0; j < categories[i].ingredients.length; j++){
  424. let ingredientDiv = ingredientTemplate.content.children[0].cloneNode(true);
  425. ingredientDiv.children[0].innerText = categories[i].ingredients[j].ingredient.name;
  426. ingredientDiv.children[1].onclick = ()=>{this.addOne(ingredientDiv)};
  427. ingredientDiv.ingredient = categories[i].ingredients[j].ingredient;
  428. categoryDiv.children[1].appendChild(ingredientDiv);
  429. }
  430. }
  431. },
  432. toggleAddIngredient: function(categoryElement){
  433. let button = categoryElement.children[0].children[1];
  434. let ingredientDisplay = categoryElement.children[1];
  435. if(ingredientDisplay.style.display === "none"){
  436. ingredientDisplay.style.display = "flex";
  437. button.children[0].style.display = "none";
  438. button.children[1].style.display = "block";
  439. }else{
  440. ingredientDisplay.style.display = "none";
  441. button.children[0].style.display = "block";
  442. button.children[1].style.display = "none";
  443. }
  444. },
  445. addOne: function(element){
  446. element.parentElement.removeChild(element);
  447. document.getElementById("myIngredients").appendChild(element);
  448. document.getElementById("myIngredientsDiv").style.display = "flex";
  449. for(let i = 0; i < this.fakeMerchant.ingredients.length; i++){
  450. if(this.fakeMerchant.ingredients[i].ingredient === element.ingredient){
  451. this.fakeMerchant.ingredients.splice(i, 1);
  452. this.chosenIngredients.push(element.ingredient);
  453. break;
  454. }
  455. }
  456. let input = document.createElement("input");
  457. input.type = "number";
  458. input.min = "0";
  459. input.step = "0.01";
  460. input.placeholder = element._unit;
  461. element.insertBefore(input, element.children[1]);
  462. element.children[2].innerText = "-";
  463. element.children[2].onclick = ()=>{this.removeOne(element)};
  464. },
  465. removeOne: function(element){
  466. element.parentElement.removeChild(element);
  467. element.removeChild(element.children[1]);
  468. element.children[1].innerText = "+";
  469. element.children[1].onclick = ()=>{this.addOne(element)};
  470. if(document.getElementById("myIngredients").children.length === 0){
  471. document.getElementById("myIngredientsDiv").style.display = "none";
  472. }
  473. for(let i = 0; i < this.chosenIngredients.length; i++){
  474. if(this.chosenIngredients[i] === element.ingredient){
  475. this.chosenIngredients.splice(i, 1);
  476. this.fakeMerchant.ingredients.push({
  477. ingredient: element.ingredient
  478. });
  479. break;
  480. }
  481. }
  482. this.populateAddIngredients();
  483. },
  484. submit: function(){
  485. let ingredients = document.getElementById("myIngredients").children;
  486. let newIngredients = [];
  487. let fetchable = [];
  488. for(let i = 0; i < ingredients.length; i++){
  489. if(ingredients[i].children[1].value === ""){
  490. banner.createError("Please enter a quantity for each ingredient you want to add to your inventory");
  491. return;
  492. }
  493. newIngredients.push({
  494. ingredient: ingredients[i].ingredient,
  495. quantity: ingredients[i].children[1].value
  496. });
  497. fetchable.push({
  498. id: ingredients[i].ingredient.id,
  499. quantity: ingredients[i].children[1].value
  500. });
  501. }
  502. let loader = document.getElementById("loaderContainer");
  503. loader.style.display = "flex";
  504. fetch("/merchant/ingredients/add", {
  505. method: "POST",
  506. headers: {
  507. "Content-Type": "application/json;charset=utf-8"
  508. },
  509. body: JSON.stringify(fetchable)
  510. })
  511. .then((response) => response.json())
  512. .then((response)=>{
  513. if(typeof(response) === "string"){
  514. banner.createError(response);
  515. }else{
  516. merchant.editIngredients(newIngredients);
  517. this.isPopulated = false;
  518. banner.createNotification("All ingredients added successfully");
  519. }
  520. })
  521. .catch((err)=>{
  522. banner.createError("Something went wrong. Try refreshing the page");
  523. })
  524. .finally(()=>{
  525. loader.style.display = "none";
  526. });
  527. }
  528. }
  529. let ingredientDetailsComp = {
  530. ingredient: {},
  531. display: function(ingredient){
  532. this.ingredient = ingredient;
  533. sidebar = document.querySelector("#ingredientDetails");
  534. document.querySelector("#ingredientDetails p").innerText = ingredient.ingredient.category;
  535. document.querySelector("#ingredientDetails h1").innerText = ingredient.ingredient.name;
  536. document.querySelector("#ingredientStock").innerText = `${ingredient.quantity} ${ingredient.ingredient.unit}`;
  537. document.querySelector("#ingredientInput").placeholder = `${ingredient.quantity} ${ingredient.ingredient.unit}`;
  538. let quantities = [];
  539. let now = new Date();
  540. for(let i = 1; i < 31; i++){
  541. let endDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i)
  542. let startDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i - 1);
  543. let indices = merchant.transactionIndices(startDay, endDay);
  544. if(indices === false){
  545. quantities.push(0);
  546. }else{
  547. quantities.push(merchant.singleIngredientSold(indices, ingredient));
  548. }
  549. }
  550. let sum = 0;
  551. for(let quantity of quantities){
  552. sum += quantity;
  553. }
  554. document.querySelector("#dailyUse").innerText = `${(sum/quantities.length).toFixed(2)} ${ingredient.ingredient.unit}`;
  555. let ul = document.querySelector("#ingredientRecipeList");
  556. let recipes = merchant.getRecipesForIngredient(ingredient.ingredient);
  557. while(ul.children.length > 0){
  558. ul.removeChild(ul.firstChild);
  559. }
  560. for(let i = 0; i < recipes.length; i++){
  561. let li = document.createElement("li");
  562. li.innerText = recipes[i].name;
  563. li.onclick = ()=>{
  564. changeStrand("recipeBookStrand");
  565. recipeDetailsComp.display(recipes[i]);
  566. }
  567. ul.appendChild(li);
  568. }
  569. openSidebar(sidebar);
  570. },
  571. remove: function(){
  572. for(let i = 0; i < merchant.recipes.length; i++){
  573. for(let j = 0; j < merchant.recipes[i].ingredients.length; j++){
  574. if(this.ingredient.ingredient === merchant.recipes[i].ingredients[j].ingredient){
  575. banner.createError("Must remove ingredient from all recipes before removing");
  576. return;
  577. }
  578. }
  579. }
  580. let loader = document.getElementById("loaderContainer");
  581. loader.style.display = "flex";
  582. fetch(`/merchant/ingredients/remove/${this.ingredient.ingredient.id}`, {
  583. method: "DELETE",
  584. })
  585. .then((response) => response.json())
  586. .then((response)=>{
  587. if(typeof(response) === "string"){
  588. banner.createError(response);
  589. }else{
  590. banner.createNotification("Ingredient removed");
  591. merchant.editIngredients([this.ingredient], true);
  592. }
  593. })
  594. .catch((err)=>{})
  595. .finally(()=>{
  596. loader.style.display = "none";
  597. });
  598. },
  599. edit: function(){
  600. document.querySelector("#ingredientStock").style.display = "none";
  601. document.querySelector("#ingredientInput").style.display = "block";
  602. document.querySelector("#editSubmitButton").style.display = "block";
  603. },
  604. editSubmit: function(){
  605. this.ingredient.quantity = Number(document.getElementById("ingredientInput").value);
  606. let data = [{
  607. id: this.ingredient.ingredient.id,
  608. quantity: this.ingredient.quantity
  609. }];
  610. let loader = document.getElementById("loaderContainer");
  611. loader.style.display = "flex";
  612. if(validator.ingredientQuantity(data[0].quantity)){
  613. fetch("/merchant/ingredients/update", {
  614. method: "PUT",
  615. headers: {
  616. "Content-Type": "application/json;charset=utf-8"
  617. },
  618. body: JSON.stringify(data)
  619. })
  620. .then((response) => response.json())
  621. .then((response)=>{
  622. if(typeof(response) === "string"){
  623. banner.createError(response);
  624. }else{
  625. merchant.editIngredients([this.ingredient]);
  626. banner.createNotification("Ingredient updated");
  627. }
  628. })
  629. .catch((err)=>{
  630. banner.createError("Something went wrong, try refreshing the page");
  631. })
  632. .finally(()=>{
  633. loader.style.display = "none";
  634. });
  635. }
  636. }
  637. }
  638. let newRecipeComp = {
  639. display: function(){
  640. let ingredientsSelect = document.querySelector("#recipeInputIngredients select");
  641. let categories = merchant.categorizeIngredients();
  642. while(ingredientsSelect.children.length > 0){
  643. ingredientsSelect.removeChild(ingredientsSelect.firstChild);
  644. }
  645. for(let category of categories){
  646. let optgroup = document.createElement("optgroup");
  647. optgroup.label = category.name;
  648. ingredientsSelect.appendChild(optgroup);
  649. for(let ingredient of category.ingredients){
  650. let option = document.createElement("option");
  651. option.value = ingredient.ingredient.id;
  652. option.innerText = ingredient.ingredient.name;
  653. optgroup.appendChild(option);
  654. }
  655. }
  656. openSidebar(document.querySelector("#addRecipe"));
  657. },
  658. //Updates the number of ingredient inputs displayed for new recipes
  659. changeRecipeCount: function(){
  660. let newCount = document.querySelector("#ingredientCount").value;
  661. let ingredientsDiv = document.querySelector("#recipeInputIngredients");
  662. let oldCount = ingredientsDiv.children.length;
  663. if(newCount > oldCount){
  664. let newDivs = newCount - oldCount;
  665. for(let i = 0; i < newDivs; i++){
  666. let newNode = ingredientsDiv.children[0].cloneNode(true);
  667. newNode.children[2].children[0].value = "";
  668. ingredientsDiv.appendChild(newNode);
  669. }
  670. for(let i = 0; i < newCount; i++){
  671. ingredientsDiv.children[i].children[0].innerText = `Ingredient ${i + 1}`;
  672. }
  673. }else if(newCount < oldCount){
  674. let newDivs = oldCount - newCount;
  675. for(let i = 0; i < newDivs; i++){
  676. ingredientsDiv.removeChild(ingredientsDiv.children[ingredientsDiv.children.length-1]);
  677. }
  678. }
  679. },
  680. submit: function(){
  681. let newRecipe = {
  682. name: document.querySelector("#newRecipeName").value,
  683. price: document.querySelector("#newRecipePrice").value,
  684. ingredients: []
  685. }
  686. let inputs = document.querySelectorAll("#recipeInputIngredients > div");
  687. for(let input of inputs){
  688. newRecipe.ingredients.push({
  689. ingredient: input.children[1].children[0].value,
  690. quantity: input.children[2].children[0].value
  691. });
  692. }
  693. if(!validator.recipe(newRecipe)){
  694. return;
  695. }
  696. let loader = document.getElementById("loaderContainer");
  697. loader.style.display = "flex";
  698. fetch("/recipe/create", {
  699. method: "POST",
  700. headers: {
  701. "Content-Type": "application/json;charset=utf-8"
  702. },
  703. body: JSON.stringify(newRecipe)
  704. })
  705. .then((response) => response.json())
  706. .then((response)=>{
  707. if(typeof(response) === "string"){
  708. banner.createError(response);
  709. }else{
  710. let recipe = new Recipe(
  711. response._id,
  712. response.name,
  713. response.price,
  714. response.ingredients,
  715. merchant,
  716. );
  717. merchant.editRecipe(recipe);
  718. banner.createNotification("New recipe successfully created");
  719. }
  720. })
  721. .catch((err)=>{
  722. banner.createError("Refresh page to update data");
  723. })
  724. .finally(()=>{
  725. loader.style.display = "none";
  726. });
  727. },
  728. }
  729. let transactionDetailsComp = {
  730. transaction: {},
  731. display: function(transaction){
  732. this.transaction = transaction;
  733. let recipeList = document.getElementById("transactionRecipes");
  734. let template = document.getElementById("transactionRecipe").content.children[0];
  735. let totalRecipes = 0;
  736. let totalPrice = 0;
  737. while(recipeList.children.length > 0){
  738. recipeList.removeChild(recipeList.firstChild);
  739. }
  740. for(let i = 0; i < transaction.recipes.length; i++){
  741. let recipe = template.cloneNode(true);
  742. let price = transaction.recipes[i].quantity * transaction.recipes[i].recipe.price;
  743. recipe.children[0].innerText = transaction.recipes[i].recipe.name;
  744. recipe.children[1].innerText = `${transaction.recipes[i].quantity} x $${parseFloat(transaction.recipes[i].recipe.price / 100).toFixed(2)}`;
  745. recipe.children[2].innerText = `$${(price / 100).toFixed(2)}`;
  746. recipeList.appendChild(recipe);
  747. totalRecipes += transaction.recipes[i].quantity;
  748. totalPrice += price;
  749. }
  750. let months = ["January", "Fecbruary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  751. let days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
  752. let dateString = `${days[transaction.date.getDay()]}, ${months[transaction.date.getMonth()]} ${transaction.date.getDate()}, ${transaction.date.getFullYear()}`;
  753. document.getElementById("transactionDate").innerText = dateString;
  754. document.getElementById("transactionTime").innerText = transaction.date.toLocaleTimeString();
  755. document.getElementById("totalRecipes").innerText = `${totalRecipes} recipes`;
  756. document.getElementById("totalPrice").innerText = `$${(totalPrice / 100).toFixed(2)}`;
  757. openSidebar(document.getElementById("transactionDetails"));
  758. },
  759. remove: function(){
  760. let loader = document.getElementById("loaderContainer");
  761. loader.style.display = "flex";
  762. fetch(`/transaction/${this.transaction.id}`, {
  763. method: "delete",
  764. headers: {
  765. "Content-Type": "application/json;charset=utf-8"
  766. },
  767. })
  768. .then(response => response.json())
  769. .then((response)=>{
  770. if(typeof(response) === "string"){
  771. banner.createError(response);
  772. }else{
  773. merchant.editTransactions(this.transaction, true);
  774. banner.createNotification("Transaction removed");
  775. }
  776. })
  777. .catch((err)=>{
  778. banner.createError("Something went wrong, please refresh the page");
  779. })
  780. .finally(()=>{
  781. loader.style.display = "none";
  782. });
  783. },
  784. }
  785. let newTransactionComp = {
  786. display: function(){
  787. let recipeList = document.getElementById("newTransactionRecipes");
  788. let template = document.getElementById("createTransaction").content.children[0];
  789. while(recipeList.children.length > 0){
  790. recipeList.removeChild(recipeList.firstChild);
  791. }
  792. for(let i = 0; i < merchant.recipes.length; i++){
  793. let recipeDiv = template.cloneNode(true);
  794. recipeDiv.recipe = merchant.recipes[i];
  795. recipeList.appendChild(recipeDiv);
  796. recipeDiv.children[0].innerText = merchant.recipes[i].name;
  797. }
  798. openSidebar(document.getElementById("newTransaction"));
  799. },
  800. submit: function(){
  801. let recipeDivs = document.getElementById("newTransactionRecipes");
  802. let date = document.getElementById("newTransactionDate").valueAsDate;
  803. console.log(recipeDivs);
  804. if(date > new Date()){
  805. banner.createError("Cannot have a date in the future");
  806. return;
  807. }
  808. let newTransaction = {
  809. date: date,
  810. recipes: []
  811. };
  812. for(let i = 0; i < recipeDivs.children.length; i++){
  813. let quantity = recipeDivs.children[i].children[1].value;
  814. if(quantity !== "" && quantity > 0){
  815. newTransaction.recipes.push({
  816. recipe: recipeDivs.children[i].recipe.id,
  817. quantity: quantity
  818. });
  819. }else if(quantity < 0){
  820. banner.createError("Cannot have negative values");
  821. return;
  822. }
  823. }
  824. if(newTransaction.recipes.length > 0){
  825. let loader = document.getElementById("loaderContainer");
  826. loader.style.display = "flex";
  827. fetch("/transaction", {
  828. method: "post",
  829. headers: {
  830. "Content-Type": "application/json;charset=utf-8"
  831. },
  832. body: JSON.stringify(newTransaction)
  833. })
  834. .then(response => response.json())
  835. .then((response)=>{
  836. if(typeof(response) === "string"){
  837. banner.createError(response);
  838. }else{
  839. let transaction = new Transaction(
  840. response._id,
  841. response.date,
  842. response.recipes,
  843. merchant
  844. );
  845. merchant.editTransactions(transaction);
  846. banner.createNotification("NEW TRANSACTION CREATED");
  847. }
  848. })
  849. .catch((err)=>{
  850. console.log(err);
  851. banner.createError("Something went wrong, try refreshing the page");
  852. })
  853. .finally(()=>{
  854. loader.style.display = "none";
  855. });
  856. }
  857. }
  858. }