Merge pull request 'BỔ SUNG CHECK THÊM điều kiện user ticket nguyên ngày' (#97) from Truong-CreateAutoTicketKPhep into master
Reviewed-on: #97
This commit is contained in:
		
						commit
						cc0a62f746
					
				| 
						 | 
					@ -69,16 +69,55 @@ class CheckUserAttendanceJob implements ShouldQueue
 | 
				
			||||||
        $hasCheckIn = $records->where('status', 'check in')->isNotEmpty();
 | 
					        $hasCheckIn = $records->where('status', 'check in')->isNotEmpty();
 | 
				
			||||||
        $hasCheckOut = $records->where('status', 'check out')->isNotEmpty();
 | 
					        $hasCheckOut = $records->where('status', 'check out')->isNotEmpty();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Kiểm tra nếu đã có ticket chờ xử lý hoặc đã được xác nhận
 | 
					        // Kiểm tra nếu đã có ticket chờ xử lý hoặc đã được xác nhận, cho cả sáng và chiều
 | 
				
			||||||
        $existingTicket = Ticket::where('user_id', $userId)
 | 
					        $existingTicket = Ticket::where('user_id', $userId)
 | 
				
			||||||
            ->where('start_date', $today->format('Y-m-d'))
 | 
					            ->where(function ($query) use ($today, $periodCode) {
 | 
				
			||||||
            ->where('end_date', $today->format('Y-m-d'))
 | 
					                // Check for tickets that exactly cover the current day and period
 | 
				
			||||||
            ->where(function ($query) use ($periodCode) {
 | 
					                $query->where(function ($subQuery) use ($today, $periodCode) {
 | 
				
			||||||
                $query->where('start_period', $periodCode) // Check the current period (S or C)
 | 
					                    $subQuery->where('start_date', $today->format('Y-m-d'))
 | 
				
			||||||
                    ->orWhere(function ($query) {
 | 
					                        ->where('end_date', $today->format('Y-m-d'))
 | 
				
			||||||
                        // Check if there's a ticket for both morning and afternoon
 | 
					                        ->where(function ($periodQuery) use ($periodCode) {
 | 
				
			||||||
                        $query->where('start_period', 'S')
 | 
					                            // Check if the ticket covers the current period
 | 
				
			||||||
                            ->where('end_period', 'C');
 | 
					                            $periodQuery->where('start_period', $periodCode)
 | 
				
			||||||
 | 
					                                ->orWhere(function ($query) {
 | 
				
			||||||
 | 
					                                    // Check for a full-day ticket (S -> C)
 | 
				
			||||||
 | 
					                                    $query->where('start_period', 'S')
 | 
				
			||||||
 | 
					                                        ->where('end_period', 'C');
 | 
				
			||||||
 | 
					                                });
 | 
				
			||||||
 | 
					                        });
 | 
				
			||||||
 | 
					                })
 | 
				
			||||||
 | 
					                    // Check for tickets that span multiple days
 | 
				
			||||||
 | 
					                    ->orWhere(function ($subQuery) use ($today, $periodCode) {
 | 
				
			||||||
 | 
					                        $subQuery->where('start_date', '<=', $today->format('Y-m-d'))
 | 
				
			||||||
 | 
					                            ->where('end_date', '>=', $today->format('Y-m-d'))
 | 
				
			||||||
 | 
					                            ->where(function ($periodQuery) use ($periodCode, $today) {
 | 
				
			||||||
 | 
					                                $periodQuery->where(function ($query)  use ($today) {
 | 
				
			||||||
 | 
					                                    $query->where('start_date', '<', $today->format('Y-m-d'))
 | 
				
			||||||
 | 
					                                        ->where('end_date', '>', $today->format('Y-m-d'));
 | 
				
			||||||
 | 
					                                })
 | 
				
			||||||
 | 
					                                    ->orWhere(function ($query) use ($today, $periodCode) {
 | 
				
			||||||
 | 
					                                        $query->where(function ($queryDateStart) use ($today, $periodCode) {
 | 
				
			||||||
 | 
					                                            $queryDateStart->where('start_date', '=', $today->format('Y-m-d'))
 | 
				
			||||||
 | 
					                                                ->where('end_date', '>', $today->format('Y-m-d'))
 | 
				
			||||||
 | 
					                                                ->where(function ($queryDateStartChild) use ($periodCode) {
 | 
				
			||||||
 | 
					                                                    $queryDateStartChild->where('start_period', $periodCode)
 | 
				
			||||||
 | 
					                                                        ->orWhere(function ($query) {
 | 
				
			||||||
 | 
					                                                            $query->where('start_period', 'S');
 | 
				
			||||||
 | 
					                                                        });
 | 
				
			||||||
 | 
					                                                });
 | 
				
			||||||
 | 
					                                        })
 | 
				
			||||||
 | 
					                                            ->orWhere(function ($queryDateEnd) use ($today, $periodCode) {
 | 
				
			||||||
 | 
					                                                $queryDateEnd->where('end_date', '=', $today->format('Y-m-d'))
 | 
				
			||||||
 | 
					                                                    ->where('start_date', '<', $today->format('Y-m-d'))
 | 
				
			||||||
 | 
					                                                    ->where(function ($queryDateStartChild) use ($periodCode) {
 | 
				
			||||||
 | 
					                                                        $queryDateStartChild->where('end_period', $periodCode)
 | 
				
			||||||
 | 
					                                                            ->orWhere(function ($query) {
 | 
				
			||||||
 | 
					                                                                $query->where('end_period', 'C');
 | 
				
			||||||
 | 
					                                                            });
 | 
				
			||||||
 | 
					                                                    });
 | 
				
			||||||
 | 
					                                            });
 | 
				
			||||||
 | 
					                                    });
 | 
				
			||||||
 | 
					                            });
 | 
				
			||||||
                    });
 | 
					                    });
 | 
				
			||||||
            })
 | 
					            })
 | 
				
			||||||
            ->whereIn('status', ['WAITING', 'CONFIRMED'])
 | 
					            ->whereIn('status', ['WAITING', 'CONFIRMED'])
 | 
				
			||||||
| 
						 | 
					@ -86,8 +125,15 @@ class CheckUserAttendanceJob implements ShouldQueue
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $type = 'ONLEAVE';
 | 
					        $type = 'ONLEAVE';
 | 
				
			||||||
        $reason = 'KHONG PHEP';
 | 
					        $reason = 'KHONG PHEP';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        //Check ngày hợp lệ nếu có check và check out của user khác
 | 
				
			||||||
 | 
					        $dateNow = Tracking::whereBetween('time_string', [$startTime, $endTime])
 | 
				
			||||||
 | 
					            ->get();
 | 
				
			||||||
 | 
					        $hasCheckIndateNow = $dateNow->where('status', 'check in')->isNotEmpty();
 | 
				
			||||||
 | 
					        $hasCheckOutdateNow = $dateNow->where('status', 'check out')->isNotEmpty();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Nếu không có check-in hoặc check-out, và chưa có ticket, tạo ticket mới
 | 
					        // Nếu không có check-in hoặc check-out, và chưa có ticket, tạo ticket mới
 | 
				
			||||||
        if (!$hasCheckIn && !$hasCheckOut && !$existingTicket) {
 | 
					        if (($hasCheckIndateNow && $hasCheckOutdateNow) && !$hasCheckIn && !$hasCheckOut && !$existingTicket) {
 | 
				
			||||||
            Ticket::create([
 | 
					            Ticket::create([
 | 
				
			||||||
                'user_id' => $userId,
 | 
					                'user_id' => $userId,
 | 
				
			||||||
                'start_date' => $today->format('Y-m-d'),
 | 
					                'start_date' => $today->format('Y-m-d'),
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue