truong-leave-day #116
			
				
			
		
		
		
	| 
						 | 
					@ -232,16 +232,11 @@ class TicketController extends Controller
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            // Kiểm tra số dư ngày phép
 | 
					            // Kiểm tra số dư ngày phép
 | 
				
			||||||
            $balanceCheckResult = $this->checkLeaveBalance($user, $currentYear, $dataListPeriod);
 | 
					            $balanceCheckResult = $this->checkLeaveBalance($user, $currentYear, $dataListPeriod);
 | 
				
			||||||
            dd($balanceCheckResult);
 | 
					 | 
				
			||||||
            // Nếu không đủ ngày phép, trả về thông báo và không tạo ticket
 | 
					            // Nếu không đủ ngày phép, trả về thông báo và không tạo ticket
 | 
				
			||||||
            if (!$balanceCheckResult['success']) {
 | 
					            if (!$balanceCheckResult['success']) {
 | 
				
			||||||
                return response()->json([
 | 
					                return response()->json($balanceCheckResult);
 | 
				
			||||||
                    'message' => $balanceCheckResult['message'],
 | 
					 | 
				
			||||||
                    'status' => false
 | 
					 | 
				
			||||||
                ]);
 | 
					 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        dd("đã qua kiểm tra");
 | 
					 | 
				
			||||||
        // --- Kết thúc kiểm tra ---
 | 
					        // --- Kết thúc kiểm tra ---
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Nếu đủ ngày phép (hoặc loại ticket không phải ONLEAVE), tiếp tục tạo ticket
 | 
					        // Nếu đủ ngày phép (hoặc loại ticket không phải ONLEAVE), tiếp tục tạo ticket
 | 
				
			||||||
| 
						 | 
					@ -296,7 +291,7 @@ class TicketController extends Controller
 | 
				
			||||||
     * @param array|null $dataListPeriod Danh sách các ngày xin nghỉ [['date' => 'Y-m-d', 'period' => 'ALL|S|C'], ...]
 | 
					     * @param array|null $dataListPeriod Danh sách các ngày xin nghỉ [['date' => 'Y-m-d', 'period' => 'ALL|S|C'], ...]
 | 
				
			||||||
     * @return array Kết quả kiểm tra ['success' => bool, 'message' => string|null, ...]
 | 
					     * @return array Kết quả kiểm tra ['success' => bool, 'message' => string|null, ...]
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    private function checkLeaveBalance($user, int $year, array $dataListPeriod = null): array
 | 
					    private function checkLeaveBalance($user, int $year, ?array $dataListPeriod = null): array
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        // Tính tổng số ngày yêu cầu
 | 
					        // Tính tổng số ngày yêu cầu
 | 
				
			||||||
        $daysRequested = $this->calculateTotalLeaveDays($dataListPeriod);
 | 
					        $daysRequested = $this->calculateTotalLeaveDays($dataListPeriod);
 | 
				
			||||||
| 
						 | 
					@ -390,7 +385,7 @@ class TicketController extends Controller
 | 
				
			||||||
        $monthsInfo = [];
 | 
					        $monthsInfo = [];
 | 
				
			||||||
        $hasInsufficientDays = false;
 | 
					        $hasInsufficientDays = false;
 | 
				
			||||||
        $errorMessage = '';
 | 
					        $errorMessage = '';
 | 
				
			||||||
 | 
					        $remainingDaysInMonthIsUsed = 0;
 | 
				
			||||||
        foreach ($requestMonths as $monthKey => $monthData) {
 | 
					        foreach ($requestMonths as $monthKey => $monthData) {
 | 
				
			||||||
            // Tính tổng số ngày nghỉ có phép trong tháng
 | 
					            // Tính tổng số ngày nghỉ có phép trong tháng
 | 
				
			||||||
            $usedDaysInMonth = $this->getUsedLeaveDaysInMonth($user, $monthData['year'], $monthData['month'], 'ONLEAVE');
 | 
					            $usedDaysInMonth = $this->getUsedLeaveDaysInMonth($user, $monthData['year'], $monthData['month'], 'ONLEAVE');
 | 
				
			||||||
| 
						 | 
					@ -425,18 +420,21 @@ class TicketController extends Controller
 | 
				
			||||||
                'status' => 'ok', // mặc định là ok
 | 
					                'status' => 'ok', // mặc định là ok
 | 
				
			||||||
            ];
 | 
					            ];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            $remainingDaysInMonthRemaining = $remainingDaysInMonth - $remainingDaysInMonthIsUsed;
 | 
				
			||||||
            // Xử lý các trường hợp thiếu ngày phép
 | 
					            // Xử lý các trường hợp thiếu ngày phép
 | 
				
			||||||
            if ($remainingDaysInMonth <= 0) { //hết phép
 | 
					            if ($remainingDaysInMonthRemaining <= 0) { //hết phép
 | 
				
			||||||
                $hasInsufficientDays = true;
 | 
					                $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']}\nBạn sẽ nộp: " . $monthData['days_requested'] . " ngày không phép.";
 | 
					                $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;
 | 
					                $errorMessage .= $errorMessage ? "\n\n" . $monthMessage : $monthMessage;
 | 
				
			||||||
            } else if ($remainingDaysInMonth < $monthData['days_requested']) { // không đủ ngày phép
 | 
					            } else if ($remainingDaysInMonthRemaining < $monthData['days_requested']) { // không đủ ngày phép
 | 
				
			||||||
                $hasInsufficientDays = true;
 | 
					                $hasInsufficientDays = true;
 | 
				
			||||||
                $month_data['status'] = 'insufficient_days';
 | 
					                $month_data['status'] = 'insufficient_days';
 | 
				
			||||||
                $daysNotEnough = $monthData['days_requested'] - $remainingDaysInMonth;
 | 
					                $daysNotEnough = $monthData['days_requested'] - $remainingDaysInMonthRemaining;
 | 
				
			||||||
                $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.";
 | 
					                $monthMessage = "Tháng {$monthData['month']}/{$monthData['year']}: Số ngày phép còn lại: {$remainingDaysInMonthRemaining}, Số ngày yêu cầu: {$monthData['days_requested']}.\nBạn sẽ sử dụng {$remainingDaysInMonthRemaining} ngày phép và {$daysNotEnough} ngày không phép.";
 | 
				
			||||||
                $errorMessage .= $errorMessage ? "\n\n" . $monthMessage : $monthMessage;
 | 
					                $errorMessage .= $errorMessage ? "\n\n" . $monthMessage : $monthMessage;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                $remainingDaysInMonthIsUsed = $remainingDaysInMonth;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            // Thêm thông tin tháng vào mảng kết quả
 | 
					            // Thêm thông tin tháng vào mảng kết quả
 | 
				
			||||||
| 
						 | 
					@ -754,6 +752,18 @@ class TicketController extends Controller
 | 
				
			||||||
        $afternoon = $time_type->get('C');
 | 
					        $afternoon = $time_type->get('C');
 | 
				
			||||||
        $all_day = $time_type->get('ALL');
 | 
					        $all_day = $time_type->get('ALL');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Get all Saturday work schedules and sort them by date in descending order
 | 
				
			||||||
 | 
					        $saturday_work_schedules = Category::where('c_type', 'SATURDAY_WORK_SCHEDULE')
 | 
				
			||||||
 | 
					            ->get()
 | 
				
			||||||
 | 
					            ->sortByDesc(function ($item) {
 | 
				
			||||||
 | 
					                // Parse the date string from c_code to a Carbon instance for proper comparison
 | 
				
			||||||
 | 
					                return Carbon::createFromFormat('d-m-Y', $item->c_code);
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Get the most recent schedule date (first item after sorting)
 | 
				
			||||||
 | 
					        $latest_schedule = $saturday_work_schedules->first();
 | 
				
			||||||
 | 
					        $latestScheduleDate = Carbon::createFromFormat('d-m-Y', $latest_schedule->c_code);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (!$morning || !$afternoon || !$all_day) {
 | 
					        if (!$morning || !$afternoon || !$all_day) {
 | 
				
			||||||
            // Handle error: TIME_TYPE categories not found
 | 
					            // Handle error: TIME_TYPE categories not found
 | 
				
			||||||
            Log::error("TIME_TYPE categories (S, C, ALL) not found in database.");
 | 
					            Log::error("TIME_TYPE categories (S, C, ALL) not found in database.");
 | 
				
			||||||
| 
						 | 
					@ -761,8 +771,24 @@ class TicketController extends Controller
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        foreach ($period as $date) {
 | 
					        foreach ($period as $date) {
 | 
				
			||||||
            // Bỏ qua Thứ 7 (6) và Chủ Nhật (0)
 | 
					            // Check if the current day is a Saturday
 | 
				
			||||||
            if ($date->dayOfWeek === Carbon::SATURDAY || $date->dayOfWeek === Carbon::SUNDAY) {
 | 
					            if ($date->dayOfWeek === Carbon::SATURDAY) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                if ($latest_schedule) {
 | 
				
			||||||
 | 
					                    $weeksDifference = $latestScheduleDate->startOfDay()->diffInWeeks($date->copy()->startOfDay());
 | 
				
			||||||
 | 
					                    $isSaturdayWorkDay = ($weeksDifference % 2 === 0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    // echo $date->toDateString() . ' - ' . ($isSaturdayWorkDay ? 'Làm việc' : 'Nghỉ') . "<br>";
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                if ($isSaturdayWorkDay) {
 | 
				
			||||||
 | 
					                    $results[] = ['date' => $date->toDateString(), 'period' => "S"];
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                continue;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            // Skip Sundays entirely
 | 
				
			||||||
 | 
					            else if ($date->dayOfWeek === Carbon::SUNDAY) {
 | 
				
			||||||
                continue;
 | 
					                continue;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue