New-OMIS/Modules/Leave/app/Http/Controllers/LeaveController.php

124 lines
3.5 KiB
PHP
Raw Normal View History

2024-04-05 04:33:35 +00:00
<?php
namespace Modules\Leave\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
2024-04-14 12:44:29 +00:00
use Modules\Employee\Repositories\EmployeeRepository;
use Modules\Leave\Repositories\LeaveRepository;
use Modules\Leave\Repositories\LeaveTypeRepository;
2024-04-05 12:11:16 +00:00
use Yoeunes\Toastr\Facades\Toastr;
2024-04-05 04:33:35 +00:00
class LeaveController extends Controller
{
2024-04-11 10:52:12 +00:00
private $leaveRepository;
private $employeeRepository;
2024-04-14 12:44:29 +00:00
private $leaveTypeRepository;
2024-04-05 04:33:35 +00:00
2024-04-14 12:44:29 +00:00
public function __construct(LeaveRepository $leaveRepository, EmployeeRepository $employeeRepository, LeaveTypeRepository $leaveTypeRepository)
2024-04-05 04:33:35 +00:00
{
$this->leaveRepository = $leaveRepository;
2024-04-11 10:52:12 +00:00
$this->employeeRepository = $employeeRepository;
2024-04-14 12:44:29 +00:00
$this->leaveTypeRepository = $leaveTypeRepository;
2024-04-05 12:11:16 +00:00
$this->middleware('role_or_permission:access leaves|create leaves|edit leaves|delete leaves', ['only' => ['index', 'show']]);
$this->middleware('role_or_permission:create leaves', ['only' => ['create', 'store']]);
$this->middleware('role_or_permission:edit leaves', ['only' => ['edit', 'update']]);
$this->middleware('role_or_permission:delete leaves', ['only' => ['destroy']]);
2024-04-05 04:33:35 +00:00
}
/**
* Display a listing of the resource.
*/
public function index()
{
$data['leaves'] = $this->leaveRepository->findAll();
2024-04-11 10:52:12 +00:00
$data['employeeList'] = $this->employeeRepository->pluck();
2024-04-11 12:10:56 +00:00
return view('leave::leave.index', $data);
2024-04-05 04:33:35 +00:00
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
$data['title'] = 'Create Leave';
2024-04-14 12:44:29 +00:00
$data['editable'] = false;
2024-04-11 10:52:12 +00:00
$data['employeeList'] = $this->employeeRepository->pluck();
2024-04-14 12:44:29 +00:00
$data['leaveTypeList'] = $this->leaveTypeRepository->pluck();
2024-04-11 12:10:56 +00:00
return view('leave::leave.create', $data);
2024-04-05 04:33:35 +00:00
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request): RedirectResponse
{
$inputData = $request->all();
try {
$this->leaveRepository->create($inputData);
2024-04-09 12:15:26 +00:00
toastr()->success('Leave Created Succesfully');
2024-04-05 04:33:35 +00:00
} catch (\Throwable $th) {
toastr()->error($th->getMessage());
}
return redirect()->route('leave.index');
}
/**
* Show the specified resource.
*/
public function show($id)
{
2024-04-11 12:10:56 +00:00
return view('leave::leave.show');
2024-04-05 04:33:35 +00:00
}
/**
* Show the form for editing the specified resource.
*/
public function edit($id)
{
2024-04-05 12:11:16 +00:00
$data['title'] = 'Edit Leave';
2024-04-09 12:15:26 +00:00
2024-04-14 12:44:29 +00:00
$data['editable'] = true;
2024-04-05 12:11:16 +00:00
$data['leave'] = $this->leaveRepository->getLeaveById($id);
2024-04-09 12:15:26 +00:00
2024-04-11 12:10:56 +00:00
return view('leave::leave.edit', $data);
2024-04-05 04:33:35 +00:00
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, $id): RedirectResponse
{
2024-04-05 12:11:16 +00:00
$inputData = $request->all();
try {
2024-04-09 12:15:26 +00:00
$this->leaveRepository->update($id, $inputData);
2024-04-05 12:11:16 +00:00
toastr()->success('Leave Updated Succesfully');
2024-04-09 12:15:26 +00:00
2024-04-05 12:11:16 +00:00
} catch (\Throwable $th) {
toastr()->error($th->getMessage());
}
return redirect()->route('leave.index');
2024-04-05 04:33:35 +00:00
}
/**
* Remove the specified resource from storage.
*/
public function destroy($id)
{
2024-04-16 11:14:18 +00:00
try {
$this->leaveRepository->delete($id);
toastr()->success('Leave Deleted Succesfully');
} catch (\Throwable $th) {
//throw $th;
toastr()->error($th->getMessage());
}
2024-04-09 12:15:26 +00:00
2024-04-05 04:33:35 +00:00
}
}