87 lines
3.5 KiB
PHP
87 lines
3.5 KiB
PHP
<?php
|
|
require_once __DIR__ . '/../db/config.php';
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
// Run migrations first
|
|
run_migrations();
|
|
|
|
$response = [
|
|
'success' => false,
|
|
'message' => 'Invalid request'
|
|
];
|
|
|
|
function get_last_status($pdo, $employee_id) {
|
|
$stmt = $pdo->prepare("SELECT * FROM time_records WHERE employee_id = ? ORDER BY id DESC LIMIT 1");
|
|
$stmt->execute([$employee_id]);
|
|
return $stmt->fetch(PDO::FETCH_ASSOC);
|
|
}
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
$action = $data['action'] ?? null;
|
|
$employee_id = $data['employee_id'] ?? null;
|
|
|
|
if ($action && $employee_id) {
|
|
try {
|
|
$pdo = db();
|
|
$last_record = get_last_status($pdo, $employee_id);
|
|
|
|
if ($action === 'clock_in') {
|
|
if ($last_record && $last_record['clock_out'] === null) {
|
|
$response['message'] = 'Ya has fichado la entrada. Debes fichar la salida primero.';
|
|
} else {
|
|
$stmt = $pdo->prepare("INSERT INTO time_records (employee_id, clock_in) VALUES (?, NOW())");
|
|
$stmt->execute([$employee_id]);
|
|
$response['success'] = true;
|
|
$response['message'] = 'Entrada registrada con éxito.';
|
|
$response['status'] = 'Fichado a las ' . date('H:i:s');
|
|
$response['action'] = 'clock_in';
|
|
}
|
|
} elseif ($action === 'clock_out') {
|
|
if (!$last_record || $last_record['clock_out'] !== null) {
|
|
$response['message'] = 'No has fichado la entrada. Debes fichar la entrada primero.';
|
|
} else {
|
|
$stmt = $pdo->prepare("UPDATE time_records SET clock_out = NOW() WHERE id = ?");
|
|
$stmt->execute([$last_record['id']]);
|
|
$response['success'] = true;
|
|
$response['message'] = 'Salida registrada con éxito.';
|
|
$response['status'] = 'Salida registrada a las ' . date('H:i:s');
|
|
$response['action'] = 'clock_out';
|
|
}
|
|
} else {
|
|
$response['message'] = 'Acción no válida.';
|
|
}
|
|
} catch (PDOException $e) {
|
|
$response['message'] = 'Error de base de datos: ' . $e->getMessage();
|
|
}
|
|
} else {
|
|
$response['message'] = 'Faltan datos en la solicitud.';
|
|
}
|
|
} elseif ($_SERVER['REQUEST_METHOD'] === 'GET') {
|
|
$employee_id = $_GET['employee_id'] ?? null;
|
|
if ($employee_id) {
|
|
try {
|
|
$pdo = db();
|
|
$last_record = get_last_status($pdo, $employee_id);
|
|
if ($last_record) {
|
|
if($last_record['clock_out'] === null) {
|
|
$response['status'] = 'Fichado a las ' . date('H:i:s', strtotime($last_record['clock_in']));
|
|
$response['last_action'] = 'clock_in';
|
|
} else {
|
|
$response['status'] = 'Salida registrada a las ' . date('H:i:s', strtotime($last_record['clock_out']));
|
|
$response['last_action'] = 'clock_out';
|
|
}
|
|
} else {
|
|
$response['status'] = 'Listo para fichar la entrada.';
|
|
$response['last_action'] = 'clock_out';
|
|
}
|
|
$response['success'] = true;
|
|
} catch (PDOException $e) {
|
|
$response['message'] = 'Error de base de datos: ' . $e->getMessage();
|
|
}
|
|
}
|
|
}
|
|
|
|
echo json_encode($response);
|