From 0b63632e1b9b42791e2ddea8e61cb6e3bc523c00 Mon Sep 17 00:00:00 2001 From: Truong Vo <41848815+vmtruong301296@users.noreply.github.com> Date: Tue, 6 Aug 2024 17:14:59 +0700 Subject: [PATCH] Update TimekeepingController.php --- .../Controllers/TimekeepingController.php | 52 +++++++++++++++---- 1 file changed, 41 insertions(+), 11 deletions(-) diff --git a/BACKEND/Modules/Admin/app/Http/Controllers/TimekeepingController.php b/BACKEND/Modules/Admin/app/Http/Controllers/TimekeepingController.php index f22f6cb..be341e5 100644 --- a/BACKEND/Modules/Admin/app/Http/Controllers/TimekeepingController.php +++ b/BACKEND/Modules/Admin/app/Http/Controllers/TimekeepingController.php @@ -114,17 +114,47 @@ class TimekeepingController extends Controller if ($user_id == "") { return response()->json(['status' => false, 'message' => 'User not found!']); } - Notes::insert([ - [ - "n_user_id" => $user_id, - "n_day" => $day, - "n_month" => $month, - "n_year" => $year, - "n_time_type" => $time_type, - "n_reason" => $reason, - "n_note" => $note - ] - ]); + // Notes::insert([ + // [ + // "n_user_id" => $user_id, + // "n_day" => $day, + // "n_month" => $month, + // "n_year" => $year, + // "n_time_type" => $time_type, + // "n_reason" => $reason, + // "n_note" => $note + // ] + // ]); + + $existingNote = Notes::where('n_user_id', $user_id) + ->where('n_day', $day) + ->where('n_month', $month) + ->where('n_year', $year) + ->where('n_reason', $reason) + ->first(); + + if ($existingNote) { + // Cập nhật bản ghi nếu đã tồn tại + $existingNote->update([ + 'n_day' => $day, + 'n_month' => $month, + 'n_year' => $year, + 'n_time_type' => $time_type, + 'n_reason' => $reason, + 'n_note' => $note + ]); + } else { + // Chèn bản ghi mới nếu không tồn tại + Notes::create([ + 'n_user_id' => $user_id, + 'n_day' => $day, + 'n_month' => $month, + 'n_year' => $year, + 'n_time_type' => $time_type, + 'n_reason' => $reason, + 'n_note' => $note + ]); + } $this->createOrUpdateRecordForCurrentMonth($month, $year);