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

177 lines
5.2 KiB
PHP
Raw 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-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-10 09:30:24 +00:00
public function __construct(EmployeeInterface $employeeRepository, UserInterface $userRepository)
2024-04-07 11:54:18 +00:00
{
$this->employeeRepository = $employeeRepository;
2024-04-10 09:30:24 +00:00
$this->userRepository = $userRepository;
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'] = [];
$data['genderList'] = [];
$data['nationalityList'] = [];
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 12:21:18 +00:00
dd($request->all());
2024-04-10 09:30:24 +00:00
$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();
}
$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']);
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();
}
$this->employeeRepository->update($id, $inputData);
toastr()->success('Employee Created Succesfully');
} catch (\Throwable $th) {
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
}