<?php
class CT_inv_draft 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_bc23", 'mdl_bc23');
		$this->load->model("exim/mdl_invoice", 'mdl_invoice');
		$this->load->helper('afunction');
	}

	function index(){
		$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_inv_draft', $data);
	
	}
	
	function loaddatadownloaded(){
		$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_upload'; // get index row - i.e. user click to sort
        $sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		
		$tipe = (isset($_POST['tipe']) && !empty($_POST['tipe']))?$_POST['tipe']:die('{"sts":"ERROR","desc":" Param tipe Tidak Ditemukan"}');
		
		$where = " 1=1 and tipe='$tipe' ";
		
		if ($search == 'true'){
			if (!empty($filters)){
				$array = json_decode($filters);
				foreach($array->rules as $arr=>$k){
					
					if ( $k->field == "filename" ) {
						$searchString = $k->data;
						$where .= " and filename like  '%".$searchString."%'"; 
					} else if ( $k->field == "date_upload" ) {
						$searchString = $k->data;
						$where .= " and date_upload  =  '$searchString'"; 
					} else if ( $k->field == "status_upload" ) {
						$searchString = $k->data;
						$where .= " and status_upload like  '%".$searchString."%'"; 
					}  else if ( $k->field == "create_by" ) {
						$searchString = $k->data;
						$where .= " and create_by like  '%".$searchString."%'"; 
					} 
				}
			}
		}
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_invoice->upload_invoiceAll($limit,$start,$sidx,$sord, $where);
        $count = $this->mdl_invoice->upload_invoicecountAll($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_invoice_upload']=$r->id_invoice_upload; 
			$responce->rows[$i]['date_upload']=$r->date_upload; 
			$responce->rows[$i]['filename']=$r->filename; 
			$responce->rows[$i]['status_upload']=$r->status_upload;
			$responce->rows[$i]['create_date']=$r->create_date;
			$responce->rows[$i]['create_by']=$r->create_by;
			$responce->rows[$i]['tipe']=$r->tipe;
		    $responce->rows[$i]['catatan']=$r->catatan;
			
			$i++;
		}
		echo json_encode($responce);
	}
	
	
	function cekloaderbc23(){
		$filename = (isset($_POST['filename']) && !empty($_POST['filename']))?$_POST['filename']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$tipe = (isset($_POST['tipe']) && !empty($_POST['tipe']))?$_POST['tipe']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$id_invoice_upload = (isset($_POST['id_invoice_upload']) && !empty($_POST['id_invoice_upload']))?$_POST['id_invoice_upload']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		

		$apppath = $this->config->item('APP_upload_path');
		$path=$apppath.'invoice/hib/'.$filename;
		
		//echo $filename; die();
		
		if (!file_exists($path)) {
			
			$responce = array("sts" => '06', "desc" => " Filename ".$filename." tidak ditemukan");
			echo json_encode($responce);
			die();
		}
		
		
		$fh = fopen($path, 'r');
		$theData = fread($fh, 4);
		fclose($fh);
		$theData=preg_replace('/\s+/', '', $theData);  
			
		$ketemu = strpos($theData, 'R1');
		
		if($ketemu === false){
			$responce = array("sts" => '06', "desc" => " Format Data Salah, Lakukan Upload Ulang");
			echo json_encode($responce);
			die();			
		
		}
		//echo " EXEC ('sp_insert_data_hib @filename=''$path'' ') ";
		//die();
				
		$qry = $this->db->query("
		
			BEGIN TRY
				EXEC ('sp_insert_data_hib @filename=''$path'', @file=''$filename'' ')
			END TRY
			BEGIN CATCH 
				SELECT 
				'error' AS status
				,ERROR_MESSAGE() AS description;
				
			END CATCH;
			
			
				
		");
		
		//EXEC sp_insert_data_hib
			
		$rs = $qry->result_array();	
		
		//var_dump($rs);
		//die();
		foreach ($rs as $row){
			$responce = array("sts" => '00', "desc" => $row['description']);
		}  
		echo json_encode($responce);
	}
	
	function loaddata(){
		
		
		$search = isset($_POST['_search'])?$_POST['_search']:false;
        $searchField = isset($_POST['searchField'])?$_POST['searchField']:'';
        $searchString = isset($_POST['searchString'])?$_POST['searchString']:'';
        $filters =  isset($_POST['filters'])?$_POST['filters']:'';
		
		$page = isset($_POST['page'])?$_POST['page']:1; // get the requested page
        $limit = isset($_POST['rows'])?$_POST['rows']:20; // get how many rows we want to have into the grid
        $sidx = isset($_POST['sidx'])?$_POST['sidx']:'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 = " 1=1 ";
		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 ";
		}
		
		
		
		$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){ $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 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 view_bc23_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_bc23_prosesinsertinv',$data);
	
	}
	
	function ceklastcar(){
	
		$bc23dir = $this->config->item('APP_modulbc23');
		$dokno = (isset($_POST['nobl']) && !empty($_POST['nobl']))?$_POST['nobl']:die('{"sts":"ERROR","desc":" Param no bl Tidak Ditemukan"}');;
		
		$lascarx = "-";
		$this->db->query("
			SET ANSI_NULLS ON
			SET ANSI_WARNINGS ON
		");
		
		$query = $this->db->query("
		
			select max(car) as maxofcar from 
			OPENDATASOURCE('Microsoft.ACE.OLEDB.12.0',
						'Data Source=$bc23dir;
						Jet OLEDB:Database Password=MumtazFarisHana;Persist Security Info=False;'
						)...tblBC23Dok
			 where dbo.trim(DokNo) = '$dokno' 
		");
		$rs = $query->result_array();
		
		
		foreach ($rs as $row){
		
			$lascarx = $row['maxofcar'];
		
		}
		
		if($lascarx != ''){
			echo $lascar;
			echo "<br />";
			echo "NO BL Sudah ada dengan nomor CAR: $lascarx";
			echo "<br />";
			die();	
		}
		
		
		$lascar = "-";
		$this->db->query("
			SET ANSI_NULLS ON
			SET ANSI_WARNINGS ON
		");
		
		$query = $this->db->query("
		
			select max(car) as lastofcar from  
			OPENDATASOURCE('Microsoft.ACE.OLEDB.12.0',
						'Data Source=$bc23dir;
						Jet OLEDB:Database Password=MumtazFarisHana;Persist Security Info=False;'
						)...tblBC23Hdr
			
		");
		$rs = $query->result_array();
		
		
		foreach ($rs as $row){
		
			$lascar = $row['lastofcar'];
		
		}
		
		$noajunext = substr($lascar, 20, 6) * 1 + 1; 
		$noreg = $this->config->item('APP_noregbc23');
		$nextcar = $noreg.date("Ymd").sprintf('%06d', $noajunext);
		echo $lascar;
		echo "<br />";
		echo "Kemungkinan aju Selanjutnya: $nextcar";
		echo "<br />";
		
		/*$bc23dir = $this->config->item('APP_modulbc23');
		$conn = new COM("ADODB.Connection") or die("Cannot start ADO");  
		$conn->Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=$bc23dir;Password=MumtazFarisHana;");
		$q = "select max(car) as lastofcar from tblBC23Hdr ";
		$rs = $conn->Execute($q) or die("Error in query: $q. " . $conn->ErrorMsg(). "in LINE __" . __LINE__);
		$lascar = @$rs->Fields("lastofcar")->value;
		$noajunext = substr($lascar, 20, 6) * 1 + 1; 
		$noreg = $this->config->item('APP_noregbc23');
		$nextcar = $noreg.date("Ymd").sprintf('%06d', $noajunext);
		echo $lascar;
		echo "<br />";
		echo "Kemungkinan aju Selanjutnya: $nextcar";
		echo "<br />";
		$rs->Close();
		$conn->Close();

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

	}
	
	function ceknocarnobl(){
		$bc23dir = $this->config->item('APP_modulbc23');
		$dokno = (isset($_POST['dokno']) && !empty($_POST['dokno']))?$_POST['dokno']:die('{"sts":"ERROR","desc":" Param no Tidak Ditemukan"}');;
		$doktg = (isset($_POST['doktg']) && !empty($_POST['doktg']))?$_POST['doktg']:die('{"sts":"ERROR","desc":" Param date Tidak Ditemukan"}');;
		
		$lascar = "-";
		$this->db->query("
			SET ANSI_NULLS ON
			SET ANSI_WARNINGS ON
		");
		
		$query = $this->db->query("
		
			select max(car) as maxofcar from 
			OPENDATASOURCE('Microsoft.ACE.OLEDB.12.0',
						'Data Source=$bc23dir;
						Jet OLEDB:Database Password=MumtazFarisHana;Persist Security Info=False;'
						)...tblBC23Dok
			 where dbo.trim(DokNo) = '$dokno' 
		");
		$rs = $query->result_array();
		
		
		foreach ($rs as $row){
		
			$lascar = $row['maxofcar'];
		
		}
		
		foreach ($rs as $row){
			$responce = array("sts" => '00', "desc" => $lascar);
		}  
		echo json_encode($responce);
	
		
		
	}
	
	function sendtomodulbc23manual(){
		$id_bc_in_hdr = (isset($_POST['id_bc_in_hdr']) && !empty($_POST['id_bc_in_hdr']))?$_POST['id_bc_in_hdr']:die('{"sts":"ERROR","desc":" Param ID Tidak Ditemukan"}');
		$car = (isset($_POST['car']) && !empty($_POST['car']))?$_POST['car']:die('{"sts":"ERROR","desc":" Param car Tidak Ditemukan"}');
		$tgdaft = (isset($_POST['tgdaft']) && !empty($_POST['tgdaft']))?$_POST['tgdaft']:'';
		$nodaft = (isset($_POST['nodaft']) && !empty($_POST['nodaft']))?$_POST['nodaft']:'';
		//$userid=$this->session->userdata('DX_user_id');
		$userid=$this->session->userdata('DX_username');
		$qryinsmodul = $this->db->query("
			BEGIN TRY
				exec('sp_insertmodulbc23_manual @car=''$car'', @idx=$id_bc_in_hdr, @nodaft=''$nodaft'',@tgdaft=''$tgdaft'',  @userid=''$userid'' ')
			END TRY
			BEGIN CATCH 
				SELECT 
				'08' AS status
				,ERROR_MESSAGE() AS hasil;
				
			END CATCH;	
			
		");
		$rs = $qryinsmodul->result_array();	
		//var_dump($rs);
		//die();
		foreach ($rs as $row){
			$responce = array("sts" => $row['status'], "desc" => $row['hasil']);
		}  
		echo json_encode($responce);
	
	
	}
	
	function view_bc23_nonhib(){
	
		$q = $this->db->query("
			SELECT    v_nonhib_inv_dtl.INVOICE_NO, v_nonhib_inv_dtl.partnumberx, tbl_data_part_hs.part_no
			FROM         v_nonhib_inv_dtl LEFT OUTER JOIN
								  tbl_data_part_hs ON v_nonhib_inv_dtl.partnumberx = tbl_data_part_hs.part_no
			where tbl_data_part_hs.part_no is null
			group by v_nonhib_inv_dtl.INVOICE_NO, v_nonhib_inv_dtl.partnumberx, tbl_data_part_hs.part_no
		");
		$rs = $q->result_array();
		$datanull='';
		foreach ($rs as $row){
			$datanull.=$row['INVOICE_NO'].'|';		
		}
		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_bc23_nonhib',$data);
	
	}
	
	function loaddata_bc23_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 = " 1=1 ";
		
		if ($search == 'true'){
			if (!empty($filters)){
				$array = json_decode($filters);
				foreach($array->rules as $arr=>$k){
					
					if ( $k->field == "noinv" ) {
						$searchString = $k->data;
						$where .= " and noinv like  '%".$searchString."%'"; 
					} else if ( $k->field == "tglinv" ) {
						$searchString = $k->data;
						$where .= " and tglinv  =  '$searchString'"; 
					} else if ( $k->field == "tglinv" ) {
						$searchString = $k->data;
						$where .= " and tglinv  =  '$searchString'"; 
					} else if ( $k->field == "nobl" ) {
						$searchString = $k->data;
						$where .= " and nobl like  '%".$searchString."%'"; 
					}  else if ( $k->field == "status" ) {
						$searchString = $k->data;
						if($searchString !=''){
							$where .= " and status =  '$searchString' "; 
						} else { 
							$where .= "  and status =  'New' ";  
						}
						if ($searchString=='2'){
							$where = ' 1=1 ';
						}
					} 
				}
			}
		} else {
			$where .= "  and status =  'New' ";
		}
		
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_invoice->bc23datanonhibhdrAll($limit,$start,$sidx,$sord, $where);
        $count = $this->mdl_invoice->bc23datanonhibhdrcountAll($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 execdatanonhibbc23(){
	
	
			
		
		$qry = $this->db->query("
		
			BEGIN TRY
			
			 exec(' sp_insert_data_non_hib ')
			
			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'].' File/Files Loaded');
		}  
		echo json_encode($responce);
	
	}
	
	function view_bc23_nonhibdtl(){
		$id_datanonhibbc23 = (isset($_POST['id_datanonhibbc23']) or !empty($_POST['id_datanonhibbc23']))?$_POST['id_datanonhibbc23']:die('{"sts":"ERROR","desc":" Param ID Tidak Ditemukan"}');
		$data["id_datanonhibbc23"] = $id_datanonhibbc23;
		
		$q = $this->db->query("
			SELECT    v_nonhib_inv_dtl.id_nonhib_hdr, v_nonhib_inv_dtl.partnumberx, tbl_data_part_hs.part_no
			FROM         v_nonhib_inv_dtl LEFT OUTER JOIN
								  tbl_data_part_hs ON v_nonhib_inv_dtl.partnumberx = tbl_data_part_hs.part_no
			where id_nonhib_hdr = $id_datanonhibbc23 and tbl_data_part_hs.part_no is null
			group by v_nonhib_inv_dtl.id_nonhib_hdr, v_nonhib_inv_dtl.partnumberx, tbl_data_part_hs.part_no
		");
		$rs = $q->result_array();
		$datanull='';
		foreach ($rs as $row){
			$datanull.=$row['partnumberx'].'|';		
		}
		if ($datanull != ''){
			$data["test"] = 'Data Part Tidak Match dengan Part HS. Mohon Ditambahkan di Master Data : ' . $datanull;
		} else {
			$data["test"]=$datanull;
		}
		
		$this->load->view('exim/view_bc23_nonhibdtl', $data);
	
	}
	
	function loaddata_viewdtl_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']:'tbl_data_out_hdr.id_data_out_hdr'; // get index row - i.e. user click to sort
		$sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		
		$id = (isset($_POST['id_datanonhibbc23']) or !empty($_POST['id_datanonhibbc23']))?$_POST['id_datanonhibbc23']:die('{"sts":"ERROR","desc":" Param ID Tidak Ditemukan"}');
		
		$where = " id_nonhib_hdr=$id ";
		
	
		if ($search == 'true'){
			if (!empty($filters)){
				$array = json_decode($filters);
				foreach($array->rules as $arr=>$k){
					
					if ( $k->field == "CONTNO" ) {
						$searchString = $k->data;
						$where .= " and CONTNO like  '%".$searchString."%'"; 
					} else if ( $k->field == "INVOICE_NO" ) {
						$searchString = $k->data;
						$where .= " and INVOICE_NO like  '%".$searchString."%'"; 
					} else if ( $k->field == "partnumber" ) {
						$searchString = $k->data;
						$where .= " and partnumber like  '%".$searchString."%'"; 
					} else if ( $k->field == "partnamecustoms" ) {
						$searchString = $k->data;
						$where .= " and partnamecustoms like  '%".$searchString."%'"; 
					} else if ( $k->field == "INVQTY" ) {
						$searchString = $k->data;
						$where .= " and INVQTY = $searchString "; 
					} else if ( $k->field == "PACK" ) {
						$searchString = $k->data;
						$where .= " and PACK =  $searchString"; 
					} else if ( $k->field == "CNO" ) {
						$searchString = $k->data;
						$where .= " and CNO like  '%".$searchString."%'"; 
					} else if ( $k->field == "ORDERNO" ) {
						$searchString = $k->data;
						$where .= " and ORDERNO like  '%".$searchString."%'"; 
					} 
					else if ( $k->field == "partname" ) {
						$searchString = $k->data;
						$where .= " and partname like  '%".$searchString."%'"; 
					} 
				}
			}
		}
		
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_invoice->bc23datanonhibdtlAll($limit,$start,$sidx,$sord, $where);
        $count = $this->mdl_invoice->bc23datanonhibdtlcountAll($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_nonhib_dtl']=$r->id_nonhib_dtl; 
			$responce->rows[$i]['id_nonhib_hdr']=$r->id_nonhib_hdr; 
			$responce->rows[$i]['INVOICE_NO']=$r->INVOICE_NO; 
			$responce->rows[$i]['partnumber']=$r->partnumber;
			$responce->rows[$i]['CONTNO']= $r->CONTNO;
			$responce->rows[$i]['INVQTY']=$r->INVQTY;
			$responce->rows[$i]['PACK']=$r->PACK;
			$responce->rows[$i]['UOMPFX']=$r->UOMPFX;
			$responce->rows[$i]['CNO']=$r->CNO;
			$responce->rows[$i]['ORDERNO']=$r->ORDERNO;
			$responce->rows[$i]['VENDOR']=$r->VENDOR;
			$responce->rows[$i]['partname']=$r->partname;
			$responce->rows[$i]['partnamecustoms']=$r->partnamecustoms;
			$i++;
		}
		echo json_encode($responce);
		
	
	
	
	}
	
	function prosesdatabl(){
		$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 ('spi_invoiceupdatebl @datadiff=''$datacsv'', @nobl=''$nobl'', @tgbl=''$tgbl'', @cmd=''proses''  ') 
			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(){
		$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']) or !empty($_POST['datadiff']))?$_POST['datadiff']:'0'; 
		$datadiff=json_decode($datadiff);
		$datacsv = '';
		foreach ($datadiff as $data ){
			$datacsv .= $data->inv_hdr_id . ',' . $data->invno . ',' . '-' . ',' . '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 @datadiff=''$datacsv'', @nobl=''$nobl'', @tgbl=''$tgbl'', @cmd=''cancel'' ') 
			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 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 updatestatusproses(){
		$typebc = 'BC23';
		$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 = 'BC23';
		$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 = 'BC23';
		$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_insertcb23_recalculate_new_inv_bl] @nobl = N'$nobl', @tgbl = '$tgbl', @typebc = N'$typebc',@noinv = '$listinv'";
		 // var_dump($noinv); die();
		$typebc = 'BC23';
		$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 loaddata_bc23_nonhib_nonobl(){
		$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_invoice->bc23datanonhibhdrAll($limit,$start,$sidx,$sord, $where);
        $count = $this->mdl_invoice->bc23datanonhibhdrcountAll($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 view_bc23_nonhib_summary(){
		$id_datanonhibbc23 = (isset($_POST['id_datanonhibbc23']) or !empty($_POST['id_datanonhibbc23']))?$_POST['id_datanonhibbc23']:die('{"sts":"ERROR","desc":" Param ID Tidak Ditemukan"}');
		$data["id_datanonhibbc23"] = $id_datanonhibbc23;
		$this->load->view('exim/view_bc23_nonhib_summary', $data);
	
	}
	
	function loaddata_viewdtl_nonhib_summary(){
		$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']:'tbl_data_out_hdr.id_data_out_hdr'; // get index row - i.e. user click to sort
		$sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		$id = (isset($_POST['id_datanonhibbc23']) or !empty($_POST['id_datanonhibbc23']))?$_POST['id_datanonhibbc23']:die('{"sts":"ERROR","desc":" Param ID Tidak Ditemukan"}');
		
		$where = " id_nonhib_hdr=$id ";
		
	
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_invoice->datanonhibsummary_all($limit,$start,$sidx,$sord, $where);
        $count = $this->mdl_invoice->datanonhibsummary_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]['partname']=$r->partname; 
			$responce->rows[$i]['INVOICE_NO']=$r->INVOICE_NO; 
			$responce->rows[$i]['sumqty']=$r->sumqty;
			$responce->rows[$i]['sumpack']= $r->sumpack;
			
			$i++;
		}
		echo json_encode($responce);
	
	}
	
	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 = 'BC23';
		$datanoinv=json_decode($datanoinv);
		$datacsv = '';
		foreach ($datanoinv as $data ){
			$datacsv .= $data->id_nonhib_hdr . ',' . trim($data->noinv); 
			$datacsv .= "|";
		}
		// echo $datacsv;	
		// var_dump($datanoinv); die();
		//echo " EXEC [sp_nonhib_insert] @datanoinv ='$datacsv', @typebc='$typebc', @tgbl='$tgbl', @nobl='$nobl'";
		//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 = 'BC23';
		$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_bc23_nonhibaddbldata(){
	
		$this->load->view('exim/view_bc23_nonhibaddbldata');
	
	}
	
	function prosesdatabl_nonhib(){
		$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->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"}');
			$blawbno = (isset($data->nobl) && !empty($data->nobl ))?$data->nobl :die('{"sts":"ERROR","desc":" Param nobl Tidak Ditemukan"}');
			$tglblawb = (isset($data->tgbl) && !empty($data->tgbl ))?$data->tgbl :die('{"sts":"ERROR","desc":" Param tgbl Tidak Ditemukan"}');
			
			$datacsv .= $inv_hdr_id . ',' . $invno . ',' . $blawbno . ',' . $tglblawb; 
			$datacsv .= "|";
		}
		// echo $datacsv;	
		// var_dump($datadiff); die();
		
		
		$qry = $this->db->query("
		
			 EXEC ('spu_nonhib_bl @datadiff=''$datacsv'', @nobl=''$nobl'', @tgbl=''$tgbl'', @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 canceldatabl_nonhib(){
		$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']) or !empty($_POST['datadiff']))?$_POST['datadiff']:'0'; 
		$datadiff=json_decode($datadiff);
		$datacsv = '';
		foreach ($datadiff as $data ){
			$datacsv .= $data->id_nonhib_hdr . ',' . $data->noinv . ',' . '-' . ',' . '1900-10-23'; 
			$datacsv .= "|";
		}
		 // echo $datacsv;	
		// var_dump($datadiff); die();
		// echo "
		
			 // EXEC ('spu_nonhib_bl @datadiff=''$datacsv'', @nobl=''$nobl'', @tgbl=''$tgbl'' ') 
			
		 // ";
		 // die();
		$qry = $this->db->query("
		
			 EXEC ('spu_nonhib_bl @datadiff=''$datacsv'', @nobl=''$nobl'', @tgbl=''$tgbl'', @cmd=''cancel'' ') 
			
		 ");
		$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 recalculatedatabl_nonhib(){
	
		$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['noinv']) && !empty($_POST['noinv']))?$_POST['noinv']:'0'; 
		$datadiff=json_decode($datadiff);
		
		$datacsv = '';
		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"}');
			$blawbno = (isset($data->nobl) && !empty($data->nobl ))?$data->nobl :die('{"sts":"ERROR","desc":" Param nobl Tidak Ditemukan"}');
			$tglblawb = (isset($data->tgbl) && !empty($data->tgbl ))?$data->tgbl :die('{"sts":"ERROR","desc":" Param tgbl Tidak Ditemukan"}');
			
			$datacsv .= $inv_hdr_id . ',' . $invno . ',' . $blawbno . ',' . $tglblawb; 
			$datacsv .= "|";
		}
		// echo $datacsv;	
		// var_dump($datadiff); die();
		
		// echo " 		
			 // EXEC ('spu_nonhib_bl_recalculate @datadiff=''$datacsv'', @nobl=''$nobl'', @tgbl=''$tgbl''  ') 
			
		 // "; die();
		$qry = $this->db->query("
		
			 EXEC ('spu_nonhib_bl_recalculate @datadiff=''$datacsv'', @nobl=''$nobl'', @tgbl=''$tgbl''  ') 
			
		 ");
		 $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_inv_datauploaded(){
	
		$this->load->view('exim/view_inv_datauploaded');
	}
	
	function view_bc23_addbldata(){
		
			$this->load->view('exim/view_bc23_addbldata');
	
		
	}
	
	
	function view_bc23_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_bc23_nonhibprosesinsertinv', $data);
	}
	
	function data_bc23_inv_bl_blank(){
	
	
	
	}
	
	function view_cek_invoice(){
		$no_invoice = (isset($_POST['no_invoice']) or !empty($_POST['no_invoice']))?$_POST['no_invoice']:die('{"sts":"ERROR","desc":" Param No INV nobl Tidak Ditemukan"}');
		$inv_hdr_id = (isset($_POST['inv_hdr_id']) or !empty($_POST['inv_hdr_id']))?$_POST['inv_hdr_id']:die('{"sts":"ERROR","desc":" Param No INV nobl Tidak Ditemukan"}');
		
		$data['no_invoice'] = $no_invoice;
		$data['inv_hdr_id'] = $inv_hdr_id;
		
		$this->load->view('exim/view_cek_invoice', $data);
	}
	function updatenoinvoice(){
		$noinvoicebaru = (isset($_POST['noinvoicebaru']) or !empty($_POST['noinvoicebaru']))?trim($_POST['noinvoicebaru']):die('{"sts":"ERROR","desc":" Param No INV  Tidak Ditemukan"}');
		$inv_hdr_id = (isset($_POST['inv_hdr_id']) or !empty($_POST['inv_hdr_id']))?trim($_POST['inv_hdr_id']):die('{"sts":"ERROR","desc":" Param id Tidak Ditemukan"}');
		$noinvoicelama = (isset($_POST['noinvoicelama']) or !empty($_POST['noinvoicelama']))?trim($_POST['noinvoicelama']):die('{"sts":"ERROR","desc":" Param No INV LAMA  Tidak Ditemukan"}');
		$bc23dir = $this->config->item('APP_modulbc23');
		
		$this->db->query("
			SET ANSI_NULLS ON
			SET ANSI_WARNINGS ON
		");
		
		
		
		$qry = $this->db->query("
		
		    declare @invoicebaru varchar(100), @invoicelama varchar(100), @inv_hdr_id bigint, @carmodul varchar(100)
			if (select count(*) FROM [dbo].[tbl_inv_hdr]  where [inv_hdr_id]=$inv_hdr_id and r3='$noinvoicelama' and status_inv = 1 and nobl <> '-' ) > 0
			begin
				select '05' as status, ' Data Sudah Di Proses' as description
				return
			end
			
			if (select count(*) FROM [dbo].[tbl_bc_in_dok] where DokNo like '%$noinvoicelama%') > 0
			begin
				select '05' as status, ' Data Sudah Di BC23' as description
				return
			end			
			
						
			update [dbo].[tbl_inv_hdr] set r3 = '$noinvoicebaru' where [inv_hdr_id]=$inv_hdr_id and r3='$noinvoicelama' and status_inv = 0 
			
			select '00' as status, ' Data Updated' as description
			
		 ");
		 $rs = $qry->result_array();
        // $r = $qry->result();		
		// var_dump($r);die();
		foreach ($rs as $row){
			$responce = array("sts" => $row['status'], "desc" => $row['description']);
		}  
		echo json_encode($responce);
		
	}
	
	function cekinvoicemodul(){
		$noinvoicelama = (isset($_POST['noinvoicelama']) or !empty($_POST['noinvoicelama']))?trim($_POST['noinvoicelama']):die('{"sts":"ERROR","desc":" Param No INV LAMA  Tidak Ditemukan"}');
		$bc23dir = $this->config->item('APP_modulbc23');
		
		$this->db->query("
			SET ANSI_NULLS ON
			SET ANSI_WARNINGS ON
		");
		
		
		
		$qry = $this->db->query("
		
		    declare @carmodul varchar(100), @carcc varchar(100)
				
			
			set @carmodul = (select top 1 car from  
			OPENDATASOURCE('Microsoft.ACE.OLEDB.12.0',
						'Data Source=$bc23dir;
						Jet OLEDB:Database Password=MumtazFarisHana;Persist Security Info=False;'
						)...tblBC23Dok where DokNo like '%$noinvoicelama%') 
			
			set @carcc = (
				select top 1 car FROM [dbo].[tbl_bc_in_dok] where DokNo like '%$noinvoicelama%'
			)
			
			select '00' as status, ' Data Find' as description, @carmodul as carmodul, @carcc as carcc
			
		 ");
		 $rs = $qry->result_array();
        // $r = $qry->result();		
		// var_dump($r);die();
		$carcc = '';
		$carmodul = '';
		foreach ($rs as $row){
		    $carcc = $row['carcc'];
			$carmodul = $row['carmodul'];
			if ( trim($carcc) == '' &&  trim($carmodul) != '' ){
			
				$carcc = 'Data di CC masih kosong, bisa ditarik(di matching)';
			} else if ( trim($carcc) == '' &&  trim($carmodul) == '' ){
			
				$carcc = 'Mungkin Invoice Untuk Dokumen BC Lain';
			}
			
			$responce = array("sts" => $row['status'], "desc" => $row['description'], "carmodul" => $carmodul, "carcc" => $carcc );
		}  
		echo json_encode($responce);
		
	}
  
 }