diff --git a/BACKEND/Modules/Admin/app/Http/Controllers/TicketController.php b/BACKEND/Modules/Admin/app/Http/Controllers/TicketController.php index fa76489..e37cc1f 100644 --- a/BACKEND/Modules/Admin/app/Http/Controllers/TicketController.php +++ b/BACKEND/Modules/Admin/app/Http/Controllers/TicketController.php @@ -388,9 +388,10 @@ class TicketController extends Controller // Danh sách ngày nghỉ theo tháng $requestMonths = $this->groupLeaveRequestsByMonth($dataListPeriod); $monthsInfo = []; + $hasInsufficientDays = false; + $errorMessage = ''; foreach ($requestMonths as $monthKey => $monthData) { - // Tính tổng số ngày nghỉ có phép trong tháng $usedDaysInMonth = $this->getUsedLeaveDaysInMonth($user, $monthData['year'], $monthData['month'], 'ONLEAVE'); @@ -403,7 +404,6 @@ class TicketController extends Controller // Tính tổng số ngày nghỉ trong tháng $totalDaysInMonth = $usedDaysInMonth + $usedDaysInMonthWithoutPay + $monthData['days_requested']; - // Tính tổng phép có trong tháng $totalLeaveDaysInMonth = $this->getTotalLeaveDaysInMonth($user, $monthData['year'], $monthData['month']); @@ -412,7 +412,6 @@ class TicketController extends Controller //Ngày phép còn lại trong tháng $remainingDaysInMonth = $totalLeaveDaysInMonth - $totalLeaveDaysInMonthToMonth; - // dd($remainingDaysInMonth, $totalLeaveDaysInMonth, $totalLeaveDaysInMonthToMonth); $month_data = [ 'year' => $monthData['year'], @@ -423,51 +422,44 @@ class TicketController extends Controller 'days_used' => $usedDaysInMonth, //tổng số ngày nghỉ có phép ở tháng hiện tại 'days_used_without_pay' => $usedDaysInMonthWithoutPay, //tổng số ngày nghỉ không phép ở tháng hiện tại 'days_requested' => $monthData['days_requested'], //số ngày yêu cầu nghỉ của tháng + 'status' => 'ok', // mặc định là ok ]; + // Xử lý các trường hợp thiếu ngày phép if ($remainingDaysInMonth <= 0) { //hết phép - return [ - 'success' => false, - 'message' => "Hiện tại bạn đã hết phép nghỉ trong tháng {$monthData['month']}/{$monthData['year']}\nBạn có chấp nhận nộp: " . $monthData['days_requested'] . " ngày không phép không?", - 'warning_type' => 'exceed_monthly_limit', - 'month_data' => $month_data - ]; - } else { - // Còn phép - if ($remainingDaysInMonth >= $monthData['days_requested']) { - return [ - 'success' => true, - 'message' => "Đủ ngày phép", - 'warning_type' => 'exceed_monthly_limit', - 'month_data' => $month_data - ]; - } else { - return $this->exceedMonthlyLimitResponse($remainingDaysInMonth, $monthData, $usedDaysInMonth, $usedDaysInMonthWithoutPay, $month_data); - } + $hasInsufficientDays = true; + $month_data['status'] = 'no_days_left'; + $monthMessage = "Hiện tại bạn đã hết phép nghỉ trong tháng {$monthData['month']}/{$monthData['year']}\nBạn sẽ nộp: " . $monthData['days_requested'] . " ngày không phép."; + $errorMessage .= $errorMessage ? "\n\n" . $monthMessage : $monthMessage; + } else if ($remainingDaysInMonth < $monthData['days_requested']) { // không đủ ngày phép + $hasInsufficientDays = true; + $month_data['status'] = 'insufficient_days'; + $daysNotEnough = $monthData['days_requested'] - $remainingDaysInMonth; + $monthMessage = "Tháng {$monthData['month']}/{$monthData['year']}: Số ngày phép còn lại: {$remainingDaysInMonth}, Số ngày yêu cầu: {$monthData['days_requested']}.\nBạn sẽ sử dụng {$remainingDaysInMonth} ngày phép và {$daysNotEnough} ngày không phép."; + $errorMessage .= $errorMessage ? "\n\n" . $monthMessage : $monthMessage; } + + // Thêm thông tin tháng vào mảng kết quả + $monthsInfo[] = $month_data; + } + + // Trả về kết quả tổng hợp + if ($hasInsufficientDays) { + return [ + 'success' => false, + 'message' => $errorMessage . "\n\nBạn có chấp nhận không?", + 'warning_type' => 'exceed_monthly_limit', + 'months_info' => $monthsInfo + ]; } return [ - 'success' => false, - 'message' => "Không tìm được mảng danh sách ngày nghỉ", - 'warning_type' => 'exceed_monthly_limit', + 'success' => true, + 'message' => "Đủ ngày phép cho tất cả tháng yêu cầu.", 'months_info' => $monthsInfo ]; } - //Trả về thông báo vượt quá giới hạn ngày nghỉ có phép trong tháng - private function exceedMonthlyLimitResponse(float $remainingDaysInMonth, array $monthData, float $usedDaysInMonth, float $usedDaysInMonthWithoutPay, array $month_data): array - { - $daysNotEnough = $monthData['days_requested'] - $remainingDaysInMonth; - $message = "Số ngày phép còn lại: {$remainingDaysInMonth}, Số ngày yêu cầu: {$monthData['days_requested']}.\n\nBạn có chấp nhận nộp: {$remainingDaysInMonth} ngày phép và {$daysNotEnough} ngày không phép không?"; - return [ - 'success' => false, - 'message' => "Bạn không đủ ngày phép.\n\nTrong tháng {$monthData['month']}/{$monthData['year']}:\n\nBạn đã nghỉ: {$usedDaysInMonth} ngày phép, {$usedDaysInMonthWithoutPay} ngày không phép. \n\n{$message}", - 'warning_type' => 'exceed_monthly_limit', - 'month_data' => $month_data - ]; - } - //Tính tổng số ngày nghỉ có phép đến tháng hiện tại private function getTotalLeaveDaysInMonthToMonth($user, int $year, int $month): float {