Truong-CreateAutoTicketKPhep #94
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
$schedule->command('initialize:leavedays')->yearlyOn(12, 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');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -0,0 +1,143 @@
|
|||
<?php
|
||||
|
||||
namespace App\Jobs;
|
||||
|
||||
use App\Mail\TicketMail;
|
||||
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 Illuminate\Support\Facades\Mail;
|
||||
use Modules\Admin\app\Http\Controllers\CategoryController;
|
||||
use Modules\Admin\app\Models\Admin;
|
||||
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::where('permission', 'not like', '%admin%')->get();
|
||||
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);
|
||||
} elseif ($this->period === 'C') {
|
||||
$this->checkAfternoon($user);
|
||||
} else {
|
||||
if ($this->period == null) {
|
||||
// Nếu không có period, kiểm tra cả sáng và chiều
|
||||
$this->checkMorning($user);
|
||||
$this->checkAfternoon($user);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function checkAttendance($user, $periodCode, $startTime, $endTime)
|
||||
{
|
||||
$today = Carbon::today();
|
||||
$userId = $user->id;
|
||||
// Lấy tất cả tracking của user trong khoảng thời gian được chỉ định
|
||||
$records = Tracking::where('user_id', $userId)
|
||||
->whereBetween('time_string', [$startTime, $endTime])
|
||||
->get();
|
||||
|
||||
$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
|
||||
$existingTicket = Ticket::where('user_id', $userId)
|
||||
->where('start_date', $today->format('Y-m-d'))
|
||||
->where('start_period', $periodCode)
|
||||
->where('end_date', $today->format('Y-m-d'))
|
||||
->where('end_period', $periodCode)
|
||||
->whereIn('status', ['WAITING', 'CONFIRMED'])
|
||||
->first();
|
||||
$type = 'ONLEAVE';
|
||||
$reason = 'KHONG PHEP';
|
||||
// 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) {
|
||||
Ticket::create([
|
||||
'user_id' => $userId,
|
||||
'start_date' => $today->format('Y-m-d'),
|
||||
'start_period' => $periodCode,
|
||||
'end_date' => $today->format('Y-m-d'),
|
||||
'end_period' => $periodCode,
|
||||
'type' => $type,
|
||||
'reason' => $reason,
|
||||
'status' => 'WAITING',
|
||||
'created_at' => Carbon::now(),
|
||||
'updated_at' => Carbon::now()
|
||||
]);
|
||||
|
||||
//Send mail
|
||||
$dataMasterStartPeriod = CategoryController::getListMasterByCodeAndType("TIME_TYPE", $periodCode);
|
||||
$dataMasterEndPeriod = CategoryController::getListMasterByCodeAndType("TIME_TYPE", $periodCode);
|
||||
$dataMasterType = CategoryController::getListMasterByCodeAndType("REASON", $type);
|
||||
|
||||
$formattedStartDate = $today->format('d/m/Y');
|
||||
$formattedEndDate = $today->format('d/m/Y');
|
||||
|
||||
$admins = Admin::where('permission', 'like', '%admin%')->get();
|
||||
foreach ($admins as $key => $value) {
|
||||
$data = array(
|
||||
"email_template" => "email.notification_tickets",
|
||||
"email" => $user->email,
|
||||
"name" => $user->name,
|
||||
"date" => $dataMasterStartPeriod->c_name . " (" . $formattedStartDate . ") - " . $dataMasterEndPeriod->c_name . " (" . $formattedEndDate . ")",
|
||||
"type" => $dataMasterType->c_name,
|
||||
"note" => $reason,
|
||||
"link" => "/tickets-management", //link đến page admin
|
||||
"subject" => "[Ticket request] Ticket From " . $user->name
|
||||
);
|
||||
Mail::to($value->email)->send(new TicketMail($data));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Kiểm tra check-in/check-out buổi sáng và tạo ticket nếu thiếu.
|
||||
* @param int $userId
|
||||
*/
|
||||
public function checkMorning($user)
|
||||
{
|
||||
$startTime = Carbon::today()->setTime(6, 0); // Thời gian bắt đầu buổi sáng
|
||||
$endTime = Carbon::createFromTime(12, 0); // Thời gian kết thúc buổi sáng
|
||||
$this->checkAttendance($user, 'S', $startTime, $endTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* Kiểm tra check-in/check-out buổi chiều và tạo ticket nếu thiếu.
|
||||
* @param int $userId
|
||||
*/
|
||||
public function checkAfternoon($user)
|
||||
{
|
||||
$startTime = Carbon::today()->setTime(12, 0); // Thời gian bắt đầu buổi chiều
|
||||
$endTime = Carbon::createFromTime(17, 30); // Thời gian kết thúc buổi chiều
|
||||
$this->checkAttendance($user, 'C', $startTime, $endTime);
|
||||
}
|
||||
}
|
||||
|
|
@ -67,7 +67,7 @@ class DeductLeaveDays implements ShouldQueue
|
|||
]);
|
||||
}
|
||||
} else {
|
||||
//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
|
||||
//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)
|
||||
->where('ld_user_id', $user->id)
|
||||
->update([
|
||||
|
|
|
|||
Loading…
Reference in New Issue