transactions.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. module.exports = {
  2. isPopulated: false,
  3. display: function(Transaction){
  4. if(!this.isPopulated){
  5. let transactionsList = document.getElementById("transactionsList");
  6. let dateDropdown = document.getElementById("dateDropdown");
  7. let recipeDropdown = document.getElementById("recipeDropDown");
  8. let template = document.getElementById("transaction").content.children[0];
  9. let now = new Date();
  10. let monthAgo = new Date(now.getFullYear(), now.getMonth() - 1, now.getDate());
  11. document.getElementById("transFilDate1").valueAsDate = monthAgo;
  12. document.getElementById("transFilDate2").valueAsDate = now;
  13. dateDropdown.style.display = "none";
  14. recipeDropdown.style.display = "none";
  15. document.getElementById("dateFilterBtn").onclick = ()=>{this.toggleDropdown(dateDropdown)};
  16. document.getElementById("recipeFilterBtn").onclick = ()=>{this.toggleDropdown(recipeDropdown)};
  17. while(recipeDropdown.children.length > 0){
  18. recipeDropdown.removeChild(recipeDropdown.firstChild);
  19. }
  20. for(let i = 0; i < merchant.recipes.length; i++){
  21. let checkbox = document.createElement("input");
  22. checkbox.type = "checkbox";
  23. checkbox.recipe = merchant.recipes[i];
  24. recipeDropdown.appendChild(checkbox);
  25. let label = document.createElement("label");
  26. label.innerText = merchant.recipes[i].name;
  27. label.for = checkbox;
  28. recipeDropdown.appendChild(label);
  29. let brk = document.createElement("br");
  30. recipeDropdown.appendChild(brk);
  31. }
  32. while(transactionsList.children.length > 0){
  33. transactionsList.removeChild(transactionsList.firstChild);
  34. }
  35. let i = 0
  36. while(i < merchant.transactions.length && i < 100){
  37. let transactionDiv = template.cloneNode(true);
  38. let transaction = merchant.transactions[i];
  39. transactionDiv.onclick = ()=>{controller.openSidebar("transactionDetails", transaction)};
  40. transactionsList.appendChild(transactionDiv);
  41. let totalRecipes = 0;
  42. let totalPrice = 0;
  43. for(let j = 0; j < merchant.transactions[i].recipes.length; j++){
  44. totalRecipes += merchant.transactions[i].recipes[j].quantity;
  45. totalPrice += merchant.transactions[i].recipes[j].recipe.price * merchant.transactions[i].recipes[j].quantity;
  46. }
  47. transactionDiv.children[0].innerText = `${merchant.transactions[i].date.toLocaleDateString()} ${merchant.transactions[i].date.toLocaleTimeString()}`;
  48. transactionDiv.children[1].innerText = `${totalRecipes} recipes sold`;
  49. transactionDiv.children[2].innerText = `$${(totalPrice / 100).toFixed(2)}`;
  50. i++;
  51. }
  52. document.getElementById("transFormSubmit").onsubmit = ()=>{this.submitFilter(Transaction)};
  53. this.isPopulated = true;
  54. }
  55. },
  56. submitFilter: function(Transaction){
  57. event.preventDefault();
  58. let data = {
  59. startDate: document.getElementById("transFilDate1").valueAsDate,
  60. endDate: document.getElementById("transFilDate2").valueAsDate,
  61. recipes: []
  62. }
  63. if(data.startDate >= data.endDate){
  64. banner.createError("START DATE CANNOT BE AFTER END DATE");
  65. return;
  66. }
  67. let recipeChoices = document.getElementById("recipeDropDown");
  68. for(let i = 0; i < recipeChoices.children.length; i += 3){
  69. if(recipeChoices.children[i].checked){
  70. data.recipes.push(recipeChoices.children[i].recipe.id);
  71. }
  72. }
  73. if(data.recipes.length === 0){
  74. for(let i = 0; i < merchant.recipes.length; i++){
  75. data.recipes.push(merchant.recipes[i].id);
  76. }
  77. }
  78. let loader = document.getElementById("loaderContainer");
  79. loader.style.display = "flex";
  80. fetch("/transaction", {
  81. method: "POST",
  82. headers: {
  83. "Content-Type": "application/json;charset=utf-8"
  84. },
  85. body: JSON.stringify(data)
  86. })
  87. .then((response) => response.json())
  88. .then((response)=>{
  89. if(typeof(response) === "string"){
  90. banner.createError(response);
  91. }else{
  92. let transactionList = document.getElementById("transactionsList");
  93. let template = document.getElementById("transaction").content.children[0];
  94. while(transactionList.children.length > 0){
  95. transactionList.removeChild(transactionList.firstChild);
  96. }
  97. for(let i = 0; i < response.length; i++){
  98. let transactionDiv = template.cloneNode(true);
  99. let recipeCount = 0;
  100. let cost = 0;
  101. let transaction = new Transaction(
  102. response[i]._id,
  103. response[i].date,
  104. response[i].recipes,
  105. merchant
  106. );
  107. for(let j = 0; j < transaction.recipes.length; j++){
  108. recipeCount += transaction.recipes[j].quantity;
  109. cost += transaction.recipes[j].quantity * transaction.recipes[j].recipe.price;
  110. }
  111. transactionDiv.children[0].innerText = `${transaction.date.toLocaleDateString()} ${transaction.date.toLocaleTimeString()}`;
  112. transactionDiv.children[1].innerText = `${recipeCount} recipes sold`;
  113. transactionDiv.children[2].innerText = `$${(cost / 100).toFixed(2)}`;
  114. transactionDiv.onclick = ()=>{controller.openSidebar("transactionDetails", transaction)};
  115. transactionList.appendChild(transactionDiv);
  116. }
  117. }
  118. })
  119. .catch((err)=>{
  120. banner.createError("UNABLE TO DISPLAY THE TRANSACTIONS");
  121. })
  122. .finally(()=>{
  123. loader.style.display = "none";
  124. });
  125. },
  126. toggleDropdown: function(dropdown){
  127. event.preventDefault();
  128. let polyline = dropdown.parentElement.children[0].children[1].children[0].children[0];
  129. if(dropdown.style.display === "none"){
  130. dropdown.style.display = "block";
  131. polyline.setAttribute("points", "18 15 12 9 6 15");
  132. }else{
  133. dropdown.style.display = "none";
  134. polyline.setAttribute("points", "6 9 12 15 18 9");
  135. }
  136. }
  137. }