<?php
class Mtgrup_mdl extends Base_Model {
    function __construct() {
        parent::__construct();
		$this->table = 'mat_group';
		$this->pk_field = 'matgroup_code';
    }
	function getPaging($page,$rows,$key=''){
		$offset = ($page-1)*$rows;
		$result = array();
		
		$q = "SELECT *,matgroup_code AS id FROM ".$this->table." a
			  WHERE matgroup_code LIKE '%$key%' OR matgroup_name LIKE '%$key%'
			  ORDER BY matgroup_code";
		$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=''){		
		$q = "SELECT *,matgroup_code AS id FROM ".$this->table." a
			  WHERE matgroup_code LIKE '%$key%' OR matgroup_name LIKE '%$key%'
			  ORDER BY matgroup_name";
		$rs=$this->db->query($q)->result();
		
		return $rs;
	}
	function getJnsBarang($key=''){		
		$q = "SELECT *,KdJnsBarang AS id FROM jenis_barang a
			  WHERE KdJnsBarang LIKE '%$key%' OR UrJnsBarang LIKE '%$key%'
			  ORDER BY UrJnsBarang";
		$rs=$this->db->query($q)->result();
		
		return $rs;
	}
	function getJnsBarang2($key=''){	
		
		// WHERE KdJnsBarang in ('1','11','0','3')
		
		$q = "SELECT *,KdJnsBarang AS id FROM jenis_barang a
			  WHERE KdJnsBarang LIKE '%$key%' AND UrJnsBarang LIKE '%$key%'
			  ORDER BY UrJnsBarang";
		$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 Item Group ".$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 Item Group ".$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 Item Group ".$id);	
		} else {
			$rs=false;
		}		
		
		return $rs;		
	}
}