diff --git a/BACKEND/Modules/Admin/app/Http/Controllers/TicketController.php b/BACKEND/Modules/Admin/app/Http/Controllers/TicketController.php index 89cb023..8ea27d8 100644 --- a/BACKEND/Modules/Admin/app/Http/Controllers/TicketController.php +++ b/BACKEND/Modules/Admin/app/Http/Controllers/TicketController.php @@ -312,14 +312,20 @@ class TicketController extends Controller } $balanceCheckResultWaiting = $this->checkLeaveBalance($user, $dataListPeriodWaiting); + // dd($balanceCheckResultWaiting,$dataListPeriodWaiting,$user); if ($balanceCheckResultWaiting['months_info']) { $monthsInfoWaiting = $balanceCheckResultWaiting['months_info']; - $waitingTicketsMessage .= $balanceCheckResultWaiting['message'] . "\n------------------------------------------------"; + if ($balanceCheckResultWaiting['success']) { + $waitingTicketsMessage .= "------------------------------------------------"; + }else{ + $waitingTicketsMessage .= $balanceCheckResultWaiting['message'] . "\n------------------------------------------------"; + } $balanceCheckResult = $this->checkLeaveBalance($user, $dataListPeriod, $monthsInfoWaiting); + // dd($balanceCheckResult, $waitingTicketsMessage); } else { $balanceCheckResult = $this->checkLeaveBalance($user, $dataListPeriod); } - + // dd($balanceCheckResult); // Nếu không đủ ngày phép, trả về thông báo và không tạo ticket if (!$balanceCheckResult['success']) { $finalMessage = $waitingTicketsMessage; @@ -449,31 +455,28 @@ class TicketController extends Controller $errorMessage = ''; $remainingDaysInMonthIsUsed = 0; + // Tổng giới hạn ngày nghỉ có phép tối đa trong tháng + $maxDaysPerMonth = $this->getMaxLeaveDaysPerMonth(); + foreach ($requestMonths as $monthKey => $monthData) { if ($monthsInfoWaiting) { + // dd($requestMonths, $monthsInfoWaiting); foreach ($monthsInfoWaiting as $monthInfo) { if ($monthInfo['month'] == $monthData['month'] && $monthInfo['year'] == $monthData['year']) { $remainingDaysInMonthIsUsed += $monthInfo['remaining_days_in_month_remaining']; + // dd($remainingDaysInMonthIsUsed); } } } - // Tính tổng số ngày nghỉ có phép trong tháng + // Tổng số ngày nghỉ có phép trong tháng $usedDaysInMonth = $this->getUsedLeaveDaysInMonth($user, $monthData['year'], $monthData['month'], 'ONLEAVE'); - - // Tính tổng số ngày nghỉ không phép trong tháng + // Tổng số ngày nghỉ không phép trong tháng $usedDaysInMonthWithoutPay = $this->getUsedLeaveDaysInMonth($user, $monthData['year'], $monthData['month'], 'LEAVE_WITHOUT_PAY'); - // Tính tổng giới hạn ngày nghỉ có phép tối đa trong tháng - - $maxDaysPerMonth = $this->getMaxLeaveDaysPerMonth(); - - $days_will_use = 0; - $days_will_use_without_pay = 0; - - // Tính tổng số ngày nghỉ trong tháng + // Tổng số ngày nghỉ trong tháng = tổng ngày nghỉ có phép + tổng ngày nghỉ không phép + tổng ngày yêu cầu $totalDaysInMonth = $usedDaysInMonth + $usedDaysInMonthWithoutPay + $monthData['days_requested']; - // Tính tổng phép có trong tháng + // Tổng phép có trong tháng $totalLeaveDaysInMonth = $this->getTotalLeaveDaysInMonth($user, $monthData['year'], $monthData['month']); // Tính tổng số ngày nghỉ có phép đến tháng hiện tại @@ -483,10 +486,24 @@ class TicketController extends Controller $remainingDaysInMonth = $totalLeaveDaysInMonth - $totalLeaveDaysInMonthToMonth; $remainingDaysInMonthRemaining = $remainingDaysInMonth - $remainingDaysInMonthIsUsed; + // if ($monthsInfoWaiting) { + // dd( + // "Ngày phép còn lại trong tháng: " . $remainingDaysInMonthRemaining, + // "Ngày phép còn lại: " . $remainingDaysInMonth, + // "Ngày phép đã sử dụng: " . $remainingDaysInMonthIsUsed, + // "Ngày phép yêu cầu: " . $monthData['days_requested'], + // "Tổng ngày nghỉ trong tháng: " . $totalDaysInMonth, + // "Ngày phép đã sử dụng: " . $usedDaysInMonth, + // ); + // } + + $month_data_status = 'ok'; + $days_will_use = 0; + $days_will_use_without_pay = 0; // Xử lý các trường hợp thiếu ngày phép if ($remainingDaysInMonthRemaining <= 0) { //hết phép $hasInsufficientDays = true; - $month_data['status'] = 'no_days_left'; + $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']}\n - Bạn sẽ nộp: " . $monthData['days_requested'] . " ngày không phép."; $errorMessage .= $errorMessage ? "\n\n" . $monthMessage : $monthMessage; @@ -494,7 +511,7 @@ class TicketController extends Controller $days_will_use_without_pay = $monthData['days_requested']; } else if ($remainingDaysInMonthRemaining < $monthData['days_requested']) { // không đủ ngày phép $hasInsufficientDays = true; - $month_data['status'] = 'insufficient_days'; + $month_data_status = 'insufficient_days'; $daysNotEnough = $monthData['days_requested'] - $remainingDaysInMonthRemaining; $monthMessage = "* Tháng {$monthData['month']}/{$monthData['year']}: \n - Số ngày phép còn lại: {$remainingDaysInMonthRemaining}, Số ngày yêu cầu: {$monthData['days_requested']}.\n - Bạn sẽ sử dụng {$remainingDaysInMonthRemaining} ngày phép và {$daysNotEnough} ngày không phép."; $errorMessage .= $errorMessage ? "\n\n" . $monthMessage : $monthMessage; @@ -502,14 +519,37 @@ class TicketController extends Controller $days_will_use = $remainingDaysInMonthRemaining; $days_will_use_without_pay = $daysNotEnough; - } else if ($remainingDaysInMonthRemaining >= $monthData['days_requested']) { // Đủ ngày phép ở tháng đó - + } else if ( + $remainingDaysInMonthRemaining >= $monthData['days_requested'] + // || $remainingDaysInMonthIsUsed + $monthData['days_requested'] > $maxDaysPerMonth + ) { // Đủ ngày phép ở tháng đó // 1. Check thêm rule 1 tháng chỉ được nghỉ tối đa $maxDaysPerMonth ngày có phép, ngày vượt sẽ là ngày không phép - if ($monthData['days_requested'] > $maxDaysPerMonth) { + if($totalDaysInMonth > $maxDaysPerMonth){ + $daysWithoutPermission = $totalDaysInMonth - $maxDaysPerMonth; + $daysWillUse = $maxDaysPerMonth - $usedDaysInMonth; // số ngày phép sẽ sử dụng + $hasInsufficientDays = true; - $month_data['status'] = 'exceed_max_days'; - $daysWithoutPermission = $monthData['days_requested'] - $maxDaysPerMonth; - $monthMessage = "* Theo quy định ngày phép tôi đa mỗi tháng là {$maxDaysPerMonth} ngày. \nTháng {$monthData['month']}/{$monthData['year']}: \n - Số ngày phép còn lại: {$remainingDaysInMonthRemaining}, Số ngày yêu cầu: {$monthData['days_requested']}.\n - Bạn sẽ sử dụng {$maxDaysPerMonth} ngày phép và {$daysWithoutPermission} ngày không phép."; + $month_data_status = 'exceed_max_days'; + + $monthMessage = "* Theo quy định ngày phép tối đa mỗi tháng là {$maxDaysPerMonth} ngày. \nTháng {$monthData['month']}/{$monthData['year']}: \n - Bạn đã sử dụng {$usedDaysInMonth} ngày phép, còn lại {$remainingDaysInMonthRemaining} ngày phép.\n - Bạn sẽ sử dụng " . $daysWillUse . " ngày phép và {$daysWithoutPermission} ngày không phép."; + + $errorMessage .= $errorMessage ? "\n\n" . $monthMessage : $monthMessage; + $days_will_use = $daysWillUse; + $days_will_use_without_pay = $daysWithoutPermission; + }else if ($monthData['days_requested'] + $remainingDaysInMonthIsUsed > $maxDaysPerMonth) { + if ($remainingDaysInMonthIsUsed > 0) { + + $daysWillUse = $maxDaysPerMonth - $remainingDaysInMonthIsUsed; // số ngày phép sẽ sử dụng + $daysWithoutPermission = $monthData['days_requested'] - $daysWillUse; + + $monthMessage = "* Theo quy định ngày phép tối đa mỗi tháng là {$maxDaysPerMonth} ngày. \nTháng {$monthData['month']}/{$monthData['year']}: \n - Bạn đã sử dụng {$remainingDaysInMonthIsUsed} ngày phép, còn lại {$remainingDaysInMonthRemaining} ngày phép.\n - Bạn sẽ sử dụng " . $daysWillUse . " ngày phép và {$daysWithoutPermission} ngày không phép."; + } else { + $daysWithoutPermission = $monthData['days_requested'] - $maxDaysPerMonth; + $monthMessage = "* Theo quy định ngày phép tối đa mỗi tháng là {$maxDaysPerMonth} ngày. \nTháng {$monthData['month']}/{$monthData['year']}: \n - Số ngày phép còn lại: {$remainingDaysInMonthRemaining}, Số ngày yêu cầu: {$monthData['days_requested']}.\n - Bạn sẽ sử dụng {$maxDaysPerMonth} ngày phép và {$daysWithoutPermission} ngày không phép."; + } + $hasInsufficientDays = true; + $month_data_status = 'exceed_max_days'; + $errorMessage .= $errorMessage ? "\n\n" . $monthMessage : $monthMessage; $days_will_use = $maxDaysPerMonth; @@ -536,7 +576,7 @@ class TicketController extends Controller 'remaining_days_in_month_remaining' => $remainingDaysInMonthRemaining, 'days_will_use' => $days_will_use, //Số ngày phép sẽ sử dụng 'days_will_use_without_pay' => $days_will_use_without_pay, //Số ngày không phép sẽ sử dụng - 'status' => 'ok', // mặc định là ok + 'status' => $month_data_status, // mặc định là ok ]; // Thêm thông tin tháng vào mảng kết quả @@ -584,9 +624,9 @@ class TicketController extends Controller // if ($leaveDaysInfo->ld_day_total > $month) { // $totalAllocated = $month; // } else { - // $totalAllocated = $leaveDaysInfo->ld_day_total; + $totalAllocated = $leaveDaysInfo->ld_day_total; // } - $totalAllocated = $month; //(+ tạm để check) + // $totalAllocated = $month; //(+ tạm để check) // bên hàm duyệt ticket sẽ check lại để + 1 ngày trước job để đảm bảo đủ ngày phép } else { Log::warning("No LeaveDays record found for user ID: {$user->id}, year: {$year}. Assuming 0 allocated days.");