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`.
This commit is contained in:
Daeng Deni Mardaeni
2025-05-04 19:17:04 +07:00
parent 9049075b0f
commit 3a4c5bf4ca
4 changed files with 70 additions and 4 deletions

View File

@@ -2,8 +2,18 @@
use Modules\Basicdata\Models\Currency; 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(); $currency = Currency::where('code', $currency)->first();
$symbol = $currency->symbol?? ''; $symbol = $currency->symbol?? '';

View File

@@ -0,0 +1,54 @@
<?php
use Carbon\Carbon;
use Modules\Basicdata\Models\HolidayCalendar;
/**
* Calculate the number of working days between two dates.
*
* This function counts the number of days that are not weekends and not holidays
* between the given start and end dates (inclusive).
*
* @param string|Carbon $startDate The starting date for the calculation
* @param string|Carbon $endDate The ending date for the calculation
* @return int The number of working days between the two dates
*/
if (!function_exists('workingDays')) {
function workingDays($startDate, $endDate)
{
$startDate = Carbon::parse($startDate)->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();
}
}

View File

@@ -37,7 +37,7 @@
['code' => 'INR', 'name' => 'Indian Rupee', 'symbol' => '₹', 'decimal_places' => 2, 'status' => true, 'created_at' => $now, 'updated_at' => $now], ['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' => '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' => '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' => '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' => '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], ['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], ['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); DB::table('currencies')->insert($currencies);
} }
} }

View File

@@ -9,7 +9,8 @@
"Modules\\Basicdata\\Providers\\BasicdataServiceProvider" "Modules\\Basicdata\\Providers\\BasicdataServiceProvider"
], ],
"files": [ "files": [
"app/Helpers/Currency.php" "app/Helpers/Currency.php",
"app/Helpers/HolidayCalendar.php"
], ],
"menu": { "menu": {
"main": [], "main": [],