<?php
class CT_bc25 extends CI_Controller{
	function __construct()
	{
	  parent::__construct();
		if (!$this->dx_auth->is_logged_in()) {
			header('HTTP/1.1 401 Unauthorize');die('expire'); 
		}
		
		$uid = $this->session->userdata('DX_user_id');
		
		if(!permissionAuth(2) && $uid != RID){ 
			header('HTTP/1.1 401 Unauthorize');die('noaccess');
		}
		
		$this->load->model("exim/mdl_bc25", 'mdl_bc25');
	}

	function index(){
		
		$this->load->view('exim/view_bc25');
	
	}
	
	function loaddata(){
		$search = isset($_POST['_search'])?$_POST['_search']:false;
        $searchField = isset($_POST['searchField'])?$_POST['searchField']:'';
        $searchString = isset($_POST['searchString'])?$_POST['searchString']:'';
        $filters =  isset($_POST['filters'])?$_POST['filters']:'';
		
		$page = isset($_POST['page'])?$_POST['page']:1; // get the requested page
        $limit = isset($_POST['rows'])?$_POST['rows']:20; // get how many rows we want to have into the grid
        $sidx = isset($_POST['sidx'])?$_POST['sidx']:'id'; // get index row - i.e. user click to sort
        $sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		
		$where = " 1=1 and status <> 'new' ";
				
		if ($search == 'true'){
			if (!empty($filters)){
				$array = json_decode($filters);
				foreach($array->rules as $arr=>$k){
					
					if ( $k->field == "SA_NO" ) {
						$searchString = $k->data;
						$where .= " and SA_NO like  '%".$searchString."%'"; 
					} 
				}
			}
		}
		
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_bc25->getbc25dataAll($limit,$start,$sidx,$sord, $where);
        $count = $this->mdl_bc25->getbc25dataCountAll($where);
	//	var_dump($records); die();
		
		if( $count > 0 ) { $total_pages = ceil($count/$limit); } else { $total_pages = 0; } 
        if ($page > $total_pages) $page=$total_pages;
        
        $responce->page = $page;
		$responce->total = $total_pages;
		$responce->records =  $count;
		$i=0;
        foreach($records as $r){
			if($r->status == null){
				$status = '-';
				
			} else { $status=$r->status;  }
			
			$responce->rows[$i]['id_sa_hdr']=$r->id_sa_hdr; 
			$responce->rows[$i]['SA_NO']=$r->SA_NO; 
			$responce->rows[$i]['status']=$status; 
			$responce->rows[$i]['NPWP_PEMBELI']=$r->NPWP_PEMBELI; 
			$responce->rows[$i]['NAMA_PEMBELI']=$r->NAMA_PEMBELI;
			$responce->rows[$i]['BC25No']= str_pad($r->BC25No, 6, "0", STR_PAD_LEFT);
			$responce->rows[$i]['BC25Tg']=$r->BC25Tg;
			$responce->rows[$i]['BC25NAJU']=$r->BC25NAJU;
			$responce->rows[$i]['BC25TGAJU']=$r->BC25TGAJU;
			$responce->rows[$i]['KdKpbc']=$r->KdKpbc;
			$responce->rows[$i]['jml_inv']=$r->jml_inv; 
			$responce->rows[$i]['create_date']=$r->create_date; 
			$responce->rows[$i]['create_by']=$r->create_by; 
			$responce->rows[$i]['update_date']=$r->update_date; 
			$responce->rows[$i]['update_by']=$r->update_by; 
			
			$i++;
		}
		echo json_encode($responce);
		
	}
	
		
	function view_bc25_getdata(){
	
		$this->load->view('exim/view_bc25_getdata');
	
	}
	
	function view_bc25_datauploaded(){
	
		$this->load->view('exim/view_bc25_datauploaded');
	}
	
	function getdatasa(){
		$filename = (isset($_POST['filename']) && !empty($_POST['filename']))?$_POST['filename']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$id_bc25_upload = (isset($_POST['id_bc25_upload']) && !empty($_POST['id_bc25_upload']))?$_POST['id_bc25_upload']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$username = $this->session->userdata('DX_username');
		
/* 		 echo " exec('[spi_getdata_bc25] @filename=''$filename'', @idfile=$id_bc25_upload, @username=''$username'' ')";
	    die();  */
		$qry = $this->db->query("
			BEGIN TRY
			
			 exec('[spi_getdata_bc25] @filename=''$filename'', @idfile=$id_bc25_upload, @username=''$username'' ')
			
			END TRY
			BEGIN CATCH 
				SELECT 
				'06' AS status
				,ERROR_MESSAGE() AS description;
				
			END CATCH;
			
			");
		 $rs = $qry->result_array();
		 // $r = $qry->result();		
		 // var_dump($r);die();
		 foreach ($rs as $row){
			 $status = $row['status'];
			 $msg = $row['description'];
		 }  
		 
		 echo json_encode(array('status' => $status, 'desc' => $msg));
	
	}
	
	function loadgetbc25data(){
		$search = isset($_POST['_search'])?$_POST['_search']:false;
        $searchField = isset($_POST['searchField'])?$_POST['searchField']:'';
        $searchString = isset($_POST['searchString'])?$_POST['searchString']:'';
        $filters =  isset($_POST['filters'])?$_POST['filters']:'';
		
		$page = isset($_POST['page'])?$_POST['page']:1; // get the requested page
        $limit = isset($_POST['rows'])?$_POST['rows']:20; // get how many rows we want to have into the grid
        $sidx = isset($_POST['sidx'])?$_POST['sidx']:'id_sa_hdr'; // get index row - i.e. user click to sort
        $sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		
		$where = " 1=1 ";
				
		if ($search == 'true'){
			if (!empty($filters)){
				$array = json_decode($filters);
				foreach($array->rules as $arr=>$k){
					
					if ( $k->field == "create_date" ) {
						$searchString = $k->data;
						$where .= "  and CONVERT(VARCHAR(10),[create_date],101)  =  '$searchString'"; 
					}  else if  ($k->field == "SA_NO" ) {
						$searchString = $k->data;
						$where .= " and [SA_NO] like  '%".$searchString."%'"; 
					}
				}
			}
		}
		
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_bc25->getbc25dataAll($limit,$start,$sidx,$sord, $where);
        $count = $this->mdl_bc25->getbc25dataCountAll($where);
	//	var_dump($records); die();
		
		if( $count > 0 ) { $total_pages = ceil($count/$limit); } else { $total_pages = 0; } 
        if ($page > $total_pages) $page=$total_pages;
        
        $responce->page = $page;
		$responce->total = $total_pages;
		$responce->records =  $count;
		$i=0;
        foreach($records as $r){
				
			$responce->rows[$i]['id_sa_hdr']=$r->id_sa_hdr; 
			$responce->rows[$i]['SA_NO']=$r->SA_NO; 
			$responce->rows[$i]['status']=$r->status; 
			$responce->rows[$i]['create_date']=$r->create_date; 
			$responce->rows[$i]['create_by']=$r->create_by; 
			$responce->rows[$i]['update_date']=$r->update_date; 
			$responce->rows[$i]['update_by']=$r->update_by; 
		
			
			$i++;
		}
		echo json_encode($responce);
	
	}
	
	function view_bc25_getassy(){
			
		$id_sa_hdr = (isset($_POST['id_sa_hdr']) && !empty($_POST['id_sa_hdr']))?$_POST['id_sa_hdr']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$SA_NO = (isset($_POST['SA_NO']) && !empty($_POST['SA_NO']))?$_POST['SA_NO']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$data['id_sa_hdr']=$id_sa_hdr ;
		$data['SA_NO']=$SA_NO ;
		$this->load->view('exim/view_bc25_getassy', $data);
	
	}
	
	function loaddata_bc25_gettassy(){
		$search = isset($_POST['_search'])?$_POST['_search']:false;
        $searchField = isset($_POST['searchField'])?$_POST['searchField']:'';
        $searchString = isset($_POST['searchString'])?$_POST['searchString']:'';
        $filters =  isset($_POST['filters'])?$_POST['filters']:'';
		
		$page = isset($_POST['page'])?$_POST['page']:1; // get the requested page
        $limit = isset($_POST['rows'])?$_POST['rows']:20; // get how many rows we want to have into the grid
        $sidx = isset($_POST['sidx'])?$_POST['sidx']:'id_sa_hdr'; // get index row - i.e. user click to sort
        $sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		$id_sa_hdr = (isset($_POST['hdnidsahdr']) && !empty($_POST['hdnidsahdr']))?$_POST['hdnidsahdr']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		
		$where = " 1=1 and id_sa_hdr=$id_sa_hdr ";
				
		if ($search == 'true'){
			if (!empty($filters)){
				$array = json_decode($filters);
				foreach($array->rules as $arr=>$k){
					
					if ( $k->field == "create_date" ) {
						$searchString = $k->data;
						$where .= "  and CONVERT(VARCHAR(10),[create_date],101)  =  '$searchString'"; 
					}  else if  ($k->field == "SA_NO" ) {
						$searchString = $k->data;
						$where .= " and [SA_NO] like  '%".$searchString."%'"; 
					}
				}
			}
		}
		
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_bc25->getassybc25viewdataAll($limit,$start,$sidx,$sord, $where);
        $count = $this->mdl_bc25->getassybc25viewCountAll($where);
	//	var_dump($records); die();
		
		if( $count > 0 ) { $total_pages = ceil($count/$limit); } else { $total_pages = 0; } 
        if ($page > $total_pages) $page=$total_pages;
        
        $responce->page = $page;
		$responce->total = $total_pages;
		$responce->records =  $count;
		$i=0;
        foreach($records as $r){
				
			$responce->rows[$i]['id_sa']=$r->id_sa; 
			$responce->rows[$i]['id_sa_hdr']=$r->id_sa_hdr; 
			$responce->rows[$i]['SA_NO']=$r->SA_NO; 
			$responce->rows[$i]['NPWP_PEMBELI']=$r->NPWP_PEMBELI; 
			$responce->rows[$i]['NAMA_PEMBELI']=$r->NAMA_PEMBELI; 
			$responce->rows[$i]['INV_NO']=$r->INV_NO; 
			$responce->rows[$i]['ASSY_CODE']=$r->ASSY_CODE; 
			$responce->rows[$i]['ASSY_NO']=$r->ASSY_NO; 
			$responce->rows[$i]['TOTAL_SET']=$r->TOTAL_SET; 
			$responce->rows[$i]['SATUAN']=$r->SATUAN; 
			$responce->rows[$i]['JUMLAH_COLI']=$r->JUMLAH_COLI; 
			$responce->rows[$i]['GROSSWEIGHT']=$r->GROSSWEIGHT; 
			$responce->rows[$i]['SATUANGROSS']=$r->SATUANGROSS; 
			$responce->rows[$i]['NETWEIGHT']=$r->NETWEIGHT; 
			$responce->rows[$i]['SATUANNET']=$r->SATUANNET; 
			$responce->rows[$i]['PRICE']=$r->PRICE; 
			$responce->rows[$i]['AMOUNT']=$r->AMOUNT; 
			$responce->rows[$i]['create_date']=$r->create_date; 
			$responce->rows[$i]['create_by']=$r->create_by; 
			$responce->rows[$i]['update_date']=$r->update_date; 
			$responce->rows[$i]['update_by']=$r->update_by; 
		
			
			$i++;
		}
		echo json_encode($responce);
	
	
	}
	
	
	function view_bc25_getbb(){
			
		$id_sa_hdr = (isset($_POST['id_sa_hdr']) && !empty($_POST['id_sa_hdr']))?$_POST['id_sa_hdr']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$SA_NO = (isset($_POST['SA_NO']) && !empty($_POST['SA_NO']))?$_POST['SA_NO']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$data['id_sa_hdr']=$id_sa_hdr ;
		$data['SA_NO']=$SA_NO ;
		$this->load->view('exim/view_bc25_getbb', $data);
	
	}
	
	function loaddata_bc25_getbb(){
		$search = isset($_POST['_search'])?$_POST['_search']:false;
        $searchField = isset($_POST['searchField'])?$_POST['searchField']:'';
        $searchString = isset($_POST['searchString'])?$_POST['searchString']:'';
        $filters =  isset($_POST['filters'])?$_POST['filters']:'';
		
		$page = isset($_POST['page'])?$_POST['page']:1; // get the requested page
        $limit = isset($_POST['rows'])?$_POST['rows']:20; // get how many rows we want to have into the grid
        $sidx = isset($_POST['sidx'])?$_POST['sidx']:'id_sa_hdr'; // get index row - i.e. user click to sort
        $sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		$id_sa_hdr = (isset($_POST['id_sa_hdr']) && !empty($_POST['id_sa_hdr']))?$_POST['id_sa_hdr']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		
		$where = " 1=1 and id_sa_hdr=$id_sa_hdr ";
				
		if ($search == 'true'){
			if (!empty($filters)){
				$array = json_decode($filters);
				foreach($array->rules as $arr=>$k){
					
					if ( $k->field == "create_date" ) {
						$searchString = $k->data;
						$where .= "  and CONVERT(VARCHAR(10),[create_date],101)  =  '$searchString'"; 
					}  else if  ($k->field == "SA_NO" ) {
						$searchString = $k->data;
						$where .= " and [SA_NO] like  '%".$searchString."%'"; 
					}
				}
			}
		}
		
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_bc25->getbbbc25viewdataAll($limit,$start,$sidx,$sord, $where);
        $count = $this->mdl_bc25->getbb25viewCountAll($where);
	//	var_dump($records); die();
		
		if( $count > 0 ) { $total_pages = ceil($count/$limit); } else { $total_pages = 0; } 
        if ($page > $total_pages) $page=$total_pages;
        
        $responce->page = $page;
		$responce->total = $total_pages;
		$responce->records =  $count;
		$i=0;
        foreach($records as $r){
				
			$responce->rows[$i]['id_sa_bb']=$r->id_sa_bb; 
			$responce->rows[$i]['id_sa_hdr']=$r->id_sa_hdr; 
			$responce->rows[$i]['SA_NO']=$r->SA_NO; 
			$responce->rows[$i]['NO_URUT']=$r->NO_URUT; 
			$responce->rows[$i]['PART_NAME']=$r->PART_NAME; 
			$responce->rows[$i]['QTY']=$r->QTY; 
			$responce->rows[$i]['UNIT']=$r->UNIT; 
			$responce->rows[$i]['STATUS']=$r->STATUS; 
			$responce->rows[$i]['CURR']=$r->CURR; 
			$responce->rows[$i]['PRICE']=$r->PRICE; 
			$responce->rows[$i]['CIF']=$r->CIF; 
			$responce->rows[$i]['NDPMB']=$r->NDPMB; 
			$responce->rows[$i]['NDPDM_NOW']=$r->NDPDM_NOW; 
			$responce->rows[$i]['HARGA_PABEAN']=$r->HARGA_PABEAN; 
			$responce->rows[$i]['HS']=$r->HS; 
			$responce->rows[$i]['BM']=$r->BM; 
			$responce->rows[$i]['PPN']=$r->PPN; 
			$responce->rows[$i]['PPH']=$r->PPH; 
			$responce->rows[$i]['BM_RP']=$r->BM_RP; 
			$responce->rows[$i]['PPN_RP']=$r->PPN_RP; 
			$responce->rows[$i]['PPH_RP']=$r->PPH_RP; 
			$responce->rows[$i]['create_date']=$r->create_date; 
			$responce->rows[$i]['create_by']=$r->create_by; 
			$responce->rows[$i]['update_date']=$r->update_date; 
			$responce->rows[$i]['update_by']=$r->update_by; 
			
			
			$i++;
		}
		echo json_encode($responce);
	}
	
	function prosesdatasa(){
		$id_sa_hdr = (isset($_POST['id_sa_hdr']) && !empty($_POST['id_sa_hdr']))?$_POST['id_sa_hdr']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$username = $this->session->userdata('DX_username');
		
		$qry = $this->db->query("
			BEGIN TRY
			
			 exec('[sp_insertbc25] @id_sa_hdr=$id_sa_hdr, @username=$username ')
			
			END TRY
			BEGIN CATCH 
				SELECT 
				'06' AS status
				,ERROR_MESSAGE() AS description;
				
			END CATCH;
			
			");
		 $rs = $qry->result_array();
		 // $r = $qry->result();		
		 // var_dump($r);die();
		 foreach ($rs as $row){
			 $status = $row['status'];
			 $msg = $row['description'];
		 }  
		 
		 echo json_encode(array('sts' => $status, 'desc' => $msg));
	
	
	}
	
	function view_bc25_dtl(){
		$id_sa_hdr = (isset($_POST['id_sa_hdr']) && !empty($_POST['id_sa_hdr']))?$_POST['id_sa_hdr']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$BC25No = (isset($_POST['BC25No']) && !empty($_POST['BC25No']))?$_POST['BC25No']:'';
		$BC25Tg = (isset($_POST['BC25Tg']) && !empty($_POST['BC25Tg']))?$_POST['BC25Tg']:'';
		$BC25NAJU = (isset($_POST['BC25NAJU']) && !empty($_POST['BC25NAJU']))?$_POST['BC25NAJU']:'';
		$BC25TGAJU = (isset($_POST['BC25TGAJU']) && !empty($_POST['BC25TGAJU']))?$_POST['BC25TGAJU']:'';
		$SA_NO = (isset($_POST['SA_NO']) && !empty($_POST['SA_NO']))?$_POST['SA_NO']:'';
	  
	    $data['id_sa_hdr'] = $id_sa_hdr;
		$data['BC25No'] = $BC25No;
		$data['BC25Tg'] = $BC25Tg;
		$data['BC25NAJU'] = $BC25NAJU;
		$data['BC25TGAJU'] = $BC25TGAJU;
		$data['SA_NO'] = $SA_NO;
		
		$q = $this->db->query("
			SELECT  id_terkait, doc_out_no, id_data_out_hdr, convert(varchar, [doc_out_date], 103) as doc_out_date, convert(varchar, [approve_date], 103) as approve_date  from [tbl_data_out_hdr] where id_terkait=$id_sa_hdr and doc_out_to_id=3
		");
		
		$rs = $q->result_array();
		//var_dump($rs); die();
		$id_data_out_hdr='';
		$doc_out_date='';
		$approve_date='';
		foreach ($rs as $row){
			$id_data_out_hdr=$row['id_data_out_hdr'];	
			$doc_out_date=$row['doc_out_date'];	
			$approve_date=$row['approve_date'];
		}
		$data["idhdrbc25"]	= $id_data_out_hdr;
		$data["doc_out_date"]	= $doc_out_date;
		$data["approve_date"]	= $approve_date;		
		$data['loading_indicator'] =  "<div class='loading_indicator'>loading...<br/><img src='./assets/images/loading.gif' alt='loading' /></div>";
		
		$this->load->view('exim/view_bc25_dtl', $data);
	
	}
	
	
	function update_bc25_sa_hdr(){
		$id_sa_hdr = (isset($_POST['id_sa_hdr']) && !empty($_POST['id_sa_hdr']))?$_POST['id_sa_hdr']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$BC25No = (isset($_POST['BC25No']) && !empty($_POST['BC25No']))?$_POST['BC25No']:'';
		$BC25Tg = (isset($_POST['BC25Tg']) && !empty($_POST['BC25Tg']))?$_POST['BC25Tg']:'';
		if ($BC25Tg != ''){
			$BC25Tgx=explode('/' , $BC25Tg);
			$BC25Tg = $BC25Tgx[1].'/'.$BC25Tgx[0].'/'.$BC25Tgx[2];
		}
	    
		 $username = $this->session->userdata('DX_username');
		
		
		/*$BC25NAJU = (isset($_POST['BC25NAJU']) && !empty($_POST['BC25NAJU']))?$_POST['BC25NAJU']:'';
		$BC25TGAJU = (isset($_POST['BC25TGAJU']) && !empty($_POST['BC25TGAJU']))?$_POST['BC25TGAJU']:'';
		$SA_NO = (isset($_POST['SA_NO']) && !empty($_POST['SA_NO']))?$_POST['SA_NO']:''; */
		
		$qry = $this->db->query("
			BEGIN TRY
			
			 exec('[spu_insertbc25] @id_sa_hdr=$id_sa_hdr, @BC25No=''$BC25No'', @BC25Tg=''$BC25Tg'', @username=$username ')
			
			END TRY
			BEGIN CATCH 
				SELECT 
				'06' AS status
				,ERROR_MESSAGE() AS description;
				
			END CATCH;
			
			");
		 $rs = $qry->result_array();
		 // $r = $qry->result();		
		 // var_dump($r);die();
		 foreach ($rs as $row){
			 $status = $row['status'];
			 $msg = $row['description'];
		 }  
		 
		 echo json_encode(array('sts' => $status, 'desc' => $msg));
	
		
	}
	
	function loaddata_bc25_hdr(){
		$search = isset($_POST['_search'])?$_POST['_search']:false;
        $searchField = isset($_POST['searchField'])?$_POST['searchField']:'';
        $searchString = isset($_POST['searchString'])?$_POST['searchString']:'';
        $filters =  isset($_POST['filters'])?$_POST['filters']:'';
		
		$page = isset($_POST['page'])?$_POST['page']:1; // get the requested page
        $limit = isset($_POST['rows'])?$_POST['rows']:20; // get how many rows we want to have into the grid
        $sidx = isset($_POST['sidx'])?$_POST['sidx']:'id_sa_hdr'; // get index row - i.e. user click to sort
        $sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		
		$id_sa_hdr = (isset($_POST['id_sa_hdr_dtl']) && !empty($_POST['id_sa_hdr_dtl']))?$_POST['id_sa_hdr_dtl']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		
		
		$where = " 1=1 and id_sa_hdr=$id_sa_hdr ";
				
		if ($search == 'true'){
			if (!empty($filters)){
				$array = json_decode($filters);
				foreach($array->rules as $arr=>$k){
					
					if ( $k->field == "create_date" ) {
						$searchString = $k->data;
						$where .= "  and CONVERT(VARCHAR(10),[create_date],101)  =  '$searchString'"; 
					}  else if  ($k->field == "SA_NO" ) {
						$searchString = $k->data;
						$where .= " and [SA_NO] like  '%".$searchString."%'"; 
					}
				}
			}
		}
		
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_bc25->getbc25dataAll($limit,$start,$sidx,$sord, $where);
        $count = $this->mdl_bc25->getbc25dataCountAll($where);
	//	var_dump($records); die();
		
		if( $count > 0 ) { $total_pages = ceil($count/$limit); } else { $total_pages = 0; } 
        if ($page > $total_pages) $page=$total_pages;
        
        $responce->page = $page;
		$responce->total = $total_pages;
		$responce->records =  $count;
		$i=0;
        foreach($records as $r){
				
			$responce->rows[$i]['id_sa_hdr']=$r->id_sa_hdr; 
			$responce->rows[$i]['SA_NO']=$r->SA_NO; 
			$responce->rows[$i]['status']=$r->status; 
			$responce->rows[$i]['NPWP_PEMBELI']=$r->NPWP_PEMBELI; 
			$responce->rows[$i]['NAMA_PEMBELI']=$r->NAMA_PEMBELI; 
			$responce->rows[$i]['BC25No']=$r->BC25No; 
			$responce->rows[$i]['BC25Tg']=$r->BC25Tg; 
			$responce->rows[$i]['KdKpbc']=$r->KdKpbc; 
			$responce->rows[$i]['JnBC25']=$r->JnBC25; 
			$responce->rows[$i]['JnBarang']=$r->JnBarang; 
			$responce->rows[$i]['Kondisi']=$r->Kondisi; 
			$responce->rows[$i]['Dari']=$r->Dari; 
			$responce->rows[$i]['Tujuan']=$r->Tujuan; 
			$responce->rows[$i]['TujuanKirim']=$r->TujuanKirim; 
			$responce->rows[$i]['UsahaID']=$r->UsahaID; 
			$responce->rows[$i]['UsahaNpwp']=$r->UsahaNpwp; 
			$responce->rows[$i]['UsahaNama']=$r->UsahaNama; 
			$responce->rows[$i]['UsahaAlmt']=$r->UsahaAlmt; 
			$responce->rows[$i]['UsahaStatus']=$r->UsahaStatus; 
			$responce->rows[$i]['PenerimaID']=$r->PenerimaID; 
			$responce->rows[$i]['PenerimaNpwp']=$r->PenerimaNpwp; 
			$responce->rows[$i]['PenerimaNama']=$r->PenerimaNama; 
			$responce->rows[$i]['PenerimaAlmt']=$r->PenerimaAlmt; 
			$responce->rows[$i]['PenerimaNiper']=$r->PenerimaNiper; 
			$responce->rows[$i]['PenerimaStatus']=$r->PenerimaStatus; 
			$responce->rows[$i]['TempatPenimbunan']=$r->TempatPenimbunan; 
			$responce->rows[$i]['TgKeluar']=$r->TgKeluar; 
			$responce->rows[$i]['TglMusnah']=$r->TglMusnah; 
			$responce->rows[$i]['BAMusnah']=$r->BAMusnah; 
			$responce->rows[$i]['KdVal']=$r->KdVal; 
			$responce->rows[$i]['Ndpbm']=$r->Ndpbm; 
			$responce->rows[$i]['HargaRp']=$r->HargaRp; 
			$responce->rows[$i]['HargaBBRp']=$r->HargaBBRp; 
			$responce->rows[$i]['Bruto']=$r->Bruto; 
			$responce->rows[$i]['Netto']=$r->Netto; 
			$responce->rows[$i]['JmBrg']=$r->JmBrg; 
			$responce->rows[$i]['JmBB']=$r->JmBB; 
			$responce->rows[$i]['JmCont']=$r->JmCont; 
			$responce->rows[$i]['KotaTtd']=$r->KotaTtd; 
			$responce->rows[$i]['TglTtd']=$r->TglTtd; 
			$responce->rows[$i]['NamaTtd']=$r->NamaTtd; 
			$responce->rows[$i]['KotattdPenerima']=$r->KotattdPenerima; 
			$responce->rows[$i]['TglTtdPenerima']=$r->TglTtdPenerima; 
			$responce->rows[$i]['NamaTtdPenerima']=$r->NamaTtdPenerima; 
			$responce->rows[$i]['TmpPeriksaFisik']=$r->TmpPeriksaFisik; 
			$responce->rows[$i]['AlmtPeriksaFisik']=$r->AlmtPeriksaFisik; 
			$responce->rows[$i]['TgPeriksaFisik']=$r->TgPeriksaFisik; 
			$responce->rows[$i]['WkPeriksaFisik']=$r->WkPeriksaFisik; 
			$responce->rows[$i]['ContakPerson']=$r->ContakPerson; 
			$responce->rows[$i]['NoPhone']=$r->ContakPerson; 
			$responce->rows[$i]['jml_inv']=$r->ContakPerson; 
			$responce->rows[$i]['typedok']=$r->typedok; 
			$responce->rows[$i]['create_date']=$r->create_date; 
			$responce->rows[$i]['create_by']=$r->create_by; 
			$responce->rows[$i]['update_date']=$r->update_date; 
			$responce->rows[$i]['update_by']=$r->update_by; 
			$responce->rows[$i]['nip_bc']=$r->nip_bc; 
			$responce->rows[$i]['nama_bc']=$r->nama_bc; 
		
			$i++;
		}
		echo json_encode($responce);
	}
	
	function loaddata_bc25_gettassy_dtl(){
		$search = isset($_POST['_search'])?$_POST['_search']:false;
        $searchField = isset($_POST['searchField'])?$_POST['searchField']:'';
        $searchString = isset($_POST['searchString'])?$_POST['searchString']:'';
        $filters =  isset($_POST['filters'])?$_POST['filters']:'';
		
		$page = isset($_POST['page'])?$_POST['page']:1; // get the requested page
        $limit = isset($_POST['rows'])?$_POST['rows']:20; // get how many rows we want to have into the grid
        $sidx = isset($_POST['sidx'])?$_POST['sidx']:'id_sa_hdr'; // get index row - i.e. user click to sort
        $sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		$id_sa_hdr = (isset($_POST['id_sa_hdr_dtl']) && !empty($_POST['id_sa_hdr_dtl']))?$_POST['id_sa_hdr_dtl']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		
		$where = " 1=1 and id_sa_hdr=$id_sa_hdr ";
				
		if ($search == 'true'){
			if (!empty($filters)){
				$array = json_decode($filters);
				foreach($array->rules as $arr=>$k){
					
					if ( $k->field == "create_date" ) {
						$searchString = $k->data;
						$where .= "  and CONVERT(VARCHAR(10),[create_date],101)  =  '$searchString'"; 
					}  else if  ($k->field == "SA_NO" ) {
						$searchString = $k->data;
						$where .= " and [SA_NO] like  '%".$searchString."%'"; 
					}
				}
			}
		}
		
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_bc25->getassybc25viewdataAll($limit,$start,$sidx,$sord, $where);
        $count = $this->mdl_bc25->getassybc25viewCountAll($where);
	//	var_dump($records); die();
		
		if( $count > 0 ) { $total_pages = ceil($count/$limit); } else { $total_pages = 0; } 
        if ($page > $total_pages) $page=$total_pages;
        
        $responce->page = $page;
		$responce->total = $total_pages;
		$responce->records =  $count;
		$i=0;
        foreach($records as $r){
				
			$responce->rows[$i]['id_sa']=$r->id_sa; 
			$responce->rows[$i]['id_sa_hdr']=$r->id_sa_hdr; 
			$responce->rows[$i]['SA_NO']=$r->SA_NO; 
			$responce->rows[$i]['NPWP_PEMBELI']=$r->NPWP_PEMBELI; 
			$responce->rows[$i]['NAMA_PEMBELI']=$r->NAMA_PEMBELI; 
			$responce->rows[$i]['INV_NO']=$r->INV_NO; 
			$responce->rows[$i]['ASSY_CODE']=$r->ASSY_CODE; 
			$responce->rows[$i]['ASSY_NO']=$r->ASSY_NO; 
			$responce->rows[$i]['TOTAL_SET']=$r->TOTAL_SET; 
			$responce->rows[$i]['SATUAN']=$r->SATUAN; 
			$responce->rows[$i]['JUMLAH_COLI']=$r->JUMLAH_COLI; 
			$responce->rows[$i]['GROSSWEIGHT']=$r->GROSSWEIGHT; 
			$responce->rows[$i]['SATUANGROSS']=$r->SATUANGROSS; 
			$responce->rows[$i]['NETWEIGHT']=$r->NETWEIGHT; 
			$responce->rows[$i]['SATUANNET']=$r->SATUANNET; 
			$responce->rows[$i]['create_date']=$r->create_date; 
			$responce->rows[$i]['create_by']=$r->create_by; 
			$responce->rows[$i]['update_date']=$r->update_date; 
			$responce->rows[$i]['update_by']=$r->update_by; 
		
			
			$i++;
		}
		echo json_encode($responce);
	
	
	}
	
	function loaddata_bc25_getbb_dtl(){
		$search = isset($_POST['_search'])?$_POST['_search']:false;
        $searchField = isset($_POST['searchField'])?$_POST['searchField']:'';
        $searchString = isset($_POST['searchString'])?$_POST['searchString']:'';
        $filters =  isset($_POST['filters'])?$_POST['filters']:'';
		
		$page = isset($_POST['page'])?$_POST['page']:1; // get the requested page
        $limit = isset($_POST['rows'])?$_POST['rows']:20; // get how many rows we want to have into the grid
        $sidx = isset($_POST['sidx'])?$_POST['sidx']:'id_sa_hdr'; // get index row - i.e. user click to sort
        $sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		$id_sa_hdr = (isset($_POST['id_sa_hdr_dtl']) && !empty($_POST['id_sa_hdr_dtl']))?$_POST['id_sa_hdr_dtl']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		
		$where = " 1=1 and id_sa_hdr=$id_sa_hdr ";
				
		if ($search == 'true'){
			if (!empty($filters)){
				$array = json_decode($filters);
				foreach($array->rules as $arr=>$k){
					
					if ( $k->field == "create_date" ) {
						$searchString = $k->data;
						$where .= "  and CONVERT(VARCHAR(10),[create_date],101)  =  '$searchString'"; 
					}  else if  ($k->field == "SA_NO" ) {
						$searchString = $k->data;
						$where .= " and [SA_NO] like  '%".$searchString."%'"; 
					}
				}
			}
		}
		
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_bc25->getbbbc25viewdataAll($limit,$start,$sidx,$sord, $where);
        $count = $this->mdl_bc25->getbb25viewCountAll($where);
	//	var_dump($records); die();
		
		if( $count > 0 ) { $total_pages = ceil($count/$limit); } else { $total_pages = 0; } 
        if ($page > $total_pages) $page=$total_pages;
        
        $responce->page = $page;
		$responce->total = $total_pages;
		$responce->records =  $count;
		$i=0;
        foreach($records as $r){
				
			$responce->rows[$i]['id_sa_bb']=$r->id_sa_bb; 
			$responce->rows[$i]['id_sa_hdr']=$r->id_sa_hdr; 
			$responce->rows[$i]['SA_NO']=$r->SA_NO; 
			$responce->rows[$i]['NO_URUT']=$r->NO_URUT; 
			$responce->rows[$i]['PART_NAME']=$r->PART_NAME; 
			$responce->rows[$i]['QTY']=$r->QTY; 
			$responce->rows[$i]['UNIT']=$r->UNIT; 
			$responce->rows[$i]['STATUS']=$r->STATUS; 
			$responce->rows[$i]['CURR']=$r->CURR; 
			$responce->rows[$i]['PRICE']=$r->PRICE; 
			$responce->rows[$i]['CIF']=$r->CIF; 
			$responce->rows[$i]['NDPMB']=$r->NDPMB; 
			$responce->rows[$i]['NDPDM_NOW']=$r->NDPDM_NOW; 
			$responce->rows[$i]['HARGA_PABEAN']=$r->HARGA_PABEAN; 
			$responce->rows[$i]['HS']=$r->HS; 
			$responce->rows[$i]['BM']=$r->BM; 
			$responce->rows[$i]['PPN']=$r->PPN; 
			$responce->rows[$i]['PPH']=$r->PPH; 
			$responce->rows[$i]['BM_RP']=$r->BM_RP; 
			$responce->rows[$i]['PPN_RP']=$r->PPN_RP; 
			$responce->rows[$i]['PPH_RP']=$r->PPH_RP; 
			$responce->rows[$i]['create_date']=$r->create_date; 
			$responce->rows[$i]['create_by']=$r->create_by; 
			$responce->rows[$i]['update_date']=$r->update_date; 
			$responce->rows[$i]['update_by']=$r->update_by; 
			
			
			$i++;
		}
		echo json_encode($responce);
	}
	
	function loaddata_sa_dok_dtl(){
		$search = isset($_POST['_search'])?$_POST['_search']:false;
        $searchField = isset($_POST['searchField'])?$_POST['searchField']:'';
        $searchString = isset($_POST['searchString'])?$_POST['searchString']:'';
        $filters =  isset($_POST['filters'])?$_POST['filters']:'';
		
		$page = isset($_POST['page'])?$_POST['page']:1; // get the requested page
        $limit = isset($_POST['rows'])?$_POST['rows']:20; // get how many rows we want to have into the grid
        $sidx = isset($_POST['sidx'])?$_POST['sidx']:'id_sa_hdr'; // get index row - i.e. user click to sort
        $sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		$id_sa_hdr = (isset($_POST['id_sa_hdr_dtl']) && !empty($_POST['id_sa_hdr_dtl']))?$_POST['id_sa_hdr_dtl']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		
		$where = " 1=1 and id_sa_hdr=$id_sa_hdr ";
				
		if ($search == 'true'){
			if (!empty($filters)){
				$array = json_decode($filters);
				foreach($array->rules as $arr=>$k){
					
				    if  ($k->field == "SA_NO" ) {
						$searchString = $k->data;
						$where .= " and [SA_NO] like  '%".$searchString."%'"; 
					}
				}
			}
		}
		
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_bc25->viewbc25_dokAll($limit,$start,$sidx,$sord, $where);
        $count = $this->mdl_bc25->viewbc25_dokCountAll($where);
	//	var_dump($records); die();
		
		if( $count > 0 ) { $total_pages = ceil($count/$limit); } else { $total_pages = 0; } 
        if ($page > $total_pages) $page=$total_pages;
        
        $responce->page = $page;
		$responce->total = $total_pages;
		$responce->records =  $count;
		$i=0;
        foreach($records as $r){
				
			
			$responce->rows[$i]['SA_NO']=$r->SA_NO; 
			$responce->rows[$i]['INV_NO']=$r->INV_NO; 
			
			$i++;
		}
		echo json_encode($responce);
	}
	
	function view_bc25_print(){
	
		$this->load->view('exim/view_bc25_print');
	
	}
	
	function print_instruksi_pemeriksaan(){
	    
		$id_sa_hdr = (isset($_REQUEST['id_sa_hdr_dtl']) && !empty($_REQUEST['id_sa_hdr_dtl']))?$_REQUEST['id_sa_hdr_dtl']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
			
		$this->load->library('java_bridge');
		$cekjava=$this->java_bridge->checkJavaExtension(); 
		$bcreportdir = $this->config->item('APP_dirreport');
		if($cekjava){
			$irep = $bcreportdir.'\rpt_ip.jrxml';
			$compileManager = new JavaClass("net.sf.jasperreports.engine.JasperCompileManager");
			$report = $compileManager->compileReport($irep);
			$fillManager = new JavaClass("net.sf.jasperreports.engine.JasperFillManager");
			$params = new Java("java.util.HashMap");
			$params->put("nama_perusahaan", "PT. EDS MANUFACTURING INDONESIA");
			$params->put("id_sa_hdr",  $id_sa_hdr);
			
			$class = new JavaClass("java.lang.Class");
			$class->forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			$driverManager = new JavaClass("java.sql.DriverManager");

			//db username and password
			$conn = $driverManager->getConnection($jdburl, $db['default']['username'], $db['default']['password']);
			
			$jasperPrint = $fillManager->fillReport($report, $params, $conn);
			$outputPath = realpath(".")."\ireport/"."instruksipemeriksaan.pdf";
			$exportManager = new JavaClass("net.sf.jasperreports.engine.JasperExportManager");
			$exportManager->exportReportToPdfFile($jasperPrint, $outputPath);
			header("Content-type: application/pdf");
			//header("Content-Disposition: attachment; filename=output.pdf"); //langsung dowload
			//$exporter->exportReport();
			readfile($outputPath);
			@unlink($outputPath);
			
		}
		
	}
	
	function print_ppbi(){
		$id_sa_hdr = (isset($_REQUEST['id_sa_hdr_dtl']) && !empty($_REQUEST['id_sa_hdr_dtl']))?$_REQUEST['id_sa_hdr_dtl']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$this->load->library('java_bridge');
		$cekjava=$this->java_bridge->checkJavaExtension(); 
		$bcreportdir = $this->config->item('APP_dirreport');
		if($cekjava){
			$irep = $bcreportdir.'\rpt_ppbi.jrxml';
			$compileManager = new JavaClass("net.sf.jasperreports.engine.JasperCompileManager");
			$report = $compileManager->compileReport($irep);
			$fillManager = new JavaClass("net.sf.jasperreports.engine.JasperFillManager");
			$params = new Java("java.util.HashMap");
			$params->put("nama_perusahaan", "PT. EDS MANUFACTURING INDONESIA");
			$params->put("id_sa_hdr",  $id_sa_hdr);
			
			$class = new JavaClass("java.lang.Class");
			$class->forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			$driverManager = new JavaClass("java.sql.DriverManager");

			//db username and password
			$conn = $driverManager->getConnection($jdburl, $db['default']['username'], $db['default']['password']);
			
			$jasperPrint = $fillManager->fillReport($report, $params, $conn);
			$outputPath = realpath(".")."\ireport/"."ppbi.pdf";
			$exportManager = new JavaClass("net.sf.jasperreports.engine.JasperExportManager");
			$exportManager->exportReportToPdfFile($jasperPrint, $outputPath);
			header("Content-type: application/pdf");
			//header("Content-Disposition: attachment; filename=output.pdf"); //langsung dowload
			//$exporter->exportReport();
			readfile($outputPath);
			@unlink($outputPath);
			
		}
	
	
	}
	
	function print_sppbi(){
	
		$id_sa_hdr = (isset($_REQUEST['id_sa_hdr_dtl']) && !empty($_REQUEST['id_sa_hdr_dtl']))?$_REQUEST['id_sa_hdr_dtl']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$this->load->library('java_bridge');
		$cekjava=$this->java_bridge->checkJavaExtension(); 
		$bcreportdir = $this->config->item('APP_dirreport');
		if($cekjava){
			$irep = $bcreportdir.'\rpt_sppb.jrxml';
			$compileManager = new JavaClass("net.sf.jasperreports.engine.JasperCompileManager");
			$report = $compileManager->compileReport($irep);
			$fillManager = new JavaClass("net.sf.jasperreports.engine.JasperFillManager");
			$params = new Java("java.util.HashMap");
			$params->put("nama_perusahaan", "PT. EDS MANUFACTURING INDONESIA");
			$params->put("id_sa_hdr",  $id_sa_hdr);
			
			$class = new JavaClass("java.lang.Class");
			$class->forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			$driverManager = new JavaClass("java.sql.DriverManager");

			//db username and password
			$conn = $driverManager->getConnection($jdburl, $db['default']['username'], $db['default']['password']);
			
			$jasperPrint = $fillManager->fillReport($report, $params, $conn);
			$outputPath = realpath(".")."\ireport/"."rpt_sppb.pdf";
			$exportManager = new JavaClass("net.sf.jasperreports.engine.JasperExportManager");
			$exportManager->exportReportToPdfFile($jasperPrint, $outputPath);
			header("Content-type: application/pdf");
			//header("Content-Disposition: attachment; filename=output.pdf"); //langsung dowload
			//$exporter->exportReport();
			readfile($outputPath);
			@unlink($outputPath);
			
		}
	
	}
	
	function print_bc25lampiran(){
		$id_sa_hdr = (isset($_REQUEST['id_sa_hdr_dtl']) && !empty($_REQUEST['id_sa_hdr_dtl']))?$_REQUEST['id_sa_hdr_dtl']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$this->load->library('java_bridge');
		$cekjava=$this->java_bridge->checkJavaExtension(); 
		$bcreportdir = $this->config->item('APP_dirreport');
		if($cekjava){
			$irep = $bcreportdir.'\rpt_lampiran.jrxml';
			$compileManager = new JavaClass("net.sf.jasperreports.engine.JasperCompileManager");
			$report = $compileManager->compileReport($irep);
			$fillManager = new JavaClass("net.sf.jasperreports.engine.JasperFillManager");
			$params = new Java("java.util.HashMap");
			$params->put("nama_perusahaan", "PT. EDS MANUFACTURING INDONESIA");
			$params->put("id_sa_hdr",  $id_sa_hdr);
			
			$class = new JavaClass("java.lang.Class");
			$class->forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			$driverManager = new JavaClass("java.sql.DriverManager");

			//db username and password
			$conn = $driverManager->getConnection($jdburl, $db['default']['username'], $db['default']['password']);
			
			$jasperPrint = $fillManager->fillReport($report, $params, $conn);
			$outputPath = realpath(".")."\ireport/"."rpt_lampiranbc25.pdf";
			$exportManager = new JavaClass("net.sf.jasperreports.engine.JasperExportManager");
			$exportManager->exportReportToPdfFile($jasperPrint, $outputPath);
			header("Content-type: application/pdf");
			//header("Content-Disposition: attachment; filename=output.pdf"); //langsung dowload
			//$exporter->exportReport();
			readfile($outputPath);
			@unlink($outputPath);
		}
		
		
		
	}
	
	function prosesdatatofg(){
		$id_bc_out = (isset($_POST['id_bc_out']) && !empty($_POST['id_bc_out']))?$_POST['id_bc_out']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$typedok = (isset($_POST['typedok']) && !empty($_POST['typedok']))?$_POST['typedok']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		
		$username = $this->session->userdata('DX_username');
      
		$qry = $this->db->query("
			BEGIN TRY
			
			 exec('[spi_prosesdatatofg] @id_bc_out=$id_bc_out, @typedok=''$typedok'', @username=''$username'' ')
			
			END TRY
			BEGIN CATCH 
				SELECT 
				'06' AS status
				,ERROR_MESSAGE() AS description;
				
			END CATCH;
			
			");
		 $rs = $qry->result_array();
		 // $r = $qry->result();		
		 // var_dump($r);die();
		 foreach ($rs as $row){
			 $status = $row['status'];
			 $msg = $row['description'];
		 }  
		//  echo json_encode($responce);
		 // $stmt = " exec('[spi_prosesdatatofg]') ";
		 // $qry = mssql_query($stmt); 
		 // $row = mssql_fetch_array($qry); 
		 // var_dump($row);die();
		
		
		$q = $this->db->query("
			SELECT  id_terkait, doc_out_no, id_data_out_hdr  from [tbl_data_out_hdr] where id_terkait=$id_bc_out and doc_out_to_id=3
		");
		
		$rs = $q->result_array();
		//var_dump($rs); die();
		$id_data_out_hdr='';
		foreach ($rs as $row){
			$id_data_out_hdr=$row['id_data_out_hdr'];		
		}
		
		
		$status = $status;
		$msg = $msg;
		
		echo json_encode(array('sts' => $status, 'desc' => $msg, 'id_bc_out' =>$id_data_out_hdr ));
	
	}
	
	
  
  
 }