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

	function index(){

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

	
	function loaddata(){
		$search = isset($_POST['_search'])?$_POST['_search']:false;
        $searchField = isset($_POST['searchField'])?$_POST['searchField']:'';
        $searchString = isset($_POST['searchString'])?$_POST['searchString']:'';
        $filters =  isset($_POST['filters'])?$_POST['filters']:'';
		
		$page = isset($_POST['page'])?$_POST['page']:1; // get the requested page
        $limit = isset($_POST['rows'])?$_POST['rows']:20; // get how many rows we want to have into the grid
        $sidx = isset($_POST['sidx'])?$_POST['sidx']:'id'; // get index row - i.e. user click to sort
        $sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		
		$where = " 1=1 and typedok='bc40' ";
				
		if ($search == 'true'){
			if (!empty($filters)){
				$array = json_decode($filters);
				foreach($array->rules as $arr=>$k){
					
					if ( $k->field == "nobl" ) {
						$searchString = $k->data;
						$where .= " and nobl like  '%".$searchString."%'"; 
					} else if ( $k->field == "tglbl" ) {
						$searchString = $k->data;
						$where .= " and tglbl =  '$searchString'"; 
					} else if ( $k->field == "CAR" ) {
						$searchString = $k->data;
						$where .= " and CAR like  '%".$searchString."%'"; 
					} else if ( $k->field == "KdKpbc" ) {
						$searchString = $k->data;
						$where .= " and KdKpbc like  '%".$searchString."%'"; 
					} else if ( $k->field == "PasokNama" ) {
						$searchString = $k->data;
						$where .= " and PasokNama like  '%".$searchString."%'"; 
					} else if ( $k->field == "BC23No" ) {
						$searchString = $k->data;
						$where .= " and BC23No like  '%".$searchString."%'"; 
					} else if ( $k->field == "BC23Tg" ) {
						$searchString = $k->data;
						$where .= " and BC23Tg like  '%".$searchString."%'"; 
					} else if ( $k->field == "JmBrg" ) {
						$searchString = $k->data;
						$where .= " and JmBrg = $searchString"; 
					} else if ( $k->field == "Status" ) {
						$searchString = $k->data;
						$where .= " and Status like  '%".$searchString."%'"; 
					}  else if ( $k->field == "statusdok" ) {
						$searchString = $k->data;
						$where .= " and statusdok like  '%".$searchString."%'"; 
					} 
				}
			}
		}
		
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_bc23->bc23hdrdataAll($limit,$start,$sidx,$sord, $where);
        $count = $this->mdl_bc23->bc23hdrdataCountAll($where);
	//	var_dump($records); die();
		
		if( $count > 0 ) { $total_pages = ceil($count/$limit); } else { $total_pages = 0; } 
        if ($page > $total_pages) $page=$total_pages;
        
        $responce->page = $page;
		$responce->total = $total_pages;
		$responce->records =  $count;
		$i=0;
        foreach($records as $r){
			if($r->Status == null){
				$status = '-';
				
			} else { $status=$r->Status;  }
			
			$responce->rows[$i]['id_bc_in_hdr']=$r->id_bc_in_hdr; 
			$responce->rows[$i]['CAR']=$r->CAR; 
			$responce->rows[$i]['KdKpbc']=$r->KdKpbc; 
			$responce->rows[$i]['Tujuan']=$r->Tujuan; 
			$responce->rows[$i]['JnsBarang']=$r->JnsBarang; 
			$responce->rows[$i]['TujuanKirim']=$r->TujuanKirim; 
			$responce->rows[$i]['PasokNama']=$r->PasokNama; 
			$responce->rows[$i]['PasokAlmt']=$r->PasokAlmt; 
			$responce->rows[$i]['PasokNeg']=$r->PasokNeg; 
			$responce->rows[$i]['UsahaId']=$r->UsahaId; 
			$responce->rows[$i]['UsahaNpwp']=$r->UsahaNpwp; 
			$responce->rows[$i]['KdTPB']=$r->KdTPB; 
			$responce->rows[$i]['Registrasi']=$r->Registrasi; 
			$responce->rows[$i]['UsahaNama']=$r->UsahaNama; 
			$responce->rows[$i]['UsahaAlmt']=$r->UsahaAlmt; 
			$responce->rows[$i]['UsahaStatus']=$r->UsahaStatus; 
			$responce->rows[$i]['ApiKd']=$r->ApiKd; 
			$responce->rows[$i]['ApiNo']=$r->ApiNo; 
			$responce->rows[$i]['PpjkId']=$r->PpjkId; 
			$responce->rows[$i]['PpjkNpwp']=$r->PpjkNpwp; 
			$responce->rows[$i]['PpjkNama']=$r->PpjkNama; 
			$responce->rows[$i]['PpjkAlmt']=$r->PpjkAlmt; 
			$responce->rows[$i]['PpjkNo']=$r->PpjkNo; 
			$responce->rows[$i]['PpjkTg']=$r->PpjkTg; 
			$responce->rows[$i]['IndId']=$r->IndId; 
			$responce->rows[$i]['IndNpwp']=$r->IndNpwp; 
			$responce->rows[$i]['IndNama']=$r->IndNama; 
			$responce->rows[$i]['IndAlmt']=$r->IndAlmt; 
			$responce->rows[$i]['Moda']=$r->Moda; 
			$responce->rows[$i]['AngkutNama']=$r->AngkutNama; 
			$responce->rows[$i]['AngkutNo']=$r->AngkutNo; 
			$responce->rows[$i]['AngkutFl']=$r->AngkutFl; 
			$responce->rows[$i]['PelBkr']=$r->PelBkr; 
			$responce->rows[$i]['PelMuat']=$r->PelMuat; 
			$responce->rows[$i]['PelTransit']=$r->PelTransit; 
			$responce->rows[$i]['BC23No']=$r->BC23No; 
			$responce->rows[$i]['BC23Tg']=$r->BC23Tg; 
			$responce->rows[$i]['KdKpbcBongkar']=$r->KdKpbcBongkar; 
			$responce->rows[$i]['KdKpbcAwas']=$r->KdKpbcAwas; 
			$responce->rows[$i]['DokTupKd']=$r->DokTupKd; 
			$responce->rows[$i]['DokTupNo']=$r->DokTupNo; 
			$responce->rows[$i]['DokTupTg']=$r->DokTupTg; 
			$responce->rows[$i]['PosNo']=$r->PosNo; 
			$responce->rows[$i]['PosSub']=$r->PosSub; 
			$responce->rows[$i]['TmpTbn']=$r->TmpTbn; 
			$responce->rows[$i]['KdVal']=$r->KdVal; 
			$responce->rows[$i]['Ndpbm']=$r->Ndpbm; 
			$responce->rows[$i]['NilInv']=$r->NilInv; 
			$responce->rows[$i]['Freight']=$r->Freight; 
			$responce->rows[$i]['BTambahan']=$r->BTambahan; 
			$responce->rows[$i]['Diskon']=$r->Diskon; 
			$responce->rows[$i]['KdAss']=$r->KdAss; 
			$responce->rows[$i]['Asuransi']=$r->Asuransi; 
			$responce->rows[$i]['KdHrg']=$r->KdHrg; 
			$responce->rows[$i]['Fob']=$r->Fob; 
			$responce->rows[$i]['Cif']=$r->Cif; 
			$responce->rows[$i]['Bruto']=$r->Bruto; 
			$responce->rows[$i]['Netto']=$r->Netto; 
			$responce->rows[$i]['JmCont']=$r->JmCont; 
			$responce->rows[$i]['JmBrg']=$r->JmBrg; 
			$responce->rows[$i]['TglTtd']=$r->TglTtd; 
			$responce->rows[$i]['KotaTtd']=$r->KotaTtd; 
			$responce->rows[$i]['Namattd']=$r->Namattd; 
			$responce->rows[$i]['ContakPerson']=$r->ContakPerson; 
			$responce->rows[$i]['NoPhone']=$r->NoPhone; 
			$responce->rows[$i]['NoFax']=$r->NoFax; 
			$responce->rows[$i]['Email']=$r->Email; 
			$responce->rows[$i]['Status']=$status;//$r->Status; 
			$responce->rows[$i]['Snrf']=$r->Snrf; 
			$responce->rows[$i]['Lengkap']=$r->Lengkap; 
			$responce->rows[$i]['PosSubSub']=$r->PosSubSub; 
			$responce->rows[$i]['NoKontrak']=$r->NoKontrak; 
			$responce->rows[$i]['TgKontrak']=$r->TgKontrak; 
			$responce->rows[$i]['HargaRp']=$r->HargaRp; 
			$responce->rows[$i]['TmpPeriksaFisik']=$r->TmpPeriksaFisik; 
			$responce->rows[$i]['AlmtPeriksaFisik']=$r->AlmtPeriksaFisik; 
			$responce->rows[$i]['TgPeriksaFisik']=$r->TgPeriksaFisik; 
			$responce->rows[$i]['WkPeriksaFisik']=$r->WkPeriksaFisik; 
			$responce->rows[$i]['create_date']=$r->create_date; 
			$responce->rows[$i]['create_by']=$r->create_by; 
			$responce->rows[$i]['update_date']=$r->update_date; 
			$responce->rows[$i]['update_by']=$r->update_by; 
			$responce->rows[$i]['typedok']=$r->typedok; 
			$responce->rows[$i]['statusdok']=$r->statusdok; 
			$responce->rows[$i]['noinv']=$r->noinv; 
			$responce->rows[$i]['tglinv']=$r->tglinv;
			$responce->rows[$i]['inv_hdr_id']=$r->inv_hdr_id;
			$responce->rows[$i]['nobl']=$r->nobl;
			$responce->rows[$i]['tglbl']=$r->tglbl;
			
			$i++;
		}
		echo json_encode($responce);
		
	}
	
	function view_bc40_nonhib(){
		$this->load->view('exim/view_bc40_nonhib');
	}
	
	function loaddata_bc40_nonhib(){
		$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_nonhib_hdr'; // get index row - i.e. user click to sort
		$sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		
		$where = " status='new' and tbl_data_supplier.supplier_country='ID' ";
		
		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 == "vendor" ) {
						$searchString = $k->data;
						$where .= " and vendor 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_bc27->bc271datanonhibhdrAll($limit,$start,$sidx,$sord, $where);
        $count = $this->mdl_bc27->bc271datanonhibhdrcountAll($where);
		
		
		if( $count > 0 ) { $total_pages = ceil($count/$limit); } else { $total_pages = 0; } 
		if ($page > $total_pages) $page=$total_pages;
		
		$responce->page = $page;
		$responce->total = $total_pages;
		$responce->records =  $count;
		$i=0;
		foreach($records as $r){
			if ($r->status == 0){ $stat = "New"; } else { $stat = "Not New"; };
			if ($r->tgbl == '1900-10-23'){ $tgbl = ""; } else { $tgbl = $r->tgbl; };	
				
			$responce->rows[$i]['id_nonhib_hdr']=$r->id_nonhib_hdr; 
			$responce->rows[$i]['from_file_name']=$r->from_file_name; 
			$responce->rows[$i]['totalitemfile']=$r->totalitemfile; 
			$responce->rows[$i]['exec_date']=$r->exec_date;
			$responce->rows[$i]['status']= $r->status;
			$responce->rows[$i]['itemcount']=$r->itemcount;
			$responce->rows[$i]['noinv']=$r->noinv;
			$responce->rows[$i]['tglinv']=$r->tglinv;
			$responce->rows[$i]['tglinv2']=$r->tglinv2;
			$responce->rows[$i]['nobl']=trim($r->nobl);
			$responce->rows[$i]['tgbl']=$tgbl;
			$responce->rows[$i]['vendor']=$r->vendor;
			$i++;
		}
		echo json_encode($responce);
	}
	
	function view_bc40_nonhibaddnoajudata(){
		
		$this->load->view('exim/view_bc40_nonhibaddnoajudata');
		
	}
	function loaddata_groupinv_nonhib(){
		$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_nonhib_hdr'; // get index row - i.e. user click to sort
		$sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		
		//$where = " status='new' ";
		$nobl = (isset($_POST['nobl']) && !empty($_POST['nobl']))?$_POST['nobl']:'';
		$tgbl = (isset($_POST['tgbl']) && !empty($_POST['tgbl']))?$_POST['tgbl']:'';
		
		
		//$where = " a.status_inv=0 ";
		$where = " 1=1 ";
		$where .= " and nobl =  '".$nobl."'";
		$where .= " and tgbl =  '".$tgbl."'";
		
			
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
	    $records = $this->mdl_bc27->bc271datanonhibhdrAll($limit,$start,$sidx,$sord, $where);
        $count = $this->mdl_bc27->bc271datanonhibhdrcountAll($where);
		
		
		if( $count > 0 ) { $total_pages = ceil($count/$limit); } else { $total_pages = 0; } 
		if ($page > $total_pages) $page=$total_pages;
		
		$responce->page = $page;
		$responce->total = $total_pages;
		$responce->records =  $count;
		$i=0;
		foreach($records as $r){
			if ($r->status == 0){ $stat = "New"; } else { $stat = "Not New"; };
			if ($r->tgbl =='1900-10-23'){ $tgbl = ""; } else { $tgbl = $r->tgbl; };	

			 if ($r->status == 'new'){
					  $status = 'INVOICE NEW ADDED';
			} else if ($r->status == 'DATA PROCESSED'){
				 $status = 'DATA ALREADY PROCESSED';
			}	
			
			if ($r->status == 'new'){
					  $statusx = 0;
			} else {
				 $statusx = 1;
			}	
			
			$responce->rows[$i]['id_nonhib_hdr']=$r->id_nonhib_hdr; 
			$responce->rows[$i]['from_file_name']=$r->from_file_name; 
			$responce->rows[$i]['totalitemfile']=$r->totalitemfile; 
			$responce->rows[$i]['exec_date']=$r->exec_date;
			$responce->rows[$i]['status']= $status;//$r->status;
			$responce->rows[$i]['itemcount']=$r->itemcount;
			$responce->rows[$i]['noinv']=$r->noinv;
			$responce->rows[$i]['tglinv']=$r->tglinv;
			$responce->rows[$i]['tglinv2']=$r->tglinv2;
			$responce->rows[$i]['nobl']=$r->nobl;
			$responce->rows[$i]['tgbl']=$r->tgbl;
			$responce->rows[$i]['statusx']=$statusx;
			$i++;
		}
		echo json_encode($responce);
	
	}
	
	function prosesgroupinv_nonhib(){
		
		$datadiff = (isset($_POST['datadiff']) && !empty($_POST['datadiff']))?$_POST['datadiff']:die('{"sts":"ERROR","desc":" Data  Tidak Ditemukan"}'); 
		$datadiff=json_decode($datadiff);
		$datacsv = '';
		$databl = '';
		foreach ($datadiff as $data ){
		    $inv_hdr_id = (isset($data->id_nonhib_hdr) && !empty($data->id_nonhib_hdr ))?$data->id_nonhib_hdr :die('{"sts":"ERROR","desc":" Param inv_hdr_id Tidak Ditemukan"}');
			$invno = (isset($data->noinv) && !empty($data->noinv ))?$data->noinv :die('{"sts":"ERROR","desc":" Param invno Tidak Ditemukan"}');
			$tglinv = (isset($data->tglinv) && !empty($data->tglinv ))?$data->tglinv :die('{"sts":"ERROR","desc":" Param invno Tidak Ditemukan"}');
			$nobl= (isset($data->nobl) && !empty($data->nobl ))?$data->nobl :'';
			$tgbl= (isset($data->tgbl) && !empty($data->tgbl ))?$data->tgbl :'';
			$datacsv .= $inv_hdr_id . ',' . $invno . ',' .$tglinv; 
			$datacsv .= "|";
			$databl .=  $nobl;
		}
		
		if ($databl != ''){
			$responce = array("sts" => '00', "desc" => 'Data Dok Group sudah ada, Mohon periksa kembali datanya atau lakukan cancel utk Group Ulang');
			echo json_encode($responce);
			die();
		}
		//echo $datacsv;	die();
	    //var_dump($datadiff); die();
		$datacsv=substr($datacsv, 0, strlen($datacsv) - 1); 
		//echo $datacsv; die();
		
		$qry = $this->db->query("
		
			 EXEC ('sp_groupinv_bc40 @datadiff=''$datacsv'', @cmd=''proses''   ') 
			
		 ");
		 $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 cancelgroupinv_nonhib(){
		$datadiff = (isset($_POST['datadiff']) && !empty($_POST['datadiff']))?$_POST['datadiff']:die('{"sts":"ERROR","desc":" Data  Tidak Ditemukan"}'); 
		
		$datadiff=json_decode($datadiff);
		$datacsv = '';
		foreach ($datadiff as $data ){
			$datacsv .= $data->id_nonhib_hdr . ',' . $data->noinv . ',' . '-' . ',' . '1900-10-23'; 
			$datacsv .= "|";
		}
		$datacsv=substr($datacsv, 0, strlen($datacsv) - 1); 
		 // echo $datacsv;	
		//var_dump($datadiff); die();
		// echo "
		
			 // EXEC ('spu_nonhib_bl @datadiff=''$datacsv'', @nobl=''$nobl'', @tgbl=''$tgbl'' ') 
			
		 // ";
		 // die();
		 
		$qry = $this->db->query("
			BEGIN TRY
			 EXEC ('sp_groupinv_bc40 @datadiff=''$datacsv'',@cmd=''cancel'' ') 
			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){
			$responce = array("sts" => $row['status'], "desc" => $row['description']);
		}  
		echo json_encode($responce);
	}
	
	function view_bc40_nonhibprosesinsertinv(){
		$nobl = (isset($_POST['nobl']) or !empty($_POST['nobl']))?$_POST['nobl']:die('{"sts":"ERROR","desc":" Param nobl Tidak Ditemukan"}');
		$tgbl = (isset($_POST['tgbl']) or !empty($_POST['tgbl']))?$_POST['tgbl']:die('{"sts":"ERROR","desc":" Param tgbl Tidak Ditemukan"}');
		$data["tgbl"] = $tgbl;
		$data["nobl"] = $nobl;
		
		$this->load->view('exim/view_bc40_nonhibprosesinsertinv', $data);
	
	}
	
	function listinvbl_nonhib_proses(){
	
	
	
	
	}
	
	function updatestatusproses_nonhib(){
		$datanoinv = (isset($_POST['noinv']) or !empty($_POST['noinv']))?$_POST['noinv']:die('{"sts":"ERROR","desc":" Param INV Tidak Ditemukan"}');
		$nobl = (isset($_POST['nobl']) or !empty($_POST['nobl']))?$_POST['nobl']:die('{"sts":"ERROR","desc":" Param nobl Tidak Ditemukan"}');
		$tgbl= (isset($_POST['tgbl']) or !empty($_POST['tgbl']))?$_POST['tgbl']:die('{"sts":"ERROR","desc":" Param tg bl Tidak Ditemukan"}');
		$typebc = 'BC40';
		$datanoinv=json_decode($datanoinv);
		$datacsv = '';
		foreach ($datanoinv as $data ){
			$datacsv .= $data->id_nonhib_hdr . ',' . trim($data->noinv); 
			$datacsv .= "|";
		}
		// echo $datacsv;	
		// var_dump($datanoinv); die();
		$datacsv = substr($datacsv, 0,  strlen($datacsv) -1); 
		// echo "EXEC [sp_nonhib_insert] @datanoinv ='$datacsv', @typebc='$typebc', @tgbl='$tgbl', @nobl='$nobl' "; die();
		$qry = $this->db->query("
			
			EXEC [sp_nonhib_insert] @datanoinv ='$datacsv', @typebc='$typebc', @tgbl='$tgbl', @nobl='$nobl' 
		
		");
		$rs = $qry->result_array();	
		// var_dump($rs);
		// die();
		foreach ($rs as $row){
			$responce = array("sts" => $row['status'], "desc" => $row['description']);
		}  
		echo json_encode($responce);
	}
	
	function recalculate_new_inv_bl_nonhib(){
		$typebc = 'BC40';
		$noinv = (isset($_POST['noinv']) && !empty($_POST['noinv']))?$_POST['noinv']:die('{"sts":"ERROR","desc":" Param INV Tidak Ditemukan"}');
		$nobl = (isset($_POST['nobl']) && !empty($_POST['nobl']))?$_POST['nobl']:die('{"sts":"ERROR","desc":" Param nobl Tidak Ditemukan"}');
		$tgbl= (isset($_POST['tgbl']) && !empty($_POST['tgbl']))?$_POST['tgbl']:die('{"sts":"ERROR","desc":" Param tg bl Tidak Ditemukan"}');
		
		$noinv=json_decode($noinv);
		
		$noinvcsv = '';
		foreach ($noinv as $data ){
			if ($data->statusx == 0){
				$id_nonhib_hdr = (isset($data->id_nonhib_hdr) && !empty($data->id_nonhib_hdr ))?$data->id_nonhib_hdr :die('{"sts":"ERROR","desc":" Param id_nonhib_hdr Tidak Ditemukan"}');
				$invno = (isset($data->noinv) && !empty($data->noinv ))?$data->noinv :die('{"sts":"ERROR","desc":" Param noinv Tidak Ditemukan"}');
				$noinvcsv .= $id_nonhib_hdr. ',' . $invno; 
				$noinvcsv .= "|";
			}
		}
		$listinv = substr($noinvcsv, 0,  strlen($noinvcsv) -1); 
		// echo "
			// EXEC [sp_nonhib_insertcb23_recalculate_new_inv_bl] @nobl = N'$nobl', @tgbl = '$tgbl', @typebc = N'$typebc',@noinv = '$listinv'
		// ";
		// var_dump($noinv); die();
		 // die();
		$qry = $this->db->query("
			
			EXEC [sp_nonhib_insertcb23_recalculate_new_inv_bl] @nobl = N'$nobl', @tgbl = '$tgbl', @typebc = N'$typebc',@noinv = '$listinv'
			
		");
		$rs = $qry->result_array();	
		// var_dump($rs);
		// die();
		foreach ($rs as $row){
			$responce = array("sts" => $row['status'], "desc" => $row['description']);
		}  
		echo json_encode($responce);
		
	}
	
	function view_bc40_dtl(){
		$idhdrbc40 = (isset($_POST['idhdrbc40']) && !empty($_POST['idhdrbc40']))?$_POST['idhdrbc40']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$inv_hdr_idbc40 = (isset($_POST['inv_hdr_idbc40']) && !empty($_POST['inv_hdr_idbc40']))?$_POST['inv_hdr_idbc40']:'lihat doc';
		$noinv  = (isset($_POST['noinv']) && !empty($_POST['noinv']))?$_POST['noinv']:'lihat doc';
		$car= (isset($_POST['car']) && !empty($_POST['car']))?$_POST['car']:'-';
		$gabinv  = (isset($_POST['gabinv']) && !empty($_POST['gabinv']))?$_POST['gabinv']:'lihat doc';
		$tglgabinv  = (isset($_POST['tglgabinv']) && !empty($_POST['tglgabinv']))?$_POST['tglgabinv']:'lihat doc';
		$DokTupTg  = (isset($_POST['DokTupTg']) && !empty($_POST['DokTupTg']))?$_POST['DokTupTg']:'';
		$BC23No  = (isset($_POST['BC23No']) && !empty($_POST['BC23No']))?$_POST['BC23No']:'';
		$BC23Tg  = (isset($_POST['BC23Tg']) && !empty($_POST['BC23Tg']))?$_POST['BC23Tg']:'';
		$PasokNama  = (isset($_POST['PasokNama']) && !empty($_POST['PasokNama']))?$_POST['PasokNama']:'';
		
		
		$data["idhdrbc40"] = $idhdrbc40; 
		$data["inv_hdr_idbc40"] = $inv_hdr_idbc40; 
		$data["noinv"] = $noinv;
		$data["car"] = $car;
		$data["gabinv"] = $gabinv;
		$data["tglgabinv"] = $tglgabinv;
		$data["DokTupTg"] = $DokTupTg;
		$data["BC23No"] = $BC23No;
		$data["BC23Tg"] = $BC23Tg;
		$data["PasokNama"] = $PasokNama;
		
		$this->load->view('exim/view_bc40_dtl', $data);
	
	}
	
	function loaddataheader(){
		$search = isset($_POST['_search'])?$_POST['_search']:false;
        $searchField = isset($_POST['searchField'])?$_POST['searchField']:'';
        $searchString = isset($_POST['searchString'])?$_POST['searchString']:'';
        $filters =  isset($_POST['filters'])?$_POST['filters']:'';
		
		$page = isset($_POST['page'])?$_POST['page']:1; // get the requested page
        $limit = isset($_POST['rows'])?$_POST['rows']:20; // get how many rows we want to have into the grid
        $sidx = isset($_POST['sidx'])?$_POST['sidx']:'id'; // get index row - i.e. user click to sort
        $sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		$idy = (isset($_POST['idhdrbc40']) or !empty($_POST['idhdrbc40']))?$_POST['idhdrbc40']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$where = " id_bc_in_hdr=$idy ";
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_bc23->bc23hdrdataAll($limit,$start,$sidx,$sord, $where);
        $count = $this->mdl_bc23->bc23hdrdataCountAll($where);
		if( $count > 0 ) { $total_pages = ceil($count/$limit); } else { $total_pages = 0; } 
        if ($page > $total_pages) $page=$total_pages;
        
        $responce->page = $page;
		$responce->total = $total_pages;
		$responce->records =  $count;
		$i=0;
        foreach($records as $r){
			if($r->Status == null){
				$status = 'New';
				
			} else { $status=$r->Status;  }
			
			$responce->rows[$i]['id_bc_in_hdr']=$r->id_bc_in_hdr; 
			$responce->rows[$i]['CAR']=$r->CAR; 
			$responce->rows[$i]['KdKpbc']=$r->KdKpbc; 
			$responce->rows[$i]['Tujuan']=$r->Tujuan; 
			$responce->rows[$i]['JnsBarang']=$r->JnsBarang; 
			$responce->rows[$i]['TujuanKirim']=$r->TujuanKirim; 
			$responce->rows[$i]['PasokNama']=$r->PasokNama; 
			$responce->rows[$i]['PasokAlmt']=$r->PasokAlmt; 
			$responce->rows[$i]['PasokNeg']=$r->PasokNeg; 
			$responce->rows[$i]['UsahaId']=$r->UsahaId; 
			$responce->rows[$i]['UsahaNpwp']=$r->UsahaNpwp; 
			$responce->rows[$i]['KdTPB']=$r->KdTPB; 
			$responce->rows[$i]['Registrasi']=$r->Registrasi; 
			$responce->rows[$i]['UsahaNama']=$r->UsahaNama; 
			$responce->rows[$i]['UsahaAlmt']=$r->UsahaAlmt; 
			$responce->rows[$i]['UsahaStatus']=$r->UsahaStatus; 
			$responce->rows[$i]['ApiKd']=$r->ApiKd; 
			$responce->rows[$i]['ApiNo']=$r->ApiNo; 
			$responce->rows[$i]['PpjkId']=$r->PpjkId; 
			$responce->rows[$i]['PpjkNpwp']=$r->PpjkNpwp; 
			$responce->rows[$i]['PpjkNama']=$r->PpjkNama; 
			$responce->rows[$i]['PpjkAlmt']=$r->PpjkAlmt; 
			$responce->rows[$i]['PpjkNo']=$r->PpjkNo; 
			$responce->rows[$i]['PpjkTg']=$r->PpjkTg; 
			$responce->rows[$i]['IndId']=$r->IndId; 
			$responce->rows[$i]['IndNpwp']=$r->IndNpwp; 
			$responce->rows[$i]['IndNama']=$r->IndNama; 
			$responce->rows[$i]['IndAlmt']=$r->IndAlmt; 
			$responce->rows[$i]['Moda']=$r->Moda; 
			$responce->rows[$i]['AngkutNama']=$r->AngkutNama; 
			$responce->rows[$i]['AngkutNo']=$r->AngkutNo; 
			$responce->rows[$i]['AngkutFl']=$r->AngkutFl; 
			$responce->rows[$i]['PelBkr']=$r->PelBkr; 
			$responce->rows[$i]['PelMuat']=$r->PelMuat; 
			$responce->rows[$i]['PelTransit']=$r->PelTransit; 
			$responce->rows[$i]['BC23No']=$r->BC23No; 
			$responce->rows[$i]['BC23Tg']=$r->BC23Tg; 
			$responce->rows[$i]['KdKpbcBongkar']=$r->KdKpbcBongkar; 
			$responce->rows[$i]['KdKpbcAwas']=$r->KdKpbcAwas; 
			$responce->rows[$i]['DokTupKd']=$r->DokTupKd; 
			$responce->rows[$i]['DokTupNo']=$r->DokTupNo; 
			$responce->rows[$i]['DokTupTg']=$r->DokTupTg; 
			$responce->rows[$i]['PosNo']=$r->PosNo; 
			$responce->rows[$i]['PosSub']=$r->PosSub; 
			$responce->rows[$i]['TmpTbn']=$r->TmpTbn; 
			$responce->rows[$i]['KdVal']=$r->KdVal; 
			$responce->rows[$i]['Ndpbm']=$r->Ndpbm; 
			$responce->rows[$i]['NilInv']=$r->NilInv; 
			$responce->rows[$i]['Freight']=$r->Freight; 
			$responce->rows[$i]['BTambahan']=$r->BTambahan; 
			$responce->rows[$i]['Diskon']=$r->Diskon; 
			$responce->rows[$i]['KdAss']=$r->KdAss; 
			$responce->rows[$i]['Asuransi']=$r->Asuransi; 
			$responce->rows[$i]['KdHrg']=$r->KdHrg; 
			$responce->rows[$i]['Fob']=$r->Fob; 
			$responce->rows[$i]['Cif']=$r->Cif; 
			$responce->rows[$i]['Bruto']=$r->Bruto; 
			$responce->rows[$i]['Netto']=$r->Netto; 
			$responce->rows[$i]['JmCont']=$r->JmCont; 
			$responce->rows[$i]['JmBrg']=$r->JmBrg; 
			$responce->rows[$i]['TglTtd']=$r->TglTtd; 
			$responce->rows[$i]['KotaTtd']=$r->KotaTtd; 
			$responce->rows[$i]['Namattd']=$r->Namattd; 
			$responce->rows[$i]['ContakPerson']=$r->ContakPerson; 
			$responce->rows[$i]['NoPhone']=$r->NoPhone; 
			$responce->rows[$i]['NoFax']=$r->NoFax; 
			$responce->rows[$i]['Email']=$r->Email; 
			$responce->rows[$i]['Status']=$status;//$r->Status; 
			$responce->rows[$i]['Snrf']=$r->Snrf; 
			$responce->rows[$i]['Lengkap']=$r->Lengkap; 
			$responce->rows[$i]['PosSubSub']=$r->PosSubSub; 
			$responce->rows[$i]['NoKontrak']=$r->NoKontrak; 
			$responce->rows[$i]['TgKontrak']=$r->TgKontrak; 
			$responce->rows[$i]['HargaRp']=$r->HargaRp; 
			$responce->rows[$i]['TmpPeriksaFisik']=$r->TmpPeriksaFisik; 
			$responce->rows[$i]['AlmtPeriksaFisik']=$r->AlmtPeriksaFisik; 
			$responce->rows[$i]['TgPeriksaFisik']=$r->TgPeriksaFisik; 
			$responce->rows[$i]['WkPeriksaFisik']=$r->WkPeriksaFisik; 
			$responce->rows[$i]['create_date']=$r->create_date; 
			$responce->rows[$i]['create_by']=$r->create_by; 
			$responce->rows[$i]['update_date']=$r->update_date; 
			$responce->rows[$i]['update_by']=$r->update_by; 
			$responce->rows[$i]['typedok']=$r->typedok; 
			$responce->rows[$i]['statusdok']=$r->statusdok; 
			$responce->rows[$i]['noinv']=$r->noinv; 
			$responce->rows[$i]['tglinv']=$r->tglinv;
			$responce->rows[$i]['inv_hdr_id']=$r->inv_hdr_id;
			
			$i++;
		}
		echo json_encode($responce);
		
	}
	
	
	function loaddata_bc40_dtl(){
		$search = isset($_POST['_search'])?$_POST['_search']:false;
        $searchField = isset($_POST['searchField'])?$_POST['searchField']:'';
        $searchString = isset($_POST['searchString'])?$_POST['searchString']:'';
        $filters =  isset($_POST['filters'])?$_POST['filters']:'';
		
		$page = isset($_POST['page'])?$_POST['page']:1; // get the requested page
        $limit = isset($_POST['rows'])?$_POST['rows']:20; // get how many rows we want to have into the grid
        $sidx = isset($_POST['sidx'])?$_POST['sidx']:'id'; // get index row - i.e. user click to sort
        $sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		
		$idhdr = (isset($_POST['idhdrbc40']) && !empty($_POST['idhdrbc40']))?$_POST['idhdrbc40']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		
		$where = " id_bc_in_hdr=$idhdr ";
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_bc23->bc23detildataAll($limit,$start,$sidx,$sord, $where);
        $count = $this->mdl_bc23->bc23detildataCountAll($where);
		if( $count > 0 ) { $total_pages = ceil($count/$limit); } else { $total_pages = 0; } 
        if ($page > $total_pages) $page=$total_pages;
        
        $responce->page = $page;
		$responce->total = $total_pages;
		$responce->records =  $count;
		$i=0;
        foreach($records as $r){
			$responce->rows[$i]['id_bc_in_dtl']=$r->id_bc_in_dtl; 
			$responce->rows[$i]['id_bc_in_hdr']=$r->id_bc_in_hdr; 
			$responce->rows[$i]['CAR']=$r->CAR; 
			$responce->rows[$i]['Serial']=$r->Serial; 
			$responce->rows[$i]['JnsBarangDtl']=$r->JnsBarangDtl; 
			$responce->rows[$i]['TujuanKirimDtl']=$r->TujuanKirimDtl; 
			$responce->rows[$i]['NoHs']=$r->NoHs; 
			$responce->rows[$i]['SeriTrp']=$r->SeriTrp; 
			$responce->rows[$i]['BrgUrai']=$r->BrgUrai; 
			$responce->rows[$i]['Merk']=$r->Merk; 
			$responce->rows[$i]['Tipe']=$r->Tipe; 
			$responce->rows[$i]['SpfLain']=$r->SpfLain; 
			$responce->rows[$i]['KdBrg']=$r->KdBrg; 
			$responce->rows[$i]['Penggunaan']=$r->Penggunaan; 
			$responce->rows[$i]['KemasJn']=$r->KemasJn;
			$responce->rows[$i]['KemasJm']=$r->KemasJm;
			$responce->rows[$i]['KdFasDtl']=$r->KdFasDtl;
			$responce->rows[$i]['BrgAsal']=$r->BrgAsal;
			$responce->rows[$i]['DNilInv']=$r->DNilInv;
			$responce->rows[$i]['KdSat']=$r->KdSat;
			$responce->rows[$i]['JmlSat']=$r->JmlSat;
			$responce->rows[$i]['NettoDtl']=$r->NettoDtl;
			$responce->rows[$i]['SatBmJm']=$r->SatBmJm;
			$responce->rows[$i]['SatCukJm']=$r->SatCukJm;
			$responce->rows[$i]['DtlOk']=$r->DtlOk;
			$responce->rows[$i]['create_date']=$r->create_date;
			$responce->rows[$i]['create_by']=$r->create_by;
			$responce->rows[$i]['update_date']=$r->update_date;
			$responce->rows[$i]['update_by']=$r->update_by;
			$i++;
		}
		echo json_encode($responce);
		
	}
	function loaddatadok(){
		$search = isset($_POST['_search'])?$_POST['_search']:false;
        $searchField = isset($_POST['searchField'])?$_POST['searchField']:'';
        $searchString = isset($_POST['searchString'])?$_POST['searchString']:'';
        $filters =  isset($_POST['filters'])?$_POST['filters']:'';
		
		$page = isset($_POST['page'])?$_POST['page']:1; // get the requested page
        $limit = isset($_POST['rows'])?$_POST['rows']:20; // get how many rows we want to have into the grid
        $sidx = isset($_POST['sidx'])?$_POST['sidx']:'id'; // get index row - i.e. user click to sort
        $sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		
		$idhdr = (isset($_POST['idhdrbc40']) && !empty($_POST['idhdrbc40']))?$_POST['idhdrbc40']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		
		$where = " id_bc_in_hdr=$idhdr ";
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_bc23->bc23dokdataAll($limit,$start,$sidx,$sord, $where);
        $count = $this->mdl_bc23->bc23dokdataCountAll($where);
		if( $count > 0 ) { $total_pages = ceil($count/$limit); } else { $total_pages = 0; } 
        if ($page > $total_pages) $page=$total_pages;
        
        $responce->page = $page;
		$responce->total = $total_pages;
		$responce->records =  $count;
		$i=0;
        foreach($records as $r){
			$responce->rows[$i]['id_bc_in_dok']=$r->id_bc_in_dok; 
			$responce->rows[$i]['id_bc_in_hdr']=$r->id_bc_in_hdr; 
			$responce->rows[$i]['CAR']=$r->CAR; 
			$responce->rows[$i]['DokKd']=$r->DokKd; 
			$responce->rows[$i]['uraian']=$r->uraian; 
			$responce->rows[$i]['DokNo']=$r->DokNo; 
			$responce->rows[$i]['DokTg']=$r->DokTg; 
			
			$i++;
		}
		echo json_encode($responce);
		
	}
	function loaddatacon(){
		$search = isset($_POST['_search'])?$_POST['_search']:false;
        $searchField = isset($_POST['searchField'])?$_POST['searchField']:'';
        $searchString = isset($_POST['searchString'])?$_POST['searchString']:'';
        $filters =  isset($_POST['filters'])?$_POST['filters']:'';
		
		$page = isset($_POST['page'])?$_POST['page']:1; // get the requested page
        $limit = isset($_POST['rows'])?$_POST['rows']:20; // get how many rows we want to have into the grid
        $sidx = isset($_POST['sidx'])?$_POST['sidx']:'id'; // get index row - i.e. user click to sort
        $sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		
		$idhdr = (isset($_POST['idhdrbc40']) && !empty($_POST['idhdrbc40']))?$_POST['idhdrbc40']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		
		$where = " id_bc_in_hdr=$idhdr ";
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_bc23->bc23condataAll($limit,$start,$sidx,$sord, $where);
        $count = $this->mdl_bc23->bc23condataCountAll($where);
		if( $count > 0 ) { $total_pages = ceil($count/$limit); } else { $total_pages = 0; } 
        if ($page > $total_pages) $page=$total_pages;
        
        $responce->page = $page;
		$responce->total = $total_pages;
		$responce->records =  $count;
		$i=0;
        foreach($records as $r){
			$responce->rows[$i]['id_bc_in_con']=$r->id_bc_in_con; 
			$responce->rows[$i]['id_bc_in_hdr']=$r->id_bc_in_hdr; 
			$responce->rows[$i]['CAR']=$r->CAR; 
			$responce->rows[$i]['ContNo']=$r->ContNo; 
			$responce->rows[$i]['ContUkur']=$r->ContUkur; 
			$responce->rows[$i]['ContTipe']=$r->ContTipe; 
			
			$i++;
		}
		echo json_encode($responce);
		
	}
	
	function updatenoaju_bc40(){
		$idhdrbc40 = (isset($_POST['idhdrbc40']) && !empty($_POST['idhdrbc40']))?$_POST['idhdrbc40']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$bc40noaju = (isset($_POST['bc40noaju']) && !empty($_POST['bc40noaju']))?$_POST['bc40noaju']:'';
		$bc40tglaju  = (isset($_POST['bc40tglaju']) && !empty($_POST['bc40tglaju']))?$_POST['bc40tglaju']:'';
		$bc40nobc40= (isset($_POST['bc40nobc40']) && !empty($_POST['bc40nobc40']))?$_POST['bc40nobc40']:'';
		$bc40tglbc40= (isset($_POST['bc40tglbc40']) && !empty($_POST['bc40tglbc40']))?$_POST['bc40tglbc40']:'';
		$PasokNama= (isset($_POST['PasokNama']) && !empty($_POST['PasokNama']))?$_POST['PasokNama']:'';
		
		$username = $this->session->userdata('DX_username');
		
		// if ($bc40tglaju != ''){
			// $bc40tglajux = explode('/', $bc40tglaju);
			// $bc40tglaju = 	$bc40tglajux[1]. '/' . $bc40tglajux[0]. '/' . $bc40tglajux[2];
		// }
		
		// if ($bc40tglbc40 != ''){
			// $bc40tglbc40x = explode('/', $bc40tglbc40);
			// $bc40tglbc40 = 	$bc40tglbc40x[1]. '/' . $bc40tglbc40x[0]. '/' . $bc40tglbc40x[2];
		// }
		
		//echo $bc40tglaju; die();
		/* echo " exec(' spu_updatenobc40 @idbc40hdr=$idhdrbc40,@userid=''$username'', @noajubc40=''$bc40noaju'', @tgajubc40=''$bc40tglaju'', @nobc40=''$bc40nobc40'', @tglbc40=''$bc40tglbc40'' ')
			";die(); */
		
		$qry = $this->db->query("
			BEGIN TRY
			
			 exec(' spu_updatenobc40 @idbc40hdr=$idhdrbc40,@userid=''$username'', @noajubc40=''$bc40noaju'', @tgajubc40=''$bc40tglaju'', @nobc40=''$bc40nobc40'', @tglbc40=''$bc40tglbc40'', @supplier=''$PasokNama'' ')
			
			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_bc40_move(){
		$id_inv_hdr = (isset($_POST['inv_hdr_idbc23']) && !empty($_POST['inv_hdr_idbc23']))?$_POST['inv_hdr_idbc23']:die('{"sts":"ERROR","desc":" Param id_inv_hdr Tidak Ditemukan"}');
		$noinv = (isset($_POST['noinv']) && !empty($_POST['noinv']))?$_POST['noinv']:die('{"sts":"ERROR","desc":" Param noinv Tidak Ditemukan"}');
		$idhdrbc23 = (isset($_POST['idhdrbc23']) && !empty($_POST['idhdrbc23']))?$_POST['idhdrbc23']:die('{"sts":"ERROR","desc":" Param idhdrbc23 Tidak Ditemukan"}');
		$nobc23 = (isset($_POST['nobc23']) && !empty($_POST['nobc23']))?$_POST['nobc23']:'KOSONG';
		
		// if ($nobc23 == ""){
			// $nobc23 = 'KOSONG';
		// }
		
		$data["noinv"] = $noinv;
		$data["id_inv_hdr"] = $id_inv_hdr;
		$data["idhdrbc23"] = $idhdrbc23;
		$data["nobc23"] = $nobc23;
		
		
		
		$this->load->view('exim/view_bc40_move', $data);
	
	}
	
	function view_bc40_hdr_form_old(){
		$id_inv_hdr = (isset($_POST['id_inv_hdr']) && !empty($_POST['id_inv_hdr']))?$_POST['id_inv_hdr']:die('{"sts":"ERROR","desc":" Param id_inv_hdr Tidak Ditemukan"}');
		$BC23No = (isset($_POST['BC23No']) && !empty($_POST['BC23No']))?$_POST['BC23No']:die('{"sts":"ERROR","desc":" Param BC23No Tidak Ditemukan"}');
		$data["BC23No"]=$BC23No;
		$this->load->view('exim/view_bc40_hdr_form_old', $data);
		//$this->load->view('exim/view_bc40_hdr_form', $data);
	
	}
	
	function view_bc40_hdr_form_print(){
	
		$data["BC23No"]="BC23No";
		$this->load->view('exim/view_bc40_hdr_form_print', $data);	
	
	}
	
	function view_bc40_hdr_print(){
	
		/* $id_inv_hdr = 'zzzz';
		$BC23No = 'xxxxxxxxxx';
		$data["BC23No"]='cccccccc';
		$this->load->view('exim/view_bc40_hdr_form', $data);
		// Get output html
		$html = $this->output->get_output();
		$this->load->library('dompdf_gen');
		$this->dompdf->load_html($html);
		$this->dompdf->render();
		$this->dompdf->stream("welcome.pdf"); */
		 
         $this->load->helper(array('dompdf', 'file'));
		 $id_inv_hdr = 'zzzz';
		 $BC23No = 'xxxxxxxxxx';
		 $data["BC23No"]='cccccccc'; 
		 //$html = $this->load->view('exim/view_bc40_hdr_form', $data);
		  $html = $this->load->view('exim/view_bc40_hdr_form', $data, true);   
		 //echo $html;
		  pdf_create($html,'bc40',true);  
		  
		  
		 //$html = $this->load->view('controller/viewfile', $data, true);
		 //pdf_create($html, 'filename');
		  //$data = pdf_create($html, '', false);
          //write_file('name', $data);
		  
		
	/* 	$this->load->helper(array('html2pdf', 'file'));
		 $id_inv_hdr = 'zzzz';
		 $BC23No = 'xxxxxxxxxx';
		 $data['id_inv_hdr'] = $id_inv_hdr;
		 $data['BC23No'] = $BC23No;
		 $html = $this->load->view('exim/view_bc40_hdr_form', $data, true);  
		 html2pdf_create($html,"test"); */
		 die();
		 
		  
	}
	
	function savetoexcelbc40(){
		$id_bc_in_hdr = (isset($_REQUEST['id_bc_in_hdr']) && !empty($_REQUEST['id_bc_in_hdr']))?$_REQUEST['id_bc_in_hdr']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$Ndpbm1 = 0;
		$BC23Tg1 = '';
		$kdval1 = '';
		$rshdr1= $this->db->query("
			SELECT [id_bc_in_hdr]
			,[Ndpbm]
			,convert(date, BC23Tg) as BC23Tg
			,kdval 
			FROM [dbo].[tbl_bc_in_hdr]
		    where [typedok] = 'bc40' and  id_bc_in_hdr=$id_bc_in_hdr 
		");
		$qryhdr1 = $rshdr1->result_array();
		foreach($qryhdr1 as $rowhdr1){
			$Ndpbm1 = $rowhdr1['Ndpbm'];
			$BC23Tg1 = $rowhdr1['BC23Tg'];
			$kdval1 =  $rowhdr1['kdval']; 
		}
		
		if ($kdval1 != 'IDR'){ $kdval1 = 'USD'; }
		$nilaikurs = 1;
		if ($BC23Tg1 != ''){
			$qrykurs= $this->db->query("
				SELECT [id_kurs]
				  ,[KdKurs]
				  ,[TgAwal]
				  ,[TgAkhir]
				  ,[Nilai]
				  ,[Ket]
			  FROM [dbo].[tblKurs]
			  where [KdKurs] = '$kdval1' and [TgAwal] <= '$BC23Tg1' and [TgAkhir] >= '$BC23Tg1'
			");
			
			$rs = $qrykurs->result_array();	
			foreach ($rs as $row){
				$nilaikurs = $row["Nilai"];
			 }  
		
		} else {
		    $BC23Tg1 = date('Y-m-d');
			$qrykurs= $this->db->query("
				SELECT [id_kurs]
				  ,[KdKurs]
				  ,[TgAwal]
				  ,[TgAkhir]
				  ,[Nilai]
				  ,[Ket]
			  FROM [dbo].[tblKurs]
			  where [KdKurs] = '$kdval1' and [TgAwal] <= '$BC23Tg1' and [TgAkhir] >= '$BC23Tg1'
			");
			
			$rs = $qrykurs->result_array();	
			
			foreach ($rs as $row){
				$nilaikurs = $row["Nilai"];
			 }  
			
		}

		
		$rshdr= $this->db->query("
			SELECT [id_bc_in_hdr]
			  ,[CAR]
			  ,[KdKpbc]
			  ,[Tujuan]
			  ,[JnsBarang]
			  ,[TujuanKirim]
			  ,[PasokNama]
			  ,[PasokAlmt]
			  ,[PasokNeg]
			  ,[UsahaId]
			  ,[UsahaNpwp]
			  ,[KdTPB]
			  ,[Registrasi]
			  ,[UsahaNama]
			  ,[UsahaAlmt]
			  ,[UsahaStatus]
			  ,[ApiKd]
			  ,[ApiNo]
			  ,[PpjkId]
			  ,[PpjkNpwp]
			  ,[PpjkNama]
			  ,[PpjkAlmt]
			  ,[PpjkNo]
			  ,[PpjkTg]
			  ,[IndId]
			  ,[IndNpwp]
			  ,[IndNama]
			  ,[IndAlmt]
			  ,[Moda]
			  ,[AngkutNama]
			  ,[AngkutNo]
			  ,[AngkutFl]
			  ,[PelBkr]
			  ,[PelMuat]
			  ,[PelTransit]
			  ,[BC23No]
			  ,convert(varchar,[BC23Tg], 106) as BC23Tg
			  ,[KdKpbcBongkar]
			  ,[KdKpbcAwas]
			  ,[DokTupKd]
			  ,[DokTupNo]
			  ,[DokTupTg]
			  ,[PosNo]
			  ,[PosSub]
			  ,[TmpTbn]
			  ,[KdVal]
			  ,[Ndpbm]
			  ,[NilInv]
			  ,[Freight]
			  ,[BTambahan]
			  ,[Diskon]
			  ,[KdAss]
			  ,[Asuransi]
			  ,[KdHrg]
			  ,[Fob]
			  ,[Cif]
			  ,[Bruto]
			  ,[Netto]
			  ,[JmCont]
			  ,[JmBrg]
			  ,[TglTtd]
			  ,[KotaTtd]
			  ,[Namattd]
			  ,[ContakPerson]
			  ,[NoPhone]
			  ,[NoFax]
			  ,[Email]
			  ,[Status]
			  ,[Snrf]
			  ,[Lengkap]
			  ,[PosSubSub]
			  ,[NoKontrak]
			  ,[TgKontrak]
			  ,[HargaRp]
			  ,[TmpPeriksaFisik]
			  ,[AlmtPeriksaFisik]
			  ,[TgPeriksaFisik]
			  ,[WkPeriksaFisik]
			  ,[create_date]
			  ,[create_by]
			  ,[update_date]
			  ,[update_by]
			  ,[typedok]
			  ,[statusdok]
			  ,[noinv]
			  ,[tginv]
			  ,[inv_hdr_id]
			  ,[bc_in_from_in]
			  ,[nopol]
			  ,[nobl]
			  ,[tglbl]
			  ,[tgaju]
		  FROM [dbo].[tbl_bc_in_hdr]
		  where [typedok] = 'bc40' and  id_bc_in_hdr=$id_bc_in_hdr 
			");
		
		$qryhdr = $rshdr->result_array();
		
		$BC23No = '';
		$BC23Tg = '';
		$PasokNama = '';
		
		foreach($qryhdr as $rowhdr){
			$BC23No = $rowhdr['BC23No'];
			$BC23Tg = $rowhdr['BC23Tg'];
			$PasokNama = $rowhdr['PasokNama'];
		}
		
		
		
		$rssup= $this->db->query("
			SELECT [id]
			  ,[supplier_code]
			  ,[supplier_name]
			  ,[supplier_address]
			  ,[supplier_country]
			  ,[base_price]
			  ,[copper_base_reff]
			  ,[subgct]
			  ,[subcode_as400]
			  ,[NPWP]
			  ,[NOIJIN]
			  ,[NOKONTRAK]
			  ,[TGLIJIN]
			  ,[TGLKONTRAK]
			  ,[supplier_address1]
		  FROM [dbo].[tbl_data_supplier]
		  where [supplier_name] like '%$PasokNama%'
		
		
		");
		
		$qrysup = $rssup->result_array();
		
		$supplier_address = '';
		$supplier_address1 = '';
		$supplier_NPWP = '';
		foreach($qrysup as $rowsup){
			$supplier_address = $rowsup['supplier_address'];
			$supplier_address1 = $rowsup['supplier_address1'];
			$supplier_NPWP = $rowsup['NPWP'];
		}
		
		$rsdtl_count= $this->db->query("
			select count(*) as cnt  
			 FROM [dbo].[tbl_bc_in_dtl_partnumber]
			where id_bc_in_hdr=$id_bc_in_hdr 
		");
		
		$qryrsdtl_count = $rsdtl_count->result_array();
		
		$jmldt = 0;
		foreach($qryrsdtl_count as $rowdtl_count){
			$jmldt = $rowdtl_count["cnt"];
		}
		
		$rsdtl= $this->db->query("
			SELECT [id_bc_in_dtl_partnumber]
			  ,[inv_dtl]
			  ,[inv_hdr_id]
			  ,[id_partnumber]
			  ,[id_bc_in_hdr]
			  ,[noinv]
			  ,[car]
			  ,[nobc]
			  ,[tglbc]
			  ,[seri_detil]
			  ,[part_no2]
			  ,[part_number]
			  ,[part_name]
			  ,[part_name_customs]
			  ,[hscode]
			  ,[bm]
			  ,[qty]
			  ,[unit]
			  ,[harga]
			  ,[kdval]
			  ,[box]
			  ,[pack]
		  FROM [dbo].[tbl_bc_in_dtl_partnumber]
		  where id_bc_in_hdr=$id_bc_in_hdr 
		
		
		");
		
		$qryrsdtl = $rsdtl->result_array();
		
		$rsdok= $this->db->query("
			SELECT [id_bc_in_dok]
				  ,[id_bc_in_hdr]
				  ,[CAR]
				  ,[DokKd]
				  ,[DokNo]
				  ,convert(varchar, [DokTg], 106) as DokTg
			  FROM [dbo].[tbl_bc_in_dok]
			   where id_bc_in_hdr=$id_bc_in_hdr  
		
		
		");
		
		$qryrsdok = $rsdok->result_array();
		
		
		$rssumbox= $this->db->query("
			SELECT sum(kemasjm) as sumbox
				
			   FROM [dbo].[tbl_bc_in_dtl]
		       where id_bc_in_hdr=$id_bc_in_hdr 
		
		
		");
		$qrysumbox = $rssumbox->result_array();
		
		$sumbox = 1;
		foreach($qrysumbox as $rsumbox){
			
			$sumbox = $rsumbox["sumbox"];
		}
		
		$invoice = '';
		$tgl_invoice = '';
		foreach($qryrsdok as $rowdok){
			if ( $rowdok["DokKd"] == '380'  ){
				$invoice .= $rowdok["DokNo"] . ' ' ;
			}
			$tgl_invoice = $rowdok["DokTg"];
		}
		
		$datenow = date('Y-m-d');
		$filename =  "BC40_". $BC23No ."_$datenow";
		
		include_once ( APPPATH."libraries/PHPExcel/IOFactory.php");	
		
		
		$objTpl = PHPExcel_IOFactory::load(APPPATH."libraries/PHPExcel/templates/BC_40.xls");	
		$objTpl->setActiveSheetIndex(0);  //set first sheet as active
		$objTpl->getActiveSheet()->setCellValue('F15', '');
		$objTpl->getActiveSheet()->setCellValue('C4', ':'.$BC23No);
		$objTpl->getActiveSheet()->setCellValue('G6', ':'.$BC23Tg);	
		$objTpl->getActiveSheet()->setCellValue('G12', ':'.$supplier_NPWP);
		$objTpl->getActiveSheet()->setCellValue('G13', ':'.$PasokNama);
		$objTpl->getActiveSheet()->setCellValue('G14', ':'.$supplier_address);
		$objTpl->getActiveSheet()->setCellValue('G15', ':'.$supplier_address1);
		$objTpl->getActiveSheet()->setCellValue('C18', ':'.$invoice . ' Tgl ' . $tgl_invoice);
		$objTpl->getActiveSheet()->setCellValue('F47', '                     Balaraja. Tgl ' . $BC23Tg . '..');
		$objTpl->getActiveSheet()->setCellValue('G27', ':'.$sumbox);	
		
		
		//show detil
		if($jmldt > 5){
			
			$objTpl->getActiveSheet()->setCellValue('B39', ' ==== Terdapat ' . $jmldt . ' barang' . ' Silahkan lihat Pada Lembar Lampiran ===');
			$objTpl->getActiveSheet()->setCellValue('B40', '');
			$objTpl->getActiveSheet()->setCellValue('A38', '');
			$objTpl->getActiveSheet()->setCellValue('A39', '');
			$objTpl->getActiveSheet()->setCellValue('A40', '');
			$objTpl->getActiveSheet()->setCellValue('A41', '');
			$objTpl->getActiveSheet()->setCellValue('A42', '');
			
			$objTpl->setActiveSheetIndex(1); 
			$objTpl->getActiveSheet()->setCellValue('A7', 'NOMOR PENGAJUAN : ' . $BC23No);
			$objTpl->getActiveSheet()->setCellValue('G8', ': ' . $BC23No);
			$objTpl->getActiveSheet()->setCellValue('G9', ': ' . $BC23Tg);
			
			
			$i = 1;
			$j = '';
			$k = 15;
			$jj = '';
			$ii = 1;
			$kk = 15;
			foreach($qryrsdtl as $rowdtl){
				
				//$harga =  $rowdtl["harga"]   * $nilaikurs * $rowdtl["qty"];
				$harga =  $rowdtl["harga"]  * $rowdtl["qty"];
				if ($i < 28 ){
				
				$k = 15 + $i;
				$j = "$k";
				$objTpl->getActiveSheet()->setCellValue("A".$j, $i);
				$objTpl->getActiveSheet()->setCellValue("B".$j, $rowdtl["part_no2"] . "," . $rowdtl["part_name"]);
				$objTpl->getActiveSheet()->setCellValue("F".$j, $rowdtl["qty"] );
				$objTpl->getActiveSheet()->setCellValue("G".$j, $rowdtl["unit"] );
				$objTpl->getActiveSheet()->setCellValue("I".$j, $harga );
				$objTpl->getActiveSheet()->getStyle("I15:"."I".$j)->getNumberFormat()->setFormatCode('#,##0.00');
				
				} else {
				    $kk = 15 + $ii;
					$jj = "$kk";
					$objTpl->setActiveSheetIndex(2); 
						
					$objTpl->getActiveSheet()->setCellValue("A".$jj, $i);
					$objTpl->getActiveSheet()->setCellValue("B".$jj, $rowdtl["part_no2"] . "," . $rowdtl["part_name"]);
					$objTpl->getActiveSheet()->setCellValue("F".$jj, $rowdtl["qty"] );
					$objTpl->getActiveSheet()->setCellValue("G".$jj, $rowdtl["unit"] );
					$objTpl->getActiveSheet()->setCellValue("I".$jj, $harga );
					$ii++;
					$objTpl->getActiveSheet()->getStyle("I15:"."I".$jj)->getNumberFormat()->setFormatCode('#,##0.00');
				}
				
				$i++;
			}
			
			 
		} else {
		
			$i = 1;
			$j = '';
			$k = 37;
			foreach($qryrsdtl as $rowdtl){
				
				//$harga =  $rowdtl["harga"]   * $nilaikurs * $rowdtl["qty"];
				$harga =  $rowdtl["harga"]   * $rowdtl["qty"];
				
				$k = 37 + $i;
				$j = "$k";
				$objTpl->getActiveSheet()->setCellValue("A".$j, $i);
				$objTpl->getActiveSheet()->setCellValue("B".$j, $rowdtl["part_no2"] . "," . $rowdtl["part_name"]);
				$objTpl->getActiveSheet()->setCellValue("F".$j, $rowdtl["qty"] );
				$objTpl->getActiveSheet()->setCellValue("G".$j, $rowdtl["unit"] );
				$objTpl->getActiveSheet()->setCellValue("I".$j, $harga );
				
				
				$i++;
			}
			
			$objTpl->getActiveSheet()->getStyle("I37:"."I".$j)->getNumberFormat()->setFormatCode('#,##0.00');
			
			
		}
		
		
		$objTpl->setActiveSheetIndex(0); 
		if($jmldt < 5){
			$objTpl->getActiveSheet()->setCellValue("E3", "Halaman 1  dari 1");
		} else if ($jmldt > 5 and $jmldt < 27) {
			$objTpl->getActiveSheet()->setCellValue("E3", "Halaman 1  dari 2");
		
		} else if ($jmldt > 27) {
			$objTpl->getActiveSheet()->setCellValue("E3", "Halaman 1  dari 3");
		
		}
		
		
		
		
		header("Content-type:application/vnd.ms-excel");
		header("Content-disposition:attachment; filename=$filename"); 
		header("Pragma:no-cache");
		header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
		header("Expires:0");

		$objWriter = PHPExcel_IOFactory::createWriter($objTpl, 'Excel5');  //downloadable file is in Excel 2003 format (.xls)
		$objWriter->save('php://output');  //send it to user, of course you can save it to disk also!
		 
		exit; //done.. exiting!	
	
	}
	
	
	//--------------bc40 HIIB
	
	function loaddatahib_bc40(){
		
		
		$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']:'inv_hdr_id'; // get index row - i.e. user click to sort
        $sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		
		//$where = " a.status_inv=0 ";
		$where = " b.supplier_country = 'ID' ";
		if ($search == 'true'){
			if (!empty($filters)){
				$array = json_decode($filters);
				foreach($array->rules as $arr=>$k){
					
					if ( $k->field == "r3" ) {
						$searchString = $k->data;
						$where .= " and r3 like  '%".$searchString."%'"; 
					}  else if ( $k->field == "supplier_name" ) {
						$searchString = $k->data;
						$where .= " and b.supplier_name like  '%".$searchString."%'"; 
					}  else if ( $k->field == "r4" ) {
						$searchString = $k->data;
						$where .= " and r4 like  '%".$searchString."%'"; 
					}  else if ( $k->field == "r20" ) {
						$searchString = $k->data;
						$where .= " and r20 like  '%".$searchString."%'"; 
					}  else if ( $k->field == "nobl" ) {
						$searchString = $k->data;
						$where .= " and nobl like  '%".$searchString."%'"; 
					}  else if ( $k->field == "tgbl" ) {
						$searchString = $k->data;
						$where .= " and tgbl =  '$searchString'"; 
					} else if ( $k->field == "r2" ) {
						$searchString = $k->data;
						$where .= " and r2 like  '%".$searchString."%'"; 
					} else if ( $k->field == "status_inv" ) {
						$searchString = $k->data;
							if ($searchString==1){ 
								$where = " a.status_inv=$searchString  "; 
							} else if ($searchString==2){ 
								$where .= " "; 
								//$where .= " or a.status_inv=1  "; 
							} else if ($searchString=='-'){ 
								$where .= " and a.status_inv=0  "; 
							} else {
								$where .= " and (a.status_inv=0  or a.status_inv='$searchString') "; 
							}
					} else if ( $k->field == "status_inv" && $k->field == "r2" )  {
						$searchString = $k->data;
						$where = " r3 like  '%".$searchString."%'"; 
					
					}
				
				}
			}
		} else {
		
			$where .= "  and a.status_inv=0 ";
		}
		
	//	echo $where;
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_bc40->inv_all($limit,$start,$sidx,$sord, $where);
        $count = $this->mdl_bc40->inv_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){
			if ($r->status_inv == 0){ $stat = "New"; } else { $stat = "Not New"; };
			if ($r->tgbl =='1900-10-23'){ $tgbl = ""; } else { $tgbl = $r->tgbl; };	
			$responce->rows[$i]['inv_hdr_id']=$r->inv_hdr_id; 
			$responce->rows[$i]['r3']=$r->r3; 
			$responce->rows[$i]['r2']=$r->r2; 
			$responce->rows[$i]['r4']=$r->r4;
			$responce->rows[$i]['r5']= inv_parsedate($r->r5); //$r->r5;
			$responce->rows[$i]['r20']=$r->r20;
			$responce->rows[$i]['supplier_name']=$r->supplier_name; 
			$responce->rows[$i]['date_create']=$r->date_create; 
			$responce->rows[$i]['status_inv']=$stat;
			$responce->rows[$i]['nobl']=$r->nobl;
			$responce->rows[$i]['tgbl']=$tgbl;
			
			$i++;
		}
		echo json_encode($responce);
		
	}
	
	function view_bc40_hib(){
		$q = $this->db->query("
			SELECT     v_noinv_dtl.noinv
			FROM         v_noinv_dtl LEFT OUTER JOIN
								  tbl_data_part_hs ON replace(v_noinv_dtl.partnumber,' ','') = tbl_data_part_hs.part_no2
			where tbl_data_part_hs.part_no is null
			group by v_noinv_dtl.noinv
		");
		$rs = $q->result_array();
		$datanull='';
		foreach ($rs as $row){
			$datanull.=$row['noinv'].'|';		
		}
		if ($datanull != ''){
			$data["test"] = 'Part Number di Data Part HS Not Match Di Invoice Berikut, Silahkan Cek Di View Data : ' . $datanull;
		} else {
			$data["test"]=$datanull;
		}
		$this->load->view('exim/view_bc40_hib', $data);
		

	
	
	}
	function view_bc40_group_invoice(){
	
		$data["test"] = "";
		$this->load->view('exim/view_bc40_group_invoice', $data);
	
	}
	
	function prosesdatabl(){
		
		$datadiff = (isset($_POST['datadiff']) && !empty($_POST['datadiff']))?$_POST['datadiff']:'0'; 
		$datadiff=json_decode($datadiff);
		$datacsv = '';
		foreach ($datadiff as $data ){
			$inv_hdr_id = (isset($data->inv_hdr_id) && !empty($data->inv_hdr_id ))?$data->inv_hdr_id :die('{"sts":"ERROR","desc":" Param inv_hdr_id Tidak Ditemukan"}');
			$invno = (isset($data->invno) && !empty($data->invno ))?$data->invno :die('{"sts":"ERROR","desc":" Param invno Tidak Ditemukan"}');
			
			$datacsv .= $inv_hdr_id . ',' . $invno; 
			$datacsv .= "|";
		}
		// echo $datacsv;	
		// var_dump($datadiff); die();
		
		
		$qry = $this->db->query("
		
			BEGIN TRY
			
				EXEC ('[spi_invoiceupdatebl_bc40_27] @datadiff=''$datacsv'', @cmd=''proses'', @typebc=''BC40''  ') 
			END TRY
			BEGIN CATCH 
				SELECT 
				'error' AS status
				,ERROR_MESSAGE() AS description;
				
			END CATCH;
			
		 ");
		 $rs = $qry->result_array();
        // $r = $qry->result();		
		// var_dump($r);die();
		foreach ($rs as $row){
			$responce = array("sts" => $row['status'], "desc" => $row['description']);
		}  
		echo json_encode($responce);
		
	
	}
	function canceldatabl(){
		
		$datadiff = (isset($_POST['datadiff']) or !empty($_POST['datadiff']))?$_POST['datadiff']:'0'; 
		$datadiff=json_decode($datadiff);
		$datacsv = '';
		foreach ($datadiff as $data ){
			$datacsv .= $data->inv_hdr_id . ',' . $data->invno . ',' . $data->blawbno . ',' . '1900-10-23'; 
			$datacsv .= "|";
		}
		 // echo $datacsv;	
		// var_dump($datadiff); die();
		
		// echo "
		
			 // EXEC ('spi_invoiceupdatebl @datadiff=''$datacsv'', @nobl=''$nobl'', @tgbl=''$tgbl'', @cmd=''cancel'' ') 
			
		 // "; die();
		
		$qry = $this->db->query("
			BEGIN TRY	
			 EXEC ('spi_invoiceupdatebl_bc40_27 @datadiff=''$datacsv'', @cmd=''cancel'', @typebc=''BC40'' ') 
			END TRY
			BEGIN CATCH 
				SELECT 
				'error' AS status
				,ERROR_MESSAGE() AS description;
				
			END CATCH;
			
		 ");
		$rs = $qry->result_array();
        // $r = $qry->result();		
		// var_dump($r);die();
		foreach ($rs as $row){
			$responce = array("sts" => $row['status'], "desc" => $row['description']);
		}  
		echo json_encode($responce);
	
	}
	
	function view_bc40_prosesinsertinv(){
		$nobl = (isset($_POST['nobl']) or !empty($_POST['nobl']))?$_POST['nobl']:die('{"sts":"ERROR","desc":" Param nobl Tidak Ditemukan"}');
		$tgbl = (isset($_POST['tgbl']) or !empty($_POST['tgbl']))?$_POST['tgbl']:die('{"sts":"ERROR","desc":" Param tgbl Tidak Ditemukan"}');
		$data["tgbl"] = $tgbl;
		$data["nobl"] = $nobl;
		$this->load->view('exim/view_bc40_prosesinsertinv',$data);
	
	}
	
	function loaddatanonobl(){
		$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']:'inv_hdr_id'; // get index row - i.e. user click to sort
        $sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		
		$nobl = (isset($_POST['nobl']) && !empty($_POST['nobl']))?$_POST['nobl']:'';
		$tgbl = (isset($_POST['tgbl']) && !empty($_POST['tgbl']))?$_POST['tgbl']:'';
		
		
		//$where = " a.status_inv=0 ";
		$where = " 1=1 ";
		$where .= " and nobl =  '".$nobl."'";
		$where .= " and tgbl =  '".$tgbl."'";
		
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_invoice->inv_all($limit,$start,$sidx,$sord, $where);
        $count = $this->mdl_invoice->inv_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){
		    if ($r->status_inv == 0){
					  $status_inv = 'INVOICE NEW ADDED';
			} else if ($r->status_inv == 1){
				 $status_inv = 'DATA ALREADY PROCESSED';
			}
			$responce->rows[$i]['inv_hdr_id']=$r->inv_hdr_id; 
			$responce->rows[$i]['invno']=$r->r3; 
			$responce->rows[$i]['blawbno']=$r->nobl; 
			$responce->rows[$i]['tglblawb']=$r->tgbl;
			$responce->rows[$i]['status_inv']=$status_inv;
			$responce->rows[$i]['status_invx']=$r->status_inv;
			
			
			$i++;
		}
		echo json_encode($responce);
		
	}
	
	function updatestatusproses(){
		$typebc = 'BC40';
		$noinv = (isset($_POST['noinv']) && !empty($_POST['noinv']))?$_POST['noinv']:die('{"sts":"ERROR","desc":" Param INV Tidak Ditemukan"}');
		$nobl = (isset($_POST['nobl']) && !empty($_POST['nobl']))?$_POST['nobl']:die('{"sts":"ERROR","desc":" Param nobl Tidak Ditemukan"}');
		$tgbl= (isset($_POST['tgbl']) && !empty($_POST['tgbl']))?$_POST['tgbl']:die('{"sts":"ERROR","desc":" Param tg bl Tidak Ditemukan"}');
		
		$noinv=json_decode($noinv);
		$noinvcsv = '';
		foreach ($noinv as $data ){
			$inv_hdr_id = (isset($data->inv_hdr_id) && !empty($data->inv_hdr_id ))?$data->inv_hdr_id :die('{"sts":"ERROR","desc":" Param inv_hdr_id Tidak Ditemukan"}');
			$invno = (isset($data->invno) && !empty($data->invno ))?$data->invno :die('{"sts":"ERROR","desc":" Param invno Tidak Ditemukan"}');
			$noinvcsv .= $inv_hdr_id. ',' . $invno; 
			$noinvcsv .= "|";
		}
		$listinv = substr($noinvcsv, 0,  strlen($noinvcsv) -1); 
		 // echo "EXEC  [sp_insertbc23] @nobl = N'$nobl', @tgbl = '$tgbl', @typebc = N'$typebc',@noinv = '$listinv'";
		 // var_dump($noinv); die();
		$typebc = 'BC40';
		$qry = $this->db->query("
			BEGIN TRY
				EXEC  [sp_insertbc23] @nobl = N'$nobl', @tgbl = '$tgbl', @typebc = N'$typebc',@noinv = '$listinv'
			END TRY
			BEGIN CATCH 
				SELECT 
				'error' AS status
				,ERROR_MESSAGE() AS description;
				
			END CATCH;
		");
		$rs = $qry->result_array();	
		// var_dump($rs);
		// die();
		foreach ($rs as $row){
			$responce = array("sts" => $row['status'], "desc" => $row['description']);
		}  
		echo json_encode($responce);
		
		
		
	}
	
	function recalculate_new_inv_bl(){
		$typebc = 'BC40';
		$noinv = (isset($_POST['noinv']) && !empty($_POST['noinv']))?$_POST['noinv']:die('{"sts":"ERROR","desc":" Param INV Tidak Ditemukan"}');
		$nobl = (isset($_POST['nobl']) && !empty($_POST['nobl']))?$_POST['nobl']:die('{"sts":"ERROR","desc":" Param nobl Tidak Ditemukan"}');
		$tgbl= (isset($_POST['tgbl']) && !empty($_POST['tgbl']))?$_POST['tgbl']:die('{"sts":"ERROR","desc":" Param tg bl Tidak Ditemukan"}');
		
		$noinv=json_decode($noinv);
		$noinvcsv = '';
		foreach ($noinv as $data ){
			$inv_hdr_id = (isset($data->inv_hdr_id) && !empty($data->inv_hdr_id ))?$data->inv_hdr_id :die('{"sts":"ERROR","desc":" Param inv_hdr_id Tidak Ditemukan"}');
			$invno = (isset($data->invno) && !empty($data->invno ))?$data->invno :die('{"sts":"ERROR","desc":" invno Param invno Tidak Ditemukan"}');
			$noinvcsv .= $inv_hdr_id. ',' . $invno; 
			$noinvcsv .= "|";
		}
		$listinv = substr($noinvcsv, 0,  strlen($noinvcsv) -1); 
		 //echo "EXEC  [sp_insertcb23_recalculate_new_inv_bl] @nobl = N'$nobl', @tgbl = '$tgbl', @typebc = N'$typebc',@noinv = '$listinv'";
		 // var_dump($noinv); die();
		$typebc = 'BC40';
		$qry = $this->db->query("
			EXEC [sp_insertcb23_recalculate_new_inv_bl] @nobl = N'$nobl', @tgbl = '$tgbl', @typebc = N'$typebc',@noinv = '$listinv'
		");
		$rs = $qry->result_array();	
		//var_dump($rs);
		//die();
		foreach ($rs as $row){
			$responce = array("sts" => $row['status'], "desc" => $row['description']);
		}  
		echo json_encode($responce);
		
	}
	function recalculatedatabl(){
	
		$nobl = (isset($_POST['nobl']) && !empty($_POST['nobl']))?$_POST['nobl']:die('{"sts":"ERROR","desc":" Param  Tidak Ditemukan"}'); 
		$tgbl = (isset($_POST['tgbl']) && !empty($_POST['tgbl']))?$_POST['tgbl']:die('{"sts":"ERROR","desc":" Param  Tidak Ditemukan"}');
		
		$datadiff = (isset($_POST['datadiff']) && !empty($_POST['datadiff']))?$_POST['datadiff']:'0'; 
		$datadiff=json_decode($datadiff);
		$datacsv = '';
		foreach ($datadiff as $data ){
			$inv_hdr_id = (isset($data->inv_hdr_id) && !empty($data->inv_hdr_id ))?$data->inv_hdr_id :die('{"sts":"ERROR","desc":" Param inv_hdr_id Tidak Ditemukan"}');
			$invno = (isset($data->invno) && !empty($data->invno ))?$data->invno :die('{"sts":"ERROR","desc":" Param invno Tidak Ditemukan"}');
			$blawbno = (isset($data->blawbno) && !empty($data->blawbno ))?$data->blawbno :die('{"sts":"ERROR","desc":" Param blawbno Tidak Ditemukan"}');
			$tglblawb = (isset($data->tglblawb) && !empty($data->tglblawb ))?$data->tglblawb :die('{"sts":"ERROR","desc":" Param tglblawb Tidak Ditemukan"}');
			
			$datacsv .= $inv_hdr_id . ',' . $invno . ',' . $blawbno . ',' . $tglblawb; 
			$datacsv .= "|";
		}
		// echo $datacsv;	
		// var_dump($datadiff); die();
		
		
		$qry = $this->db->query("
			BEGIN TRY
			 EXEC ('spu_insertbc23_bl @datadiff=''$datacsv'', @nobl=''$nobl'', @tgbl=''$tgbl''  ') 
			END TRY
			BEGIN CATCH 
				SELECT 
				'error' AS status
				,ERROR_MESSAGE() AS description;
				
			END CATCH;
			
		 ");
		 $rs = $qry->result_array();
        // $r = $qry->result();		
		// var_dump($r);die();
		foreach ($rs as $row){
			$responce = array("sts" => $row['status'], "desc" => $row['description']);
		}  
		echo json_encode($responce);
	
	}
	
	function cancelbcinvoicebc(){
	
		$nobl = (isset($_POST['nobl']) && !empty($_POST['nobl']))?$_POST['nobl']:die('{"sts":"ERROR","desc":" Param  Tidak Ditemukan"}'); 
		$tgbl = (isset($_POST['tgbl']) && !empty($_POST['tgbl']))?$_POST['tgbl']:die('{"sts":"ERROR","desc":" Param  Tidak Ditemukan"}');
		
		/* echo " EXEC ('spu_invoicenonhib_del_bc40_27 @nobl=''$nobl'', @tgbl=''$tgbl'', @typebc=''BC40''  ')  ";
		die();
		 */
		$qry = $this->db->query("
			BEGIN TRY
			 EXEC ('spu_invoicenonhib_del_bc40_27 @nobl=''$nobl'', @tgbl=''$tgbl'', @typebc=''BC40''  ') 
			END TRY
			BEGIN CATCH 
				SELECT 
				'error' AS status
				,ERROR_MESSAGE() AS description;
				
			END CATCH;
			
		 ");
		 $rs = $qry->result_array();
        // $r = $qry->result();		
		// var_dump($r);die();
		foreach ($rs as $row){
			$responce = array("sts" => $row['status'], "desc" => $row['description']);
		}  
		echo json_encode($responce);
	
	}
	
	
	
	//---------------proses invoice_bc40 bahan penolong
	function proses_inv_bc40(){
		$id_inv_bc40 = (isset($_POST['id_inv_bc40']) && !empty($_POST['id_inv_bc40']))?$_POST['id_inv_bc40']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$uid = $this->session->userdata('DX_username');
		
		// echo " exec('[spi_invoice_bc40sparepart]  @id_inv_bc40 = $id_inv_bc40, @tipe=''INVMPK'', @username=''$uid'' ') ";
		// die();
		$qry_inv_bc40 = $this->db->query("
			
			BEGIN TRY
			
			 exec('[spi_invoice_bc40]  @id_inv_bc40 = $id_inv_bc40, @tipe=''INVBC40'', @username=''$uid'' ')
			
			END TRY
			BEGIN CATCH 
				SELECT 
				'06' AS status
				,ERROR_MESSAGE() AS description;
				
			END CATCH;
			
			");
		$rs = $qry_inv_bc40->result_array();
        //$r = $qry_inv_bc40->result();		
		//var_dump($rs);die();
		foreach ($rs as $row){
			$responce = array("sts" => $row['status'], "desc" => $row['description']);
		}  
		echo json_encode($responce);
	
	
	}
	
	function view_bc40_getdatapo(){
		
		$this->load->view('exim/view_bc40_getdatapo');
	}
	
	function loaddata_proses_inv_bc40_hdr(){
		$search = isset($_POST['_search'])?$_POST['_search']:false;
        $searchField = isset($_POST['searchField'])?$_POST['searchField']:'';
        $searchString = isset($_POST['searchString'])?$_POST['searchString']:'';
        $filters =  isset($_POST['filters'])?$_POST['filters']:'';
		
		$page = isset($_POST['page'])?$_POST['page']:1; // get the requested page
        $limit = isset($_POST['rows'])?$_POST['rows']:20; // get how many rows we want to have into the grid
        $sidx = isset($_POST['sidx'])?$_POST['sidx']:'order_date'; // get index row - i.e. user click to sort
        $sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		
		$where = " 1=1 ";
				
		if ($search == 'true'){
			if (!empty($filters)){
				$array = json_decode($filters);
				foreach($array->rules as $arr=>$k){
					
					if ( $k->field == "invoice_no" ) {
						$searchString = $k->data;
						$where .= " and invoice_no like  '%".$searchString."%'"; 
					} else if ( $k->field == "part_name" ) {
						$searchString = $k->data;
						$where .= " and part_name like  '%".$searchString."%'"; 
					}  else if ( $k->field == "status_data" ) {
						$searchString = $k->data;
						$where .= " and status_data like  '%".$searchString."%'"; 
						if ($searchString == 'all'){
						
							$where = " 1=1 ";
						}
					}
				}
			}
		} else {
		
			$where = " 1=1 and status_data='new' ";
		
		}
		
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_bc40->bc40_invbc40_hdrAll($limit,$start,$sidx,$sord, $where);
        $count = $this->mdl_bc40->bc40_invbc40_hdrCountAll($where);
	//	var_dump($records); die();
		
		if( $count > 0 ) { $total_pages = ceil($count/$limit); } else { $total_pages = 0; } 
        if ($page > $total_pages) $page=$total_pages;
        
        $responce->page = $page;
		$responce->total = $total_pages;
		$responce->records =  $count;
		$i=0;
        foreach($records as $r){
			$responce->rows[$i]['kode_supplier']=$r->kode_supplier;
			$responce->rows[$i]['order_no']=$r->order_no;
			$responce->rows[$i]['order_date']=$r->order_date;
			$responce->rows[$i]['invoice_no']=$r->invoice_no;
			$responce->rows[$i]['invoice_date']=$r->invoice_date;
			$responce->rows[$i]['status_data']=$r->status_data;
			$responce->rows[$i]['nobl']=$r->nobl;
			$responce->rows[$i]['tglbl']=$r->tglbl;
			
			$i++;
		}
		echo json_encode($responce);
	}
	
	function view_bc40_detil_invbc40(){
	
		$postData = (isset($_POST['postData']) && !empty($_POST['postData']))?$_POST['postData']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan or not selected"}');
		
		$data['dataarray'] =  $postData;
		$this->load->view('exim/view_bc40_detil_invbc40', $data);		

	
	
	
	}
	function loaddata_proses_inv_bc40(){
		$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_invoice_bc40'; // get index row - i.e. user click to sort
        $sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		$dimana = (isset($_POST['where']) && !empty($_POST['where']))?$_POST['where']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$dimana = str_replace("<font color='red'>", "", $dimana);
		$rep = array('<font color="red">', '<font color="blue">','</font>');
		$dimana = str_replace($rep, "", $dimana);
		$dataarray = json_decode($dimana);
		/* 
		print_r($dataarray);
		die();
		 */
		$whereinvoice_no = '';
		$whereorder_no = '';
		foreach ($dataarray as $r ){
			$whereinvoice_no .= "'" . $r->invoice_no . "'" .',';
			$whereorder_no .= "'" . $r->order_no . "'"  . ',';
		}
		
				
		$whereinvoice_no = substr($whereinvoice_no, 0, strlen($whereinvoice_no) - 1);
		$whereorder_no = substr($whereorder_no, 0, strlen($whereorder_no) - 1);
		
		$whereinvoice_no = '('.$whereinvoice_no.')';
		$whereorder_no = '('.$whereorder_no.')';
		
				
		$where = " 1=1 and status_data='new' and [invoice_no] in $whereinvoice_no and order_no in $whereorder_no  ";
			
		if ($search == 'true'){
			if (!empty($filters)){
				$array = json_decode($filters);
				foreach($array->rules as $arr=>$k){
					
					if ( $k->field == "invoice_no" ) {
						$searchString = $k->data;
						$where .= " and invoice_no like  '%".$searchString."%'"; 
					} else if ( $k->field == "part_name" ) {
						$searchString = $k->data;
						$where .= " and part_name like  '%".$searchString."%'"; 
					} 
				}
			}
		}
		
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_bc40->bc40_invbc40_All($limit,$start,$sidx,$sord, $where);
        $count = $this->mdl_bc40->bc40_invbc40_CountAll($where);
	//	var_dump($records); die();
		
		if( $count > 0 ) { $total_pages = ceil($count/$limit); } else { $total_pages = 0; } 
        if ($page > $total_pages) $page=$total_pages;
        
        $responce->page = $page;
		$responce->total = $total_pages;
		$responce->records =  $count;
		$i=0;
        foreach($records as $r){
		
			$responce->rows[$i]['id_invoice_bc40']=$r->id_invoice_bc40;
			$responce->rows[$i]['part_name']=$r->part_name;
			$responce->rows[$i]['part_no']=$r->part_no;
			$responce->rows[$i]['kode_barang']=$r->kode_barang;
			$responce->rows[$i]['kode_supplier']=$r->kode_supplier;
			$responce->rows[$i]['maker']=$r->maker;
			$responce->rows[$i]['order_qty']=$r->order_qty;
			$responce->rows[$i]['order_no']=$r->order_no;
			$responce->rows[$i]['order_date']=$r->order_date;
			$responce->rows[$i]['req_eta_date']=$r->req_eta_date;
			$responce->rows[$i]['shipping_by']=$r->shipping_by;
			$responce->rows[$i]['invoice_no']=$r->invoice_no;
			$responce->rows[$i]['invoice_date']=$r->invoice_date;
			$responce->rows[$i]['inv_qty']=$r->inv_qty;
			$responce->rows[$i]['inv_unit']=$r->inv_unit;
			$responce->rows[$i]['status_inv']=$r->status_inv;
			$responce->rows[$i]['rec_qty']=$r->rec_qty;
			$responce->rows[$i]['rec_date']=$r->rec_date;
			$responce->rows[$i]['amount']=$r->amount;
			$responce->rows[$i]['remark']=$r->remark;
			$responce->rows[$i]['nobl']=$r->nobl;
			$responce->rows[$i]['tglbl']=$r->tglbl;
			$responce->rows[$i]['nocar']=$r->nocar;
			$responce->rows[$i]['tglaju']=$r->tglaju;
			$responce->rows[$i]['status_data']=$r->status_data;
			$responce->rows[$i]['create_date']=$r->create_date;
			$responce->rows[$i]['create_by']=$r->create_by;
			$responce->rows[$i]['update_date']=$r->update_date;
			$responce->rows[$i]['update_by']=$r->update_by;
			
			$i++;
		}
		echo json_encode($responce);
		
	
	
	}
	
	function proses_update_bl_bc40(){
		
		$this->load->library('arraytoxml');
		$uid = $this->session->userdata('DX_username');
		$postData = (isset($_POST['postData']) && !empty($_POST['postData']))?$_POST['postData']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan or not selected"}');
		$no_bl_bc40 = (isset($_POST['no_bl_bc40']) && !empty($_POST['no_bl_bc40']))?$_POST['no_bl_bc40']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan or not selected"}');
		$date_bl_bc40 = (isset($_POST['date_bl_bc40']) && !empty($_POST['date_bl_bc40']))?$_POST['date_bl_bc40']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan or not selected"}');
		
		$rep = array('<font color="red">', '<font color="blue">','</font>');
		$postData = str_replace($rep, "", $postData);
		
		$array = json_decode($postData);
		$array = objectToArray($array);
		
		$postdata = $this->arraytoxml->toXml($array);
		//$xxx = str_replace("&Acirc;&nbsp;", "", $postdata);		
		$search = array('&acirc;','&acirc;','&acirc;','&acirc;','&Acirc;&pound;','&Acirc;&not;','&acirc;&cent;','&Acirc;','&nbsp;','&Acirc;&nbsp;');
		$xxx = str_replace($search, "", $postdata);
			
		
		//echo $xxx1;
			
 	 /* 	echo " exec('[spu_bl_mesinsparepart]  @username=''$uid'', @xmldata=''$xxx'',@no_bl_bc40=''$no_bl_bc40'',@date_bl_bc40=''$date_bl_bc40'' ')";
		die(); */ 
		
		$uid = $this->session->userdata('DX_username');
		$qry_inv_mesin = $this->db->query("
			
			BEGIN TRY
			
			 exec('[spu_bl_invbc40]  @username=''$uid'', @xmldata=''$xxx'',@no_bl_invbc40=''$no_bl_bc40'',@date_bl_invbc40=''$date_bl_bc40'' ')
			
			END TRY
			BEGIN CATCH 
				SELECT 
				'06' AS status
				,ERROR_MESSAGE() AS description;
				
			END CATCH;
			
			");
		$rs = $qry_inv_mesin->result_array();
        //$r = $qry_inv_mesin->result();		
		//var_dump($rs);die();
		foreach ($rs as $row){
			$responce = array("sts" => $row['status'], "desc" => $row['description']);
		}  
		echo json_encode($responce);
		
	
	
	
	}
	
	function proses_update_inv_bc40(){
	
		$this->load->library('arraytoxml');
		$uid = $this->session->userdata('DX_username');
		$postData = (isset($_POST['postData']) && !empty($_POST['postData']))?$_POST['postData']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan or not selected"}');
		
		$rep = array('<font color="red">', '<font color="blue">','</font>');
		$postData = str_replace($rep, "", $postData);
		
		$array = json_decode($postData);
		$array = objectToArray($array);
		
		$postdata = $this->arraytoxml->toXml($array);
		//$xxx = str_replace("&Acirc;&nbsp;", "", $postdata);		
		$search = array('&acirc;','&acirc;','&acirc;','&acirc;','&Acirc;&pound;','&Acirc;&not;','&acirc;&cent;','&Acirc;','&nbsp;','&Acirc;&nbsp;','&lt;font color="red"&gt;new');
		$xxx = str_replace($search, "", $postdata);
			
		//echo $xxx1;
			
 		/*echo "exec('[spu_invoice_mesinsparepart]  @username=''$uid'', @xmldata=''$xxx'' ')";
		die(); 
		*/
		$uid = $this->session->userdata('DX_username');
		$qry_inv_mesin = $this->db->query("
			
			BEGIN TRY
			
			 exec('[spu_invoice_bc40]  @username=''$uid'', @xmldata=''$xxx'' ')
			
			END TRY
			BEGIN CATCH 
				SELECT 
				'06' AS status
				,ERROR_MESSAGE() AS description;
				
			END CATCH;
			
			");
		$rs = $qry_inv_mesin->result_array();
        //$r = $qry_inv_mesin->result();		
		//var_dump($rs);die();
		foreach ($rs as $row){
			$responce = array("sts" => $row['status'], "desc" => $row['description']);
		}  
		echo json_encode($responce);
		
	
	
	
	
	}
	
	function proses_bc40_invbc40(){
	
		$this->load->library('arraytoxml');
		$uid = $this->session->userdata('DX_username');
		$postData = (isset($_POST['postData']) && !empty($_POST['postData']))?$_POST['postData']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan or not selected"}');
		
		$array = json_decode($postData);
		$array = objectToArray($array);
		
		$postdata = $this->arraytoxml->toXml($array);
		//$xxx = str_replace("&Acirc;&nbsp;", "", $postdata);		
		$search = array('&acirc;','&acirc;','&acirc;','&acirc;','&Acirc;&pound;','&Acirc;&not;','&acirc;&cent;','&Acirc;','&nbsp;'
		,'&Acirc;&nbsp;','&lt;font color="red"&gt;new&lt;/font&gt;');
		$xxx = str_replace($search, "", $postdata);
		$typebc = 'BC40';	
		//echo $xxx1;
		/* $typebc = 'BC40';	
 	 	 echo "  exec('[spi_proses_inv_bc40]  @username=''$uid'', @xmldata=''$xxx'', @typebc=''$typebc'' ')
			 ";
		 die(); 
		 */
		$uid = $this->session->userdata('DX_username');
		$qry_inv_mesin = $this->db->query("
			
			BEGIN TRY
			
			 exec('[spi_proses_inv_bc40]  @username=''$uid'', @xmldata=''$xxx'', @typebc=''$typebc'' ')
			
			END TRY
			BEGIN CATCH 
				SELECT 
				'06' AS status
				,ERROR_MESSAGE() AS description;
				
			END CATCH;
			
			");
		$rs = $qry_inv_mesin->result_array();
        //$r = $qry_inv_mesin->result();		
		//var_dump($rs);die();
		foreach ($rs as $row){
			$responce = array("sts" => $row['status'], "desc" => $row['description']);
		}  
		echo json_encode($responce);
	
	
	}
	
 }