first commit

This commit is contained in:
Sampanna Rimal
2024-08-27 17:48:06 +05:45
commit 53c0140f58
10839 changed files with 1125847 additions and 0 deletions

View File

@@ -0,0 +1,149 @@
<?php
namespace Modules\Leave\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Modules\Employee\Repositories\EmployeeInterface;
use Modules\Employee\Repositories\EmployeeRepository;
use Modules\Leave\Models\Leave;
use Modules\Leave\Repositories\LeaveInterface;
use Modules\Leave\Repositories\LeaveRepository;
use Modules\Leave\Repositories\LeaveTypeInterface;
use Modules\Leave\Repositories\LeaveTypeRepository;
use Yoeunes\Toastr\Facades\Toastr;
class LeaveController extends Controller
{
private $leaveRepository;
private $employeeRepository;
private $leaveTypeRepository;
public function __construct(LeaveInterface $leaveRepository, EmployeeInterface $employeeRepository, LeaveTypeInterface $leaveTypeRepository)
{
$this->leaveRepository = $leaveRepository;
$this->employeeRepository = $employeeRepository;
$this->leaveTypeRepository = $leaveTypeRepository;
}
/**
* Display a listing of the resource.
*/
public function index(Request $request)
{
$filters = $request->all();
$data['leaves'] = $this->leaveRepository->findAll($filters);
$data['employeeList'] = $this->employeeRepository->pluck();
$data['status'] = Leave::PROGRESS_STATUS;
return view('leave::leave.index', $data);
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
$data['title'] = 'Create Leave';
$data['editable'] = false;
$data['employeeList'] = $this->employeeRepository->pluck();
$data['leaveTypeList'] = $this->leaveTypeRepository->pluck();
$data['status'] = Leave::PROGRESS_STATUS;
$data['duration'] = Leave::DURATION;
return view('leave::leave.create', $data);
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request): RedirectResponse
{
$inputData = $request->all();
try {
$this->leaveRepository->create($inputData);
$employeemodel = $this->employeeRepository->getEmployeeById($inputData['employee_id']);
sendNotification($employeemodel->user, [
'msg' => 'Leave Created']);
toastr()->success('Leave Created Succesfully');
} catch (\Throwable $th) {
toastr()->error($th->getMessage());
}
return redirect()->route('leave.index');
}
/**
* Show the specified resource.
*/
public function show($id)
{
$data['title'] = 'View Leave';
$data['leave'] = $this->leaveRepository->getLeaveById($id);
return view('leave::leave.show', $data);
}
/**
* Show the form for editing the specified resource.
*/
public function edit($id)
{
$data['title'] = 'Edit Leave';
$data['editable'] = true;
$data['leave'] = $this->leaveRepository->getLeaveById($id);
$data['employeeList'] = $this->employeeRepository->pluck();
$data['leaveTypeList'] = $this->leaveTypeRepository->pluck();
$data['status'] = Leave::PROGRESS_STATUS;
$data['duration'] = Leave::DURATION;
return view('leave::leave.edit', $data);
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, $id): RedirectResponse
{
$inputData = $request->except(['_method', "_token"]);
try {
$this->leaveRepository->update($id, $inputData);
toastr()->success('Leave Updated Succesfully');
} catch (\Throwable $th) {
toastr()->error($th->getMessage());
}
return redirect()->route('leave.index');
}
/**
* Remove the specified resource from storage.
*/
public function destroy($id)
{
try {
$this->leaveRepository->delete($id);
toastr()->success('Leave Deleted Succesfully');
} catch (\Throwable $th) {
//throw $th;
toastr()->error($th->getMessage());
}
}
/**
* Update the status of the specified resource in storage.
*/
public function updateStatus(Request $request)
{
try {
$this->leaveRepository->update($request->leave_id, $request->except(['_method', "_token"]));
$employeemodel = $this->employeeRepository->getEmployeeById($request->employee_id);
sendNotification($employeemodel->user, [
'msg' => 'Leave Status Changed']);
toastr()->success('Leave Status Updated Successfully');
} catch (\Throwable $th) {
toastr()->error($th->getMessage());
}
return redirect()->route('leave.index');
}
}

View File

@@ -0,0 +1,100 @@
<?php
namespace Modules\Leave\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Modules\Leave\Repositories\LeaveTypeInterface;
use Modules\Leave\Repositories\LeaveTypeRepository;
class LeaveTypeController extends Controller
{
private $leaveTypeRepository;
public function __construct(LeaveTypeInterface $leaveTypeRepository)
{
$this->leaveTypeRepository = $leaveTypeRepository;
}
/**
* Display a listing of the resource.
*/
public function index()
{
$data['title'] = 'LeaveType List';
$data['leaveTypeLists'] = $this->leaveTypeRepository->findAll();
return view('leave::leave-type.index', $data);
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
$data['title'] = 'Create LeaveType';
$data['editable'] = false;
return view('leave::leave-type.create', $data);
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request): RedirectResponse
{
try {
$this->leaveTypeRepository->create($request->all());
toastr()->success('LeaveType created successfully');
} catch (\Throwable $th) {
toastr()->error($th->getMessage());
}
return redirect()->route('leaveType.index');
}
/**
* Show the specified resource.
*/
public function show($id)
{
$data['title'] = 'View Leave';
$data['leave'] = $this->leaveTypeRepository->getLeaveTypeById($id);
return view('leave::leave-type.show', $data);
}
/**
* Show the form for editing the specified resource.
*/
public function edit($id)
{
$data['editable'] = true;
$data['title'] = 'Edit LeaveType';
$data['leaveType'] = $this->leaveTypeRepository->getLeaveTypeById($id);
return view('leave::leave-type.edit', $data);
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, $id): RedirectResponse
{
try {
$this->leaveTypeRepository->update($id, $request->except(['_token', '_method']));
toastr()->success('LeaveType updated successfully');
} catch (\Throwable $th) {
toastr()->error($th->getMessage());
}
return redirect()->route('leaveType.index');
}
/**
* Remove the specified resource from storage.
*/
public function destroy($id)
{
$this->leaveTypeRepository->delete($id);
toastr()->success('LeaveType deleted successfully');
return redirect()->route('leaveType.index');
}
}

View File

View File

@@ -0,0 +1,26 @@
<?php
namespace Modules\Leave\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class LeaveTypeRequest extends FormRequest
{
/**
* Get the validation rules that apply to the request.
*/
public function rules(): array
{
return [
//
];
}
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
}

View File

View File

@@ -0,0 +1,76 @@
<?php
namespace Modules\Leave\Models;
use App\Models\Scopes\CreatedByScope;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Modules\Employee\Models\Employee;
class Leave extends Model
{
protected $table = 'tbl_leaves';
protected $primaryKey = 'leave_id';
protected $guarded = [];
protected $appends = ['status_name'];
const PROGRESS_STATUS = [
1 => 'Pending',
2 => 'Approved',
3 => 'Rejected',
];
const DURATION = [
1 => 'Full Day',
2 => 'Half Day',
3 => 'Multiple',
];
protected static function booted(): void
{
static::addGlobalScope(new CreatedByScope);
}
protected function statusName(): Attribute
{
return Attribute::make(
get: function (mixed $value, array $attributes) {
switch ($attributes['status']) {
case '1':
$color = 'dark';
break;
case '2':
$color = 'success';
break;
case '3':
$color = 'danger';
break;
default:
$color = 'light';
break;
}
return collect([
'status' => self::PROGRESS_STATUS[$attributes['status']],
'color' => $color]);
},
set: fn($value) => $value,
);
}
public function getDuration()
{
return self::DURATION[$this->duration] ?? null;
}
public function employee()
{
return $this->belongsTo(Employee::class, 'employee_id');
}
public function leaveType()
{
return $this->belongsTo(LeaveType::class, 'leave_type_id');
}
}

View File

@@ -0,0 +1,32 @@
<?php
namespace Modules\Leave\Models;
use App\Traits\StatusTrait;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class LeaveType extends Model
{
use HasFactory, StatusTrait;
protected $table = 'tbl_leave_types';
protected $primaryKey = 'leave_type_id';
protected $fillable = [
'name',
'total_days',
'max_accumulated_days',
'is_accumulated',
'is_proportionate',
'status',
'description',
'remarks',
'createdBy',
'updatedBy',
];
protected $casts = [
'is_accumulated' => 'boolean',
'is_proportionate' => 'boolean',
];
}

View File

View File

@@ -0,0 +1,121 @@
<?php
namespace Modules\Leave\Providers;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;
use Modules\Leave\Repositories\LeaveInterface;
use Modules\Leave\Repositories\LeaveRepository;
use Modules\Leave\Repositories\LeaveTypeInterface;
use Modules\Leave\Repositories\LeaveTypeRepository;
class LeaveServiceProvider extends ServiceProvider
{
protected string $moduleName = 'Leave';
protected string $moduleNameLower = 'leave';
/**
* 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(LeaveInterface::class, LeaveRepository::class);
$this->app->bind(LeaveTypeInterface::class, LeaveTypeRepository::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\Leave\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('Leave', '/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('Leave', '/routes/api.php'));
}
}

View File

View File

@@ -0,0 +1,12 @@
<?php
namespace Modules\Leave\Repositories;
interface LeaveInterface
{
public function findAll($filters = [], $limit = null, $offset = null);
public function getLeaveById($leaveId);
public function delete($leaveId);
public function create(array $LeaveDetails);
public function update($leaveId, array $newDetails);
}

View File

@@ -0,0 +1,52 @@
<?php
namespace Modules\Leave\Repositories;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Modules\Leave\Models\Leave;
class LeaveRepository implements LeaveInterface
{
public function findAll($filters = [], $limit = null, $offset = null)
{
return Leave::with(['leaveType', 'employee'])->when($filters, function ($query) use ($filters) {
if (isset($filters["employee_id"])) {
$query->whereHas('employee', function (Builder $query) use ($filters) {
$query->where('id', '=', $filters["employee_id"]);
});
}
if (isset($filters["status"])) {
$query->where("status", $filters["status"]);
}
if (isset($filters["date"])) {
$explodeDate = explode("to", $filters['date']);
$query->whereBetween("start_date", [$explodeDate[0], preg_replace('/\s+/', '', $explodeDate[1])]);
}
})->get();
}
public function getLeaveById($leaveId)
{
return Leave::findOrFail($leaveId);
}
public function delete($leaveId)
{
Leave::destroy($leaveId);
}
public function create(array $leaveDetails)
{
return Leave::create($leaveDetails);
}
public function update($leaveId, array $newDetails)
{
return Leave::where('leave_id', $leaveId)->update($newDetails);
}
}

View File

@@ -0,0 +1,13 @@
<?php
namespace Modules\Leave\Repositories;
interface LeaveTypeInterface
{
public function pluck();
public function findAll();
public function getLeaveTypeById($leaveTypeId);
public function delete($leaveTypeId);
public function create(array $LeaveTypeDetails);
public function update($leaveTypeId, array $newDetails);
}

View File

@@ -0,0 +1,38 @@
<?php
namespace Modules\Leave\Repositories;
use Modules\Leave\Models\LeaveType;
class LeaveTypeRepository implements LeaveTypeInterface
{
public function pluck()
{
return LeaveType::pluck('name', 'leave_type_id');
}
public function findAll()
{
return LeaveType::get();
}
public function getLeaveTypeById($leaveTypeId)
{
return LeaveType::findOrFail($leaveTypeId);
}
public function create(array $leaveTypeDetails)
{
return LeaveType::create($leaveTypeDetails);
}
public function update($leaveTypeId, array $newDetails)
{
return LeaveType::where('leave_type_id', $leaveTypeId)->update($newDetails);
}
public function delete($leaveTypeId)
{
LeaveType::destroy($leaveTypeId);
}
}