auth()->id(),
'ip_address' => $request->ip()
]);
try {
$branches = Branch::orderBy('name')->get();
return view('webstatement::email-statement-logs.index', compact('branches'));
} catch (\Exception $e) {
Log::error('Failed to load email statement log index page', [
'error' => $e->getMessage(),
'user_id' => auth()->id()
]);
return back()->with('error', 'Gagal memuat halaman log pengiriman email statement.');
}
}
public function dataForDatatables(Request $request)
{
Log::info('Fetching email statement log data for datatables', [
'user_id' => auth()->id(),
'filters' => $request->only(['branch_code', 'account_number', 'period_from', 'period_to', 'request_type', 'status'])
]);
DB::beginTransaction();
try {
$query = PrintStatementLog::query()
->with(['user', 'branch'])
->select([
'id',
'user_id',
'branch_code',
'account_number',
'request_type',
'batch_id',
'total_accounts',
'processed_accounts',
'success_count',
'failed_count',
'status',
'period_from',
'period_to',
'email',
'email_sent_at',
'is_available',
'authorization_status',
'started_at',
'completed_at',
'created_at',
'updated_at'
]);
// Filter berdasarkan branch
if ($request->filled('branch_code')) {
$query->where('branch_code', $request->branch_code);
}
// Filter berdasarkan account number (hanya untuk single account)
if ($request->filled('account_number')) {
$query->where('account_number', 'like', '%' . $request->account_number . '%');
}
// Filter berdasarkan request type
if ($request->filled('request_type')) {
$query->where('request_type', $request->request_type);
}
// Filter berdasarkan status
if ($request->filled('status')) {
$query->where('status', $request->status);
}
// Filter berdasarkan periode
if ($request->filled('period_from')) {
$query->where('period_from', '>=', $request->period_from);
}
if ($request->filled('period_to')) {
$query->where('period_to', '<=', $request->period_to);
}
// Filter berdasarkan tanggal
if ($request->filled('date_from')) {
$query->whereDate('created_at', '>=', $request->date_from);
}
if ($request->filled('date_to')) {
$query->whereDate('created_at', '<=', $request->date_to);
}
$query->orderBy('created_at', 'desc');
$totalRecords = $query->count();
if ($request->filled('start')) {
$query->skip($request->start);
}
if ($request->filled('length') && $request->length != -1) {
$query->take($request->length);
}
$logs = $query->get();
$data = $logs->map(function ($log) {
return [
'id' => $log->id,
'request_type' => $this->formatRequestType($log->request_type),
'branch_code' => $log->branch_code,
'branch_name' => $log->branch->name ?? 'N/A',
'account_number' => $log->account_number ?? '-',
'period_display' => $log->period_display,
'batch_id' => $log->batch_id,
'total_accounts' => $log->total_accounts ?? 1,
'processed_accounts' => $log->processed_accounts ?? 0,
'success_count' => $log->success_count ?? 0,
'failed_count' => $log->failed_count ?? 0,
'progress_percentage' => $log->getProgressPercentage(),
'success_rate' => $log->getSuccessRate(),
'status' => $this->formatStatus($log->status),
'email' => $log->email,
'email_status' => $log->email_sent_at ? 'Terkirim' : 'Pending',
'email_sent_at' => $log->email_sent_at ?? '-',
'authorization_status' => ucfirst($log->authorization_status),
'user_name' => $log->user->name ?? 'System',
'started_at' => $log->started_at ? $log->started_at->format('d/m/Y H:i:s') : '-',
'completed_at' => $log->completed_at ? $log->completed_at->format('d/m/Y H:i:s') : '-',
'created_at' => $log->created_at->format('d/m/Y H:i:s'),
'actions' => $this->generateActionButtons($log)
];
});
DB::commit();
return response()->json([
'draw' => intval($request->draw),
'recordsTotal' => $totalRecords,
'recordsFiltered' => $totalRecords,
'data' => $data
]);
} catch (\Exception $e) {
DB::rollBack();
Log::error('Failed to fetch email statement log data', [
'error' => $e->getMessage(),
'user_id' => auth()->id()
]);
return response()->json([
'draw' => intval($request->draw),
'recordsTotal' => 0,
'recordsFiltered' => 0,
'data' => [],
'error' => 'Gagal memuat data log pengiriman email statement.'
]);
}
}
public function show($id)
{
try {
$log = PrintStatementLog::with(['user', 'branch'])->findOrFail($id);
return view('webstatement::email-statement-logs.show', compact('log'));
} catch (\Exception $e) {
Log::error('Failed to load email statement log detail', [
'log_id' => $id,
'error' => $e->getMessage(),
'user_id' => auth()->id()
]);
return back()->with('error', 'Log pengiriman email statement tidak ditemukan.');
}
}
/**
* Mengirim ulang email statement untuk batch atau single account
*/
public function resendEmail(Request $request, $id)
{
Log::info('Attempting to resend statement email', [
'log_id' => $id,
'user_id' => auth()->id()
]);
DB::beginTransaction();
try {
$log = PrintStatementLog::findOrFail($id);
// Buat batch ID baru untuk resend
$newBatchId = 'resend_' . time() . '_' . $log->id;
// Dispatch job dengan parameter yang sama
SendStatementEmailJob::dispatch(
$log->period_from,
$log->request_type,
$log->request_type === 'single_account' ? $log->account_number :
($log->request_type === 'branch' ? $log->branch_code : null),
$newBatchId,
$log->id
);
// Reset status untuk tracking ulang
$log->update([
'status' => 'pending',
'batch_id' => $newBatchId,
'processed_accounts' => 0,
'success_count' => 0,
'failed_count' => 0,
'started_at' => null,
'completed_at' => null,
'error_message' => null
]);
DB::commit();
Log::info('Statement email resend job dispatched successfully', [
'log_id' => $id,
'new_batch_id' => $newBatchId,
'request_type' => $log->request_type
]);
return back()->with('success', 'Email statement berhasil dijadwalkan untuk dikirim ulang.');
} catch (\Exception $e) {
DB::rollBack();
Log::error('Failed to resend statement email', [
'log_id' => $id,
'error' => $e->getMessage(),
'user_id' => auth()->id()
]);
return back()->with('error', 'Gagal mengirim ulang email statement.');
}
}
private function formatRequestType($requestType)
{
$types = [
'single_account' => 'Single Account',
'branch' => 'Per Cabang',
'all_branches' => 'Seluruh Cabang'
];
return $types[$requestType] ?? $requestType;
}
private function formatStatus($status)
{
$statuses = [
'pending' => 'Pending',
'processing' => 'Processing',
'completed' => 'Completed',
'failed' => 'Failed'
];
return $statuses[$status] ?? $status;
}
private function generateActionButtons(PrintStatementLog $log)
{
$buttons = [];
// Tombol view detail
$buttons[] = '' .
'' .
'';
// Tombol resend email
if (in_array($log->status, ['completed', 'failed']) && $log->authorization_status === 'approved') {
$buttons[] = '';
}
return implode(' ', $buttons);
}
}