StocksNew/Modules/Leave/app/Http/Controllers/LeaveController.php
Sampanna Rimal 53c0140f58 first commit
2024-08-27 17:48:06 +05:45

150 lines
4.7 KiB
PHP

<?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');
}
}