New-OMIS/Modules/Employee/app/Http/Controllers/EmployeeController.php

189 lines
5.5 KiB
PHP
Raw Permalink Normal View History

2024-04-04 12:19:56 +00:00
<?php
namespace Modules\Employee\Http\Controllers;
use App\Http\Controllers\Controller;
2024-04-10 09:30:24 +00:00
use Carbon\Carbon;
2024-04-04 12:19:56 +00:00
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
2024-04-10 09:30:24 +00:00
use Illuminate\Support\Facades\Hash;
2024-04-11 10:59:09 +00:00
use Illuminate\Support\Facades\Storage;
use Modules\Admin\Services\AdminService;
2024-04-07 11:54:18 +00:00
use Modules\Employee\Repositories\EmployeeInterface;
2024-04-10 09:30:24 +00:00
use Modules\User\Repositories\UserInterface;
2024-04-10 12:21:18 +00:00
use Spatie\Permission\Models\Role;
2024-04-04 12:19:56 +00:00
class EmployeeController extends Controller
{
2024-04-07 11:54:18 +00:00
2024-04-10 09:30:24 +00:00
private $employeeRepository;
private $userRepository;
2024-04-07 11:54:18 +00:00
2024-04-11 10:59:09 +00:00
private $adminService;
public function __construct(EmployeeInterface $employeeRepository, UserInterface $userRepository, AdminService $adminService)
2024-04-07 11:54:18 +00:00
{
$this->employeeRepository = $employeeRepository;
2024-04-10 09:30:24 +00:00
$this->userRepository = $userRepository;
2024-04-11 10:59:09 +00:00
$this->adminService = $adminService;
2024-04-10 09:30:24 +00:00
2024-04-07 11:54:18 +00:00
}
2024-04-04 12:19:56 +00:00
/**
* Display a listing of the resource.
*/
public function index()
{
2024-04-07 11:54:18 +00:00
$data['employees'] = $this->employeeRepository->findAll();
2024-04-10 09:30:24 +00:00
$data['roleLists'] = Role::pluck('name', 'id');
// dd($data['employees']->toArray());
2024-04-07 11:54:18 +00:00
return view('employee::index', $data);
2024-04-04 12:19:56 +00:00
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
2024-04-07 07:58:06 +00:00
$data['title'] = 'Create Employee';
2024-04-10 09:30:24 +00:00
$data['departmentList'] = [];
$data['designationList'] = [];
2024-04-11 10:59:09 +00:00
$data['nationalityList'] = $this->adminService->pluckNationalities();
$data['genderList'] = $this->adminService->pluckGenders();
$data['casteList'] = $this->adminService->pluckCastes();
$data['cityList'] = $this->adminService->pluckCities();
2024-04-10 09:30:24 +00:00
2024-04-07 07:58:06 +00:00
return view('employee::create', $data);
2024-04-04 12:19:56 +00:00
}
/**
* Store a newly created resource in storage.
*/
2024-04-07 11:54:18 +00:00
public function store(Request $request)
2024-04-04 12:19:56 +00:00
{
2024-04-10 09:30:24 +00:00
$inputData = $request->all();
try {
2024-04-11 10:59:09 +00:00
if ($request->hasFile('profile_picture')) {
$image = $request->profile_picture;
$fileName = time() . '_' . $image->getClientOriginalName();
$filePath = Storage::disk('public')->putFileAs('uploads', $image, $fileName);
$inputData['profile_picture'] = 'storage/' . $filePath;
2024-04-10 09:30:24 +00:00
}
$this->employeeRepository->create($inputData);
2024-04-10 12:21:18 +00:00
2024-04-10 09:30:24 +00:00
toastr()->success('Employee Created Succesfully');
2024-04-10 12:21:18 +00:00
2024-04-10 09:30:24 +00:00
} catch (\Throwable $th) {
2024-04-10 12:21:18 +00:00
echo $th->getMessage();
2024-04-10 09:30:24 +00:00
toastr()->error($th->getMessage());
2024-04-07 11:54:18 +00:00
}
return redirect()->route('employee.index');
2024-04-04 12:19:56 +00:00
}
/**
* Show the specified resource.
*/
public function show($id)
{
2024-04-10 09:30:24 +00:00
$data['employee'] = $this->employeeRepository->getEmployeeById($id);
return view('employee::show', $data);
2024-04-04 12:19:56 +00:00
}
/**
* Show the form for editing the specified resource.
*/
public function edit($id)
{
2024-04-07 11:54:18 +00:00
$data['title'] = 'Edit Employee';
$data['employee'] = $this->employeeRepository->getEmployeeById($id);
return view('employee::edit', $data);
2024-04-04 12:19:56 +00:00
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, $id): RedirectResponse
{
2024-04-10 09:30:24 +00:00
$inputData = $request->except(['_method', '_token']);
2024-04-11 10:59:09 +00:00
2024-04-10 09:30:24 +00:00
try {
2024-04-11 10:59:09 +00:00
if ($request->hasFile('profile_picture')) {
$image = $request->profile_picture;
$fileName = time() . '_' . $image->getClientOriginalName();
$filePath = Storage::disk('public')->putFileAs('uploads', $image, $fileName);
$inputData['profile_picture'] = 'storage/' . $filePath;
2024-04-10 09:30:24 +00:00
}
$this->employeeRepository->update($id, $inputData);
2024-04-11 10:59:09 +00:00
2024-04-10 09:30:24 +00:00
toastr()->success('Employee Created Succesfully');
2024-04-11 10:59:09 +00:00
2024-04-10 09:30:24 +00:00
} catch (\Throwable $th) {
2024-04-11 10:59:09 +00:00
2024-04-10 09:30:24 +00:00
toastr()->error($th->getMessage());
2024-04-07 11:54:18 +00:00
}
return redirect()->route('employee.index');
2024-04-04 12:19:56 +00:00
}
/**
* Remove the specified resource from storage.
*/
2024-04-10 12:21:18 +00:00
public function destroy(Request $request)
2024-04-04 12:19:56 +00:00
{
2024-04-10 09:30:24 +00:00
try {
2024-04-10 12:21:18 +00:00
$employeeModel = $this->employeeRepository->getEmployeeById($request->id);
optional($employeeModel)->user?->roles()?->detach();
optional($employeeModel)->user?->delete();
optional($employeeModel)->delete();
2024-04-10 09:30:24 +00:00
toastr()->success('Employee Delete Succesfully');
} catch (\Throwable $th) {
toastr()->error($th->getMessage());
}
2024-04-10 12:21:18 +00:00
return response()->json(['status' => true, 'message' => 'Employee Delete Succesfully']);
2024-04-10 09:30:24 +00:00
}
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');
2024-04-04 12:19:56 +00:00
}
2024-04-10 09:30:24 +00:00
2024-04-10 12:21:18 +00:00
public function changePassword(Request $request)
{
dd($request->all());
try {
} catch (\Throwable $th) {
toastr()->error($th->getMessage());
}
}
2024-04-04 12:19:56 +00:00
}