From 3a4c5bf4ca8b61cb382db3f18f416b87c5ad635d Mon Sep 17 00:00:00 2001 From: Daeng Deni Mardaeni Date: Sun, 4 May 2025 19:17:04 +0700 Subject: [PATCH] feat(basicdata): tambahkan helper untuk penghitungan hari kerja dan pembaruan format mata uang - Menambahkan helper `workingDays` untuk menghitung jumlah hari kerja antara dua tanggal. - Menambahkan helper `holidays` untuk mendapatkan daftar tanggal libur. - Memperbarui fungsi `format_currency` menjadi `currencyFormat` dengan menambahkan dokumentasi yang lebih jelas. - Mengubah format desimal mata uang IDR menjadi 2 desimal di `CurrencySeeder`. - Menambahkan file `HolidayCalendar.php` ke dalam daftar autoload di `module.json`. - Menambahkan proses truncate sebelum insert data di seeder `CurrencySeeder`. --- app/Helpers/Currency.php | 14 ++++++-- app/Helpers/HolidayCalendar.php | 54 +++++++++++++++++++++++++++++ database/seeders/CurrencySeeder.php | 3 +- module.json | 3 +- 4 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 app/Helpers/HolidayCalendar.php diff --git a/app/Helpers/Currency.php b/app/Helpers/Currency.php index a1a0e8a..ffc9263 100644 --- a/app/Helpers/Currency.php +++ b/app/Helpers/Currency.php @@ -2,8 +2,18 @@ use Modules\Basicdata\Models\Currency; - if(!function_exists('format_currency')) { - function format_currency($number, $currency = 'IDR') { + /** + * Format a number as a currency string with appropriate symbol and formatting. + * + * This function retrieves currency information from the database and formats + * the provided number according to the currency's specifications. + * + * @param float|int $number The number to be formatted as currency + * @param string $currency The currency code (default: 'IDR') + * @return string The formatted currency string with symbol and proper number formatting + */ + if(!function_exists('currencyFormat')) { + function currencyFormat($number, $currency = 'IDR') { $currency = Currency::where('code', $currency)->first(); $symbol = $currency->symbol?? ''; diff --git a/app/Helpers/HolidayCalendar.php b/app/Helpers/HolidayCalendar.php new file mode 100644 index 0000000..f48d747 --- /dev/null +++ b/app/Helpers/HolidayCalendar.php @@ -0,0 +1,54 @@ +startOfDay(); + $endDate = Carbon::parse($endDate)->endOfDay(); + + $workingDay = 0; + $now = $startDate->copy(); + + while ($now <= $endDate) { + if (!$now->isWeekend() && !in_array($now->format('Y-m-d'), holidays())) { + $workingDay++; + } + $now->addDay(); + } + + return $workingDay; + } + } + + + /** + * Get a list of all holiday dates from the holiday calendar. + * + * This function retrieves all holiday dates from the HolidayCalendar model + * and formats them as Y-m-d strings for easy comparison in date calculations. + * + * @return array An array of holiday dates in Y-m-d format + */ + if (!function_exists('holidays')) { + function holidays() + { + return HolidayCalendar::pluck('date')->map( + function ($item) { + return Carbon::parse($item)->format('Y-m-d'); + }, + )->toArray(); + } + } diff --git a/database/seeders/CurrencySeeder.php b/database/seeders/CurrencySeeder.php index 4a634a5..b1aae83 100644 --- a/database/seeders/CurrencySeeder.php +++ b/database/seeders/CurrencySeeder.php @@ -37,7 +37,7 @@ ['code' => 'INR', 'name' => 'Indian Rupee', 'symbol' => '₹', 'decimal_places' => 2, 'status' => true, 'created_at' => $now, 'updated_at' => $now], ['code' => 'BRL', 'name' => 'Brazilian Real', 'symbol' => 'R$', 'decimal_places' => 2, 'status' => true, 'created_at' => $now, 'updated_at' => $now], ['code' => 'ZAR', 'name' => 'South African Rand', 'symbol' => 'R', 'decimal_places' => 2, 'status' => true, 'created_at' => $now, 'updated_at' => $now], - ['code' => 'IDR', 'name' => 'Indonesian Rupiah', 'symbol' => 'Rp', 'decimal_places' => 0, 'status' => true, 'created_at' => $now, 'updated_at' => $now], + ['code' => 'IDR', 'name' => 'Indonesian Rupiah', 'symbol' => 'Rp', 'decimal_places' => 2, 'status' => true, 'created_at' => $now, 'updated_at' => $now], ['code' => 'MYR', 'name' => 'Malaysian Ringgit', 'symbol' => 'RM', 'decimal_places' => 2, 'status' => true, 'created_at' => $now, 'updated_at' => $now], ['code' => 'PHP', 'name' => 'Philippine Peso', 'symbol' => '₱', 'decimal_places' => 2, 'status' => true, 'created_at' => $now, 'updated_at' => $now], ['code' => 'THB', 'name' => 'Thai Baht', 'symbol' => '฿', 'decimal_places' => 2, 'status' => true, 'created_at' => $now, 'updated_at' => $now], @@ -59,6 +59,7 @@ ['code' => 'PKR', 'name' => 'Pakistani Rupee', 'symbol' => '₨', 'decimal_places' => 2, 'status' => true, 'created_at' => $now, 'updated_at' => $now], ]; + DB::table('currencies')->truncate(); DB::table('currencies')->insert($currencies); } } diff --git a/module.json b/module.json index cb66829..2461691 100644 --- a/module.json +++ b/module.json @@ -9,7 +9,8 @@ "Modules\\Basicdata\\Providers\\BasicdataServiceProvider" ], "files": [ - "app/Helpers/Currency.php" + "app/Helpers/Currency.php", + "app/Helpers/HolidayCalendar.php" ], "menu": { "main": [],