diff --git a/app/Http/Controllers/WebstatementController.php b/app/Http/Controllers/WebstatementController.php index 89c2984..af8f66d 100644 --- a/app/Http/Controllers/WebstatementController.php +++ b/app/Http/Controllers/WebstatementController.php @@ -25,18 +25,22 @@ $jobIds = []; $data = []; - foreach ($this->listAccount() as $accountNumber) { - foreach ($this->listPeriod() as $period) { - $job = new ExportStatementJob( - $accountNumber, - $period, - $this->getAccountBalance($accountNumber, $period) - ); - $jobIds[] = app(Dispatcher::class)->dispatch($job); - $data[] = [ - 'account_number' => $accountNumber, - 'period' => $period - ]; + foreach ($this->listAccount() as $clientName => $accounts) { + foreach ($accounts as $accountNumber) { + foreach ($this->listPeriod() as $period) { + $job = new ExportStatementJob( + $accountNumber, + $period, + $this->getAccountBalance($accountNumber, $period), + $clientName // Pass the client name to the job + ); + $jobIds[] = app(Dispatcher::class)->dispatch($job); + $data[] = [ + 'client_name' => $clientName, + 'account_number' => $accountNumber, + 'period' => $period + ]; + } } } @@ -45,9 +49,10 @@ 'jobs' => array_map(function ($index, $jobId) use ($data) { return [ 'job_id' => $jobId, + 'client_name' => $data[$index]['client_name'], 'account_number' => $data[$index]['account_number'], 'period' => $data[$index]['period'], - 'file_name' => "{$data[$index]['account_number']}_{$data[$index]['period']}.csv" + 'file_name' => "{$data[$index]['client_name']}_{$data[$index]['account_number']}_{$data[$index]['period']}.csv" ]; }, array_keys($jobIds), $jobIds) ]); @@ -55,57 +60,67 @@ function listAccount(){ return [ - '1080426085', - '1080425781', - - '1081647484', - '1081647485', - - '1083123710', - '1083123711', - '1083123712', - '1083123713', - '1083123714', - '1083123715', - '1083123716', - '1083123718', - '1083123719', - '1083123721', - '1083123722', - '1083123723', - '1083123724', - '1083123726', - '1083123727', - '1083123728', - '1083123730', - '1083123731', - '1083123732', - '1083123734', - '1083123735', - - '1086677889', - '1086677890', - '1086677891', - '1086677892', - '1086677893', - '1086677894', - '1086677895', - '1086677896', - '1086677897', - - '1080119298', - '1080119361', - '1080119425', - '1080119387', - '1082208069', - - '1085151668', - - '1085368601', - - '1078333878', - - '0081272689' + 'OY' => [ + '1080426085', + '1080425781', + ], + 'PLUANG' => [ + '1081647484', + '1081647485', + ], + 'INDORAYA' => [ + '1083123710', + '1083123711', + '1083123712', + '1083123713', + '1083123714', + '1083123715', + '1083123716', + '1083123718', + '1083123719', + '1083123721', + '1083123722', + '1083123723', + '1083123724', + '1083123726', + '1083123727', + '1083123728', + '1083123730', + '1083123731', + '1083123732', + '1083123734', + '1083123735', + ], + 'TDC' => [ + '1086677889', + '1086677890', + '1086677891', + '1086677892', + '1086677893', + '1086677894', + '1086677895', + '1086677896', + '1086677897', + ], + 'ASIA_PARKING' => [ + '1080119298', + '1080119361', + '1080119425', + '1080119387', + '1082208069', + ], + 'DAU' => [ + '1085151668', + ], + 'EGR' => [ + '1085368601', + ], + 'SARANA_PACTINDO' => [ + '1078333878', + ], + 'SWADAYA_PANDU' => [ + '0081272689', + ] ]; } @@ -126,4 +141,3 @@ return $accountBalance->actual_balance ?? 0; } } - diff --git a/app/Jobs/ExportStatementJob.php b/app/Jobs/ExportStatementJob.php index 97a4fca..b1e59ff 100644 --- a/app/Jobs/ExportStatementJob.php +++ b/app/Jobs/ExportStatementJob.php @@ -26,6 +26,7 @@ protected $period; protected $saldo; protected $disk; + protected $client; protected $fileName; protected $chunkSize = 1000; // Proses data dalam chunk untuk mengurangi penggunaan memori @@ -37,12 +38,13 @@ * @param string $saldo * @param string $disk */ - public function __construct(string $account_number, string $period, string $saldo, string $disk = 'local') + public function __construct(string $account_number, string $period, string $saldo, string $client = '', string $disk = 'local') { $this->account_number = $account_number; $this->period = $period; $this->saldo = $saldo; $this->disk = $disk; + $this->client = $client; $this->fileName = "{$account_number}_{$period}.csv"; } @@ -298,9 +300,25 @@ private function exportToCsv() : void { + // Determine the base path based on client + $basePath = !empty($this->client) + ? "statements/{$this->client}" + : "statements"; + + // Create client directory if it doesn't exist + if (!empty($this->client)) { + Storage::disk($this->disk)->makeDirectory($basePath); + } + + // Create account directory + $accountPath = "{$basePath}/{$this->account_number}"; + Storage::disk($this->disk)->makeDirectory($accountPath); + + $filePath = "{$accountPath}/{$this->fileName}"; + // Delete existing file if it exists - if (Storage::disk($this->disk)->exists("statements/{$this->fileName}")) { - Storage::disk($this->disk)->delete("statements/{$this->fileName}"); + if (Storage::disk($this->disk)->exists($filePath)) { + Storage::disk($this->disk)->delete($filePath); } $csvContent = "NO|TRANSACTION.DATE|REFERENCE.NUMBER|TRANSACTION.AMOUNT|TRANSACTION.TYPE|DESCRIPTION|END.BALANCE|ACTUAL.DATE\n"; @@ -309,7 +327,7 @@ ProcessedStatement::where('account_number', $this->account_number) ->where('period', $this->period) ->orderBy('sequence_no') - ->chunk($this->chunkSize, function ($statements) use (&$csvContent) { + ->chunk($this->chunkSize, function ($statements) use (&$csvContent, $filePath) { foreach ($statements as $statement) { $csvContent .= implode('|', [ $statement->sequence_no, @@ -324,11 +342,11 @@ } // Tulis ke file secara bertahap untuk mengurangi penggunaan memori - Storage::disk($this->disk)->append("statements/{$this->fileName}", $csvContent); + Storage::disk($this->disk)->append($filePath, $csvContent); $csvContent = ''; // Reset content setelah ditulis }); - Log::info("Statement exported to {$this->disk} disk: statements/{$this->fileName}"); + Log::info("Statement exported to {$this->disk} disk: {$filePath}"); } /**