222 lines
7.1 KiB
PHP
222 lines
7.1 KiB
PHP
<?php
|
|
|
|
namespace Modules\Employee\Http\Controllers;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\User;
|
|
use App\Notifications\SendNotification;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Modules\Admin\Repositories\FieldInterface;
|
|
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;
|
|
private $fieldRepository;
|
|
|
|
public function __construct(
|
|
FieldInterface $fieldRepository,
|
|
EmployeeInterface $employeeRepository,
|
|
UserInterface $userRepository,
|
|
AdminService $adminService
|
|
) {
|
|
$this->employeeRepository = $employeeRepository;
|
|
$this->userRepository = $userRepository;
|
|
$this->adminService = $adminService;
|
|
$this->fieldRepository = $fieldRepository;
|
|
}
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index()
|
|
{
|
|
// Display an error toast with no title
|
|
// toastr()->error('Oops! Something went wrong!');
|
|
$data['employees'] = $this->employeeRepository->findAll();
|
|
$data['roleLists'] = Role::pluck('name', 'id');
|
|
return view('employee::index', $data);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*/
|
|
public function create()
|
|
{
|
|
$data['title'] = 'Create Employee';
|
|
$data['departmentList'] = $this->adminService->pluckDepartments();
|
|
$data['designationList'] = $this->adminService->pluckDesignations();
|
|
$data['cityList'] = $this->adminService->pluckCities();
|
|
$data['nationalityList'] = $this->fieldRepository->getDropdownByAlias('nationality');
|
|
$data['genderList'] = $this->fieldRepository->getDropdownByAlias('gender');
|
|
$data['casteList'] = $this->fieldRepository->getDropdownByAlias('caste');
|
|
|
|
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')) {
|
|
$inputData['profile_picture'] = uploadImage($request->profile_picture);
|
|
}
|
|
$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);
|
|
$data['departmentList'] = $this->adminService->pluckDepartments();
|
|
$data['designationList'] = $this->adminService->pluckDesignations();
|
|
$data['nationalityList'] = $this->fieldRepository->getDropdownByAlias('nationality');
|
|
$data['genderList'] = $this->fieldRepository->getDropdownByAlias('gender');
|
|
$data['casteList'] = $this->fieldRepository->getDropdownByAlias('caste');
|
|
$data['cityList'] = $this->adminService->pluckCities();
|
|
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')) {
|
|
$inputData['profile_picture'] = uploadImage($request->profile_picture);
|
|
}
|
|
|
|
$this->employeeRepository->update($id, $inputData);
|
|
|
|
sendNotification(auth()->user(), [
|
|
'msg' => 'Employee Updated',
|
|
]);
|
|
|
|
toastr()->success('Employee Updated 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();
|
|
$employeeModel->status = 10;
|
|
$employeeModel->save();
|
|
|
|
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 {
|
|
$checkUserModel = User::where('email', $request->email)->first();
|
|
if ($checkUserModel) {
|
|
$checkUserModel->roles()->detach();
|
|
$checkUserModel->roles()->attach($request->role_id);
|
|
} else {
|
|
|
|
$employeeModel = $this->employeeRepository->getEmployeeByEmail($request->email);
|
|
|
|
$inputData = [
|
|
'name' => $employeeModel->full_name,
|
|
'email' => $request->email,
|
|
'password' => Hash::make('password'),
|
|
'email_verified_at' => Carbon::now(),
|
|
'employee_id' => $employeeModel->id,
|
|
];
|
|
|
|
$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)
|
|
{
|
|
try {
|
|
$employeemodel = $this->employeeRepository->getEmployeeById($request->employee_id);
|
|
$inputData = [
|
|
'password' => Hash::make($request->password),
|
|
];
|
|
$employeemodel->user->update($inputData);
|
|
|
|
sendNotification($employeemodel->user, [
|
|
'msg' => 'Your Password has been changed'
|
|
]);
|
|
toastr()->success('Password changed Successfully');
|
|
|
|
} catch (\Throwable $th) {
|
|
toastr()->error($th->getMessage());
|
|
}
|
|
return redirect()->route('employee.index');
|
|
}
|
|
|
|
public function getEmployeeById(Request $request)
|
|
{
|
|
return $this->employeeRepository->getEmployeeById($request->employeeId);
|
|
}
|
|
|
|
}
|