<?php
class CT_bc262 extends CI_Controller{
	function __construct()
	{
	  parent::__construct();
		if (!$this->dx_auth->is_logged_in()) {
			header('HTTP/1.1 401 Unauthorize');die('expire'); 
		}
		
		$uid = $this->session->userdata('DX_user_id');
		
		if(!permissionAuth(2) && $uid != RID){ 
			header('HTTP/1.1 401 Unauthorize');die('noaccess');
		}
		
		$this->load->model("pasi_exim/mdl_exim", 'pasi_mdl_exim');
		$this->db_pasi =$this->load->database('pasi', TRUE);
	}

	function index(){
		
		$this->load->view('pasi_exim/view_bc262');
	
	}
	
	function view_bc40form(){
	
		$this->load->view('pasi_exim/view_bc262form',$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_bc262_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 == "CAR" ) {
						$searchString = $k->data;
						$where .= " and CAR like  '%".$searchString."%'"; 	
					} else if ( $k->field == "KdKpbc" ) {
						$searchString = $k->data;
						$where .= " and KdKpbc like  '%".$searchString."%'";	
					} 
				}
			}
		}
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->pasi_mdl_exim->bc262hdrdataAll($limit,$start,$sidx,$sord, $where);
		$count = $this->pasi_mdl_exim->bc262hdrdataCountAll($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_bc262_hdr']=$r->id_bc262_hdr; 
			$responce->rows[$i]['CAR']=$r->CAR; 
			$responce->rows[$i]['PasokNama']=$r->PasokNama;
			$responce->rows[$i]['Nodaf']=$r->Nodaf;
			$responce->rows[$i]['Tgldaf']=$r->Tgldaf;
			$responce->rows[$i]['NilSerahUsd']=$r->NilSerahUsd; 

			$i++;
		}
		echo json_encode($responce);
	}
	
	function loaddatadetail(){
		$car_detail 	= (isset($_POST['car']) && !empty($_POST['car']))?$_POST['car']:die('{"sts":"ERROR","desc":"Param Tidak Ditemukan"}');
//		echo $car_detail;die();
		
		$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_bc262_detail'; // get index row - i.e. user click to sort
		$sord 	= isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		
			
		$where = " CAR = '$car_detail' ";
		
		if ($search == 'true'){
			if (!empty($filters)){
				$array = json_decode($filters);
				foreach($array->rules as $arr=>$k){

					if ( $k->field == "CAR" ) {
						$searchString = $k->data;
						$where .= " and CAR like  '%".$searchString."%'"; 	
					} else if ( $k->field == "NoBc27Asal" ) {
						$searchString = $k->data;
						$where .= " and NoBc27Asal like  '%".$searchString."%'";	
					} 
				}
			}
		}
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->pasi_mdl_exim->bc262detildataAll($limit,$start,$sidx,$sord, $where);
		$count = $this->pasi_mdl_exim->bc262detildataCountAll($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_bc262_detail']=$r->id_bc262_detail; 
			$responce->rows[$i]['CAR']=$r->CAR; 
			$responce->rows[$i]['NoUrut']=$r->NoUrut;
			$responce->rows[$i]['NoHs']=$r->NoHs;
			$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]['FobDtl']=$r->FobDtl;
			$responce->rows[$i]['HrgFob']=$r->HrgFob;
			$responce->rows[$i]['Diskon']=$r->Diskon;
			$responce->rows[$i]['Freigth']=$r->Freigth;
			$responce->rows[$i]['JmlSat']=$r->JmlSat;
			$responce->rows[$i]['Asuransi']=$r->Asuransi;
			$responce->rows[$i]['KdSat']=$r->KdSat; 
			$responce->rows[$i]['HrgCifUsd']=$r->HrgCifUsd; 
			$responce->rows[$i]['HrgSat']=$r->HrgSat;
			$responce->rows[$i]['HrgCifRp']=$r->HrgCifRp;
			$responce->rows[$i]['KemasJm']=$r->KemasJm;
			$responce->rows[$i]['KemasJn']=$r->KemasJn;						
			$responce->rows[$i]['NettoDtl']=$r->NettoDtl; 
			$responce->rows[$i]['BmRp']=$r->BmRp; 
			$responce->rows[$i]['PpnbmRp']=$r->PpnbmRp;
			$responce->rows[$i]['PpnRp']=$r->PpnRp;
			$responce->rows[$i]['PphRp']=$r->PphRp;
			$responce->rows[$i]['CukaiRp']=$r->CukaiRp;			

			$i++;	
			
		}
		echo json_encode($responce);
	}

	function updatebc262header(){
		
		$username = $this->session->userdata('DX_username');
		$bc_status='BC 262';
		
		$CAR=$_REQUEST['CAR'];
		$KdKpbc=$_REQUEST['KdKpbc'];
		$KdKpbcAwas=$_REQUEST['KdKpbcAwas'];
		$PasokNama=$_REQUEST['PasokNama'];
		$PasokAlmt1=$_REQUEST['PasokAlmt1'];
		$KdVal=$_REQUEST['KdVal'];
		$Cif=$_REQUEST['Cif'];
		$Bruto=$_REQUEST['Bruto'];
		$Netto=$_REQUEST['Netto'];
		$Nodaf=$_REQUEST['Nodaf'];
		$Tgldaf=$_REQUEST['Tgldaf'];
		$UsahaNama=$_REQUEST['UsahaNama'];
		$UsahaNPWP=$_REQUEST['UsahaNPWP'];
		$UsahaAlamat1=$_REQUEST['UsahaAlamat1'];
		$UsahaAlamat2=$_REQUEST['UsahaAlamat2'];
		$UsahaSkep=$_REQUEST['UsahaSkep'];
		$PasokAlmt2=$_REQUEST['PasokAlmt2'];
		$PasokNPWP=$_REQUEST['PasokNPWP'];
		$NoPackinglist=$_REQUEST['NoPackinglist'];
		$TgPackinglist=$_REQUEST['TgPackinglist'];
		$NoKontrak=$_REQUEST['NoKontrak'];
		$TgKontrak=$_REQUEST['TgKontrak'];
		$SuratKep=$_REQUEST['SuratKep'];
		$TgSuratKep=$_REQUEST['TgSuratKep'];
		$SuratLain=$_REQUEST['SuratLain'];
		$TgSuratLain=$_REQUEST['TgSuratLain'];
		$Nopol=$_REQUEST['Nopol'];
		$JnsAngkutan=$_REQUEST['JnsAngkutan'];
		$Ndpbm2=$_REQUEST['Ndpbm2'];
		$CifRp=$_REQUEST['CifRp'];
		$KemasJn=$_REQUEST['KemasJn'];
		$KemasJm=$_REQUEST['KemasJm'];
		$KemasMrk=$_REQUEST['KemasMrk'];
		$Volume=$_REQUEST['Volume'];
		$NilSerahUsd=$_REQUEST['NilSerahUsd'];
		$NilSerah=$_REQUEST['NilSerah'];
		$JnsJamin=$_REQUEST['JnsJamin'];
		$NoJamin=$_REQUEST['NoJamin'];
		$NilJamin=$_REQUEST['NilJamin'];
		$TempoJamin=$_REQUEST['TempoJamin'];
		$Penjamin=$_REQUEST['Penjamin'];
		$BuktiPenjamin=$_REQUEST['BuktiPenjamin'];
	  
 		$datacsv = '';
		
		$datacsv .= $CAR.','.$KdKpbc.','.$KdKpbcAwas.','.$PasokNama.','.$PasokAlmt1.','.$KdVal.','.$Cif.','.$Bruto.','.$Netto.','.$Nodaf.','.$Tgldaf.','.$UsahaNama.','.$UsahaNPWP.','.$UsahaAlamat1.','.$UsahaAlamat2.
					','.$UsahaSkep.','.$PasokAlmt2.','.$PasokNPWP.','.$NoPackinglist.','.$TgPackinglist.','.$NoKontrak.','.$TgKontrak.','.$SuratKep.','.$TgSuratKep.','.$SuratLain.','.$TgSuratLain.','.$Nopol.
					','.$JnsAngkutan.','.$Ndpbm2.','.$CifRp.','.$KemasJn.','.$KemasJm.','.$KemasMrk.','.$Volume.','.$NilSerahUsd.','.$NilSerah.','.$JnsJamin.','.$NoJamin.','.$NilJamin.
					','.$TempoJamin.','.$Penjamin.','.$BuktiPenjamin;

		//echo $datacsv;die();
		//echo "EXEC ('sp_updatebc @id_car=''$CAR'', @datadiff=''$datacsv'', @cmd=''proses'', @bc=''$bc_status'',@username=''$username''  ')";die();
		
		$qry = $this->db_pasi->query("
		
			BEGIN TRY
			
				EXEC ('sp_updatebc_header @id_car=''$CAR'', @datadiff=''$datacsv'', @cmd=''proses'', @bc=''$bc_status'',@username=''$username''  ') 
			END TRY
			BEGIN CATCH 
				SELECT 
				'error' AS STATUS
				,ERROR_MESSAGE() AS DESCRIPTION;
				
			END CATCH;
			
		 ");
		 $rs = $qry->result_array();
         //$r = $qry->result();		
		 //var_dump($r);die();
		foreach ($rs as $row){
			$responce = array("sts" => $row['STATUS'], "desc" => $row['DESCRIPTION']);
		}  
		echo json_encode($responce);
	}

	function updatebc262detail(){
	
		$username = $this->session->userdata('DX_username');
		$bc_status='BC 262';
		
		$id_detail_bc262 = $_REQUEST['idbcdetail']; 		
		$CAR 	= $_REQUEST['car'];
		$no_urut = $_REQUEST['NoUrut'];
		$NoHs = $_REQUEST['NoHs'];
		$BrgUrai = $_REQUEST['BrgUrai'];
		$Merk = $_REQUEST['Merk'];
		$Tipe = $_REQUEST['Tipe'];
		$SpfLain = $_REQUEST['SpfLain'];
		$KdBrg = $_REQUEST['KdBrg'];
		$FobDtl = $_REQUEST['FobDtl'];
		$HrgFob = $_REQUEST['HrgFob']; 
		$Diskon = $_REQUEST['Diskon'];
		$freigth = $_REQUEST['freigth'];
		$JmlSat = $_REQUEST['JmlSat'];
		$Asuransi = $_REQUEST['Asuransi'];
		$KdSat = $_REQUEST['KdSat'];
		$HrgCifUsd = $_REQUEST['HrgCifUsd'];
		$HrgSat = $_REQUEST['HrgSat'];
		$HrgCifRp = $_REQUEST['HrgCifRp'];
		$KemasJm = $_REQUEST['KemasJm'];
		$KemasJn = $_REQUEST['KemasJn'];
		$NettoDtl = $_REQUEST['NettoDtl'];
		$BmRp = $_REQUEST['BmRp'];
		$PpnbmRp = $_REQUEST['PpnbmRp'];
		$PpnRp = $_REQUEST['PpnRp'];
		$PphRp = $_REQUEST['PphRp'];
		$CukaiRp = $_REQUEST['CukaiRp'];

		
 		$datacsv = '';
		$datacsv .= $CAR.','.$BrgUrai.','.$Merk.','.$Tipe.','.$SpfLain.','.$KdBrg.','.$KdSat.','.$JmlSat.','.$NettoDtl.','.$no_urut.','.$NoHs.','.$FobDtl.','.$HrgFob.','.$Diskon.','.$freigth.','.$Asuransi.','.$HrgCifUsd.
					','.$HrgSat.','.$HrgCifRp.','.$KemasJm.','.$KemasJn.','.$BmRp.','.$PpnbmRp.','.$PpnRp.','.$PphRp.','.$CukaiRp;
	  
		//echo $datacsv;die();
		//echo "EXEC ('sp_updatebc @id_car=''$CAR'', @datadiff=''$datacsv'', @cmd=''proses'', @bc=''$bc_status'',@username=''$username''  ')";die();
		
		$qry = $this->db_pasi->query("
		
			BEGIN TRY
			
				EXEC ('sp_updatebc_detail @id_detailbc=$id_detail_bc262,@id_car=''$CAR'', @datadiff=''$datacsv'', @cmd=''proses'', @bc=''$bc_status'',@username=''$username''  ') 
			END TRY
			BEGIN CATCH 
				SELECT 
				'error' AS STATUS
				,ERROR_MESSAGE() AS DESCRIPTION;
				
			END CATCH;
			
		 ");
		 $rs = $qry->result_array();
         //$r = $qry->result();		
		 //var_dump($r);die();
		foreach ($rs as $row){
			$responce = array("sts" => $row['STATUS'], "desc" => $row['DESCRIPTION']);
		}  
		echo json_encode($responce);
		
	
	}	
  
 }