80 lines
2.8 KiB
PHP
80 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Models\LeaveDays;
|
|
use App\Models\Notes;
|
|
use App\Models\User;
|
|
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\DB;
|
|
use Carbon\Carbon;
|
|
|
|
class DeductLeaveDays implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
protected $year;
|
|
|
|
/**
|
|
* Create a new job instance.
|
|
*/
|
|
public function __construct($year = null)
|
|
{
|
|
// Nếu không có năm được truyền vào, sử dụng năm hiện tại
|
|
$this->year = $year ?? Carbon::now()->year;
|
|
}
|
|
|
|
/**
|
|
* Execute the job.
|
|
*/
|
|
public function handle()
|
|
{
|
|
$users = User::get();
|
|
foreach ($users as $user) {
|
|
$existingData = LeaveDays::where('ld_user_id', $user->id)
|
|
->where('ld_year', $this->year)
|
|
->where('ld_additional_day', ">", 0)
|
|
->first();
|
|
if (!$existingData) {
|
|
continue;
|
|
}
|
|
|
|
$totalLeaveDaysByMonth = Notes::join('categories', function ($join) {
|
|
$join->on('notes.n_time_type', '=', 'categories.c_code')
|
|
->where('categories.c_type', 'TIME_TYPE');
|
|
})
|
|
->select(
|
|
DB::raw('notes.n_user_id as n_user_id'),
|
|
DB::raw('notes.n_year as year'),
|
|
DB::raw('SUM(categories.c_value) as leave_days')
|
|
)
|
|
->where('notes.n_year', $this->year)
|
|
->where('notes.n_user_id', $user->id)
|
|
->where('notes.n_reason', 'ONLEAVE')
|
|
->groupBy(DB::raw('notes.n_year'))
|
|
->first();
|
|
|
|
if ($totalLeaveDaysByMonth) {
|
|
//Nếu ngày phép thừa năm trước chưa sử dụng hết => cập nhật lại ngày đó (Ngày tồn đọng - ngày sử dụng)
|
|
if ($existingData->ld_additional_day > $totalLeaveDaysByMonth->leave_days) {
|
|
LeaveDays::where('ld_year', $this->year)
|
|
->where('ld_user_id', $user->id)
|
|
->update([
|
|
'ld_additional_day' => $totalLeaveDaysByMonth->leave_days,
|
|
]);
|
|
}
|
|
} 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
|
|
LeaveDays::where('ld_year', $this->year)
|
|
->where('ld_user_id', $user->id)
|
|
->update([
|
|
'ld_additional_day' => "0",
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
}
|