transactions.js 5.5 KB

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