<?php

class AppUnitkerja extends Database 
{

	protected $mSqlFile= 'module/unitkerja_tree/business/appunitkerja.sql.php';
	
	public function __construct($connectionNumber=0) 
    {
		/*if($connectionNumber == 0){
 			 $connectionNumber = GTFWConfiguration::GetValue('application', 'gtfinansi_conn');
		}*/
		parent::__construct($connectionNumber);
		$this->idUser = Security::Instance()->mAuthentication->getcurrentuser()->GetUserId();
		//$this->setDebugOn();
	}
	
	function DoAddGtfwUnit($unitkerjaNama){
		$appId = GTFWConfiguration::GetValue('application','application_id');
		$result = $this->Execute($this->mSqlQueries['do_add_gtfw_unit'],array($unitkerjaNama,$appId));
		return $result;
	}
	
	function DoUpdateGtfwUnit($unitkerjaNama,$unitId){
		$result = $this->Execute($this->mSqlQueries['do_update_gtfw_unit'],array($unitkerjaNama,$unitId));
		return $result;
	}
	
    public function GetDataExcel ($unitkerja='', $kode='', $tipeunit='') 
    {
		if($tipeunit != "") 
			$str_tipeunit = " AND unitkerjaTipeUnitId = " . $tipeunit;
		else 
			$str_tipeunit = "";

		$sql = sprintf( $this->mSqlQueries['get_data_excel'],
                        '%'.$kode.'%', 
                        '%'.$kode.'%', 
                        '%'.$unitkerja.'%', 
                        '%'.$unitkerja.'%', 
                        $str_tipeunit);
		
		return $this->Open($sql, array());
	}
   
	public function GetUnitKerja($offset, $limit,$kode,$nama,$type)
    {
		$strplus ='';
		if($type!='')
			$strplus = " AND unitkerjaTipeunitId = '%s'";
		
		$newSql = sprintf($this->mSqlQueries['get_unit_kerja'],'%s','%s',$strplus,'%s','%s',$strplus,'%s','%s');
		
		if($type!='')
			$ret = $this->Open($newSql,
                                array(
                                        '%'.$kode.'%',
                                        '%'.$nama.'%',
                                        $type,
                                        '%'.$kode.'%',
                                        '%'.$nama.'%',
                                        $type,
                                        $offset, 
                                        $limit));
		else
			$ret = $this->Open($newSql,
                                array(
                                        '%'.$kode.'%',
                                        '%'.$nama.'%',
                                        '%'.$kode.'%',
                                        '%'.$nama.'%',
                                        $offset, 
                                        $limit));
      //print_r($ret);
      return $ret;
	}
	//old function

    public function GetQueryKeren($sql,$params) 
    {
		foreach ($params as $k => $v) {
			if (is_array($v)) {
				$params[$k] = '~~' . join("~~,~~", $v) . '~~';
				$params[$k] = str_replace('~~', '\'', addslashes($params[$k]));
			} else {
				$params[$k] = addslashes($params[$k]);
			}
		}
		$param_serialized = '~~' . join("~~,~~", $params) . '~~';
		$param_serialized = str_replace('~~', '\'', addslashes($param_serialized));
		eval('$sql_parsed = sprintf("' . $sql . '", ' . $param_serialized . ');');
		//echo $sql_parsed;
		return $sql_parsed;
	}
		
	public function GetDataUnitkerja ($offset, $limit, $unitkerja='', $kode='', $tipeunit='',$parentId=0) 
    {
		if($tipeunit != "") 
			$str_tipeunit = " AND unitkerjaTipeUnitId = " . $tipeunit;
		else 
			$str_tipeunit = "";

		$sql = sprintf(
					$this->mSqlQueries['get_data_unitkerja'],
							'%'.$kode.'%', 
							'%'.$kode.'%', 
							'%'.$unitkerja.'%', 
							'%'.$unitkerja.'%', 
							$str_tipeunit, 
							$offset, 
							$limit);
		return $this->Open($sql, array());
	}

	public function GetCountDataUnitkerja ($unitkerja='', $kode='', $tipeunit='',$parentId=0) 
    {
		if($tipeunit != "") 
			$str_tipeunit = " AND unitkerjaTipeUnitId = " . $tipeunit;
		else 
			$str_tipeunit = "";
		/*
      if($satker != "") $str_satker = " AND ukr.unitkerjaParentId = $satker ";
		else $str_satker = "";
		if($tipeunit != "") $str_tipeunit = " AND tukr.tipeunitId = $tipeunit ";
		else $str_tipeunit = "";
      */
		$sql = sprintf(
						$this->mSqlQueries['get_count_data_unitkerja'], 
								'%'.$kode.'%', 
								'%'.$kode.'%', 
								'%'.$unitkerja.'%', 
								'%'.$unitkerja.'%',
								 $str_tipeunit);
		$result = $this->Open($sql, array());
	 // $debug = sprintf($this->mSqlQueries['get_count_data_unitkerja'], '%'.$kode.'%', '%'.$unitkerja.'%', $str_satker, $str_tipeunit);
	 // echo $debug;
	  //print_r($result);

		if (!$result) {
			return 0;
		} else {
			return $result[0]['total'];
		}
	}
	 
	public function GetDataUnitkerjaById($unitkerjaId) 
    {
		$result = $this->Open($this->mSqlQueries['get_data_unitkerja_by_id'], array($unitkerjaId));
	
		return $result;
	}

	public function GetDataUnitkerjaByArrayId($arrUnitkerjaId) 
    {
		$unitkerjaId = implode("', '", $arrUnitkerjaId);
		$result = $this->Open($this->mSqlQueries['get_data_unitkerja_by_array_id'], array($unitkerjaId));
		return $result;
	}

	//untuk combo box
	public function GetDataSatker($unitkerjaId = NULL) 
    {
		$sql_params = (empty($unitkerjaId) ? "" : " WHERE unitkerjaId='".$unitkerjaId."'");
		$sql = sprintf($this->mSqlQueries['get_data_satker'],$sql_params);
		$result = $this->Open($sql, array());
		return $result;
	}

	public function GetDataTipeUnit($unitkerjaId = NULL) 
    {
		$result = $this->Open($this->mSqlQueries['get_data_tipe_unit'], array());
		return $result;
	}
	
	public function GetStatusUnitKerja()
    {
		return $this->Open($this->mSqlQueries['get_status_unit_kerja'],array());
	}

//===DO==
	public function GetGenerateKodeSistem($unitKerjaParent=0)
	{
		$result = $this->Open($this->mSqlQueries['generate_kode_sistem'], 
							array(
							$unitKerjaParent,$unitKerjaParent,$unitKerjaParent,
							$unitKerjaParent,$unitKerjaParent,$unitKerjaParent,
							$unitKerjaParent
							));
		return $result[0]['kode'];
	}
    
	public function DoAddUnitkerja($pimpinan,$unitkerjaKode,$unitkerjaNama,$tipeunit,$statusunit,$parentId=0) 
    {	
      if($parentId == '' or empty($parentId)) $parentId = '0';
      	$kodeSistem  = $this->GetGenerateKodeSistem($parentId);
		$result = $this->Execute($this->mSqlQueries['do_add_unitkerja'], 
						array(
								$unitkerjaKode, 
								$kodeSistem,
								$unitkerjaNama, 
								$tipeunit, 
								$statusunit, 
								$parentId, 
								$pimpinan));
		return $result;
	}
	
	public function DoUpdateUnitkerja($pimpinan, $unitkerjaKode, $unitkerjaNama, $tipeunit, 
                $statusunit, $parentId=0, $unitkerjaId) 
    {
		//$this->setDebugOn();
      if($parentId == '' or empty($parentId)) $parentId = '0';
      
      $get_unit_kode_sistem = $this->GetKodeSistem($unitkerjaId);
      
      $kodeSistemLama =  $get_unit_kode_sistem['kode_sistem'];
      
      if($parentId != $get_unit_kode_sistem['parent_id']){
            $kodeSistem = $this->GetGenerateKodeSistem($parentId);
      } else {
            $kodeSistem = $get_unit_kode_sistem['kode_sistem'];
      }
      
      $kodeSistemBaru = $kodeSistem;
      
		$this->StartTrans();
		$result = $this->Execute($this->mSqlQueries['do_update_unitkerja'], 
                                    array(
                                            $unitkerjaKode, 
                                            /*$kodeSistem,*/
                                            $unitkerjaNama, 
                                            $tipeunit, 
                                            $statusunit,
                                             $parentId, 
                                             $pimpinan, 
                                             $unitkerjaId));
        if($result && ($kodeSistemLama != $kodeSistemBaru)){
			$result = $this->Execute($this->mSqlQueries['do_update_kode_sistem'], 
                                    array(
                                            $kodeSistemBaru,
                                            $kodeSistemLama,
                                            $kodeSistemLama.'.%',
                                            $kodeSistemLama
                                            ));
		}
		$this->EndTrans($result);
		return $result;
	}
	
	public function DoDeleteUnitkerjaById($unitkerjaId) 
    {
		$result=$this->Execute($this->mSqlQueries['do_delete_unitkerja_by_id'], 
                                    array(
                                            $unitkerjaId,
                                            $unitkerjaId));
		
		return $result;
	}
	public function DoDeleteUnitkerjaByArrayId($arrUnitkerjaId) 
    {
		$unitkerjaId = implode("', '", $arrUnitkerjaId);
		$result=$this->Execute($this->mSqlQueries['do_delete_unitkerja_by_array_id'], 
                                    array(
                                            $unitkerjaId,
                                            $unitkerjaId));
		
		return $result;
	}

   public function GetComboUnitKerja()
   {
      return $this->Open($this->mSqlQueries['get_combo_unit_kerja'],array());
   }
   
   public function cekUnitParent($parentId)
   {
      $total = $this->Open($this->mSqlQueries['cek_unit_parent'],array($parentId));
      return $total[0]['total'];
   }
   
   public function GetKodeSistem($unitId)
   {
        $kode_sistem = $this->Open($this->mSqlQueries['get_kode_sistem'],array($unitId));
        return $kode_sistem[0];
   }
   
   //function GetUnitKerjaByParentId($parentId = 0,$namaUnit='',$tipe='',$kode='')
   public function GetUnitKerjaByParentId($parentId = 0)
   {
    /**
          if($tipe != ''){
            $sql = " AND (c.`unitkerjaTipeunitId` = ".$tipe." OR m.`unitkerjaTipeunitId` = ".$tipe.")";
          } else {
            $sql ="";
          }
          */
          if(empty($parentId)){
            $parentId = 0;
         }
         
         $query = sprintf($this->mSqlQueries['get_unit_kerja_by_parent_id'],$parentId);
         /**
         $query = sprintf($this->mSqlQueries['get_unit_kerja_by_parent_id_v2'],
                                    '%'.$namaUnit.'%',
                                    '%'.$namaUnit.'%',
                                    '%'.$kode.'%',
                                    '%'.$kode.'%',
                                    $sql,
                                    '%'.$namaUnit.'%',
                                    '%'.$namaUnit.'%',
                                    '%'.$kode.'%',
                                    '%'.$kode.'%',
                                    $sql,
                                    $parentId);
                                    */
        return $this->Open($query,array());
    }
   
    public function GetCountChild($parentId = 0)
    {
        if(empty($parentId)){
            $parentId = 0;
         }
        $result = $this->Open($this->mSqlQueries['get_count_child'],array($parentId));
        return $result[0]['total'];
    }
}