Files
webstatement/app/Http/Controllers/CombinePdfController.php
Daeng Deni Mardaeni db99465690 feat(webstatement): tambahkan konfigurasi sumber file r23 dan optimalkan log combine PDF
- Memperbarui fungsi `combinePdfs` di `CombinePdfController`:
  - Menambahkan konfigurasi sumber file `r23` dengan opsi `local` atau `sftp`.
  - Memindahkan logika pencarian file `r23` ke dalam pengaturan berbasis konfigurasi:
    - Jika menggunakan `local`, sistem mencari file `r23` dalam penyimpanan lokal.
    - Jika menggunakan `sftp`, sistem mengunduh file `r23` dari SFTP dan menyimpannya ke direktori sementara.
  - Menambahkan log informasi untuk menentukan sumber file `r23` yang digunakan (`local` atau `sftp`).
  - Merubah pencatatan log yang sebelumnya statis menjadi dinamis berdasarkan konfigurasi.

- Menyesuaikan pengelolaan jalur file `r23`:
  - Menambahkan jalur sementara untuk penyimpanan file hasil unduhan SFTP.
  - Memisahkan logika jalur file lokal dan file unduhan berdasarkan konfigurasi.

Signed-off-by: Daeng Deni Mardaeni <ddeni05@gmail.com>
2025-06-05 11:31:31 +07:00

188 lines
6.6 KiB
PHP

<?php
namespace Modules\Webstatement\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Log;
use Modules\Webstatement\Jobs\CombinePdfJob;
use Modules\Webstatement\Models\Account;
use Carbon\Carbon;
class CombinePdfController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
return view('webstatement::index');
}
/**
* Combine PDF files from r14 and r23 folders for all accounts
*
* @param Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function combinePdfs($period)
{
// Configuration: Set r23 file source - 'local' or 'sftp'
$file_r23 = 'local'; // Change this to 'sftp' to use SFTP for r23 files
// Get period from request or use current period
$period = $period ?? date('Ym');
// Get all accounts with customer relation
$accounts = Account::where('branch_code','ID0010052')->get();
$processedCount = 0;
$skippedCount = 0;
$errorCount = 0;
foreach ($accounts as $account) {
$branchCode = $account->branch_code;
$accountNumber = $account->account_number;
// Define file paths
$r14Path = storage_path("app/r14/{$accountNumber}_{$period}.pdf");
// Define r23 paths based on configuration
$r23LocalPath = storage_path("app/r23/{$accountNumber}.1.pdf");
$r23SftpPath = "r23/{$accountNumber}.1.pdf";
// Define temporary path for r23 file downloaded from SFTP
$tempDir = storage_path("app/temp/{$period}");
if (!File::exists($tempDir)) {
File::makeDirectory($tempDir, 0755, true);
}
$r23TempPath = "{$tempDir}/{$accountNumber}_r23.pdf";
$outputDir = storage_path("app/combine/{$period}/{$branchCode}");
$outputFilename = "{$accountNumber}_{$period}.pdf";
// Check if r14 file exists locally
$r14Exists = File::exists($r14Path);
// Check for r23 file based on configuration
$r23Exists = false;
$r23FinalPath = null;
if ($file_r23 === 'local') {
// Use local r23 files
if (File::exists($r23LocalPath)) {
$r23Exists = true;
$r23FinalPath = $r23LocalPath;
Log::info("Found r23 file locally for account {$accountNumber}");
}
} elseif ($file_r23 === 'sftp') {
// Use SFTP r23 files
try {
if (Storage::disk('sftpStatement')->exists($r23SftpPath)) {
$r23Content = Storage::disk('sftpStatement')->get($r23SftpPath);
File::put($r23TempPath, $r23Content);
$r23Exists = true;
$r23FinalPath = $r23TempPath;
Log::info("Downloaded r23 file for account {$accountNumber} from SFTP");
}
} catch (\Exception $e) {
Log::error("Error downloading r23 file from SFTP for account {$accountNumber}: {$e->getMessage()}");
}
}
// Skip if neither file exists
if (!$r14Exists && !$r23Exists) {
//Log::warning("No PDF files found for account {$accountNumber}");
$skippedCount++;
continue;
}
// Prepare file list for processing
$pdfFiles = [];
if ($r14Exists) {
$pdfFiles[] = $r14Path;
}
if ($r23Exists) {
$pdfFiles[] = $r23FinalPath;
}
try {
// Generate password based on customer relation data
$password = $this->generatePassword($account);
// Dispatch job to combine PDFs or apply password protection
CombinePdfJob::dispatch($pdfFiles, $outputDir, $outputFilename, $password);
$processedCount++;
Log::info("Queued PDF processing for account {$accountNumber} - r14: local, r23: {$file_r23}, password: {$password}");
} catch (\Exception $e) {
Log::error("Error processing PDF for account {$accountNumber}: {$e->getMessage()}");
$errorCount++;
}
}
return response()->json([
'message' => "PDF combination process has been queued (r14: local, r23: {$file_r23})",
'processed' => $processedCount,
'skipped' => $skippedCount,
'errors' => $errorCount,
'period' => $period
]);
}
/**
* Generate password based on customer relation data
* Format: date+end 2 digit account_number
* Example: 05Oct202585
*
* @param Account $account
* @return string
*/
private function generatePassword(Account $account)
{
$customer = $account->customer;
$accountNumber = $account->account_number;
// Get last 2 digits of account number
$lastTwoDigits = substr($accountNumber, -2);
// Determine which date to use based on sector
$dateToUse = null;
if ($customer && $customer->sector) {
$firstDigitSector = substr($customer->sector, 0, 1);
if ($firstDigitSector === '1') {
// Use date_of_birth if available, otherwise birth_incorp_date
$dateToUse = $customer->date_of_birth ?: $customer->birth_incorp_date;
} else {
// Use birth_incorp_date for sector > 1
$dateToUse = $customer->birth_incorp_date;
}
}
// If no date found, fallback to account number
if (!$dateToUse) {
Log::warning("No date found for account {$accountNumber}, using account number as password");
return $accountNumber;
}
try {
// Parse the date and format it
$date = Carbon::parse($dateToUse);
$day = $date->format('d');
$month = $date->format('M'); // 3-letter month abbreviation
$year = $date->format('Y');
// Format: ddMmmyyyyXX (e.g., 05Oct202585)
$password = $day . $month . $year . $lastTwoDigits;
return $password;
} catch (\Exception $e) {
Log::error("Error parsing date for account {$accountNumber}: {$e->getMessage()}");
return $accountNumber; // Fallback to account number
}
}
}