@php
use Carbon\Carbon;
@endphp
Pro-rata adjustment test
@php
// // Add Pro-rata adjustment
// if (!function_exists('roundOrFloor')) {
// function roundOrFloor($num)
// {
// if ($num >= 0 && $num < 0.5) {
// return 0.5;
// } else {
// return 1;
// }
// }
// }
// $total = 20;
// // $today = Carbon::now(); //Today as in NOW
// $today = Carbon::create(2025, 02, 01);
// $totalInt = intval($total);
// // Calculate the remaining days and months in the year
// $remainingDaysInMonth = $today->daysInMonth - $today->day + 1; // Days left in the current month including today
// $remainingMonths = 12 - $today->month; // Remaining full months after the current month
// // Calculate the pro-rata based on the remaining days and months
// $decimal = $remainingDaysInMonth / $today->daysInMonth;
// $final = ($totalInt / 12) * ($remainingMonths + $decimal);
// // Apply rounding
// $final = floor($final) + roundOrFloor(fmod($final, 1));
// $adjust = $total - $final;
// $daysInMonth = $today->daysInMonth;
// // Calculate the middle day of the month
// $middleDay = ceil($daysInMonth / 2);
// // Get the middle date of the month
// $middleOfMonth = Carbon::create($today->year, $today->month, $middleDay);
// Add Pro-rata adjustment
// if (!function_exists('roundOrFloor')) {
// function roundOrFloor($num)
// {
// // Round up to the nearest half
// if ($num >= 0 && $num < 0.5) {
// return 0.5;
// } else {
// return ceil($num * 2) / 2;
// }
// }
// }
// Total annual holiday entitlement
$total = 20; // days
// User's join date (hardcoded for this example)
$joinDate = Carbon::create(2024, 6, 17); // Example join date
// Determine the number of days in the join month
$daysInMonth = $joinDate->daysInMonth;
// Calculate the middle of the month
$middleOfMonth = ceil($daysInMonth / 2); // The middle day
// Calculate the remaining months from the join date to the end of the year
$remainingMonths = 12 - $joinDate->month;
// Determine the month balance based on the join date
if ($joinDate->day < $middleOfMonth) {
$monthBalance = $remainingMonths + 1; // Count the join month as a full month
} else {
$monthBalance = $remainingMonths + 0.5; // Count the join month as half a month
}
// Calculate the pro-rata holiday entitlement
$proRataHoliday = ($total / 12) * $monthBalance;
// Round to the nearest half-day
$final = round($proRataHoliday * 2) / 2;
// Adjustment based on the final pro-rata calculation
$adjust = $total - $final;
// Output the results below
@endphp
Holiday Set: {{ $total }}
Join Date: {{ $joinDate->toDateString() }}
Month balance {{ $monthBalance }}
Working out: {{ $proRataHoliday }}
Adjustment: {{ $adjust }}
Final Pro-rata Holiday: {{ $final }}