feat(webstatement): ekstrak generatePassword ke helper

Perubahan yang dilakukan:
- Memindahkan fungsi `generatePassword` dari `CombinePdfController` ke `helpers.php` untuk peningkatan reusabilitas.
- Menambahkan dependency `use Carbon\Carbon` dan `use Modules\Webstatement\Models\Account` di `helpers.php`.
- Menyesuaikan pemanggilan fungsi `generatePassword` di `CombinePdfController` dengan versi helper.

Tujuan perubahan:
- Mengurangi duplikasi kode dengan menjadikan fungsi `generatePassword` dapat diakses secara global.
- Mempermudah perawatan kode melalui pemisahan tanggung jawab fungsi.

Signed-off-by: Daeng Deni Mardaeni <ddeni05@gmail.com>
This commit is contained in:
Daeng Deni Mardaeni
2025-07-10 14:12:10 +07:00
parent 0aa7d22094
commit d4e6a3d73d
2 changed files with 53 additions and 58 deletions

View File

@@ -135,7 +135,7 @@ class CombinePdfController extends Controller
try {
// Generate password based on customer relation data
$password = $this->generatePassword($account);
$password = generatePassword($account);
// Dispatch job to combine PDFs or apply password protection
CombinePdfJob::dispatch($pdfFiles, $outputDir, $outputFilename, $password, $output_destination, $branchCode, $period);
@@ -158,58 +158,4 @@ class CombinePdfController extends Controller
'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
}
}
}