<?php include("parent/callMe.php"); ?>
<?php include("sessioncheck.php");?>
<?php


// ===== this particular code is send the pending sms of all the schools ====

$sql="SELECT msg_from,msg_details,msg_id,msg_sender FROM edu_messages_details where msg_status='1' limit 100 ";

$data = $m->query($sql);

 foreach($data as $value) {
  
  
    $msg_id=$value['msg_id'];
    $numb=$value['msg_from'];
    $sms=$value['msg_details']; 
    $from=$value['msg_sender'];

    if(substr($numb,0,1)=="3"){
    $number1 = "92".$numb;
    }else if(substr($numb,0,1)=="0"){
    $number1 =  "92".substr($numb,1);
    }
    else if(substr($numb,0,2)=="92"){
    $sendmsg1 =  $numb;
    }
    else{
     $number1 = "00";
    }

//======= sms sending Jazz APIs  ==========

  $curl = curl_init();
  curl_setopt_array($curl, array(
    CURLOPT_URL => 'https://connect.jazzcmt.com/sendsms_url.html?Username=03265055739&Password=Jazz@123&From='.$from.'&To='.$number1.'&Message='.urlencode($sms),
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'GET',
  ));
  $response = curl_exec($curl);
  curl_close($curl);



  $data = array('msg_status' => 0);  
  $m->where("msg_id",$msg_id);
  $m->update('edu_messages_details',$data) ;


 } // ending main for loop

//========== sms sending area closed =======



/// This is the code of complete sms sending via ufone APIs

 /*$sql="SELECT msg_from,msg_details,msg_id FROM edu_messages_details where msg_status='1'";

$data = $m->query($sql);

 foreach(array_chunk($data, 10) as $chunkNumber) {
  //echo "<pre>"; print_r($chunkNumber); echo "</pre>"; 
  
  foreach($chunkNumber as $numb => $value) { 
    $msg_id=$value['msg_id'];
    $numb=$value['msg_from'];
    $sms=$value['msg_details']; 

    if(substr($numb,0,1)=="3"){
    $number1 = "92".$numb;
    }else if(substr($numb,0,1)=="0"){
    $number1 =  "92".substr($numb,1);
    }
    else if(substr($numb,0,2)=="92"){
    $sendmsg1 =  $numb;
    }
    else{
     $number1 = "00";
    }



//========================= sms sending ufone APIs  =========================

    $curl = curl_init();

    $url='https://bsms.ufone.com/bsms_v8_api/sendapi-0.3.jsp?id=03348960296&message='.urlencode($sms).'&shortcode=EduPortal&lang=English&mobilenum='.$number1.'&password=UfoneAPI@12&groupname&messagetype=Nontransactional';

      curl_setopt_array($curl, array(
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'GET',
        CURLOPT_HTTPHEADER => array(
          'Cookie: cookiesession1=678B2881QRSTUVXYZABCDEFGHIMN88F6'
        ),
      ));
      $response = curl_exec($curl);
      curl_close($curl);


      $data = array('msg_status' => 0);  
      $m->where("msg_id",$msg_id);
      $m->update('edu_messages_details',$data) ;


        } // ending chunkes loop
    




    
 } // ending main for loop

*/





$date=date('Y-m-d');
$sql = " select * from  eduportal_limton_emp_attendance where attendance_date like '$date%' ";

$WorkAssignListings = $m->query($sql);

     ?>
<!DOCTYPE html>
<html lang="en">
  <head>
    
    <title>View Employees | EduPortal</title>
    <?php include('RootMe/css_links.php');?>
 
<style>

.tdStyle{border-bottom:1px solid lightgray;}
.thStyle{background-color:#00AFEF;color:white;}

</style>  

<script type="text/javascript">

function HideDate(str,empIds){

 
  if(str=="Other"+empIds){
    
 //alert("786");
    document.getElementById("col1"+empIds).style.display="block";
   
  }

  else {
    
  // alert("800");
    document.getElementById("col1"+empIds).style.display="none";
   
   
  }
}
</script>
  </head>
  
  <body class="nav-md">
  
     
     <?php 

     $count=1;
  
      foreach($WorkAssignListings as $WorkAssignListVal)
      { 

        $stringFirstValue=substr($WorkAssignListVal['employee_id'], 0, 1);

         if ($stringFirstValue==1) {
          
          $m->where("emp_biometric_code", $WorkAssignListVal['employee_id']);
          $staffData=$m->get("edu_user_config");

        } elseif($stringFirstValue==2){

          $m->where("std_biometric_id", $WorkAssignListVal['employee_id']);
          $staffData=$m->get("edu_student_details");
        }



        if ($stringFirstValue==1) { // if condition chek staff ids

        if ($staffData[0]['emp_biometric_code']=="" || $staffData[0]['emp_biometric_code']=="NULL") {
          continue;
        }

        // remove first digit in string value
        $removeFirstString=SUBSTR( $staffData[0]['emp_biometric_code'],1); 

        // get first 4 digit in string value
        $getBranchCode=substr($removeFirstString, 0, 4);

        // remove all leading zeros in a string
        $branchID = ltrim($getBranchCode, "0"); 


        // get branch session
        $m->where("branch_id", $branchID);
        $branchSession=$m->get("edu_branch_config");

        // get school timing
        $m->where("branch_id", $branchID);
        $branchTiming=$m->get("eduportal_staff_attendnace_reporting_time");

        $att;
        if ($branchTiming[0]['staff_time_in']>=$WorkAssignListVal['attendance_time']) {
          $att='P';
        } else {
          $att='LA';
        }


        // check duplicate entries

        $m->where ("date", date('Y-m-d'));

        $m->where ("emp_id", $staffData[0]['user_id']);

        $m->where ("session_id", $branchSession[0]['current_session']);

        $m->where ("branch_id", $branchID);       

        $AttendanceAlreadySaved =  $m->get ('eduportal_hrm_emp_attendance');

        if(count($AttendanceAlreadySaved)>0){ 

        continue;

        } else {
        
        $data = array(

      'emp_id' => $staffData[0]['user_id'],

      'attendance' => $att,

      'date' => date('Y-m-d'),

      'branch_id' => $branchID,   

      'session_id' => $branchSession[0]['current_session'],

      'time_in' => $WorkAssignListVal['attendance_time']

      );


      //echo "<pre>"; print_r($data); echo "</pre>";

      $result = $m->insert('eduportal_hrm_emp_attendance',$data);
} // end else condition


 } elseif($stringFirstValue==2) { //elseif chek student ids



      if ($staffData[0]['std_biometric_id']=="" || $staffData[0]['std_biometric_id']=="NULL") {
          continue;
        }

        // remove first digit in string value
        $removeFirstString=SUBSTR( $staffData[0]['std_biometric_id'],1); 

        // get first 4 digit in string value
        $getBranchCode=substr($removeFirstString, 0, 4);

        // remove all leading zeros in a string
        $branchID = ltrim($getBranchCode, "0"); 


        // get branch session
        $m->where("branch_id", $branchID);
        $branchSession=$m->get("edu_branch_config");

        // get school timing
        $m->where("branch_id", $branchID);
        $branchTiming=$m->get("eduportal_staff_attendnace_reporting_time");

        $att;
        if ($branchTiming[0]['student_time_in']>=$WorkAssignListVal['attendance_time']) {
          $att='P';
        } else {
          $att='LA';
        }


         // check duplicate entries

        $m->where ("date", date('Y-m-d'));

        $m->where ("student_id", $staffData[0]['student_id']);     

        $m->where ("branch_id", $branchID);       

        $studentAttendanceAlreadySaved =  $m->get ('eduportal_attendance_detail');

        if(count($AttendanceAlreadySaved)>0){ 

        continue;

        } else {

        
        $data = array(

      'student_id' => $staffData[0]['student_id'],

      'attendance' => $att,

      'status' => '1',

      'class_id' => $staffData[0]['class_id'],   

      'section_id' => $staffData[0]['section_id'],

      'year' => date('Y'),

      'month' => date('m'),

      'date' => date('Y-m-d'),

      'message_status' => '0',

      'branch_Id' => $branchID, 

      'time_in' => $WorkAssignListVal['attendance_time']

      );

      //echo "<pre>"; print_r($data); echo "</pre>";

      $result = $m->insert('eduportal_attendance_detail',$data);
    } // end condition already saved

   }
      ?>




<?php 
      }

$date=date('Y-m-d');
$sql = " DELETE FROM eduportal_limton_emp_attendance where device_number!='C260C8850B312B37' and attendance_date like '$date%' ";
$m->query($sql);


       ?>


<!-- ==============================start whatsapp api code ================================== -->

<?php 



// =================== this particular code is send the pending Whatsapp sms of all the schools ===================

$sql="SELECT msg_id,recipient,msg_details,msg_status,branch_id,attach_file FROM edu_whatsapp_messages_details where msg_status='0' order by msg_id limit 200";

$data = $m->query($sql);

 //foreach(array_chunk($data, 1) as $chunkNumber) {

 // echo "<pre>"; print_r($chunkNumber); echo "</pre>"; 
  
  foreach($data as $value) { 


    // get instance ID again branch

$query="select * from eduportal_branch_configration where branch_id =".$value['branch_id'];
$instanceID = $m->query($query); 

$fromNumber=$instanceID[0]['from_number'];
$accessToken=$instanceID[0]['access_token'];
$instID=$instanceID[0]['instance_id'];


    $msg_id=$value['msg_id'];
    $file=$value['attach_file'];
    $numb=$value['recipient'];     
    $message = str_replace(' ', '%20', $value['msg_details']);
    $message = str_replace('@', '%40', $message);
    $message = str_replace('&', '%26', $message);
    $message = str_replace('$', '%24', $message);
    $message = str_replace('*', '%2A', $message);
    $message = str_replace('#', '%23', $message);
    $message = str_replace(',', '%2C', $message);
    $message = str_replace('"', '%22', $message);
    $message = str_replace('“', '%93', $message);
    $message = str_replace('”', '%94', $message);

    if(substr($numb,0,1)=="3"){

    $to = "92".$numb;

    }else if(substr($numb,0,1)=="0"){

    $to =  "92".substr($numb,1);

    }

    else if(substr($numb,0,2)=="92"){

    // echo   $numb."<br>";

    }
//echo   $to."<br>";

    if ($file!=NULL) {


  $curl = curl_init();
  curl_setopt_array($curl, array(


  CURLOPT_URL => "https://smsknow.com/api/send.php?number=$to&type=media&message=$message&media_url=https://apps.eduportal-pk.com/EduPortal/Production/parent_images/$file&filename=$file&instance_id=$instID&access_token=$accessToken",

  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  ));

  $response = curl_exec($curl);

  curl_close($curl);
  //echo $response;


    } else {


    $curl = curl_init();

    curl_setopt_array($curl, array(
    CURLOPT_URL => "https://smsknow.com/api/send.php?number=$to&type=text&message=$message&instance_id=$instID&access_token=$accessToken",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'POST',
    ));

    $response = curl_exec($curl);

    curl_close($curl);
    //echo $response;


    }
     



      $data = array('msg_status' => 1);  
      $m->where("msg_id",$msg_id);
      $m->update('edu_whatsapp_messages_details',$data) ;


        } // ending chunkes loop
    
 //} // ending main for loop

?>

      
  </body>
</html>
