<?php
class Matcons_mdl extends Base_Model {
    function __construct() {
        parent::__construct();
		$this->pk = 'bom_id';
    }
	function getHdr($page,$rows,$key=''){
		$offset = ($page-1)*$rows;
		$result = array();
		
		$q = "SELECT distinct a.*,a.".$this->pk." AS id,
			  DATE_FORMAT(bom_date,'%d/%m/%Y') AS Fbom_date,
			  fg.NmBarang,fg.Sat,fg.PartNo
			  FROM ".$this->tbl." a
			  LEFT JOIN ".$this->tbl3." fg ON fg.".$this->pk3."=a.fg_id
			  LEFT JOIN ".$this->tbl2." c ON a.bom_id = c.bom_id
				WHERE 1=1
				AND ".$this->type." = 'FG-P'";
		$q .= " AND (a.bom_no LIKE '%$key%' OR a.fg_id LIKE '%$key%' or c.mat_id LIKE '%$key%')
			  ORDER BY bom_date, bom_id DESC";
		$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 a.*,a.".$this->pk." AS id,
			  DATE_FORMAT(bom_date,'%d/%m/%Y') AS Fbom_date,
			  fg.NmBarang,fg.Sat
			  FROM ".$this->tbl." a
			  LEFT JOIN ".$this->tbl3." fg ON fg.".$this->pk3."=a.fg_id
			  WHERE a.".$this->pk."='$key'
				LIMIT 1
				AND ".$this->type." = 'FG-P'";
		$rs=$this->db->query($q)->result();
		
		return $rs[0];
	}
	
	function getDet($page,$rows,$key=''){
		$offset = ($page-1)*$rows;
		$result = array();
		
		$q="SELECT a.*,bom_id AS id,
			FORMAT(qty, 2) AS Fqty,
			b.NmBarang,b.Sat,b.PartNo
		  	FROM ".$this->tbl2." a 
			LEFT JOIN ".$this->tbl3." b ON ".$this->pk3." = a.mat_id 
		  	LEFT JOIN ".$this->tbl5." c ON ".$this->pk5." = a.weight 				
		  	WHERE ".$this->pk."='$key' 
		  	ORDER BY child_no 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 getDetAll($key=''){
		
		$q="SELECT a.*,bom_id AS id,
			FORMAT(qty, 2) AS Fqty,
			b.NmBarang,b.Sat
		  	FROM ".$this->tbl2." a 
		  	LEFT JOIN ".$this->tbl3." b ON ".$this->pk3." = mat_id 
		  	WHERE ".$this->pk."='$key' 
		  	ORDER BY child_no ASC";
		$rs=$this->db->query($q)->result();
		
		return $rs;
	}
	
	function getHdrDet($page,$rows,$key='',$dtdari='',$dtsampai=''){
		$offset = ($page-1)*$rows;
		$result = array();
		
		$q="SELECT a.*,a.".$this->pk." AS id,
			DATE_FORMAT(bom_date,'%d/%m/%Y') AS Fbom_date,
			b.*,
			FORMAT(qty, 2) AS Fqty,
			fg.NmBarang AS NmBarang0,fg.Sat AS Sat0,
			c.NmBarang,c.Sat
			FROM ".$this->tbl." a
			LEFT JOIN ".$this->tbl2." b ON b.".$this->pk."=a.".$this->pk."
			LEFT JOIN ".$this->tbl3." fg ON fg.".$this->pk3."=a.fg_id
			LEFT JOIN ".$this->tbl3." c ON c.".$this->pk3."=b.mat_id
			WHERE 1=1
			AND ".$this->type." = 'FG-P'";
			  
		if ($key != ""){  
			$q .= " AND (a.bom_no LIKE '%$key%' OR mat_id LIKE '%$key%')";
		}	
			  
		if($dtdari != '' && $dtsampai != ''):
			$q .= " AND bom_date BETWEEN '".$this->dmys2ymd($dtdari)."' AND '".$this->dmys2ymd($dtsampai)."'";
		endif;
		
		$q .= " ORDER BY bom_date, a.bom_no DESC";
		$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 getNewNo(){
		$q="SELECT MAX(".$this->pk.") AS auto_no FROM ".$this->tbl." LIMIT 1";
		$rs=$this->db->query($q)->result();
		$autono=($rs)?$rs[0]->auto_no+1:1;
		
		return $autono;
	}
	
	function add($data){
		$run = $this->db->insert($this->tbl, $data); 
		if ($this->db->affected_rows()>0){
			$rs=true;
			$log=$this->addLog($data['nama_user'],"Tambah ".$this->title." ".$data[$this->pkh]);	
		} else {
			$rs=false;
		}
		
		return $rs;
	}
	
	function addDet($data){
		unset($data['NmBarang']);
		unset($data['Sat']);
		$run = $this->db->insert($this->tbl2, $data); 
		if ($this->db->affected_rows()>0){
			$rs=true;
			$log=$this->addLog($data['nama_user'],"Tambah ".$this->title." ".$data[$this->pk]);	
		} else {
			$rs=false;
		}
		
		return $rs;
	}
	
	function editHdr($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 ".$this->title." Header".$id);	
		} else {
			$rs=false;
		}		
		
		return $rs;
	}
	
	function editDet($data,$pk){
		unset($data['NmBarang']);
		unset($data['Sat']);
		//print_r($data);
		$run = $this->db->update($this->tbl2, $data, $pk); 
		//echo $this->db->last_query();
		if ($this->db->affected_rows()>0){
			$rs=true;
			$log=$this->addLog($pk,"Ubah ".$this->title." Detail".$pk);	
		} else {
			$rs=false;
		}
		
		return $rs;
	}
	
}