transactions.js 5.7 KB

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