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>
97 lines
3.2 KiB
PHP
97 lines
3.2 KiB
PHP
<?php
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Modules\Webstatement\Models\Account;
|
|
use Modules\Webstatement\Models\ProvinceCore;
|
|
|
|
if(!function_exists('calculatePeriodDates')) {
|
|
/**
|
|
* Fungsi untuk menghitung tanggal periode berdasarkan periode yang diberikan
|
|
* Jika periode 202505, mulai dari tanggal 9 sampai akhir bulan
|
|
* Jika periode lain, mulai dari tanggal 1 sampai akhir bulan
|
|
*/
|
|
function calculatePeriodDates($period)
|
|
{
|
|
$year = substr($period, 0, 4);
|
|
$month = substr($period, 4, 2);
|
|
|
|
// Log untuk debugging
|
|
Log::info('Calculating period dates', [
|
|
'period' => $period,
|
|
'year' => $year,
|
|
'month' => $month,
|
|
]);
|
|
|
|
if ($period === '202505') {
|
|
// Untuk periode 202505, mulai dari tanggal 9
|
|
$startDate = \Carbon\Carbon::createFromDate($year, $month, 9,'Asia/Jakarta');
|
|
} else {
|
|
// Untuk periode lain, mulai dari tanggal 1
|
|
$startDate = \Carbon\Carbon::createFromDate($year, $month, 1,'Asia/Jakarta');
|
|
}
|
|
|
|
// Tanggal akhir selalu akhir bulan
|
|
$endDate = \Carbon\Carbon::createFromDate($year, $month, 1)->endOfMonth();
|
|
|
|
return [
|
|
'start' => $startDate,
|
|
'end' => $endDate,
|
|
];
|
|
}
|
|
}
|
|
|
|
if(!function_exists('getProvinceCoreName')){
|
|
function getProvinceCoreName($code){
|
|
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
|
|
}
|
|
}
|
|
}
|