firstcommit

This commit is contained in:
2024-05-16 09:31:08 +05:45
commit 34d9672cb8
1396 changed files with 86482 additions and 0 deletions

View File

@ -0,0 +1,210 @@
<?php
namespace Modules\Employee\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
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()
{
$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);
// sendNotification(auth()->user(), [
// 'msg' => 'Employee Created',
// ]);
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');
}
}

View File

View File

@ -0,0 +1,44 @@
<?php
namespace Modules\Employee\Models;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Modules\Admin\Models\Department;
use Modules\Admin\Models\Designation;
class Employee extends Model
{
use Notifiable;
protected $table = 'tbl_employees';
protected $guarded = [];
protected $appends = ['full_name', 'profile_pic'];
protected function getFullNameAttribute()
{
// return $this->first_name . ' ' . $this->middle_name . ' ' . $this->last_name;
return $this->first_name . ' ' . $this->last_name;
}
protected function getProfilePicAttribute()
{
return $this->profile_picture ? asset('storage/' . $this->profile_picture) : asset('assets/images/task.png');
}
public function user()
{
return $this->belongsTo(User::class, 'users_id');
}
public function department()
{
return $this->belongsTo(Department::class, 'department_id')->withDefault();
}
public function designation()
{
return $this->HasOne(Designation::class, 'designation_id')->withDefault();
}
}

View File

View File

@ -0,0 +1,117 @@
<?php
namespace Modules\Employee\Providers;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;
use Modules\Employee\Repositories\EmployeeInterface;
use Modules\Employee\Repositories\EmployeeRepository;
class EmployeeServiceProvider extends ServiceProvider
{
protected string $moduleName = 'Employee';
protected string $moduleNameLower = 'employee';
/**
* Boot the application events.
*/
public function boot(): void
{
$this->registerCommands();
$this->registerCommandSchedules();
$this->registerTranslations();
$this->registerConfig();
$this->registerViews();
$this->loadMigrationsFrom(module_path($this->moduleName, 'database/migrations'));
}
/**
* Register the service provider.
*/
public function register(): void
{
$this->app->bind(EmployeeInterface::class, EmployeeRepository::class);
$this->app->register(RouteServiceProvider::class);
}
/**
* Register commands in the format of Command::class
*/
protected function registerCommands(): void
{
// $this->commands([]);
}
/**
* Register command Schedules.
*/
protected function registerCommandSchedules(): void
{
// $this->app->booted(function () {
// $schedule = $this->app->make(Schedule::class);
// $schedule->command('inspire')->hourly();
// });
}
/**
* Register translations.
*/
public function registerTranslations(): void
{
$langPath = resource_path('lang/modules/' . $this->moduleNameLower);
if (is_dir($langPath)) {
$this->loadTranslationsFrom($langPath, $this->moduleNameLower);
$this->loadJsonTranslationsFrom($langPath);
} else {
$this->loadTranslationsFrom(module_path($this->moduleName, 'lang'), $this->moduleNameLower);
$this->loadJsonTranslationsFrom(module_path($this->moduleName, 'lang'));
}
}
/**
* Register config.
*/
protected function registerConfig(): void
{
$this->publishes([module_path($this->moduleName, 'config/config.php') => config_path($this->moduleNameLower . '.php')], 'config');
$this->mergeConfigFrom(module_path($this->moduleName, 'config/config.php'), $this->moduleNameLower);
}
/**
* Register views.
*/
public function registerViews(): void
{
$viewPath = resource_path('views/modules/' . $this->moduleNameLower);
$sourcePath = module_path($this->moduleName, 'resources/views');
$this->publishes([$sourcePath => $viewPath], ['views', $this->moduleNameLower . '-module-views']);
$this->loadViewsFrom(array_merge($this->getPublishableViewPaths(), [$sourcePath]), $this->moduleNameLower);
$componentNamespace = str_replace('/', '\\', config('modules.namespace') . '\\' . $this->moduleName . '\\' . ltrim(config('modules.paths.generator.component-class.path'), config('modules.paths.app_folder', '')));
Blade::componentNamespace($componentNamespace, $this->moduleNameLower);
}
/**
* Get the services provided by the provider.
*/
public function provides(): array
{
return [];
}
private function getPublishableViewPaths(): array
{
$paths = [];
foreach (config('view.paths') as $path) {
if (is_dir($path . '/modules/' . $this->moduleNameLower)) {
$paths[] = $path . '/modules/' . $this->moduleNameLower;
}
}
return $paths;
}
}

View File

@ -0,0 +1,49 @@
<?php
namespace Modules\Employee\Providers;
use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
class RouteServiceProvider extends ServiceProvider
{
/**
* Called before routes are registered.
*
* Register any model bindings or pattern based filters.
*/
public function boot(): void
{
parent::boot();
}
/**
* Define the routes for the application.
*/
public function map(): void
{
$this->mapApiRoutes();
$this->mapWebRoutes();
}
/**
* Define the "web" routes for the application.
*
* These routes all receive session state, CSRF protection, etc.
*/
protected function mapWebRoutes(): void
{
Route::middleware('web')->group(module_path('Employee', '/routes/web.php'));
}
/**
* Define the "api" routes for the application.
*
* These routes are typically stateless.
*/
protected function mapApiRoutes(): void
{
Route::middleware('api')->prefix('api')->name('api.')->group(module_path('Employee', '/routes/api.php'));
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace Modules\Employee\Repositories;
use Modules\Employee\Models\Employee;
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);
public function pluck();
}

View File

@ -0,0 +1,63 @@
<?php
namespace Modules\Employee\Repositories;
use Illuminate\Support\Facades\DB;
use Modules\Employee\Models\Employee;
class EmployeeRepository implements EmployeeInterface
{
public function findAll()
{
return Employee::when(true, function ($query) {
if (auth()->user()->hasRole('employee')) {
$user = \Auth::user();
$query->where('id', $user->employee_id);
}
})->paginate(20);
}
public function getEmployeeById($employeeId)
{
return Employee::findOrFail($employeeId);
}
public function getUserByEmpId($employeeId)
{
$employee = Employee::findOrFail($employeeId);
return $employee->user ?? null;
}
public function getEmployeeByEmail($email)
{
return Employee::where('email', $email)->first();
}
public function delete($employeeId)
{
Employee::destroy($employeeId);
}
public function create($employeeDetails)
{
return Employee::create($employeeDetails);
}
public function update($employeeId, array $newDetails)
{
return Employee::whereId($employeeId)->update($newDetails);
}
public function pluck()
{
$employee = Employee::query();
if (auth()->user()->hasRole('employee')) {
$employee->where('id', auth()->user()->employee_id);
}
$query = $employee->pluck(DB::raw('CONCAT(first_name, " ", COALESCE(middle_name, ""), " ", last_name) AS full_name'), 'id');
return $query;
}
}