if(true){ $device_ip = $device['ip']; $device_port = $device['port']; $device_modal = $device['deviceModel']; $system_ip = $device_ip; $end_point = "https://ds.creativegarage.org/mangla_green_housing/web/attendance/fetch_api.php?system_ip=$ip"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $end_point); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); $response = curl_exec($ch); if (curl_errno($ch)) { die('Error with cURL: ' . curl_error($ch)); // If error occurs with cURL } curl_close($ch); $attendances = json_decode($response, true); // echo json_encode($attendances);exit; if (!empty($attendances)) { $mysqli->begin_transaction();// Begin the transaction so that we can roll back in case of error loading attendance $device_id = $device['id']; $school_id = $device['school']; $company_id = $device['companyId']; $location_id = $device['locationId']; $uuid = $device['uuid']; $new_count = 0; $companyId = 1; if($system =="mangla") { $companyId = 1; } else if($system =="lahore") { $companyId = 2; } foreach ($attendances as $attendance) { $uid = $attendance['id']; $state = $attendance['state']; $timestamp = $attendance['timestamp']; $status = "Present"; $deviceId = $uid; $uid = filter_var($uid, FILTER_VALIDATE_INT); if($uid !== false && $uid > 0) { // $deviceDate = date('Y-m-d', strtotime($timestamp)); $sql_check = "SELECT COUNT(*) FROM attendance_import WHERE userId = :user_id AND companyId = :companyId AND deviceTime = :deviceDate"; $existingRecord = Yii::$app->db->createCommand($sql_check) ->bindValue(':companyId', $companyId) ->bindValue(':user_id', $uid) ->bindValue(':deviceDate', $timestamp) ->queryScalar(); $stmt = $mysqli->prepare("INSERT INTO attendance_import (uuid, userId, companyId, state, deviceId, deviceTime) VALUES ($uid, $id, $companyId, $state, $deviceId, '".$timestamp."')"); if ($existingRecord < 1) { $sql_insert = "IINSERT INTO attendance_import ( uuid, userId, companyId, state, deviceId, deviceTime ) VALUES ( :uuid, :userId,:company_id, :state, :deviceId, :deviceTime )"; Yii::$app->db->createCommand($sql_insert) ->bindValue(':uuid', $uuid) ->bindValue(':userId', $uid) ->bindValue(':company_id', $company_id) ->bindValue(':state', $state) ->bindValue(':deviceId', $deviceId) ->bindValue(':deviceTime', $timestamp) ->execute(); $new_count++; } } } $transaction->commit(); // $message = count($attendances)." Attendance Records Loaded!"; $message = $new_count." new entries Loaded!"; echo ""; exit; } else { $transaction->rollBack(); echo ""; exit; } }