Tạo ticket tự động cho user khi user không check in, check out

This commit is contained in:
Truong Vo 2024-10-09 10:26:33 +07:00
parent f55ac7e65c
commit 7fa50478cc
4 changed files with 166 additions and 1 deletions

View File

@ -0,0 +1,33 @@
<?php
namespace App\Console\Commands;
use App\Jobs\CheckUserAttendanceJob;
use Illuminate\Console\Command;
class CheckUserAttendanceCommand extends Command
{
// Định nghĩa command signature
protected $signature = 'attendance:check {period?}';
protected $description = 'Kiểm tra check in và check out của người dùng và tạo ticket nếu thiếu';
/**
* Create a new command instance.
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*/
public function handle()
{
// Lấy argument period (Sáng "S" hoặc Chiều "C")
$period = $this->argument('period');
// Dispatch job để kiểm tra check in và check out
CheckUserAttendanceJob::dispatch($period);
}
}

View File

@ -26,6 +26,12 @@ class Kernel extends ConsoleKernel
// Chạy command vào ngày 31/12 lúc 23:59:59 mỗi năm // Chạy command vào ngày 31/12 lúc 23:59:59 mỗi năm
$schedule->command('initialize:leavedays')->yearlyOn(12, 31, '23:59:59'); $schedule->command('initialize:leavedays')->yearlyOn(12, 31, '23:59:59');
$schedule->command('leave:deduct')->yearlyOn(3, 31, '23:59:59'); $schedule->command('leave:deduct')->yearlyOn(3, 31, '23:59:59');
// Chạy buổi sáng lúc 12:00
$schedule->command('attendance:check S')->dailyAt('12:00');
// Chạy buổi chiều lúc 17:30
$schedule->command('attendance:check C')->dailyAt('17:30');
} }
/** /**

View File

@ -0,0 +1,126 @@
<?php
namespace App\Jobs;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Modules\Admin\app\Models\Ticket;
use Modules\Admin\app\Models\Tracking;
class CheckUserAttendanceJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $period;
/**
* Create a new job instance.
* @param string|null $period
*/
public function __construct($period = null)
{
$this->period = $period;
}
/**
* Execute the job.
*/
public function handle()
{
// Lấy tất cả người dùng
$users = User::all();
foreach ($users as $key => $user) {
// if($user->id != 2){
// continue;
// }
// Kiểm tra dựa trên period (Sáng 'S' hoặc Chiều 'C')
if ($this->period === 'S') {
$this->checkMorning($user->id);
} elseif ($this->period === 'C') {
$this->checkAfternoon($user->id);
} else {
// Nếu không có period, kiểm tra cả sáng và chiều
$this->checkMorning($user->id);
$this->checkAfternoon($user->id);
}
}
}
/**
* Kiểm tra check-in/check-out buổi sáng tạo ticket nếu thiếu.
* @param int $userId
*/
public function checkMorning($userId)
{
$morningCheckIn = Carbon::createFromTime(7, 30);
$morningCheckOut = Carbon::createFromTime(11, 30);
$checkDeadline = Carbon::createFromTime(12, 0); // Hạn kiểm tra cho buổi sáng
$today = Carbon::today();
// Lấy tất cả tracking của user cho buổi sáng (từ 7:30 đến 12:00)
$morningRecords = Tracking::where('user_id', $userId)
->whereBetween('time_string', [$today->copy()->setTime(7, 30), $checkDeadline])
->get();
$hasMorningCheckIn = $morningRecords->where('status', 'check in')->isNotEmpty();
$hasMorningCheckOut = $morningRecords->where('status', 'check out')->isNotEmpty();
// Nếu không có check-in hoặc check-out, tạo ticket
if (!$hasMorningCheckIn && !$hasMorningCheckOut) {
Ticket::create([
'user_id' => $userId,
'start_date' => $today->format('Y-m-d'),
'start_period' => 'S', // Morning
'end_date' => $today->format('Y-m-d'),
'end_period' => 'S', // Morning
'type' => 'UNAUTHORIZEDLEAVE',
'reason' => 'No check in/out for morning',
'status' => 'WAITING',
'created_at' => Carbon::now(),
'updated_at' => Carbon::now()
]);
}
}
/**
* Kiểm tra check-in/check-out buổi chiều tạo ticket nếu thiếu.
* @param int $userId
*/
public function checkAfternoon($userId)
{
$afternoonCheckIn = Carbon::createFromTime(13, 0);
$afternoonCheckOut = Carbon::createFromTime(17, 0);
$checkDeadline = Carbon::createFromTime(17, 30); // Hạn kiểm tra cho buổi chiều
$today = Carbon::today();
// Lấy tất cả tracking của user cho buổi chiều (từ 13:00 đến 17:30)
$afternoonRecords = Tracking::where('user_id', $userId)
->whereBetween('time_string', [$today->copy()->setTime(13, 0), $checkDeadline])
->get();
$hasAfternoonCheckIn = $afternoonRecords->where('status', 'check in')->isNotEmpty();
$hasAfternoonCheckOut = $afternoonRecords->where('status', 'check out')->isNotEmpty();
// Nếu không có check-in hoặc check-out, tạo ticket
if (!$hasAfternoonCheckIn && !$hasAfternoonCheckOut) {
Ticket::create([
'user_id' => $userId,
'start_date' => $today->format('Y-m-d'),
'start_period' => 'C', // Afternoon
'end_date' => $today->format('Y-m-d'),
'end_period' => 'C', // Afternoon
'type' => 'UNAUTHORIZEDLEAVE',
'reason' => 'No check in/out for afternoon',
'status' => 'WAITING',
'created_at' => Carbon::now(),
'updated_at' => Carbon::now()
]);
}
}
}

View File

@ -67,7 +67,7 @@ class DeductLeaveDays implements ShouldQueue
]); ]);
} }
} else { } else {
//Nếu không sử dụng ngày nghỉ còn lại ở nm rồi thì xóa => theo luật ld //Nếu không sử dụng ngày nghỉ còn lại ở năm rồi thì xóa => theo luật ld
LeaveDays::where('ld_year', $this->year) LeaveDays::where('ld_year', $this->year)
->where('ld_user_id', $user->id) ->where('ld_user_id', $user->id)
->update([ ->update([