employee = $employee; $this->userService = $userService; $this->designationService = $designationService; $this->departmentService = $departmentService; } /** * Display a listing of the resource. */ public function index(Request $request) { $data['title'] = "Employee List"; if ($request->ajax()) { $data['employees'] = $this->employee->findAll(request: $request, query: function ($query) { $query->where('status', "!=", "terminated")->with('user')->latest(); }, paginate:true, limit:12); $view = view('employee::employee.partials.employee-list', $data)->render(); return response()->json(['status', 200, 'html' => $view], 200); } return view('employee::employee.index', $data); } /** * Show the form for creating a new resource. */ public function create() { $data['title'] = 'Create Employee'; $data['editable'] = false; $data['genderOptions'] = config("constants.gender_options"); $data['statusOptions'] = config("constants.employee_status_options"); $data['branchOptions'] = Branch::pluck('title', 'id'); $data['departmentOptions'] = $this->departmentService->pluck(); $data['designationOptions'] = $this->designationService->pluck(); return view('employee::employee.create', $data); } /** * Store a newly created resource in storage. */ public function store(Request $request) { $validatedData = $request->validate([ 'mobile' => [ 'required', 'regex:/^(98|97)[0-9]{8}$/', ], 'photo' => 'nullable', 'email' => 'required|email|unique:employees,email', ], [ 'mobile.required' => 'Mobile number is required.', 'mobile.regex' => 'Mobile number must start with 98 or 97 and be 10 digits long.', 'email.required' => 'Email is required.', 'email.email' => 'Invalid email format.', 'email.unique' => 'The email has already been taken.', ]); $request->merge([ 'employee_code' => 'EMP' . mt_rand(1000, 9999), ]); try { DB::transaction(function () use ($validatedData, $request) { $employeeInput = $request->only(Employee::getFillableFields()); $userInput = $request->only(User::getFillableFields()); $employee = $this->employee->create($employeeInput); $userInput['name'] = $employee->full_name; $userInput['order'] = ($maxOrder = User::max('order')) !== null ? ++$maxOrder : 1; $user = $this->userService->storeUser($userInput); $employee->update(['user_id' => $user->id]); }); flash()->success('Employee has been created successfully!'); return redirect()->route('employee.index'); } catch (\Exception $e) { flash()->error($e->getMessage()); return redirect()->back()->withInput(); } } /** * Show the specified resource. */ public function show($id) { $data['employee'] = $this->employee->findById($id); // $data['leaves'] = $this->leave->getLeaveByEmployeeId(id: $id); // $data['leaveTypeList'] = $this->leaveType->pluck(); // $data['status'] = Leave::PROGRESS_STATUS; // $data['duration'] = Leave::DURATION; // $data['attends'] = $this->attendance->getAttendanceByEmployeeId(id: $id); // $data['roleList'] = $this->role->pluck(); // $data['desgination'] = $data['employee']->designation->name; // $data['department'] = $data['employee']->department_id ? optional($this->dropDown->getDropdownById($data['employee']->department_id))->title : null; // $data['leaveBalance'] = $this->leaveBalance->where([ // 'employee_id' => $id, // ])->with(['leaveType:leave_type_id,name'])->get() // ->transform(function ($leaveBalance) { // return [ // 'leave_type_id' => $leaveBalance->leave_type_id, // 'leave_type' => $leaveBalance->leaveType?->name, // 'total' => $leaveBalance->total, // 'remain' => $leaveBalance->remain, // ]; // }); return view('employee::show', $data); } /**ss * Show the form for editing the specified resource. */ public function edit($id) { $data['title'] = 'Edit Employee'; $data['editable'] = true; $data['genderOptions'] = config("constants.gender_options"); $data['statusOptions'] = config("constants.employee_status_options"); $data['branchOptions'] = Branch::pluck('title', 'id'); $data['departmentOptions'] = $this->departmentService->pluck(); $data['designationOptions'] = $this->designationService->pluck(); $data['employee'] = $this->employee->findById($id); return view('employee::employee.edit', $data); } /** * Update the specified resource in storage. */ public function update(Request $request, $id) { $validated = $request->validate([ 'mobile' => [ 'required', 'regex:/^(98|97)[0-9]{8}$/', ], 'email' => 'required|email|unique:employees,email,' . $id, ], [ 'contact.required' => 'The contact number is required.', 'contact.regex' => 'The contact number must start with 98 or 97 and be 10 digits long.', 'email.required' => 'The email is required.', 'email.email' => 'The email must be a valid email address.', 'email.unique' => 'The email has already been taken.', ]); try { DB::transaction(function () use ($request, $id) { $input = $request->only(Employee::getFillableFields()); $employee = $this->employee->update($id, $input); $userData = [ 'name' => $employee->full_name, 'email' => $employee->email, ]; if ($employee->isDirty("email")) { $userData["can_login"] = false; } $employee->user->update($userData); }); flash()->success('Employee has been updated!'); return redirect()->route('employee.index'); } catch (\Throwable $th) { flash()->error($th->getMessage()); return redirect()->back()->withInput(); } } /** * Remove the specified resource from storage. */ public function destroy($id) { try { DB::transaction(function () use ($id) { $employee = $this->employee->delete($id); $employee->user->delete(); }); return response()->json([ 'status' => 200, 'message' => 'Employee has been deleted!', ], 200); } catch (\Throwable $th) { return response()->json([ 'status' => 500, 'message' => 'Failed to delete employee!', 'error' => $th->getMessage(), ], 500); } } public function terminate($id) { try { DB::transaction(function () use ($id) { $employee = $this->employee->terminate($id); $employee->user->update([ "can_login" => false, ]); }); return response()->json([ 'status' => 200, 'message' => 'Employee has been terminate!', ], 200); } catch (\Throwable $th) { return response()->json([ 'status' => 500, 'message' => 'Failed to terminate employee!', 'error' => $th->getMessage(), ], 500); } } }