<?php

class Trxin_mdl extends Base_Model {

    function __construct() {

        parent::__construct();
		$this->pk = 'matin_id';
    }

	function getHdr($page,$rows,$key=''){
		$offset = ($page-1)*$rows;
		$result = array();
		
		$q = "SELECT a.*,a.".$this->pk." AS id,
			  DATE_FORMAT(matin_date,'%d/%m/%Y') AS Fmatin_date,
			  DATE_FORMAT(TgDaf,'%d/%m/%Y') AS FTgDaf,
			  c.matin_name,d.NmPrshn,f.UrJnsDok  
			  FROM ".$this->tbl." a
			  LEFT JOIN ".$this->tbl3." c ON c.".$this->pk3."=a.".$this->pk3."
			  LEFT JOIN ".$this->tbl4." d ON d.".$this->pk4."=a.supplier
			  LEFT JOIN ".$this->tbl6." f ON f.".$this->pk6."=a.".$this->pk6."
			  WHERE mat_type = '".$this->mat_type."' AND (a.matin_no LIKE '%$key%' OR NmPrshn LIKE '%$key%')
			  ORDER BY matin_date, matin_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(matin_date,'%d/%m/%Y') AS Fmatin_date,
			  DATE_FORMAT(TgDaf,'%d/%m/%Y') AS FTgDaf,
			  c.matin_name,d.NmPrshn,f.UrJnsDok  
			  FROM ".$this->tbl." a
			  LEFT JOIN ".$this->tbl3." c ON c.".$this->pk3."=a.".$this->pk3."
			  LEFT JOIN ".$this->tbl4." d ON d.".$this->pk4."=a.supplier
			  LEFT JOIN ".$this->tbl6." f ON f.".$this->pk6."=a.".$this->pk6."
			  WHERE a.".$this->pk."='$key'
			  LIMIT 1";
		$rs=$this->db->query($q)->result();
		
		return $rs[0];
	}
	
	function getDet($page,$rows,$key=''){
		$offset = ($page-1)*$rows;
		$result = array();
		
		$q="SELECT a.*,matin_id AS id,
			FORMAT(qty, 2) AS Fqty,
			FORMAT(price, 4) AS Fprice,
			FORMAT(qty*price, 2) AS amount,
			b.NmBarang,b.Sat
		  	FROM ".$this->tbl2." a 
		  	LEFT JOIN ".$this->tbl5." b ON KdBarang = mat_id 
		  	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.*,matin_id AS id,
			FORMAT(qty, 2) AS Fqty,
			FORMAT(price, 4) AS Fprice,
			FORMAT(qty*price, 2) AS amount,
			b.NmBarang,b.Sat
		  	FROM ".$this->tbl2." a 
		  	LEFT JOIN ".$this->tbl5." b ON KdBarang = 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(matin_date,'%d/%m/%Y') AS Fmatin_date,
			DATE_FORMAT(TgDaf,'%d/%m/%Y') AS FTgDaf,
			b.*,
			FORMAT(qty, 2) AS Fqty,
			FORMAT(price, 4) AS Fprice,
			FORMAT(qty*price, 2) AS amount,
			c.matin_name,d.NmPrshn,f.UrJnsDok,
			e.NmBarang,e.Sat
			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."=a.matin_type
			LEFT JOIN ".$this->tbl4." d ON d.".$this->pk4."=a.supplier
			LEFT JOIN ".$this->tbl5." e ON e.".$this->pk5."= b.mat_id 
			LEFT JOIN ".$this->tbl6." f ON f.".$this->pk6."= a.KdJnsDok
			WHERE mat_type = '".$this->mat_type."'";
			  
		if ($key != ""){  
			$q .= " AND (a.matin_no LIKE '%$key%' OR NmPrshn LIKE '%$key%' OR KdBarang LIKE '%$key%')";
		}	
			  
		if($dtdari != '' && $dtsampai != ''):
			$q .= " AND matin_date BETWEEN '".$this->dmys2ymd($dtdari)."' AND '".$this->dmys2ymd($dtsampai)."'";
		endif;
		
		$q .= " ORDER BY matin_date, a.matin_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->addLogSubkon($data['nama_user'],"Tambah ".$this->title." ".$data[$this->pkh]);	
		} else {
			$rs=false;
		}
		
		return $rs;
	}
	
	function addDet($data){
		unset($data['NmBarang']);
		unset($data['Sat']);
		unset($data['amount']);
		$run = $this->db->insert($this->tbl2, $data); 
		if ($this->db->affected_rows()>0){
			$rs=true;
			$log=$this->addLogSubkon($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->addLogSubkon($id,"Ubah ".$this->title." Header".$id);	
		} else {
			$rs=false;
		}		
		
		return $rs;
	}
	
	function editDet($data,$pk){
		unset($data['NmBarang']);
		unset($data['Sat']);
		unset($data['amount']);
		//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->addLogSubkon($pk,"Ubah ".$this->title." Detail".$pk);	
		} else {
			$rs=false;
		}
		
		return $rs;
	}
	
}