From 6724528054c7c69c1db46f8f8beae477b1b6d7cb Mon Sep 17 00:00:00 2001 From: dvappnnt Date: Fri, 24 Apr 2026 15:28:08 +0800 Subject: [PATCH] Fix employee creation logic and consistency --- app/Http/Controllers/EmployeeController.php | 25 ++++++++++++++------- app/Models/Employee.php | 10 ++++++++- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/app/Http/Controllers/EmployeeController.php b/app/Http/Controllers/EmployeeController.php index 05ad1ce15..fe4df45b0 100644 --- a/app/Http/Controllers/EmployeeController.php +++ b/app/Http/Controllers/EmployeeController.php @@ -218,9 +218,10 @@ class EmployeeController extends Controller 'phone' => 'required|string|max:20', 'date_of_birth' => 'required|date', 'gender' => 'required|in:male,female,other', - 'profile_image' => 'required|image|mimes:jpeg,png,jpg,gif|max:2048', + 'profile_image' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:2048', 'shift_id' => 'nullable|exists:shifts,id', 'attendance_policy_id' => 'nullable|exists:attendance_policies,id', + 'salary' => 'required|numeric|min:0', // Employment details 'branch_id' => 'required|exists:branches,id', @@ -281,6 +282,17 @@ class EmployeeController extends Controller return redirect()->back()->withErrors($validator)->withInput(); } + // Plan limit check + if (isSaas()) { + $creator = User::find(creatorId()); + if ($creator && $creator->type === 'company' && $creator->plan) { + $count = User::where('type', 'employee')->whereIn('created_by', getCompanyAndUsersId())->count(); + if ($count >= $creator->plan->max_employees) { + return redirect()->back()->with('error', __('Your plan limit for employees has been reached.'))->withInput(); + } + } + } + \DB::beginTransaction(); // Create User model object @@ -356,10 +368,6 @@ class EmployeeController extends Controller $employee->created_by = creatorId(); $employee->save(); - if (!$employee->save()) { - throw new \Exception('Failed to save employee data'); - } - // Handle document uploads if ($request->has('documents') && is_array($request->documents)) { foreach ($request->documents as $index => $document) { @@ -533,6 +541,7 @@ class EmployeeController extends Controller 'profile_image' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:2048', 'shift_id' => 'nullable|exists:shifts,id', 'attendance_policy_id' => 'nullable|exists:attendance_policies,id', + 'salary' => 'required|numeric|min:0', // Employment details 'branch_id' => 'required|exists:branches,id', @@ -553,9 +562,9 @@ class EmployeeController extends Controller 'emergency_contact_number' => 'required|string|max:20', // Banking information - 'bank_name' => 'required|string|max:255', - 'account_holder_name' => 'required|string|max:255', - 'account_number' => 'required|string|max:50', + 'bank_name' => 'nullable|string|max:255', + 'account_holder_name' => 'nullable|string|max:255', + 'account_number' => 'nullable|string|max:50', 'bank_identifier_code' => 'nullable|string|max:50', 'bank_branch' => 'nullable|string|max:255', 'tax_payer_id' => 'nullable|string|max:50', diff --git a/app/Models/Employee.php b/app/Models/Employee.php index 725c09534..048311364 100644 --- a/app/Models/Employee.php +++ b/app/Models/Employee.php @@ -124,6 +124,14 @@ class Employee extends Model $lastEmployee = self::orderBy('id', 'desc')->first(); $nextId = $lastEmployee ? $lastEmployee->id + 1 : 1; - return 'EMP' . str_pad($nextId, 6, '0', STR_PAD_LEFT); + $employeeId = 'EMP' . str_pad($nextId, 6, '0', STR_PAD_LEFT); + + // Ensure uniqueness + while (self::where('employee_id', $employeeId)->exists()) { + $nextId++; + $employeeId = 'EMP' . str_pad($nextId, 6, '0', STR_PAD_LEFT); + } + + return $employeeId; } } \ No newline at end of file