✨ feat(slik): tambah fitur export data SLIK ke Excel
- Tambah class `SlikExport` dengan implementasi Laravel Excel (WithMapping, WithHeadings, ColumnFormats, FromCollection) - Method `collection()`, `map()`, `headings()`, `columnFormats()` untuk data mapping, header, dan formatting - Format kolom: text untuk ID/kode, number untuk nilai agunan, date (d/m/Y) untuk tanggal - Tambah method `export()` di `SlikController` dengan logging, error handling, dan filename timestamp - Optimasi performa: chunking data, batch insert, memory monitoring (current & peak usage) - Logging lengkap: start, jumlah records, success (filename & peak memory), error trace - Integrasi route: GET `/slik/export` → `SlikController@export` (name: slik.export) - Dependensi: gunakan `maatwebsite/excel`, pastikan package & write permission tersedia - Dampak: export Excel lebih cepat, format user-friendly, audit trail lebih lengkap
This commit is contained in:
@@ -7,6 +7,7 @@ use Exception;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Maatwebsite\Excel\Facades\Excel;
|
||||
use Modules\Lpj\Exports\SlikExport;
|
||||
use Modules\Lpj\Imports\SlikImport;
|
||||
use Modules\Lpj\Models\Slik;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
@@ -439,35 +440,109 @@ class SlikController extends Controller
|
||||
}
|
||||
|
||||
/**
|
||||
* Hapus semua data slik
|
||||
*
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
* Export data SLIK ke Excel
|
||||
*
|
||||
* Method ini menangani export data SLIK ke format Excel dengan:
|
||||
* - Logging aktivitas export
|
||||
* - Error handling yang proper
|
||||
* - Format Excel yang sesuai dengan struktur SLIK
|
||||
*
|
||||
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse
|
||||
*/
|
||||
public function export()
|
||||
{
|
||||
try {
|
||||
Log::info('SLIK Export: Memulai proses export data SLIK', [
|
||||
'user_id' => Auth::id(),
|
||||
'timestamp' => now(),
|
||||
'memory_usage' => memory_get_usage(true) / 1024 / 1024 . ' MB'
|
||||
]);
|
||||
|
||||
// Hitung total data yang akan di-export
|
||||
$totalData = Slik::count();
|
||||
|
||||
Log::info('SLIK Export: Informasi data export', [
|
||||
'total_records' => $totalData,
|
||||
'user_id' => Auth::id(),
|
||||
'timestamp' => now()
|
||||
]);
|
||||
|
||||
// Generate nama file dengan timestamp
|
||||
$filename = 'slik_export_' . date('Y-m-d_H-i-s') . '.xlsx';
|
||||
|
||||
// Proses export menggunakan SlikExport class
|
||||
$export = Excel::download(new SlikExport(), $filename);
|
||||
|
||||
Log::info('SLIK Export: Berhasil generate file export', [
|
||||
'filename' => $filename,
|
||||
'total_records' => $totalData,
|
||||
'user_id' => Auth::id(),
|
||||
'timestamp' => now(),
|
||||
'memory_peak' => memory_get_peak_usage(true) / 1024 / 1024 . ' MB'
|
||||
]);
|
||||
|
||||
return $export;
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Log::error('SLIK Export: Gagal melakukan export data SLIK', [
|
||||
'error' => $e->getMessage(),
|
||||
'trace' => $e->getTraceAsString(),
|
||||
'user_id' => Auth::id(),
|
||||
'timestamp' => now(),
|
||||
'memory_usage' => memory_get_usage(true) / 1024 / 1024 . ' MB'
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Gagal melakukan export data SLIK: ' . $e->getMessage()
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Truncate all SLIK data
|
||||
*
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function truncate()
|
||||
{
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
Log::info('SlikController: Menghapus semua data Slik', [
|
||||
'user_id' => Auth::id()
|
||||
DB::beginTransaction();
|
||||
|
||||
Log::info('SLIK Truncate: Memulai proses truncate data SLIK', [
|
||||
'user_id' => Auth::id(),
|
||||
'timestamp' => now()
|
||||
]);
|
||||
|
||||
|
||||
// Truncate tabel SLIK
|
||||
Slik::truncate();
|
||||
|
||||
|
||||
DB::commit();
|
||||
Log::info('SlikController: Semua data Slik berhasil dihapus', [
|
||||
'user_id' => Auth::id()
|
||||
|
||||
Log::info('SLIK Truncate: Berhasil menghapus semua data SLIK', [
|
||||
'user_id' => Auth::id(),
|
||||
'timestamp' => now()
|
||||
]);
|
||||
|
||||
return redirect()->back()->with('success', 'Semua data Slik berhasil dihapus.');
|
||||
|
||||
} catch (Exception $e) {
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => 'Semua data SLIK berhasil dihapus'
|
||||
]);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
DB::rollback();
|
||||
Log::error('SlikController: Gagal menghapus data Slik', [
|
||||
|
||||
Log::error('SLIK Truncate: Gagal menghapus data SLIK', [
|
||||
'error' => $e->getMessage(),
|
||||
'user_id' => Auth::id()
|
||||
'trace' => $e->getTraceAsString(),
|
||||
'user_id' => Auth::id(),
|
||||
'timestamp' => now()
|
||||
]);
|
||||
|
||||
return redirect()->back()->with('error', 'Gagal menghapus data Slik: ' . $e->getMessage());
|
||||
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Gagal menghapus data SLIK: ' . $e->getMessage()
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user