<?php
class Rmsout_mdl extends Trxout_mdl {

    function __construct() {

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

    function getHdr($page,$rows,$key='',$KdJnsDok=''){
		$offset = ($page-1)*$rows;
		$result = array();
		$cond_KdJnsDok = "";
		if ($KdJnsDok != ""){
			$cond_KdJnsDok = " AND a.KdJnsDok = '$KdJnsDok' ";
		}

		// $cond_matout_type = "";
		if ($this->matout_type != ""){
			$cond_matout_type = " AND a.matout_type = '$this->matout_type' ";
		}
		
		$q = "SELECT distinct a.*,a.".$this->pk." AS id,
			  DATE_FORMAT(matout_date,'%d/%m/%Y') AS Fmatout_date,
			  DATE_FORMAT(TgDaf,'%d/%m/%Y') AS FTgDaf,
			  DATE_FORMAT(TgAju,'%d/%m/%Y') AS FTgAju,
			  DATE_FORMAT(TgTTD,'%d/%m/%Y') AS FTgTTD,
			  DATE_FORMAT(etd,'%d/%m/%Y') AS Fetd,
			  DATE_FORMAT(eta,'%d/%m/%Y') AS Feta,
			  c.matout_name,d.NmPrshn,f.UrJnsDok,
			  g.NmPrshn AS company, a.port_odispacth,a.port_odischarge,
			  a.final_odestination,a.feder_name,a.insured,a.shipping,a.vpd,
			  FORMAT(cif,2) AS cif,
			  FORMAT(tot_amount,2) AS tot_amount
			  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.cust
			  LEFT JOIN ".$this->tbl6." f ON f.".$this->pk6."=a.".$this->pk6."
			  LEFT JOIN ".$this->tbl4." g ON g.".$this->pk4."=a.kd_cmpy
				WHERE mat_type = '".$this->mat_type."'
				$cond_matout_type
				$cond_KdJnsDok";
				
		$q .= " AND (a.matout_no LIKE '%$key%' OR d.NmPrshn LIKE '%$key%' 
				OR a.NoDaf LIKE '%$key%' OR a.ref_no LIKE '%$key%')
			  ORDER BY matout_id desc";

			//   var_dump($q);
			//   die();
		$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 getHdrDet($page,$rows,$key='',$dtdari='',$dtsampai=''){
		$offset = ($page-1)*$rows;
		$result = array();
		$cond_KdJnsDok = "";
		if ($KdJnsDok != ""){
			$cond_KdJnsDok = " AND a.KdJnsDok = '$KdJnsDok' ";
		}

		// $cond_matout_type = "";
		if ($this->matout_type != ""){
			$cond_matout_type = " AND a.matout_type = '$this->matout_type' ";
		}
		
		$q="SELECT a.".$this->pk." AS id,
			DATE_FORMAT(a.matout_date,'%d/%m/%Y') AS Fmatout_date,
			DATE_FORMAT(a.TgDaf,'%d/%m/%Y') AS FTgDaf,
			DATE_FORMAT(a.TgAju,'%d/%m/%Y') AS FTgAju,
			a.matout_id, a.matout_type, a.cust, a.matout_no,
			b.mat_id, a.kurs, a.currency, a.NoAju, a.NoDaf,
			FORMAT(b.qty, 2) AS Fqty,
			FORMAT(b.price, 4) AS Fprice,
			FORMAT(b.ng_qty,2) AS Ngqty,
			FORMAT(b.qty*b.price, 2) AS amount,
			c.matout_name, d.NmPrshn, f.UrJnsDok,
			e.NmBarang, e.Sat, c.matout_name
			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.matout_type
			LEFT JOIN ".$this->tbl4." d ON d.".$this->pk4."=a.cust and d.".$this->pk4." = a.kd_cmpy
			LEFT JOIN ".$this->tbl5." e ON e.".$this->pk5."= b.mat_id 
			LEFT JOIN ".$this->tbl6." f ON f.".$this->pk6."= a.KdJnsDok
			WHERE a.mat_type = '".$this->mat_type."'
			$cond_matout_type
				$cond_KdJnsDok
			";
			
		// if ($this->session->userdata('kd_cmpy') != "0001"){
		// 	$q .= " AND kd_cmpy = '".$this->session->userdata('kd_cmpy')."'";	
		// }
			  
		if ($key != ""){  
			$q .= " AND (a.matout_no LIKE '%$key%' OR d.NmPrshn LIKE '%$key%' 
			OR e.KdBarang LIKE '%$key%' OR c.matout_name LIKE '%$key%')";
		}	
			  
		if($dtdari != '' && $dtsampai != ''):
			$q .= " AND a.matout_date BETWEEN '".$this->dmys2ymd($dtdari)."' AND '".$this->dmys2ymd($dtsampai)."'";
		endif;
		
		$q .= " ORDER BY a.matout_date DESC, a.matout_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;
	}

}