
<?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){
        $rs=$this->selectquerySP($qry);
        $rtn="";
        if($rs){
            // $rw=$rs->fetch();
            // $rtn=$rw[$fieldname];
            while($rw=$rs->fetch()){
                $d=explode(",",$fieldname);
                for($i=0;$i<count($d);$i++){
                    $rtn.=$rw[$d[$i]].",";
                }
                
            }
            // echo $rtn;return false;
            $rtn=substr($rtn,0,strlen($rtn)-1);
        }
        return explode(",",$rtn);
    }


    public function index(){
        $rtn=false;
        //$qry="select * from tbl_attendancelist where isdeleted=0 and nik in('000188','000162','000453','000737')";
        $qry="select * from tbl_attendancelist where (cast(idt as date)=cast(now() as date) and isdeleted=0) or duration is null";
        $rs=$this->selectquerySP($qry);
        if($rs){
            while($rw=$rs->fetch()){
                $idxdept=$rw['idxdept'];
                if($idxdept==33||$idxdept==34||$idxdept==30||$idxdept==31||$idxdept==22){
                    $qry="update tbl_attendancelist 
                          set duration=SEC_TO_TIME(TIMESTAMPDIFF(SECOND,checkin,checkout)) 
                          where _idx='".$rw["_idx"]."'";      
                }
                else{
                    $qry="update tbl_attendancelist 
                          set duration=SEC_TO_TIME(TIMESTAMPDIFF(SECOND,checkin,R1)+TIMESTAMPDIFF(SECOND,R2,checkout)) 
                          where _idx='".$rw["_idx"]."'";  
                }
                echo $qry."\n";
                $this->executeSP($qry);

            }
        }
        return true;
    }



}

$cl = new LogController();
$rtn =$cl->index();

#if(isset($_REQUEST['isgetdata'])){
#   echo "indx";
#    $rtn = $cl->index();
#}
#else{
#    $rtn = $cl->fn_curl();
#}

echo $rtn;
