« Back to History
attendance_api.php
|
20260723_000646.php
Initial Domain Snapshot
Copy Code
<?php /* ============================================================================ Attendance API — IST timezone, geofence STATUS + MARK (in/out) File: /erp/modules/attendance/attendance_api.php ============================================================================ */ error_reporting(E_ALL); ini_set('display_errors', 1); header('Content-Type: application/json'); require __DIR__ . '/../auth/auth.php'; require_login(); $u = auth_user(); $company_id = (int)$u['company_id']; $user_id = (int)$u['id']; $pdo = $GLOBALS['pdo'] ?? null; if (!$pdo) { require __DIR__ . '/../../core/db.php'; $pdo = $GLOBALS['pdo'] ?? null; } /* ---- IST for this module only ---- */ date_default_timezone_set('Asia/Kolkata'); try { $pdo && $pdo->exec("SET time_zone = '+05:30'"); } catch(Throwable $e){} function json_out($a){ echo json_encode($a); exit; } function haversine_m($lat1,$lon1,$lat2,$lon2){ $R=6371000.0; $dLat=deg2rad($lat2-$lat1); $dLon=deg2rad($lon2-$lon1); $a=sin($dLat/2)**2 + cos(deg2rad($lat1))*cos(deg2rad($lat2))*sin($dLon/2)**2; return 2*$R*atan2(sqrt($a),sqrt(1-$a)); } /* ---- fetch active geofences ---- */ $gf = $pdo->prepare("SELECT id,name,latitude,longitude,radius_m FROM company_locations WHERE company_id=? AND is_active=1 ORDER BY id ASC"); $gf->execute([$company_id]); $geofences = $gf->fetchAll(PDO::FETCH_ASSOC); /* ---- router ---- */ $action = strtolower((string)($_REQUEST['action'] ?? '')); /* STATUS: return today’s record + geofences */ if ($action === 'status' || $_SERVER['REQUEST_METHOD']==='GET') { $today = (new DateTime('today', new DateTimeZone('Asia/Kolkata')))->format('Y-m-d'); $sel = $pdo->prepare("SELECT * FROM attendace_data WHERE company_id=? AND user_id=? AND work_date=?"); $sel->execute([$company_id,$user_id,$today]); $row = $sel->fetch(PDO::FETCH_ASSOC) ?: null; json_out(['ok'=>true, 'work_date'=>$today, 'record'=>$row, 'geofences'=>$geofences]); } /* MARK: in/out with basic server-side geofence check */ if ($action === 'mark' && $_SERVER['REQUEST_METHOD']==='POST') { if (!$geofences) json_out(['ok'=>false,'error'=>'No active geofence configured for this company.']); $type = strtolower((string)($_POST['type'] ?? '')); $lat = (float)($_POST['lat'] ?? 0); $lng = (float)($_POST['lng'] ?? 0); $acc = (float)($_POST['accuracy'] ?? 99999); if (!in_array($type,['in','out'],true)) json_out(['ok'=>false,'error'=>'Invalid type']); // nearest geofence $nearest=null; $min=PHP_FLOAT_MAX; foreach($geofences as $g){ $d = haversine_m($lat,$lng,(float)$g['latitude'],(float)$g['longitude']); if ($d < $min){ $min=$d; $nearest=$g; } } $allowed = $nearest ? ($min <= ((int)$nearest['radius_m']+25)) : false; if (!$allowed) json_out(['ok'=>false,'error'=>'Outside company location']); $now = (new DateTime('now', new DateTimeZone('Asia/Kolkata')))->format('Y-m-d H:i:s'); $today = (new DateTime('today', new DateTimeZone('Asia/Kolkata')))->format('Y-m-d'); // ensure row $pdo->prepare("INSERT IGNORE INTO attendace_data (company_id,user_id,work_date) VALUES (?,?,?)")->execute([$company_id,$user_id,$today]); if ($type==='in') { $pdo->prepare("UPDATE attendace_data SET in_time=IFNULL(in_time, ?) WHERE company_id=? AND user_id=? AND work_date=?") ->execute([$now,$company_id,$user_id,$today]); } else { $pdo->prepare("UPDATE attendace_data SET out_time=IFNULL(out_time, ?) WHERE company_id=? AND user_id=? AND work_date=?") ->execute([$now,$company_id,$user_id,$today]); } // return fresh record + nearest geofence $sel = $pdo->prepare("SELECT * FROM attendace_data WHERE company_id=? AND user_id=? AND work_date=?"); $sel->execute([$company_id,$user_id,$today]); $row = $sel->fetch(PDO::FETCH_ASSOC); json_out(['ok'=>true,'record'=>$row,'nearest'=>$nearest,'distance_m'=>round($min)]); } json_out(['ok'=>false,'error'=>'Unsupported request']);