<?php
class Trxopn_mdl extends Base_Model {
    function __construct() {
        parent::__construct();
		$this->pk = 'opname_id';
    }
	function getHdr($page,$rows,$key=''){
		$offset = ($page-1)*$rows;
		$result = array();
		
		$q = "SELECT a.*,a.".$this->pk." AS id,
			  DATE_FORMAT(opname_date,'%d/%m/%Y') AS Fopname_date,
			  g.NmPrshn AS company,
			  l.nm_line
			  FROM ".$this->tbl." a
			  LEFT JOIN ".$this->tbl4." g ON g.".$this->pk4."=a.kd_cmpy
			  LEFT JOIN ".$this->tbl5." l ON l.".$this->pk5."=a.line
			  WHERE mat_type = '".$this->mat_type."'";
		if ($this->session->userdata('kd_cmpy') != "0001"){
			$q .= " AND a.kd_cmpy = '".$this->session->userdata('kd_cmpy')."'";	
		}
		$q .= " AND (a.opname_no LIKE '%$key%')
			  ORDER BY opname_date, opname_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 getRow($key=''){		
		$q = "SELECT a.*,a.".$this->pk." AS id,
			  DATE_FORMAT(opname_date,'%d/%m/%Y') AS Fopname_date,
			  g.NmPrshn AS company,
			  l.nm_line
			  FROM ".$this->tbl." a
			  LEFT JOIN ".$this->tbl4." g ON g.".$this->pk4."=a.kd_cmpy
			  LEFT JOIN ".$this->tbl5." l ON l.".$this->pk5."=a.line
			  WHERE a.".$this->pk."='$key'
			  LIMIT 1";
		$rs=$this->db->query($q)->result();
		
		return $rs[0];
	}
	
	function getDet($page,$rows,$key='',$opname_id=''){
		$offset = ($page-1)*$rows;
		$result = array();
		
		$q="SELECT a.*,opname_id AS id,
			FORMAT(qty, 2) AS Fqty,
			FORMAT(qty_in, 2) AS Fqty_in,
			FORMAT(qty_out, 2) AS Fqty_out,
			b.NmBarang,b.Sat
		  	FROM ".$this->tbl2." a 
		  	LEFT JOIN ".$this->tbl3." b ON ".$this->pk3." = mat_id 
			  WHERE ".$this->pk."='$opname_id'
			  AND (mat_id LIKE '%$key%' OR NmBarang LIKE '%$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.*,opname_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(opname_date,'%d/%m/%Y') AS Fopname_date,
			FORMAT(qty, 2) AS Fqty,
			c.NmBarang,c.Sat,
			g.NmPrshn AS company
			FROM ".$this->tbl." a
			LEFT JOIN ".$this->tbl2." b ON b.".$this->pk."=a.".$this->pk."
			LEFT JOIN ".$this->tbl3." c ON c.".$this->pk3."=b.mat_id
			LEFT JOIN ".$this->tbl4." g ON g.".$this->pk4."=a.kd_cmpy
			WHERE mat_type = '".$this->mat_type."'";
			
		if ($this->session->userdata('kd_cmpy') != "0001"){
			$q .= " AND kd_cmpy = '".$this->session->userdata('kd_cmpy')."'";	
		}
			  
		if ($key != ""){  
			$q .= " AND (a.opname_no LIKE '%$key%' OR mat_id LIKE '%$key%')";
		}	
			  
		if($dtdari != '' && $dtsampai != ''):
			$q .= " AND opname_date BETWEEN '".$this->dmys2ymd($dtdari)."' AND '".$this->dmys2ymd($dtsampai)."'";
		endif;
		
		$q .= " ORDER BY opname_date, a.opname_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;
	}
	
}