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:
@@ -1,6 +1,8 @@
|
||||
<?php
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Modules\Webstatement\Models\Account;
|
||||
use Modules\Webstatement\Models\ProvinceCore;
|
||||
|
||||
if(!function_exists('calculatePeriodDates')) {
|
||||
@@ -41,7 +43,54 @@ use Modules\Webstatement\Models\ProvinceCore;
|
||||
|
||||
if(!function_exists('getProvinceCoreName')){
|
||||
function getProvinceCoreName($code){
|
||||
$province = ProvinceCore::where('code',$code)->first();
|
||||
return $province->name;
|
||||
return $code;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(!function_exists('generatePassword')){
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user