<?php
class General_mdl extends Base_Model {
    function __construct() {
        parent::__construct();
    }
	
	function getTpPrshn(){
		$q = "SELECT *
			  FROM mst_tp_prshn
			  WHERE id_tp_prshn <> 'o'
			  ORDER BY nm_tp_prshn";
		$rs = $this->db->query($q)->result();
		
		return $rs;
	}
	
	function getNegara($key){
		$q = "SELECT *
			  FROM mst_negara
			  WHERE KdNegara LIKE '%$key%' OR NmNegara LIKE '%$key%'
			  ORDER BY KdNegara";
		$rs = $this->db->query($q)->result();
		
		return $rs;
	}
	function getGroupId($key){
		$q = "SELECT *
			  FROM user_group
			  WHERE name_group_user LIKE '%$key%'
			  ORDER BY id_group_user";
		$rs = $this->db->query($q)->result();
		
		return $rs;
	}
	
	function getValuta($key){
		$q = "SELECT *
			  FROM valuta
			  WHERE KdVal LIKE '%$key%'
			  ORDER BY KdVal";
		$rs = $this->db->query($q)->result();
		
		return $rs;
	}
	function getValuta2($key){
		$accdb 	= $this->load->database('accounting', TRUE);
		$db 		= $this->load->database('default', TRUE);
		$q = "SELECT *
			  FROM doerp_sgp.valuta a, accdo_sgp.accdailykurs b
				WHERE  a.KdVal = b.currency
				AND KdVal LIKE '%$key%'
			  ORDER BY KdVal";
		$rs = $this->db->query($q)->result();
		
		return $rs;
	}
	function getARList($key){
		$q = "SELECT a.*,a.id AS id,
					DATE_FORMAT(a.date,'%d/%m/%Y') AS Fdate,
					FORMAT(a.tot_amount, 2) AS ftot_amount,
					CONCAT(a.accountd_id,' ',a.acctnamed) AS facctnamed,
					c.NmPrshn, e.matout_no, a.tax, e.cust_inv
					FROM acc_araphdr a
					LEFT JOIN mst_perusahaan c ON c.KdPrshn=a.partner_id
					LEFT JOIN mat_outhdr_sub e ON e.matout_id=a.ref_id
					WHERE type IN ('ar','arotm') AND ( cust_inv LIKE '%$key%' OR NmPrshn LIKE '%$key%')
					AND a.created_journal = 'yes'
					AND a.id NOT IN (select ref_id from acc_brbp)";
			  
		$db = $this->load->database('default', TRUE);
		$rs = $db->query($q)->result();
		
		return $rs;
	}
	function getAPList($key){
		$q = "SELECT a.*,a.id AS id,
					DATE_FORMAT(a.date,'%d/%m/%Y') AS Fdate,
					FORMAT(a.tot_amount, 2) AS ftot_amount,
					CONCAT(a.accountd_id,' ',a.acctnamed) AS facctnamed,
					c.NmPrshn, e.matin_no, a.tax, e.supl_inv
					FROM acc_araphdr a
					LEFT JOIN mst_perusahaan c ON c.KdPrshn=a.partner_id
					LEFT JOIN mat_inchdr_sub e ON e.matin_id=a.ref_id
					WHERE type = 'ap' AND ( supl_inv LIKE '%$key%' OR NmPrshn LIKE '%$key%')
					AND a.created_journal = 'yes'
					AND a.id NOT IN (select ref_id from acc_brbp)";

			  
		$db = $this->load->database('default', TRUE);
		$rs = $db->query($q)->result();
		
		return $rs;
	}
	function getAR($key){
		$q = "SELECT acctnum, acctname, CONCAT(acctnum, ' ', acctname) display_name
			  FROM accaccounts
			  WHERE acctnum like '130%' AND acctnum NOT like '1303%' AND(acctnum LIKE '%$key%' OR acctname LIKE '%$key%')
			  ORDER BY acctnum";
			  
		$accdb = $this->load->database('accounting', TRUE);
		$rs = $accdb->query($q)->result();
		
		return $rs;
	}
	function getAP($key){
		$q = "SELECT acctnum, acctname, CONCAT(acctnum, ' ', acctname) display_name
			  FROM accaccounts
			  WHERE subnum in ('41','91') AND(acctnum LIKE '%$key%' OR acctname LIKE '%$key%')
			  ORDER BY acctnum";
			  
		$accdb = $this->load->database('accounting', TRUE);
		$rs = $accdb->query($q)->result();
		
		return $rs;
	}
	function getBR($key){
		$q = "SELECT acctnum, acctname, CONCAT(acctnum, ' ', acctname) display_name
			  FROM accaccounts
				WHERE acctnum LIKE '12%' 
			  AND (acctnum LIKE '%$key%' OR acctname LIKE '%$key%')
			  ORDER BY acctnum";
			  
		$accdb = $this->load->database('accounting', TRUE);
		$rs = $accdb->query($q)->result();
		
		return $rs;
	}

	function getCR($key){
		$q = "SELECT acctnum, acctname, CONCAT(acctnum, ' ', acctname) display_name
			  FROM accaccounts
			  WHERE acctnum LIKE '110%'
			  AND (acctnum LIKE '%$key%' OR acctname LIKE '%$key%')
			  ORDER BY acctnum";
			  
		$accdb = $this->load->database('accounting', TRUE);
		$rs = $accdb->query($q)->result();
		
		return $rs;
	}

	function getBP($key){
		$q = "SELECT acctnum, acctname, CONCAT(acctnum, ' ', acctname) display_name
			  FROM accaccounts
			  WHERE acctnum LIKE '12%' 
			  AND (acctnum LIKE '%$key%' OR acctname LIKE '%$key%')
			  ORDER BY acctnum";
			  
		$accdb = $this->load->database('accounting', TRUE);
		$rs = $accdb->query($q)->result();
		
		return $rs;
	}
	function getCP($key){
		$q = "SELECT acctnum, acctname, CONCAT(acctnum, ' ', acctname) display_name
			  FROM accaccounts
			  WHERE acctnum LIKE '110%' 
			  AND (acctnum LIKE '%$key%' OR acctname LIKE '%$key%')
			  ORDER BY acctnum";
			  
		$accdb = $this->load->database('accounting', TRUE);
		$rs = $accdb->query($q)->result();
		
		return $rs;
	}
	function getAPCash($key){
		$q = "SELECT acctnum, acctname, CONCAT(acctnum, ' ', acctname) display_name
			  FROM accaccounts
			  WHERE acctnum = '3110001' OR subnum = '11' AND(acctnum LIKE '%$key%' OR acctname LIKE '%$key%')
			  ORDER BY acctnum";
			  
		$accdb = $this->load->database('accounting', TRUE);
		$rs = $accdb->query($q)->result();
		
		return $rs;
	}
	function getCash($key){
		$q = "SELECT acctnum, acctname, CONCAT(acctnum, ' ', acctname) display_name
			  FROM accaccounts
			  WHERE subnum = '11' AND(acctnum LIKE '%$key%' OR acctname LIKE '%$key%')
			  ORDER BY acctnum";
			  
		$accdb = $this->load->database('accounting', TRUE);
		$rs = $accdb->query($q)->result();
		
		return $rs;
	}
	function getInv($key){
		$q = "SELECT acctnum, acctname, CONCAT(acctnum, ' ', acctname) display_name
			  FROM accaccounts
			  WHERE acctnum LIKE '140%' OR acctnum LIKE '2103%' 
			  AND(acctnum LIKE '%$key%' OR acctname LIKE '%$key%')
			  ORDER BY acctnum";
			  
		$accdb = $this->load->database('accounting', TRUE);
		$rs = $accdb->query($q)->result();
		
		return $rs;
	}
	function getIncFgJournalD($key){
		$q = "SELECT acctnum, acctname, CONCAT(acctnum, ' ', acctname) display_name
			  FROM accaccounts
			  WHERE acctnum = '1300005' AND(acctnum LIKE '%$key%' OR acctname LIKE '%$key%')
			  ORDER BY acctnum";
			  
		$accdb = $this->load->database('accounting', TRUE);
		$rs = $accdb->query($q)->result();
		
		return $rs;
	}
	function getIncFgJournalC($key){
		$q = "SELECT acctnum, acctname, CONCAT(acctnum, ' ', acctname) display_name
			  FROM accaccounts
			  WHERE acctnum = '1300003' AND(acctnum LIKE '%$key%' OR acctname LIKE '%$key%')
			  ORDER BY acctnum";
			  
		$accdb = $this->load->database('accounting', TRUE);
		$rs = $accdb->query($q)->result();
		
		return $rs;
	}
	function getWipJournalD($key){
		$q = "SELECT acctnum, acctname, CONCAT(acctnum, ' ', acctname) display_name
			  FROM accaccounts
			  WHERE acctnum = '1300005' AND(acctnum LIKE '%$key%' OR acctname LIKE '%$key%')
			  ORDER BY acctnum";
			  
		$accdb = $this->load->database('accounting', TRUE);
		$rs = $accdb->query($q)->result();
		
		return $rs;
	}
	function getWipJournalC($key){
		$q = "SELECT acctnum, acctname, CONCAT(acctnum, ' ', acctname) display_name
			  FROM accaccounts
			  WHERE acctnum = '1300003' AND(acctnum LIKE '%$key%' OR acctname LIKE '%$key%')
			  ORDER BY acctnum";
			  
		$accdb = $this->load->database('accounting', TRUE);
		$rs = $accdb->query($q)->result();
		
		return $rs;
	}
	function getOutFgJournalD($key){
		$q = "SELECT acctnum, acctname, CONCAT(acctnum, ' ', acctname) display_name
			  FROM accaccounts
			  WHERE acctnum = '5110002' AND(acctnum LIKE '%$key%' OR acctname LIKE '%$key%')
			  ORDER BY acctnum";
			  
		$accdb = $this->load->database('accounting', TRUE);
		$rs = $accdb->query($q)->result();
		
		return $rs;
	}
	function getOutFgJournalC($key){
		$q = "SELECT acctnum, acctname, CONCAT(acctnum, ' ', acctname) display_name
			  FROM accaccounts
			  WHERE acctnum = '1300006' AND(acctnum LIKE '%$key%' OR acctname LIKE '%$key%')
			  ORDER BY acctnum";
			  
		$accdb = $this->load->database('accounting', TRUE);
		$rs = $accdb->query($q)->result();
		
		return $rs;
	}
	function getOutMatJournalD($key){
		$q = "SELECT acctnum, acctname, CONCAT(acctnum, ' ', acctname) display_name
			  FROM accaccounts
			  WHERE acctnum = '1300002' AND(acctnum LIKE '%$key%' OR acctname LIKE '%$key%')
			  ORDER BY acctnum";
			  
		$accdb = $this->load->database('accounting', TRUE);
		$rs = $accdb->query($q)->result();
		
		return $rs;
	}
	function getOutMatJournalC($key){
		$q = "SELECT acctnum, acctname, CONCAT(acctnum, ' ', acctname) display_name
			  FROM accaccounts
			  WHERE acctnum = '1300004' AND(acctnum LIKE '%$key%' OR acctname LIKE '%$key%')
			  ORDER BY acctnum";
			  
		$accdb = $this->load->database('accounting', TRUE);
		$rs = $accdb->query($q)->result();
		
		return $rs;
	}
	
	function getJnsDok($key){
		$q = "SELECT *
			  FROM jenis_dok
			  WHERE NmJnsDok LIKE '%$key%'
			  ORDER BY KdJnsDok";
		$rs = $this->db->query($q)->result();
		
		return $rs;
	}
	function getPacking($key){
		$q = "SELECT *
			  FROM referensi_kemasan
			  ORDER BY KODE_KEMASAN";
		$rs = $this->db->query($q)->result();
		
		return $rs;
	}
	
	function getJnsDokIn($key){
		$q = "SELECT *
			  FROM jenis_dok
			  WHERE KdJnsDok IN ('1','5','6','8') AND UrJnsDok LIKE '%$key%'
			  ORDER BY KdJnsDok";
		$rs = $this->db->query($q)->result();
		
		return $rs;
	}
	
	function getJnsDokOut($key){
		$q = "SELECT *
			  FROM jenis_dok
			  WHERE KdJnsDok IN ('3','4','7','9','10') AND UrJnsDok LIKE '%$key%'
			  ORDER BY KdJnsDok";
		$rs = $this->db->query($q)->result();
		
		return $rs;
	}
	
	function getInTypeAll($key){
		$q = "SELECT *,matin_type AS fgin_type
			  FROM mst_in_type
			  ORDER BY matin_type";
		$rs = $this->db->query($q)->result();
		
		return $rs;
	}
	function getInType($key){
		$q = "SELECT *,matin_type AS fgin_type
			  FROM mst_in_type
			  WHERE matin_type NOT IN ('8')
			  ORDER BY matin_type";
		$rs = $this->db->query($q)->result();
		return $rs;
	}
	function getInTypeSk($key){
		$q = "SELECT *,matin_type AS fgin_type
			  FROM mst_in_type
			  WHERE matin_type IN ('4')
			  ORDER BY matin_type";
		$rs = $this->db->query($q)->result();
		return $rs;
	}
	function getInTypeScrap($key){
		$q = "SELECT *,matin_type AS fgin_type
			  FROM mst_in_type
			  WHERE matin_type IN ('7','8')
			  ORDER BY matin_type";
		$rs = $this->db->query($q)->result();
		return $rs;
	}
	
	function getInTypeWh($key){
		$q = "SELECT *,matin_type AS fgin_type
			  FROM mst_in_type
			  WHERE matin_type IN ('0','1','2','3','4','6','8','9','10')
			  ORDER BY matin_type";
		$rs = $this->db->query($q)->result();
		
		return $rs;
	}
	
	function getInTypeQc($key){
		$q = "SELECT *,matin_type AS fgin_type
			  FROM ms_in_type
			  WHERE matin_type IN ('11','8')
			  ORDER BY matin_type";
		$rs = $this->db->query($q)->result();
		
		return $rs;
	}
	
	function getInTypeProd($key){
		$q = "SELECT *,matin_type AS fgin_type
			  FROM ms_in_type
			  WHERE matin_type IN ('5','8')
			  ORDER BY matin_type";
		$rs = $this->db->query($q)->result();
		
		return $rs;
	}
	
	function getInTypeFg($key){
		$q = "SELECT *,matin_type AS fgin_type
			  FROM ms_in_type
			  WHERE matin_type IN ('4','5','8','9')
			  ORDER BY matin_type";
		$rs = $this->db->query($q)->result();
		
		return $rs;
	}
	
	function getOutTypeAll($key){
		$q = "SELECT *,matout_type AS fgout_type
			  FROM mst_out_type
			  ORDER BY matout_type";
		$rs = $this->db->query($q)->result();
		
		return $rs;
	}
	function getOutType($key){
		$q = "SELECT *,matout_type AS fgout_type
			  FROM mst_out_type
			  WHERE matout_type NOT IN ('7','8')
			  ORDER BY matout_type";
		$rs = $this->db->query($q)->result();
		return $rs;
	}
	function getOutTypeScrap($key){
		$q = "SELECT *,matout_type AS fgout_type
			  FROM mst_out_type
			  WHERE matout_type IN ('7','8')
			  ORDER BY matout_type";
		$rs = $this->db->query($q)->result();
		return $rs;
	}
	
	function getOutTypeWh($key){
		$q = "SELECT *,matout_type AS fgout_type
			  FROM mst_out_type
			  WHERE matout_type IN ('0','1','2','3','6','7','8','9','10','11','13')
			  ORDER BY matout_type";
		$rs = $this->db->query($q)->result();
		
		return $rs;
	}
	
	function getOutTypeProd($key){
		$q = "SELECT *,matout_type AS fgout_type
			  FROM mst_out_type
			  WHERE matout_type IN ('0','1','6','12')
			  ORDER BY matout_type";
		$rs = $this->db->query($q)->result();
		
		return $rs;
	}
	
	function getOutTypeDo($key){
		$q = "SELECT *,matout_type AS fgout_type
			  FROM mst_out_type
			  WHERE matout_type IN ('3','4')
			  ORDER BY matout_type";
		$rs = $this->db->query($q)->result();
		
		return $rs;
	}
	
	function getColor($key){
		$q = "SELECT *
			  FROM mt_color
			  WHERE color LIKE '%$key%'
			  ORDER BY color";
		$rs = $this->db->query($q)->result();
		
		return $rs;
	}
	function getSize($key){
		$q = "SELECT *
			  FROM mt_size
			  WHERE kd_size LIKE '%$key%'
			  ORDER BY kd_size";
		$rs = $this->db->query($q)->result();
		
		return $rs;
	}
	
	function getProd($key){
		$q = "SELECT *
			  FROM mt_proses_prod
			  WHERE nm_proses_prod LIKE '%$key%'
			  ORDER BY no_urut";
		$rs = $this->db->query($q)->result();
		
		return $rs;
	}
	
	function getProdGudang($key){
		$q = "SELECT *
			  FROM mt_proses_prod
			  WHERE kd_proses_prod IN ('1','3','12','16') AND nm_proses_prod LIKE '%$key%'
			  ORDER BY no_urut";
		$rs = $this->db->query($q)->result();
		
		return $rs;
	}
	
	function getProdTo($key,$fgout_type,$prod_from){		
		$q = "SELECT *
			  FROM mt_prod_sequence a
			  INNER JOIN prod_sequence b ON b.nm_prod_seq=a.nm_prod_seq
			  WHERE prodcard_id = '$key'";	  
		
		if(($fgout_type=='1') || ($fgout_type=='6')){
			if($prod_from==0){
				$prod_to='';
			} else {			
				$q1 .= $q." AND kd_proses_prod='$prod_from'";
				$q1 .= " LIMIT 1";
				$rs1 = $this->db->query($q1)->result();
				if ($rs1){
					$no_proses_to=$rs1[0]->no_proses-1;
				} 
				
				$q2 = $q." AND no_proses='$no_proses_to'";
				$q2 .= " LIMIT 1";
				//echo $q2;
				$rs2 = $this->db->query($q2)->result();
				if ($rs2){
					$prod_to=$rs2[0]->kd_proses_prod;
				} else {
					$prod_to='';
				}
			}
			
		} else {
			if($prod_from==0){
				$q1 .= $q." AND no_proses=1";
				$q1 .= " LIMIT 1";
				//echo $q1;
				$rs = $this->db->query($q1)->result();
				$prod_to=$rs[0]->kd_proses_prod;
			} else {			
				$q1 .= $q." AND kd_proses_prod='$prod_from'";
				$q1 .= " LIMIT 1";
				$rs1 = $this->db->query($q1)->result();
				if ($rs1){
					$no_proses_to=$rs1[0]->no_proses+1;
				} 
				
				$q2 = $q." AND no_proses='$no_proses_to'";
				$q2 .= " LIMIT 1";
				//echo $q2;
				$rs2 = $this->db->query($q2)->result();
				if ($rs2){
					$prod_to=$rs2[0]->kd_proses_prod;
				} else {
					$prod_to='';
				}
			}
		}
		
		return $prod_to;
	}
	
	function getBom($page,$rows,$key='',$bom_type=''){
		$offset = ($page-1)*$rows;
		$result = array();
		
		$q = "SELECT a.*,a.bom_id AS id, fg.KdBarang as KdBarang,
		DATE_FORMAT(bom_date,'%d/%m/%Y') AS Fbom_date,
		fg.NmBarang,fg.Sat
		FROM mat_bomhdr_sub a
		LEFT JOIN mst_barang fg ON fg.KdBarang=a.fg_id
		WHERE 1=1 AND (fg.KdBarang LIKE '%$key%' OR fg.NmBarang LIKE '%$key%')
		AND bom_type = '".$bom_type."'";
		$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 getBomAll($page,$rows,$key=''){
		$offset = ($page-1)*$rows;
		$result = array();
		
		$q = "SELECT a.*,a.bom_id AS po_id, a.bom_no AS po_no, fg.KdBarang as KdBarang,
		DATE_FORMAT(bom_date,'%d/%m/%Y') AS Fbom_date,
		fg.NmBarang,fg.Sat
		FROM mat_bomhdr_sub a
		LEFT JOIN mst_barang fg ON fg.KdBarang=a.fg_id
		WHERE 1=1
		AND bom_type IN ('SMT','CB','ML')";
		$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 getBarang($page,$rows,$key='',$tp_barang){
		$offset = ($page-1)*$rows;
		$result = array();
		
		$q = "SELECT *,KdBarang AS id FROM mst_barang a
			  LEFT JOIN mat_group b ON b.matgroup_code=a.MatGroup
			  WHERE ".$tp_barang." AND (a.KdBarang LIKE '%$key%' OR PartNo LIKE '%$key%' OR `FG-P` LIKE '%$key%')
			  GROUP BY KdBarang
			  ORDER BY KdBarang";
		$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 getBarangBOMNP($page,$rows,$key=''){
		$offset = ($page-1)*$rows;
		$result = array();
		
		$q = "SELECT *,KdBarang AS id FROM mst_barang a
			  LEFT JOIN mat_group b ON b.matgroup_code=a.MatGroup
			  WHERE TpBarang=0 and (a.KdBarang LIKE '%$key%' OR PartNo LIKE '%$key%' OR `FG-P` LIKE '%$key%')
			  GROUP BY KdBarang
			  ORDER BY KdBarang";
		$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 getBarangMSO($page,$rows,$key=''){
		$offset = ($page-1)*$rows;
		$result = array();
		
		$q = "SELECT *,KdBarang AS id FROM mst_barang a
			  LEFT JOIN mat_group b ON b.matgroup_code=a.MatGroup
			  WHERE matgroup_code in ('FG-P','FG-NP') AND (a.KdBarang LIKE '%$key%' OR PartNo LIKE '%$key%')
			  GROUP BY KdBarang
			  ORDER BY KdBarang";
		$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 getBarangNGOUT($page,$rows,$key=''){
		$offset = ($page-1)*$rows;
		$result = array();
		
		$q = "SELECT *,KdBarang AS id FROM mst_barang a
			  LEFT JOIN mat_group b ON b.matgroup_code=a.MatGroup
			  WHERE matgroup_code in ('FG','FG-P','FG-NP') AND (a.KdBarang LIKE '%$key%' OR PartNo LIKE '%$key%')
			  GROUP BY KdBarang
			  ORDER BY KdBarang";
		$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 getBarangFGIN($page,$rows,$key=''){
		$offset = ($page-1)*$rows;
		$result = array();
		
		$q = "SELECT *,KdBarang AS id FROM mst_barang a
			  LEFT JOIN mat_group b ON b.matgroup_code=a.MatGroup
			  WHERE matgroup_code in ('FG','FG-P','FG-NP') AND (a.KdBarang LIKE '%$key%' OR PartNo LIKE '%$key%')
			  GROUP BY KdBarang
			  ORDER BY KdBarang";
		$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 getBarangScrap($page,$rows,$key=''){
		$offset = ($page-1)*$rows;
		$result = array();
		
		$q = "SELECT *,KdBarang AS id FROM mst_barang a
			  LEFT JOIN mat_group b ON b.matgroup_code=a.MatGroup
			  WHERE TpBarang NOT IN ('3')
			  AND KdBarang NOT LIKE ('01%') AND (a.KdBarang LIKE '%$key%' OR PartNo LIKE '%$key%' OR `FG-P` LIKE '%$key%')
			  GROUP BY KdBarang
			  ORDER BY KdBarang";
		$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 getBarangPoAll($page,$rows,$key=''){
		$offset = ($page-1)*$rows;
		$result = array();
		
		$q = "SELECT *,KdBarang AS id FROM mst_barang a
			  LEFT JOIN mat_group b ON b.matgroup_code=a.MatGroup
			  WHERE TpBarang NOT IN ('11') AND (a.KdBarang LIKE '%$key%' OR PartNo LIKE '%$key%' OR `FG-P` LIKE '%$key%')
			  GROUP BY KdBarang
			  ORDER BY KdBarang";
		$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 getBarangSMT($page,$rows,$key='',$tp_barang){
		$offset = ($page-1)*$rows;
		$result = array();
		
		$q = "SELECT *,KdBarang AS id FROM mst_barang a
			  LEFT JOIN mat_group b ON b.matgroup_code=a.MatGroup
			  WHERE TpBarang = '".$tp_barang."' AND MatGroup = 'SMT' AND (a.KdBarang LIKE '%$key%' OR NmBarang LIKE '%$key%')
			  GROUP BY KdBarang
			  ORDER BY KdBarang";
		$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 getBarangCB($page,$rows,$key='',$tp_barang){
		$offset = ($page-1)*$rows;
		$result = array();
		
		$q = "SELECT *,KdBarang AS id FROM mst_barang a
			  LEFT JOIN mat_group b ON b.matgroup_code=a.MatGroup
			  WHERE TpBarang = '".$tp_barang."' AND MatGroup = 'CB' AND (a.KdBarang LIKE '%$key%' OR NmBarang LIKE '%$key%')
			  GROUP BY KdBarang
			  ORDER BY KdBarang";
		$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 getBomSMT($page,$rows,$key=''){
		$offset = ($page-1)*$rows;
		$result = array();
		
		$q = "SELECT a.*,a.bom_id AS id, fg.KdBarang as KdBarang,
		DATE_FORMAT(bom_date,'%d/%m/%Y') AS Fbom_date,
		fg.NmBarang,fg.Sat
		FROM mat_bomhdr_smt_sub a
		LEFT JOIN mst_barang fg ON fg.KdBarang=a.fg_id
		WHERE 1=1 AND (fg.KdBarang LIKE '%$key%' OR fg.NmBarang LIKE '%$key%')";
		$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 getBarangML($page,$rows,$key='',$tp_barang){
		$offset = ($page-1)*$rows;
		$result = array();
		
		$q = "SELECT *,KdBarang AS id FROM mst_barang a
			  LEFT JOIN mat_group b ON b.matgroup_code=a.MatGroup
			  WHERE TpBarang = '".$tp_barang."' AND MatGroup = 'ML' AND (a.KdBarang LIKE '%$key%' OR NmBarang LIKE '%$key%')
			  GROUP BY KdBarang
			  ORDER BY KdBarang";
		$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 getBomCB($page,$rows,$key=''){
		$offset = ($page-1)*$rows;
		$result = array();
		
		$q = "SELECT a.*,a.bom_id AS id, fg.KdBarang as KdBarang,
		DATE_FORMAT(bom_date,'%d/%m/%Y') AS Fbom_date,
		fg.NmBarang,fg.Sat
		FROM mat_bomhdr_cb_sub a
		LEFT JOIN mst_barang fg ON fg.KdBarang=a.fg_id
		WHERE 1=1 AND (fg.KdBarang LIKE '%$key%' OR fg.NmBarang LIKE '%$key%')";
		$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 getBarangDO($page,$rows,$key='',$matout_id){
		$offset = ($page-1)*$rows;
		$result = array();
		
		$q = "SELECT od.*, a.KdBarang, a.NmBarang, a.Sat 
			  FROM mat_outdet_sub od
			  LEFT JOIN mst_barang a ON a.KdBarang = od.mat_id 
			  LEFT JOIN mat_group b ON b.matgroup_code=a.MatGroup
			  WHERE matout_id = '".$matout_id."' 
			  AND (od.mat_id LIKE '%$key%' OR NmBarang LIKE '%$key%')
			  ORDER BY od.child_no";
		$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 getBarangSO($page,$rows,$key='',$matout_id){
		$offset = ($page-1)*$rows;
		$result = array();
		
		$q = "SELECT od.*, a.KdBarang, a.NmBarang, a.Sat 
			  FROM pur_podet od
			  LEFT JOIN mst_barang a ON a.KdBarang = od.mat_id 
			  LEFT JOIN mat_group b ON b.matgroup_code=a.MatGroup
			  WHERE matout_id = '".$matout_id."' 
			  AND (od.mat_id LIKE '%$key%' OR NmBarang LIKE '%$key%')
			  ORDER BY od.child_no";
		$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 getBarangPO($page,$rows,$key='',$po_id){
		$offset = ($page-1)*$rows;
		$result = array();
		
		$q = "SELECT od.*, a.KdBarang, a.NmBarang, a.Sat 
			  FROM pur_podet od
			  LEFT JOIN mst_barang a ON a.KdBarang = od.mat_id 
			  LEFT JOIN mat_group b ON b.matgroup_code=a.MatGroup
			  WHERE od.po_id = '".$matout_id."' 
			  AND (od.mat_id LIKE '%$key%' OR od.PartNo LIKE '%$key%')
			  ORDER BY od.child_no";
		$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 getBarangIN($page,$rows,$key='',$matin_id){
		$offset = ($page-1)*$rows;
		$result = array();
		
		$q = "SELECT od.*, a.KdBarang, a.NmBarang, a.Sat 
			  FROM mat_incdet_sub od
			  LEFT JOIN mst_barang a ON a.KdBarang = od.mat_id 
			  LEFT JOIN mat_group b ON b.matgroup_code=a.MatGroup
			  WHERE matin_id = '".$matin_id."' 
			  AND (od.mat_id LIKE '%$key%' OR NmBarang LIKE '%$key%')
			  ORDER BY od.child_no";
		$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 getPO($page,$rows,$key='', $po_type){
		$offset = ($page-1)*$rows;
		$result = array();
		
		$q = "SELECT h.po_id, h.po_no, h.po_date, h.supplier, h.currency, s.NmPrshn, h.kurs,h.tot_amount as HargaSerah,
					DATE_FORMAT('%d/%m/%Y', po_date) AS fpo_date, count(f.id) as jmlbrg
			  FROM pur_pohdr h
			  LEFT JOIN mst_perusahaan s ON s.KdPrshn=h.supplier
				LEFT JOIN pur_podet f ON f.po_id=h.po_id
				WHERE(h.po_no LIKE '%$key%') 
			  	GROUP BY h.po_id, h.po_no, h.po_date, h.supplier, h.currency, s.NmPrshn, h.kurs
			  ORDER BY po_date DESC, po_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 getPOmc($page,$rows,$key=''){
		$offset = ($page-1)*$rows;
		$result = array();
		
		$q = "SELECT h.po_id, h.po_no, h.po_date, h.supplier, h.currency, s.NmPrshn, h.kurs,
					DATE_FORMAT('%d/%m/%Y', po_date) AS fpo_date, count(f.id) as jmlbrg
			  FROM pur_pohdr h
			  LEFT JOIN mst_perusahaan s ON s.KdPrshn=h.supplier
				LEFT JOIN pur_podet f ON f.po_id=h.po_id
				WHERE  po_type='0' AND (h.po_no LIKE '%$key%')
			  	GROUP BY h.po_id, h.po_no, h.po_date, h.supplier, h.currency, s.NmPrshn, h.kurs
			  ORDER BY po_date DESC, po_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 getPOSc($page,$rows,$key=''){
		$offset = ($page-1)*$rows;
		$result = array();
		$q = "SELECT *,KdBarang AS id FROM mst_barang a			  
				WHERE TpBarang ='12'
				  ORDER BY KdBarang";
		$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 getBomWipIn($page,$rows,$key='', $ref_type){
		$offset = ($page-1)*$rows;
		$result = array();
		
		$q = "SELECT a.*,a.bom_id,
			  DATE_FORMAT(bom_date,'%d/%m/%Y') AS bom_date,
			  fg.NmBarang,fg.Sat
			  FROM mat_bomhdr_wip_sub a
			  LEFT JOIN mst_barang fg ON fg.KdBarang=a.fg_id
				WHERE 1=1
				AND a.ref_type = ".$ref_type."
				AND (a.bom_no LIKE '%%' OR a.fg_id LIKE '%%')
			  ORDER BY bom_date, 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 getDO($page,$rows,$key=''){
		$offset = ($page-1)*$rows;
		$result = array();
		
		$q = "SELECT h.*,
		DATE_FORMAT('%d/%m/%Y', matout_date) AS fmatout_date,
		s.NmPrshn
			FROM mat_outhdr_sub h
			LEFT JOIN mst_perusahaan s ON s.KdPrshn=h.cust
			WHERE mat_type IN ('0', '3') AND matout_type = '4' AND (matout_no LIKE '%$key%' OR NmPrshn LIKE '%$key%' OR cust_inv LIKE '%$key%')
			AND tot_amount > 0
				AND h.matout_id NOT IN 
					(SELECT a.ref_id
					FROM acc_araphdr a, mat_outhdr_sub b
					WHERE a.ref_id != b.matout_id AND a.type = 'ar')
			ORDER BY matout_date DESC, 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;
	}
	function getIN($page,$rows,$key=''){
		$offset = ($page-1)*$rows;
		$result = array();
		
		$q = "SELECT h.*,
		DATE_FORMAT('%d/%m/%Y', matin_date) AS fmatin_date,
		s.NmPrshn
		FROM mat_inchdr_sub h
		LEFT JOIN mst_perusahaan s ON s.KdPrshn=h.supplier
		WHERE mat_type IN (1,3) AND (matin_no LIKE '%$key%' OR NmPrshn LIKE '%$key%' OR supl_inv LIKE '%$key%')
		AND tot_amount > 0
			AND h.matin_id NOT IN 
				(SELECT a.ref_id
				FROM acc_araphdr a, mat_inchdr_sub b
				WHERE a.ref_id != b.matin_id AND a.type = 'ap')
		ORDER BY matin_date DESC, 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 getLine(){
		$q = "SELECT *
			  FROM mst_line
			  ORDER BY nm_line";
		//echo $q;
		$rs = $this->db->query($q)->result();
		return $rs;
	}

	function getOpname(){
		$q = "SELECT *
			  FROM opname_type
			  ORDER BY id";
		//echo $q;
		$rs = $this->db->query($q)->result();
		return $rs;
	}

	function getInco(){
		$q = "SELECT *
			  FROM incoterm
			  ORDER BY KdInco";
		//echo $q;
		$rs = $this->db->query($q)->result();
		return $rs;
	}
	function getOutHdr($page,$rows,$key=''){
		$offset = ($page-1)*$rows;
		$result = array();
		$q = "SELECT a.*,a.matout_id AS id,
			  DATE_FORMAT(matout_date,'%d/%m/%Y') AS Fmatout_date,
			  DATE_FORMAT(TgDaf,'%d/%m/%Y') AS FTgDaf,
			  c.matout_name,d.NmPrshn,
			  g.NmPrshn AS company
			  FROM mat_outhdr_sub a
			  LEFT JOIN mst_out_type c ON c.matout_type=a.matout_type
			  LEFT JOIN mst_perusahaan d ON d.KdPrshn=a.cust
			  LEFT JOIN mst_perusahaan g ON g.KdPrshn=a.kd_cmpy
			  WHERE mat_type = '1'";
		if ($this->session->userdata('kd_cmpy') != "0001"){
			$q .= " AND kd_cmpy = '".$this->session->userdata('kd_cmpy')."'";
		}
		$q .= " AND (a.matout_no LIKE '%$key%' OR d.NmPrshn LIKE '%$key%')
			  ORDER BY matout_date, 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;
	}
}
