From 7e369785d592d2f19ab1df57a4afd668c65c33e6 Mon Sep 17 00:00:00 2001 From: Truong Vo <41848815+vmtruong301296@users.noreply.github.com> Date: Tue, 30 Dec 2025 10:08:18 +0700 Subject: [PATCH] fix --- .../app/Http/Controllers/TicketController.php | 42 ++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/BACKEND/Modules/Admin/app/Http/Controllers/TicketController.php b/BACKEND/Modules/Admin/app/Http/Controllers/TicketController.php index 6488e7a..19b4ca4 100644 --- a/BACKEND/Modules/Admin/app/Http/Controllers/TicketController.php +++ b/BACKEND/Modules/Admin/app/Http/Controllers/TicketController.php @@ -726,6 +726,40 @@ class TicketController extends Controller ]; } + /** + * Khởi tạo dữ liệu ngày phép cho user nếu chưa có + * + * @param UserModel $user + * @param int $year + * @return LeaveDays|null + */ + private function initializeLeaveDaysForYear($user, int $year): ?LeaveDays + { + // Nếu là nhân viên chưa chính thức, ko cộng phép + if (!$user->is_permanent) { + return null; + } + + // Nếu là nhân viên nghỉ việc, ko cộng phép + if ($user->is_separated) { + return null; + } + + // 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 + $leaveDaysInfo = new LeaveDays([ + 'ld_user_id' => $user->id, + 'ld_day_total' => 1, + 'ld_year' => $year, + 'ld_additional_day' => 0, + 'ld_note' => 'Khởi tạo ngày phép tháng 1 khi tạo ticket cho năm sau', + 'ld_special_leave_day' => 0, + ]); + $leaveDaysInfo->save(); + + return $leaveDaysInfo; + } + //Tính tổng số ngày nghỉ có phép đến tháng hiện tại private function getTotalLeaveDaysInMonthToMonth($user, int $year, int $month): float { @@ -745,7 +779,13 @@ class TicketController extends Controller $leaveDaysInfo = LeaveDays::where('ld_user_id', $user->id) ->where('ld_year', $year) ->first(); - + + if (!$leaveDaysInfo) { + $leaveDaysInfo = $this->initializeLeaveDaysForYear($user, $year); + if (!$leaveDaysInfo) { + return 0; + } + } $totalAllocated = 0; // Xử lý gửi ticket sau tháng hiện tại if ($leaveDaysInfo && $user->is_permanent) {