info('Starting Closing Balance report generation...'); // Get parameters $accountNumber = $this->argument('account_number'); $period = $this->argument('period'); $group = $this->argument('group'); $userId = $this->option('user_id'); // Validate parameters if (!$this->validateParameters($accountNumber, $period, $group, $userId)) { return Command::FAILURE; } try { DB::beginTransaction(); // Log start of process Log::info('Console command: Starting closing balance report generation', [ 'account_number' => $accountNumber, 'period' => $period, 'group' => $group, 'user_id' => $userId, 'command' => 'webstatement:generate-closing-balance-report' ]); // Create report log entry $reportLog = $this->createReportLog($accountNumber, $period, $group, $userId); if (!$reportLog) { $this->error('Failed to create report log entry'); DB::rollback(); return Command::FAILURE; } // Dispatch the job with group parameter GenerateClosingBalanceReportJob::dispatch($accountNumber, $period, $reportLog->id, $group); DB::commit(); $this->info("Closing Balance report generation job queued successfully!"); $this->info("Account Number: {$accountNumber}"); $this->info("Period: {$period}"); $this->info("Group: {$group}"); $this->info("Report Log ID: {$reportLog->id}"); $this->info('The report will be generated in the background.'); $this->info('Check the closing_balance_report_logs table for progress.'); // Log successful dispatch Log::info('Console command: Closing balance report job dispatched successfully', [ 'account_number' => $accountNumber, 'period' => $period, 'group' => $group, 'report_log_id' => $reportLog->id, 'user_id' => $userId ]); return Command::SUCCESS; } catch (Exception $e) { DB::rollback(); $this->error('Error queuing Closing Balance report job: ' . $e->getMessage()); // Log error Log::error('Console command: Error generating closing balance report', [ 'account_number' => $accountNumber, 'period' => $period, 'group' => $group, 'user_id' => $userId, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); return Command::FAILURE; } } /** * Validate command parameters * Validasi parameter command * * @param string $accountNumber * @param string $period * @param string $group * @param int $userId * @return bool */ private function validateParameters(string $accountNumber, string $period, string $group, int $userId): bool { // Validate account number if (empty($accountNumber)) { $this->error('Account number parameter is required.'); return false; } // Validate period format (YYYYMMDD) if (!preg_match('/^\\d{8}$/', $period)) { $this->error('Invalid period format. Use YYYYMMDD format (example: 20250515)'); return false; } // Validate date $year = substr($period, 0, 4); $month = substr($period, 4, 2); $day = substr($period, 6, 2); if (!checkdate($month, $day, $year)) { $this->error('Invalid date in period parameter.'); return false; } // Validate group parameter $allowedGroups = ['QRIS', 'DEFAULT']; if (!in_array(strtoupper($group), $allowedGroups)) { $this->error('Invalid group parameter. Allowed values: ' . implode(', ', $allowedGroups)); return false; } // Validate user exists $user = User::find($userId); if (!$user) { $this->error("User with ID {$userId} not found."); return false; } return true; } /** * Create report log entry * Membuat entry log laporan * * @param string $accountNumber * @param string $period * @param string $group * @param int $userId * @return ClosingBalanceReportLog|null */ private function createReportLog(string $accountNumber, string $period, string $group, int $userId): ?ClosingBalanceReportLog { try { // Convert period string to Carbon date $reportDate = Carbon::createFromFormat('Ymd', $period); $reportLog = ClosingBalanceReportLog::create([ 'account_number' => $accountNumber, 'period' => $period, 'report_date' => $reportDate, // Required field yang sebelumnya missing 'group_name' => strtoupper($group), // Tambahkan group_name ke log 'status' => 'pending', 'user_id' => $userId, 'created_by' => $userId, // Required field yang sebelumnya missing 'updated_by' => $userId, 'ip_address' => request()->ip() ?? '127.0.0.1', // Default untuk console 'user_agent' => 'Console Command', 'created_at' => now(), 'updated_at' => now() ]); Log::info('Console command: Report log created', [ 'report_log_id' => $reportLog->id, 'account_number' => $accountNumber, 'period' => $period, 'group' => $group, 'report_date' => $reportDate->format('Y-m-d'), 'user_id' => $userId ]); return $reportLog; } catch (Exception $e) { Log::error('Console command: Error creating report log', [ 'account_number' => $accountNumber, 'period' => $period, 'group' => $group, 'user_id' => $userId, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); return null; } } }