<?php
class CT_bc41 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_bc27", 'mdl_bc27');
			$this->load->model("exim/mdl_bc41", 'mdl_bc41');
  }
  
	function index(){

		$this->load->view('exim/view_bc41');
	}
	
	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';
		$id_bc_out_hdr =(isset($_POST['id_bc_out_hdr']) && !empty($_POST['id_bc_out_hdr']))?$_POST['id_bc_out_hdr']:'';
			
		$where = " 1=1 and typedok='bc41' ";
		
		if ($q=='66'){
			$where =" id_bc_out_hdr=$id_bc_out_hdr ";
		}
		

		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_bc41->bc41hdr_All($limit,$start,$sidx,$sord, $where);
		$count = $this->mdl_bc41->bc41hdr_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; 
			
		
			$i++;
		}
		echo json_encode($responce);
			
        
		
		
    }
	
	 function view_bc41_getdata(){

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

	}
	
	function load_bc41_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['statusbc41loaddatahdr']) && !empty($_POST['statusbc41loaddatahdr']))?$_POST['statusbc41loaddatahdr']:'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_bc41->bc41getdatahdr_All($limit,$start,$sidx,$sord, $where);
		$count = $this->mdl_bc41->bc41getdatahdr_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]['idbc41hdr']=$r->idbc41hdr; 
		  $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 getdatabc41(){

		$filename = (isset($_POST['filename']) && !empty($_POST['filename']))?$_POST['filename']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$id_bc41_upload = (isset($_POST['id_bc41_upload']) && !empty($_POST['id_bc41_upload']))?$_POST['id_bc41_upload']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$username = $this->session->userdata('DX_username');
		
	/* 	echo " exec('[spi_getdata_bc41] @filename=''$filename'', @idfile=$id_bc41_upload, @username=''$username'' ')";
	    die();  */
		$qry = $this->db->query("
			BEGIN TRY
			
			 exec('[spi_getdata_bc41] @filename=''$filename'', @idfile=$id_bc41_upload, @username=''$username'' ')
			
			END TRY
			BEGIN CATCH 
				SELECT 
				'06' AS status
				,ERROR_MESSAGE() AS description;
				
			END CATCH;
			
			");
		 $rs = $qry->result_array();
		 // $r = $qry->result();		
		 // var_dump($r);die();
		 foreach ($rs as $row){
			 $status = $row['status'];
			 $msg = $row['description'];
		 }  
		 
		 echo json_encode(array('status' => $status, 'desc' => $msg));



	}
	
	function view_bc41_datauploaded(){
	
		$this->load->view('exim/view_bc41_datauploaded');
	
	}
	
	function prosesdatasa(){
	    $username = $this->session->userdata('DX_username');
		$idbc41hdr = (isset($_POST['idbc41hdr']) && !empty($_POST['idbc41hdr']))?$_POST['idbc41hdr']:die('{"sts":"ERROR","desc":"SA Param Tidak Ditemukan"}');
		$qry = $this->db->query("
			BEGIN TRY
			
			 exec('[spi_getdata_bc41] @idbc41hdr=$idbc41hdr, @userid=''$username'' ')
			
			END TRY
			BEGIN CATCH 
				SELECT 
				'06' AS status
				,ERROR_MESSAGE() AS description;
				
			END CATCH;
			
			");
		 $rs = $qry->result_array();
		 // $r = $qry->result();		
		 // var_dump($r);die();
		 foreach ($rs as $row){
			 $status = $row['status'];
			 $msg = $row['description'];
		 }  
		 
		 echo json_encode(array('status' => $status, 'desc' => $msg));
	
	}
	function view_bc41_getassy(){
		$idbc41hdr = (isset($_POST['idbc41hdr']) && !empty($_POST['idbc41hdr']))?$_POST['idbc41hdr']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$gabinvoice = (isset($_POST['gabinvoice']) && !empty($_POST['gabinvoice']))?$_POST['gabinvoice']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$data['idbc41hdr']=$idbc41hdr;
		$data['gabinvoice']=$gabinvoice;
		$this->load->view('exim/view_bc41_getassy', $data);
	
	}
	
	function data_bc41_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']:'[idbc23dtl]'; // 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"}');
	$idbc41hdr = (isset($_POST['idbc41hdr']) && !empty($_POST['idbc41hdr']))?$_POST['idbc41hdr']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
	
	$where = " 1=1 and idbc41hdr='$idbc41hdr' ";
	

	if ($search == 'true'){
		if (!empty($filters)){
			$array = json_decode($filters);
			foreach($array->rules as $arr=>$k){
				
				if ( $k->field == "[idbc23dtl]" ) {
					$searchString = $k->data;
					$where .= " and [idbc23dtl] like  '%".$searchString."%'"; 
				}
		
			}
		}
	}	
		
	$start = $limit*$page - $limit;
	if ($start<0) $start=0;
	
	$records = $this->mdl_bc41->bc41getdatadtl_All($limit,$start,$sidx,$sord, $where);
	$count = $this->mdl_bc41->bc41getdatadtl_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]['idbc41dtl']=$r->idbc41dtl;
		  $responce->rows[$i]['idbc41hdr']=$r->idbc41hdr;
		  $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 prosesdatabc41(){
	    $username = $this->session->userdata('DX_username');
		$idbc41hdr = (isset($_POST['idbc41hdr']) && !empty($_POST['idbc41hdr']))?$_POST['idbc41hdr']:die('{"sts":"ERROR","desc":"SA Param Tidak Ditemukan"}');
		//echo " exec('[spi_proses_bc41] @idbc41hdr=$idbc41hdr, @userid=''$username'' ')"; die();
		$qry = $this->db->query("
			BEGIN TRY
			
			 exec('[spi_proses_bc41] @idbc41hdr=$idbc41hdr, @userid=''$username'' ')
			
			END TRY
			BEGIN CATCH 
				SELECT 
				'06' AS status
				,ERROR_MESSAGE() AS description;
				
			END CATCH;
			
			");
		 $rs = $qry->result_array();
		 // $r = $qry->result();		
		 // var_dump($r);die();
		 foreach ($rs as $row){
			 $status = $row['status'];
			 $msg = $row['description'];
		 }  
		 
		 echo json_encode(array('sts' => $status, 'desc' => $msg));
	
	}
	
	function update_bc41_sa_hdr(){
		$username = $this->session->userdata('DX_username');
		$idbc41hdr = (isset($_POST['id_bc_out_hdr']) && !empty($_POST['id_bc_out_hdr']))?$_POST['id_bc_out_hdr']:die('{"sts":"ERROR","desc":"SA Param Tidak Ditemukan"}');
		$nobc41 = (isset($_POST['nobc41']) && !empty($_POST['nobc41']))?$_POST['nobc41']:die('{"sts":"ERROR","desc":"NO BC Param Tidak Ditemukan"}');
		$tglbc41 = (isset($_POST['tglbc41']) && !empty($_POST['tglbc41']))?$_POST['tglbc41']:die('{"sts":"ERROR","desc":"TGL Param Tidak Ditemukan"}');
		
		
		//echo " exec('[spi_proses_bc41] @idbc41hdr=$idbc41hdr, @userid=''$username'' ')"; die();
		$qry = $this->db->query("
			BEGIN TRY
			
			 exec(' sp_insertmodulbc41 @idbc41hdr=$idbc41hdr, @userid=''$username'', @nobc41=''$nobc41'', @tglbc41=''$tglbc41'' ')
			
			END TRY
			BEGIN CATCH 
				SELECT 
				'06' AS status
				,ERROR_MESSAGE() AS description;
				
			END CATCH;
			
			");
		 $rs = $qry->result_array();
		 // $r = $qry->result();		
		 // var_dump($r);die();
		 foreach ($rs as $row){
			 $status = $row['status'];
			 $msg = $row['description'];
		 }  
		 
		 echo json_encode(array('sts' => $status, 'desc' => $msg));
	
		
	
	
	}
	function view_bc41_dtl(){
		$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":" 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=$id_bc_out_hdr and doc_out_to_id=5
		");
		
		$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'];		
		}
		
		
		$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 = $id_bc_out_hdr
		");
		
		$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["idhdrbc41"]	= $id_data_out_hdr;
		$data['id_bc_out_hdr']=$id_bc_out_hdr;
	    $data['CAR']=$CAR;
	    $data['loading_indicator'] =  "<div class='loading_indicator'>loading...<br/><img src='./assets/images/loading.gif' alt='loading' /></div>";
		
		$this->load->view('exim/view_bc41_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"}');
		 $idhdrbc41 = (isset($_POST['idhdrbc41']) && !empty($_POST['idhdrbc41']))?$_POST['idhdrbc41']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		
		 $where = " id_bc_out_hdr=$idhdrbc41 ";
		

		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_bc41->bc41datadtl_All($limit,$start,$sidx,$sord, $where);
		$count = $this->mdl_bc41->bc41datadtl_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"}');
		 $idhdrbc41 = (isset($_POST['idhdrbc41']) && !empty($_POST['idhdrbc41']))?$_POST['idhdrbc41']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		
		 $where = " id_bc_out_hdr=$idhdrbc41 ";
		

		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_bc41->bc41datadok_All($limit,$start,$sidx,$sord, $where);
		$count = $this->mdl_bc41->bc41datadok_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 print_bc41_hdr(){
	
		$id_bc_out_hdr = (isset($_REQUEST['id_bc_out_hdr']) && !empty($_REQUEST['id_bc_out_hdr']))?$_REQUEST['id_bc_out_hdr']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		
	
	}
	
	function print_bc41_dtl(){
	
		$id_bc_out_hdr = (isset($_REQUEST['id_bc_out_hdr']) && !empty($_REQUEST['id_bc_out_hdr']))?$_REQUEST['id_bc_out_hdr']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		
	
	}
	
	
	function view_bc41_memo_data_scrap_ak(){
		$id_memo = isset($_POST['id_memo'])?$_POST['id_memo']:'0';
        
		$data["id_memo"] = $id_memo;
		$this->load->view('exim/view_bc41_memo_data_scrap_ak', $data);
		
	}
	
	function view_bc41_memo_data_scrap(){
		$id_memo = isset($_POST['id_memo'])?$_POST['id_memo']:'0';
        
		$data["id_memo"] = $id_memo;
		$this->load->view('exim/view_bc41_memo_data_scrap', $data);
		
	}
	
	function view_bc41_matching_ceisa(){
		$data["param"] = "";
		$this->load->view('exim/view_bc41_matching_ceisa', $data);
		
	}
	
	function datamodul_detil_ceisa(){
		
		$bc23dir = $this->config->item('APP_modulbc23');
		$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']:'car'; // get index row - i.e. user click to sort
        $sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		
		$tglawalbc41_ceisacar = (isset($_POST['tglawalbc41_ceisacar']) && !empty($_POST['tglawalbc41_ceisacar']))?$_POST['tglawalbc41_ceisacar']:date('Y-m-d');
		$tglakhirbc41_ceisacar = (isset($_POST['tglakhirbc41_ceisacar']) && !empty($_POST['tglakhirbc41_ceisacar']))?$_POST['tglakhirbc41_ceisacar']:date('Y-m-d');
		
		$car = (isset($_POST['car']) && !empty($_POST['car']))?$_POST['car']:'';
		
		$where = " 1=1 ";
		
		$where = " CAST(SUBSTRING(a.NOMOR_AJU,21,6) as int) < 500000 and c.KODE_DOKUMEN = '41' and  a.KODE_DOKUMEN_PABEAN ='41' and  convert(varchar(10),a.TANGGAL_AJU,126) between '$tglawalbc41_ceisacar' and '$tglakhirbc41_ceisacar' ";
		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 a.NOMOR_AJU like  '%".$searchString."%'"; 
					} else if ( $k->field == "carx" ) {
						$searchString = $k->data;
						$where .= " and b.car like  '%".$searchString."%'"; 
					}  
				}
			}
		}
		
		
		if (!empty($car)){
			
			$where = " CAST(SUBSTRING(a.NOMOR_AJU,21,6) as int) < 500000 and c.KODE_DOKUMEN = '41' and a.NOMOR_AJU like '%$car%' and a.KODE_DOKUMEN_PABEAN ='41'  ";
		}
		
		$this->db->query("
			SET ANSI_NULLS ON
			SET ANSI_WARNINGS ON
		");
		
		$query = $this->db->query("
			
			select count(*) as cnt from openquery(TPBDB,'select * from tpb_header') a
			left outer join tbl_bc_out_hdr b on a.NOMOR_AJU = b.CAR
			inner join openquery(TPBDB,'select * from referensi_status')   c on c.KODE_STATUS = a.KODE_STATUS
			where $where 
			
		
		");
	
		
		$result = $query->result_array();
		
		$cnt = 0;
		foreach ($result as $row){
		
			$cnt = $row['cnt'];
		
		}
				
		$count=$cnt;
		if( $count >0 ) { $total_pages = ceil($count/$limit); } 
			else { $total_pages = 0; } 
		if ($page > $total_pages) $page=$total_pages; 
		$start = $limit*$page - $limit; // do not put $limit*($page - 1) if ($start<0) $start = 0;
		$batas = $count - (($page - 1) * $limit);
		
		 $q = $this->db->query("
			select top $limit car, convert(varchar(10), xdate, 126) as xdate,  carx, 
			sts,
			jnsbarang,NOMOR_DAFTAR,TANGGAL_DAFTAR,id_bc_out_hdr, ID,
			URAIAN_STATUS
			 from (
				select TOP $batas a.NOMOR_AJU as CAR, a.TANGGAL_AJU as xdate
				,b.car as carx, 
				a.KODE_STATUS as sts, a.KODE_DOKUMEN_PABEAN as jnsbarang 
				,a.NOMOR_DAFTAR as NOMOR_DAFTAR, CONVERT(varchar(10),a.TANGGAL_DAFTAR, 126) as TANGGAL_DAFTAR
				,b.id_bc_out_hdr ,a.ID as ID, c.URAIAN_STATUS
				from openquery(TPBDB,'select * from tpb_header') a 
				inner join openquery(TPBDB,'select * from referensi_status') c on c.KODE_STATUS = a.KODE_STATUS
				left outer join dbo.tbl_bc_out_hdr b on a.NOMOR_AJU = b.car
				where $where
				order by $sidx desc
			)  t1 Order By car $sord
			
			"); 
		$rs = $q->result_array();
		//echo $this->db->last_query(); die();
		
		// var_dump($rs);
		// die(); 
		
		$responce->page = $page;
		$responce->total = $total_pages;
		$responce->records =(integer) $count;
		$i=0;
		
		foreach ($rs as $r){
			
			$responce->rows[$i]["CAR"]=$r["car"];
			$responce->rows[$i]["carx"]=$r["carx"];
			$responce->rows[$i]["xdate"]=$r["xdate"];
	    	$responce->rows[$i]["NOMOR_DAFTAR"]=$r["NOMOR_DAFTAR"];
			$responce->rows[$i]["TANGGAL_DAFTAR"]=$r["TANGGAL_DAFTAR"]; 
			$responce->rows[$i]["sts"]=$r["sts"];
			$responce->rows[$i]["jnsbarang"]=$r["jnsbarang"];
			$responce->rows[$i]["id_bc_out_hdr"]=$r["id_bc_out_hdr"];
			$responce->rows[$i]["ID"]=$r["ID"];
			$responce->rows[$i]["URAIAN_STATUS"]=$r["URAIAN_STATUS"];
			$i++;
		}
		
		echo json_encode($responce);
		die();
	}
	
	function proses_bahanpenolong_memo(){
		$id_memo = (isset($_POST['id_memo']) && !empty($_POST['id_memo']))?$_POST['id_memo']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$username=$this->session->userdata('DX_username');
		$qry = $this->db->query("
			declare @id_memo bigint = '$id_memo', @id_bc_out_hdr bigint, @now date = GETDATE()
			BEGIN TRY
				
				insert into tbl_bc_out_hdr (
					dbo.tbl_bc_out_hdr.CAR,
					dbo.tbl_bc_out_hdr.JnEks,
					dbo.tbl_bc_out_hdr.KatEks,
					dbo.tbl_bc_out_hdr.JNDAG,
					dbo.tbl_bc_out_hdr.JNBYR,
					dbo.tbl_bc_out_hdr.IDEKS,
					dbo.tbl_bc_out_hdr.NPWPEKS,
					dbo.tbl_bc_out_hdr.NamaEks,
					dbo.tbl_bc_out_hdr.AlmtEks,
					dbo.tbl_bc_out_hdr.NoSiup,
					dbo.tbl_bc_out_hdr.TgSiup,
					dbo.tbl_bc_out_hdr.JnPerus,
					dbo.tbl_bc_out_hdr.NoTdp,
					dbo.tbl_bc_out_hdr.TgTdp,
					dbo.tbl_bc_out_hdr.Niper,
					dbo.tbl_bc_out_hdr.StatusH,
					dbo.tbl_bc_out_hdr.NAMABELI,
					dbo.tbl_bc_out_hdr.ALMTBELI,
					dbo.tbl_bc_out_hdr.NEGBELI,
					dbo.tbl_bc_out_hdr.MODA,
					dbo.tbl_bc_out_hdr.PROPBRG,
					dbo.tbl_bc_out_hdr.NEGTUJU,
					dbo.tbl_bc_out_hdr.DELIVERY,
					dbo.tbl_bc_out_hdr.KDVAL,
					dbo.tbl_bc_out_hdr.KdHrg,
					dbo.tbl_bc_out_hdr.NilInv,
					dbo.tbl_bc_out_hdr.FOB,
					dbo.tbl_bc_out_hdr.JMCONT,
					dbo.tbl_bc_out_hdr.BRUTO,
					dbo.tbl_bc_out_hdr.NETTO,
					dbo.tbl_bc_out_hdr.JMBRG,
					dbo.tbl_bc_out_hdr.KDKTR,
					dbo.tbl_bc_out_hdr.KTRGATE,
					dbo.tbl_bc_out_hdr.JNTPB,
					dbo.tbl_bc_out_hdr.KDLOKBRG,
					dbo.tbl_bc_out_hdr.NpwpQq,
					dbo.tbl_bc_out_hdr.NILKURS,
					dbo.tbl_bc_out_hdr.typedok,
					dbo.tbl_bc_out_hdr.statusdok,
					dbo.tbl_bc_out_hdr.bc_in_from_in,
					dbo.tbl_bc_out_hdr.create_date,
					dbo.tbl_bc_out_hdr.create_by,
					dbo.tbl_bc_out_hdr.update_date,
					dbo.tbl_bc_out_hdr.update_by
				) 
				SELECT 
					'-' as CAR
					,1 as JnEks
					,41 as KatEks
					,3 as JNDAG
					,4 as JNBYR
					,5 as IDEKS
					,(select top 1 NPWPEKS from dbo.tblEksportir) as [NPWPEKS]
					,(select top 1 NamaEks from dbo.tblEksportir) as [NamaEks]
					,(select top 1 AlmtEks from dbo.tblEksportir) as [AlmtEks]
					,(select top 1 NoSiup from dbo.tblEksportir) as [NoSiup]
					,(select top 1 TgSiup  from dbo.tblEksportir) as [TgSiup]
					,(select top 1 JnPerus from dbo.tblEksportir) as [JnPerus]
					,(select top 1 NoTdp from dbo.tblEksportir) as [NoTdp]
					,(select top 1 TgTdp from dbo.tblEksportir) as [TgTdp]
					,(select top 1 Niper from dbo.tblEksportir) as [Niper]
					,(select top 1 StatusH from dbo.tblEksportir) as [StatusH]

					,nama_pembeli as NAMABELI
					,(SELECT top 1 supplier_address from tbl_data_supplier where subcode_as400 = tbl_bahanpenolong_memo.kode_pembeli ) as ALMTBELI
					,'ID' as NEGBELI
					,1 as MODA
					,3671 as PROPBRG
					,'ID' as NEGTUJU
					,'FOB' as DELIVERY
					,kdval as KDVAL
					,1 as KdHrg
					,harga_penyerahan as NilInv
					,harga_penyerahan as FOB
					,1 as JMCONT
					,bruto as BRUTO
					,(select sum(netto) from tbl_bahanpenolong_out where id_memo_bp =  @id_memo and doc_out_to = 3	 ) as NETTO
					,(select count(*) from tbl_bahanpenolong_out where id_memo_bp =  @id_memo and doc_out_to = 3	 ) as JMBRG
					,'150100' as KDKTR
					,'150300' as KTRGATE
					,'KB' as JNTPB
					,'1' as KDLOKBRG
					, SUBSTRING(dbo.axRegexReplace(isnull(npwp_pembeli,''),'[^\d]',''),1,15) as NpwpQq
					,(select dbo.get_rate_valuta(GETDATE(),kdval)) as NILKURS
					,'BC41' as typedok
					,'DATA BARU' as statusdok
					,'MEMOBP' as bc_in_from_in
					,@now
					,'$username'
					,@now
					,'$username'
					from tbl_bahanpenolong_memo 
					where id_memo = @id_memo
					set @id_bc_out_hdr = SCOPE_IDENTITY()
									
					insert into tbl_bc_out_dtl (

						dbo.tbl_bc_out_dtl.id_bc_out_hdr,
						dbo.tbl_bc_out_dtl.SERIBRG,
						dbo.tbl_bc_out_dtl.HS,
						dbo.tbl_bc_out_dtl.URBRG1,
						dbo.tbl_bc_out_dtl.URBRG2,
						dbo.tbl_bc_out_dtl.URBRG3,
						dbo.tbl_bc_out_dtl.URBRG4,
						dbo.tbl_bc_out_dtl.DMERK,
						dbo.tbl_bc_out_dtl.[SIZE],
						dbo.tbl_bc_out_dtl.TYPE,
						dbo.tbl_bc_out_dtl.KDBRG,
						dbo.tbl_bc_out_dtl.JMKOLI,
						dbo.tbl_bc_out_dtl.JNKOLI,
						dbo.tbl_bc_out_dtl.DNilInv,
						dbo.tbl_bc_out_dtl.FOBPERBRG,
						dbo.tbl_bc_out_dtl.JMSATUAN,
						dbo.tbl_bc_out_dtl.JNSATUAN,
						dbo.tbl_bc_out_dtl.NETDET,
						dbo.tbl_bc_out_dtl.NegAsal

				) SELECT

					@id_bc_out_hdr
					,row_number() over(order by (select 0)) as SERIBRG
					,hs_kode as HS
					,SUBSTRING(part_name, 1, 40) as URBRG1
					,SUBSTRING(part_name, 41, 40) as URBRG2
					,SUBSTRING(part_name, 81, 40) as URBRG3
					,SUBSTRING(part_name, 121, 40) as URBRG4
					,'-' as DMERK
					,'-' as [SIZE]
					,'-' as TYPE
					,part_no as KDBRG
					,jmkemas as JMKOLI
					,SUBSTRING(jnkemas, 1, 2) as JNKOLI
					,round((price *  qty), 4) as DNilInv
					,price as FOBPERBRG
					,qty as JMSATUAN
					,uom as JNSATUAN
					,netto as  NETDET
					,'ID' as NegAsal

					from tbl_bahanpenolong_out 
					where id_memo_bp =  @id_memo and doc_out_to = 3

					insert into tbl_bc_out_dok (
					id_bc_out_hdr	
					,kddok
						,nodok
						,tgdok
					) SELECT
					@id_bc_out_hdr
					 ,'380'
					,nomor_memo
					,tgl_memo
					from tbl_bahanpenolong_memo 
					where id_memo = @id_memo

				insert into tbl_bc_out_dok (
					id_bc_out_hdr	
					,kddok
						,nodok
						,tgdok
				) 
				SELECT
					@id_bc_out_hdr
					, '217'
					,nomor_memo
					,tgl_memo
					from tbl_bahanpenolong_memo 
				where id_memo = @id_memo
				
				INSERT INTO tbl_bc_out_hdr_h_status
					  (id_bc_out_hdr, statusdok, status_date
							  ,[create_by]
							  ,[create_date]
							  ,[update_by]
							  ,[update_date])
				VALUES     (@id_bc_out_hdr, (select top 1 statusdok from tbl_bc_out_hdr where id_bc_out_hdr = @id_bc_out_hdr), @now
					,'''+@userid+'''
					,@now
					,'''+@userid+'''
					,@now
				)
				declare @ndpbm float, @kdval varchar(5)
				select
					@kdval=kdval
				from  tbl_bahanpenolong_memo where	 id_memo = @id_memo 
				set @ndpbm = (select dbo.get_rate_valuta(@now,@kdval))
				update tbl_bahanpenolong_memo set status_data = 'PROCESSEDBC', typedok='BC41', ndpbm=@ndpbm,id_bc_out_hdr=@id_bc_out_hdr 	where id_memo = @id_memo 
				
			END TRY
			BEGIN CATCH 
				SELECT 
				'error' AS status
				,ERROR_MESSAGE() AS description;
				
			END CATCH; 
			SELECT 
				'00' AS status
				,' SUKSES' 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_bahanpenolong_memo_scrap(){
		$id_memo = (isset($_POST['id_memo']) && !empty($_POST['id_memo']))?$_POST['id_memo']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$username=$this->session->userdata('DX_username');
		$qry = $this->db->query("
			declare @id_memo bigint = '$id_memo', @id_bc_out_hdr bigint, @now date = GETDATE()
			BEGIN TRY
				
				insert into tbl_bc_out_hdr (
					dbo.tbl_bc_out_hdr.CAR,
					dbo.tbl_bc_out_hdr.JnEks,
					dbo.tbl_bc_out_hdr.KatEks,
					dbo.tbl_bc_out_hdr.JNDAG,
					dbo.tbl_bc_out_hdr.JNBYR,
					dbo.tbl_bc_out_hdr.IDEKS,
					dbo.tbl_bc_out_hdr.NPWPEKS,
					dbo.tbl_bc_out_hdr.NamaEks,
					dbo.tbl_bc_out_hdr.AlmtEks,
					dbo.tbl_bc_out_hdr.NoSiup,
					dbo.tbl_bc_out_hdr.TgSiup,
					dbo.tbl_bc_out_hdr.JnPerus,
					dbo.tbl_bc_out_hdr.NoTdp,
					dbo.tbl_bc_out_hdr.TgTdp,
					dbo.tbl_bc_out_hdr.Niper,
					dbo.tbl_bc_out_hdr.StatusH,
					dbo.tbl_bc_out_hdr.NAMABELI,
					dbo.tbl_bc_out_hdr.ALMTBELI,
					dbo.tbl_bc_out_hdr.NEGBELI,
					dbo.tbl_bc_out_hdr.MODA,
					dbo.tbl_bc_out_hdr.PROPBRG,
					dbo.tbl_bc_out_hdr.NEGTUJU,
					dbo.tbl_bc_out_hdr.DELIVERY,
					dbo.tbl_bc_out_hdr.KDVAL,
					dbo.tbl_bc_out_hdr.KdHrg,
					dbo.tbl_bc_out_hdr.NilInv,
					dbo.tbl_bc_out_hdr.FOB,
					dbo.tbl_bc_out_hdr.JMCONT,
					dbo.tbl_bc_out_hdr.BRUTO,
					dbo.tbl_bc_out_hdr.NETTO,
					dbo.tbl_bc_out_hdr.JMBRG,
					dbo.tbl_bc_out_hdr.KDKTR,
					dbo.tbl_bc_out_hdr.KTRGATE,
					dbo.tbl_bc_out_hdr.JNTPB,
					dbo.tbl_bc_out_hdr.KDLOKBRG,
					dbo.tbl_bc_out_hdr.NpwpQq,
					dbo.tbl_bc_out_hdr.NILKURS,
					dbo.tbl_bc_out_hdr.typedok,
					dbo.tbl_bc_out_hdr.statusdok,
					dbo.tbl_bc_out_hdr.bc_in_from_in,
					dbo.tbl_bc_out_hdr.create_date,
					dbo.tbl_bc_out_hdr.create_by,
					dbo.tbl_bc_out_hdr.update_date,
					dbo.tbl_bc_out_hdr.update_by
				) 
				SELECT 
					'-' as CAR
					,1 as JnEks
					,41 as KatEks
					,3 as JNDAG
					,4 as JNBYR
					,5 as IDEKS
					,(select top 1 NPWPEKS from dbo.tblEksportir) as [NPWPEKS]
					,(select top 1 NamaEks from dbo.tblEksportir) as [NamaEks]
					,(select top 1 AlmtEks from dbo.tblEksportir) as [AlmtEks]
					,(select top 1 NoSiup from dbo.tblEksportir) as [NoSiup]
					,(select top 1 TgSiup  from dbo.tblEksportir) as [TgSiup]
					,(select top 1 JnPerus from dbo.tblEksportir) as [JnPerus]
					,(select top 1 NoTdp from dbo.tblEksportir) as [NoTdp]
					,(select top 1 TgTdp from dbo.tblEksportir) as [TgTdp]
					,(select top 1 Niper from dbo.tblEksportir) as [Niper]
					,(select top 1 StatusH from dbo.tblEksportir) as [StatusH]

					,nama_pembeli as NAMABELI
					,(SELECT top 1 supplier_address from tbl_data_supplier where subcode_as400 = tbl_bahanpenolong_memo.kode_pembeli ) as ALMTBELI
					,'ID' as NEGBELI
					,1 as MODA
					,3671 as PROPBRG
					,'ID' as NEGTUJU
					,'FOB' as DELIVERY
					,kdval as KDVAL
					,1 as KdHrg
					,round(((select  sum(dbo.tbl_bahanpenolong_memo_scrap.harga_scrap) from tbl_bahanpenolong_memo_scrap where id_memo =  @id_memo ) * (select sum(dbo.tbl_bahanpenolong_memo_scrap.qty_scrap)  from tbl_bahanpenolong_memo_scrap where id_memo =  @id_memo)), 4) as NilInv
					,round(((select  sum(dbo.tbl_bahanpenolong_memo_scrap.harga_scrap) from tbl_bahanpenolong_memo_scrap where id_memo =  @id_memo ) * (select sum(dbo.tbl_bahanpenolong_memo_scrap.qty_scrap)  from tbl_bahanpenolong_memo_scrap where id_memo =  @id_memo)), 4) as FOB
					,1 as JMCONT
					,bruto as BRUTO
					,(select sum(netto_scrap) from tbl_bahanpenolong_memo_scrap where id_memo =  @id_memo ) as NETTO
					,(select count(cnt) from (
						select count(kode_scrap) as cnt from tbl_bahanpenolong_memo_scrap where id_memo =  @id_memo GROUP BY kode_scrap
						) x
					) as JMBRG
					,'150100' as KDKTR
					,'150300' as KTRGATE
					,'KB' as JNTPB
					,'1' as KDLOKBRG
					, SUBSTRING(dbo.axRegexReplace(isnull(npwp_pembeli,''),'[^\d]',''),1,15) as NpwpQq
					,(select dbo.get_rate_valuta(GETDATE(),kdval)) as NILKURS
					,'BC41' as typedok
					,'DATA BARU' as statusdok
					,'MEMOBPSCR' as bc_in_from_in
					,@now
					,'admin'
					,@now
					,'admin'
					from tbl_bahanpenolong_memo 
					where id_memo = @id_memo
					set @id_bc_out_hdr = SCOPE_IDENTITY()
									
					insert into tbl_bc_out_dtl (

						dbo.tbl_bc_out_dtl.id_bc_out_hdr,
						dbo.tbl_bc_out_dtl.SERIBRG,
						dbo.tbl_bc_out_dtl.HS,
						dbo.tbl_bc_out_dtl.URBRG1,
						dbo.tbl_bc_out_dtl.URBRG2,
						dbo.tbl_bc_out_dtl.URBRG3,
						dbo.tbl_bc_out_dtl.URBRG4,
						dbo.tbl_bc_out_dtl.DMERK,
						dbo.tbl_bc_out_dtl.[SIZE],
						dbo.tbl_bc_out_dtl.TYPE,
						dbo.tbl_bc_out_dtl.KDBRG,
						dbo.tbl_bc_out_dtl.JMKOLI,
						dbo.tbl_bc_out_dtl.JNKOLI,
						dbo.tbl_bc_out_dtl.DNilInv,
						dbo.tbl_bc_out_dtl.FOBPERBRG,
						dbo.tbl_bc_out_dtl.JMSATUAN,
						dbo.tbl_bc_out_dtl.JNSATUAN,
						dbo.tbl_bc_out_dtl.NETDET,
						dbo.tbl_bc_out_dtl.NegAsal

				) SELECT

					@id_bc_out_hdr
					,row_number() over(order by (select 0)) as SERIBRG
					,(select hs_code from tbl_scrap_data where part_no = dbo.tbl_bahanpenolong_memo_scrap.kode_scrap ) as HS
					,SUBSTRING(dbo.tbl_bahanpenolong_memo_scrap.nama_scrap, 1, 40) as URBRG1
					,SUBSTRING(dbo.tbl_bahanpenolong_memo_scrap.nama_scrap, 41, 40) as URBRG2
					,SUBSTRING(dbo.tbl_bahanpenolong_memo_scrap.nama_scrap, 81, 40) as URBRG3
					,SUBSTRING(dbo.tbl_bahanpenolong_memo_scrap.nama_scrap, 121, 40) as URBRG4
					,'-' as DMERK
					,'-' as [SIZE]
					,'-' as TYPE
					,dbo.tbl_bahanpenolong_memo_scrap.kode_scrap as KDBRG
					,sum(dbo.tbl_bahanpenolong_memo_scrap.jmkemas_scrap) as JMKOLI
					,SUBSTRING(jnkemas_scrap, 1, 2) as JNKOLI
					,round((sum(dbo.tbl_bahanpenolong_memo_scrap.harga_scrap) *  sum(dbo.tbl_bahanpenolong_memo_scrap.qty_scrap)), 4) as DNilInv
					,sum(dbo.tbl_bahanpenolong_memo_scrap.harga_scrap) as FOBPERBRG
					,sum(dbo.tbl_bahanpenolong_memo_scrap.qty_scrap) as JMSATUAN
					,dbo.tbl_bahanpenolong_memo_scrap.satuan as JNSATUAN
					,sum(dbo.tbl_bahanpenolong_memo_scrap.netto_scrap) as  NETDET
					,'ID' as NegAsal
FROM
					dbo.tbl_bahanpenolong_memo_scrap where id_memo = @id_memo
				GROUP BY
					kode_scrap, nama_scrap, satuan, jnkemas_scrap

					insert into tbl_bc_out_dok (
					id_bc_out_hdr	
					,kddok
						,nodok
						,tgdok
					) SELECT
					@id_bc_out_hdr
					 ,'380'
					,nomor_memo
					,tgl_memo
					from tbl_bahanpenolong_memo 
					where id_memo = @id_memo

				insert into tbl_bc_out_dok (
					id_bc_out_hdr	
					,kddok
						,nodok
						,tgdok
				) 
				SELECT
					@id_bc_out_hdr
					, '217'
					,nomor_memo
					,tgl_memo
					from tbl_bahanpenolong_memo 
				where id_memo = @id_memo
				
				INSERT INTO tbl_bc_out_hdr_h_status
					  (id_bc_out_hdr, statusdok, status_date
							  ,[create_by]
							  ,[create_date]
							  ,[update_by]
							  ,[update_date])
				VALUES     (@id_bc_out_hdr, (select top 1 statusdok from tbl_bc_out_hdr where id_bc_out_hdr = @id_bc_out_hdr), @now
					,'''+@userid+'''
					,@now
					,'''+@userid+'''
					,@now
				)
				declare @ndpbm float, @kdval varchar(5)
				select
					@kdval=kdval
				from  tbl_bahanpenolong_memo where	 id_memo = @id_memo 
				set @ndpbm = (select dbo.get_rate_valuta(@now,@kdval))
				update tbl_bahanpenolong_memo set status_data = 'PROCESSEDBC', typedok='BC41', ndpbm=@ndpbm,id_bc_out_hdr=@id_bc_out_hdr 	where id_memo = @id_memo 
				
			END TRY
			BEGIN CATCH 
				SELECT 
				'error' AS status
				,ERROR_MESSAGE() AS description;
				
			END CATCH; 
			SELECT 
				'00' AS status
				,' SUKSES' 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_alatkantor_memo(){
		$id_memo = (isset($_POST['id_memo']) && !empty($_POST['id_memo']))?$_POST['id_memo']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$username=$this->session->userdata('DX_username');
		$qry = $this->db->query("
			declare @id_memo bigint = '$id_memo', @id_bc_out_hdr bigint, @now date = GETDATE()
			BEGIN TRY
				
				insert into tbl_bc_out_hdr (
					dbo.tbl_bc_out_hdr.CAR,
					dbo.tbl_bc_out_hdr.JnEks,
					dbo.tbl_bc_out_hdr.KatEks,
					dbo.tbl_bc_out_hdr.JNDAG,
					dbo.tbl_bc_out_hdr.JNBYR,
					dbo.tbl_bc_out_hdr.IDEKS,
					dbo.tbl_bc_out_hdr.NPWPEKS,
					dbo.tbl_bc_out_hdr.NamaEks,
					dbo.tbl_bc_out_hdr.AlmtEks,
					dbo.tbl_bc_out_hdr.NoSiup,
					dbo.tbl_bc_out_hdr.TgSiup,
					dbo.tbl_bc_out_hdr.JnPerus,
					dbo.tbl_bc_out_hdr.NoTdp,
					dbo.tbl_bc_out_hdr.TgTdp,
					dbo.tbl_bc_out_hdr.Niper,
					dbo.tbl_bc_out_hdr.StatusH,
					dbo.tbl_bc_out_hdr.NAMABELI,
					dbo.tbl_bc_out_hdr.ALMTBELI,
					dbo.tbl_bc_out_hdr.NEGBELI,
					dbo.tbl_bc_out_hdr.MODA,
					dbo.tbl_bc_out_hdr.PROPBRG,
					dbo.tbl_bc_out_hdr.NEGTUJU,
					dbo.tbl_bc_out_hdr.DELIVERY,
					dbo.tbl_bc_out_hdr.KDVAL,
					dbo.tbl_bc_out_hdr.KdHrg,
					dbo.tbl_bc_out_hdr.NilInv,
					dbo.tbl_bc_out_hdr.FOB,
					dbo.tbl_bc_out_hdr.JMCONT,
					dbo.tbl_bc_out_hdr.BRUTO,
					dbo.tbl_bc_out_hdr.NETTO,
					dbo.tbl_bc_out_hdr.JMBRG,
					dbo.tbl_bc_out_hdr.KDKTR,
					dbo.tbl_bc_out_hdr.KTRGATE,
					dbo.tbl_bc_out_hdr.JNTPB,
					dbo.tbl_bc_out_hdr.KDLOKBRG,
					dbo.tbl_bc_out_hdr.NpwpQq,
					dbo.tbl_bc_out_hdr.NILKURS,
					dbo.tbl_bc_out_hdr.typedok,
					dbo.tbl_bc_out_hdr.statusdok,
					dbo.tbl_bc_out_hdr.bc_in_from_in,
					dbo.tbl_bc_out_hdr.create_date,
					dbo.tbl_bc_out_hdr.create_by,
					dbo.tbl_bc_out_hdr.update_date,
					dbo.tbl_bc_out_hdr.update_by
				) 
				SELECT 
					'-' as CAR
					,1 as JnEks
					,41 as KatEks
					,3 as JNDAG
					,4 as JNBYR
					,5 as IDEKS
					,(select top 1 NPWPEKS from dbo.tblEksportir) as [NPWPEKS]
					,(select top 1 NamaEks from dbo.tblEksportir) as [NamaEks]
					,(select top 1 AlmtEks from dbo.tblEksportir) as [AlmtEks]
					,(select top 1 NoSiup from dbo.tblEksportir) as [NoSiup]
					,(select top 1 TgSiup  from dbo.tblEksportir) as [TgSiup]
					,(select top 1 JnPerus from dbo.tblEksportir) as [JnPerus]
					,(select top 1 NoTdp from dbo.tblEksportir) as [NoTdp]
					,(select top 1 TgTdp from dbo.tblEksportir) as [TgTdp]
					,(select top 1 Niper from dbo.tblEksportir) as [Niper]
					,(select top 1 StatusH from dbo.tblEksportir) as [StatusH]

					,nama_pembeli as NAMABELI
					,(SELECT top 1 supplier_address from tbl_data_supplier where subcode_as400 = tbl_alatkantor_memo.kode_pembeli ) as ALMTBELI
					,'ID' as NEGBELI
					,1 as MODA
					,3671 as PROPBRG
					,'ID' as NEGTUJU
					,'FOB' as DELIVERY
					,kdval as KDVAL
					,1 as KdHrg
					,harga_penyerahan as NilInv
					,harga_penyerahan as FOB
					,1 as JMCONT
					,bruto as BRUTO
					,(select sum(netto) from tbl_alatkantor_out where id_memo_ak =  @id_memo and doc_out_to = 3	 ) as NETTO
					,(select count(*) from tbl_alatkantor_out where id_memo_ak =  @id_memo and doc_out_to = 3	 ) as JMBRG
					,'150100' as KDKTR
					,'150300' as KTRGATE
					,'KB' as JNTPB
					,'1' as KDLOKBRG
					, SUBSTRING(dbo.axRegexReplace(isnull(npwp_pembeli,''),'[^\d]',''),1,15) as NpwpQq
					,(select dbo.get_rate_valuta(GETDATE(),kdval)) as NILKURS
					,'BC41' as typedok
					,'DATA BARU' as statusdok
					,'MEMOBP' as bc_in_from_in
					,@now
					,'$username'
					,@now
					,'$username'
					from tbl_alatkantor_memo 
					where id_memo = @id_memo
					set @id_bc_out_hdr = SCOPE_IDENTITY()
									
					insert into tbl_bc_out_dtl (

						dbo.tbl_bc_out_dtl.id_bc_out_hdr,
						dbo.tbl_bc_out_dtl.SERIBRG,
						dbo.tbl_bc_out_dtl.HS,
						dbo.tbl_bc_out_dtl.URBRG1,
						dbo.tbl_bc_out_dtl.URBRG2,
						dbo.tbl_bc_out_dtl.URBRG3,
						dbo.tbl_bc_out_dtl.URBRG4,
						dbo.tbl_bc_out_dtl.DMERK,
						dbo.tbl_bc_out_dtl.[SIZE],
						dbo.tbl_bc_out_dtl.TYPE,
						dbo.tbl_bc_out_dtl.KDBRG,
						dbo.tbl_bc_out_dtl.JMKOLI,
						dbo.tbl_bc_out_dtl.JNKOLI,
						dbo.tbl_bc_out_dtl.DNilInv,
						dbo.tbl_bc_out_dtl.FOBPERBRG,
						dbo.tbl_bc_out_dtl.JMSATUAN,
						dbo.tbl_bc_out_dtl.JNSATUAN,
						dbo.tbl_bc_out_dtl.NETDET,
						dbo.tbl_bc_out_dtl.NegAsal

				) SELECT

					@id_bc_out_hdr
					,row_number() over(order by (select 0)) as SERIBRG
					,hs_kode as HS
					,SUBSTRING(part_name, 1, 40) as URBRG1
					,SUBSTRING(part_name, 41, 40) as URBRG2
					,SUBSTRING(part_name, 81, 40) as URBRG3
					,SUBSTRING(part_name, 121, 40) as URBRG4
					,'-' as DMERK
					,'-' as [SIZE]
					,'-' as TYPE
					,part_no as KDBRG
					,jmkemas as JMKOLI
					,SUBSTRING(jnkemas, 1, 2) as JNKOLI
					,round((price *  qty), 4) as DNilInv
					,price as FOBPERBRG
					,qty as JMSATUAN
					,uom as JNSATUAN
					,netto as  NETDET
					,'ID' as NegAsal

					from tbl_alatkantor_out 
					where id_memo_ak =  @id_memo and doc_out_to = 3

					insert into tbl_bc_out_dok (
					id_bc_out_hdr	
					,kddok
						,nodok
						,tgdok
					) SELECT
					@id_bc_out_hdr
					 ,'380'
					,nomor_memo
					,tgl_memo
					from tbl_alatkantor_memo 
					where id_memo = @id_memo

				insert into tbl_bc_out_dok (
					id_bc_out_hdr	
					,kddok
						,nodok
						,tgdok
				) 
				SELECT
					@id_bc_out_hdr
					, '217'
					,nomor_memo
					,tgl_memo
					from tbl_alatkantor_memo 
				where id_memo = @id_memo
				
				INSERT INTO tbl_bc_out_hdr_h_status
					  (id_bc_out_hdr, statusdok, status_date
							  ,[create_by]
							  ,[create_date]
							  ,[update_by]
							  ,[update_date])
				VALUES     (@id_bc_out_hdr, (select top 1 statusdok from tbl_bc_out_hdr where id_bc_out_hdr = @id_bc_out_hdr), @now
					,'''+@userid+'''
					,@now
					,'''+@userid+'''
					,@now
				)
				declare @ndpbm float, @kdval varchar(5)
				select
					@kdval=kdval
				from  tbl_alatkantor_memo where	 id_memo = @id_memo 
				set @ndpbm = (select dbo.get_rate_valuta(@now,@kdval))
				update tbl_alatkantor_memo set status_data = 'PROCESSEDBC', typedok='BC41', ndpbm=@ndpbm,id_bc_out_hdr=@id_bc_out_hdr 	where id_memo = @id_memo 
				
			END TRY
			BEGIN CATCH 
				SELECT 
				'error' AS status
				,ERROR_MESSAGE() AS description;
				
			END CATCH; 
			SELECT 
				'00' AS status
				,' SUKSES' 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_alatkantor_memo_scrap(){
		$id_memo = (isset($_POST['id_memo']) && !empty($_POST['id_memo']))?$_POST['id_memo']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$username=$this->session->userdata('DX_username');
		$qry = $this->db->query("
			declare @id_memo bigint = '$id_memo', @id_bc_out_hdr bigint, @now date = GETDATE()
			BEGIN TRY
				
				insert into tbl_bc_out_hdr (
					dbo.tbl_bc_out_hdr.CAR,
					dbo.tbl_bc_out_hdr.JnEks,
					dbo.tbl_bc_out_hdr.KatEks,
					dbo.tbl_bc_out_hdr.JNDAG,
					dbo.tbl_bc_out_hdr.JNBYR,
					dbo.tbl_bc_out_hdr.IDEKS,
					dbo.tbl_bc_out_hdr.NPWPEKS,
					dbo.tbl_bc_out_hdr.NamaEks,
					dbo.tbl_bc_out_hdr.AlmtEks,
					dbo.tbl_bc_out_hdr.NoSiup,
					dbo.tbl_bc_out_hdr.TgSiup,
					dbo.tbl_bc_out_hdr.JnPerus,
					dbo.tbl_bc_out_hdr.NoTdp,
					dbo.tbl_bc_out_hdr.TgTdp,
					dbo.tbl_bc_out_hdr.Niper,
					dbo.tbl_bc_out_hdr.StatusH,
					dbo.tbl_bc_out_hdr.NAMABELI,
					dbo.tbl_bc_out_hdr.ALMTBELI,
					dbo.tbl_bc_out_hdr.NEGBELI,
					dbo.tbl_bc_out_hdr.MODA,
					dbo.tbl_bc_out_hdr.PROPBRG,
					dbo.tbl_bc_out_hdr.NEGTUJU,
					dbo.tbl_bc_out_hdr.DELIVERY,
					dbo.tbl_bc_out_hdr.KDVAL,
					dbo.tbl_bc_out_hdr.KdHrg,
					dbo.tbl_bc_out_hdr.NilInv,
					dbo.tbl_bc_out_hdr.FOB,
					dbo.tbl_bc_out_hdr.JMCONT,
					dbo.tbl_bc_out_hdr.BRUTO,
					dbo.tbl_bc_out_hdr.NETTO,
					dbo.tbl_bc_out_hdr.JMBRG,
					dbo.tbl_bc_out_hdr.KDKTR,
					dbo.tbl_bc_out_hdr.KTRGATE,
					dbo.tbl_bc_out_hdr.JNTPB,
					dbo.tbl_bc_out_hdr.KDLOKBRG,
					dbo.tbl_bc_out_hdr.NpwpQq,
					dbo.tbl_bc_out_hdr.NILKURS,
					dbo.tbl_bc_out_hdr.typedok,
					dbo.tbl_bc_out_hdr.statusdok,
					dbo.tbl_bc_out_hdr.bc_in_from_in,
					dbo.tbl_bc_out_hdr.create_date,
					dbo.tbl_bc_out_hdr.create_by,
					dbo.tbl_bc_out_hdr.update_date,
					dbo.tbl_bc_out_hdr.update_by
				) 
				SELECT 
					'-' as CAR
					,1 as JnEks
					,41 as KatEks
					,3 as JNDAG
					,4 as JNBYR
					,5 as IDEKS
					,(select top 1 NPWPEKS from dbo.tblEksportir) as [NPWPEKS]
					,(select top 1 NamaEks from dbo.tblEksportir) as [NamaEks]
					,(select top 1 AlmtEks from dbo.tblEksportir) as [AlmtEks]
					,(select top 1 NoSiup from dbo.tblEksportir) as [NoSiup]
					,(select top 1 TgSiup  from dbo.tblEksportir) as [TgSiup]
					,(select top 1 JnPerus from dbo.tblEksportir) as [JnPerus]
					,(select top 1 NoTdp from dbo.tblEksportir) as [NoTdp]
					,(select top 1 TgTdp from dbo.tblEksportir) as [TgTdp]
					,(select top 1 Niper from dbo.tblEksportir) as [Niper]
					,(select top 1 StatusH from dbo.tblEksportir) as [StatusH]

					,nama_pembeli as NAMABELI
					,(SELECT top 1 supplier_address from tbl_data_supplier where subcode_as400 = tbl_alatkantor_memo.kode_pembeli ) as ALMTBELI
					,'ID' as NEGBELI
					,1 as MODA
					,3671 as PROPBRG
					,'ID' as NEGTUJU
					,'FOB' as DELIVERY
					,kdval as KDVAL
					,1 as KdHrg
					,round(((select  sum(dbo.tbl_alatkantor_memo_scrap.harga_scrap) from tbl_alatkantor_memo_scrap where id_memo =  @id_memo ) * (select sum(dbo.tbl_alatkantor_memo_scrap.qty_scrap)  from tbl_alatkantor_memo_scrap where id_memo =  @id_memo)), 4) as NilInv
					,round(((select  sum(dbo.tbl_alatkantor_memo_scrap.harga_scrap) from tbl_alatkantor_memo_scrap where id_memo =  @id_memo ) * (select sum(dbo.tbl_alatkantor_memo_scrap.qty_scrap)  from tbl_alatkantor_memo_scrap where id_memo =  @id_memo)), 4) as FOB
					,1 as JMCONT
					,bruto as BRUTO
					,(select sum(netto_scrap) from tbl_alatkantor_memo_scrap where id_memo =  @id_memo ) as NETTO
					,(select count(cnt) from (
						select count(kode_scrap) as cnt from tbl_alatkantor_memo_scrap where id_memo =  @id_memo GROUP BY kode_scrap
						) x
					) as JMBRG
					,'150100' as KDKTR
					,'150300' as KTRGATE
					,'KB' as JNTPB
					,'1' as KDLOKBRG
					, SUBSTRING(dbo.axRegexReplace(isnull(npwp_pembeli,''),'[^\d]',''),1,15) as NpwpQq
					,(select dbo.get_rate_valuta(GETDATE(),kdval)) as NILKURS
					,'BC41' as typedok
					,'DATA BARU' as statusdok
					,'MEMOBPSCR' as bc_in_from_in
					,@now
					,'admin'
					,@now
					,'admin'
					from tbl_alatkantor_memo 
					where id_memo = @id_memo
					set @id_bc_out_hdr = SCOPE_IDENTITY()
									
					insert into tbl_bc_out_dtl (

						dbo.tbl_bc_out_dtl.id_bc_out_hdr,
						dbo.tbl_bc_out_dtl.SERIBRG,
						dbo.tbl_bc_out_dtl.HS,
						dbo.tbl_bc_out_dtl.URBRG1,
						dbo.tbl_bc_out_dtl.URBRG2,
						dbo.tbl_bc_out_dtl.URBRG3,
						dbo.tbl_bc_out_dtl.URBRG4,
						dbo.tbl_bc_out_dtl.DMERK,
						dbo.tbl_bc_out_dtl.[SIZE],
						dbo.tbl_bc_out_dtl.TYPE,
						dbo.tbl_bc_out_dtl.KDBRG,
						dbo.tbl_bc_out_dtl.JMKOLI,
						dbo.tbl_bc_out_dtl.JNKOLI,
						dbo.tbl_bc_out_dtl.DNilInv,
						dbo.tbl_bc_out_dtl.FOBPERBRG,
						dbo.tbl_bc_out_dtl.JMSATUAN,
						dbo.tbl_bc_out_dtl.JNSATUAN,
						dbo.tbl_bc_out_dtl.NETDET,
						dbo.tbl_bc_out_dtl.NegAsal

				) SELECT

					@id_bc_out_hdr
					,row_number() over(order by (select 0)) as SERIBRG
					,(select hs_code from tbl_scrap_data where part_no = dbo.tbl_alatkantor_memo_scrap.kode_scrap ) as HS
					,SUBSTRING(dbo.tbl_alatkantor_memo_scrap.nama_scrap, 1, 40) as URBRG1
					,SUBSTRING(dbo.tbl_alatkantor_memo_scrap.nama_scrap, 41, 40) as URBRG2
					,SUBSTRING(dbo.tbl_alatkantor_memo_scrap.nama_scrap, 81, 40) as URBRG3
					,SUBSTRING(dbo.tbl_alatkantor_memo_scrap.nama_scrap, 121, 40) as URBRG4
					,'-' as DMERK
					,'-' as [SIZE]
					,'-' as TYPE
					,dbo.tbl_alatkantor_memo_scrap.kode_scrap as KDBRG
					,sum(dbo.tbl_alatkantor_memo_scrap.jmkemas_scrap) as JMKOLI
					,SUBSTRING(jnkemas_scrap, 1, 2) as JNKOLI
					,round((sum(dbo.tbl_alatkantor_memo_scrap.harga_scrap) *  sum(dbo.tbl_alatkantor_memo_scrap.qty_scrap)), 4) as DNilInv
					,sum(dbo.tbl_alatkantor_memo_scrap.harga_scrap) as FOBPERBRG
					,sum(dbo.tbl_alatkantor_memo_scrap.qty_scrap) as JMSATUAN
					,dbo.tbl_alatkantor_memo_scrap.satuan as JNSATUAN
					,sum(dbo.tbl_alatkantor_memo_scrap.netto_scrap) as  NETDET
					,'ID' as NegAsal
FROM
					dbo.tbl_alatkantor_memo_scrap where id_memo = @id_memo
				GROUP BY
					kode_scrap, nama_scrap, satuan, jnkemas_scrap

					insert into tbl_bc_out_dok (
					id_bc_out_hdr	
					,kddok
						,nodok
						,tgdok
					) SELECT
					@id_bc_out_hdr
					 ,'380'
					,nomor_memo
					,tgl_memo
					from tbl_alatkantor_memo 
					where id_memo = @id_memo

				insert into tbl_bc_out_dok (
					id_bc_out_hdr	
					,kddok
						,nodok
						,tgdok
				) 
				SELECT
					@id_bc_out_hdr
					, '217'
					,nomor_memo
					,tgl_memo
					from tbl_alatkantor_memo 
				where id_memo = @id_memo
				
				INSERT INTO tbl_bc_out_hdr_h_status
					  (id_bc_out_hdr, statusdok, status_date
							  ,[create_by]
							  ,[create_date]
							  ,[update_by]
							  ,[update_date])
				VALUES     (@id_bc_out_hdr, (select top 1 statusdok from tbl_bc_out_hdr where id_bc_out_hdr = @id_bc_out_hdr), @now
					,'''+@userid+'''
					,@now
					,'''+@userid+'''
					,@now
				)
				declare @ndpbm float, @kdval varchar(5)
				select
					@kdval=kdval
				from  tbl_alatkantor_memo where	 id_memo = @id_memo 
				set @ndpbm = (select dbo.get_rate_valuta(@now,@kdval))
				update tbl_alatkantor_memo set status_data = 'PROCESSEDBC', typedok='BC41', ndpbm=@ndpbm,id_bc_out_hdr=@id_bc_out_hdr 	where id_memo = @id_memo 
				
			END TRY
			BEGIN CATCH 
				SELECT 
				'error' AS status
				,ERROR_MESSAGE() AS description;
				
			END CATCH; 
			SELECT 
				'00' AS status
				,' SUKSES' 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 cancelnocar_nonsa(){
		$uid = $this->session->userdata('DX_username');
		if ($uid != 'admin') {
			die('{"sts":"ERROR","desc":" Harus Admin"}');
		
		}
		$idhdrbc41 = (isset($_POST['idhdrbc41']) && !empty($_POST['idhdrbc41']))?$_POST['idhdrbc41']:die('{"sts":"ERROR","desc":" idhdrbc41 Param Tidak Ditemukan"}');
		$typedok = (isset($_POST['typedok']) && !empty($_POST['typedok']))?$_POST['typedok']:die('{"sts":"ERROR","desc":" typedok Param Tidak Ditemukan"}');
		
		$qry = $this->db->query("
			declare @bc_in_from_in varchar(50), @noprosesdata varchar(50)
			set @bc_in_from_in=(select top 1 bc_in_from_in from tbl_bc_out_hdr where id_bc_out_hdr=$idhdrbc41)
			--set @noprosesdata = replace(@bc_in_from_in, 'SA', '') 
			--if (select count(*) from tblbc41hdr where noprosesdata = replace(@bc_in_from_in, 'SA', '') ) = 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 and id_bc_out_hdr=$idhdrbc41 )
			
			declare @id_data_out_hdr bigint
			set @id_data_out_hdr = (select id_data_out_hdr from tbl_data_out_hdr  where id_terkait = @id_bc_out_hdr and doc_out_to_id=5)
			
			BEGIN TRY  
	           	BEGIN TRANSACTION
				delete from tbl_bc_out_bahan_baku where id_bc_out_hdr = @id_bc_out_hdr
				delete from tbl_bc_out_bahan_baku_tarif where id_bc_out_hdr = @id_bc_out_hdr
				
				delete from tbl_data_out_hdr_h_status where id_data_out_hdr=@id_data_out_hdr
				delete from tbl_data_out_dtl where id_data_out_hdr=@id_data_out_hdr and type_items='FG'
				delete from tbl_data_out_hdr where id_data_out_hdr=@id_data_out_hdr and doc_out_to_id=4 
				
				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].[tblbc41dtl] where [idbc41hdr] in (select idbc41hdr from tblbc41hdr where [noprosesdata] = @noprosesdata)
				--delete from [dbo].[tblbc41hdr] where [noprosesdata] = @noprosesdata
				update tbl_bahanpenolong_memo set status_data = 'PROSESEXIM',typedok = NULL, ndpbm =NULL,id_bc_out_hdr=NULL 
				where id_bc_out_hdr = @id_bc_out_hdr  and typedok = 'BC41' 
				update tbl_alatkantor_memo set status_data = 'PROSESEXIM',typedok = NULL, ndpbm =NULL,id_bc_out_hdr=NULL 
				where id_bc_out_hdr = @id_bc_out_hdr  and typedok = 'BC41' 
				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 DATA DI PROSES ULANG 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_bc41_send_ceisa(){
		$this->mysql =$this->load->database('mysql', TRUE);	
		
		$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"}');
		$q = $this->db->query("select car, nodok as dokno, convert(char(10),tgdok,126) as doktg from tbl_bc_out_dok where id_bc_out_hdr = $id_bc_out_hdr and kddok='380'  ");
		
		$no_inv = "";
		$pre = "";
		
		foreach($q->result() as $r){
			$pre = "<br />";
			$no_inv .= $r->dokno . " # " . $r->doktg . $pre;
		}
		$carr = "";
		$q_car = $this->db->query("
			select car from tbl_bc_out_hdr where id_bc_out_hdr = $id_bc_out_hdr
		");
		foreach ($q_car->result() as $rcar){
			$carr = $rcar->car;
		}
		
		$q_last_car = $this->mysql->query("
			select NOMOR_AJU from tpb_header where KODE_DOKUMEN_PABEAN = '41'  and SUBSTR(NOMOR_AJU, 21, 6) < 5000 order by NOMOR_AJU desc limit 1
		");
		$last_car = "";
		foreach($q_last_car->result() as $r){
			$last_car = $r->NOMOR_AJU;
		}
		
		$this->db->query("
			SET ANSI_NULLS ON
			SET ANSI_WARNINGS ON
		");
		
		$next_car="";
		$q_car = $this->db->query("
			select dbo.FN_CEISA_GET_CAR_AUTO(41,50000) as a
		");
		foreach ($q_car->result() as $rcar){
			$next_car = $rcar->a;
		}
		
		$data['id_bc_out_hdr'] = $id_bc_out_hdr;
		$data['no_inv'] = $no_inv;
		$data['last_car'] = $last_car;
		$data['next_car'] = $next_car;
		$data['car'] = $carr;
		$this->load->view('exim/view_bc41_send_ceisa', $data);
		
	}
	
	function proses_bc41_ceisa(){
		$this->mysql =$this->load->database('mysql', TRUE);	
		$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"}');
		$this->db->query("
			SET ANSI_NULLS ON
			SET ANSI_WARNINGS ON
		");
		
		$next_car="";
		$q_car = $this->db->query("
			select dbo.FN_CEISA_GET_CAR_AUTO(41,50000) as a
		");
		foreach ($q_car->result() as $rcar){
			$next_car = $rcar->a;
		}
		$ID_MODUL = (int)substr($next_car, 6,6);
		$my_ref_pengusaha = $this->mysql->query("
			SELECT
			referensi_pengusaha.ID,
			referensi_pengusaha.ALAMAT,
			referensi_pengusaha.CONTACT_PERSON,
			referensi_pengusaha.EMAIL,
			referensi_pengusaha.FAX,
			referensi_pengusaha.ID_PENGENAL,
			referensi_pengusaha.JENISTPB,
			referensi_pengusaha.KODE_ID,
			referensi_pengusaha.KODE_KANTOR,
			referensi_pengusaha.NAMA,
			referensi_pengusaha.NOMOR_PENGENAL,
			referensi_pengusaha.NOMOR_SKEP,
			referensi_pengusaha.NPWP,
			referensi_pengusaha.STATUS_IMPORTIR,
			referensi_pengusaha.TANGGAL_SKEP,
			referensi_pengusaha.TELEPON
			FROM
			referensi_pengusaha limit 1
			
		");
		$ALAMAT = "";
		$KODE_ID = "";
		$NAMA = "";
		$NOMOR_SKEP = "";
		$TANGGAL_SKEP = "";
		$NPWP = "";
		$ID = "";
		$ID_PENGENAL = "";
		$NOMOR_PENGENAL = "";
		
		foreach ($my_ref_pengusaha->result() as $r){
			$ALAMAT = $r->ALAMAT;
			$KODE_ID = $r->KODE_ID;
			$NAMA = $r->NAMA;
			$NOMOR_SKEP = $r->NOMOR_SKEP;
			$TANGGAL_SKEP = $r->TANGGAL_SKEP;
			$NPWP = $r->NPWP;
			$ID = $r->ID;
			$ID_PENGENAL = $r->ID_PENGENAL;
			$NOMOR_PENGENAL = $r->NOMOR_PENGENAL;
		}
		
		$q_hdr=$this->db->query("
			select
			  dbo.tbl_bc_out_hdr.CAR,
				dbo.tbl_bc_out_hdr.JnEks,
				dbo.tbl_bc_out_hdr.KatEks,
				dbo.tbl_bc_out_hdr.JNDAG,
				dbo.tbl_bc_out_hdr.JNBYR,
				dbo.tbl_bc_out_hdr.IDEKS,
				dbo.tbl_bc_out_hdr.NPWPEKS,
				dbo.tbl_bc_out_hdr.NamaEks,
				dbo.tbl_bc_out_hdr.AlmtEks,
				dbo.tbl_bc_out_hdr.NoSiup,
				dbo.tbl_bc_out_hdr.TgSiup,
				dbo.tbl_bc_out_hdr.JnPerus,
				dbo.tbl_bc_out_hdr.NoTdp,
				dbo.tbl_bc_out_hdr.TgTdp,
				dbo.tbl_bc_out_hdr.Niper,
				dbo.tbl_bc_out_hdr.StatusH,
				dbo.tbl_bc_out_hdr.NAMABELI,
				dbo.tbl_bc_out_hdr.ALMTBELI,
				dbo.tbl_bc_out_hdr.NEGBELI,
				dbo.tbl_bc_out_hdr.MODA,
				dbo.tbl_bc_out_hdr.PROPBRG,
				dbo.tbl_bc_out_hdr.NEGTUJU,
				dbo.tbl_bc_out_hdr.DELIVERY,
				dbo.tbl_bc_out_hdr.KDVAL,
				dbo.tbl_bc_out_hdr.KdHrg,
				dbo.tbl_bc_out_hdr.NilInv,
				dbo.tbl_bc_out_hdr.FOB,
				dbo.tbl_bc_out_hdr.JMCONT,
				dbo.tbl_bc_out_hdr.BRUTO,
				dbo.tbl_bc_out_hdr.NETTO,
				dbo.tbl_bc_out_hdr.JMBRG,
				dbo.tbl_bc_out_hdr.KDKTR,
				dbo.tbl_bc_out_hdr.KTRGATE,
				dbo.tbl_bc_out_hdr.JNTPB,
				dbo.tbl_bc_out_hdr.KDLOKBRG,
				dbo.tbl_bc_out_hdr.NpwpQq,
				dbo.tbl_bc_out_hdr.NILKURS
			  from 
			  [tbl_bc_out_hdr]
			  where  id_bc_out_hdr = $id_bc_out_hdr
		");
		if($q_hdr->num_rows == 0){
			echo json_encode(array('sts' => '06 ', 'desc' => "DATA TIDAK DITEMUKAN  $next_car ", 'insert_id' => 0 ));
			die();
		} 
		$NPWP_PEMBELI = "";
		foreach($q_hdr->result() as $r){
			$NPWP_PEMBELI = $r->NpwpQq;
		}
		if($NPWP_PEMBELI == ''){
			echo json_encode(array('sts' => '06 ', 'desc' => "DATA NPWP TIDAK DITEMUKAN  $NPWP_PEMBELI ", 'insert_id' => 0 ));
			die();
		}
		
		$my_ref_penerima = $this->mysql->query("
			SELECT
			referensi_pengusaha.ID,
			referensi_pengusaha.ALAMAT,
			referensi_pengusaha.CONTACT_PERSON,
			referensi_pengusaha.EMAIL,
			referensi_pengusaha.FAX,
			referensi_pengusaha.ID_PENGENAL,
			referensi_pengusaha.JENISTPB,
			referensi_pengusaha.KODE_ID,
			referensi_pengusaha.KODE_KANTOR,
			referensi_pengusaha.NAMA,
			referensi_pengusaha.NOMOR_PENGENAL,
			referensi_pengusaha.NOMOR_SKEP,
			referensi_pengusaha.NPWP,
			referensi_pengusaha.STATUS_IMPORTIR,
			referensi_pengusaha.TANGGAL_SKEP,
			referensi_pengusaha.TELEPON
			FROM
			referensi_pengusaha 
			where replace(replace(referensi_pengusaha.NPWP,'.',''),'-','')  = '$NPWP_PEMBELI'
			limit 1
			
		");

		$ALAMAT_PENERIMA = "";
		$KODE_ID_PENERIMA = "";
		$NAMA_PENERIMA = "";
		$NOMOR_SKEP_PENERIMA = "";
		$TANGGAL_SKEP_PENERIMA = "";
		$NPWP_PENERIMA = "";
		$ID_PENERIMA = "";
		$ID_PENGENAL_PENERIMA = "";
		$NOMOR_PENGENAL_PENERIMA = "";
		
		foreach ($my_ref_penerima->result() as $r){
			$ALAMAT_PENERIMA = $r->ALAMAT;
			$KODE_ID_PENERIMA = $r->KODE_ID;
			$NAMA_PENERIMA = $r->NAMA;
			$NOMOR_SKEP_PENERIMA = $r->NOMOR_SKEP;
			$TANGGAL_SKEP_PENERIMA = $r->TANGGAL_SKEP;
			$NPWP_PENERIMA = $r->NPWP;
			$ID_PENERIMA = $r->ID;
			$ID_PENGENAL_PENERIMA = $r->ID_PENGENAL;
			$NOMOR_PENGENAL_PENERIMA = $r->NOMOR_PENGENAL;
		}
		
		
		if($NPWP_PENERIMA == ''){
			echo json_encode(array('sts' => '06 ', 'desc' => "DATA NPWP TIDAK DITEMUKAN  $NPWP_PEMBELI ", 'insert_id' => 0 ));
			die();
		} 
		
		
		$qx = $this->db->query("
			select tipe_out from tbl_bahanpenolong_memo where id_bc_out_hdr = $id_bc_out_hdr
		");
		$KODE_TUJUAN_PENGIRIMAN = "1";
		foreach ($qx->result() as $rx){
			if($rx->tipe_out == 'REPAIR'){
				$KODE_TUJUAN_PENGIRIMAN = "4";
			} 
		} 
		
		$Namattd = "";
		$q_ttd = $this->db->query("
			select top 1 Namattd from tblImportir 
		");
		foreach ($q_ttd->result() as $r_ttd){
			$Namattd = $r_ttd->Namattd;
		}
		$NILKURS = 1;
		$hdr_array = array();
		foreach($q_hdr->result() as $r){
			$NILKURS = $r->NILKURS;
			$hdr_array[] = array(
				"ALAMAT_PENERIMA_BARANG" => $NAMA_PENERIMA,
				"ALAMAT_PENGUSAHA" => $ALAMAT,
				"API_PENGUSAHA" => $NOMOR_PENGENAL,
				"ASAL_DATA" => "I",
				"BRUTO" => $r->BRUTO,
				"CIF" => $r->NilInv,
				"HARGA_PENYERAHAN" => ((float)$r->NilInv * (float)$r->NILKURS),
				"ID_MODUL" => $ID_MODUL,
				"ID_PENERIMA_BARANG" => $NPWP_PENERIMA,
				"ID_PENGUSAHA" => $NPWP,
				"JABATAN_TTD" =>  "KUASA DIREKSI",
				"JUMLAH_BARANG" => $r->JMBRG,
				"JUMLAH_KEMASAN" => "1",
				"KODE_DOKUMEN_PABEAN" => "41",
				"KODE_ID_PENERIMA_BARANG" => "1",
				"KODE_ID_PENGUSAHA" => "1",
				"KODE_JENIS_TPB" => "1",
				"KODE_KANTOR" => "150300", 
				"KODE_KANTOR_TUJUAN" => "150300",
				"KODE_STATUS" => "00",
				"KODE_TUJUAN_PENGIRIMAN" => $KODE_TUJUAN_PENGIRIMAN,
				"KODE_TUJUAN_TPB" => "1",
				"KODE_VALUTA" => $r->KDVAL,
				"KOTA_TTD" => "TANGERANG",
				"NAMA_PENERIMA_BARANG" => $NAMA_PENERIMA,
				"NAMA_PENGANGKUT" => "TRUCK",
				"NAMA_PENGUSAHA" => $NAMA,
				"NAMA_TTD" => $Namattd,
				"NDPBM" => $r->NILKURS,
				"NETTO" => $r->NETTO,
				"NOMOR_AJU" => $next_car,
				"NOMOR_IJIN_TPB" => $NOMOR_SKEP,
				"NOMOR_IJIN_TPB_PENERIMA" => $NOMOR_SKEP_PENERIMA,
				"TANGGAL_AJU" => date("Y-m-d h:i:s"),
				"TANGGAL_TTD" => date("Y-m-d"),
				"VERSI_MODUL" => "3.1.7"
				
				
			);
		}	
		
		
		
		$q_dtl=$this->db->query("
			SELECT
				dbo.tbl_bc_out_dtl.SERIBRG,
				dbo.tbl_bc_out_dtl.HS,
				dbo.tbl_bc_out_dtl.URBRG1 + dbo.tbl_bc_out_dtl.URBRG2 + dbo.tbl_bc_out_dtl.URBRG3 + dbo.tbl_bc_out_dtl.URBRG4 as URAIAN,
				dbo.tbl_bc_out_dtl.DMERK,
				dbo.tbl_bc_out_dtl.[SIZE],
				dbo.tbl_bc_out_dtl.TYPE,
				dbo.tbl_bc_out_dtl.KDBRG,
				dbo.tbl_bc_out_dtl.JMKOLI,
				dbo.tbl_bc_out_dtl.JNKOLI,
				dbo.tbl_bc_out_dtl.DNilInv,
				dbo.tbl_bc_out_dtl.JMSATUAN,
				dbo.tbl_bc_out_dtl.JNSATUAN,
				dbo.tbl_bc_out_dtl.NETDET,
				dbo.tbl_bc_out_dtl.FOBPERBRG

				FROM
				dbo.tbl_bc_out_dtl
				where id_bc_out_hdr = $id_bc_out_hdr
		");
		
		$tpb_barang = array();
		foreach($q_dtl->result() as $r){
			$HARGA_PENYERAHAN = (float) $NILKURS * (float) $r->DNilInv;
			$tpb_barang[] = array(
				"CIF" => $r->DNilInv,
				"HARGA_PENYERAHAN" => $HARGA_PENYERAHAN,
				"JUMLAH_BAHAN_BAKU" => 0,
				"JUMLAH_SATUAN" => $r->JMSATUAN,
				"KODE_BARANG" => $r->KDBRG,
				"KODE_SATUAN" => $r->JNSATUAN,
				"KODE_STATUS" => "00",
				"MERK" => $r->DMERK,
				"NETTO" => $r->NETDET,
				"POS_TARIF" =>  $r->HS,
				"SERI_BARANG" => $r->SERIBRG,
				"TIPE" =>  $r->KDBRG,
				"URAIAN" => $r->URAIAN,
				
			);
		}	
		
		
		$q_dok=$this->db->query("
			select
			 [KdDok]
		 	 ,[NoDok]
		 	 ,CONVERT(varchar, [TgDok], 126) as TgDok
			  from 
			  tbl_bc_out_dok
			  where  id_bc_out_hdr = $id_bc_out_hdr 
		");
		
		
		$tpb_dokumen = array();
		$i = 1;
		foreach($q_dok->result() as $r){
			$tpb_dokumen[] = array(
				"KODE_JENIS_DOKUMEN" => $r->KdDok,
				"NOMOR_DOKUMEN" => $r->NoDok,
				"TANGGAL_DOKUMEN" => $r->TgDok,
				"TIPE_DOKUMEN" => "02",
				"SERI_DOKUMEN" => $i
				
			);
			$i++;
		}	
		
		$this->mysql->trans_start();
		$msg = $this->mysql->insert('tpb_header', $hdr_array[0]);
		$insert_id = $this->mysql->insert_id();
		$this->mysql->trans_complete();
		
		for ($x = 0; $x <= count($tpb_barang) - 1; $x++ ){
			$this->mysql->trans_start();
			$this->mysql->insert("tpb_barang", array_merge($tpb_barang[$x], array("ID_HEADER" => $insert_id)));
			$insert_id_dtl = $this->mysql->insert_id();
			$this->mysql->trans_complete();
			
		}
		
		for ($x = 0; $x <= count($tpb_dokumen) - 1; $x++ ){
			$this->mysql->insert("tpb_dokumen", array_merge($tpb_dokumen[$x], array("ID_HEADER" => $insert_id)));
		}
		
		if( !$msg )
		{
		   $errNo   = $this->mysql->_error_number();
		   $errMess = $this->mysql->_error_message();
		   echo json_encode(array('sts' => $errNo, 'desc' => $errMess));
		   die();		
		}
		$stsa = '00';
		$msga = $msg . ' DATA CREATED..';
		if ($stsa != 1){
			$status = '06';
			$msg = $stsa;
		}
		
		$q=$this->db->query("
			update tbl_bc_out_hdr set CAR='$next_car',  statusdok = 'DATA PROCESSED' where id_bc_out_hdr = $id_bc_out_hdr
		");
		
		echo json_encode(array('sts' => $stsa, 'desc' => $msga, 'insert_id' => $insert_id ));	
	}
	
	function proses_get_replace_bc41_ceisa(){
		$car = (isset($_POST['carproses_matchbc41_ceisa']) && !empty($_POST['carproses_matchbc41_ceisa']))?$_POST['carproses_matchbc41_ceisa']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$id_ceisa = (isset($_POST['id_ceisa']) && !empty($_POST['id_ceisa']))?$_POST['id_ceisa']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$id_bc_out_hdr = (isset($_POST['id_bc_out_hdr']) && !empty($_POST['id_bc_out_hdr']))?$_POST['id_bc_out_hdr']:'';
		$username=$this->session->userdata('DX_username');
		
		
		
		$this->db->query("
			SET ANSI_NULLS ON
			SET ANSI_WARNINGS ON
		");
		if ($id_bc_out_hdr != ''){
			$matchcar = $this->db->query("
				declare @car varchar(50)
				declare @NOMOR_DAFTAR varchar(50)
				declare @TANGGAL_DAFTAR date
				
				set @car = (select car from tbl_bc_out_hdr where id_bc_out_hdr = $id_bc_out_hdr) 
				
				BEGIN TRY
				if	(select 
					count(*)	
					from openquery(TPBDB,'select * from tpb_header where KODE_DOKUMEN_PABEAN =''41'' ')
					where NOMOR_AJU = @car and ) <> 1
				begin
					SELECT 
					'06' AS status
					,' CAR TIDAK DITEMUKAN ATAU LEBIH DARI SATU NOMOR AJU SAMA, GUNAKAN TARIK MANUAL' AS description
					return
				end	
				
				select 
					@NOMOR_DAFTAR=NOMOR_DAFTAR,
					@TANGGAL_DAFTAR=TANGGAL_DAFTAR	
				from openquery(TPBDB,'select * from tpb_header where KODE_DOKUMEN_PABEAN ='41' ')
				where NOMOR_AJU = @car
				
				update tbl_bc_out_hdr set NODAFT=@NOMOR_DAFTAR, TGDAFT=@TANGGAL_DAFTAR where id_bc_out_hdr = $id_bc_out_hdr  
				
				SELECT 
					'00' AS status
					,' NO DAFTAR : ' + @NOMOR_DAFTAR AS description;
				END TRY
				BEGIN CATCH 
					SELECT 
					'06' AS status
					,ERROR_MESSAGE() AS description;
					
				END CATCH;
				
			");
		} else {
			
			$q_con = $this->db->query("
				select count(*)  as cnt
				from openquery(TPBDB,'select * from tpb_kontainer where id_header=$id_ceisa ')
				where id_header=$id_ceisa
			");
			
			$JMCON = 0;
			foreach ($q_con->result() as $r_con){
				$JMCON =  $r_con->cnt;
			}
			
				
			$matchcar = $this->db->query("
				declare @car varchar(50)
				declare @carceisa varchar(50)
				declare @NOMOR_DAFTAR varchar(50)
				declare @TANGGAL_DAFTAR date
				declare @id_ceisa bigint
				set @id_ceisa = $id_ceisa
				
				set @carceisa = (select NOMOR_AJU from openquery(TPBDB,'select * from tpb_header where id = $id_ceisa ') where ID = @id_ceisa) 
				
				BEGIN TRY
					insert into [tbl_bc_out_hdr] (
						CAR
						,JnEks
						,KatEks
						,IDEKS
						,NPWPEKS
						,NamaEks
						,AlmtEks
						,NpwpQq
						,NAMABELI
						,ALMTBELI
						,CARRIER
						,MODA
						,KDVAL
						,NilInv
						,FOB
						,BRUTO
						,NETTO
						,JMBRG
						,NODAFT
						,TGDAFT
						,KDKTR
						,KTRGATE
						,JNTPB
						,TTDPEB
						,STATUS
						,NILKURS
						,NEGBELI
						,typedok
						,statusdok
						,bc_in_from_in
						,JMCONT 
						,[create_date]
					      ,[create_by]
					      ,[update_date]
					      ,[update_by]

					) select
					 NOMOR_AJU			
					,KODE_JENIS_TPB
					,'41'
					,KODE_ID_PENGUSAHA
					,ID_PENGUSAHA
					,NAMA_PENGUSAHA
					,SUBSTRING(ALAMAT_PENGUSAHA, 1, 70)
					,ID_PENERIMA_BARANG
					,NAMA_PENERIMA_BARANG
					,SUBSTRING(ALAMAT_PENERIMA_BARANG, 1, 70)
					,NAMA_PENGANGKUT
					,'1'	
					,KODE_VALUTA
					,HARGA_PENYERAHAN
					,HARGA_PENYERAHAN
					,BRUTO
					,NETTO
					,JUMLAH_BARANG
					,NOMOR_DAFTAR
					,TANGGAL_DAFTAR
					,KODE_KANTOR
					,KODE_KANTOR
					,'KB'
					,NAMA_TTD
					,KODE_STATUS
					,NDPBM
					,'ID'
					,'BC41'
					,'DATA PROCESSED'
					,'CEISA_BC41'
					,$JMCON
					,GETDATE()
					,'$username'
					,GETDATE()
					,'$username'
					 from openquery(TPBDB,'select * from tpb_header  where id = $id_ceisa ') where ID = @id_ceisa
				declare @id_hdrx bigint
				set @id_hdrx = @@IDENTITY 
				
				insert into tbl_bc_out_dtl (
					id_bc_out_hdr
					,CAR
					,[SERIBRG]
					,URBRG1
					,URBRG2
					,URBRG3
					,URBRG4
					,DMERK
					,SIZE
					,TYPE
					,KDBRG
					,JMKOLI
					,JNKOLI
					,DNilInv
					,FOBPERBRG
					,JMSATUAN
					,JNSATUAN
					,NETDET
					,DVolume
					,NegAsal
					,HS
				) select
				@id_hdrx
				,@carceisa
				,SERI_BARANG
				,SUBSTRING(URAIAN,1,40)
				,SUBSTRING(URAIAN,41,80)
				,SUBSTRING(URAIAN,81,120)
				,SUBSTRING(URAIAN,121,160)
				,MERK
				,UKURAN
				,TIPE
				,KODE_BARANG
				,JUMLAH_KEMASAN
				,KODE_KEMASAN
				,HARGA_PENYERAHAN
				,HARGA_SATUAN
				,JUMLAH_SATUAN
				,KODE_SATUAN
				,NETTO
				,VOLUME
				,KODE_NEGARA_ASAL
				,POS_TARIF
				 from openquery(TPBDB,'select * from tpb_barang  where ID_HEADER = $id_ceisa ') where ID_HEADER = @id_ceisa
				
				insert into tbl_bc_out_dok (
					id_bc_out_hdr
					,CAR
					,[KdDok]
			      ,[NoDok]
			      ,[TgDok]
					
				) select
				@id_hdrx
				,@carceisa
				,SUBSTRING(KODE_JENIS_DOKUMEN, 1, 3)
				,NOMOR_DOKUMEN
				,CONVERT(varchar(10), TANGGAL_DOKUMEN, 126)
				 from openquery(TPBDB,'select * from tpb_dokumen where ID_HEADER= $id_ceisa ') where ID_HEADER = @id_ceisa
				
				insert into tbl_bc_out_con (
					id_bc_out_hdr
					,CAR
					 ,[NoCont]
				      ,[Size]
				      ,[Type]
				      
					
				) select
				@id_hdrx
				,@carceisa
				,NOMOR_KONTAINER
				,KODE_UKURAN_KONTAINER
				,KODE_TIPE_KONTAINER
				 from openquery(TPBDB,'select * from tpb_kontainer where ID_HEADER= $id_ceisa ') where ID_HEADER = @id_ceisa
				
				insert into tbl_bc_out_kms (
					id_bc_out_hdr
					,CAR
					,[JnKemas]
     				 ,[JmKemas]
					
				) select
				@id_hdrx
				,@carceisa
				,KODE_JENIS_KEMASAN
				,JUMLAH_KEMASAN
				 from openquery(TPBDB,'select * from tpb_kemasan where ID_HEADER= $id_ceisa ') where ID_HEADER = @id_ceisa
					
				
				insert into [tbl_bc_out_bahan_baku] (
					[id_bc_out_hdr]
					,[CAR]
				    ,[ID]
			      ,[CIF]
			      ,[CIF_RUPIAH]
			      ,[HARGA_PENYERAHAN]
			      ,[HARGA_PEROLEHAN]
			      ,[JENIS_SATUAN]
			      ,[JUMLAH_SATUAN]
			      ,[KODE_ASAL_BAHAN_BAKU]
			      ,[KODE_BARANG]
			      ,[KODE_FASILITAS_DOKUMEN]
			      ,[KODE_JENIS_DOK_ASAL]
			      ,[KODE_KANTOR]
			      ,[KODE_SKEMA_TARIF]
			      ,[KODE_STATUS]
			      ,[MERK]
			      ,[NDPBM]
			      ,[NETTO]
			      ,[NOMOR_AJU_DOK_ASAL]
			      ,[NOMOR_DAFTAR_DOK_ASAL]
			      ,[POS_TARIF]
			      ,[SERI_BAHAN_BAKU]
			      ,[SERI_BARANG]
			      ,[SERI_BARANG_DOK_ASAL]
			      ,[SPESIFIKASI_LAIN]
			      ,[TANGGAL_DAFTAR_DOK_ASAL]
			      ,[TIPE]
			      ,[UKURAN]
			      ,[URAIAN]
			      ,[ID_BARANG]
			      ,[ID_HEADER]
				     
				) select
				@id_hdrx
				,@carceisa,
				ID,
				CIF,
				CIF_RUPIAH,
				HARGA_PENYERAHAN,
				HARGA_PEROLEHAN,
				JENIS_SATUAN,
				JUMLAH_SATUAN,
				KODE_ASAL_BAHAN_BAKU,
				KODE_BARANG,
				KODE_FASILITAS_DOKUMEN,
				KODE_JENIS_DOK_ASAL,
				KODE_KANTOR,
				KODE_SKEMA_TARIF,
				KODE_STATUS,
				MERK,
				NDPBM,
				NETTO,
				NOMOR_AJU_DOK_ASAL,
				NOMOR_DAFTAR_DOK_ASAL,
				POS_TARIF,
				SERI_BAHAN_BAKU,
				SERI_BARANG,
				SERI_BARANG_DOK_ASAL,
				SPESIFIKASI_LAIN,
				TANGGAL_DAFTAR_DOK_ASAL,
				TIPE,
				UKURAN,
				URAIAN,
				ID_BARANG,
				ID_HEADER

				 from openquery(TPBDB,'select * from tpb_bahan_baku where  ID_HEADER= $id_ceisa ') where ID_HEADER = @id_ceisa
					
				
				insert into [tbl_bc_out_bahan_baku_tarif] (
					[id_bc_out_hdr]
					,[CAR],
				  ID,
					JENIS_TARIF,
					JUMLAH_SATUAN,
					KODE_ASAL_BAHAN_BAKU,
					KODE_FASILITAS,
					KODE_KOMODITI_CUKAI,
					KODE_SATUAN,
					KODE_TARIF,
					NILAI_BAYAR,
					NILAI_FASILITAS,
					NILAI_SUDAH_DILUNASI,
					SERI_BAHAN_BAKU,
					SERI_BARANG,
					TARIF,
					TARIF_FASILITAS,
					ID_BAHAN_BAKU,
					ID_BARANG,
					ID_HEADER
				     
				) select
				@id_hdrx
				,@carceisa
				,ID,
				JENIS_TARIF,
				JUMLAH_SATUAN,
				KODE_ASAL_BAHAN_BAKU,
				KODE_FASILITAS,
				KODE_KOMODITI_CUKAI,
				KODE_SATUAN,
				KODE_TARIF,
				NILAI_BAYAR,
				NILAI_FASILITAS,
				NILAI_SUDAH_DILUNASI,
				SERI_BAHAN_BAKU,
				SERI_BARANG,
				TARIF,
				TARIF_FASILITAS,
				ID_BAHAN_BAKU,
				ID_BARANG,
				ID_HEADER

				 from openquery(TPBDB,'select * from tpb_bahan_baku_tarif  where  ID_HEADER= $id_ceisa ') where ID_HEADER = @id_ceisa
					
				
					
					
				END TRY
				BEGIN CATCH 
					SELECT 
					'06' AS status
					,ERROR_MESSAGE() AS description;
					
				END CATCH;
				
				SELECT 
					'00' AS status
					,' DATA PROCESSED' AS description;
				
			");
		}	
		$rs = $matchcar->result_array();
        //$r = $matchcar->result();		
		//var_dump($rs);die();
		foreach ($rs as $row){
			$responce = array("sts" => $row['status'], "desc" => $row['description']);
		}  
		echo json_encode($responce);
	}
	
  
 }