<?php

class Company_mdl extends Base_Model {

    function __construct() {

        parent::__construct();
		$this->tbl = 'mst_perusahaan';
		$this->tbl2 = 'mst_tp_prshn';
		$this->tbl3 = 'mst_negara';
		$this->tbl4 = 'valuta';
		$this->pk_field = 'KdPrshn';
    }

	function getPaging($page,$rows,$key=''){
		$offset = ($page-1)*$rows;
		$result = array();
		
		$q = "SELECT *,KdPrshn AS id FROM ".$this->tbl." a
			  LEFT JOIN ".$this->tbl2." b ON b.id_tp_prshn=a.TpPrshn
			  LEFT JOIN ".$this->tbl3." c ON c.KdNegara=a.Negara
			  WHERE TpPrshn <> 'o' AND (a.KdPrshn LIKE '%$key%' OR NmPrshn LIKE '%$key%')
			  ORDER BY KdPrshn";
		$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 getPaging2($page,$rows,$key=''){
		$offset = ($page-1)*$rows;
		$result = array();
		
		$q = "SELECT *,KdPrshn AS id FROM ".$this->tbl." a
			  LEFT JOIN ".$this->tbl2." b ON b.id_tp_prshn=a.TpPrshn
			  LEFT JOIN ".$this->tbl3." c ON c.KdNegara=a.Negara
			  WHERE TpPrshn <> 'o' AND (a.KdPrshn LIKE '%$key%' OR NmPrshn LIKE '%$key%')
			  ORDER BY KdPrshn";
		$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 getAll($key=''){			
		$q = "SELECT *,KdPrshn AS id FROM ".$this->tbl." a
			  LEFT JOIN ".$this->tbl3." c ON c.KdNegara=a.Negara
			  WHERE TpPrshn <> 'o' AND (a.KdPrshn LIKE '%$key%' OR NmPrshn LIKE '%$key%')
			  ORDER BY KdPrshn";
		$rs=$this->db->query($q)->result();
		
		return $rs;
	}
	
	function getSub($key=''){	
		$kd_cmpy=$this->session->userdata('kd_cmpy');		
		$q = "SELECT *,KdPrshn AS id 
			  FROM ".$this->tbl." a
			  LEFT JOIN ".$this->tbl3." c ON c.KdNegara=a.Negara
			  WHERE TpPrshn IN ('k','o') 
			  AND (a.KdPrshn LIKE '%$key%' OR NmPrshn LIKE '%$key%')";
		if ($kd_cmpy != '9221'){
			  $q .= " AND a.KdPrshn = '".$kd_cmpy."'";
		}
		$q .= " ORDER BY KdPrshn";
		// die($q);
		$rs=$this->db->query($q)->result();
		
		return $rs;
	}
	
	function getSub2($key=''){	
		$q = "SELECT *,KdPrshn AS id 
			  FROM ".$this->tbl." a
			  LEFT JOIN ".$this->tbl3." c ON c.KdNegara=a.Negara
			  WHERE TpPrshn IN ('k','o') 
			  AND (a.KdPrshn LIKE '%$key%' OR NmPrshn LIKE '%$key%')";
 	    $q .= " AND a.KdPrshn <> '9221'";
		$q .= " ORDER BY KdPrshn";
		$rs=$this->db->query($q)->result();
		
		return $rs;
	}
	
	function getRow($key=''){			
		$q = "SELECT *,KdPrshn AS id FROM ".$this->tbl." a
			  LEFT JOIN ".$this->tbl3." c ON c.KdNegara=a.Negara
			  WHERE a.KdPrshn = '$key'
			  ORDER BY KdPrshn";
		$rs=$this->db->query($q)->result();
		
		return $rs[0]->NmPrshn;
	}
	
	function getBuyerAll($key=''){			
		$q = "SELECT *,KdPrshn AS id FROM ".$this->tbl." a
			  LEFT JOIN ".$this->tbl3." c ON c.KdNegara=a.Negara
			  WHERE TpPrshn = 'BY' AND (a.KdPrshn LIKE '%$key%' OR NmPrshn LIKE '%$key%')
			  ORDER BY KdPrshn";
		$rs=$this->db->query($q)->result();
		
		return $rs;
	}
	
	function getSupplierAll($key=''){			
		$q = "SELECT *,KdPrshn AS id FROM ".$this->tbl." a
			  LEFT JOIN ".$this->tbl3." c ON c.KdNegara=a.Negara
			  WHERE TpPrshn = 'SP' AND (a.KdPrshn LIKE '%$key%' OR NmPrshn LIKE '%$key%')
			  ORDER BY KdPrshn";
		$rs=$this->db->query($q)->result();
		
		return $rs;
	}
	
	function getSubkonAll($key=''){			
		$q = "SELECT *,KdPrshn AS id FROM ".$this->tbl." a
			  LEFT JOIN ".$this->tbl3." c ON c.KdNegara=a.Negara
			  WHERE TpPrshn = 'SC' AND (a.KdPrshn LIKE '%$key%' OR NmPrshn LIKE '%$key%')
			  ORDER BY KdPrshn";
		$rs=$this->db->query($q)->result();
		
		return $rs;
	}

	// CHECK FOR DUPLICATE COMPANY --ir
	function checkCompany($KdPrshn='',$TpPrshn=''){		
		$q = "SELECT COUNT(KdPrshn) as KdPrshn
						FROM ".$this->tbl."
						WHERE KdPrshn = '$KdPrshn'
						LIMIT 1";
		$rs=$this->db->query($q)->result();		
		return $rs;
	}
	
	function add($data){
		$run = $this->db->insert($this->tbl, $data); 
		if ($this->db->affected_rows()>0){
			$rs=true;
			$log=$this->addlog($data['nik'],"Tambah Business Partner ".$data['nik']);	
		} else {
			$rs=false;
		}
		
		return $rs;
	}
	
	function edit($data,$id){
		$run = $this->db->update($this->tbl, $data, array($this->pk_field => $id)); 
		if ($this->db->affected_rows()>0){
			$rs=true;
			$log=$this->addlog($id,"Ubah Business Partner ".$id);	
		} else {
			$rs=false;
		}		
		
		return $rs;
	}
	
	function del($id){
		$this->db->delete($this->tbl, array($this->pk_field => $id)); 		
		if ($this->db->affected_rows()>0){
			$rs=true;
			$log=$this->addlog($id,"Hapus Business Partner ".$id);	
		} else {
			$rs=false;
		}		
		
		return $rs;		
	}
}