feat(webstatement): update print statement functionalities

- Menambahkan kolom `remarks` pada tabel print_statement_logs untuk menyimpan catatan tambahan.
- Mengubah validasi periode pada `PrintStatementRequest` untuk mencegah request duplikasi periode.
- Memperbaiki tampilan di `statements.index` dan `statements.show` agar lebih responsif dan informatif.
- Mengubah logika download statement untuk mendukung file range periode dalam format zip.
- Menambahkan logika cek file statement berdasarkan ketersediaan file di storage SFTP.
- Menghapus file legacy `create.blade.php` yang tidak lagi digunakan.
- Menyesuaikan ikon menu dari `calendar` ke `printer` agar lebih relevan.

Signed-off-by: Daeng Deni Mardaeni <ddeni05@gmail.com>
This commit is contained in:
Daeng Deni Mardaeni
2025-05-13 14:01:41 +07:00
parent 823ccf0fe7
commit 8e5c2ce79e
8 changed files with 485 additions and 133 deletions

View File

@@ -5,9 +5,11 @@
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
use Modules\Webstatement\Http\Requests\PrintStatementRequest;
use Modules\Webstatement\Models\PrintStatementLog;
use Modules\Basicdata\Models\Branch;
use phpseclib3\Net\SFTP;
class PrintStatementController extends Controller
{
@@ -49,7 +51,7 @@
// Process statement availability check (this would be implemented based on your system)
$this->checkStatementAvailability($statement);
return redirect()->route('webstatement.statements.show', $statement->id)
return redirect()->route('statements.index')
->with('success', 'Statement request has been created successfully.');
}
@@ -72,9 +74,9 @@
return back()->with('error', 'Statement is not available for download.');
}
if ($statement->authorization_status !== 'approved') {
/* if ($statement->authorization_status !== 'approved') {
return back()->with('error', 'Statement download requires authorization.');
}
}*/
// Update download status
$statement->update([
@@ -84,10 +86,75 @@
]);
// Generate or fetch the statement file (implementation depends on your system)
$filePath = $this->generateStatementFile($statement);
$disk = Storage::disk('sftpStatement');
$filePath = "{$statement->period_from}/Print/{$statement->branch_code}/{$statement->account_number}.pdf";
// Return file download response
return response()->download($filePath, $this->generateFileName($statement));
if ($statement->is_period_range && $statement->period_to) {
$periodFrom = \Carbon\Carbon::createFromFormat('Ym', $statement->period_from);
$periodTo = \Carbon\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 . "/Print/{$statement->branch_code}/{$statement->account_number}.pdf";
if ($disk->exists($periodPath)) {
$availablePeriods[] = $periodFormatted;
} else {
$missingPeriods[] = $periodFormatted;
}
}
// If any period is missing, the statement is not available
if (count($availablePeriods) > 0) {
// Create a temporary zip file
$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);
}
// Create a new zip archive
$zip = new \ZipArchive();
if ($zip->open($zipFilePath, \ZipArchive::CREATE | \ZipArchive::OVERWRITE) === TRUE) {
// Add each available statement to the zip
foreach ($availablePeriods as $period) {
$filePath = "{$period}/Print/{$statement->branch_code}/{$statement->account_number}.pdf";
$localFilePath = storage_path("app/temp/{$statement->account_number}_{$period}.pdf");
// Download the file from SFTP to local storage temporarily
file_put_contents($localFilePath, $disk->get($filePath));
// Add the file to the zip
$zip->addFile($localFilePath, "{$statement->account_number}_{$period}.pdf");
}
$zip->close();
// Clean up temporary files
foreach ($availablePeriods as $period) {
$localFilePath = storage_path("app/temp/{$statement->account_number}_{$period}.pdf");
if (file_exists($localFilePath)) {
unlink($localFilePath);
}
}
// Return the zip file for download
return response()->download($zipFilePath, $zipFileName)->deleteFileAfterSend(true);
} else {
return back()->with('error', 'Failed to create zip archive.');
}
} else {
return back()->with('error', 'No statements available for download.');
}
} else if($disk->exists($filePath)) {
return $disk->download($filePath, "{$statement->account_number}_{$statement->period_from}.pdf");
}
}
/**
@@ -111,8 +178,9 @@
$statusText = $request->authorization_status === 'approved' ? 'approved' : 'rejected';
return redirect()->route('webstatement.statements.show', $statement->id)
return redirect()->route('statements.show', $statement->id)
->with('success', "Statement request has been {$statusText} successfully.");
}
/**
@@ -123,12 +191,60 @@
{
// This would be implemented based on your system's logic
// For example, checking an API or database for statement availability
$disk = Storage::disk('sftpStatement');
//format folder /periode/Print/branch_code/account_number.pdf
$filePath = "{$statement->period_from}/Print/{$statement->branch_code}/{$statement->account_number}.pdf";
// Check if the statement exists in the storage
if ($statement->is_period_range && $statement->period_to) {
$periodFrom = \Carbon\Carbon::createFromFormat('Ym', $statement->period_from);
$periodTo = \Carbon\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 . "/Print/{$statement->branch_code}/{$statement->account_number}.pdf";
if ($disk->exists($periodPath)) {
$availablePeriods[] = $periodFormatted;
} else {
$missingPeriods[] = $periodFormatted;
}
}
// If any period is missing, the statement is not available
if (count($missingPeriods) > 0) {
$notes = "Missing periods: " . implode(', ', $missingPeriods);
$statement->update([
'is_available' => false,
'remarks' => $notes,
'updated_by' => Auth::id()
]);
return;
} else {
// All periods are available
$statement->update([
'is_available' => true,
'updated_by' => Auth::id()
]);
}
} else if($disk->exists($filePath)) {
$statement->update([
'is_available' => true,
'updated_by' => Auth::id()
]);
return;
}
// Placeholder implementation - set to available for demo
$statement->update([
'is_available' => true,
'is_available' => false,
'updated_by' => Auth::id()
]);
return;
}
/**
@@ -213,6 +329,7 @@
'account' => 'account_number',
'period' => 'period_from',
'status' => 'authorization_status',
'remarks' =>'remarks',
// Add more mappings as needed
];
@@ -254,10 +371,11 @@
'authorization_status' => $item->authorization_status,
'is_available' => $item->is_available,
'is_downloaded' => $item->is_downloaded,
'created_at' => $item->created_at->format('Y-m-d H:i:s'),
'created_at' => dateFormat($item->created_at,1,1),
'created_by' => $item->user->name ?? 'N/A',
'authorized_by' => $item->authorizer ? $item->authorizer->name : null,
'authorized_at' => $item->authorized_at ? $item->authorized_at->format('Y-m-d H:i:s') : null,
'remarks' => $item->remarks,
];
});
@@ -278,4 +396,13 @@
'data' => $data,
]);
}
public function destroy(PrintStatementLog $statement){
// Delete the statement
$statement->delete();
return response()->json([
'message' => 'Statement deleted successfully.',
]);
}
}