diff --git a/app/Helpers/helpers.php b/app/Helpers/helpers.php index 65979cc..f92ee39 100644 --- a/app/Helpers/helpers.php +++ b/app/Helpers/helpers.php @@ -1,6 +1,8 @@ first(); - return $province->name; + return $code; } - } \ No newline at end of file + } + + 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 + } + } + } diff --git a/app/Http/Controllers/CombinePdfController.php b/app/Http/Controllers/CombinePdfController.php index 37b8c44..64736ee 100644 --- a/app/Http/Controllers/CombinePdfController.php +++ b/app/Http/Controllers/CombinePdfController.php @@ -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 - } - } }