<?php
class Brpt_keluarbarang_mdl extends Base_Model {
    function __construct() {
        parent::__construct();
		
		$this->usr=$this->session->userdata('username');
    }
	function getAll($page,$rows,$key='',$KdJnsDok='',$dtdari='',$dtsampai=''){
		$offset = ($page-1)*$rows;
		$result = array();
		
		$q = "SELECT 
				a.NoDaf,
				a.matout_no,
				a.currency,
				DATE_FORMAT( a.TgDaf, '%d/%m/%Y' ) AS FTgDaf,
				DATE_FORMAT( a.matout_date, '%d/%m/%Y' ) AS Fmatout_date,
				e.NmPrshn,
				b.mat_id,
				FORMAT( (price), 2 ) AS Famount,
				c.UrJnsDok,
				b.mat_id as KdBarang,
				b.PartNo as NmBarang,
				d.PartNo,
				b.uom as Sat,
				FORMAT(b.qty, 2) AS Fqty,
				g.NmNegara
			FROM
				mat_outhdr_sub a
			INNER JOIN mat_outdet_sub b ON b.matout_id = a.matout_id
			INNER JOIN jenis_dok c ON c.KdJnsDok = a.KdJnsDok
			LEFT JOIN mst_barang d ON d.KdBarang = b.mat_id
			LEFT JOIN mst_perusahaan e ON e.KdPrshn = a.cust
			LEFT JOIN mst_negara g ON g.KdNegara = e.Negara 
			WHERE 1=1
			AND b.qty != 0"; 
			  
		if ($KdJnsDok != ""){  
			$q .= " AND a.KdJnsDok LIKE '$KdJnsDok' ";
		} else {
			$q .= " AND a.KdJnsDok IN ('3','4','7','9','10') ";
		}
		
		if ($key != ""){  
			$q .= " AND (mat_id LIKE '%$key%' OR NmBarang LIKE '%$key%' OR NoDaf LIKE '%$key%')";
		}
		
		if($dtdari != '' && $dtsampai != ''):
			$q .= "AND TgDaf BETWEEN '".$this->dmys2ymd($dtdari)."' AND '".$this->dmys2ymd($dtsampai)."' ";
		endif;
		// die($q);
		$q .= "ORDER BY TgDaf, a.KdJnsDok,NoDaf";
		$rstot=$this->db->query($q)->result();
	
		$q .= " LIMIT $offset,$rows";
		$rs=$this->db->query($q)->result();
		
		$result["total"] = count($rstot);
		$result["rows"] = $rs;
		
		return $result;
	}
	function getAll2($page,$rows,$key='',$KdJnsDok='',$dtdari='',$dtsampai=''){
		$offset = ($page-1)*$rows;
		$result = array();
		
		$q = "SELECT 
				a.NoDaf,
				a.matout_no,
				a.currency,
				DATE_FORMAT( a.TgDaf, '%d/%m/%Y' ) AS FTgDaf,
				DATE_FORMAT( a.matout_date, '%d/%m/%Y' ) AS Fmatout_date,
				e.NmPrshn,
				b.mat_id,
				FORMAT( (b.qty*price), 2 ) AS Famount,
				c.UrJnsDok,
				d.KdBarang,
					b.PartNo as NmBarang,
				b.PartNo,
				ifnull(d.Sat,b.Sat) AS Sat,
				FORMAT(b.qty, 2) AS Fqty,
				g.NmNegara
			FROM
				mat_outhdr_sub a
			INNER JOIN mat_outdet_sub b ON b.matout_id = a.matout_id
			INNER JOIN jenis_dok c ON c.KdJnsDok = a.KdJnsDok
			LEFT JOIN mst_barang d ON d.KdBarang = b.mat_id
			LEFT JOIN mst_perusahaan e ON e.KdPrshn = a.cust
			LEFT JOIN mst_negara g ON g.KdNegara = e.Negara 
			WHERE 1=1
			AND b.qty != 0"; 
			  
		if ($KdJnsDok != ""){  
			$q .= " AND a.KdJnsDok LIKE '$KdJnsDok' ";
		} else {
			$q .= " AND a.KdJnsDok IN ('4','7','9','10') ";
		}
		
		if ($key != ""){  
			$q .= " AND (mat_id LIKE '%$key%' OR NmBarang LIKE '%$key%' OR NoDaf LIKE '%$key%')";
		}
		
		if($dtdari != '' && $dtsampai != ''):
			$q .= "AND TgDaf BETWEEN '".$this->dmys2ymd($dtdari)."' AND '".$this->dmys2ymd($dtsampai)."' ";
		endif;
		// die($q);
		$q .= "ORDER BY TgDaf, a.KdJnsDok,NoDaf";
		$rstot=$this->db->query($q)->result();
	
		// $q .= " LIMIT $offset,$rows";
		$rs=$this->db->query($q)->result();
		
		$result["total"] = count($rstot);
		$result["rows"] = $rs;
		
		return $result;
	}
	
	function add($data){
		$run = $this->db->insert($this->table, $data); 
		if ($this->db->affected_rows()>0){
			$rs=true;
			$brpt_masukrm=$this->addbrpt_masukrm($data['nik'],"Tambah Laporan Pemasukan Bahan Baku ".$data['nik']);	
		} else {
			$rs=false;
		}
		
		return $rs;
	}
	
	function edit($data,$id){
		$run = $this->db->update($this->table, $data, array($this->pk_field => $id)); 
		if ($this->db->affected_rows()>0){
			$rs=true;
			$brpt_masukrm=$this->addbrpt_masukrm($id,"Ubah Laporan Pemasukan Bahan Baku ".$id);	
		} else {
			$rs=false;
		}		
		
		return $rs;
	}
	
	function del($id){
		$this->db->delete($this->table, array($this->pk_field => $id)); 		
		if ($this->db->affected_rows()>0){
			$rs=true;
			$brpt_masukrm=$this->addbrpt_masukrm($id,"Hapus Laporan Pemasukan Bahan Baku ".$id);	
		} else {
			$rs=false;
		}		
		
		return $rs;		
	}
}