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

243 lines
7.8 KiB
PHP

<?php
namespace Modules\Attendance\Http\Controllers;
use App\Http\Controllers\Controller;
use Carbon\Carbon;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Modules\Admin\Models\Holiday;
use Modules\Attendance\Models\Attendance;
use Modules\Attendance\Repositories\AttendanceInterface;
use Modules\Attendance\Repositories\AttendanceRepository;
use Modules\Employee\Repositories\EmployeeInterface;
use Modules\Leave\Models\Leave;
class AttendanceController extends Controller
{
private $attendanceRepository;
private $employeeRepository;
public function __construct(
AttendanceInterface $attendanceRepository, EmployeeInterface $employeeRepository) {
$this->attendanceRepository = $attendanceRepository;
$this->employeeRepository = $employeeRepository;
}
/**
* Display a listing of the resource.
*/
public function index()
{
$data['title'] = 'Attendance Lists';
$data['employees'] = $this->employeeRepository->findAll()
->transform(function ($employee) use ($data) {
$employee->status = 'Absent';
$leave = Leave::
where('employee_id', $employee->id)
->whereDate('start_date', date('Y-m-d'))
->whereStatus(2)
->first();
if ($leave) {
$employee->status = 'Leave';
}
$holiday = Holiday::
whereDate('start_date', date('Y-m-d'))
->first();
if ($holiday) {
$employee->status = 'Holiday';
}
$attendances = Attendance::
where('employee_id', $employee->id)
->whereDate('date', date('Y-m-d'))
->first();
if ($attendances) {
$employee->atd_date = $attendances->date;
$employee->clock_in = $attendances->clock_in_time;
$employee->clock_out = $attendances->clock_out_time;
$employee->total_hrs = Carbon::parse($employee->clock_out)->diffInMinutes(Carbon::parse($employee->clock_in));
$employee->status = 'Present';
}
return $employee;
});
return view('attendance::attendances.index', $data);
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
$data['title'] = 'Create Attendance';
$data['editable'] = false;
$data['employeeList'] = $this->employeeRepository->pluck();
return view('attendance::attendances.create', $data);
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request): RedirectResponse
{
try {
$attendance = Attendance::whereDate('date', now())->where('employee_id', $request->employee_id)->first();
if ($attendance) {
toastr()->error("Attendance Already Exists");
return redirect()->back();
}
$request->merge([
'date' => $request->date ? $request->date : now()->format('Y-m-d'),
// 'clock_in_time' => $request->clock_in_time ? $request->clock_in_time : now()->format('H:i:s'),
'clock_in_ip' => request()->ip(),
'clock_out_ip' => request()->ip(),
'createdBy' => auth()->user()->id,
]);
$this->attendanceRepository->create($request->all());
toastr()->success('Attendance Created Successfully');
} catch (\Throwable $th) {
toastr()->error($th->getMessage());
}
return redirect()->route('attendance.index');
}
/**
* Show the specified resource.
*/
public function show($id)
{
return view('attendance::attendances.show');
}
/**
* Show the form for editing the specified resource.
*/
public function edit($id)
{
try {
$data['title'] = 'Edit Attendance';
$data['editable'] = true;
$data['employeeList'] = $this->employeeRepository->pluck();
$data['attendance'] = $this->attendanceRepository->getAttendanceById($id);
} catch (\Throwable $th) {
toastr()->error($th->getMessage());
}
return view('attendance::attendances.edit', $data);
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, $id): RedirectResponse
{
try {
$this->attendanceRepository->update($id, $request->all());
toastr()->success('Attendance Updated Successfully');
} catch (\Throwable $th) {
toastr()->error($th->getMessage());
}
return redirect()->route('attendance.index');
}
/**
* Remove the specified resource from storage.
*/
public function destroy($id)
{
try {
$this->attendanceRepository->delete($id);
toastr()->success('Attendance Deleted Successfully');
} catch (\Throwable $th) {
toastr()->error($th->getMessage());
}
return redirect()->route('attendance.index');
}
public function clockInOut(Request $request): RedirectResponse
{
$request->merge([
'date' => now()->format('Y-m-d'),
'employee_id' => auth()->user()->employee_id,
'createdBy' => auth()->user()->id,
]);
$attendance = Attendance::whereDate('date', now())->where('employee_id', auth()->user()->employee_id)->first();
if ($attendance) {
if ($request->type == 'clockin') {
$request->merge([
'clock_in_time' => now()->format('H:i:s'),
'clock_in_ip' => request()->ip(),
]);
}
if ($request->type == 'clockout') {
$request->merge([
'clock_out_time' => now()->format('H:i:s'),
'clock_out_ip' => request()->ip(),
]);
}
$attendance->update($request->all());
} else {
if ($request->type == 'clockin') {
$request->merge([
'clock_in_time' => now()->format('H:i:s'),
'clock_in_ip' => request()->ip(),
]);
}
$this->attendanceRepository->create($request->all());
}
// dd('asd');
// try {
// $attendance = Attendance::whereDate('date', now())->where('employee_id', auth()->user()->employee_id)->latest()->first();
// if ($attendance && $attendance->clock_out_time == null) {
// $request->merge([
// 'clock_out_time' => now()->format('H:i:s'),
// 'clock_out_ip' => request()->ip(),
// 'updatedBy' => auth()->user()->id,
// ]);
// $this->attendanceRepository->update($attendance->attendance_id, $request->except('_token'));
// toastr()->success('Clocked Out Successfully.');
// } else {
// $request->merge([
// 'date' => now()->format('Y-m-d'),
// 'clock_in_time' => now()->format('H:i:s'),
// 'clock_in_ip' => request()->ip(),
// 'employee_id' => auth()->user()->employee_id,
// 'createdBy' => auth()->user()->id,
// ]);
// $this->attendanceRepository->create($request->all());
// toastr()->success('Clocked In Successfully');
// }
// } catch (\Throwable $th) {
// toastr()->error($th->getMessage());
// }
return redirect()->back();
}
}