Files
HRM-System/app/Http/Controllers/Api/AttendanceApiController.php

111 lines
3.3 KiB
PHP

<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\AttendanceRecord;
use App\Services\AttendanceService;
use App\Services\EmployeeService;
use Illuminate\Http\Request;
use Exception;
class AttendanceApiController extends Controller
{
protected EmployeeService $employeeService;
protected AttendanceService $attendanceService;
public function __construct(EmployeeService $employeeService, AttendanceService $attendanceService)
{
$this->employeeService = $employeeService;
$this->attendanceService = $attendanceService;
}
public function today(Request $request)
{
try {
$employee = $this->employeeService->resolveEmployee($request->user());
} catch (Exception $e) {
return response()->json([
'success' => false,
'data' => null,
'message' => 'Employee profile not found',
'errors' => null,
], 403);
}
$record = $this->attendanceService->getTodayRecord($employee);
return response()->json([
'success' => true,
'data' => $record ? [
'id' => $record->id,
'date' => $record->date,
'clock_in' => $record->clock_in,
'clock_out' => $record->clock_out,
'total_hours' => $record->total_hours,
'status' => $record->status ?? 'present',
'notes' => $record->notes,
] : null,
'message' => null,
'errors' => null,
]);
}
public function clock(Request $request)
{
$request->validate([
'action' => 'required|string|in:clock_in,clock_out',
'latitude' => 'nullable|numeric',
'longitude' => 'nullable|numeric',
'notes' => 'nullable|string',
'activity_note' => 'nullable|string',
]);
try {
$employee = $this->employeeService->resolveEmployee($request->user());
} catch (Exception $e) {
return response()->json([
'success' => false,
'data' => null,
'message' => 'Employee profile not found',
'errors' => null,
], 403);
}
$result = $this->attendanceService->processClock($request->user(), $employee, $request->all());
return response()->json([
'success' => $result['success'],
'data' => $result['data'],
'message' => $result['message'],
'errors' => null,
], $result['status_code']);
}
public function history(Request $request)
{
try {
$employee = $this->employeeService->resolveEmployee($request->user());
} catch (Exception $e) {
return response()->json([
'success' => false,
'data' => [],
'message' => 'Employee profile not found',
'errors' => null,
], 403);
}
$records = AttendanceRecord::where('employee_id', $request->user()->id)
->orderBy('date', 'desc')
->limit(30)
->get();
return response()->json([
'success' => true,
'data' => $records,
'message' => null,
'errors' => null,
]);
}
}