Files
tthcmservices/getdatadevicesbyip.php
2025-08-28 13:09:05 +07:00

363 lines
17 KiB
PHP
Executable File

<?php
include('zklib/ZKLib.php');
class LogController
{
// $ppHCMTT#2024
public function fn_getconn(){
$pdo = new PDO("mysql:host=localhost;dbname=dbhcm", "apphcm", "\$ppHCMTT#2024");
return $pdo;
}
public function selectquerySP($sql){
// $pdo = new PDO("mysql:host=".DB_HOST_NAME.";dbname=".DB_IMS, DB_USER_NAME, DB_USER_PASS);
$pdo = $this->fn_getconn(); //new PDO("mysql:host=localhost;dbname=dbhcm", "apphcm", "\$pp");
$q = $pdo->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);
return $q;
}
public function executeSP($qry){
$rtn=false;
$pdo = $this->fn_getconn();
$statement = $pdo->prepare($qry);
if($statement->execute()){
$rtn=true;
}
return $rtn;
}
public function fn_getfieldvalue($fieldname,$qry){
$rs=$this->selectquerySP($qry);
$rtn="";
if($rs){
// $rw=$rs->fetch();
// $rtn=$rw[$fieldname];
while($rw=$rs->fetch()){
$rtn=$rw[$fieldname];
}
}
return $rtn;
}
public function fn_getfieldsvalues($fieldname,$qry){
try {
$rs=$this->selectquerySP($qry);
if($rs->rowCount()>0){
$rtn="";
if($rs){
while($rw=$rs->fetch()){
if($rw){
$d=explode(",",$fieldname);
for($i=0;$i<count($d);$i++){
$rtn.=$rw[$d[$i]].",";
}
}
}
// echo $rtn;return false;
if($rtn){
$rtn=substr($rtn,0,strlen($rtn)-1);
}
else{
$rtn=",,,,,,,,,,,,,,,,,,,";
}
}
}
else{
$rtn=",,,,,,,,,,,,,,,,,,,,,";
}
return explode(",",$rtn);
} catch (Exception $e) {
//throw $th;
return explode(",",",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,");
}
}
public function index()
{
$ip = "172.16.116.5"; //$row['deviceip'];
$qry="select * from tbl_deviceinfo where isreprocess=1 and isdeleted=0 order by _idx asc";
#echo $qry;
$rs=$this->selectquerySP($qry);
if($rs){
while($rw=$rs->fetch()){
$ip=$rw['deviceip'];
$zk = new ZKLib($ip);
echo "Start time : ".date("Y-m-d H:i:s")."\n";
echo $ip."\n";
// die();
$allData = [];
$connectStatus = $zk->connect();
// echo $connectStatus;
if ($connectStatus) {
try {
$zk->disableDevice();
// $zk->clearAttendance();
$users = $zk->getUser();
$getData = $zk->getAttendance();
// echo json_encode($getData);
$data = [];
foreach ($getData as $att) {
// echo date("Y-m-d", strtotime($att['timestamp']));
if (date("Y-m-d", strtotime($att['timestamp'])) == date("Y-m-d",strtotime($rw['reprocessdate']))) {
// $data[] = [
// "idUser" => $att['id'],
// "name" => isset($users[$att['id']]) ? $users[$att['id']]['name'] : $att['id'],
// "state" => ZK\Util::getAttState($att['state']),
// "date" => date("Y-m-d", strtotime($att['timestamp'])),
// "time" => date("H:i:s", strtotime($att['timestamp'])),
// "deviceIP" => $ip
// ];
// echo json_encode($att)."\n";
// $res=$this->fn_setattendance($att, $ip);
$reslog=$this->fn_setattendancelogs($att, $ip);
$data[] = [
"fingerid" => $att['id'],
"time" => date("Y-m-d H:i:s", strtotime($att['timestamp'])),
"status" => $reslog
];
}
}
$allData = array_merge($allData, $data);
} catch (Exception $e) {
echo $e->getMessage();
} finally {
$zk->disconnect();
}
}
echo "End time : ".date("Y-m-d H:i:s")."\n\n";
}
}
// header('Content-Type: application/json');
return json_encode($allData);
}
public function fn_setattendancelogs($data, $ip){
$rtn=false;
$dates=date("Y-m-d", strtotime($data['timestamp']));
$times=date("H:i:s", strtotime($data['timestamp']));
$qry="insert into tbl_attendancelogs
set fingerid='".$data['id']."',username='".$data['id']."',dates='$dates',times='$times',deviceip='$ip',iby='svc',idt=now()";
$qryexists="select _idx from tbl_attendancelogs
where trim(fingerid)=trim('".$data['id']."')
and dates='$dates' and times='$times' and isdeleted=0
order by _idx desc limit 1";
$idx=$this->fn_getfieldvalue("_idx",$qryexists);
if($idx){
$qry="update tbl_attendancelogs
set udt=now()
where _idx='$idx'";
}
if($this->executeSP($qry)){
$rtn=true;
}
return $rtn;
}
public function fn_setattendanceold($data, $ip){
$qryexists="select _idx,deviceid,deviceidcheckout from tbl_attendanceemployee
where trim(fingerid)=trim('".$data['id']."') and cast(dates as date)=cast('".date("Y-m-d", strtotime($data['timestamp']))."' as date)
and isdeleted=0 and attendancename='IN'
order by _idx asc limit 1";
$idx=$this->fn_getfieldvalue("_idx",$qryexists);
}
public function fn_setattendance($data, $ip){
$rtn=false;
$months=(int)date("m", strtotime($data['timestamp']));
$years=(int)date("Y", strtotime($data['timestamp']));
$timestamp=(int)date("Hi", strtotime($data['timestamp']));
$datestimes=date("Y-m-d H:i:s", strtotime($data['timestamp']));
$qry="select e._idx idxemployee,e.nik,e.employeename,e.fingerid,t.idxdept,t.deptname,t.attendancename,t.starttimestr,t.starttime,
t.endtimestr,t.endtime,t.thresholdtimestr,t.thresholdtime,t.shiftcode
from tbl_employee e inner join tbl_attendancethreshold t on e.idxdept=t.idxdept
where e.isdeleted=0 and t.isdeleted=0 and e.fingerid='".$data['id']."'
and ('$timestamp'>=t.starttime and '$timestamp'<=t.thresholdtime)
order by t._idx asc limit 1";
$sp=$this->fn_getfieldsvalues("idxdept,idxemployee", $qry);
$idxdeptemp=$sp[0];$idxemployee=$sp[1];
if($idxdeptemp==33||$idxdeptemp==34){
$sp=$this->fn_getfieldsvalues("shiftcategory,idxemployee","select shiftcategory from tbl_lojaschedule where cast(dates as date)=cast(now() as date) and idxemployee='$idxemployee'");
$shiftcode=$sp[0];$idxemployee=$sp[1];
$qry="select e._idx idxemployee,e.nik,e.employeename,e.fingerid,t.idxdept,t.deptname,t.attendancename,t.starttimestr,t.starttime,
t.endtimestr,t.endtime,t.thresholdtimestr,t.thresholdtime,t.shiftcode
from tbl_employee e inner join tbl_attendancethreshold t on e.idxdept=t.idxdept
where e.isdeleted=0 and t.isdeleted=0 and e.fingerid='".$data['id']."' and t.shiftcode='$shiftcode'
and ('$timestamp'>=t.starttime and '$timestamp'<=t.thresholdtime)
order by t._idx asc limit 1";
}
// echo $qry."\n\nnndd";
$rs=$this->selectquerySP($qry);
if($rs){
while($rw=$rs->fetch()){
$islate=0;$shiftcode=$rw['shiftcode'];
// if($rw['idxdept']==30||$rw['idxdept']==22){
if((int)$rw['idxdept']==30){
$qrysc="select distinct t.*
from tbl_attendancethreshold t inner join tbl_callcenterschedule s on t.idxshift=s.idxshift and t.idxdept='".$rw['idxdept']."'
where s.idxemployee='".$rw['idxemployee']."' and t.attendancename='IN'
and cast(s.dates as date)=cast('".date("Y-m-d", strtotime($data['timestamp']))."' as date)
and t.isdeleted=0 and s.isdeleted=0 order by t._idx desc limit 1";
$endtimecc=$this->fn_getfieldvalue("endtime",$qrysc);
if($endtimecc){
if((int)$timestamp>(int)$endtimecc&&$rw['attendancename']=='IN'){
$islate=1;
}
}
}
elseif((int)$rw['idxdept']==22){
$qrysc="select distinct t.*
from tbl_attendancethreshold t inner join tbl_supervisaoschedule s on t.idxshift=s.idxshift and t.idxdept='".$rw['idxdept']."'
where s.idxemployee='".$rw['idxemployee']."' and t.attendancename='IN'
and cast(s.dates as date)=cast('".date("Y-m-d", strtotime($data['timestamp']))."' as date)
and t.isdeleted=0 and s.isdeleted=0 order by t._idx desc limit 1";
$endtimecc=$this->fn_getfieldvalue("endtime",$qrysc);
if($endtimecc){
if((int)$timestamp>(int)$endtimecc&&$rw['attendancename']=='IN'){
$islate=1;
}
}
}
elseif((int)$rw['idxdept']==33||(int)$rw['idxdept']==34){
$qrysc="select distinct t.*
from tbl_attendancethreshold t inner join tbl_lojaschedule s on t.idxshift=s.idxshift and t.idxdept='".$rw['idxdept']."'
where s.idxemployee='".$rw['idxemployee']."' and t.attendancename='IN'
and cast(s.dates as date)=cast('".date("Y-m-d", strtotime($data['timestamp']))."' as date)
and t.isdeleted=0 and s.isdeleted=0 order by t._idx desc limit 1";
$endtimecc=$this->fn_getfieldvalue("endtime",$qrysc);
if($endtimecc){
if((int)$timestamp>(int)$endtimecc&&$rw['attendancename']=='IN'){
$islate=1;
}
}
}
else{
if((int)$timestamp>(int)$rw['endtime']&&$rw['attendancename']=='IN'){
$islate=1;
}
}
// $qryexists="select _idx,deviceid,deviceidcheckout from tbl_attendanceemployee
// where trim(fingerid)=trim('".$data['id']."') and cast(dates as date)=cast('".date("Y-m-d", strtotime($data['timestamp']))."' as date)
// and isdeleted=0 and attendancename='".$rw['attendancename']."'
// order by _idx asc limit 1";
// $idx=$this->fn_getfieldvalue("_idx",$qryexists);
$qryexists="select _idx,deviceid,deviceidcheckout from tbl_attendanceemployee
where trim(fingerid)=trim('".$data['id']."') and cast(dates as date)=cast('".date("Y-m-d", strtotime($data['timestamp']))."' as date)
and isdeleted=0 and attendancename='".$rw['attendancename']."'
order by _idx asc limit 1";
$idx=$this->fn_getfieldvalue("_idx",$qryexists);
// echo $qryexists;
// $sp=$this->fn_getfieldsvalues("_idx,deviceid,deviceidcheckout",$qryexists);
// $idx=$sp[0];$deviceidcheckin=$sp[1];$deviceidcheckout=$sp[2];
// echo $idx." ".$deviceidcheckin." ".$deviceidcheckout."\n";return false;
// $deviceidcheckin="";$deviceidcheckout="";
$qry="select deviceid from tbl_deviceinfo where trim(deviceip)=trim('$ip') and isdeleted=0 order by _idx desc limit 1";
$deviceid=$this->fn_getfieldvalue("deviceid",$qry);
if($idx){
$qry="update tbl_attendanceemployee
set udt=now()
where _idx='$idx'";
}
else{
// if(!$deviceidcheckin||is_null($deviceidcheckin)){$deviceidcheckin=$deviceid;}
$swheredeviceid="deviceid='".$deviceid."',";
$swheredeviceid="";
// if($rw['attendancename']=='OUT'){
// if(!$deviceidcheckout||is_null($deviceidcheckout)){$deviceidcheckout=$deviceid;}
// // $swheredeviceid="deviceidcheckout='".$deviceidcheckout."',";
// // ==== Get Device ID Checkin
// $qrygetdevicecheckin="select deviceid from tbl_attendanceemployee
// where idxemployee='".$rw['idxemployee']."' and attendancename='IN'
// and cast(dates as date)=cast('".date("Y-m-d", strtotime($data['timestamp']))."' as date)
// order by _idx desc limit 1";
// $deviceidcheckin=$this->fn_getfieldvalue("deviceid",$qrygetdevicecheckin);
// }
$qry="insert into tbl_attendanceemployee
set fingerid='".$data['id']."',fingerip='".$ip."',".$swheredeviceid."idxemployee='".$rw['idxemployee']."',
nik='".$rw['nik']."',employeename='".$rw['employeename']."',dates='".date("Y-m-d", strtotime($data['timestamp']))."',
times='".date("H:i:s", strtotime($data['timestamp']))."',datestimes='$datestimes',attendancename='".$rw['attendancename']."',thresholdtime='".$rw['thresholdtime']."',
thresholdtimestr='".$rw['thresholdtimestr']."',shiftcode='".$shiftcode."',months='$months',years='$years',islates='$islate',iby='".$rw['nik']."',idt=now()";
}
echo $qry."\n";
if($deviceid=='172.16.110.251'){
echo $qry."\n";
}
if($this->executeSP($qry)){
// if($rw['attendancename']=='OUT'){
// $qry="update tbl_attendanceemployee set deviceid='$deviceidcheckin',deviceidcheckout='$deviceidcheckout'
// where trim(fingerid)=trim('".$data['id']."') and cast(dates as date)=cast('".date("Y-m-d", strtotime($data['timestamp']))."' as date)
// and isdeleted=0";
// $this->executeSP($qry);
// }
$rtn=true;
}
}
}
return $rtn;
}
public function fn_curl()
{
$url="http://localhost/tthcm/getdatadevices.php?isgetdata=1";
$method="GET";
$contenttype="application/json";
// $data=array("")
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0");
// if($method=='POST')curl_setopt($ch, CURLOPT_POST, true);
if ($method == 'POST' || $method == 'PUT') curl_setopt($ch, CURLOPT_POST, true);
if ($method == 'DELETE') curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
if ($method == 'GET') curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
// if($data)curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
// curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
// curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: ' . $contenttype,
'x-token: '
));
$res = curl_exec($ch);
curl_close($ch);
return $res;
// $arr = json_decode($res, true);
// return $arr;
}
}
$cl = new LogController();
$rtn =$cl->index();
#if(isset($_REQUEST['isgetdata'])){
# echo "indx";
# $rtn = $cl->index();
#}
#else{
# $rtn = $cl->fn_curl();
#}
echo $rtn;