328 lines
11 KiB
PHP
328 lines
11 KiB
PHP
<?php
|
|
|
|
namespace Modules\Attendance\Http\Controllers;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
use Modules\Admin\Models\Holiday;
|
|
use Modules\Attendance\Models\Attendance;
|
|
use Modules\Attendance\Repositories\AttendanceInterface;
|
|
use Modules\Employee\Interfaces\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(Request $request)
|
|
{
|
|
$data['title'] = 'Attendance Lists';
|
|
|
|
$data['employees'] = $this->employeeRepository->findAll($request)
|
|
->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 = $employee->clock_out ? number_format(Carbon::parse($employee->clock_in)->diffInHours(Carbon::parse($employee->clock_out)), 2) : '';
|
|
$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)
|
|
{
|
|
try {
|
|
$attendance = Attendance::whereDate('date', $request->date ?? now())
|
|
->where('employee_id', $request->employee_id)
|
|
->first();
|
|
|
|
if ($attendance) {
|
|
flash()->error("Attendance Already Exists");
|
|
return redirect()->back();
|
|
}
|
|
|
|
$clockInTime = Carbon::parse($request->clock_in_time);
|
|
$clockOutTime = Carbon::parse($request->clock_out_time);
|
|
$totalMinutes = $clockOutTime->diffInMinutes($clockInTime);
|
|
$totalHours = round($totalMinutes / 60, 2);
|
|
|
|
$request->merge([
|
|
'date' => $request->date ?? now()->format('Y-m-d'),
|
|
'clock_in_ip' => request()->ip(),
|
|
'clock_out_ip' => request()->ip(),
|
|
'total_hours' => $totalHours,
|
|
'createdBy' => auth()->user()->id,
|
|
]);
|
|
|
|
$this->attendanceRepository->create($request->all());
|
|
|
|
flash()->success('Attendance has been recorded!');
|
|
return to_route('attendance.index');
|
|
} catch (\Throwable $th) {
|
|
flash()->error($th->getMessage());
|
|
return redirect()->back();
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* 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) {
|
|
flash()->error($th->getMessage());
|
|
}
|
|
|
|
return view('attendance::attendances.edit', $data);
|
|
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(Request $request, $id)
|
|
{
|
|
try {
|
|
|
|
$this->attendanceRepository->update($id, $request->all());
|
|
flash()->success('Attendance Updated Successfully');
|
|
|
|
} catch (\Throwable $th) {
|
|
flash()->error($th->getMessage());
|
|
}
|
|
return redirect()->route('attendance.index');
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy($id)
|
|
{
|
|
try {
|
|
$this->attendanceRepository->delete($id);
|
|
flash()->success('Attendance Deleted Successfully');
|
|
} catch (\Throwable $th) {
|
|
flash()->error($th->getMessage());
|
|
}
|
|
return redirect()->route('attendance.index');
|
|
}
|
|
|
|
public function clockInOut(Request $request)
|
|
{
|
|
$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(),
|
|
]);
|
|
flash()->success('Clock-in recorded successfully.');
|
|
}
|
|
|
|
if ($request->type == 'clockout') {
|
|
$request->merge([
|
|
'clock_out_time' => now()->format('H:i:s'),
|
|
'clock_out_ip' => request()->ip(),
|
|
]);
|
|
|
|
if ($attendance->clock_in_time && $request->clock_out_time) {
|
|
$clockIn = Carbon::parse($attendance->clock_in_time);
|
|
$clockOut = Carbon::parse($request->clock_out_time);
|
|
$totalMinutes = $clockOut->diffInMinutes($clockIn);
|
|
$totalHours = round($totalMinutes / 60, 2);
|
|
|
|
$attendance->total_hours = $totalHours;
|
|
}
|
|
|
|
flash()->success('Clock-out recorded successfully.');
|
|
}
|
|
|
|
$attendance->update($request->all());
|
|
} else {
|
|
if ($request->type == 'clockin') {
|
|
$request->merge([
|
|
'clock_in_time' => now()->format('H:i:s'),
|
|
'clock_in_ip' => request()->ip(),
|
|
]);
|
|
flash()->success('Clock-in recorded successfully.');
|
|
}
|
|
|
|
if ($request->type == 'clockout') {
|
|
$request->merge([
|
|
'clock_out_time' => now()->format('H:i:s'),
|
|
'clock_out_ip' => request()->ip(),
|
|
]);
|
|
|
|
if ($request->clock_in_time && $request->clock_out_time) {
|
|
$clockIn = Carbon::parse($request->clock_in_time);
|
|
$clockOut = Carbon::parse($request->clock_out_time);
|
|
$totalMinutes = $clockOut->diffInMinutes($clockIn);
|
|
$totalHours = round($totalMinutes / 60, 2);
|
|
|
|
$request->merge(['total_hours' => $totalHours]);
|
|
}
|
|
|
|
flash()->success('Clock-out recorded successfully.');
|
|
}
|
|
|
|
$this->attendanceRepository->create($request->all());
|
|
}
|
|
|
|
return redirect()->back();
|
|
}
|
|
|
|
|
|
// 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();
|
|
// }
|
|
|
|
|
|
}
|