From b5c603ceec69414b65598f7bf1da4d23c846f9bd Mon Sep 17 00:00:00 2001 From: Ranjan Date: Wed, 10 Apr 2024 15:15:24 +0545 Subject: [PATCH] employee crud --- .../Http/Controllers/EmployeeController.php | 109 ++++++--- Modules/Employee/app/Models/Employee.php | 5 + .../app/Repositories/EmployeeInterface.php | 1 + .../app/Repositories/EmployeeRepository.php | 7 +- ...24_04_07_093523_create_employees_table.php | 27 +-- .../Employee/resources/views/index.blade.php | 68 ++++-- .../resources/views/partials/action.blade.php | 215 ++++++++---------- .../views/partials/action.blade.php.bak | 128 +++++++++++ Modules/Employee/routes/web.php | 8 +- .../User/app/Repositories/UserRepository.php | 3 +- app/Models/User.php | 8 +- app/Service/DepartmentService.php | 33 +++ public/assets/js/custom.js | 2 +- 13 files changed, 420 insertions(+), 194 deletions(-) create mode 100644 Modules/Employee/resources/views/partials/action.blade.php.bak create mode 100644 app/Service/DepartmentService.php diff --git a/Modules/Employee/app/Http/Controllers/EmployeeController.php b/Modules/Employee/app/Http/Controllers/EmployeeController.php index 6bf2ae0..8d9da57 100644 --- a/Modules/Employee/app/Http/Controllers/EmployeeController.php +++ b/Modules/Employee/app/Http/Controllers/EmployeeController.php @@ -3,18 +3,25 @@ namespace Modules\Employee\Http\Controllers; use App\Http\Controllers\Controller; +use App\Models\Role; +use Carbon\Carbon; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; +use Illuminate\Support\Facades\Hash; use Modules\Employee\Repositories\EmployeeInterface; +use Modules\User\Repositories\UserInterface; class EmployeeController extends Controller { - private EmployeeInterface $employeeRepository; + private $employeeRepository; + private $userRepository; - public function __construct(EmployeeInterface $employeeRepository) + public function __construct(EmployeeInterface $employeeRepository, UserInterface $userRepository) { $this->employeeRepository = $employeeRepository; + $this->userRepository = $userRepository; + } /** * Display a listing of the resource. @@ -22,6 +29,8 @@ class EmployeeController extends Controller public function index() { $data['employees'] = $this->employeeRepository->findAll(); + $data['roleLists'] = Role::pluck('name', 'id'); + // dd($data['employees']->toArray()); return view('employee::index', $data); } @@ -31,6 +40,11 @@ class EmployeeController extends Controller public function create() { $data['title'] = 'Create Employee'; + $data['departmentList'] = []; + $data['designationList'] = []; + $data['genderList'] = []; + $data['nationalityList'] = []; + return view('employee::create', $data); } @@ -39,22 +53,20 @@ class EmployeeController extends Controller */ public function store(Request $request) { - $inputData = $request->only(['first_name', 'last_name']); - // try { + $inputData = $request->all(); + try { - if ($request->hasFile('profile_pic')) { - $fileName = time() . '_' . $request->profile_pic->getClientOriginalName(); - $filePath = $request->file('profile_pic')->storeAs('uploads', $fileName, 'public'); - $inputData['profile_picture'] = time() . '_' . $request->profile_pic->getClientOriginalName(); - // $fileModel->file_path = '/storage/' . $filePath; - // $fileModel->save(); + if ($request->hasFile('profile_pic')) { + $fileName = time() . '_' . $request->profile_pic->getClientOriginalName(); + $filePath = $request->file('profile_pic')->storeAs('uploads', $fileName, 'public'); + $inputData['profile_picture'] = time() . '_' . $request->profile_pic->getClientOriginalName(); + } + + $this->employeeRepository->create($inputData); + toastr()->success('Employee Created Succesfully'); + } catch (\Throwable $th) { + toastr()->error($th->getMessage()); } - - $this->employeeRepository->create($inputData); - toastr()->success('Employee Created Succesfully'); - // } catch (\Throwable $th) { - // toastr()->error($th->getMessage()); - // } return redirect()->route('employee.index'); } @@ -63,8 +75,8 @@ class EmployeeController extends Controller */ public function show($id) { - - return view('employee::show'); + $data['employee'] = $this->employeeRepository->getEmployeeById($id); + return view('employee::show', $data); } /** @@ -82,20 +94,20 @@ class EmployeeController extends Controller */ public function update(Request $request, $id): RedirectResponse { - $inputData = $request->only(['first_name', 'last_name']); - // try { + $inputData = $request->except(['_method', '_token']); + try { - if ($request->hasFile('profile_pic')) { - $fileName = time() . '_' . $request->profile_pic->getClientOriginalName(); - $filePath = $request->file('profile_pic')->storeAs('uploads', $fileName, 'public'); - $inputData['profile_picture'] = time() . '_' . $request->profile_pic->getClientOriginalName(); + if ($request->hasFile('profile_pic')) { + $fileName = time() . '_' . $request->profile_pic->getClientOriginalName(); + $filePath = $request->file('profile_pic')->storeAs('uploads', $fileName, 'public'); + $inputData['profile_picture'] = time() . '_' . $request->profile_pic->getClientOriginalName(); + } + + $this->employeeRepository->update($id, $inputData); + toastr()->success('Employee Created Succesfully'); + } catch (\Throwable $th) { + toastr()->error($th->getMessage()); } - - $this->employeeRepository->update($id, $inputData); - toastr()->success('Employee Created Succesfully'); - // } catch (\Throwable $th) { - // toastr()->error($th->getMessage()); - // } return redirect()->route('employee.index'); } @@ -104,6 +116,41 @@ class EmployeeController extends Controller */ public function destroy($id) { - // + try { + $employeeModel = $this->employeeRepository->getEmployeeById($id); + $employeeModel->user->roles()->detach(); + $employeeModel->user->delete(); + $employeeModel->delete(); + toastr()->success('Employee Delete Succesfully'); + + } catch (\Throwable $th) { + toastr()->error($th->getMessage()); + } + return redirect()->route('employee.index'); + } + + public function assignRole(Request $request) + { + try { + $employeeModel = $this->employeeRepository->getEmployeeByEmail($request->email); + + $inputData = [ + 'name' => $employeeModel->first_name . ' ' . $employeeModel->last_name, + 'email' => $request->email, + 'password' => Hash::make('password'), + 'email_verified_at' => Carbon::now(), + ]; + $userModel = $this->userRepository->create($inputData, [$request->role_id]); + + $employeeModel->users_id = $userModel->id; + $employeeModel->save(); + + toastr()->success('Role Assigned Succesfully'); + } catch (\Throwable $th) { + toastr()->error($th->getMessage()); + } + return redirect()->route('employee.index'); + } + } diff --git a/Modules/Employee/app/Models/Employee.php b/Modules/Employee/app/Models/Employee.php index 40dfe5f..71d31d3 100644 --- a/Modules/Employee/app/Models/Employee.php +++ b/Modules/Employee/app/Models/Employee.php @@ -2,6 +2,7 @@ namespace Modules\Employee\Models; +use App\Models\User; use Illuminate\Database\Eloquent\Model; class Employee extends Model @@ -10,4 +11,8 @@ class Employee extends Model protected $primaryKey = 'id'; protected $guarded = []; + public function user() + { + return $this->belongsTo(User::class, 'users_id'); + } } diff --git a/Modules/Employee/app/Repositories/EmployeeInterface.php b/Modules/Employee/app/Repositories/EmployeeInterface.php index 274637e..e31844d 100644 --- a/Modules/Employee/app/Repositories/EmployeeInterface.php +++ b/Modules/Employee/app/Repositories/EmployeeInterface.php @@ -6,6 +6,7 @@ interface EmployeeInterface { public function findAll(); public function getEmployeeById($employeeId); + public function getEmployeeByEmail($email); public function delete($employeeId); public function create($EmployeeDetails); public function update($employeeId, array $newDetails); diff --git a/Modules/Employee/app/Repositories/EmployeeRepository.php b/Modules/Employee/app/Repositories/EmployeeRepository.php index 01b1bb4..ee32a5e 100644 --- a/Modules/Employee/app/Repositories/EmployeeRepository.php +++ b/Modules/Employee/app/Repositories/EmployeeRepository.php @@ -8,7 +8,7 @@ class EmployeeRepository implements EmployeeInterface { public function findAll() { - return Employee::paginate(20); + return Employee::with('user.roles')->paginate(20); } public function getEmployeeById($employeeId) @@ -16,6 +16,11 @@ class EmployeeRepository implements EmployeeInterface return Employee::findOrFail($employeeId); } + public function getEmployeeByEmail($email) + { + return Employee::where('email', $email)->first(); + } + public function delete($employeeId) { Employee::destroy($employeeId); diff --git a/Modules/Employee/database/migrations/2024_04_07_093523_create_employees_table.php b/Modules/Employee/database/migrations/2024_04_07_093523_create_employees_table.php index e2a9103..527270e 100644 --- a/Modules/Employee/database/migrations/2024_04_07_093523_create_employees_table.php +++ b/Modules/Employee/database/migrations/2024_04_07_093523_create_employees_table.php @@ -17,36 +17,23 @@ return new class extends Migration $table->string('middle_name')->nullable(); $table->string('last_name')->nullable(); $table->string('email')->nullable(); - $table->unsignedBigInteger('genders_id')->nullable(); $table->date('nepali_dob')->nullable(); $table->date('dob')->nullable(); - $table->unsignedBigInteger('nationalities_id')->nullable(); - $table->text('about_me')->nullable(); $table->string('signature')->nullable(); $table->string('father_name')->nullable(); - $table->string('mother_name')->nullable(); - $table->string('grand_father_name')->nullable(); - $table->string('grand_mother_name')->nullable(); - $table->string('spouse')->nullable(); $table->string('contact')->nullable(); - $table->string('alt_contact')->nullable(); $table->string('profile_picture')->nullable(); + $table->unsignedBigInteger('genders_id')->nullable(); + $table->unsignedBigInteger('nationalities_id')->nullable(); $table->unsignedBigInteger('users_id')->nullable(); - $table->text('skills')->nullable(); - $table->text('experience')->nullable(); + $table->integer('is_user_assigned')->nullable()->default(10); + $table->unsignedBigInteger('organization_id')->nullable(); + $table->unsignedBigInteger('department_id')->nullable(); + $table->unsignedBigInteger('designation_id')->nullable(); $table->text('permanent_address')->nullable(); - $table->unsignedBigInteger('permanent_city')->nullable(); $table->text('temporary_address')->nullable(); - $table->unsignedBigInteger('temporary_city')->nullable(); - $table->text('old_system_address')->nullable(); - $table->text('education')->nullable(); - $table->unsignedBigInteger('castes_id')->nullable(); - $table->unsignedBigInteger('ethnicities_id')->nullable(); - $table->unsignedBigInteger('dags_id')->nullable(); - $table->string('status')->nullable(); + $table->string('status')->nullable()->default(11); $table->string('remarks')->nullable(); - $table->unsignedBigInteger('createdby')->nullable(); - $table->unsignedBigInteger('updatedby')->nullable(); $table->timestamps(); }); } diff --git a/Modules/Employee/resources/views/index.blade.php b/Modules/Employee/resources/views/index.blade.php index ecca651..adb94d1 100644 --- a/Modules/Employee/resources/views/index.blade.php +++ b/Modules/Employee/resources/views/index.blade.php @@ -45,8 +45,28 @@
-
Active
+ @if ($employee->user) +
+ {{ optional($employee->user)->getRoleNames()->first() }} +
+ @endif
+
+
+
+
+ +
+
+
  • @@ -124,29 +145,31 @@
  • -