<?php
class CT_bc30 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_bc30", 'mdl_bc30');
  }
  
  function index(){
 	
	$this->load->view('exim/view_bc30');
  }
  
  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_bc_out_hdr'; // get index row - i.e. user click to sort
	$sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
	
	$q =(isset($_POST['q']) && !empty($_POST['q']))?$_POST['q']:'0';
	$idhdrbc30 =(isset($_POST['idhdrbc30']) && !empty($_POST['idhdrbc30']))?$_POST['idhdrbc30']:'';
		
	$where = " 1=1 and typedok='BC30' ";
	
	if ($q=='66'){
		$where =" id_bc_out_hdr=$idhdrbc30 ";
	}
	
	if (isset($_POST['tglawal']) and isset($_POST['tglakhir'])){
			$tglawal=$_POST['tglawal'];
			$tglakhir=$_POST['tglakhir'];
			$where .= " and TGDAFT between '$tglawal' and '$tglakhir' ";
		}	

	if ($search == 'true'){
		if (!empty($filters)){
			$array = json_decode($filters);
			foreach($array->rules as $arr=>$k){
				
				if ( $k->field == "CAR" ) {
					$searchString = $k->data;
					$where .= " and CAR like  '%".$searchString."%'"; 
				} else if ( $k->field == "NAMABELI" ) {
					$searchString = $k->data;
					$where .= " and NAMABELI like  '%".$searchString."%'"; 
				} else if ( $k->field == "NEGBELI" ) {
					$searchString = $k->data;
					$where .= " and NEGBELI like  '%".$searchString."%'"; 
				} else if ( $k->field == "NOINV" ) {
					$searchString = $k->data;
					$where .= " and NOINV like  '%".$searchString."%'"; 
				} else if ( $k->field == "NilInv" ) {
					$searchString = $k->data;
					$where .= " and NilInv =  $searchString"; 
				} else if ( $k->field == "TGINV" ) {
					$searchString = $k->data;
					$where .= " and TGINV =  '$searchString'"; 
				} else if ( $k->field == "KDVAL" ) {
					$searchString = $k->data;
					$where .= " and KDVAL 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_bc30->bc30hdr_All($limit,$start,$sidx,$sord, $where);
	$count = $this->mdl_bc30->bc30hdr_CountAll($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_out_hdr']=$r->id_bc_out_hdr; 
		$responce->rows[$i]['CAR']=$r->CAR; 
		$responce->rows[$i]['JnEks']=$r->JnEks; 
		$responce->rows[$i]['KatEks']=$r->KatEks; 
		$responce->rows[$i]['JNPEB']=$r->JNPEB; 
		$responce->rows[$i]['JNEKSOld']=$r->JNEKSOld; 
		$responce->rows[$i]['JNLAIN']=$r->JNLAIN; 
		$responce->rows[$i]['JNDAG']=$r->JNDAG; 
		$responce->rows[$i]['JNBYR']=$r->JNBYR; 
		$responce->rows[$i]['IDEKS']=$r->IDEKS; 
		$responce->rows[$i]['NPWPEKS']=$r->NPWPEKS; 
		$responce->rows[$i]['NamaEks']=$r->NamaEks; 
		$responce->rows[$i]['AlmtEks']=$r->AlmtEks; 
		$responce->rows[$i]['NoSiup']=$r->NoSiup; 
		$responce->rows[$i]['TgSiup']=$r->TgSiup; 
		$responce->rows[$i]['JnPerus']=$r->JnPerus; 
		$responce->rows[$i]['NoTdp']=$r->NoTdp; 
		$responce->rows[$i]['TgTdp']=$r->TgTdp; 
		$responce->rows[$i]['Niper']=$r->Niper; 
		$responce->rows[$i]['StatusH']=$r->StatusH; 
		$responce->rows[$i]['IdQq']=$r->IdQq; 
		$responce->rows[$i]['NpwpQq']=$r->NpwpQq; 
		$responce->rows[$i]['NamaQq']=$r->NamaQq; 
		$responce->rows[$i]['AlmtQq']=$r->AlmtQq; 
		$responce->rows[$i]['NiperQq']=$r->NiperQq; 
		$responce->rows[$i]['NAMABELI']=$r->NAMABELI; 
		$responce->rows[$i]['ALMTBELI']=$r->ALMTBELI; 
		$responce->rows[$i]['NEGBELI']=$r->NEGBELI; 
		$responce->rows[$i]['IDPPJK']=$r->IDPPJK; 
		$responce->rows[$i]['NPWPPPJK']=$r->NPWPPPJK; 
		$responce->rows[$i]['NamaPpjk']=$r->NamaPpjk; 
		$responce->rows[$i]['AlmtPpjk']=$r->AlmtPpjk; 
		$responce->rows[$i]['NoPpjk']=$r->NoPpjk; 
		$responce->rows[$i]['TgPpjk']=$r->TgPpjk; 
		$responce->rows[$i]['MODA']=$r->MODA; 
		$responce->rows[$i]['TGEKS']=$r->TGEKS; 
		$responce->rows[$i]['CARRIER']=$r->CARRIER; 
		$responce->rows[$i]['VOY']=$r->VOY; 
		$responce->rows[$i]['NOSHIPORD']=$r->NOSHIPORD; 
		$responce->rows[$i]['TGSHIPORD']=$r->TGSHIPORD; 
		$responce->rows[$i]['PELMUAT']=$r->PELMUAT; 
		$responce->rows[$i]['PELBONGKAR']=$r->PELBONGKAR; 
		$responce->rows[$i]['PELTRANSIT']=$r->PELTRANSIT; 
		$responce->rows[$i]['NOINV']=$r->NOINV; 
		$responce->rows[$i]['TGINV']=$r->TGINV; 
		$responce->rows[$i]['NOLPSE']=$r->NOLPSE; 
		$responce->rows[$i]['TGLPSE']=$r->TGLPSE; 
		$responce->rows[$i]['PROPBRG']=$r->PROPBRG; 
		$responce->rows[$i]['NEGTUJU']=$r->NEGTUJU; 
		$responce->rows[$i]['FLSIE']=$r->FLSIE; 
		$responce->rows[$i]['FLKARANT']=$r->FLKARANT; 
		$responce->rows[$i]['FLSM']=$r->FLSM; 
		$responce->rows[$i]['FLIZLAIN']=$r->FLIZLAIN; 
		$responce->rows[$i]['DELIVERY']=$r->DELIVERY; 
		$responce->rows[$i]['KDVAL']=$r->KDVAL; 
		$responce->rows[$i]['KdHrg']=$r->KdHrg; 
		$responce->rows[$i]['NilInv']=$r->NilInv; 
		$responce->rows[$i]['FREIGHT']=$r->FREIGHT; 
		$responce->rows[$i]['ASURANSI']=$r->ASURANSI; 
		$responce->rows[$i]['KdAss']=$r->KdAss; 
		$responce->rows[$i]['FOB']=$r->FOB; 
		$responce->rows[$i]['MERK']=$r->MERK; 
		$responce->rows[$i]['JMCONT']=$r->JMCONT; 
		$responce->rows[$i]['BRUTO']=$r->BRUTO; 
		$responce->rows[$i]['NETTO']=$r->NETTO; 
		$responce->rows[$i]['JMBRG']=$r->JMBRG; 
		$responce->rows[$i]['JMBRGCTH']=$r->JMBRGCTH; 
		$responce->rows[$i]['JMBRGGAB']=$r->JMBRGGAB; 
		$responce->rows[$i]['JMBRGPJT']=$r->JMBRGPJT; 
		$responce->rows[$i]['NODAFT']=$r->NODAFT; 
		$responce->rows[$i]['TGDAFT']=$r->TGDAFT; 
		$responce->rows[$i]['KDKTR']=$r->KDKTR; 
		$responce->rows[$i]['KTRGATE']=$r->KTRGATE; 
		$responce->rows[$i]['NILPE']=$r->NILPE; 
		$responce->rows[$i]['KDBANK']=$r->KDBANK; 
		$responce->rows[$i]['LOKBANK']=$r->LOKBANK; 
		$responce->rows[$i]['NOSSP']=$r->NOSSP; 
		$responce->rows[$i]['TGSSP']=$r->TGSSP; 
		$responce->rows[$i]['JNTPB']=$r->JNTPB; 
		$responce->rows[$i]['LOKTPB']=$r->LOKTPB; 
		$responce->rows[$i]['KDLOKBRG']=$r->KDLOKBRG; 
		$responce->rows[$i]['KDKTRMUAT']=$r->KDKTRMUAT; 
		$responce->rows[$i]['KDKTRPRIKS']=$r->KDKTRPRIKS; 
		$responce->rows[$i]['TGSIAP']=$r->TGSIAP; 
		$responce->rows[$i]['WKSIAP']=$r->WKSIAP; 
		$responce->rows[$i]['ASALDATA']=$r->ASALDATA; 
		$responce->rows[$i]['TPNAME']=$r->TPNAME; 
		$responce->rows[$i]['PEBOK']=$r->PEBOK; 
		$responce->rows[$i]['LENGKAP']=$r->LENGKAP; 
		$responce->rows[$i]['JNPARTOF']=$r->JNPARTOF; 
		$responce->rows[$i]['BENDERA']=$r->BENDERA;
		$responce->rows[$i]['TTDPEB']=$r->TTDPEB;
		$responce->rows[$i]['STATUS']=$r->STATUS;
		$responce->rows[$i]['SNRF']=$r->SNRF;
		$responce->rows[$i] ['NOBCF'] =$r->NOBCF; 
		$responce->rows[$i] ['TGBCF'] =$r->TGBCF; 
		$responce->rows[$i] ['SERIBCF'] =$r->SERIBCF; 
		$responce->rows[$i] ['NOMOREDI'] =$r->NOMOREDI; 
		$responce->rows[$i] ['KMDT'] =$r->KMDT; 
		$responce->rows[$i] ['VOLUME'] =$r->VOLUME; 
		$responce->rows[$i] ['KDHANGGAR'] =$r->KDHANGGAR; 
		$responce->rows[$i] ['KONS']=$r->KONS; 
		$responce->rows[$i] ['JMLKMSPEB']=$r->JMLKMSPEB; 
		$responce->rows[$i] ['PELMUATEKS'] =$r->PELMUATEKS; 
		$responce->rows[$i] ['NILKURS']=$r->NILKURS; 
		$responce->rows[$i] ['PNBP'] =$r->PNBP; 
		$responce->rows[$i] ['NOBC11'] =$r->NOBC11; 
		$responce->rows[$i] ['TGBC11'] =$r->TGBC11; 
		$responce->rows[$i] ['POSBC11'] =$r->POSBC11; 
		$responce->rows[$i] ['SUBPOSBC11'] =$r->SUBPOSBC11; 
		$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] ['noinv1'] =$r->noinv1; 
		$responce->rows[$i] ['tginv1'] =$r->tginv1; 
		$responce->rows[$i] ['inv_hdr_id'] =$r->inv_hdr_id; 
		$responce->rows[$i] ['bc_in_from_in'] =$r->bc_in_from_in; 
		$responce->rows[$i] ['nopol'] =$r->nopol; 
		$responce->rows[$i] ['doc_out_date'] =$r->doc_out_date; 
	
		$i++;
	}
	echo json_encode($responce);
	    
        
		
		
  }
  function view_bc30_getdata(){
  
	$this->load->view('exim/view_bc30_getdata');
	
  }
  
  function load_bc30_getadata(){
	$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']:'idpebhdr'; // get index row - i.e. user click to sort
	$sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
	
	$status =(isset($_POST['statusbc30loaddatahdr']) && !empty($_POST['statusbc30loaddatahdr']))?$_POST['statusbc30loaddatahdr']:'XX';
		
	$where = " 1=1 ";
	
	if($status == '00'){
		
		$where = " 1=1 and status='00' ";
	};
	

	if ($search == 'true'){
		if (!empty($filters)){
			$array = json_decode($filters);
			foreach($array->rules as $arr=>$k){
				
				if ( $k->field == "gabinvoice" ) {
					$searchString = $k->data;
					$where .= " and gabinvoice like  '%".$searchString."%'"; 
				} else if ( $k->field == "laodeddate" ) {
					$searchString = $k->data;
					$where .= " and laodeddate =  '$searchString'"; 
				} else if ( $k->field == "NAMABELI" ) {
					$searchString = $k->data;
					$where .= " and NAMABELI like  '%".$searchString."%'"; 
				} else if ( $k->field == "NEGBELI" ) {
					$searchString = $k->data;
					$where .= " and NEGBELI like  '%".$searchString."%'"; 
				} else if ( $k->field == "NOINV" ) {
					$searchString = $k->data;
					$where .= " and NOINV like  '%".$searchString."%'"; 
				} else if ( $k->field == "NilInv" ) {
					$searchString = $k->data;
					$where .= " and NilInv =  $searchString"; 
				} else if ( $k->field == "TGINV" ) {
					$searchString = $k->data;
					$where .= " and TGINV =  '$searchString'"; 
				} else if ( $k->field == "KDVAL" ) {
					$searchString = $k->data;
					$where .= " and KDVAL like  '%".$searchString."%'"; 
				} 
		
			}
		}
	}	
		
	$start = $limit*$page - $limit;
	if ($start<0) $start=0;
	
	$records = $this->mdl_bc30->bc30getdatahdr_All($limit,$start,$sidx,$sord, $where);
	$count = $this->mdl_bc30->bc30getdatahdr_CountAll($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]['idpebhdr']=$r->idpebhdr; 
      $responce->rows[$i]['CAR']=$r->CAR; 
      $responce->rows[$i]['JnEks']=$r->JnEks; 
      $responce->rows[$i]['KatEks']=$r->KatEks; 
      $responce->rows[$i]['JNPEB']=$r->JNPEB; 
      $responce->rows[$i]['JNEKSOld']=$r->JNEKSOld; 
      $responce->rows[$i]['JNLAIN']=$r->JNLAIN; 
      $responce->rows[$i]['JNDAG']=$r->JNDAG; 
      $responce->rows[$i]['JNBYR']=$r->JNBYR; 
      $responce->rows[$i]['IDEKS']=$r->IDEKS; 
      $responce->rows[$i]['NPWPEKS']=$r->NPWPEKS; 
      $responce->rows[$i]['NamaEks']=$r->NamaEks; 
      $responce->rows[$i]['AlmtEks']=$r->AlmtEks; 
      $responce->rows[$i]['NoSiup']=$r->NoSiup; 
      $responce->rows[$i]['TgSiup']=$r->TgSiup; 
      $responce->rows[$i]['JnPerus']=$r->JnPerus; 
      $responce->rows[$i]['NoTdp']=$r->NoTdp; 
      $responce->rows[$i]['TgTdp']=$r->TgTdp; 
      $responce->rows[$i]['Niper']=$r->Niper; 
      $responce->rows[$i]['StatusH']=$r->StatusH; 
      $responce->rows[$i]['IdQq']=$r->IdQq; 
      $responce->rows[$i]['NpwpQq']=$r->NpwpQq; 
      $responce->rows[$i]['NamaQq']=$r->NamaQq; 
      $responce->rows[$i]['AlmtQq']=$r->AlmtQq; 
      $responce->rows[$i]['NiperQq']=$r->NiperQq; 
      $responce->rows[$i]['NAMABELI']=$r->NAMABELI; 
      $responce->rows[$i]['ALMTBELI']=$r->ALMTBELI; 
      $responce->rows[$i]['NEGBELI']=$r->NEGBELI; 
      $responce->rows[$i]['IDPPJK']=$r->IDPPJK; 
      $responce->rows[$i]['NPWPPPJK']=$r->NPWPPPJK; 
      $responce->rows[$i]['NamaPpjk']=$r->NamaPpjk; 
      $responce->rows[$i]['AlmtPpjk']=$r->AlmtPpjk; 
      $responce->rows[$i]['NoPpjk']=$r->NoPpjk; 
      $responce->rows[$i]['TgPpjk']=$r->TgPpjk; 
      $responce->rows[$i]['MODA']=$r->MODA; 
      $responce->rows[$i]['TGEKS']=$r->TGEKS; 
      $responce->rows[$i]['CARRIER']=$r->CARRIER; 
      $responce->rows[$i]['VOY']=$r->VOY; 
      $responce->rows[$i]['NOSHIPORD']=$r->NOSHIPORD; 
      $responce->rows[$i]['TGSHIPORD']=$r->TGSHIPORD; 
      $responce->rows[$i]['PELMUAT']=$r->PELMUAT; 
      $responce->rows[$i]['PELBONGKAR']=$r->PELBONGKAR; 
      $responce->rows[$i]['PELTRANSIT']=$r->PELTRANSIT; 
      $responce->rows[$i]['NOINV']=$r->NOINV; 
      $responce->rows[$i]['TGINV']=$r->TGINV; 
      $responce->rows[$i]['NOLPSE']=$r->NOLPSE; 
      $responce->rows[$i]['TGLPSE']=$r->TGLPSE; 
      $responce->rows[$i]['PROPBRG']=$r->PROPBRG; 
      $responce->rows[$i]['NEGTUJU']=$r->NEGTUJU; 
      $responce->rows[$i]['FLSIE']=$r->FLSIE; 
      $responce->rows[$i]['FLKARANT']=$r->FLKARANT; 
      $responce->rows[$i]['FLSM']=$r->FLSM; 
      $responce->rows[$i]['FLIZLAIN']=$r->FLIZLAIN; 
      $responce->rows[$i]['DELIVERY']=$r->DELIVERY; 
      $responce->rows[$i]['KDVAL']=$r->KDVAL; 
      $responce->rows[$i]['KdHrg']=$r->KdHrg; 
      $responce->rows[$i]['NilInv']=$r->NilInv; 
      $responce->rows[$i]['FREIGHT']=$r->FREIGHT; 
      $responce->rows[$i]['ASURANSI']=$r->ASURANSI; 
      $responce->rows[$i]['KdAss']=$r->KdAss; 
      $responce->rows[$i]['FOB']=$r->FOB; 
      $responce->rows[$i]['MERK']=$r->MERK; 
      $responce->rows[$i]['JMCONT']=$r->JMCONT; 
      $responce->rows[$i]['BRUTO']=$r->BRUTO; 
      $responce->rows[$i]['NETTO']=$r->NETTO; 
      $responce->rows[$i]['JMBRG']=$r->JMBRG; 
      $responce->rows[$i]['JMBRGCTH']=$r->JMBRGCTH; 
      $responce->rows[$i]['JMBRGGAB']=$r->JMBRGGAB; 
      $responce->rows[$i]['JMBRGPJT']=$r->JMBRGPJT; 
      $responce->rows[$i]['NODAFT']=$r->NODAFT; 
      $responce->rows[$i]['TGDAFT']=$r->TGDAFT; 
      $responce->rows[$i]['KDKTR']=$r->KDKTR; 
      $responce->rows[$i]['KTRGATE']=$r->KTRGATE; 
      $responce->rows[$i]['NILPE']=$r->NILPE; 
      $responce->rows[$i]['KDBANK']=$r->KDBANK; 
      $responce->rows[$i]['LOKBANK']=$r->LOKBANK; 
      $responce->rows[$i]['NOSSP']=$r->NOSSP; 
      $responce->rows[$i]['TGSSP']=$r->TGSSP; 
      $responce->rows[$i]['JNTPB']=$r->JNTPB; 
      $responce->rows[$i]['LOKTPB']=$r->LOKTPB; 
      $responce->rows[$i]['KDLOKBRG']=$r->KDLOKBRG; 
      $responce->rows[$i]['KDKTRMUAT']=$r->KDKTRMUAT; 
      $responce->rows[$i]['KDKTRPRIKS']=$r->KDKTRPRIKS; 
      $responce->rows[$i]['TGSIAP']=$r->TGSIAP; 
      $responce->rows[$i]['WKSIAP']=$r->WKSIAP; 
      $responce->rows[$i]['ASALDATA']=$r->ASALDATA; 
      $responce->rows[$i]['TPNAME']=$r->TPNAME; 
      $responce->rows[$i]['PEBOK']=$r->PEBOK; 
      $responce->rows[$i]['LENGKAP']=$r->LENGKAP; 
      $responce->rows[$i]['JNPARTOF']=$r->JNPARTOF; 
      $responce->rows[$i]['BENDERA']=$r->BENDERA;
      $responce->rows[$i]['TTDPEB']=$r->TTDPEB;
      $responce->rows[$i]['STATUS']=$r->STATUS;
      $responce->rows[$i]['SNRF']=$r->SNRF;
      $responce->rows[$i] ['NOBCF'] =$r->NOBCF; 
      $responce->rows[$i] ['TGBCF'] =$r->TGBCF; 
      $responce->rows[$i] ['SERIBCF'] =$r->SERIBCF; 
      $responce->rows[$i] ['NOMOREDI'] =$r->NOMOREDI; 
      $responce->rows[$i] ['KMDT'] =$r->KMDT; 
      $responce->rows[$i] ['VOLUME'] =$r->VOLUME; 
      $responce->rows[$i] ['KDHANGGAR'] =$r->KDHANGGAR; 
      $responce->rows[$i] ['KONS']=$r->KONS; 
      $responce->rows[$i] ['JMLKMSPEB']=$r->JMLKMSPEB; 
      $responce->rows[$i] ['PELMUATEKS'] =$r->PELMUATEKS; 
      $responce->rows[$i] ['NILKURS']=$r->NILKURS; 
      $responce->rows[$i] ['PNBP'] =$r->PNBP; 
      $responce->rows[$i] ['NOBC11'] =$r->NOBC11; 
      $responce->rows[$i] ['TGBC11'] =$r->TGBC11; 
      $responce->rows[$i] ['POSBC11'] =$r->POSBC11; 
      $responce->rows[$i] ['SUBPOSBC11'] =$r->SUBPOSBC11; 
      $responce->rows[$i] ['jnkemas']=$r->jnkemas; 
      $responce->rows[$i] ['gabinvoice'] =$r->gabinvoice; 
      $responce->rows[$i] ['filename'] =$r->filename; 
      $responce->rows[$i] ['laodeddate'] =$r->laodeddate; 

		
		$i++;
	}
	echo json_encode($responce);
	
  }
  
  function getdatabc30(){
  
	$qry = $this->db->query("
		BEGIN TRY
		 EXEC ('spi_getdata_bc30') 
		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'].' FilesLoaded');
	}  
	echo json_encode($responce);
  
  
  }
  
  function view_bc30_getdatadetil(){
  
	$gabinvoice = (isset($_POST['gabinvoice']) && !empty($_POST['gabinvoice']))?$_POST['gabinvoice']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
	$NOINV = (isset($_POST['NOINV']) && !empty($_POST['NOINV']))?$_POST['NOINV']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
	$data['NOINV']=$NOINV;
	$data['gabinvoice']=$gabinvoice;
	$this->load->view('exim/view_bc30_getdatadetil', $data);
  
  
  }
  
  function data_bc30_getdatadetil(){
	
	$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']:'[idpebdtl]'; // get index row - i.e. user click to sort
	$sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
	
	$gabinvoice = (isset($_POST['gabinvoice']) && !empty($_POST['gabinvoice']))?$_POST['gabinvoice']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
	$NOINV = (isset($_POST['NOINV']) && !empty($_POST['NOINV']))?$_POST['NOINV']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
	
	$where = " gabinvoice='$gabinvoice' and NOINV='$NOINV' ";
	

	if ($search == 'true'){
		if (!empty($filters)){
			$array = json_decode($filters);
			foreach($array->rules as $arr=>$k){
				
				if ( $k->field == "[idpebdtl]" ) {
					$searchString = $k->data;
					$where .= " and [idpebdtl] like  '%".$searchString."%'"; 
				}
		
			}
		}
	}	
		
	$start = $limit*$page - $limit;
	if ($start<0) $start=0;
	
	$records = $this->mdl_bc30->bc30getdatadtl_All($limit,$start,$sidx,$sord, $where);
	$count = $this->mdl_bc30->bc30getdatadtl_CountAll($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]['idpebdtl']=$r->idpebdtl;
		  $responce->rows[$i]['idpebhdr']=$r->idpebhdr;
		  $responce->rows[$i]['CAR']=$r->CAR;
		  $responce->rows[$i]['SERIBRG']=$r->SERIBRG;
		  $responce->rows[$i]['HS']=$r->HS;
		  $responce->rows[$i]['URBRG1']=$r->URBRG1;
		  $responce->rows[$i]['URBRG2']=$r->URBRG2;
		  $responce->rows[$i]['URBRG3']=$r->URBRG3;
		  $responce->rows[$i]['URBRG4']=$r->URBRG4;
		  $responce->rows[$i]['DMERK']=$r->DMERK;
		  $responce->rows[$i]['SIZE']=$r->SIZE;
		  $responce->rows[$i]['TYPE']=$r->TYPE;
		  $responce->rows[$i]['KDBRG']=$r->KDBRG;
		  $responce->rows[$i]['KDIZIN']=$r->KDIZIN;
		  $responce->rows[$i]['NOIZIN']=$r->NOIZIN;
		  $responce->rows[$i]['TGIZIN']=$r->TGIZIN;
		  $responce->rows[$i]['JMKOLI']=$r->JMKOLI;
		  $responce->rows[$i]['JNKOLI']=$r->JNKOLI;
		  $responce->rows[$i]['DNilInv']=$r->DNilInv;
		  $responce->rows[$i]['FOBPERBRG']=$r->FOBPERBRG;
		  $responce->rows[$i]['FOBPERSAT']=$r->FOBPERSAT;
		  $responce->rows[$i]['KDPE']=$r->KDPE;
		  $responce->rows[$i]['TARIPPE']=$r->TARIPPE;
		  $responce->rows[$i]['HPATOK']=$r->HPATOK;
		  $responce->rows[$i]['JMSATUAN']=$r->JMSATUAN;
		  $responce->rows[$i]['JNSATUAN']=$r->JNSATUAN;
		  $responce->rows[$i]['NETDET']=$r->NETDET;
		  $responce->rows[$i]['JMSATPE']=$r->JMSATPE;
		  $responce->rows[$i]['JNSATPE']=$r->JNSATPE;
		  $responce->rows[$i]['KDVALPE']=$r->KDVALPE;
		  $responce->rows[$i]['NILVALPE']=$r->NILVALPE;
		  $responce->rows[$i]['PEPERBRG']=$r->PEPERBRG;
		  $responce->rows[$i]['DTLOK']=$r->DTLOK;
		  $responce->rows[$i]['NCV']=$r->NCV;
		  $responce->rows[$i]['DVolume']=$r->DVolume;
		  $responce->rows[$i]['NegAsal']=$r->NegAsal;
		  $responce->rows[$i]['NOINV']=$r->NOINV;
		  $responce->rows[$i]['gabinvoice']=$r->gabinvoice;
	
		$i++;
	}
	echo json_encode($responce);
	
  
  }
  
  function view_bc30_prosesdata(){
	
	$datadiff = (isset($_POST['datadiff']) && !empty($_POST['datadiff']))?$_POST['datadiff']:'0'; 
	$data['json'] = $datadiff;	
	$this->load->view('exim/view_bc30_prosesdata', $data);
  
  }
  
	function proses_bc30(){
		$datadiff = (isset($_POST['datadiff']) && !empty($_POST['datadiff']))?$_POST['datadiff']:'0'; 
		$datadiff = json_decode($datadiff);
		$datacsv = '';
		foreach ($datadiff as $data){
			
			$idpebhdr = (isset($data->idpebhdr) && !empty($data->idpebhdr))?$data->idpebhdr :die('{"sts":"ERROR","desc":" Param idpebhdr Tidak Ditemukan"}');
			$gabinvoice = (isset($data->gabinvoice) && !empty($data->gabinvoice))?$data->gabinvoice :die('{"sts":"ERROR","desc":" Param  Tidak Ditemukan"}');
			$NOINV = (isset($data->NOINV) && !empty($data->NOINV))?$data->NOINV :die('{"sts":"ERROR","desc":" Param  Tidak Ditemukan"}');
			 
			$datacsv .= $idpebhdr . ',' . $gabinvoice . ',' . $NOINV;
			$datacsv .= "|";
		}
		
		 //echo "EXEC ('[spi_proses_bc30] @datadiff=''$datacsv''') ";
		 //die();
		//$userid=$this->session->userdata('DX_user_id');
		$userid=$this->session->userdata('DX_username');
		$qry = $this->db->query("

		 EXEC ('[spi_proses_bc30] @datadiff=''$datacsv'', @userid=''$userid'' ') 

		");
		$rs = $qry->result_array();
		// $r = $qry->result();		
		// var_dump($r);die();
		foreach ($rs as $row){
		$responce = array("sts" => $row['status'], "desc" => $row['description'].' Ok');
		}  
		echo json_encode($responce);
  
	}	
	
	function view_bc30_dtl(){
		$idhdrbc30 = (isset($_POST['idhdrbc30']) && !empty($_POST['idhdrbc30']))?$_POST['idhdrbc30']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
	    
		$q = $this->db->query("
			SELECT  id_terkait, doc_out_no, id_data_out_hdr  from [tbl_data_out_hdr] where id_terkait=$idhdrbc30 and doc_out_to_id=2
		");
		
		$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'];		
		}
		
		//echo $id_data_out_hdr; die();
		$q1 = $this->db->query("
			select [id_data_out_hdr]
			  ,[doc_out_no]
			  ,convert(varchar, [doc_out_date], 103) as doc_out_date
			  ,[doc_out_to_id]
			  ,[id_data_out_hdr_h_status]
			  ,[id_terkait]
			  ,convert(varchar, [approve_date], 103) as approve_date
			  FROM[tbl_data_out_hdr]
			  where id_terkait = $idhdrbc30
		");
		
		$rs1 = $q1->result_array();
		//var_dump($rs); die();
		$doc_out_date='';
		$approve_date='';
		foreach ($rs1 as $row1){
			$doc_out_date=$row1['doc_out_date'];		
			$approve_date=$row1['approve_date'];		
		}
		
		$data["doc_out_date"]= $doc_out_date;
		$data["approve_date"]= $approve_date;
		
		
		
		$data["idhdrbc30"]	= $idhdrbc30;
		$data['id_data_out_hdr'] = $id_data_out_hdr;
		$this->load->view('exim/view_bc30_dtl', $data);
  
	}
	
	function loaddatadetil(){
		$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']:'[idpebdtl]'; // get index row - i.e. user click to sort
		$sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		
		// $gabinvoice = (isset($_POST['gabinvoice']) && !empty($_POST['gabinvoice']))?$_POST['gabinvoice']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		 $idhdrbc30 = (isset($_POST['idhdrbc30']) && !empty($_POST['idhdrbc30']))?$_POST['idhdrbc30']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		
		 $where = " id_bc_out_hdr=$idhdrbc30 ";
		

		if ($search == 'true'){
			if (!empty($filters)){
				$array = json_decode($filters);
				foreach($array->rules as $arr=>$k){
					
					if ( $k->field == "[SERIBRG]" ) {
						$searchString = $k->data;
						$where .= " and [SERIBRG] = $searchString "; 
					} else if ( $k->field == "HS" ) {
						$searchString = $k->data;
						$where .= " and HS like  '%".$searchString."%'"; 
					} else if ( $k->field == "URBRG1" ) {
						$searchString = $k->data;
						$where .= " and URBRG1 like  '%".$searchString."%'"; 
					} else if ( $k->field == "URBRG2" ) {
						$searchString = $k->data;
						$where .= " and URBRG2 like  '%".$searchString."%'"; 
					} else if ( $k->field == "URBRG3" ) {
						$searchString = $k->data;
						$where .= " and URBRG3 like  '%".$searchString."%'"; 
					} else if ( $k->field == "URBRG4" ) {
						$searchString = $k->data;
						$where .= " and URBRG4 like  '%".$searchString."%'"; 
					} else if ( $k->field == "DMERK" ) {
						$searchString = $k->data;
						$where .= " and DMERK like  '%".$searchString."%'"; 
					}  else if ( $k->field == "SIZE" ) {
						$searchString = $k->data;
						$where .= " and SIZE like  '%".$searchString."%'"; 
					}  else if ( $k->field == "TYPE" ) {
						$searchString = $k->data;
						$where .= " and TYPE like  '%".$searchString."%'"; 
					}   else if ( $k->field == "KDBRG" ) {
						$searchString = $k->data;
						$where .= " and KDBRG like  '%".$searchString."%'"; 
					}  else if ( $k->field == "JNSATUAN" ) {
						$searchString = $k->data;
						$where .= " and JNSATUAN like  '%".$searchString."%'"; 
					} 
			
				}
			}
		}	
			
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_bc30->bc30datadtl_All($limit,$start,$sidx,$sord, $where);
		$count = $this->mdl_bc30->bc30datadtl_CountAll($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_out_dtl']=$r->id_bc_out_dtl;
			  $responce->rows[$i]['id_bc_out_hdr']=$r->id_bc_out_hdr;
			  $responce->rows[$i]['CAR']=$r->CAR;
			  $responce->rows[$i]['SERIBRG']=$r->SERIBRG;
			  $responce->rows[$i]['HS']=$r->HS;
			  $responce->rows[$i]['URBRG1']=$r->URBRG1;
			  $responce->rows[$i]['URBRG2']=$r->URBRG2;
			  $responce->rows[$i]['URBRG3']=$r->URBRG3;
			  $responce->rows[$i]['URBRG4']=$r->URBRG4;
			  $responce->rows[$i]['DMERK']=$r->DMERK;
			  $responce->rows[$i]['SIZE']=$r->SIZE;
			  $responce->rows[$i]['TYPE']=$r->TYPE;
			  $responce->rows[$i]['KDBRG']=$r->KDBRG;
			  $responce->rows[$i]['KDIZIN']=$r->KDIZIN;
			  $responce->rows[$i]['NOIZIN']=$r->NOIZIN;
			  $responce->rows[$i]['TGIZIN']=$r->TGIZIN;
			  $responce->rows[$i]['JMKOLI']=$r->JMKOLI;
			  $responce->rows[$i]['JNKOLI']=$r->JNKOLI;
			  $responce->rows[$i]['DNilInv']=$r->DNilInv;
			  $responce->rows[$i]['FOBPERBRG']=$r->FOBPERBRG;
			  $responce->rows[$i]['FOBPERSAT']=$r->FOBPERSAT;
			  $responce->rows[$i]['KDPE']=$r->KDPE;
			  $responce->rows[$i]['TARIPPE']=$r->TARIPPE;
			  $responce->rows[$i]['HPATOK']=$r->HPATOK;
			  $responce->rows[$i]['JMSATUAN']=$r->JMSATUAN;
			  $responce->rows[$i]['JNSATUAN']=$r->JNSATUAN;
			  $responce->rows[$i]['NETDET']=$r->NETDET;
			  $responce->rows[$i]['JMSATPE']=$r->JMSATPE;
			  $responce->rows[$i]['JNSATPE']=$r->JNSATPE;
			  $responce->rows[$i]['KDVALPE']=$r->KDVALPE;
			  $responce->rows[$i]['NILVALPE']=$r->NILVALPE;
			  $responce->rows[$i]['PEPERBRG']=$r->PEPERBRG;
			  $responce->rows[$i]['DTLOK']=$r->DTLOK;
			  $responce->rows[$i]['NCV']=$r->NCV;
			  $responce->rows[$i]['DVolume']=$r->DVolume;
			  $responce->rows[$i]['NegAsal']=$r->NegAsal;
			 
		
		$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']:'[idpebdtl]'; // get index row - i.e. user click to sort
		$sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		
		// $gabinvoice = (isset($_POST['gabinvoice']) && !empty($_POST['gabinvoice']))?$_POST['gabinvoice']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		 $idhdrbc30 = (isset($_POST['idhdrbc30']) && !empty($_POST['idhdrbc30']))?$_POST['idhdrbc30']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		
		 $where = " id_bc_out_hdr=$idhdrbc30 ";
		

		if ($search == 'true'){
			if (!empty($filters)){
				$array = json_decode($filters);
				foreach($array->rules as $arr=>$k){
					
					if ( $k->field == "[SERIBRG]" ) {
						$searchString = $k->data;
						$where .= " and [SERIBRG] = $searchString "; 
					} else if ( $k->field == "HS" ) {
						$searchString = $k->data;
						$where .= " and HS like  '%".$searchString."%'"; 
					} 
			
				}
			}
		}	
			
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_bc30->bc30datadok_All($limit,$start,$sidx,$sord, $where);
		$count = $this->mdl_bc30->bc30datadok_CountAll($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_out_dok']=$r->id_bc_out_dok;
			$responce->rows[$i]['id_bc_out_hdr']=$r->id_bc_out_hdr;
			$responce->rows[$i]['CAR']=$r->CAR;
			$responce->rows[$i]['KdDok']=$r->KdDok;
			$responce->rows[$i]['uraian']=$r->uraian;
			$responce->rows[$i]['NoDok']=$r->NoDok;
			$responce->rows[$i]['TgDok']=$r->TgDok;
			$i++;
		}
		echo json_encode($responce);
	
	}
	
	function ceklastcar(){
		$bc30dir = $this->config->item('APP_modulbc30');
		$conn = new COM("ADODB.Connection") or die("Cannot start ADO");  
		$conn->Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=$bc30dir;Password=MumtazFarisHana;");
		$q = "select max(car) as lastofcar from tblPebHdr ";
		$rs = $conn->Execute($q) or die("Error in query: $q. " . $conn->ErrorMsg(). "in LINE __" . __LINE__);
		$lascar = @$rs->Fields("lastofcar")->value;
		$noajunext = substr($lascar, 20, 6) * 1 + 1; 
		$noreg = $this->config->item('APP_noregbc30');
		$nextcar = $noreg.date("Ymd").sprintf('%06d', $noajunext);
		echo $lascar;
		echo "<br />";
		echo "Kemungkinan aju Selanjutnya: $nextcar";
		echo "<br />";
		$rs->Close();
		$conn->Close();

		$rs = null;
		$conn = null;
	
	}
	
	function sendtomodulbc30(){
		$id = (isset($_POST['id']) && !empty($_POST['id']))?$_POST['id']:die('{"sts":"ERROR","desc":" Param ID Tidak Ditemukan"}');
		//$noinv = (isset($_POST['noinv']) && !empty($_POST['noinv']))?$_POST['noinv']:die('{"sts":"ERROR","desc":" Param INVx Tidak Ditemukan"}');
		//$userid=$this->session->userdata('DX_user_id');
		$userid=$this->session->userdata('DX_username');
		$bc30dir = $this->config->item('APP_modulbc30');
		
		$conn = new COM("ADODB.Connection") or die("Cannot start ADO");  
		$conn->Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=$bc30dir;Password=MumtazFarisHana;");
		$q = "select max(car) as lastofcar from tblPebHdr ";
		$rs = $conn->Execute($q) or die("Error in query: $q. " . $conn->ErrorMsg(). "in LINE __" . __LINE__);
		$lascar = @$rs->Fields("lastofcar")->value;
		$noajunext = substr($lascar, 20, 6) * 1 + 1; 
		$noreg = $this->config->item('APP_noregbc30');
		$nextcar = $noreg.date("Ymd").sprintf('%06d', $noajunext);
		$rs->Close();
		$conn->Close();
				
		$this->db->query("
			SET ANSI_NULLS ON
			SET ANSI_WARNINGS ON
		");
		$qryinsmodul = $this->db->query("
			--SET ANSI_NULLS ON
			--SET ANSI_WARNINGS ON
			EXEC sp_insertmodulbc30 @car =N'$nextcar', @idx=$id, @conn='$bc30dir', @userid='$userid'
			
			");
				
		//$qryinsmodul->result();
		$rs = $qryinsmodul->result_array();	
		//var_dump($rs);
		//die();
		foreach ($rs as $row){
			$responce = array("sts" => $row['status'], "desc" => $row['hasil']);
		}  
		echo json_encode($responce);

	}
	
	function sendtomodulbc30manual(){
	
		$id_bc_out_hdr = (isset($_POST['id_bc_out_hdr']) && !empty($_POST['id_bc_out_hdr']))?$_POST['id_bc_out_hdr']:die('{"sts":"ERROR","desc":" Param ID Tidak Ditemukan"}');
		$car = (isset($_POST['car']) && !empty($_POST['car']))?$_POST['car']:die('{"sts":"ERROR","desc":" Param car Tidak Ditemukan"}');
		$tgdaft = (isset($_POST['tgdaft']) && !empty($_POST['tgdaft']))?$_POST['tgdaft']:'';
		$nodaft = (isset($_POST['nodaft']) && !empty($_POST['nodaft']))?$_POST['nodaft']:'';
		$userid=$this->session->userdata('DX_username');
		$qryinsmodul = $this->db->query("
			BEGIN TRY
				exec('sp_insertmodulbc30manual @car=''$car'', @idx=$id_bc_out_hdr, @nodaft=''$nodaft'',@tgdaft=''$tgdaft'',  @userid=''$userid'' ')
			END TRY
			BEGIN CATCH 
				SELECT 
				'08' AS status
				,ERROR_MESSAGE() AS hasil;
				
			END CATCH;	
			
		");
		$rs = $qryinsmodul->result_array();	
		//var_dump($rs);
		//die();
		foreach ($rs as $row){
			$responce = array("sts" => $row['status'], "desc" => $row['hasil']);
		}  
		echo json_encode($responce);
	
	
	
	}
	
	function getdatafrommodulbc30(){
		$id = (isset($_POST['id']) && !empty($_POST['id']))?$_POST['id']:die('{"sts":"ERROR","desc":" Param ID Tidak Ditemukan"}');
		$car = (isset($_POST['car']) && !empty($_POST['car']))?$_POST['car']:die('{"sts":"ERROR","desc":" Param ID Tidak Ditemukan"}');
		$bc30dir = $this->config->item('APP_modulbc30');
		//echo " exec('[sp_getdata_modulbc30] @id=$id, @car=''$car'', @bc30dir=''$bc30dir'' ')"; die();
		$qry = $this->db->query("
			
				BEGIN TRY
				
				 exec('[sp_getdata_modulbc30] @id=$id, @car=''$car'', @bc30dir=''$bc30dir'' ')
				
				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(array('sts' => $status, 'desc' => $msg));
	
	
	
	}
	
	function cek_inv_bc30(){
		$bc30dir = $this->config->item('APP_modulbc30');
		$conn = new COM("ADODB.Connection") or die("Cannot start ADO");  
		$conn->Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=$bc30dir;Password=MumtazFarisHana;");
		$datadiff = (isset($_POST['datadiff']) && !empty($_POST['datadiff']))?$_POST['datadiff']:die('{"sts":"ERROR","desc":" Data  Tidak Ditemukan"}'); 
		$datadiff=json_decode($datadiff);
		$car = '';
		foreach ($datadiff as $data ){
			if ($data->KdDok == '380'){
				$q = "select car, nodok from tblPebDok where NoDok = '" . $data->NoDok ."' ";
				$rs = $conn->Execute($q) or die("Error in query: $q. " . $conn->ErrorMsg(). "in LINE __" . __LINE__); 	
				$car .= @$rs->Fields("car")->value . ' >> ' . @$rs->Fields("nodok")->value .  " <br />";	
				
			}
		}
		
		$rs->Close();
		$conn->Close();

		$rs = null;
		$conn = null;
//		$dcar = explode('|', $car);
		

		$responce = array("sts" => '00', "desc" => $car);
		echo json_encode($responce);
	}
	
	function cek_inv_car(){
		
		$bc23dir = $this->config->item('APP_modulbc30');
		$NoDok = (isset($_POST['NoDok']) && !empty($_POST['NoDok']))?$_POST['NoDok']:die('{"sts":"ERROR","desc":" Param Invoice Tidak Ditemukan"}');
		$TgDok = (isset($_POST['TgDok']) && !empty($_POST['TgDok']))?$_POST['TgDok']:die('{"sts":"ERROR","desc":" Param Invoice Tidak Ditemukan"}');
		
		
		$this->db->query("
			SET ANSI_NULLS ON
			SET ANSI_WARNINGS ON
		");
		
		
		$query = $this->db->query("
			
			select car 
			from OPENDATASOURCE('Microsoft.ACE.OLEDB.12.0',
						'Data Source=$bc23dir;
						Jet OLEDB:Database Password=MumtazFarisHana;Persist Security Info=False;'
						)...tblPebdok
			where NoDok = '$NoDok' and TgDok='$TgDok'
			and [KdDok]='380'
		
		");
		$result = $query->result_array();
		$car = '';
		foreach ($result as $row){
		
			$car .= $row['car'];
		
		}
		
		$responce = array("sts" => '00', "desc" => $car);
		echo json_encode($responce);
	}
	
	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=2
		");
		
		$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 ));
	
	}
	
	function view_bc30_revisi(){
		$id_bc_out_hdr = (isset($_POST['id_bc_out_hdr']) && !empty($_POST['id_bc_out_hdr']))?$_POST['id_bc_out_hdr']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$car = (isset($_POST['car']) && !empty($_POST['car']))?$_POST['car']:die('{"sts":"ERROR","desc":" CAR Param Tidak Ditemukan"}');
		
		$qry = $this->db->query("
			select [NoDok], [TgDok] 
			FROM [dbo].[tbl_bc_out_dok] where [id_bc_out_hdr] = $id_bc_out_hdr and [KdDok]='380'
		 ");
		 $rs = $qry->result_array();
		// $r = $qry->result();		
		//var_dump($rs);die();	
		$dx = "";	
		foreach ($rs as $row){
			$dx .= $row['NoDok'] . "," ;
		
		
		}
		
		$data["id_bc_out_hdr"]=$id_bc_out_hdr;
		$data["car"]=$car; 
		$data["dx"]=$dx;
		$this->load->view('exim/view_bc30_revisi', $data);	
	
	}
	function updatenocar(){
		$carlama = (isset($_POST['carlama']) && !empty($_POST['carlama']))?$_POST['carlama']:die('{"sts":"ERROR","desc":" car lama Param Tidak Ditemukan"}');
		$carbaru = (isset($_POST['carbaru']) && !empty($_POST['carbaru']))?$_POST['carbaru']:die('{"sts":"ERROR","desc":" car baru Param Tidak Ditemukan"}');
		$typedok = (isset($_POST['typedok']) && !empty($_POST['typedok']))?$_POST['typedok']:die('{"sts":"ERROR","desc":" typedok Param Tidak Ditemukan"}');
		$bc_in_from_in = (isset($_POST['bc_in_from_in']) && !empty($_POST['bc_in_from_in']))?$_POST['bc_in_from_in']:die('{"sts":"ERROR","desc":" bc_in_from_in Param Tidak Ditemukan"}');
		$status = '06';
		$msg = 'Unknown Error';
		$bc_in_from = $bc_in_from_in;
		$carbaru = trim(str_replace('-', '',$carbaru));
		$bc_in_from_in = str_replace('SA', '',$bc_in_from_in);
		
		$qry = $this->db->query("
			
			if (select count(*) from tblPebHdr where noprosesdata = '$bc_in_from_in' ) = 0
			begin
				select '06' as status, 'Data SA tidak ditemukan..' as description
				return
			end
			
			if (select count(*) from [tbl_bc_out_hdr] where CAR = '$carlama' ) = 0
			begin
				select '06' as status, 'Data CAR LAMA tidak ditemukan..' as description
				return
			end
			
			declare @idbcout bigint
			set @idbcout = (select id_bc_out_hdr from [tbl_bc_out_hdr] where car = '$carlama'  and bc_in_from_in = '$bc_in_from' )
			
			update [tbl_bc_out_dok] set CAR='$carbaru' where [id_bc_out_hdr] = @idbcout
			update [tbl_bc_out_dtl] set  CAR='$carbaru' where [id_bc_out_hdr] = @idbcout
			update [tbl_bc_out_hdr] set  CAR='$carbaru' where [id_bc_out_hdr] = @idbcout
			
			select '00' as status, 'CAR UPDATED' as description
			
			
		 ");
		 $rs = $qry->result_array();
		// $r = $qry->result();		
		//var_dump($rs);die();	
		
		
		
		
		foreach ($rs as $row){
			$status = $row['status'];
			$msg = $row['description'];
		}
		
		echo json_encode(array('sts' => $status, 'desc' => $msg));
	
	}
	
	function cancelnocar(){
		$idhdrbc30 = (isset($_POST['idhdrbc30']) && !empty($_POST['idhdrbc30']))?$_POST['idhdrbc30']:die('{"sts":"ERROR","desc":" idhdrbc30 Param Tidak Ditemukan"}');
		$typedok = (isset($_POST['typedok']) && !empty($_POST['typedok']))?$_POST['typedok']:die('{"sts":"ERROR","desc":" typedok Param Tidak Ditemukan"}');
		$bc_in_from_in = (isset($_POST['bc_in_from_in']) && !empty($_POST['bc_in_from_in']))?$_POST['bc_in_from_in']:die('{"sts":"ERROR","desc":" bc_in_from_in Param Tidak Ditemukan"}');
		$bc_in_from_in1 = str_replace('SA', '',$bc_in_from_in);
		
		$qry = $this->db->query("
			if (select count(*) from tblPebHdr where noprosesdata = '$bc_in_from_in1' ) = 0
			begin
				select '06' as status, 'Data SA tidak ditemukan..' as description
				return
			end
			
			declare @id_bc_out_hdr bigint
			declare @idpebhdr bigint
			
			set @id_bc_out_hdr = ( select id_bc_out_hdr from [dbo].[tbl_bc_out_hdr] where [bc_in_from_in] = '$bc_in_from_in' )
			if (select count(*) from tbl_data_out_hdr where id_terkait = @id_bc_out_hdr and doc_out_to_id = 2) > 0
			begin
				select '06' as status, 'Sudah ada data pengeluaran..' as description
				return
			end
			
			BEGIN TRY  
	           BEGIN TRANSACTION
				delete from [dbo].[tbl_bc_out_hdr_h_status] where [id_bc_out_hdr] = @id_bc_out_hdr
				delete from [dbo].[tbl_bc_out_kms] where [id_bc_out_hdr] = @id_bc_out_hdr
				delete from [dbo].[tbl_bc_out_con] where [id_bc_out_hdr] = @id_bc_out_hdr
				delete from [dbo].[tbl_bc_out_dok] where [id_bc_out_hdr] = @id_bc_out_hdr
				delete from [dbo].[tbl_bc_out_dtl] where [id_bc_out_hdr] = @id_bc_out_hdr
				delete from [dbo].[tbl_bc_out_hdr] where [id_bc_out_hdr] = @id_bc_out_hdr
				
				delete from [dbo].[tblPebCon] where [idpebhdr] in (select idpebhdr from tblPebHdr where [noprosesdata] = '$bc_in_from_in1')
				delete from [dbo].[tblPebDok] where [idpebhdr] in (select idpebhdr from tblPebHdr where [noprosesdata] = '$bc_in_from_in1')
				delete from [dbo].[tblPebDtl] where [idpebhdr] in (select idpebhdr from tblPebHdr where [noprosesdata] = '$bc_in_from_in1')
				delete from [dbo].[tblPebKms] where [idpebhdr] in (select idpebhdr from tblPebHdr where [noprosesdata] = '$bc_in_from_in1')
				delete from [dbo].[tblPebHdr] where [noprosesdata] = '$bc_in_from_in1'
				COMMIT
			END TRY	
			BEGIN CATCH
			-- Whoops, there was an error
			IF @@TRANCOUNT > 0
			 ROLLBACK

			-- Raise an error with the details of the exception
			DECLARE @ErrMsg nvarchar(4000), @ErrSeverity int
			Set @ErrMsg = ERROR_MESSAGE()
			set @ErrSeverity = ERROR_SEVERITY()
			select @ErrMsg as status,@ErrSeverity as description
			RAISERROR(@ErrMsg, @ErrSeverity, 1)
			END CATCH 
			
			select '00' as status, 'DATA CANCELED..Silahkan Upload ulang SA jika akan diulang' as description
			
			
		 ");
		 $rs = $qry->result_array();
		// $r = $qry->result();		
		//var_dump($rs);die();	
		
		
		
		
		foreach ($rs as $row){
			$status = $row['status'];
			$msg = $row['description'];
		}
		
		echo json_encode(array('sts' => $status, 'desc' => $msg));	
		
	}
	function view_bc30_search(){
	
		$this->load->view('exim/view_bc30_search');
	
	}
	
	function downloadcsvbc30_inv(){
		$tglawal = (isset($_GET['tglawal']) && !empty($_GET['tglawal']))?$_GET['tglawal']:die('{"sts":"ERROR","desc":" idhdrbc30 Param Tidak Ditemukan"}');
		$tglakhir = (isset($_GET['tglakhir']) && !empty($_GET['tglakhir']))?$_GET['tglakhir']:die('{"sts":"ERROR","desc":" idhdrbc30 Param Tidak Ditemukan"}');
		
		
		
		
		$qry = $this->db->query("
			SELECT        tbl_bc_out_hdr.CAR, SUBSTRING(dbo.trim(dbo.tbl_bc_out_hdr.CAR), 19, 2) + '-' + SUBSTRING(dbo.trim(dbo.tbl_bc_out_hdr.CAR), 17, 2) +'-'+ SUBSTRING(dbo.trim(dbo.tbl_bc_out_hdr.CAR), 13, 4)   AS datecar, tbl_bc_out_hdr.NODAFT, CONVERT(char(10),tbl_bc_out_hdr.TGDAFT,103) as TGDAFT, tbl_bc_out_dok.NoDok, CONVERT(char(10), tbl_bc_out_dok.TgDok,103) as TgDok
			FROM            tbl_bc_out_hdr INNER JOIN
									 tbl_bc_out_dok ON tbl_bc_out_hdr.id_bc_out_hdr = tbl_bc_out_dok.id_bc_out_hdr
									 where (tbl_bc_out_hdr.typedok='BC30' and tbl_bc_out_dok.KdDok='380') and tbl_bc_out_hdr.TGDAFT between '$tglawal' and '$tglakhir'
			
			
		 ");
		 $rs = $qry->result_array();
		// $r = $qry->result();		
		//var_dump($rs);die();	
		
		
		
		//$data = array(
		//		'noaju,tgaju,nobc,tgbc,noinv,tginv'
		//);
		$data1 = array();
		foreach ($rs as $row){
			$car = substr($row['CAR'], 6, 6) . '-' . substr($row['CAR'], 12, 8) . '-' . substr($row['CAR'], 20,6);
			$data1[] = $car . "," . str_replace('-','',$row['datecar']) . "," .  $row['NODAFT'] . "," .  str_replace('/','',$row['TGDAFT']) . "," .  $row['NoDok'] . "," .  str_replace('/','',$row['TgDok']) ;
		}
		//$result = array_merge($data, $data1);
		$result = $data1;		
		header('Content-Type: application/excel');
		header('Content-Disposition: attachment; filename="sample.csv"');
		
		$fp = fopen('php://output', 'w');
		foreach ( $result as $line ) {
			$val = explode(",", $line);
			fputcsv($fp, $val);
		}
		fclose($fp);
	
	
	}
 }