« Back to History
attendance_sync.php
|
20260721_154032.php
Initial Bulk Import
Copy Code
<?php require_once __DIR__ . '/modules/auth/page_acl.php'; $ctx = page_require_access('attendance_sync'); $pdo = $ctx['pdo']; $company_id = (int)$ctx['company_id']; // ========================== // 🔐 AUTH // ========================== $auth_raw = "GANPATITEXTILE:GANPATITEXTILE:Ganpati@123:true"; $auth = base64_encode($auth_raw); // ========================== // 📅 DATE RANGE (last 2 days safe) // ========================== $from_dt = new DateTime('-32 days'); $to_dt = new DateTime(); $from = urlencode($from_dt->format('d/m/Y') . "_00:00"); $to = urlencode($to_dt->format('d/m/Y') . "_23:59"); // ========================== // 🔗 API URL // ========================== $url = "https://api.etimeoffice.com/api/DownloadPunchData?Empcode=ALL&FromDate=$from&ToDate=$to"; // ========================== // 📡 API CALL // ========================== $ch = curl_init(); curl_setopt_array($ch, [ CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => [ "Authorization: Basic $auth" ], CURLOPT_TIMEOUT => 30 ]); $response = curl_exec($ch); if (curl_errno($ch)) { die("API Error: " . curl_error($ch)); } curl_close($ch); // ========================== // 🧠 JSON // ========================== $data = json_decode($response, true); if (!$data || !empty($data['Error'])) { die("API Error: " . ($data['Msg'] ?? 'Invalid Response')); } if (empty($data['PunchData'])) { echo "No new data"; exit; } // ========================== // 💾 INSERT // ========================== $inserted = 0; foreach ($data['PunchData'] as $row) { $emp_code = trim($row['Empcode'] ?? ''); $punch_time = trim($row['PunchDate'] ?? ''); $mflag = trim($row['M_Flag'] ?? ''); if (!$emp_code || !$punch_time) continue; $dt = DateTime::createFromFormat('d/m/Y H:i:s', $punch_time); if (!$dt) continue; $mysql_time = $dt->format('Y-m-d H:i:s'); $stmt = $pdo->prepare(" INSERT IGNORE INTO attendance_logs (company_id, employee_code, punch_time, punch_type, raw_data) VALUES (:cid, :emp, :ptime, :ptype, :raw) "); $stmt->execute([ ':cid' => $company_id, ':emp' => $emp_code, ':ptime' => $mysql_time, ':ptype' => $mflag ? 'MANUAL' : 'AUTO', ':raw' => json_encode($row) ]); if ($stmt->rowCount()) $inserted++; } echo "Sync Done. Inserted: $inserted";