New-OMIS/Modules/Employee/app/Http/Controllers/EmployeeController.php
2024-04-11 16:44:09 +05:45

189 lines
5.5 KiB
PHP

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