<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 
class CT_bc262 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->helper('obj2array');
		
		$this->load->model("exim/mdl_bc27", 'mdl_bc27');
		$this->load->model("exim/mdl_bc23", 'mdl_bc23');
		$this->load->model("exim/mdl_bc261", 'mdl_bc261');
		$this->load->model("exim/mdl_invoice", 'mdl_invoice');
		$this->load->helper('afunction');
	}
  
	function index(){

		$this->load->view('exim/view_bc262');
	}

	function view_bc262_invoice(){
		$this->load->view('exim/view_bc262_invoice');
	}
	
	function data_upload_packinglist262(){
		$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_upload_packinglist'; // 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 == "filename" ) {
						$searchString = $k->data;
						$where .= " and filename like  '%".$searchString."%'"; 
					} else if ( $k->field == "date_upload" ) {
						$searchString = $k->data;
						$where .= " and CONVERT(VARCHAR(10),[date_upload],101)  =  '$searchString'"; 
					} else if ( $k->field == "status_upload" ) {
						$searchString = $k->data;
						$where .= " and status_upload like  '%".$searchString."%'"; 
					}  else if ( $k->field == "create_by" ) {
						$searchString = $k->data;
						$where .= " and create_by like  '%".$searchString."%'"; 
					} 
				}
			}
		}
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_bc261->upload_packinglist_262All($limit,$start,$sidx,$sord, $where);
        $count = $this->mdl_bc261->upload_packinglist_262countAll($where);
		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_upload_packinglist']=$r->id_upload_packinglist; 
			$responce->rows[$i]['date_upload']=$r->date_upload; 
			$responce->rows[$i]['filename']=$r->filename; 
			$responce->rows[$i]['status_upload']=$r->status_upload;
			$responce->rows[$i]['create_date']=$r->create_date;
			$responce->rows[$i]['create_by']=$r->create_by;
			$responce->rows[$i]['tipe']=$r->tipe;
			
			$i++;
		}
		echo json_encode($responce);	
	
	}
	
	function upload_file_packing_list262(){
		$status = "";
	   $msg = "";
	   $file_element_name = 'userfilepacking_list262';
	   //$username = $this->session->userdata('DX_user_id');
	   $username = $this->session->userdata('DX_username');
	   $tipe = 'packinglist262';
	   if ($status != "error")
	   {
		  $apppath = $this->config->item('APP_upload_path');
		  $config['upload_path'] = $apppath.'exim/packing_list262/';
		  $config['allowed_types'] = 'xls';
		  $config['max_size']  = 1024 * 100;
		  $config['encrypt_name'] = false;
		  $config['overwrite']=true;
		  $config['remove_spaces']=true;
		  $this->load->library('upload', $config);
		  if (!$this->upload->do_upload($file_element_name))
		  {
			 $status = 'error';
			 $msg = $this->upload->display_errors('', '');
		  }
		  else
		  {
			 $data = $this->upload->data();
			 
			 // print_r($data);
			 // die();
			 if($data)
			 {
				$myFile = $data['full_path']; 
				$newfile=$apppath.'exim/packing_list262/log/'.$data['file_name'];
				$filename=$data['file_name'];
				$dirfile = $apppath.'exim/packing_list262/';

			/* 	echo " exec('[spuload_perhitungan_exim] @filepath=''$dirfile'', @filename=''$filename'', @username=''$username'', @tipe=''$tipe'', @trx=''add'', @id=0 ') ";
				die();	 */	
				$qry = $this->db->query("
					BEGIN TRY
					
					 exec('[spuload_packinglist262] @filepath=''$dirfile'', @filename=''$filename'', @username=''$username'', @tipe=''$tipe'', @trx=''add'', @id=0 ')
					
					END TRY
					BEGIN CATCH 
						SELECT 
						'error' 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('[spuload_parths]') ";
				 // $qry = mssql_query($stmt); 
				 // $row = mssql_fetch_array($qry); 
				 // var_dump($row);die();
						
				
				
				
				$status = $status;
				$msg = $msg;
			 }
			 else
			 {
				unlink($data['full_path']);
				$status = "error";
				$msg = "Something went wrong when saving the file, please try again.";
			 }
		  }
		  @unlink($_FILES[$file_element_name]);
	   }
	   echo json_encode(array('status' => $status, 'msg' => $msg));
	
	
	}
	
	function proses_packinglistuploadbc262(){
		$id = (isset($_POST['id_upload_packinglist']) && !empty($_POST['id_upload_packinglist']))?$_POST['id_upload_packinglist']:die('{"sts":"ERROR","desc":" Param ID Tidak Ditemukan"}');
		$filename = (isset($_POST['filename']) && !empty($_POST['filename']))?$_POST['filename']:die('{"sts":"ERROR","desc":" Param ID Tidak Ditemukan"}');
		$username = $this->session->userdata('DX_username');
		$tipe = 'packinglist262';
		//echo "exec('[spi_proses_packinglist262] @filename=''$filename'', @id_upload_packinglist=$id, @tipe=''$tipe'',  @username=''$username'' ')"; die();
		$qry = $this->db->query("
			BEGIN TRY
			  exec('[spi_proses_packinglist262] @filename=''$filename'', @id_upload_packinglist=$id, @tipe=''$tipe'',  @username=''$username'' ')
			END TRY
			BEGIN CATCH 
				SELECT 
				'error' AS status
				,ERROR_MESSAGE() AS description;
				
			END CATCH; 
			
			 ");
		 $rs = $qry->result_array();
         //// $r = $qry->result();		
		//// var_dump($r);die();
		 foreach ($rs as $row){
			 $responce = array("sts" => $row['status'], "desc" => $row['description'].' ');
		 }  
		 echo json_encode($responce);			
	}
	
	
	function data_v_packinglistbc262(){
		$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']:'no_packinglist'; // 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 == "no_packinglist" ) {
						$searchString = $k->data;
						$where .= " and no_packinglist like  '%".$searchString."%'"; 
					} else if ( $k->field == "tgl_packinglist" ) {
						$searchString = $k->data;
						$where .= " and CONVERT(VARCHAR(10),[tgl_packinglist],101)  =  '$searchString'"; 
					} else if ( $k->field == "status" ) {
						$searchString = $k->data;
						$where .= " and status like  '%".$searchString."%'"; 
					} 
				}
			}
		}
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_bc261->data_v_packinglistbc262all($limit,$start,$sidx,$sord, $where);
        $count = $this->mdl_bc261->data_v_packinglistbc262countall($where);
		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]['no_packinglist']=$r->no_packinglist; 
			$responce->rows[$i]['tgl_packinglist']=$r->tgl_packinglist; 
			$responce->rows[$i]['status']=$r->status;
			$responce->rows[$i]['no_kontrak']=$r->no_kontrak; 
			$responce->rows[$i]['tgl_kontrak']=$r->tgl_kontrak;
			$responce->rows[$i]['no_surat_jalan']=$r->no_surat_jalan;
			$responce->rows[$i]['tgl_surat_jalan']=$r->tgl_surat_jalan;
			$responce->rows[$i]['id_bc_hdr']=$r->id_bc_hdr;
			$responce->rows[$i]['id_bc261']=$r->id_bc261;
			$responce->rows[$i]['kurs']=$r->kurs;
			$responce->rows[$i]['no_bc262']=$r->no_bc262;
			$responce->rows[$i]['tgl_bc262']=$r->tgl_bc262;
			$responce->rows[$i]['ex_no_bc261']=$r->ex_no_bc261;
			$responce->rows[$i]['ex_tgl_bc261']=$r->ex_tgl_bc261;
			$i++;
		}
		echo json_encode($responce);	
	
	}
	
	function data_packinglist_bc262(){
		$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_packinglist'; // get index row - i.e. user click to sort
        $sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		
		$where = " 1=1 ";
		
		if(isset($_POST['BB']) && !empty($_POST['BB'])){
			$bb = $_POST['BB'];
			if ($bb == 'XXXX'){
				$no_packinglistbc262 = (isset($_POST['no_packinglistbc262']) && !empty($_POST['no_packinglistbc262']))?$_POST['no_packinglistbc262']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
				$tgl_packinglistbc262 = (isset($_POST['tgl_packinglistbc262']) && !empty($_POST['tgl_packinglistbc262']))?$_POST['tgl_packinglistbc262']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');	
				$where = " no_packinglist = '$no_packinglistbc262' and tgl_packinglist = '$tgl_packinglistbc262' ";
			}
		}
		
		if ($search == 'true'){
			if (!empty($filters)){
				$array = json_decode($filters);
				foreach($array->rules as $arr=>$k){
					
					if ( $k->field == "no_packinglist" ) {
						$searchString = $k->data;
						$where .= " and no_packinglist like  '%".$searchString."%'"; 
					} else if ( $k->field == "tgl_packinglist" ) {
						$searchString = $k->data;
						$where .= " and CONVERT(VARCHAR(10),[tgl_packinglist],101)  =  '$searchString'"; 
					} else if ( $k->field == "status" ) {
						$searchString = $k->data;
						$where .= " and status like  '%".$searchString."%'"; 
					} 
				}
			}
		}
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_bc261->data_packinglist_bc262all($limit,$start,$sidx,$sord, $where);
        $count = $this->mdl_bc261->data_packinglist_bc262countall($where);
		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_packinglist']=$r->id_packinglist; 
			$responce->rows[$i]['no_kontrak']=$r->no_kontrak; 
			$responce->rows[$i]['tgl_kontrak']=$r->tgl_kontrak;
			$responce->rows[$i]['no_packinglist']=$r->no_packinglist;
			$responce->rows[$i]['tgl_packinglist']=$r->tgl_packinglist;
			$responce->rows[$i]['assy_no']=$r->assy_no;
			$responce->rows[$i]['qty']=$r->qty;
			$responce->rows[$i]['qty_box']=$r->qty_box;
			$responce->rows[$i]['unit']=$r->unit;
			$responce->rows[$i]['price']=$r->price;
			$responce->rows[$i]['amount']=$r->amount;
			$responce->rows[$i]['kurs']=$r->kurs;
			$responce->rows[$i]['no_surat_jalan']=$r->no_surat_jalan;
			$responce->rows[$i]['tgl_surat_jalan']=$r->tgl_surat_jalan;
			$responce->rows[$i]['id_bc_hdr']=$r->id_bc_hdr;
			$responce->rows[$i]['id_bc261']=$r->id_bc261;
			$responce->rows[$i]['status']=$r->status;
			
			$i++;
		}
		echo json_encode($responce);	
	
	}
	
	function view_bc262_invoice_dtl(){
		$no_packinglist = (isset($_POST['no_packinglist']) && !empty($_POST['no_packinglist']))?$_POST['no_packinglist']:die('{"sts":"ERROR","desc":" Param ID Tidak Ditemukan"}');
		$tgl_packinglist = (isset($_POST['tgl_packinglist']) && !empty($_POST['tgl_packinglist']))?$_POST['tgl_packinglist']:die('{"sts":"ERROR","desc":" Param ID Tidak Ditemukan"}');
		$data['no_packinglist'] = $no_packinglist; 
		$data['tgl_packinglist'] = $tgl_packinglist; 
		
		$this->load->view('exim/view_bc262_invoice_dtl', $data);
		
	
	}
	
	function view_bc262_invoice_form(){
		$no_packinglist = (isset($_POST['no_packinglistbc262']) && !empty($_POST['no_packinglistbc262']))?$_POST['no_packinglistbc262']:die('{"sts":"ERROR","desc":" Param ID Tidak Ditemukan"}');
		$tgl_packinglist = (isset($_POST['tgl_packinglistbc262']) && !empty($_POST['tgl_packinglistbc262']))?$_POST['tgl_packinglistbc262']:die('{"sts":"ERROR","desc":" Param ID Tidak Ditemukan"}');
		$data['no_packinglist'] = $no_packinglist; 
		$data['tgl_packinglist'] = $tgl_packinglist; 
		
		$this->load->view('exim/view_bc262_invoice_form', $data);
	
	}
	
	function proses_update_dtl_bc262(){
		$no_packinglistbc262_from = (isset($_POST['no_packinglistbc262_from']) && !empty($_POST['no_packinglistbc262_from']))?$_POST['no_packinglistbc262_from']:die('{"sts":"ERROR","desc":" Param ID Tidak Ditemukan"}');
		$tgl_packinglistbc262_from = (isset($_POST['tgl_packinglistbc262_from']) && !empty($_POST['tgl_packinglistbc262_from']))?$_POST['tgl_packinglistbc262_from']:die('{"sts":"ERROR","desc":" Param ID Tidak Ditemukan"}');
		
		$txtnokontrak = (isset($_POST['txtnokontrak']) && !empty($_POST['txtnokontrak']))?$_POST['txtnokontrak']:die('{"sts":"ERROR","desc":" Param ID Tidak Ditemukan"}');
		$txttglkontrak = (isset($_POST['txttglkontrak']) && !empty($_POST['txttglkontrak']))?$_POST['txttglkontrak']:die('{"sts":"ERROR","desc":" Param ID Tidak Ditemukan"}');
		$txtsuratjalan = (isset($_POST['txtsuratjalan']) && !empty($_POST['txtsuratjalan']))?$_POST['txtsuratjalan']:die('{"sts":"ERROR","desc":" Param ID Tidak Ditemukan"}');
		$txttanggalsuratjalan = (isset($_POST['txttanggalsuratjalan']) && !empty($_POST['txttanggalsuratjalan']))?$_POST['txttanggalsuratjalan']:die('{"sts":"ERROR","desc":" Param ID Tidak Ditemukan"}');
		$txtkurs = (isset($_POST['txtkurs']) && !empty($_POST['txtkurs']))?$_POST['txtkurs']:die('{"sts":"ERROR","desc":" Param ID Tidak Ditemukan"}');
		
		$qry = $this->db->query("
			BEGIN TRY
				update tbl_packinglist_bc262 set 
				[no_kontrak] = '$txtnokontrak'
				,[tgl_kontrak] = '$txttglkontrak'
				,[no_surat_jalan] = '$txtsuratjalan'
				,[tgl_surat_jalan] = '$txttanggalsuratjalan'
				,[kurs] = $txtkurs
				where no_packinglist = '$no_packinglistbc262_from' and [tgl_packinglist] = '$tgl_packinglistbc262_from'
			END TRY
			BEGIN CATCH 
				SELECT 
				'error' AS status
				,ERROR_MESSAGE() AS description;
				
			END CATCH; 
			select '00' as status, 'DATA UPDATED' as description
			
			 ");
		 $rs = $qry->result_array();
         //// $r = $qry->result();		
		//// var_dump($r);die();
		 foreach ($rs as $row){
			 $responce = array("sts" => $row['status'], "desc" => $row['description'].' ');
		 }  
		 echo json_encode($responce);	
	
	
	}
	
	function proses_bc262(){
		$no_packinglistbc262_proses = (isset($_POST['no_packinglistbc262_proses']) && !empty($_POST['no_packinglistbc262_proses']))?$_POST['no_packinglistbc262_proses']:die('{"sts":"ERROR","desc":" Param ID Tidak Ditemukan"}');
		$tgl_packinglistbc262_proses = (isset($_POST['tgl_packinglistbc262_proses']) && !empty($_POST['tgl_packinglistbc262_proses']))?$_POST['tgl_packinglistbc262_proses']:die('{"sts":"ERROR","desc":" Param ID Tidak Ditemukan"}');
		$no_kontranbc262_proses = (isset($_POST['no_kontranbc262_proses']) && !empty($_POST['no_kontranbc262_proses']))?$_POST['no_kontranbc262_proses']:die('{"sts":"ERROR","desc":" Param ID Tidak Ditemukan"}');
		$tgl_kontranbc262_proses = (isset($_POST['tgl_kontranbc262_proses']) && !empty($_POST['tgl_kontranbc262_proses']))?$_POST['tgl_kontranbc262_proses']:die('{"sts":"ERROR","desc":" Param ID Tidak Ditemukan"}');
		
		$no_bc262 = (isset($_POST['no_bc262']) && !empty($_POST['no_bc262']))?$_POST['no_bc262']:die('{"sts":"ERROR","desc":" Param ID Tidak Ditemukan"}');
		$tgl_bc262 = (isset($_POST['tgl_bc262']) && !empty($_POST['tgl_bc262']))?$_POST['tgl_bc262']:die('{"sts":"ERROR","desc":" Param ID Tidak Ditemukan"}');
		$no_ex_bc261 = (isset($_POST['no_ex_bc261']) && !empty($_POST['no_ex_bc261']))?$_POST['no_ex_bc261']:die('{"sts":"ERROR","desc":" Param ID Tidak Ditemukan"}');
		$tgl_ex_bc261 = (isset($_POST['tgl_ex_bc261']) && !empty($_POST['tgl_ex_bc261']))?$_POST['tgl_ex_bc261']:die('{"sts":"ERROR","desc":" Param ID Tidak Ditemukan"}');
		$kurs = (isset($_POST['kurs']) && !empty($_POST['kurs']))?$_POST['kurs']:die('{"sts":"ERROR","desc":" Param ID Tidak Ditemukan"}');
		$uid = $this->session->userdata('DX_username');
		
		$qry = $this->db->query("
			declare @tgl date
			set @tgl=GETDATE()
			declare @no_packinglist varchar(50),  @tgl_packinglist date, @no_kontrak varchar(50), @tgl_kontrak date, @no_surat_jalan varchar(50), @tgl_surat_jalan date
			declare @PasokNama varchar(50), @PasokAlmt varchar(100), @PpjkNama varchar(50),  @KdVal varchar(5), @Ndpbm float, @no_bc262 varchar(50), @tgl_bc262 date
			
			if (select count(*) from [dbo].[v_packinglistbc262] where [no_packinglist] = '$no_packinglistbc262_proses' and [tgl_packinglist] = '$tgl_packinglistbc262_proses') = 0
			begin
				select '06' as status, 'DATA TIDAK DITEMUKAN' as description
				return
			end
			
			SELECT top 1
			  @no_packinglist=[no_packinglist]
			  ,@tgl_packinglist=[tgl_packinglist]
			  ,@no_kontrak=[no_kontrak]
			  ,@tgl_kontrak=[tgl_kontrak]
			  ,@no_surat_jalan=[no_surat_jalan]
			  ,@tgl_surat_jalan=[tgl_surat_jalan]
			  ,@Ndpbm=[kurs]
			  ,@no_bc262=[no_bc262]
			  ,@tgl_bc262=[tgl_bc262]
			
			FROM [dbo].[v_packinglistbc262]
			WHERE [no_packinglist] = '$no_packinglistbc262_proses' and [tgl_packinglist] = '$tgl_packinglistbc262_proses'
			
			
			
			if (select count(*) from [dbo].[tblbc261_perhitungan_exim_hdr] where [NOKONTRAK]=@no_kontrak  and  [TGLKONTRAK]=@tgl_kontrak )	= 0
			begin	
				select '06' as status, 'DATA KONTRAK TIDAK DITEMUKAN' as description
				return
			end
			
			select top 1
				@PpjkNama=[NPWPTUJU]
				,@PasokNama=[NAMATUJU]
				,@PasokAlmt=[ALMTTUJU]
				,@KdVal=[kdval]	
			from 
				[dbo].[tblbc261_perhitungan_exim_hdr]
			where 
				[NOKONTRAK]=@no_kontrak  and  [TGLKONTRAK]=@tgl_kontrak 
					
			
			BEGIN TRY
				declare @idhdr bigint
				insert into dbo.tbl_bc_in_hdr (
					[CAR]
					,statusdok 
					,typedok
					,bc_in_from_in
					,KdKpbc
					,Tujuan
					,JnsBarang
					,TujuanKirim
					,PasokNama
					,PasokAlmt
					,PasokNeg
					,AngkutNama
					,AngkutFl
					,Moda
					,KdVal
					,NilInv
					,KdHrg
					,JmBrg
					,Registrasi
					,UsahaNama
					,UsahaAlmt
					,UsahaStatus
					,KdTPB
					,ApiKd
					,ApiNo
					,KdKpbcBongkar
					,KdKpbcAwas
					,Namattd
					,KotaTtd
					,nobl
					,tglbl
					,Ndpbm
					,[BC23No]
					,[BC23Tg]
					,[DokTupNo]
					,[DokTupTg]
					,create_by
					,update_by
					,update_date
					,create_date
					,tgaju
					
					) 
				select top 1
					'$no_bc262'
					,'DATA PROCESSED'
					,'BC262'
					,'INVSUBKON'
					,'150300'
					,'1'
					,'09' -- jenis barang subkon
					,'02'
					,@PasokNama
					,@PasokAlmt
					,'ID'
					,'MOBIL BOX'
					,'1'
					,''
					,'USD'
					,[nillinv]
					,'1'
					,[jmlbrg]
					,(select top 1 Registrasi from tblImportir)
					,(select top 1 impnama from tblImportir)
					,(select top 1 impalmt from tblImportir)
					,(select top 1 usahastatus from tblImportir)
					,(select top 1 KdTPB from tblImportir)
					,(select top 1 ApiKd from tblImportir)
					,(select top 1 ApiNo from tblImportir)
					,'150300'
					,'150300'
					,(select top 1 Namattd from tblImportir)
					,(select top 1 KotaTtd from tblImportir)
					,[no_surat_jalan]
					,[tgl_surat_jalan]
					,[kurs]
					,'$no_bc262'
					,'$tgl_bc262'
					,'$no_ex_bc261'
					,'$tgl_ex_bc261'
					,'$uid'
					,'$uid'
					,@tgl
					,@tgl
					,'$tgl_bc262'
					
				FROM [dbo].[v_packinglistbc262]
				WHERE [no_packinglist] = '$no_packinglistbc262_proses' and [tgl_packinglist] = '$tgl_packinglistbc262_proses'

				set @idhdr=@@IDENTITY
				
				insert into dbo.tbl_bc_in_dtl (
					[CAR]
					,Serial
					,BrgUrai
					,DNilInv
					,NoHs
					,id_bc_in_hdr
					,JnsBarangDtl
					,TujuanKirimDtl
					,SeriTrp
					,Merk
					,Tipe
					,KdBrg
					,JmlSat
					,KdSat
					,BrgAsal
					,Penggunaan
					,[KemasJn]
					,[KemasJm]

				) select 
				  '$no_bc262'
				  ,row_number() over(order by (select 0))	
				  ,assy_no
				  ,amount
				  ,'8544301900'
				  ,@idhdr
				  ,'01'
				  ,'02'
				  ,'0'
				  ,'-'
				  ,'-'
				  ,(select top 1 tbl_assy_list.ASSY_CODE from tbl_assy_list where  replace(tbl_assy_list.ASSY_NO,char(32), '')  = replace(tbl_packinglist_bc262.assy_no,char(32), '') )
				  ,[qty]
				  ,'SET'
				  ,'ID'
				  ,'1'
				  ,'BX'
				  ,[qty_box]	
				FROM  [dbo].[tbl_packinglist_bc262] WHERE [no_packinglist] = '$no_packinglistbc262_proses' and [tgl_packinglist] = '$tgl_packinglistbc262_proses' and [no_kontrak]=@no_kontrak and [tgl_kontrak] = @tgl_kontrak
				-- update dokumen
				insert into dbo.tbl_bc_in_dok (
					CAR
					,DokKd
					,DokNo
					,DokTg		
					,id_bc_in_hdr

				) values (
				   '$no_bc262'
				  ,'380'
				  ,@no_packinglist
				  ,@tgl_packinglist
				  ,@idhdr
				)
				
				update [dbo].[tbl_packinglist_bc262] set [no_bc262] = '$no_bc262',[tgl_bc262]='$tgl_bc262',[ex_no_bc261]='$no_ex_bc261'	,[ex_tgl_bc261]='$tgl_ex_bc261', [status] = 'DATA PROCESSED', id_bc_hdr=@idhdr  
				WHERE [no_packinglist] = @no_packinglist and [tgl_packinglist] = @tgl_packinglist 	
			
				
			END TRY
			BEGIN CATCH 
				SELECT 
				'error' AS status
				,ERROR_MESSAGE() AS description;
				
			END CATCH; 
			select '00' as status, 'DATA PROCCESSEDD' as description
			
			 ");
		 $rs = $qry->result_array();
         //// $r = $qry->result();		
		//// var_dump($r);die();
		 foreach ($rs as $row){
			 $responce = array("sts" => $row['status'], "desc" => $row['description'].' ');
		 }  
		 echo json_encode($responce);			
	
	}
	
	function updatenoaju_bc262(){
		$idhdrbc262 = (isset($_POST['idhdrbc262']) && !empty($_POST['idhdrbc262']))?$_POST['idhdrbc262']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$bc262noaju = (isset($_POST['bc262noaju']) && !empty($_POST['bc262noaju']))?$_POST['bc262noaju']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$bc261tglaju  = (isset($_POST['bc261tglaju']) && !empty($_POST['bc261tglaju']))?$_POST['bc261tglaju']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$bc262nobc262= (isset($_POST['bc262nobc262']) && !empty($_POST['bc262nobc262']))?$_POST['bc262nobc262']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$bc262tglbc262= (isset($_POST['bc262tglbc262']) && !empty($_POST['bc262tglbc262']))?$_POST['bc262tglbc262']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$bc261noaju= (isset($_POST['bc261noaju']) && !empty($_POST['bc261noaju']))?$_POST['bc261noaju']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$bc262tglaju= (isset($_POST['bc262tglaju']) && !empty($_POST['bc262tglaju']))?$_POST['bc262tglaju']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		
		$username = $this->session->userdata('DX_username');
		
		
		$qry = $this->db->query("
			BEGIN TRY
				update [dbo].[tbl_bc_in_hdr]  set 
					[DokTupNo] = '$bc261noaju'
					,[DokTupTg] = '$bc261tglaju'
					 ,[BC23No] = '$bc262nobc262'
					,[BC23Tg] = '$bc262tglbc262'
					,[CAR] ='$bc262noaju'
					,[tgaju] = '$bc262tglaju'
			    where id_bc_in_hdr = $idhdrbc262
				
				update tbl_bc_in_dtl set  
					[CAR] ='$bc262noaju'
				where id_bc_in_hdr = $idhdrbc262
				
				update tbl_bc_in_dok set  
					[CAR] ='$bc262noaju'
				where id_bc_in_hdr = $idhdrbc262
			END TRY
			BEGIN CATCH 
				SELECT 
				'error' AS status
				,ERROR_MESSAGE() AS description;
				
			END CATCH; 
			select '00' as status, 'DATA UPDATED' as description
			
			 ");
		 $rs = $qry->result_array();
         //// $r = $qry->result();		
		//// var_dump($r);die();
		 foreach ($rs as $row){
			 $responce = array("sts" => $row['status'], "desc" => $row['description'].' ');
		 }  
		 echo json_encode($responce);	
	}
	
	function cancel_car(){
		$id_bc_in_hdr = (isset($_POST['id_bc_in_hdr']) && !empty($_POST['id_bc_in_hdr']))?$_POST['id_bc_in_hdr']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		//$doc_in_from_id = (isset($_POST['doc_in_from_id']) && !empty($_POST['doc_in_from_id']))?$_POST['doc_in_from_id']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		
		
		$in_inv = $this->db->query("
			
			declare @id_data_in_hdr bigint
			
			--if (select count(*) from [tbl_data_in_hdr] where id_terkait=$id_bc_in_hdr and doc_in_from_id=5) > 0 
			--begin
			--	select '06' as status, ' DATA SUDAH DIPROSES.. CANCEL DAHULU, KALO DAH APPROVE KAGAK BISA' as description
			--	return
			--end
			
			set @id_data_in_hdr = (select id_data_in_hdr from [tbl_data_in_hdr] where id_terkait=$id_bc_in_hdr and doc_in_from_id=3) -- doc_in_from_id=3 bc262
			delete from [tbl_data_in_hdr_h_status] where id_data_in_hdr = @id_data_in_hdr
			delete from [tbl_data_in_dtl] where id_data_in_hdr = @id_data_in_hdr
			delete from [tbl_data_in_hdr] where id_data_in_hdr = @id_data_in_hdr
			
			if (select count(*) from tbl_bc_in_hdr where id_bc_in_hdr = $id_bc_in_hdr and typedok ='BC262' ) > 0 
			begin
				
				update  [dbo].[tbl_packinglist_bc262] set [id_bc_hdr] = NULL, status = 'NEW' where [id_bc_hdr] = $id_bc_in_hdr
				
				delete from [dbo].[tbl_bc_in_hdr_h_status] where id_bc_in_hdr = $id_bc_in_hdr
				delete from [dbo].tbl_bc_in_dtl where id_bc_in_hdr = $id_bc_in_hdr
				delete from [dbo].tbl_bc_in_dok where id_bc_in_hdr = $id_bc_in_hdr
				delete from [dbo].tbl_bc_in_con where id_bc_in_hdr = $id_bc_in_hdr
				delete from [dbo].tbl_bc_in_kms where id_bc_in_hdr = $id_bc_in_hdr
				delete from [dbo].tbl_bc_in_fas where id_bc_in_hdr = $id_bc_in_hdr
				delete from [dbo].tbl_bc_in_hdr where id_bc_in_hdr = $id_bc_in_hdr
				
			end
			select '00' as status, ' SUKSES CANCEL SILAHKAN BUKA MENU INVOiCE UNTUK PENGECEKAN'  as description
				
			");
		$rs = $in_inv->result_array();
        //$r = $in_inv->result();		
		//var_dump($r);die();
		foreach ($rs as $row){
			$responce = array("sts" => $row['status'], "desc" => $row['description']);
		}  
		echo json_encode($responce);
	
	
	
	}
	
	function proses_pemasukan(){
		$id_bc_in_hdr = (isset($_POST['id_bc_in_hdr']) && !empty($_POST['id_bc_in_hdr']))?$_POST['id_bc_in_hdr']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$bc262doc_in_no = (isset($_POST['bc262doc_in_no']) && !empty($_POST['bc262doc_in_no']))?$_POST['bc262doc_in_no']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$doc_in_date = (isset($_POST['doc_in_date']) && !empty($_POST['doc_in_date']))?$_POST['doc_in_date']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		
		$in_inv = $this->db->query("
			if (select COUNT(*) from dbo.tbl_data_in_hdr where id_terkait = $id_bc_in_hdr and doc_in_from_id=3) > 0
			begin
				select '01' as status, ' DATA ALREADY EXIST ' as description
				return
			end
			declare @idx_hdr bigint
			BEGIN TRY
				insert into dbo.tbl_data_in_hdr	(doc_in_no, doc_in_date, doc_in_from_id, id_terkait, status) values('$bc262doc_in_no','$doc_in_date',3, $id_bc_in_hdr, 'APPROVED')
				set @idx_hdr=@@IDENTITY
				insert into dbo.tbl_data_in_dtl (
					id_data_in_hdr
					,doc_in_from_id
					,part_name_customs
					,part_number
					,part_no
					,qty_in
					,unit_in
					,box
					,pack
					,id_part_number
					,harga
					,kdval
					,type_brg
					,part_name
				) select 
					@idx_hdr
				  ,3
				  ,[BrgUrai]
				  ,[BrgUrai]
				  ,[KdBrg]
				  ,[JmlSat]
				  ,[KdSat]
				  ,[KemasJm]
				  ,[KemasJm]
				  ,0
				  ,[DNilInv]
				  ,'USD'
				  ,9 --mutasi barang subkon
				  ,[BrgUrai]
				from dbo.tbl_bc_in_dtl where id_bc_in_hdr = $id_bc_in_hdr
				
				update dbo.tbl_bc_in_hdr set statusdok = 'APPROVED INVENTORY' where id_bc_in_hdr = $id_bc_in_hdr
				INSERT INTO tbl_bc_in_hdr_h_status (id_bc_in_hdr, statusdok, status_date) values ($id_bc_in_hdr, 'DATA PROCESSED INVENTORY', GETDATE())
				INSERT INTO tbl_data_in_hdr_h_status
                      (id_data_in_hdr, status, status_date)
				VALUES     (@idx_hdr, 'APPROVED', GETDATE())
			END TRY
			BEGIN CATCH 
				SELECT 
				'error' AS status
				,ERROR_MESSAGE() AS description;
				
			END CATCH; 
			select '00' as status, ' DATA PEMASUKAN PROCCESSED, LANGSUNG APPROVED...SILAKAH TUTUP TERUS BUKA LAGI, BIAR TERLIHAT UPDATENYA'  as description
				
		");
		$rs = $in_inv->result_array();
        //$r = $in_inv->result();		
		//var_dump($r);die();
		foreach ($rs as $row){
			$responce = array("sts" => $row['status'], "desc" => $row['description']);
		}  
		echo json_encode($responce);
	
	}
	
	function proses_pemasukan_update(){
		$id_bc_in_hdr = (isset($_POST['id_bc_in_hdr']) && !empty($_POST['id_bc_in_hdr']))?$_POST['id_bc_in_hdr']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$bc262doc_in_no = (isset($_POST['bc262doc_in_no']) && !empty($_POST['bc262doc_in_no']))?$_POST['bc262doc_in_no']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$doc_in_date = (isset($_POST['doc_in_date']) && !empty($_POST['doc_in_date']))?$_POST['doc_in_date']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		
		$in_inv = $this->db->query("
			BEGIN TRY
				update dbo.tbl_data_in_hdr set [doc_in_no] = '$bc262doc_in_no'
				,[doc_in_date] = '$doc_in_date'
				where [id_terkait] = $id_bc_in_hdr
			END TRY
			BEGIN CATCH 
				SELECT 
				'error' AS status
				,ERROR_MESSAGE() AS description;
				
			END CATCH; 
			select '00' as status, ' DATA PEMASUKAN PROCCESSED, LANGSUNG APPROVED...SILAKAH TUTUP TERUS BUKA LAGI, BIAR TERLIHAT UPDATENYA'  as description
				
		");
		$rs = $in_inv->result_array();
        //$r = $in_inv->result();		
		//var_dump($r);die();
		foreach ($rs as $row){
			$responce = array("sts" => $row['status'], "desc" => $row['description']);
		}  
		echo json_encode($responce);
	
	
	}
	
	function proses_del_packinglist_262(){
		$no_packinglist = (isset($_POST['no_packinglist']) && !empty($_POST['no_packinglist']))?$_POST['no_packinglist']:die('{"sts":"ERROR","desc":" Param ID Tidak Ditemukan"}');
		$tgl_packinglist = (isset($_POST['tgl_packinglist']) && !empty($_POST['tgl_packinglist']))?$_POST['tgl_packinglist']:die('{"sts":"ERROR","desc":" Param ID Tidak Ditemukan"}');
		$in_inv = $this->db->query("
			BEGIN TRY
				if (select count(*) from tbl_packinglist_bc262 where no_packinglist = '$no_packinglist' and tgl_packinglist = '$tgl_packinglist') =0 
				begin
					select '06' as status, ' DATA TIDAK DITEMUKAN'  as description
				end
				delete from tbl_packinglist_bc262 where no_packinglist = '$no_packinglist' and tgl_packinglist = '$tgl_packinglist'
			END TRY
			BEGIN CATCH 
				SELECT 
				'error' AS status
				,ERROR_MESSAGE() AS description;
				
			END CATCH; 
			select '00' as status, ' DATA DELETED'  as description
				
		");
		$rs = $in_inv->result_array();
        //$r = $in_inv->result();		
		//var_dump($r);die();
		foreach ($rs as $row){
			$responce = array("sts" => $row['status'], "desc" => $row['description']);
		}  
		echo json_encode($responce);
	
	
	}
	
	function autocompletenokontrak(){
		$kd = isset($_GET['name_startsWith'])?$_GET['name_startsWith']:'';
		$qry = $this->db->query("
			select
				[NOKONTRAK]
				,[TGLKONTRAK]
				,[kurs]
				,[kdval]
			from
				[dbo].[tblbc261_perhitungan_exim_hdr]
			where [NOKONTRAK] like '%$kd%'
				
		");
        $i=0;
        foreach($qry->result() as $r){
			$responce[$i]['NOKONTRAK']=$r->NOKONTRAK; 
			$responce[$i]['TGLKONTRAK']=$r->TGLKONTRAK; 
			$responce[$i]['kurs']=$r->kurs; 
			$i++;
		}
		echo json_encode($responce);
	}
	
	function autocompleteexbc261(){
		$kd = isset($_GET['name_startsWith'])?$_GET['name_startsWith']:'';
		$qry = $this->db->query("
			select
				[NODAFT]
				 ,convert(DATE, [TGDAFT], 126) as TGDAFT
				 ,[NOINV]
				 ,[TGINV]
			from
				[dbo].[tbl_bc_out_hdr]
			where typedok = 'BC261' and ([NODAFT] like '%$kd%' or [NOINV] like '%$kd%')
				
		");
        $i=0;
        foreach($qry->result() as $r){
			$responce[$i]['NODAFT']=$r->NODAFT; 
			$responce[$i]['TGDAFT']=$r->TGDAFT; 
			$responce[$i]['NOINV']=$r->NOINV; 
			$responce[$i]['TGINV']=$r->TGINV; 
			$i++;
		}
		echo json_encode($responce);
	}
	
	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 typedok='BC262' ";
		if (isset($_POST['tglawal']) and isset($_POST['tglakhir'])){
			$tglawal=$_POST['tglawal'];
			$tglakhir=$_POST['tglakhir'];
			$where .= " and BC23Tg between '$tglawal' and '$tglakhir' ";
		}			
		if ($search == 'true'){
			if (!empty($filters)){
				$array = json_decode($filters);
				foreach($array->rules as $arr=>$k){
					
					if ( $k->field == "nobl" ) {
						$searchString = $k->data;
						$where .= " and nobl like  '%".$searchString."%'"; 
					} else if ( $k->field == "tglbl" ) {
						$searchString = $k->data;
						$where .= " and tglbl =  '$searchString'"; 
					} else if ( $k->field == "CAR" ) {
						$searchString = $k->data;
						$where .= " and CAR like  '%".$searchString."%'"; 
					} else if ( $k->field == "KdKpbc" ) {
						$searchString = $k->data;
						$where .= " and KdKpbc like  '%".$searchString."%'"; 
					} else if ( $k->field == "PasokNama" ) {
						$searchString = $k->data;
						$where .= " and PasokNama like  '%".$searchString."%'"; 
					} else if ( $k->field == "BC23No" ) {
						$searchString = $k->data;
						$where .= " and BC23No like  '%".$searchString."%'"; 
					} else if ( $k->field == "BC23Tg" ) {
						$searchString = $k->data;
						$where .= " and BC23Tg like  '%".$searchString."%'"; 
					} else if ( $k->field == "JmBrg" ) {
						$searchString = $k->data;
						$where .= " and JmBrg = $searchString"; 
					} else if ( $k->field == "Status" ) {
						$searchString = $k->data;
						$where .= " and Status like  '%".$searchString."%'"; 
					}  else if ( $k->field == "statusdok" ) {
						$searchString = $k->data;
						$where .= " and statusdok like  '%".$searchString."%'"; 
					} 
				}
			}
		}
		
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_bc23->bc23hdrdataAll($limit,$start,$sidx,$sord, $where);
        $count = $this->mdl_bc23->bc23hdrdataCountAll($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_bc_in_hdr']=$r->id_bc_in_hdr; 
			$responce->rows[$i]['CAR']=$r->CAR; 
			$responce->rows[$i]['KdKpbc']=$r->KdKpbc; 
			$responce->rows[$i]['Tujuan']=$r->Tujuan; 
			$responce->rows[$i]['JnsBarang']=$r->JnsBarang; 
			$responce->rows[$i]['TujuanKirim']=$r->TujuanKirim; 
			$responce->rows[$i]['PasokNama']=$r->PasokNama; 
			$responce->rows[$i]['PasokAlmt']=$r->PasokAlmt; 
			$responce->rows[$i]['PasokNeg']=$r->PasokNeg; 
			$responce->rows[$i]['UsahaId']=$r->UsahaId; 
			$responce->rows[$i]['UsahaNpwp']=$r->UsahaNpwp; 
			$responce->rows[$i]['KdTPB']=$r->KdTPB; 
			$responce->rows[$i]['Registrasi']=$r->Registrasi; 
			$responce->rows[$i]['UsahaNama']=$r->UsahaNama; 
			$responce->rows[$i]['UsahaAlmt']=$r->UsahaAlmt; 
			$responce->rows[$i]['UsahaStatus']=$r->UsahaStatus; 
			$responce->rows[$i]['ApiKd']=$r->ApiKd; 
			$responce->rows[$i]['ApiNo']=$r->ApiNo; 
			$responce->rows[$i]['PpjkId']=$r->PpjkId; 
			$responce->rows[$i]['PpjkNpwp']=$r->PpjkNpwp; 
			$responce->rows[$i]['PpjkNama']=$r->PpjkNama; 
			$responce->rows[$i]['PpjkAlmt']=$r->PpjkAlmt; 
			$responce->rows[$i]['PpjkNo']=$r->PpjkNo; 
			$responce->rows[$i]['PpjkTg']=$r->PpjkTg; 
			$responce->rows[$i]['IndId']=$r->IndId; 
			$responce->rows[$i]['IndNpwp']=$r->IndNpwp; 
			$responce->rows[$i]['IndNama']=$r->IndNama; 
			$responce->rows[$i]['IndAlmt']=$r->IndAlmt; 
			$responce->rows[$i]['Moda']=$r->Moda; 
			$responce->rows[$i]['AngkutNama']=$r->AngkutNama; 
			$responce->rows[$i]['AngkutNo']=$r->AngkutNo; 
			$responce->rows[$i]['AngkutFl']=$r->AngkutFl; 
			$responce->rows[$i]['PelBkr']=$r->PelBkr; 
			$responce->rows[$i]['PelMuat']=$r->PelMuat; 
			$responce->rows[$i]['PelTransit']=$r->PelTransit; 
			$responce->rows[$i]['BC23No']=$r->BC23No; 
			$responce->rows[$i]['BC23Tg']=$r->BC23Tg; 
			$responce->rows[$i]['KdKpbcBongkar']=$r->KdKpbcBongkar; 
			$responce->rows[$i]['KdKpbcAwas']=$r->KdKpbcAwas; 
			$responce->rows[$i]['DokTupKd']=$r->DokTupKd; 
			$responce->rows[$i]['DokTupNo']=$r->DokTupNo; 
			$responce->rows[$i]['DokTupTg']=$r->DokTupTg; 
			$responce->rows[$i]['PosNo']=$r->PosNo; 
			$responce->rows[$i]['PosSub']=$r->PosSub; 
			$responce->rows[$i]['TmpTbn']=$r->TmpTbn; 
			$responce->rows[$i]['KdVal']=$r->KdVal; 
			$responce->rows[$i]['Ndpbm']=$r->Ndpbm; 
			$responce->rows[$i]['NilInv']=$r->NilInv; 
			$responce->rows[$i]['Freight']=$r->Freight; 
			$responce->rows[$i]['BTambahan']=$r->BTambahan; 
			$responce->rows[$i]['Diskon']=$r->Diskon; 
			$responce->rows[$i]['KdAss']=$r->KdAss; 
			$responce->rows[$i]['Asuransi']=$r->Asuransi; 
			$responce->rows[$i]['KdHrg']=$r->KdHrg; 
			$responce->rows[$i]['Fob']=$r->Fob; 
			$responce->rows[$i]['Cif']=$r->Cif; 
			$responce->rows[$i]['Bruto']=$r->Bruto; 
			$responce->rows[$i]['Netto']=$r->Netto; 
			$responce->rows[$i]['JmCont']=$r->JmCont; 
			$responce->rows[$i]['JmBrg']=$r->JmBrg; 
			$responce->rows[$i]['TglTtd']=$r->TglTtd; 
			$responce->rows[$i]['KotaTtd']=$r->KotaTtd; 
			$responce->rows[$i]['Namattd']=$r->Namattd; 
			$responce->rows[$i]['ContakPerson']=$r->ContakPerson; 
			$responce->rows[$i]['NoPhone']=$r->NoPhone; 
			$responce->rows[$i]['NoFax']=$r->NoFax; 
			$responce->rows[$i]['Email']=$r->Email; 
			$responce->rows[$i]['Status']=$status;//$r->Status; 
			$responce->rows[$i]['Snrf']=$r->Snrf; 
			$responce->rows[$i]['Lengkap']=$r->Lengkap; 
			$responce->rows[$i]['PosSubSub']=$r->PosSubSub; 
			$responce->rows[$i]['NoKontrak']=$r->NoKontrak; 
			$responce->rows[$i]['TgKontrak']=$r->TgKontrak; 
			$responce->rows[$i]['HargaRp']=$r->HargaRp; 
			$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]['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]['typedok']=$r->typedok; 
			$responce->rows[$i]['statusdok']=$r->statusdok; 
			$responce->rows[$i]['noinv']=$r->noinv; 
			$responce->rows[$i]['tglinv']=$r->tglinv;
			$responce->rows[$i]['inv_hdr_id']=$r->inv_hdr_id;
			$responce->rows[$i]['nobl']=$r->nobl;
			$responce->rows[$i]['tglbl']=$r->tglbl;
			$responce->rows[$i]['bc_in_from_in']=$r->bc_in_from_in;
			$responce->rows[$i]['tgaju']=$r->tgaju;
			
			$i++;
		}
		echo json_encode($responce);
		
	}
	function view_bc262_dtl(){
		$idhdrbc262 = (isset($_POST['idhdrbc262']) && !empty($_POST['idhdrbc262']))?$_POST['idhdrbc262']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$inv_hdr_idbc262 = (isset($_POST['inv_hdr_idbc262']) && !empty($_POST['inv_hdr_idbc262']))?$_POST['inv_hdr_idbc262']:'lihat doc';
		$noinv  = (isset($_POST['noinv']) && !empty($_POST['noinv']))?$_POST['noinv']:'lihat doc';
		$car= (isset($_POST['car']) && !empty($_POST['car']))?$_POST['car']:'-';
		$gabinv  = (isset($_POST['gabinv']) && !empty($_POST['gabinv']))?$_POST['gabinv']:'lihat doc';
		$tglgabinv  = (isset($_POST['tglgabinv']) && !empty($_POST['tglgabinv']))?$_POST['tglgabinv']:'lihat doc';
		$DokTupTg  = (isset($_POST['DokTupTg']) && !empty($_POST['DokTupTg']))?$_POST['DokTupTg']:'';
		$DokTupNo  = (isset($_POST['DokTupNo']) && !empty($_POST['DokTupNo']))?$_POST['DokTupNo']:'';
		$BC23No  = (isset($_POST['BC23No']) && !empty($_POST['BC23No']))?$_POST['BC23No']:'';
		$BC23Tg  = (isset($_POST['BC23Tg']) && !empty($_POST['BC23Tg']))?$_POST['BC23Tg']:'';
		$PasokNama  = (isset($_POST['PasokNama']) && !empty($_POST['PasokNama']))?$_POST['PasokNama']:'';
		$tgaju  = (isset($_POST['tgaju']) && !empty($_POST['tgaju']))?$_POST['tgaju']:'';
		
		
		$qry = $this->db->query("
			select
				[id_data_in_hdr]
				  ,[doc_in_no]
				  ,CONVERT(varchar, [doc_in_date], 101)  as doc_in_date
				  ,[status]
			from
				[dbo].[tbl_data_in_hdr]
			where doc_in_from_id = 3 and [id_terkait] = $idhdrbc262
				
		");
		$id_data_in_hdr = 0;
		$doc_in_no = '';
		$doc_in_date = '';
		$status = '';
        $i=0;
        foreach($qry->result() as $r){
			$id_data_in_hdr = $r->id_data_in_hdr;
			$doc_in_no =  $r->doc_in_no;
			$doc_in_date =  $r->doc_in_date;
			$status =  $r->status;
			$i++;
		}
		
		$data["idhdrbc262"] = $idhdrbc262; 
		$data["inv_hdr_idbc262"] = $inv_hdr_idbc262; 
		$data["noinv"] = $noinv;
		$data["car"] = $car;
		$data["gabinv"] = $gabinv;
		$data["tglgabinv"] = $tglgabinv;
		$data["DokTupTg"] = $DokTupTg;
		$data["DokTupNo"] = $DokTupNo;
		$data["BC23No"] = $BC23No;
		$data["BC23Tg"] = $BC23Tg;
		$data["PasokNama"] = $PasokNama;
		$data["tgaju"] = $tgaju;
		$data["id_data_in_hdr"] = $id_data_in_hdr;
		$data["doc_in_no"] = $doc_in_no;
		$data["doc_in_date"] = $doc_in_date;
		$data["status"] = $status;
		$this->load->view('exim/view_bc262_dtl', $data);
		
	
	}
	
	function loaddataheader(){
		$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	
		$idy = (isset($_POST['idhdrbc262']) or !empty($_POST['idhdrbc262']))?$_POST['idhdrbc262']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$where = " id_bc_in_hdr=$idy ";
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_bc23->bc23hdrdataAll($limit,$start,$sidx,$sord, $where);
        $count = $this->mdl_bc23->bc23hdrdataCountAll($where);
		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 = 'New';
				
			} else { $status=$r->Status;  }
			
			$responce->rows[$i]['id_bc_in_hdr']=$r->id_bc_in_hdr; 
			$responce->rows[$i]['CAR']=$r->CAR; 
			$responce->rows[$i]['KdKpbc']=$r->KdKpbc; 
			$responce->rows[$i]['Tujuan']=$r->Tujuan; 
			$responce->rows[$i]['JnsBarang']=$r->JnsBarang; 
			$responce->rows[$i]['TujuanKirim']=$r->TujuanKirim; 
			$responce->rows[$i]['PasokNama']=$r->PasokNama; 
			$responce->rows[$i]['PasokAlmt']=$r->PasokAlmt; 
			$responce->rows[$i]['PasokNeg']=$r->PasokNeg; 
			$responce->rows[$i]['UsahaId']=$r->UsahaId; 
			$responce->rows[$i]['UsahaNpwp']=$r->UsahaNpwp; 
			$responce->rows[$i]['KdTPB']=$r->KdTPB; 
			$responce->rows[$i]['Registrasi']=$r->Registrasi; 
			$responce->rows[$i]['UsahaNama']=$r->UsahaNama; 
			$responce->rows[$i]['UsahaAlmt']=$r->UsahaAlmt; 
			$responce->rows[$i]['UsahaStatus']=$r->UsahaStatus; 
			$responce->rows[$i]['ApiKd']=$r->ApiKd; 
			$responce->rows[$i]['ApiNo']=$r->ApiNo; 
			$responce->rows[$i]['PpjkId']=$r->PpjkId; 
			$responce->rows[$i]['PpjkNpwp']=$r->PpjkNpwp; 
			$responce->rows[$i]['PpjkNama']=$r->PpjkNama; 
			$responce->rows[$i]['PpjkAlmt']=$r->PpjkAlmt; 
			$responce->rows[$i]['PpjkNo']=$r->PpjkNo; 
			$responce->rows[$i]['PpjkTg']=$r->PpjkTg; 
			$responce->rows[$i]['IndId']=$r->IndId; 
			$responce->rows[$i]['IndNpwp']=$r->IndNpwp; 
			$responce->rows[$i]['IndNama']=$r->IndNama; 
			$responce->rows[$i]['IndAlmt']=$r->IndAlmt; 
			$responce->rows[$i]['Moda']=$r->Moda; 
			$responce->rows[$i]['AngkutNama']=$r->AngkutNama; 
			$responce->rows[$i]['AngkutNo']=$r->AngkutNo; 
			$responce->rows[$i]['AngkutFl']=$r->AngkutFl; 
			$responce->rows[$i]['PelBkr']=$r->PelBkr; 
			$responce->rows[$i]['PelMuat']=$r->PelMuat; 
			$responce->rows[$i]['PelTransit']=$r->PelTransit; 
			$responce->rows[$i]['BC23No']=$r->BC23No; 
			$responce->rows[$i]['BC23Tg']=$r->BC23Tg; 
			$responce->rows[$i]['KdKpbcBongkar']=$r->KdKpbcBongkar; 
			$responce->rows[$i]['KdKpbcAwas']=$r->KdKpbcAwas; 
			$responce->rows[$i]['DokTupKd']=$r->DokTupKd; 
			$responce->rows[$i]['DokTupNo']=$r->DokTupNo; 
			$responce->rows[$i]['DokTupTg']=$r->DokTupTg; 
			$responce->rows[$i]['PosNo']=$r->PosNo; 
			$responce->rows[$i]['PosSub']=$r->PosSub; 
			$responce->rows[$i]['TmpTbn']=$r->TmpTbn; 
			$responce->rows[$i]['KdVal']=$r->KdVal; 
			$responce->rows[$i]['Ndpbm']=$r->Ndpbm; 
			$responce->rows[$i]['NilInv']=$r->NilInv; 
			$responce->rows[$i]['Freight']=$r->Freight; 
			$responce->rows[$i]['BTambahan']=$r->BTambahan; 
			$responce->rows[$i]['Diskon']=$r->Diskon; 
			$responce->rows[$i]['KdAss']=$r->KdAss; 
			$responce->rows[$i]['Asuransi']=$r->Asuransi; 
			$responce->rows[$i]['KdHrg']=$r->KdHrg; 
			$responce->rows[$i]['Fob']=$r->Fob; 
			$responce->rows[$i]['Cif']=$r->Cif; 
			$responce->rows[$i]['Bruto']=$r->Bruto; 
			$responce->rows[$i]['Netto']=$r->Netto; 
			$responce->rows[$i]['JmCont']=$r->JmCont; 
			$responce->rows[$i]['JmBrg']=$r->JmBrg; 
			$responce->rows[$i]['TglTtd']=$r->TglTtd; 
			$responce->rows[$i]['KotaTtd']=$r->KotaTtd; 
			$responce->rows[$i]['Namattd']=$r->Namattd; 
			$responce->rows[$i]['ContakPerson']=$r->ContakPerson; 
			$responce->rows[$i]['NoPhone']=$r->NoPhone; 
			$responce->rows[$i]['NoFax']=$r->NoFax; 
			$responce->rows[$i]['Email']=$r->Email; 
			$responce->rows[$i]['Status']=$status;//$r->Status; 
			$responce->rows[$i]['Snrf']=$r->Snrf; 
			$responce->rows[$i]['Lengkap']=$r->Lengkap; 
			$responce->rows[$i]['PosSubSub']=$r->PosSubSub; 
			$responce->rows[$i]['NoKontrak']=$r->NoKontrak; 
			$responce->rows[$i]['TgKontrak']=$r->TgKontrak; 
			$responce->rows[$i]['HargaRp']=$r->HargaRp; 
			$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]['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]['typedok']=$r->typedok; 
			$responce->rows[$i]['statusdok']=$r->statusdok; 
			$responce->rows[$i]['noinv']=$r->noinv; 
			$responce->rows[$i]['tglinv']=$r->tglinv;
			$responce->rows[$i]['inv_hdr_id']=$r->inv_hdr_id;
			$responce->rows[$i]['bc_in_from_in']=$r->bc_in_from_in;
			
			$i++;
		}
		echo json_encode($responce);
		
	}
	
	function loaddata_bc262_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'; // get index row - i.e. user click to sort
        $sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		
		$idhdr = (isset($_POST['idhdrbc262']) && !empty($_POST['idhdrbc262']))?$_POST['idhdrbc262']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		
		$where = " id_bc_in_hdr=$idhdr ";
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_bc23->bc23detildataAll($limit,$start,$sidx,$sord, $where);
        $count = $this->mdl_bc23->bc23detildataCountAll($where);
		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_bc_in_dtl']=$r->id_bc_in_dtl; 
			$responce->rows[$i]['id_bc_in_hdr']=$r->id_bc_in_hdr; 
			$responce->rows[$i]['CAR']=$r->CAR; 
			$responce->rows[$i]['Serial']=$r->Serial; 
			$responce->rows[$i]['JnsBarangDtl']=$r->JnsBarangDtl; 
			$responce->rows[$i]['TujuanKirimDtl']=$r->TujuanKirimDtl; 
			$responce->rows[$i]['NoHs']=$r->NoHs; 
			$responce->rows[$i]['SeriTrp']=$r->SeriTrp; 
			$responce->rows[$i]['BrgUrai']=$r->BrgUrai; 
			$responce->rows[$i]['Merk']=$r->Merk; 
			$responce->rows[$i]['Tipe']=$r->Tipe; 
			$responce->rows[$i]['SpfLain']=$r->SpfLain; 
			$responce->rows[$i]['KdBrg']=$r->KdBrg; 
			$responce->rows[$i]['Penggunaan']=$r->Penggunaan; 
			$responce->rows[$i]['KemasJn']=$r->KemasJn;
			$responce->rows[$i]['KemasJm']=$r->KemasJm;
			$responce->rows[$i]['KdFasDtl']=$r->KdFasDtl;
			$responce->rows[$i]['BrgAsal']=$r->BrgAsal;
			$responce->rows[$i]['DNilInv']=$r->DNilInv;
			$responce->rows[$i]['KdSat']=$r->KdSat;
			$responce->rows[$i]['JmlSat']=$r->JmlSat;
			$responce->rows[$i]['NettoDtl']=$r->NettoDtl;
			$responce->rows[$i]['SatBmJm']=$r->SatBmJm;
			$responce->rows[$i]['SatCukJm']=$r->SatCukJm;
			$responce->rows[$i]['DtlOk']=$r->DtlOk;
			$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 loaddatadok(){
		$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	
		
		$idhdr = (isset($_POST['idhdrbc262']) && !empty($_POST['idhdrbc262']))?$_POST['idhdrbc262']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		
		$where = " id_bc_in_hdr=$idhdr ";
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_bc23->bc23dokdataAll($limit,$start,$sidx,$sord, $where);
        $count = $this->mdl_bc23->bc23dokdataCountAll($where);
		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_bc_in_dok']=$r->id_bc_in_dok; 
			$responce->rows[$i]['id_bc_in_hdr']=$r->id_bc_in_hdr; 
			$responce->rows[$i]['CAR']=$r->CAR; 
			$responce->rows[$i]['DokKd']=$r->DokKd; 
			$responce->rows[$i]['uraian']=$r->uraian; 
			$responce->rows[$i]['DokNo']=$r->DokNo; 
			$responce->rows[$i]['DokTg']=$r->DokTg; 
			
			$i++;
		}
		echo json_encode($responce);
		
	}
	
 }