<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class migrasi extends CI_Model {

	function getKodePN(){
		try {
			$this->db->where('id',61);
			$this->db->select('value');
			$result = $this->db->get('sys_config');
			if($result->num_rows>0){
				return $result->row()->value;
			}else{
				return '';
			}
		} catch (Exception $e) {
			log_message('error', $e);
		}
	}
	
	function exe_script($sql){
		try {

			$counter = intval($this->nativesession->get('counter'));
			$update_progress = intval($this->nativesession->get('update_progress'));
			if($counter>=1){
				$update_progress++;
				$this->nativesession->set('update_progress', $update_progress);
				session_write_close();
				session_start();
				$this->nativesession->set('counter', 0);
				session_write_close();
				session_start();

			}else{
				$counter++;
				$this->nativesession->set('counter', ($counter));
				session_write_close();
				session_start();
			}
			return $this->db->query($sql);
		} catch (Exception $e) {
			return FALSE;
		}
	}

	function update_data($params,$data,$table_name){
		try {
			return $this->db->update($table_name, $data, $params);
		} catch (Exception $e) {
			return FALSE;
		}
	}

	function delete_data($params,$data,$table_name){
		try {
			foreach ($params as $param) {
				$this->db->where($param['column'],$param['value']);
			}
			$this->db->delete($table_name,$data);
		} catch (Exception $e) {
			
		}
	}

	function replace_data($table_name,$data,$datas=''){
		try {
			$this->db->replace($table_name, $data);
			return TRUE;
		} catch (Exception $e) {
			return $e;
		}
	}


	function get_maxid_pihak(){
		
		try {
			$query = $this->db->query('SELECT MAX(id)+1 AS maxid_pihak  FROM pihak');
			return $query;
		} catch (Exception $e) {
			log_message('error', $e);
		}
	}

	function get_list_perkara_id(){
		
		try {
			$query = $this->db->query('SELECT SUBSTRING(GROUP_CONCAT(perkara_id),1,CHAR_LENGTH(GROUP_CONCAT(perkara_id))-1) AS perkara_id FROM perkara');
			return $query;
		} catch (Exception $e) {
			log_message('error', $e);
		}
	}

	function get_list_id_biaya_lama(){
		
		try {
			$query = $this->db->query('SELECT SUBSTRING(GROUP_CONCAT(idjenisbiaya_lama),1,CHAR_LENGTH(GROUP_CONCAT(idjenisbiaya_lama))-1) AS idjenisbiaya_lama FROM idjenisbiaya_lama_baru');
			return $query;
		} catch (Exception $e) {
			log_message('error', $e);
		}
	}


	//--tambah
	function get_maxid_pengacarapihak2(){
		
		try {
			$query = $this->db->query('SELECT MAX(id)+1 AS maxid_pengacara  FROM perkara_pengacara');
			return $query;
		} catch (Exception $e) {
			log_message('error', $e);
		}
	}
	//--tambah end


	function get_id_pihakbaru($idpihaklama){
		
		try {
			$query = $this->db->query('SELECT pihak2_id_baru FROM pihak2_idlama_idbaru WHERE pihak2_id_lama="'.$idpihaklama.'"');
			return $query;
		} catch (Exception $e) {
			log_message('error', $e);
		}
	}


	//tambah
	function get_id_jenisbiaya_baru($idbiayalama){
		
		try {
			$query = $this->db->query('SELECT idjenisbiaya_baru FROM idjenisbiaya_lama_baru WHERE idjenisbiaya_lama="'.$idbiayalama.'"');
			return $query;
		} catch (Exception $e) {
			log_message('error', $e);
		}
	}

	function get_id_jenisbiaya_baru_pa($idbiayalama){
		
		try {
			$query = $this->db->query('SELECT kode_sipp FROM kode_perkiraan_siadpa WHERE kode_siad="'.$idbiayalama.'"');
			return $query;
		} catch (Exception $e) {
			log_message('error', $e);
		}
	}


	function get_id_hakim_baru($nama_hakim_clean){
		
		try {
			$query = $this->db->query('SELECT id_baru FROM hakim_pn_temp2 WHERE nama_clean="'.$nama_hakim_clean.'"');
			return $query;
		} catch (Exception $e) {
			log_message('error', $e);
		}
	}

	function get_id_panitera_baru($nama_panitera_clean){
		
		try {
			$query = $this->db->query('SELECT id_baru FROM panitera_pn_temp2 WHERE nama_clean="'.$nama_panitera_clean.'"');
			return $query;
		} catch (Exception $e) {
			log_message('error', $e);
		}
	}

	//--end


	function cek_perkara(){
		try {
			$query = $this->db->query('SELECT count(*) AS jml_perkara  FROM perkara');
			return $query;
		} catch (Exception $e) {
			log_message('error', $e);
		}
	}

	function isColumnExist($table_name,$column_name){
		try {
			$result = $this->db->query('
				SELECT COLUMN_NAME AS colname
			    FROM information_schema.columns 
			    WHERE TABLE_NAME = "'.$table_name.'"
			    AND COLUMN_NAME = "'.$column_name.'" AND TABLE_SCHEMA = "'.$this->db->database.'" 
			    GROUP BY column_name;
				');
			if($result->num_rows>0){
				return $result->row()->colname;
			}else{
				return '';
			}
		} catch (Exception $e) {
			return FALSE;
		}
	}

	function isKeyExist($table_name,$key_name){
		try {
			$result = $this->db->query('
				SELECT count(*)  AS counter
				FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS  
			    WHERE TABLE_NAME = "'.$table_name.'"
			    AND CONSTRAINT_NAME = "'.$key_name.'" AND CONSTRAINT_SCHEMA = "'.$this->db->database.'";');
			if($result->num_rows>0){
				if($result->row()->counter>0){
					return TRUE;
				}
			}else{
				return '';
			}
		} catch (Exception $e) {
			return FALSE;
		}
	}

	function isTableExist($table_name){
		try {
			return $this->db->table_exists($table_name);
		} catch (Exception $e) {
			return FALSE;
		}
	}

}