add export excel and pdf district payment recaps

This commit is contained in:
arifal
2025-03-19 18:55:51 +07:00
parent 47a9fb1dfb
commit eadfddb3a4
8 changed files with 194 additions and 1 deletions

View File

@@ -7,6 +7,8 @@ class ReportPaymentRecaps {
constructor() {
this.table = null;
this.initTableReportPaymentRecaps();
this.handleExportPDF();
this.handleExportToExcel();
}
initTableReportPaymentRecaps() {
let tableContainer = document.getElementById(
@@ -63,6 +65,100 @@ class ReportPaymentRecaps {
fixedHeader: true,
}).render(tableContainer);
}
async handleExportToExcel() {
const button = document.getElementById("btn-export-excel");
if (!button) {
console.error("Button not found: #btn-export-excel");
return;
}
let exportUrl = button.getAttribute("data-url");
button.addEventListener("click", async () => {
button.disabled = true;
try {
const response = await fetch(`${exportUrl}`, {
method: "GET",
credentials: "include",
headers: {
Authorization: `Bearer ${document
.querySelector('meta[name="api-token"]')
.getAttribute("content")}`,
},
});
if (!response.ok) {
console.error("Error fetching data:", response.statusText);
button.disabled = false;
return;
}
// Convert response to Blob and trigger download
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "laporan-rekap-data-pembayaran.xlsx";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
} catch (error) {
console.error("Error fetching data:", error);
button.disabled = false;
return;
} finally {
button.disabled = false;
}
});
}
async handleExportPDF() {
const button = document.getElementById("btn-export-pdf");
if (!button) {
console.error("Button not found: #btn-export-pdf");
return;
}
let exportUrl = button.getAttribute("data-url");
button.addEventListener("click", async () => {
button.disabled = true;
try {
const response = await fetch(`${exportUrl}`, {
method: "GET",
credentials: "include",
headers: {
Authorization: `Bearer ${document
.querySelector('meta[name="api-token"]')
.getAttribute("content")}`,
},
});
if (!response.ok) {
console.error("Error fetching data:", response.statusText);
button.disabled = false;
return;
}
// Convert response to Blob and trigger download
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "laporan-rekap-data-pembayaran.pdf";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
} catch (error) {
console.error("Error fetching data:", error);
button.disabled = false;
return;
} finally {
button.disabled = false;
}
});
}
}
document.addEventListener("DOMContentLoaded", function (e) {
new ReportPaymentRecaps();