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

class M_region extends CI_Model {
	
	// deklarasi nama table
	public $table="am_region";
	
	function get_all(){
		$Code = $this->input->post('RegionCode') ? $this->input->post('RegionCode') : '';
        $Name = $this->input->post('RegionName') ? $this->input->post('RegionName') : '';
		
		$this->db->select('*');
		$this->db->from($this->table);
        $this->db->where_in('am_region.TransStatus',array(0,1));
		$this->db->like('am_region.RegionCode',$Code);
		$this->db->like('am_region.RegionName',$Name); 
		$query 	= $this->db->get();
		$result	= $query->result();
		return $result;
	}
	
	function check_code_exist($txt_regioncode){
		$query = $this->db->get_where($this->table, array('RegionCode' => $txt_regioncode), 1, 0);
		
		if ($query->num_rows() > 0) {
			return TRUE;
		}
		else {
			return FALSE;
		}
	}
	
	function get_by_id($region_code){
		$query = $this->db->get_where($this->table, array('RegionCode' => $region_code));
        return $query->result();
	}
	
	function insert($data){
//		$this->db->trans_begin();
		$this->db->insert($this->table, $data);
//		
//		if ($this->db->trans_status() === FALSE) {
//			$this->db->trans_rollback();
//			return 1;
//		}
//		else {
//			$this->db->trans_commit();
//			return 0;
//		}
	}
	
	function update($data, $region_code){
//		$this->db->trans_begin();//ga dipakai karena pakai array
		$this->db->update($this->table, $data, array('RegionCode' => $region_code));
//		
//		if ($this->db->trans_status() === FALSE) {
//			$this->db->trans_rollback();
//			return 1;
//		}
//		else {
//			$this->db->trans_commit();
//			return 0;
//		}
	}
	
	// Function Delete	
	function delete($id) {
//		$this->db->trans_begin();// ga dipakai karena pakai array
        $this->db->delete($this->table, array('RegionCode' => $id));
//		
//		if ($this->db->trans_status() === FALSE) {
//			$this->db->trans_rollback();
//			return 1;
//		}
//		else {
//			$this->db->trans_commit();
//			return 0;
//		}
	}
	
	function create_new_id(){
		$RegionCode = 'RG-';
		
		// Get Last Region Code
        $this->db->like('RegionCode', $RegionCode);
        $num            = $this->db->get( $this->table )->num_rows()+1;
        $long           = strlen($num);
		
		// Create Region Code
        if($long == 1){
            $number='00'.$num;
        }
        elseif($long == 2){
            $number='0'.$num;
        }
        else{
            $number=$num;
        }
		
		// Return new Region Code
        return  $RegionCode . $number;
	}
	
    //============= DHIDHIK, 28 Maret 2016 ===============================//
    function datasend_region_insert($dataSend){         
        $this->db->trans_start();  
        $this->insert($dataSend['data_region']);
        $this->db->trans_complete();
        if($this->db->trans_status() == FALSE){
            $result  = false;
        }else{
            $result = true;
        }
        return $result;
    }   
    function datasend_region_update($dataSend){         
        $this->db->trans_start(); 
        $this->update($dataSend['data_region'], $dataSend['idcode']);
        $this->db->trans_complete();
        if($this->db->trans_status() == FALSE){
            $result  = false;
        }else{
            $result = true;
        }
        return $result;
    }
    function datasend_region_suspend($dataSend){         
        $this->db->trans_start();
        $this->update($dataSend['data_region'], $dataSend['idcode']);
        $this->db->trans_complete();
        if($this->db->trans_status() == FALSE){
            $result  = false;
        }else{
            $result = true;
        }
        return $result;
    }
    
    function get_all_to_print($data_filter){
            $this->db->select('*');
            $this->db->from($this->table);
            $this->db->where_in('am_region.TransStatus',array(0,1)); 
            $query 	= $this->db->get();
            $result	= $query->result();
            return $result;
    }
    //============= End DHIDHIK, 28 Maret 2016 ===============================//
}

/* End of file m_region.php */
/* Location: ./application/models/m_region.php */