transactions.js 6.0 KB

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