[Ngày Phép] Thực hiện Job cập nhật ngày phép bảng mỗi tháng + 1 ngày phép cho mỗi user, thời gian 00:00 ngày 1 hàng tháng
This commit is contained in:
parent
05cb3f722a
commit
308f5a3a2e
|
|
@ -0,0 +1,24 @@
|
|||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use App\Jobs\AddMonthlyLeaveDays;
|
||||
|
||||
class AddMonthlyLeaveDaysCommand extends Command
|
||||
{
|
||||
protected $signature = 'add:monthly-leavedays {month?} {year?}';
|
||||
protected $description = 'Cộng 1 ngày phép hàng tháng cho tất cả người dùng';
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function handle()
|
||||
{
|
||||
$month = $this->argument('month');
|
||||
$year = $this->argument('year');
|
||||
AddMonthlyLeaveDays::dispatch($month, $year);
|
||||
}
|
||||
}
|
||||
|
|
@ -8,7 +8,7 @@ use App\Jobs\InitializeLeaveDays;
|
|||
class InitializeLeaveDaysCommand extends Command
|
||||
{
|
||||
protected $signature = 'initialize:leavedays {year?}';
|
||||
protected $description = 'Initialize leave days for users';
|
||||
protected $description = 'Cấp phép năm cho tất cả người dùng';
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
|
|
@ -18,6 +18,7 @@ class InitializeLeaveDaysCommand extends Command
|
|||
public function handle()
|
||||
{
|
||||
$year = $this->argument('year');
|
||||
InitializeLeaveDays::dispatch($year);
|
||||
// Không sử dụng nữa, theo rule mới
|
||||
// InitializeLeaveDays::dispatch($year);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
namespace App\Console;
|
||||
|
||||
use App\Jobs\DeductLeaveDays;
|
||||
use App\Jobs\AddMonthlyLeaveDays;
|
||||
use Illuminate\Console\Scheduling\Schedule;
|
||||
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
|
||||
|
||||
|
|
@ -24,7 +25,7 @@ class Kernel extends ConsoleKernel
|
|||
// ->dailyAt('18:00');
|
||||
|
||||
// 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');
|
||||
|
||||
// Chạy buổi sáng lúc 12:00
|
||||
|
|
@ -32,6 +33,9 @@ class Kernel extends ConsoleKernel
|
|||
|
||||
// Chạy buổi chiều lúc 17:30
|
||||
$schedule->command('attendance:check C')->dailyAt('17:30');
|
||||
|
||||
// Chạy vào 00:01 ngày đầu tiên của mỗi tháng
|
||||
$schedule->command('add:monthly-leavedays')->monthlyOn(1, '00:01');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -0,0 +1,70 @@
|
|||
<?php
|
||||
|
||||
namespace App\Jobs;
|
||||
|
||||
use App\Models\LeaveDays;
|
||||
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;
|
||||
|
||||
class AddMonthlyLeaveDays implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
protected $month;
|
||||
protected $year;
|
||||
|
||||
public function __construct($month = null, $year = null)
|
||||
{
|
||||
$this->month = $month ?? Carbon::now()->month;
|
||||
$this->year = $year ?? Carbon::now()->year;
|
||||
}
|
||||
|
||||
public function handle(): void
|
||||
{
|
||||
$users = User::get();
|
||||
|
||||
foreach ($users as $user) {
|
||||
$leaveDay = LeaveDays::where('ld_user_id', $user->id)
|
||||
->where('ld_year', $this->year)
|
||||
->first();
|
||||
|
||||
if (!$leaveDay) {
|
||||
// Nếu chưa có dữ liệu năm hiện tại, tạo mới
|
||||
// Số ngày phép bằng với tháng hiện tại
|
||||
$leaveDay = new LeaveDays([
|
||||
'ld_user_id' => $user->id,
|
||||
'ld_day_total' => $this->month, // Số ngày phép bằng tháng hiện tại
|
||||
'ld_year' => $this->year,
|
||||
'ld_additional_day' => 0,
|
||||
'ld_note' => 'Khởi tạo ngày phép đến tháng ' . $this->month,
|
||||
'ld_special_leave_day' => 0,
|
||||
]);
|
||||
$leaveDay->save();
|
||||
} else {
|
||||
// Kiểm tra nếu số ngày phép hiện tại nhỏ hơn tháng hiện tại
|
||||
if ($leaveDay->ld_day_total < $this->month) {
|
||||
// Cập nhật số ngày phép bằng với tháng hiện tại
|
||||
$oldDays = $leaveDay->ld_day_total;
|
||||
$leaveDay->ld_day_total = $this->month;
|
||||
|
||||
// Xử lý ghi chú
|
||||
$newNote = "Cập nhật ngày phép đến tháng " . $this->month;
|
||||
if (!empty($leaveDay->ld_note)) {
|
||||
// Nếu đã có ghi chú, thêm ghi chú mới vào và xuống dòng
|
||||
$leaveDay->ld_note = $leaveDay->ld_note . "\n" . $newNote;
|
||||
} else {
|
||||
// Nếu chưa có ghi chú, gán ghi chú mới
|
||||
$leaveDay->ld_note = $newNote;
|
||||
}
|
||||
|
||||
$leaveDay->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue