<?php
class Depan2 extends CI_Controller {

    function __construct() {
        parent::__construct();
		error_reporting(E_ALL ^ (E_NOTICE | E_WARNING));
		$this->load->helper('nama');
    }
    function index() {
    	$this->uri->segment('2');
			//$this->tarik_data();
			$this->load->view('depan/index2');
    }
	function tarik_data() {
		$result=$this->db->query("SELECT * FROM web_setting WHERE id='1'");
		foreach ($result->result() as $baris)  {
		$jam_masuk=$baris->jam_masuk;
		$batas_jam=$baris->batas_jam;
		$jam_pulang=$baris->jam_pulang;
		}
		$minimal=$jam_masuk;
		$waktu_masuk=$batas_jam;
		$jam_pulang=$jam_pulang;
		$result = $this->db->query("SELECT ip FROM web_komp WHERE publish='Y'");
		$Key="0";
		$hariini = date("Y-m-d");
		//echo $waktu_masuk;
		foreach ($result->result() as $ros) {
			$Connect = @fsockopen($ros->ip, "80", $errno, $errstr, 1);
			if($Connect){
					$soap_request="<GetAttLog><ArgComKey xsi:type=\"xsd:integer\">".$Key."</ArgComKey><Arg><PIN xsi:type=\"xsd:integer\">All</PIN></Arg></GetAttLog>";
					$newLine="\r\n";
					fputs($Connect, "POST /iWsService HTTP/1.0".$newLine);
					fputs($Connect, "Content-Type: text/xml".$newLine);
					fputs($Connect, "Content-Length: ".strlen($soap_request).$newLine.$newLine);
					fputs($Connect, $soap_request.$newLine);
					$buffer="";
					while($Response=fgets($Connect, 1024)){
						$buffer=$buffer.$Response;
					}

				$buffer=Parse_Data($buffer,"<GetAttLogResponse>","</GetAttLogResponse>");
				$buffer=explode("\r\n",$buffer);
				for($a=0;$a<count($buffer);$a++){
					$data=Parse_Data($buffer[$a],"<Row>","</Row>");
					$PIN=Parse_Data($data,"<PIN>","</PIN>");
					$DateTime=Parse_Data($data,"<DateTime>","</DateTime>");
					$Verified=Parse_Data($data,"<Verified>","</Verified>");
					$Status=Parse_Data($data,"<Status>","</Status>");
					$ceknama=$this->db->query("SELECT nip,NM_PEGAWAI as A,KD_JENIS_KETENAGAAN as B,NOMOR_INDUK as C,NOMOR_INDUk_BARU as D,NO_hp as hp,absen FROM web_siswa WHERE absen LIKE '$PIN'");
					foreach ($ceknama->result() as $baris) {
						$nipq=$baris->absen;
						$namapegawai=$baris->A;
					}
					$DateTime=trim($DateTime);
					$aa=explode(' ',$DateTime);
					unset($error,$st,$kett,$telat,$tepat_waktu);
					if (!empty($namapegawai)) {
						$tgl=$aa[0];
						$jam=$aa[1];
						$ip=$ros->ip;
						$cek1=$this->db->query("SELECT status,jam,ket FROM web_absen WHERE tgl='$tgl' AND nip='$nipq' AND status=1");
						$hasilcek1=$cek1->num_rows();
						if ($hasilcek1 ==0) {
							if ($jam <= $waktu_masuk) {
								$st=1;
								$awal=explode(':',$minimal);
								$akhir=explode(':',$jam);
								$dj=$akhir[0]-$awal[0];
								$dm=$akhir[1]-$awal[1];
								$ds=$akhir[2]-$awal[2];
								if ($dj>=0) {
									$telat=($dj>0) ? "$dj jam ":" ";
									$telat.=($dm>0) ? "$dm menit ": " ";
									$telat .=($ds>0) ? "$ds detik ": " ";
								} else {
									$tepat_waktu="selamat pagi, anda datang tepat waktu";
								}
								if ($telat) { $keterangan="Anda terlambat $telat"; $kett="terlambat $telat";}
								if ($tepat_waktu) { $keterangan=$tepat_waktu; $kett="tepat waktu";}
								$this->db->query("REPLACE INTO web_absen(tgl,jam,nip,status,komp,ket) VALUES('$tgl','$jam','$nipq','$st','$ip','$kett')");
							}
						}
						$cek2=$this->db->query("SELECT status,jam,ket FROM web_absen WHERE tgl='$tgl' AND nip='$nipq' AND status=2");
						$hasilcek2=$cek2->num_rows();
						if ($hasilcek2 ==0) {
							if ($jam >= $jam_pulang) {
								$st=2;
								$this->db->query("REPLACE INTO web_absen(tgl,jam,nip,status,komp,ket) VALUES('$tgl','$jam','$nipq','$st','$ip','$kett')");
							}
						}
					}
				}
			}
		}
		//$this->db->query("DELETE FROM `web_absen` WHERE tgl ='0000-00-00'");
	}
	function hapus_data()
	{
		
		$result = $this->db->query("SELECT ip FROM web_komp WHERE publish='Y'");
		$Key="0";
		foreach ($result->result() as $ros) {
			$Connect2 = @fsockopen($ros->ip, "80", $errno, $errstr, 1);
			if($Connect2){
				$soap_request="<ClearData><ArgComKey xsi:type=\"xsd:integer\">".$Key."</ArgComKey><Arg><Value xsi:type=\"xsd:integer\">3</Value></Arg></ClearData>";
				$newLine="\r\n";
				fputs($Connect2, "POST /iWsService HTTP/1.0".$newLine);
				fputs($Connect2, "Content-Type: text/xml".$newLine);
				fputs($Connect2, "Content-Length: ".strlen($soap_request).$newLine.$newLine);
				fputs($Connect2, $soap_request.$newLine);
				$buffer="";
			}
		}
	}
	function peta()
	{
		$tiga=$this->db->query("SELECT * from web_setting where id='1'");
      foreach ($tiga->result() as $rt) {
      $nama_sekolah=$rt->nama;
      $latx=$rt->lat_x;
      $laty=$rt->lat_y;
      }
		  $data['statusserver'] = 'SELAMAT DATANG ADMIN';
  		$data['warna'] = 'success';
  		$data['keterangan'] = 'Aplikasi Siap di gunakan';
      $data['namasek'] = $nama_sekolah;
      $data['koordinat'] = $latx.','.$laty;
		$this->load->view('depan/peta',$data);
	}
}
