Menambahkan fitur command line untuk generate laporan closing balance sekaligus memperbaiki pengisian field yang required di database. Perubahan yang dilakukan: - Membuat command `webstatement:generate-closing-balance-report` dengan parameter: - `account_number`: nomor rekening (required) - `period`: format tanggal YYYYMMDD (required) - `--user_id=`: ID user (optional, default 1) - Menambahkan field `report_date` dengan konversi dari parameter `period` menggunakan Carbon - Menambahkan field `created_by` dan `updated_by` untuk kebutuhan audit trail - Menambahkan field `ip_address` dan `user_agent` dengan default 'console' untuk identifikasi proses non-web - Memperbaiki validasi parameter dengan regex dan proper escaping - Menghindari error SQLSTATE[23502] terkait field not null di database schema - Menggunakan database transaction untuk menjaga konsistensi data - Mengupdate fungsi `closing_balance_report_logs` untuk menyimpan semua field yang dibutuhkan - Integrasi dengan `GenerateClosingBalanceReportJob` untuk pemrosesan laporan secara background - Menambahkan logging komprehensif untuk monitoring `report_date` dan proses lainnya - Mendukung eksekusi manual dan penjadwalan via Laravel scheduler - Kompatibel dengan proses laporan closing balance via web dan CLI Tujuan perubahan: - Mempermudah proses generate laporan closing balance melalui CLI secara manual atau terjadwal - Memastikan seluruh field wajib di `closing_balance_report_logs` terisi dengan benar - Menyediakan audit trail lengkap dan logging yang detail untuk proses via console - Meningkatkan keandalan sistem dengan validasi dan error handling yang lebih baik
212 lines
6.9 KiB
PHP
212 lines
6.9 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}
|
|
{--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 dan periode 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');
|
|
$userId = $this->option('user_id');
|
|
|
|
// Validate parameters
|
|
if (!$this->validateParameters($accountNumber, $period, $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,
|
|
'user_id' => $userId,
|
|
'command' => 'webstatement:generate-closing-balance-report'
|
|
]);
|
|
|
|
// Create report log entry
|
|
$reportLog = $this->createReportLog($accountNumber, $period, $userId);
|
|
|
|
if (!$reportLog) {
|
|
$this->error('Failed to create report log entry');
|
|
DB::rollback();
|
|
return Command::FAILURE;
|
|
}
|
|
|
|
// Dispatch the job
|
|
GenerateClosingBalanceReportJob::dispatch($accountNumber, $period, $reportLog->id);
|
|
|
|
DB::commit();
|
|
|
|
$this->info("Closing Balance report generation job queued successfully!");
|
|
$this->info("Account Number: {$accountNumber}");
|
|
$this->info("Period: {$period}");
|
|
$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,
|
|
'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,
|
|
'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 int $userId
|
|
* @return bool
|
|
*/
|
|
private function validateParameters(string $accountNumber, string $period, 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 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 int $userId
|
|
* @return ClosingBalanceReportLog|null
|
|
*/
|
|
private function createReportLog(string $accountNumber, string $period, 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
|
|
'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,
|
|
'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,
|
|
'user_id' => $userId,
|
|
'error' => $e->getMessage(),
|
|
'trace' => $e->getTraceAsString()
|
|
]);
|
|
|
|
return null;
|
|
}
|
|
}
|
|
}
|