From 1c16b4b16498ea77d8b106152d1849ba67f2e51a Mon Sep 17 00:00:00 2001 From: Truong Vo <41848815+vmtruong301296@users.noreply.github.com> Date: Wed, 16 Oct 2024 09:03:52 +0700 Subject: [PATCH 1/2] =?UTF-8?q?B=E1=BB=94=20SUNG=20CHECK=20TH=C3=8AM=20?= =?UTF-8?q?=C4=91i=E1=BB=81u=20ki=E1=BB=87n=20user=20ticket=20nguy=C3=AAn?= =?UTF-8?q?=20ng=C3=A0y?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BACKEND/app/Jobs/CheckUserAttendanceJob.php | 57 +++++++++++++++++---- 1 file changed, 48 insertions(+), 9 deletions(-) diff --git a/BACKEND/app/Jobs/CheckUserAttendanceJob.php b/BACKEND/app/Jobs/CheckUserAttendanceJob.php index 52952dd..1bf9fa0 100644 --- a/BACKEND/app/Jobs/CheckUserAttendanceJob.php +++ b/BACKEND/app/Jobs/CheckUserAttendanceJob.php @@ -69,16 +69,55 @@ class CheckUserAttendanceJob implements ShouldQueue $hasCheckIn = $records->where('status', 'check in')->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) - ->where('start_date', $today->format('Y-m-d')) - ->where('end_date', $today->format('Y-m-d')) - ->where(function ($query) use ($periodCode) { - $query->where('start_period', $periodCode) // Check the current period (S or C) - ->orWhere(function ($query) { - // Check if there's a ticket for both morning and afternoon - $query->where('start_period', 'S') - ->where('end_period', 'C'); + ->where(function ($query) use ($today, $periodCode) { + // Check for tickets that exactly cover the current day and period + $query->where(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) { + // Check if the ticket covers the current period + $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']) From 4334f3e73d024a17e8d39c477fa4fce9f2b98ef2 Mon Sep 17 00:00:00 2001 From: Truong Vo <41848815+vmtruong301296@users.noreply.github.com> Date: Wed, 16 Oct 2024 09:44:29 +0700 Subject: [PATCH 2/2] =?UTF-8?q?B=E1=BB=95=20sung=20check=20=C4=91i?= =?UTF-8?q?=E1=BB=81u=20ki=E1=BB=87n=20c=C3=B3=20user=20check=20in=20check?= =?UTF-8?q?=20out=20(ng=C3=A0y=20h=E1=BB=A3p=20l=E1=BB=87)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BACKEND/app/Jobs/CheckUserAttendanceJob.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/BACKEND/app/Jobs/CheckUserAttendanceJob.php b/BACKEND/app/Jobs/CheckUserAttendanceJob.php index 1bf9fa0..5dc3e21 100644 --- a/BACKEND/app/Jobs/CheckUserAttendanceJob.php +++ b/BACKEND/app/Jobs/CheckUserAttendanceJob.php @@ -125,8 +125,15 @@ class CheckUserAttendanceJob implements ShouldQueue $type = 'ONLEAVE'; $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 - if (!$hasCheckIn && !$hasCheckOut && !$existingTicket) { + if (($hasCheckIndateNow && $hasCheckOutdateNow) && !$hasCheckIn && !$hasCheckOut && !$existingTicket) { Ticket::create([ 'user_id' => $userId, 'start_date' => $today->format('Y-m-d'),