<?php

class Uom_mdl extends Base_Model {

    function __construct() {

        parent::__construct();
		$this->table = 'satuan';
		$this->pk_field = 'KdSat';
    }

	function getPaging($page,$rows,$key=''){
		$offset = ($page-1)*$rows;
		$result = array();
		
		$q = "SELECT *,KdSat AS id FROM ".$this->table." a
			  WHERE KdSat LIKE '%$key%' OR UrSat LIKE '%$key%'
			  ORDER BY KdSat";
		$rstot=$this->db->query($q)->result();
	
		$q .= " LIMIT $offset,$rows";
		$rs=$this->db->query($q)->result();
		
		$result["total"] = count($rstot);
		$result["rows"] = $rs;
		
		return $result;
	}
	
	function getAll($key=''){
		$result = array();
		
		$q = "SELECT *,KdSat AS id FROM ".$this->table." a
			  WHERE KdSat LIKE '%$key%' OR UrSat LIKE '%$key%'
			  ORDER BY KdSat";	
		$rs=$this->db->query($q)->result();
		
		return $rs;
	}
	
	function add($data){
		$run = $this->db->insert($this->table, $data); 
		if ($this->db->affected_rows()>0){
			$rs=true;
			$log=$this->addlog($data['nik'],"Tambah Unit Of Measure ".$data['nik']);	
		} else {
			$rs=false;
		}
		
		return $rs;
	}
	
	function edit($data,$id){
		$run = $this->db->update($this->table, $data, array($this->pk_field => $id)); 
		if ($this->db->affected_rows()>0){
			$rs=true;
			$log=$this->addlog($id,"Ubah Unit Of Measure ".$id);	
		} else {
			$rs=false;
		}		
		
		return $rs;
	}
	
	function del($id){
		$this->db->delete($this->table, array($this->pk_field => $id)); 		
		if ($this->db->affected_rows()>0){
			$rs=true;
			$log=$this->addlog($id,"Hapus Unit Of Measure ".$id);	
		} else {
			$rs=false;
		}		
		
		return $rs;		
	}
}