transactions.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. module.exports = {
  2. isPopulated: false,
  3. display: function(){
  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 = ()=>{transactionDetailsComp.display(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. this.isPopulated = true;
  53. }
  54. },
  55. submitFilter: function(){
  56. event.preventDefault();
  57. let data = {
  58. startDate: document.getElementById("transFilDate1").valueAsDate,
  59. endDate: document.getElementById("transFilDate2").valueAsDate,
  60. recipes: []
  61. }
  62. if(data.startDate >= data.endDate){
  63. banner.createError("START DATE CANNOT BE AFTER END DATE");
  64. return;
  65. }
  66. let recipeChoices = document.getElementById("recipeDropDown");
  67. for(let i = 0; i < recipeChoices.children.length; i += 3){
  68. if(recipeChoices.children[i].checked){
  69. data.recipes.push(recipeChoices.children[i].recipe.id);
  70. }
  71. }
  72. if(data.recipes.length === 0){
  73. for(let i = 0; i < merchant.recipes.length; i++){
  74. data.recipes.push(merchant.recipes[i].id);
  75. }
  76. }
  77. let loader = document.getElementById("loaderContainer");
  78. loader.style.display = "flex";
  79. fetch("/transaction", {
  80. method: "POST",
  81. headers: {
  82. "Content-Type": "application/json;charset=utf-8"
  83. },
  84. body: JSON.stringify(data)
  85. })
  86. .then((response) => response.json())
  87. .then((response)=>{
  88. if(typeof(response) === "string"){
  89. banner.createError(response);
  90. }else{
  91. let transactionList = document.getElementById("transactionsList");
  92. let template = document.getElementById("transaction").content.children[0];
  93. while(transactionList.children.length > 0){
  94. transactionList.removeChild(transactionList.firstChild);
  95. }
  96. for(let i = 0; i < response.length; i++){
  97. let transactionDiv = template.cloneNode(true);
  98. let recipeCount = 0;
  99. let cost = 0;
  100. let transaction = new Transaction(
  101. response[i]._id,
  102. response[i].date,
  103. response[i].recipes,
  104. merchant
  105. );
  106. for(let j = 0; j < transaction.recipes.length; j++){
  107. recipeCount += transaction.recipes[j].quantity;
  108. cost += transaction.recipes[j].quantity * transaction.recipes[j].recipe.price;
  109. }
  110. transactionDiv.children[0].innerText = `${transaction.date.toLocaleDateString()} ${transaction.date.toLocaleTimeString()}`;
  111. transactionDiv.children[1].innerText = `${recipeCount} recipes sold`;
  112. transactionDiv.children[2].innerText = `$${(cost / 100).toFixed(2)}`;
  113. transactionDiv.onclick = ()=>{transactionDetailsComp.display(transaction)};
  114. transactionList.appendChild(transactionDiv);
  115. }
  116. }
  117. })
  118. .catch((err)=>{
  119. banner.createError("UNABLE TO DISPLAY THE TRANSACTIONS");
  120. })
  121. .finally(()=>{
  122. loader.style.display = "none";
  123. });
  124. },
  125. toggleDropdown: function(dropdown){
  126. event.preventDefault();
  127. let polyline = dropdown.parentElement.children[0].children[1].children[0].children[0];
  128. if(dropdown.style.display === "none"){
  129. dropdown.style.display = "block";
  130. polyline.setAttribute("points", "18 15 12 9 6 15");
  131. }else{
  132. dropdown.style.display = "none";
  133. polyline.setAttribute("points", "6 9 12 15 18 9");
  134. }
  135. }
  136. }