<?php
ini_set('memory_limit', '512M');
class Bahanbaku_mdl extends Base_Model {
  function __construct() {
    parent::__construct();
		$this->tbl = 'mst_barang';
		$this->tbl2 = 'mat_group';
		$this->tbl3 = 'jenis_barang';
		$this->tbl4 = 'mst_line';
		$this->pk_field = 'KdBarang';
	}
	
	function getAll($page,$rows,$key='',$TpBarang=''){
		$offset = ($page-1)*$rows;
		$result = array();
		$cond_TpBarang = "";
		if ($TpBarang != ""){
			$cond_TpBarang = " AND a.TpBarang = '$TpBarang' ";
		}
		
		// TpBarang in ('1','11','0','3') AND
		$q = "SELECT *,KdBarang AS id FROM ".$this->tbl." a
			  LEFT JOIN ".$this->tbl2." b ON b.matgroup_code=a.MatGroup
			  LEFT JOIN ".$this->tbl3." jb ON jb.KdJnsBarang=a.TpBarang
			  LEFT JOIN ".$this->tbl4." c ON c.Matgroup=a.MatGroup
				WHERE (a.KdBarang LIKE '%$key%' OR NmBarang LIKE '%$key%')
				$cond_TpBarang				
			  ORDER BY a.CF asc";
		$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;
	}
	
	function getAll2($page,$rows,$key=''){
		$offset = ($page-1)*$rows;
		$result = array();
		
		$q = "SELECT *,KdBarang AS id FROM ".$this->tbl." a
			  LEFT JOIN ".$this->tbl2." b ON b.matgroup_code=a.MatGroup
			  LEFT JOIN ".$this->tbl3." jb ON jb.KdJnsBarang=a.TpBarang
			  WHERE (a.KdBarang LIKE '%$key%' OR NmBarang LIKE '%$key%')
			  ORDER BY a.CF asc";
		$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;
	}
	
	function getRow($key=''){		
		$q = "SELECT *,KdBarang AS id FROM ".$this->tbl." a
			  LEFT JOIN ".$this->tbl2." b ON b.matgroup_code=a.MatGroup
			  WHERE TpBarang = '1' 
			  AND a.KdBarang = '$key'
			  LIMIT 1";
		$rs=$this->db->query($q)->result();
		
		return $rs[0];
	}

	// CHECK FOR DUPLICATE ITEM --ir
	function checkItem($KdBarang=''){		
		$q = "SELECT COUNT(KdBarang) as KdBarang
						FROM ".$this->tbl."
						WHERE KdBarang = '$KdBarang'
						LIMIT 1";
		$rs=$this->db->query($q)->result();		
		return $rs;
	}
	
	function add($data){
		// $this->addColor($data['warna']);
		
		$run = $this->db->insert($this->tbl, $data);
		
		if ($this->db->affected_rows()==0){	
			$rs=false;
		} else {
			$rs=true;
			$log=$this->addlog($data['nik'],"Tambah Raw Material ".$data['nik']);	
		}
		
		return $rs;
	}
	
	function edit($data,$id){
		$run = $this->db->update($this->tbl, $data, array($this->pk_field => $id)); 
		if ($this->db->affected_rows()>0){
			$rs=true;
			$log=$this->addlog($id,"Ubah Raw Material ".$id);	
		} else {
			$rs=false;
		}		
		
		return $rs;
	}
	
	function del($id){
		$this->db->delete($this->tbl, array($this->pk_field => $id)); 		
		if ($this->db->affected_rows()>0){
			$rs=true;
			$log=$this->addlog($id,"Hapus Raw Material ".$id);	
		} else {
			$rs=false;
		}		
		
		return $rs;		
	}
}