diff --git a/app/Http/Controllers/PrintStatementController.php b/app/Http/Controllers/PrintStatementController.php index 0a8b0bd..18f66ae 100644 --- a/app/Http/Controllers/PrintStatementController.php +++ b/app/Http/Controllers/PrintStatementController.php @@ -172,7 +172,11 @@ ini_set('max_execution_time', 300000); try { $disk = Storage::disk('sftpStatement'); - $filePath = "{$statement->period_from}/{$statement->branch_code}/{$statement->account_number}_{$statement->period_from}.pdf"; + + // Convert period format from YYYYMM to YYYYMMDD.YYYYMMDD for folder path + $periodPath = formatPeriodForFolder($statement->period_from); + + $filePath = "{$periodPath}/PRINT/{$statement->branch_code}/{$statement->account_number}.1.pdf"; // Log untuk debugging Log::info('Checking SFTP file path', [ @@ -190,7 +194,8 @@ ini_set('max_execution_time', 300000); for ($period = clone $periodFrom; $period->lte($periodTo); $period->addMonth()) { $periodFormatted = $period->format('Ym'); - $periodPath = $periodFormatted . "/{$statement->branch_code}/{$statement->account_number}_{$periodFormatted}.pdf"; + $periodFolderPath = formatPeriodForFolder($periodFormatted); + $periodPath = $periodFolderPath . "/{$statement->branch_code}/{$statement->account_number}_{$periodFormatted}.pdf"; if ($disk->exists($periodPath)) { $availablePeriods[] = $periodFormatted; @@ -320,7 +325,8 @@ ini_set('max_execution_time', 300000); // Generate or fetch the statement file $disk = Storage::disk('sftpStatement'); - $filePath = "{$statement->period_from}/{$statement->branch_code}/{$statement->account_number}_{$statement->period_from}.pdf"; + $periodPath = formatPeriodForFolder($statement->period_from); + $filePath = "{$periodPath}/{$statement->branch_code}/{$statement->account_number}_{$statement->period_from}.pdf"; if ($statement->is_period_range && $statement->period_to) { // Log: Memulai proses download period range @@ -343,7 +349,8 @@ ini_set('max_execution_time', 300000); for ($period = clone $periodFrom; $period->lte($periodTo); $period->addMonth()) { $periodFormatted = $period->format('Ym'); - $periodPath = $periodFormatted . "/{$statement->branch_code}/{$statement->account_number}_{$periodFormatted}.pdf"; + $periodFolderPath = formatPeriodForFolder($periodFormatted); + $periodPath = $periodFolderPath . "/{$statement->branch_code}/{$statement->account_number}_{$periodFormatted}.pdf"; if ($disk->exists($periodPath)) { $availablePeriods[] = $periodFormatted; @@ -387,7 +394,8 @@ ini_set('max_execution_time', 300000); // Add each available statement to the zip foreach ($availablePeriods as $period) { - $periodFilePath = "{$period}/{$statement->branch_code}/{$statement->account_number}_{$period}.pdf"; + $periodFolderPath = formatPeriodForFolder($period); + $periodFilePath = "{$periodFolderPath}/{$statement->branch_code}/{$statement->account_number}_{$period}.pdf"; $localFilePath = storage_path("app/temp/{$statement->account_number}_{$period}.pdf"); try { @@ -672,7 +680,8 @@ ini_set('max_execution_time', 300000); $localDisk = Storage::disk('local'); $sftpDisk = Storage::disk('sftpStatement'); - $filePath = "{$statement->period_from}/{$statement->branch_code}/{$statement->account_number}_{$statement->period_from}.pdf"; + $periodPath = formatPeriodForFolder($statement->period_from); + $filePath = "{$periodPath}/{$statement->branch_code}/{$statement->account_number}_{$statement->period_from}.pdf"; /** * Fungsi helper untuk mendapatkan file dari disk dengan prioritas local @@ -722,7 +731,8 @@ ini_set('max_execution_time', 300000); for ($period = clone $periodFrom; $period->lte($periodTo); $period->addMonth()) { $periodFormatted = $period->format('Ym'); - $periodPath = "{$periodFormatted}/{$statement->branch_code}/{$statement->account_number}_{$periodFormatted}.pdf"; + $periodFolderPath = formatPeriodForFolder($periodFormatted); + $periodPath = "{$periodFolderPath}/{$statement->branch_code}/{$statement->account_number}_{$periodFormatted}.pdf"; $fileInfo = $getFileFromDisk($periodPath); @@ -910,6 +920,7 @@ ini_set('max_execution_time', 300000); $norek = $statement->account_number; $period = $statement->period_from; + $endPeriod = $statement->period_to ?? $period; $format='pdf'; // Generate nama file PDF @@ -981,20 +992,21 @@ ini_set('max_execution_time', 300000); Log::info('Statement data prepared successfully', [ 'account_number' => $norek, 'period' => $period, + 'endPeriod' => $endPeriod ?? $period, 'saldo_period' => $saldoPeriod, 'saldo_awal' => $saldoAwalBulan->actual_balance ?? 0, 'entries_count' => $stmtEntries->count() ]); - $periodDates = calculatePeriodDates($period); + $periodDates = formatPeriodForFolder($period); // Jika format adalah PDF, generate PDF if ($format === 'pdf') { - return $this->generateStatementPdf($norek, $period, $stmtEntries, $account, $customer, $headerTableBg, $branch, $saldoAwalBulan, $statement->id, $tempPath, $filename); + return $this->generateStatementPdf($norek, $period, $endPeriod, $stmtEntries, $account, $customer, $headerTableBg, $branch, $saldoAwalBulan, $statement->id, $tempPath, $filename); } // Default return HTML view - return view('webstatement::statements.stmt', compact('stmtEntries', 'account', 'customer', 'headerTableBg', 'branch', 'period', 'saldoAwalBulan')); + return view('webstatement::statements.stmt', compact('stmtEntries', 'account', 'customer', 'headerTableBg', 'branch', 'period', 'saldoAwalBulan', 'endPeriod')); } catch (Exception $e) { DB::rollBack(); @@ -1032,7 +1044,7 @@ ini_set('max_execution_time', 300000); * @param object $saldoAwalBulan Data saldo awal * @return \Illuminate\Http\Response */ - protected function generateStatementPdf($norek, $period, $stmtEntries, $account, $customer, $headerTableBg, $branch, $saldoAwalBulan, $statementId, $tempPath, $filename) + protected function generateStatementPdf($norek, $period, $endPeriod, $stmtEntries, $account, $customer, $headerTableBg, $branch, $saldoAwalBulan, $statementId, $tempPath, $filename) { try { DB::beginTransaction(); @@ -1040,6 +1052,7 @@ ini_set('max_execution_time', 300000); Log::info('Starting PDF generation with storage', [ 'account_number' => $norek, 'period' => $period, + 'endPeriod' => $endPeriod ?? $period, 'user_id' => Auth::id() ]); @@ -1051,10 +1064,12 @@ ini_set('max_execution_time', 300000); 'headerTableBg', 'branch', 'period', + 'endPeriod', 'saldoAwalBulan' ))->render(); - // Tentukan path storage - $storagePath = "statements/{$period}/{$norek}"; + // Tentukan path storage dengan format folder baru + $periodPath = formatPeriodForFolder($period); + $storagePath = "statements/{$periodPath}/{$norek}"; $fullStoragePath = "{$storagePath}/{$filename}"; // Generate PDF menggunakan Browsershot dan simpan langsung ke storage @@ -1230,7 +1245,8 @@ ini_set('max_execution_time', 300000); $account = Account::where('account_number',$norek)->first(); - $storagePath = "statements/{$period}/{$account->branch_code}/{$filename}"; + $periodPath = formatPeriodForFolder($period); + $storagePath = "statements/{$periodPath}/{$account->branch_code}/{$filename}"; // Cek apakah file ada di storage if (!Storage::disk('local')->exists($storagePath)) { @@ -1289,7 +1305,8 @@ ini_set('max_execution_time', 300000); $filename = $this->generatePdfFileName($norek, $period); } - $storagePath = "statements/{$period}/{$norek}/{$filename}"; + $periodPath = formatPeriodForFolder($period); + $storagePath = "statements/{$periodPath}/{$norek}/{$filename}"; if (Storage::disk('local')->exists($storagePath)) { $deleted = Storage::disk('local')->delete($storagePath); @@ -1523,6 +1540,8 @@ ini_set('max_execution_time', 300000); $accountNumber = $statement->account_number; $period = $statement->period_from ?? date('Ym'); + $endPeriod = $statement->period_to ?? $period; + $balance = AccountBalance::where('account_number', $accountNumber) ->when($period === '202505', function($query) { return $query->where('period', '>=', '20250512') @@ -1546,7 +1565,7 @@ ini_set('max_execution_time', 300000); } // Dispatch the job - $job = ExportStatementPeriodJob::dispatch($statement->id, $accountNumber, $period, $balance, $clientName); + $job = ExportStatementPeriodJob::dispatch($statement->id, $accountNumber, $period, $endPeriod, $balance, $clientName); Log::info("Statement export job dispatched successfully", [ 'job_id' => $job->job_id ?? null, @@ -1606,8 +1625,9 @@ ini_set('max_execution_time', 300000); ], 404); } - // Find ZIP file - $zipFiles = Storage::disk('local')->files("statements/{$statement->period_from}/multi_account/{$statementId}"); + // Find ZIP file dengan format folder baru + $periodPath = formatPeriodForFolder($statement->period_from); + $zipFiles = Storage::disk('local')->files("statements/{$periodPath}/multi_account/{$statementId}"); $zipFile = null; foreach ($zipFiles as $file) { diff --git a/app/Jobs/ExportStatementPeriodJob.php b/app/Jobs/ExportStatementPeriodJob.php index f4882e3..d27ae98 100644 --- a/app/Jobs/ExportStatementPeriodJob.php +++ b/app/Jobs/ExportStatementPeriodJob.php @@ -38,6 +38,7 @@ class ExportStatementPeriodJob implements ShouldQueue protected $account_number; protected $period; // Format: YYYYMM (e.g., 202505) + protected $endPeriod; // Format: YYYYMM (e.g., 202505) protected $saldo; protected $disk; protected $client; @@ -57,11 +58,12 @@ class ExportStatementPeriodJob implements ShouldQueue * @param string $client * @param string $disk */ - public function __construct(int $statementId, string $account_number, string $period, string $saldo, string $client = '', string $disk = 'local', bool $toCsv = true) + public function __construct(int $statementId, string $account_number, string $period, string $endPeriod, string $saldo, string $client = '', string $disk = 'local', bool $toCsv = true) { $this->statementId = $statementId; $this->account_number = $account_number; $this->period = $period; + $this->endPeriod = $endPeriod; $this->saldo = $saldo; $this->disk = $disk; $this->client = $client; @@ -69,13 +71,13 @@ class ExportStatementPeriodJob implements ShouldQueue $this->toCsv = $toCsv; // Calculate start and end dates based on period - $this->calculatePeriodDates(); + $this->formatPeriodForFolder(); } /** * Calculate start and end dates for the given period */ - private function calculatePeriodDates(): void + private function formatPeriodForFolder(): void { $year = substr($this->period, 0, 4); $month = substr($this->period, 4, 2); @@ -90,6 +92,13 @@ class ExportStatementPeriodJob implements ShouldQueue // End date is always the last day of the month $this->endDate = Carbon::createFromDate($year, $month, 1)->endOfMonth()->endOfDay(); + + // If endPeriod is provided, use it instead of endDate + if($this->endPeriod){ + $year = substr($this->endPeriod, 0, 4); + $month = substr($this->endPeriod, 4, 2); + $this->endDate = Carbon::createFromDate($year, $month, 1)->endOfMonth()->endOfDay(); + } } /** @@ -478,8 +487,9 @@ class ExportStatementPeriodJob implements ShouldQueue // Generate filename $filename = "{$this->account_number}_{$this->period}.pdf"; - // Tentukan path storage - $storagePath = "statements/{$this->period}/{$account->branch_code}"; + // Tentukan path storage dengan format folder baru + $periodPath = formatPeriodForFolder($this->period); + $storagePath = "statements/{$periodPath}/{$account->branch_code}"; $tempPath = storage_path("app/temp/{$filename}"); $fullStoragePath = "{$storagePath}/{$filename}"; @@ -492,6 +502,7 @@ class ExportStatementPeriodJob implements ShouldQueue Storage::makeDirectory($storagePath); $period = $this->period; + $endPeriod = $this->endPeriod; // Render HTML view $html = view('webstatement::statements.stmt', compact( @@ -501,6 +512,7 @@ class ExportStatementPeriodJob implements ShouldQueue 'headerTableBg', 'branch', 'period', + 'endPeriod', 'saldoAwalBulan' ))->render(); @@ -616,7 +628,8 @@ class ExportStatementPeriodJob implements ShouldQueue // Determine the base path based on client $account = Account::where('account_number', $this->account_number)->first(); - $storagePath = "statements/{$this->period}/{$account->branch_code}"; + $periodPath = formatPeriodForFolder($this->period); + $storagePath = "statements/{$periodPath}/{$account->branch_code}"; Storage::disk($this->disk)->makeDirectory($storagePath); $filePath = "{$storagePath}/{$this->fileName}"; diff --git a/resources/views/statements/index.blade.php b/resources/views/statements/index.blade.php index 0c08519..65936d5 100644 --- a/resources/views/statements/index.blade.php +++ b/resources/views/statements/index.blade.php @@ -6,7 +6,7 @@ @section('content')
-
+

Request Print Stetement

@@ -71,10 +71,6 @@ {{ in_array('BY.MAIL.TO.KTP.ADDR', old('stmt_sent_type', $statement->stmt_sent_type ?? [])) ? 'selected' : '' }}> BY MAIL TO KTP ADDR -