From 66aa72874561f59b0bfcad3adf6f73f869792090 Mon Sep 17 00:00:00 2001 From: jouel88 Date: Fri, 20 Mar 2026 17:02:25 +0700 Subject: [PATCH] ID User --- app/Http/Controllers/PegawaiController.php | 55 ++++++++++++++++++++++ app/Http/Requests/StorePegawaiRequest.php | 1 - resources/views/pegawai/create.blade.php | 4 +- resources/views/pegawai/edit.blade.php | 4 +- 4 files changed, 59 insertions(+), 5 deletions(-) diff --git a/app/Http/Controllers/PegawaiController.php b/app/Http/Controllers/PegawaiController.php index e3b0b9aa..8ab6bc17 100644 --- a/app/Http/Controllers/PegawaiController.php +++ b/app/Http/Controllers/PegawaiController.php @@ -70,6 +70,57 @@ public function index(Request $request) return view('pegawai.index', compact('pegawai', 'stats', 'allJabatan', 'allKantor')); } + private function generateIdKaryawan($tglBergabung, $idDivisi, $idJabatan) + { + // 1. Ambil Tahun & Bulan (YYYYMM) + $date = \Carbon\Carbon::parse($tglBergabung); + $yearMonth = $date->format('Ym'); + + // 2. Ambil 2 huruf dari Divisi & Jabatan (XXYY) + $divisi = Divisi::find($idDivisi); + $jabatan = Jabatan::find($idJabatan); + + $kodeDivisi = 'XX'; + if ($divisi) { + $words = explode(' ', $divisi->nama_divisi); + if (count($words) >= 2) { + $kodeDivisi = strtoupper(substr($words[0], 0, 1) . substr($words[1], 0, 1)); + } else { + $kodeDivisi = strtoupper(substr($divisi->nama_divisi, 0, 2)); + } + } + + $kodeJabatan = 'YY'; + if ($jabatan) { + $words = explode(' ', $jabatan->nama_jabatan); + if (count($words) >= 2) { + $kodeJabatan = strtoupper(substr($words[0], 0, 1) . substr($words[1], 0, 1)); + } else { + $kodeJabatan = strtoupper(substr($jabatan->nama_jabatan, 0, 2)); + } + } + $kodeTengah = $kodeDivisi . $kodeJabatan; + + // 3. Generate Nomor Urut (NNN) per tahun bergabun + $yearStarts = $date->copy()->startOfYear()->toDateString(); + $yearEnds = $date->copy()->endOfYear()->toDateString(); + + $lastUser = User::whereBetween('tgl_bergabung', [$yearStarts, $yearEnds]) + ->whereNotNull('nik') + ->orderBy('id', 'desc') + ->first(); + + $nextNumber = 1; + if ($lastUser && preg_match('/-(\d{3})$/', $lastUser->nik, $matches)) { + $nextNumber = intval($matches[1]) + 1; + } + + $nomorUrut = str_pad($nextNumber, 3, '0', STR_PAD_LEFT); + + // Format: MPG-202603-ITST-001 + return "MPG-{$yearMonth}-{$kodeTengah}-{$nomorUrut}"; + } + public function store(StorePegawaiRequest $request) { $data = $request->validated(); @@ -78,6 +129,10 @@ public function store(StorePegawaiRequest $request) $data['foto'] = $request->file('foto')->store('foto-profil', 'public'); } + // Generate ID Karyawan Otomatis + $tglBergabung = $data['tgl_bergabung'] ?? now()->toDateString(); + $data['nik'] = $this->generateIdKaryawan($tglBergabung, $data['id_divisi'], $data['id_jabatan']); + $data['password'] = Hash::make('Mpg123!'); $data['status_aktif'] = 1; $data['sisa_cuti'] = 12; diff --git a/app/Http/Requests/StorePegawaiRequest.php b/app/Http/Requests/StorePegawaiRequest.php index 056fd57e..980e8902 100644 --- a/app/Http/Requests/StorePegawaiRequest.php +++ b/app/Http/Requests/StorePegawaiRequest.php @@ -14,7 +14,6 @@ public function authorize(): bool public function rules(): array { return [ - 'nik' => ['required', 'numeric', 'unique:users,nik'], 'nama_lengkap' => ['required', 'string', 'max:255'], 'email' => ['required', 'email', 'unique:users,email'], 'no_telp' => ['nullable', 'string', 'max:20'], diff --git a/resources/views/pegawai/create.blade.php b/resources/views/pegawai/create.blade.php index d1c8bca1..f293b15c 100644 --- a/resources/views/pegawai/create.blade.php +++ b/resources/views/pegawai/create.blade.php @@ -22,8 +22,8 @@ class="grid grid-cols-1 lg:grid-cols-3 gap-6">
- +
diff --git a/resources/views/pegawai/edit.blade.php b/resources/views/pegawai/edit.blade.php index df8fb76c..79ba8b46 100644 --- a/resources/views/pegawai/edit.blade.php +++ b/resources/views/pegawai/edit.blade.php @@ -22,8 +22,8 @@ class="grid grid-cols-1 lg:grid-cols-3 gap-6">

Informasi Pribadi

- +