Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
20692e4324 | ||
|
|
c4c5898141 | ||
|
|
133015b3ec | ||
|
|
308db66c9b |
86
api/track_time.php
Normal file
86
api/track_time.php
Normal file
@ -0,0 +1,86 @@
|
||||
<?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);
|
||||
110
assets/css/custom.css
Normal file
110
assets/css/custom.css
Normal file
@ -0,0 +1,110 @@
|
||||
|
||||
body {
|
||||
background-color: #F4F7F6;
|
||||
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
height: 100vh;
|
||||
margin: 0;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.clock-container {
|
||||
background-color: #FFFFFF;
|
||||
padding: 40px;
|
||||
border-radius: 12px;
|
||||
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
|
||||
text-align: center;
|
||||
width: 100%;
|
||||
max-width: 400px;
|
||||
}
|
||||
|
||||
h1 {
|
||||
font-size: 24px;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
|
||||
#current-time {
|
||||
font-size: 48px;
|
||||
font-weight: 600;
|
||||
margin-bottom: 20px;
|
||||
letter-spacing: 2px;
|
||||
}
|
||||
|
||||
#status-message {
|
||||
font-size: 16px;
|
||||
color: #555;
|
||||
margin-bottom: 30px;
|
||||
min-height: 24px;
|
||||
}
|
||||
|
||||
.btn {
|
||||
border: none;
|
||||
padding: 15px 30px;
|
||||
font-size: 18px;
|
||||
font-weight: 500;
|
||||
border-radius: 8px;
|
||||
cursor: pointer;
|
||||
transition: all 0.3s ease;
|
||||
width: 100%;
|
||||
margin-bottom: 10px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
.btn-primary {
|
||||
background-color: #4A90E2;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.btn-primary:hover {
|
||||
background-color: #357ABD;
|
||||
}
|
||||
|
||||
.btn-danger {
|
||||
background-color: #D0021B;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.btn-danger:hover {
|
||||
background-color: #A80115;
|
||||
}
|
||||
|
||||
.btn:disabled {
|
||||
background-color: #D8D8D8;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.toast {
|
||||
position: fixed;
|
||||
top: 20px;
|
||||
right: 20px;
|
||||
background-color: #333;
|
||||
color: white;
|
||||
padding: 15px 20px;
|
||||
border-radius: 8px;
|
||||
box-shadow: 0 5px 15px rgba(0,0,0,0.2);
|
||||
opacity: 0;
|
||||
visibility: hidden;
|
||||
transition: opacity 0.3s, visibility 0.3s, transform 0.3s;
|
||||
transform: translateY(-20px);
|
||||
z-index: 1000;
|
||||
}
|
||||
|
||||
.toast.show {
|
||||
opacity: 1;
|
||||
visibility: visible;
|
||||
transform: translateY(0);
|
||||
}
|
||||
|
||||
.toast.success {
|
||||
background-color: #50E3C2;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.toast.error {
|
||||
background-color: #D0021B;
|
||||
}
|
||||
92
assets/js/main.js
Normal file
92
assets/js/main.js
Normal file
@ -0,0 +1,92 @@
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
const timeElement = document.getElementById('current-time');
|
||||
const statusMessage = document.getElementById('status-message');
|
||||
const clockInButton = document.getElementById('clock-in-btn');
|
||||
const clockOutButton = document.getElementById('clock-out-btn');
|
||||
|
||||
function updateTime() {
|
||||
const now = new Date();
|
||||
timeElement.textContent = now.toLocaleTimeString('es-ES');
|
||||
}
|
||||
|
||||
setInterval(updateTime, 1000);
|
||||
updateTime();
|
||||
|
||||
function showToast(message, type = 'success') {
|
||||
const toast = document.createElement('div');
|
||||
toast.className = `toast ${type}`;
|
||||
toast.textContent = message;
|
||||
document.body.appendChild(toast);
|
||||
|
||||
setTimeout(() => {
|
||||
toast.classList.add('show');
|
||||
}, 100);
|
||||
|
||||
setTimeout(() => {
|
||||
toast.classList.remove('show');
|
||||
setTimeout(() => {
|
||||
document.body.removeChild(toast);
|
||||
}, 300);
|
||||
}, 3000);
|
||||
}
|
||||
|
||||
async function handleClockAction(action) {
|
||||
clockInButton.disabled = true;
|
||||
clockOutButton.disabled = true;
|
||||
|
||||
try {
|
||||
const response = await fetch('api/track_time.php', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ action: action, employee_id: 1 }) // Hardcoded employee_id
|
||||
});
|
||||
|
||||
const result = await response.json();
|
||||
|
||||
if (result.success) {
|
||||
showToast(result.message);
|
||||
statusMessage.textContent = result.status;
|
||||
if (result.action === 'clock_in') {
|
||||
clockOutButton.disabled = false;
|
||||
} else {
|
||||
clockInButton.disabled = false;
|
||||
}
|
||||
} else {
|
||||
showToast(result.message, 'error');
|
||||
// Re-enable buttons based on assumed last state if error
|
||||
if(action === 'clock_in') clockInButton.disabled = false; else clockOutButton.disabled = false;
|
||||
}
|
||||
} catch (error) {
|
||||
showToast('Error de conexión con el servidor.', 'error');
|
||||
if(action === 'clock_in') clockInButton.disabled = false; else clockOutButton.disabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
clockInButton.addEventListener('click', () => handleClockAction('clock_in'));
|
||||
clockOutButton.addEventListener('click', () => handleClockAction('clock_out'));
|
||||
|
||||
// Initial state check
|
||||
async function checkInitialState() {
|
||||
try {
|
||||
const response = await fetch('api/track_time.php?employee_id=1');
|
||||
const result = await response.json();
|
||||
if(result.status) {
|
||||
statusMessage.textContent = result.status;
|
||||
if(result.last_action === 'clock_in'){
|
||||
clockInButton.disabled = true;
|
||||
clockOutButton.disabled = false;
|
||||
} else {
|
||||
clockInButton.disabled = false;
|
||||
clockOutButton.disabled = true;
|
||||
}
|
||||
}
|
||||
} catch(e) {
|
||||
// assume default state
|
||||
clockOutButton.disabled = true;
|
||||
}
|
||||
}
|
||||
|
||||
checkInitialState();
|
||||
});
|
||||
@ -15,3 +15,17 @@ function db() {
|
||||
}
|
||||
return $pdo;
|
||||
}
|
||||
|
||||
function run_migrations() {
|
||||
$pdo = db();
|
||||
$migration_files = glob(__DIR__ . '/migrations/*.sql');
|
||||
foreach ($migration_files as $file) {
|
||||
try {
|
||||
$sql = file_get_contents($file);
|
||||
$pdo->exec($sql);
|
||||
} catch (PDOException $e) {
|
||||
// Optionally log this error instead of dying
|
||||
error_log("Migration failed for file: $file. Error: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
8
db/migrations/001_create_time_records_table.sql
Normal file
8
db/migrations/001_create_time_records_table.sql
Normal file
@ -0,0 +1,8 @@
|
||||
-- 001_create_time_records_table.sql
|
||||
CREATE TABLE IF NOT EXISTS `time_records` (
|
||||
`id` INT AUTO_INCREMENT PRIMARY KEY,
|
||||
`employee_id` INT NOT NULL,
|
||||
`clock_in` DATETIME NOT NULL,
|
||||
`clock_out` DATETIME DEFAULT NULL,
|
||||
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
158
index.php
158
index.php
@ -1,131 +1,37 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
@ini_set('display_errors', '1');
|
||||
@error_reporting(E_ALL);
|
||||
@date_default_timezone_set('UTC');
|
||||
|
||||
$phpVersion = PHP_VERSION;
|
||||
$now = date('Y-m-d H:i:s');
|
||||
?>
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<!DOCTYPE html>
|
||||
<html lang="es">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<title>New Style</title>
|
||||
<link rel="preconnect" href="https://fonts.googleapis.com">
|
||||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
||||
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap" rel="stylesheet">
|
||||
<style>
|
||||
:root {
|
||||
--bg-color-start: #6a11cb;
|
||||
--bg-color-end: #2575fc;
|
||||
--text-color: #ffffff;
|
||||
--card-bg-color: rgba(255, 255, 255, 0.01);
|
||||
--card-border-color: rgba(255, 255, 255, 0.1);
|
||||
}
|
||||
body {
|
||||
margin: 0;
|
||||
font-family: 'Inter', sans-serif;
|
||||
background: linear-gradient(45deg, var(--bg-color-start), var(--bg-color-end));
|
||||
color: var(--text-color);
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
min-height: 100vh;
|
||||
text-align: center;
|
||||
overflow: hidden;
|
||||
position: relative;
|
||||
}
|
||||
body::before {
|
||||
content: '';
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
background-image: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 100 100"><path d="M-10 10L110 10M10 -10L10 110" stroke-width="1" stroke="rgba(255,255,255,0.05)"/></svg>');
|
||||
animation: bg-pan 20s linear infinite;
|
||||
z-index: -1;
|
||||
}
|
||||
@keyframes bg-pan {
|
||||
0% { background-position: 0% 0%; }
|
||||
100% { background-position: 100% 100%; }
|
||||
}
|
||||
main {
|
||||
padding: 2rem;
|
||||
}
|
||||
.card {
|
||||
background: var(--card-bg-color);
|
||||
border: 1px solid var(--card-border-color);
|
||||
border-radius: 16px;
|
||||
padding: 2rem;
|
||||
backdrop-filter: blur(20px);
|
||||
-webkit-backdrop-filter: blur(20px);
|
||||
box-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
.loader {
|
||||
margin: 1.25rem auto 1.25rem;
|
||||
width: 48px;
|
||||
height: 48px;
|
||||
border: 3px solid rgba(255, 255, 255, 0.25);
|
||||
border-top-color: #fff;
|
||||
border-radius: 50%;
|
||||
animation: spin 1s linear infinite;
|
||||
}
|
||||
@keyframes spin {
|
||||
from { transform: rotate(0deg); }
|
||||
to { transform: rotate(360deg); }
|
||||
}
|
||||
.hint {
|
||||
opacity: 0.9;
|
||||
}
|
||||
.sr-only {
|
||||
position: absolute;
|
||||
width: 1px; height: 1px;
|
||||
padding: 0; margin: -1px;
|
||||
overflow: hidden;
|
||||
clip: rect(0, 0, 0, 0);
|
||||
white-space: nowrap; border: 0;
|
||||
}
|
||||
h1 {
|
||||
font-size: 3rem;
|
||||
font-weight: 700;
|
||||
margin: 0 0 1rem;
|
||||
letter-spacing: -1px;
|
||||
}
|
||||
p {
|
||||
margin: 0.5rem 0;
|
||||
font-size: 1.1rem;
|
||||
}
|
||||
code {
|
||||
background: rgba(0,0,0,0.2);
|
||||
padding: 2px 6px;
|
||||
border-radius: 4px;
|
||||
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
|
||||
}
|
||||
footer {
|
||||
position: absolute;
|
||||
bottom: 1rem;
|
||||
font-size: 0.8rem;
|
||||
opacity: 0.7;
|
||||
}
|
||||
</style>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Control de Presencia</title>
|
||||
<meta name="description" content="Aplicación para el control de presencia del personal.">
|
||||
<meta property="og:title" content="Control de Presencia">
|
||||
<meta property="og:description" content="Aplicación simple y moderna para fichar la entrada y salida.">
|
||||
<meta property="og:type" content="website">
|
||||
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
|
||||
<script src="https://unpkg.com/feather-icons"></script>
|
||||
</head>
|
||||
<body>
|
||||
<main>
|
||||
<div class="card">
|
||||
<h1>Analyzing your requirements and generating your website…</h1>
|
||||
<div class="loader" role="status" aria-live="polite" aria-label="Applying initial changes">
|
||||
<span class="sr-only">Loading…</span>
|
||||
</div>
|
||||
<p class="hint"><?= ($_SERVER['HTTP_HOST'] ?? '') === 'appwizzy.com' ? 'AppWiZZy' : 'Flatlogic' ?> AI is collecting your requirements and applying the first changes.</p>
|
||||
<p class="hint">This page will update automatically as the plan is implemented.</p>
|
||||
<p>Runtime: PHP <code><?= htmlspecialchars($phpVersion) ?></code> — UTC <code><?= htmlspecialchars($now) ?></code></p>
|
||||
|
||||
<div class="clock-container">
|
||||
<h1>Control de Presencia</h1>
|
||||
<div id="current-time">--:--:--</div>
|
||||
<div id="status-message">Cargando estado...</div>
|
||||
|
||||
<button id="clock-in-btn" class="btn btn-primary">
|
||||
<i data-feather="log-in"></i>
|
||||
<span>Fichar Entrada</span>
|
||||
</button>
|
||||
|
||||
<button id="clock-out-btn" class="btn btn-danger" disabled>
|
||||
<i data-feather="log-out"></i>
|
||||
<span>Fichar Salida</span>
|
||||
</button>
|
||||
</div>
|
||||
</main>
|
||||
<footer>
|
||||
Page updated: <?= htmlspecialchars($now) ?> (UTC)
|
||||
</footer>
|
||||
|
||||
<script src="assets/js/main.js?v=<?php echo time(); ?>"></script>
|
||||
<script>
|
||||
feather.replace();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
</html>
|
||||
Loading…
x
Reference in New Issue
Block a user