<?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->model("pasi_exim/mdl_exim", 'pasi_mdl_exim');
		$this->db_pasi =$this->load->database('pasi', TRUE);
	}

	function index(){
		
		$this->load->view('pasi_exim/view_bc40');
	
	}
	
	function view_bc40form(){
	
		$this->load->view('pasi_exim/view_bc40form',$data);

	}

	function view_bc40_searching(){
	
		$this->load->view('pasi_exim/view_bc40_searching');
		
	}
	
	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_bc40_hdr'; // get index row - i.e. user click to sort
		$sord 	= isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		
			
		$caritanggal = (isset($_POST['caritanggalbc40']) && !empty($_POST['caritanggalbc40']))?$_POST['caritanggalbc40']:'';
		
		if ($caritanggal=='ya'){	
			$awalperiode = (isset($_POST['awalperiodebc40']) && !empty($_POST['awalperiodebc40']))?$_POST['awalperiodebc40']:'';
			$akhirperiode = (isset($_POST['akhirperiodebc40']) && !empty($_POST['akhirperiodebc40']))?$_POST['akhirperiodebc40']:'';
			
			$awalperiode = date_create($awalperiode);
			$awalperiode = date_format($awalperiode,'Y-m-d');	
			
			$akhirperiode = date_create($akhirperiode);
			$akhirperiode = date_format($akhirperiode,'Y-m-d');
			
			//echo $akhirperiode;die();
			
			$where = " 1 = 1 and tbl_header_bc40.tgldaf BETWEEN '$awalperiode' and '$akhirperiode'  ";
			
		} else {
		
			$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."%'";	
					}  else if ( $k->field == "Tujuan" ) {
						$searchString = $k->data;
						$where .= " and Tujuan like  '%".$searchString."%'";	
					}  else if ( $k->field == "JnsAngkutan" ) {
						$searchString = $k->data;
						$where .= " and JnsAngkutan like  '%".$searchString."%'";	
					}  else if ( $k->field == "KemasJn" ) {
						$searchString = $k->data;
						$where .= " and KemasJn like  '%".$searchString."%'";	
					}  else if ( $k->field == "KemasJm" ) {
						$searchString = $k->data;
						$where .= " and KemasJm like  '%".$searchString."%'";	
					}  else if ( $k->field == "Tgldaf" ) {
						$searchString = $k->data;
						$where .= " and Tgldaf like  '%".$searchString."%'";	
					}  else if ( $k->field == "TgKontrak" ) {
						$searchString = $k->data;
						$where .= " and TgKontrak like  '%".$searchString."%'";	
					}  else if ( $k->field == "PasokNama" ) {
						$searchString = $k->data;
						$where .= " and PasokNama like  '%".$searchString."%'";	
					}  else if ( $k->field == "Nodaf" ) {
						$searchString = $k->data;
						$where .= " and Nodaf like  '%".$searchString."%'";	
					}  else if ( $k->field == "status" ) {
						$searchString = $k->data;
						$where .= " and status like  '%".$searchString."%'";	
					} 

				}
			}
		}
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->pasi_mdl_exim->bc40hdrdataAll($limit,$start,$sidx,$sord, $where);
		$count = $this->pasi_mdl_exim->bc40hdrdataCountAll($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_bc40_hdr']=$r->id_bc40_hdr;
			  $responce->rows[$i]['CAR']=$r->CAR;
			  $responce->rows[$i]['KdKpbc']=$r->KdKpbc;
			  $responce->rows[$i]['Tujuan']=$r->Tujuan;
			  $responce->rows[$i]['JnsAngkutan']=$r->JnsAngkutan;
			  $responce->rows[$i]['KemasJn']=$r->KemasJn;
			  $responce->rows[$i]['KemasJm']=$r->KemasJm;
			  $responce->rows[$i]['KemasMrk']=$r->KemasMrk;
			  $responce->rows[$i]['KdKpbcBongkar']=$r->KdKpbcBongkar;
			  $responce->rows[$i]['PasokNama']=$r->PasokNama;
			  $responce->rows[$i]['PasokAlmt1']=$r->PasokAlmt1;
			  $responce->rows[$i]['Bruto']=$r->Bruto;
			  $responce->rows[$i]['Netto']=$r->Netto;
			  $responce->rows[$i]['NilSerah']=$r->NilSerah;
			  $responce->rows[$i]['Nodaf']=$r->Nodaf;
			  $responce->rows[$i]['Tgldaf']=$r->Tgldaf;
			  $responce->rows[$i]['UsahaNama']=$r->UsahaNama;
			  $responce->rows[$i]['UsahaNPWP']=$r->UsahaNPWP;
			  $responce->rows[$i]['UsahaAlamat1']=$r->UsahaAlamat1;
			  $responce->rows[$i]['UsahaAlamat2']=$r->UsahaAlamat2;
			  $responce->rows[$i]['UsahaSkep']=$r->UsahaSkep;
			  $responce->rows[$i]['PasokAlmt2']=$r->PasokAlmt2;
			  $responce->rows[$i]['PasokNPWP']=$r->PasokNPWP;
			  $responce->rows[$i]['NoPackinglist']=$r->NoPackinglist;
			  $responce->rows[$i]['NoKontrak']=$r->NoKontrak;
			  $responce->rows[$i]['TgKontrak']=$r->TgKontrak;
			  $responce->rows[$i]['SuratKep']=$r->SuratKep;
			  $responce->rows[$i]['TgSuratKep']=$r->TgSuratKep;
			  $responce->rows[$i]['SuratLain']=$r->SuratLain;
			  $responce->rows[$i]['TgSuratLain']=$r->TgSuratLain;
			  $responce->rows[$i]['Nopol']=$r->Nopol;
			  $responce->rows[$i]['Volume']=$r->Volume;
			  $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]['status']=$r->status;

			$i++;
		}
		echo json_encode($responce);
	}
	
	function loaddatadetail(){
		$car_detail 	= (isset($_POST['car']) && !empty($_POST['car']))?$_POST['car']:die('{"sts":"ERROR","desc":"Param Tidak Ditemukan"}');
				
		$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_bc40_hdr'; // 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->bc40detildataAll($limit,$start,$sidx,$sord, $where);
		$count = $this->pasi_mdl_exim->bc40detildataCountAll($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_bc40_detail']=$r->id_bc40_detail; 
			$responce->rows[$i]['CAR']=$r->CAR; 
			$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]['KdSat']=$r->KdSat;
			$responce->rows[$i]['JmlSat']=$r->JmlSat;
			$responce->rows[$i]['NettoDtl']=$r->NettoDtl;
			$responce->rows[$i]['DSerah']=$r->DSerah;
			$i++;
		}
		echo json_encode($responce);
	}
	
	function MoveData2Inv(){
		$idhdrbc40 	= (isset($_POST['id_bc40_hdr']) && !empty($_POST['id_bc40_hdr']))?$_POST['id_bc40_hdr']:die('{"sts":"ERROR","desc":" Param id HDR BC 40 Tidak Ditemukan"}');
		$car 		= (isset($_POST['car']) && !empty($_POST['car']))?$_POST['car']:die('{"sts":"ERROR","desc":" Param Tidak CAR Ditemukan"}');
		$bc ='bc40';
		$username = $this->session->userdata('DX_username');
		
		//echo $car;die();
		//echo "exec('sp_insert_bc_to_incoming_item @idhdrbc40=$idhdrbc40 , @car=''$car'' ,@username=''$username'', @bc=''$bc''')";die();
		
		$qry = $this->db_pasi->query("
			
			BEGIN TRY
				exec('sp_insert_bc_to_incoming_item @idhdrbc=$idhdrbc40 , @car=''$car'' ,@username=''$username'', @bc=''$bc''')
			END TRY
			BEGIN CATCH
				SELECT 
					'06' AS STATUS
					,ERROR_MESSAGE() AS DESCRIPTION;
				END CATCH
				
			 ");
			 
		 $rs = $qry->result_array();
         // $r = $qry->result();		
		 //var_dump($rs);die();
		 
		 foreach ($rs as $row){
			 $responce = array("sts" => $row['STATUS'], "desc" => $row['DESCRIPTION']);
		 }  
		 echo json_encode($responce);	
	}

	function updatebc40header(){
		
		$username = $this->session->userdata('DX_username');
		$bc_status='BC40';
		
		$CAR=$_REQUEST['CAR'];
		$KdKpbc=$_REQUEST['KdKpbc'];
		$Tujuan=$_REQUEST['Tujuan'];
		$JnsAngkutan=$_REQUEST['JnsAngkutan'];
		$KemasJn=$_REQUEST['KemasJn'];
		$KemasJm=$_REQUEST['KemasJm'];
		$KemasMrk=$_REQUEST['KemasMrk'];
		$KdKpbcBongkar=$_REQUEST['KdKpbcBongkar'];
		$PasokNama=$_REQUEST['PasokNama'];
		$PasokAlmt1=$_REQUEST['PasokAlmt1'];
		$Bruto=$_REQUEST['Bruto'];
		$Netto=$_REQUEST['Netto'];
		$NilSerah=$_REQUEST['NilSerah'];
		$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'];
		$NoKontrak=$_REQUEST['NoKontrak'];
		$TgKontrak=$_REQUEST['TgKontrak'];
		$SuratKep=$_REQUEST['SuratKep'];
		$TgSuratKep=$_REQUEST['TgSuratKep'];
		$SuratLain=$_REQUEST['SuratLain'];
		$TgSuratLain=$_REQUEST['TgSuratLain'];
		$Nopol=$_REQUEST['Nopol'];
		$Volume=$_REQUEST['Volume'];
	  
 		$datacsv = '';
		
		$datacsv .= $CAR.','.$KdKpbc.','.$Tujuan.','.$JnsAngkutan.','.$KemasJn.','.$KemasJm.','.$KemasMrk.','.$KdKpbcBongkar.','.$PasokNama.','.$PasokAlmt1.','.$Bruto.','.$Netto.','.$NilSerah.','.$Nodaf.','.$Tgldaf.
					','.$UsahaNama.','.$UsahaNPWP.','.$UsahaAlamat1.','.$UsahaAlamat2.','.$UsahaSkep.','.$PasokAlmt2.','.$PasokNPWP.','.$NoPackinglist.','.$NoKontrak.','.$TgKontrak.','.$SuratKep.','.$TgSuratKep.
					','.$SuratLain.','.$TgSuratLain.','.$Nopol.','.$Volume;

		//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 updatebc40detail(){
	
		$username = $this->session->userdata('DX_username');
		$bc_status='BC40';
		
		$id_detail_bc40 = $_REQUEST['idbcdetail']; 		
		$CAR= $_REQUEST['car'];
		$NoUrut=$_REQUEST['NoUrut'];
		$NoHs=$_REQUEST['NoHs'];
		$BrgUrai=$_REQUEST['BrgUrai'];
		$Merk=$_REQUEST['Merk'];
		$Tipe=$_REQUEST['Tipe'];
		$SpfLain=$_REQUEST['SpfLain'];
		$KdBrg=$_REQUEST['KdBrg'];
		$DCif=$_REQUEST['DCif'];
		$DSerah=$_REQUEST['DSerah'];
		$Dvol=$_REQUEST['Dvol'];
		$JmlSat=$_REQUEST['JmlSat'];
		$KdSat=$_REQUEST['KdSat'];
		$NettoDtl=$_REQUEST['NettoDtl'];

		
 		$datacsv = '';
		$datacsv .= $CAR.','.$NoHs.','.$BrgUrai.','.$Merk.','.$Tipe.','.$SpfLain.','.$KdBrg.','.$KdSat.','.$JmlSat.','.$NettoDtl.','.$DSerah.','.$Dvol.','.$NoUrut;

		//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_bc40,@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 deletebc40(){
	
		$username = $this->session->userdata('DX_username');
		$idbc40 = (isset($_POST['id_bc40_hdr']) && !empty($_POST['id_bc40_hdr']))?$_POST['id_bc40_hdr']:die('{"sts":"ERROR","desc":" Param id Tidak Ditemukan"}');
		
		$qry = $this->db_pasi->query("select * from tbl_detail_bc40 where id_bc40_hdr ='".$idbc40."'"); 
		$rs = $qry->result_array();

		foreach ($rs as $row){
		
			$idx=$row['id_bpb'];
			
			$s = $this->db_pasi->query("update tbl_bpb_raw_material set status='DELETED' where id_bpb_header='".$idx."'");
			$t = $this->db_pasi->query("delete from tbl_detail_bc40 where id_bpb='".$idx."'");
			$u = $this->db_pasi->query("delete from tbl_header_bc40 where id_bc40_hdr='".$idbc40."'");
			$v = $this->db_pasi->query("delete from tbl_pemasukan_barang where id_bc_header='".$idbc40."' and type_bc='bc40'");
			
			$responce = array("sts" => '00', "desc" => 'BC 4.0 Deleted');
		
		}
 
		echo json_encode($responce);
	}
	
  
 }