@if($leave->employee->photo)
 }})
@else
{{ substr($leave->employee->name ?? '-', 0, 1) }}
@endif
{{ $leave->employee->name ?? '-' }}
{{ optional($leave->employee->position)->name ?? 'غير محدد' }}
{{ optional($leave->employee->department)->name ?? 'غير محدد' }}
@php
// ✅ حساب الرصيد التراكمي حتى نهاية الشهر السابق (الرصيد الفعلي)
$employee = $leave->employee;
$hireDate = \Carbon\Carbon::parse($employee->hire_date);
$currentDate = \Carbon\Carbon::now();
$lastMonthEnd = \Carbon\Carbon::createFromDate($currentDate->year, $currentDate->month, 1)->subDay(); // آخر يوم في الشهر السابق
if ($hireDate->greaterThan($lastMonthEnd)) {
$monthsWorked = 0;
} else {
$monthsWorked = $hireDate->diffInMonths($lastMonthEnd);
if ($lastMonthEnd->day >= $hireDate->day) {
$monthsWorked += 1;
}
}
$monthsWorked = max(0, min($monthsWorked, 12)); // ⚠️ قد يُطبق الحد الأقصى السنوي
$actualCumulativeBalance = round(($employee->monthly_leave_days_allowed ?? 0) * $monthsWorked, 2);
// حساب الأيام المستخدمة حتى نهاية الشهر السابق
$usedDaysThisMonth = $employee->leaves()
->whereIn('status', ['approved', 'modified'])
->ofCodes(['annual', 'emergency'])
->whereMonth('start_date', $currentDate->month)
->whereYear('start_date', $currentDate->year)
->sum('days_count');
$usedDaysBeforeThisMonth = $employee->leaves()
->whereIn('status', ['approved', 'modified'])
->ofCodes(['annual', 'emergency'])
->where(function ($query) use ($lastMonthEnd) {
$query->whereDate('start_date', '<=', $lastMonthEnd)
->orWhereDate('end_date', '<=', $lastMonthEnd);
})
->sum('days_count');
$expectedBalanceAfterThisMonthUsage = $actualCumulativeBalance - $usedDaysThisMonth;
// ✅ حساب الرصيد بعد استخدام هذا الطلب (الحالي)
$newTotalUsed = $usedDaysBeforeThisMonth + $usedDaysThisMonth + $leave->days_count;
$balanceAfterLeave = $actualCumulativeBalance - $newTotalUsed;
@endphp
{{ $actualCumulativeBalance }}
الرصيد الفعلي (التراكمي)
{{ $newTotalUsed }}
المستخدمة (الحالية + هذا الطلب)
{{ $balanceAfterLeave }}
المتبقية (بعد هذا الطلب)
@if($balanceAfterLeave < 0)