feat(webstatement): tambahkan fitur konfirmasi email dan optimasi proses download statement
- **Penambahan Fitur Konfirmasi Email:**
- Menambahkan event listener untuk form submit:
- Menampilkan SweetAlert jika field email telah diisi.
- Mengonfirmasi pengiriman statement ke alamat email yang diisi pengguna.
- Submit form hanya setelah user mengonfirmasi.
- **Optimalisasi Proses Download Statement:**
- Menangani logic download statement dalam rentang periode (period range):
- Mencatat log keberadaan file untuk setiap periode.
- Membuat file ZIP yang berisi semua file statement yang tersedia dalam rentang tersebut.
- Mengelola file sementara untuk proses kompresi dengan pembersihan otomatis.
- Menambahkan log error dan warning untuk file yang hilang dalam rentang periode.
- Mendukung mekanisme download file tunggal untuk periode tertentu.
- Menyesuaikan log dengan detail proses, seperti:
- Informasi periode yang tersedia dan tidak.
- Notifikasi penyelesaian atau kegagalan proses download ZIP.
- Menambahkan logging trace pada exception untuk debugging lebih rinci.
- **Perubahan Validasi Logic:**
- Validasi baru pada `PrintStatementRequest`:
- Menentukan `is_period_range` hanya jika `period_to` berbeda dengan `period_from`.
- **Perbaikan dan Penyesuaian Pengiriman Email:**
- Menambahkan pengecekan field email sebelum menjalankan fungsi kirim email di `PrintStatementController`.
- Mengintegrasikan fungsi `sendEmail` jika terdapat email pada statement.
- **Penambahan Dokumentasi Kode:**
- Menambahkan komentar inline di beberapa bagian:
- Logika konfirmasi email.
- Proses pembuatan ZIP dan penanganan download.
- Menjelaskan tiap langkah operasional untuk mempermudah pemahaman dan debugging.
Perubahan ini mengintegrasikan fitur konfirmasi email yang lebih interaktif, meningkatkan proses download statement berjenjang, serta memperbaiki validasi dan logging pada tiap langkah proses.
Signed-off-by: Daeng Deni Mardaeni <ddeni05@gmail.com>
This commit is contained in:
@@ -107,6 +107,10 @@
|
||||
// Process statement availability check
|
||||
$this->checkStatementAvailability($statement);
|
||||
|
||||
$statement = PrintStatementLog::find($statement->id);
|
||||
if($statement->email){
|
||||
$this->sendEmail($statement->id);
|
||||
}
|
||||
DB::commit();
|
||||
|
||||
return redirect()->route('statements.index')
|
||||
@@ -287,17 +291,145 @@
|
||||
$filePath = "{$statement->period_from}/{$statement->branch_code}/{$statement->account_number}_{$statement->period_from}.pdf";
|
||||
|
||||
if ($statement->is_period_range && $statement->period_to) {
|
||||
// Handle period range download (existing logic)
|
||||
// ... existing zip creation logic ...
|
||||
// Log: Memulai proses download period range
|
||||
Log::info('Starting period range download', [
|
||||
'statement_id' => $statement->id,
|
||||
'period_from' => $statement->period_from,
|
||||
'period_to' => $statement->period_to
|
||||
]);
|
||||
|
||||
/**
|
||||
* Handle period range download dengan membuat zip file
|
||||
* yang berisi semua statement dalam rentang periode
|
||||
*/
|
||||
$periodFrom = Carbon::createFromFormat('Ym', $statement->period_from);
|
||||
$periodTo = Carbon::createFromFormat('Ym', $statement->period_to);
|
||||
|
||||
// Loop through each month in the range
|
||||
$missingPeriods = [];
|
||||
$availablePeriods = [];
|
||||
|
||||
for ($period = clone $periodFrom; $period->lte($periodTo); $period->addMonth()) {
|
||||
$periodFormatted = $period->format('Ym');
|
||||
$periodPath = $periodFormatted . "/{$statement->branch_code}/{$statement->account_number}_{$periodFormatted}.pdf";
|
||||
|
||||
if ($disk->exists($periodPath)) {
|
||||
$availablePeriods[] = $periodFormatted;
|
||||
Log::info('Period available for download', [
|
||||
'period' => $periodFormatted,
|
||||
'path' => $periodPath
|
||||
]);
|
||||
} else {
|
||||
$missingPeriods[] = $periodFormatted;
|
||||
Log::warning('Period not available for download', [
|
||||
'period' => $periodFormatted,
|
||||
'path' => $periodPath
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
// If any period is available, create a zip and download it
|
||||
if (count($availablePeriods) > 0) {
|
||||
/**
|
||||
* Membuat zip file temporary untuk download
|
||||
* dengan semua statement yang tersedia dalam periode
|
||||
*/
|
||||
$zipFileName = "{$statement->account_number}_{$statement->period_from}_to_{$statement->period_to}.zip";
|
||||
$zipFilePath = storage_path("app/temp/{$zipFileName}");
|
||||
|
||||
// Ensure the temp directory exists
|
||||
if (!file_exists(storage_path('app/temp'))) {
|
||||
mkdir(storage_path('app/temp'), 0755, true);
|
||||
Log::info('Created temp directory for zip files');
|
||||
}
|
||||
|
||||
// Create a new zip archive
|
||||
$zip = new ZipArchive();
|
||||
if ($zip->open($zipFilePath, ZipArchive::CREATE | ZipArchive::OVERWRITE) === true) {
|
||||
Log::info('Zip archive created successfully', ['zip_path' => $zipFilePath]);
|
||||
|
||||
// Add each available statement to the zip
|
||||
foreach ($availablePeriods as $period) {
|
||||
$periodFilePath = "{$period}/{$statement->branch_code}/{$statement->account_number}_{$period}.pdf";
|
||||
$localFilePath = storage_path("app/temp/{$statement->account_number}_{$period}.pdf");
|
||||
|
||||
try {
|
||||
// Download the file from SFTP to local storage temporarily
|
||||
file_put_contents($localFilePath, $disk->get($periodFilePath));
|
||||
|
||||
// Add the file to the zip
|
||||
$zip->addFile($localFilePath, "{$statement->account_number}_{$period}.pdf");
|
||||
|
||||
Log::info('Added file to zip', [
|
||||
'period' => $period,
|
||||
'local_path' => $localFilePath
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
Log::error('Failed to add file to zip', [
|
||||
'period' => $period,
|
||||
'error' => $e->getMessage()
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
$zip->close();
|
||||
Log::info('Zip archive closed successfully');
|
||||
|
||||
// Return the zip file for download
|
||||
$response = response()->download($zipFilePath, $zipFileName)->deleteFileAfterSend(true);
|
||||
|
||||
// Clean up temporary PDF files
|
||||
foreach ($availablePeriods as $period) {
|
||||
$localFilePath = storage_path("app/temp/{$statement->account_number}_{$period}.pdf");
|
||||
if (file_exists($localFilePath)) {
|
||||
unlink($localFilePath);
|
||||
Log::info('Cleaned up temporary file', ['file' => $localFilePath]);
|
||||
}
|
||||
}
|
||||
|
||||
Log::info('Period range download completed successfully', [
|
||||
'statement_id' => $statement->id,
|
||||
'available_periods' => count($availablePeriods),
|
||||
'missing_periods' => count($missingPeriods)
|
||||
]);
|
||||
|
||||
return $response;
|
||||
} else {
|
||||
Log::error('Failed to create zip archive', ['zip_path' => $zipFilePath]);
|
||||
return back()->with('error', 'Failed to create zip archive for download.');
|
||||
}
|
||||
} else {
|
||||
Log::warning('No statements available for download in period range', [
|
||||
'statement_id' => $statement->id,
|
||||
'missing_periods' => $missingPeriods
|
||||
]);
|
||||
return back()->with('error', 'No statements available for download in the specified period range.');
|
||||
}
|
||||
} else if ($disk->exists($filePath)) {
|
||||
/**
|
||||
* Handle single period download
|
||||
* Download file PDF tunggal untuk periode tertentu
|
||||
*/
|
||||
Log::info('Single period download', [
|
||||
'statement_id' => $statement->id,
|
||||
'file_path' => $filePath
|
||||
]);
|
||||
|
||||
return $disk->download($filePath, "{$statement->account_number}_{$statement->period_from}.pdf");
|
||||
} else {
|
||||
Log::warning('Statement file not found', [
|
||||
'statement_id' => $statement->id,
|
||||
'file_path' => $filePath
|
||||
]);
|
||||
return back()->with('error', 'Statement file not found.');
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
DB::rollBack();
|
||||
|
||||
Log::error('Failed to download statement', [
|
||||
'statement_id' => $statement->id,
|
||||
'error' => $e->getMessage()
|
||||
'error' => $e->getMessage(),
|
||||
'trace' => $e->getTraceAsString()
|
||||
]);
|
||||
|
||||
return back()->with('error', 'Failed to download statement: ' . $e->getMessage());
|
||||
@@ -481,6 +613,7 @@
|
||||
public function sendEmail($id)
|
||||
{
|
||||
$statement = PrintStatementLog::findOrFail($id);
|
||||
|
||||
// Check if statement has email
|
||||
if (empty($statement->email)) {
|
||||
return redirect()->back()->with('error', 'No email address provided for this statement.');
|
||||
|
||||
@@ -104,14 +104,14 @@ class PrintStatementRequest extends FormRequest
|
||||
$this->merge([
|
||||
'period_to' => substr($this->period_to, 0, 4) . substr($this->period_to, 5, 2),
|
||||
]);
|
||||
}
|
||||
|
||||
// Convert is_period_range to boolean if it exists
|
||||
if ($this->has('period_to')) {
|
||||
// Only set is_period_range to true if period_to is different from period_from
|
||||
if ($this->period_to !== $this->period_from) {
|
||||
$this->merge([
|
||||
'is_period_range' => true,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
// Set default request_type if not provided
|
||||
if (!$this->has('request_type')) {
|
||||
|
||||
@@ -57,7 +57,7 @@
|
||||
@enderror
|
||||
</div>
|
||||
|
||||
<div class="form-group" style="display:none">
|
||||
<div class="form-group">
|
||||
<label class="form-label" for="email">Email</label>
|
||||
<input type="email" class="input form-control @error('email') is-invalid @enderror"
|
||||
id="email" name="email" value="{{ old('email', $statement->email ?? '') }}"
|
||||
@@ -177,6 +177,10 @@
|
||||
|
||||
@push('scripts')
|
||||
<script type="text/javascript">
|
||||
/**
|
||||
* Fungsi untuk menghapus data statement
|
||||
* @param {number} data - ID statement yang akan dihapus
|
||||
*/
|
||||
function deleteData(data) {
|
||||
Swal.fire({
|
||||
title: 'Are you sure?',
|
||||
@@ -207,6 +211,56 @@
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Konfirmasi email sebelum submit form
|
||||
* Menampilkan SweetAlert jika email diisi untuk konfirmasi pengiriman
|
||||
*/
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
const form = document.querySelector('form');
|
||||
const emailInput = document.getElementById('email');
|
||||
|
||||
// Log: Inisialisasi event listener untuk konfirmasi email
|
||||
console.log('Email confirmation listener initialized');
|
||||
|
||||
form.addEventListener('submit', function(e) {
|
||||
const emailValue = emailInput.value.trim();
|
||||
|
||||
// Jika email diisi, tampilkan konfirmasi
|
||||
if (emailValue) {
|
||||
e.preventDefault(); // Hentikan submit form sementara
|
||||
|
||||
// Log: Email terdeteksi, menampilkan konfirmasi
|
||||
console.log('Email detected:', emailValue);
|
||||
|
||||
Swal.fire({
|
||||
title: 'Konfirmasi Pengiriman Email',
|
||||
text: `Apakah Anda yakin ingin mengirimkan statement ke email: ${emailValue}?`,
|
||||
icon: 'question',
|
||||
showCancelButton: true,
|
||||
confirmButtonColor: '#3085d6',
|
||||
cancelButtonColor: '#d33',
|
||||
confirmButtonText: 'Ya, Kirim Email',
|
||||
cancelButtonText: 'Batal',
|
||||
reverseButtons: true
|
||||
}).then((result) => {
|
||||
if (result.isConfirmed) {
|
||||
// Log: User konfirmasi pengiriman email
|
||||
console.log('User confirmed email sending');
|
||||
|
||||
// Submit form setelah konfirmasi
|
||||
form.submit();
|
||||
} else {
|
||||
// Log: User membatalkan pengiriman email
|
||||
console.log('User cancelled email sending');
|
||||
}
|
||||
});
|
||||
} else {
|
||||
// Log: Tidak ada email, submit form normal
|
||||
console.log('No email provided, submitting form normally');
|
||||
}
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
||||
<script type="module">
|
||||
|
||||
Reference in New Issue
Block a user