Perubahan yang dilakukan: - Menambahkan parameter `group` sebagai argument wajib pada signature command - Memperbarui deskripsi command untuk mencantumkan informasi tentang parameter `group` - Menambahkan validasi `group` agar hanya menerima nilai yang diizinkan: `QRIS` dan `DEFAULT` - Memperbarui method `validateParameters()` untuk mendukung validasi nilai `group` - Memperbarui method `createReportLog()` agar menyimpan `group_name` ke database - Menambahkan `group` ke semua entri log untuk keperluan tracking dan debugging - Menyesuaikan pemanggilan `GenerateClosingBalanceReportJob` dengan menyertakan parameter `group` - Menambahkan informasi `group` pada pesan output console untuk feedback pengguna - Menjamin konsistensi dengan implementasi `GenerateClosingBalanceReportJob` yang telah mendukung parameter `group` - Meningkatkan fleksibilitas command untuk mendukung multiple jenis transaksi Manfaat: - Memungkinkan generate laporan closing balance berdasarkan tipe transaksi - Logging lebih informatif dan terstruktur berdasarkan kelompok transaksi - Command lebih fleksibel dan extensible untuk kebutuhan selanjutnya - Validasi ketat memastikan data yang diproses sesuai spesifikasi sistem Refs: #closing-balance-refactor
230 lines
7.7 KiB
PHP
230 lines
7.7 KiB
PHP
<?php
|
|
|
|
namespace Modules\Webstatement\Console;
|
|
|
|
use Exception;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Modules\Usermanagement\Models\User;
|
|
use Modules\Webstatement\Jobs\GenerateClosingBalanceReportJob;
|
|
use Modules\Webstatement\Models\ClosingBalanceReportLog;
|
|
use Carbon\Carbon;
|
|
|
|
/**
|
|
* Console command untuk generate laporan closing balance
|
|
* Command ini dapat dijalankan secara manual atau dijadwalkan
|
|
*/
|
|
class GenerateClosingBalanceReportCommand extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'webstatement:generate-closing-balance-report
|
|
{account_number : Nomor rekening untuk generate laporan}
|
|
{period : Period laporan format YYYYMMDD, contoh: 20250515}
|
|
{group=DEFAULT : Group transaksi QRIS atau DEFAULT}
|
|
{--user_id=1 : ID user yang menjalankan command (default: 1)}';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Generate Closing Balance report untuk nomor rekening, periode, dan group tertentu';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
* Menjalankan proses generate laporan closing balance
|
|
*
|
|
* @return int
|
|
*/
|
|
public function handle()
|
|
{
|
|
$this->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;
|
|
}
|
|
}
|
|
}
|