<?php
class Valuta_mdl extends Base_Model {
  function __construct() {
  	parent::__construct();
		$this->tbl = 'valuta';
		$this->pk = 'KdVal';
  }
	function getPage($page,$rows,$key=''){		
		$offset = ($page-1)*$rows;
		$result = array();
		
		$q = "SELECT * , KdVal AS id FROM ".$this->tbl." a
			  WHERE a.UrVal LIKE '%$key%'";
		$q .= " ORDER BY KdVal";
		$rstot=$this->db->query($q)->result();
		// echo $q;
		$q .= " LIMIT $offset,$rows";
		$rs=$this->db->query($q)->result();
		
		$result["total"] = count($rstot);
		$result["rows"] = $rs;
		
		return $result;
	}

	// CHECK FOR DUPLICATE VALUTA --ir
	function checkValuta($KdVal=''){		
		$q = "SELECT COUNT(KdVal) as KdVal
						FROM ".$this->tbl."
						WHERE KdVal = '$KdVal'
						LIMIT 1";
		$rs=$this->db->query($q)->result();		
		return $rs;
	}
	
	function add($data){
		$run = $this->db->insert($this->tbl, $data); 
		if ($this->db->affected_rows()>0){
			$rs=true;
			$log=$this->addLog($data['nik'],"Tambah Data Valuta ".$data['nik']);	
		} else {
			$rs=false;
		}
		
		return $rs;
	}
	
	function edit($data,$id){
		$run = $this->db->update($this->tbl, $data, array($this->pk => $id)); 
		if ($this->db->affected_rows()>0){
			$rs=true;
			$log=$this->addLog($id,"Ubah Data Valuta ".$id);	
		} else {
			$rs=false;
		}		
		
		return $rs;
	}
	
	function del($KdVal){
		$this->db->delete($this->tbl, array($this->pk => $KdVal)); 		
		if ($this->db->affected_rows()>0){
			$rs=true;
			$log=$this->addLog($id,"Hapus Data Valuta ".$id);	
		} else {
			$rs=false;
		}		
		
		return $rs;		
	}
}