<?php if (!defined('BASEPATH')) exit('No direct script access allowed');


class CT_reportmutasi_mesin 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');
		}

		$session_arr = $this->session->userdata('DX_permission');
		$u_readonly = $session_arr['readonly'];
	}
    
	function index()
	{	
		$xmldoc =  XMLbasic('dbo.MG_LIST_ADDITONAL');
		$str = $xmldoc->saveXML();
		connectwebservice_other_nodes($str, '',$sts,$des,$rows,$detail,'mode_report',$other); 
		if ($other!= null){
			foreach ($other as $per_row) {
				$mode_report['mode_sto'] 	=	getXMLValue($per_row,'mode_sto');
				$mode_report['pemasukan_pabean'] 	=	getXMLValue($per_row,'pemasukan_pabean');
				$mode_report['pengeluaran_pabean'] 	=	getXMLValue($per_row,'pengeluaran_pabean');
				break;
			} 
		}
		
		$i=0;
		$data['rID'][$i]='';
		if ($rows != null){
			foreach ($rows as $per_row) {
				$data['rID'][$i] 	=	getXMLValue($per_row,'rID');
				
				$i++;
			}
		}
		
		$data['end_date'] 		= date("Y-m-d");
		$data['start_date'] 		= date("Y-m-d", strtotime( date( "Y-m-d", strtotime( date("Y-m-d") ) ) . "-1 month" ) );
		
		$data['end_date_grid'] 	= date("Ymd");		
		$data['start_date_grid']= date("Ymd", strtotime( date( "Ymd", strtotime( date("Ymd") ) ) . "-1 month" ) );

		$data['from_sub'] = '';

		$this->load->view('ms_report/view_reportmutasi_mesin',$data);
	}
    
	function loadreport(){
		
    	$def_col= array();
		$spt='dbo.RPT_DYNAMIC_MUTASI_MESIN_TEST';   
		$def_col['rownumber']=array("db" => "rn" ,"sc" => "rn","ctype"=>"int", "bypassvalue"=>"");
	 	$def_col['rID']=array("db" => "rID" ,"sc" => "rID","ctype"=>"text", "bypassvalue"=>"");
	 	$def_col['r1']=array("db" => "r1" ,"sc" => "r1","ctype"=>"text", "bypassvalue"=>"");
		$def_col['r2']=array("db" => "r2" ,"sc" => "r2","ctype"=>"text", "bypassvalue"=>"");
		$def_col['r3']=array("db" => "r3" ,"sc" => "r3","ctype"=>"text", "bypassvalue"=>"");
		$def_col['r4']=array("db" => "r4" ,"sc" => "r4","ctype"=>"text", "bypassvalue"=>"");
		$def_col['r5']=array("db" => "r5" ,"sc" => "r5","ctype"=>"int", "bypassvalue"=>"");
		$def_col['r6']=array("db" => "r6" ,"sc" => "r6","ctype"=>"int", "bypassvalue"=>"");
		$def_col['r7']=array("db" => "r7" ,"sc" => "r7","ctype"=>"int", "bypassvalue"=>"");
		$def_col['r8']=array("db" => "r8" ,"sc" => "r8","ctype"=>"int", "bypassvalue"=>"");
		$def_col['r9']=array("db" => "r9" ,"sc" => "r9","ctype"=>"int", "bypassvalue"=>"");
		$def_col['r10']=array("db" => "r10" ,"sc" => "r10","ctype"=>"int", "bypassvalue"=>""); 
		$def_col['r11']=array("db" => "r11" ,"sc" => "r11","ctype"=>"int", "bypassvalue"=>""); 
		$def_col['r12']=array("db" => "r12" ,"sc" => "r12","ctype"=>"text", "bypassvalue"=>""); 
		  
 		$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']:''; 
	 	$show_cancel =  isset($_POST['show_cancel'])?$_POST['show_cancel']:'';		
		$source =  isset($_POST['source'])?$_POST['source']:'-1';		
      	 
		$page = isset($_POST['page'])?$_POST['page']:1; // get the requested page 
		$limit = isset($_POST['rows'])?$_POST['rows']:10; // get how many rows we want to have into the grid 
		$sidx = isset($_POST['sidx'])?$_POST['sidx']:'rID'; // get index row - i.e. user click to sort 
		$sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction 
	    
		$start_date = isset($_POST['start_date'])?$_POST['start_date']:'20130128'; 
		$end_date = isset($_POST['end_date'])?$_POST['end_date']:'20130528';
		
		if ($sord == 'asc') { $sord = '0'; } else if ($sord == 'desc') { $sord = '1'; } 
 		 
		$fsidx=$def_col[$sidx]; 
		if ($fsidx !=null) 
			$sidx=$fsidx['db']; 
		else 
			$sidx='rID'; //DEFAULT SORT KALU GA ADA  
			 
		$xmldoc =  XMLbasic($spt,'0'); 
		paramView($xmldoc, $limit, $page,  $sidx, $sord); 
		AddField($xmldoc,'mode','1');
     	AddField($xmldoc,'format','');
		AddField($xmldoc,'start_date',$start_date);  
		AddField($xmldoc,'end_date',$end_date);   
		AddField($xmldoc,'source',$source);
 		
		if ($search == 'true'){ 
			if (!empty($filters)){ 
				$array = json_decode($filters); 
				foreach($array->rules as $arr=>$k){ 
					$fcr=$def_col[$k->field]; 
					if ($fcr != null) 
					{ 
						if ($fcr['ctype']=='int'){ 
							$searchString = $k->data; 
							if ($searchString!=$fcr['bypassvalue']){
								addSearchNum($xmldoc,$fcr['sc'],$searchString,'2','1'); 
							}  
						} 
						elseif ($fcr['ctype']=='text'){ 
							$searchString = $k->data; 
							if ($searchString!=$fcr['bypassvalue']) 
							  addSearchText($xmldoc,$fcr['sc'],'1','1',$searchString); 
						}  
						elseif ($fcr['ctype']=='date'){ 
							$searchString = $k->data; 
							if (trim($searchString) != '' ){ 
								$dateArray=explode('/',$searchString);  
								$searchString = $dateArray[2].$dateArray[1].$dateArray[0]; 
							} 
								addSearchDate($xmldoc,$fcr['sc'],$searchString,'1','1'); 
						}  
					} 
				} 
			} 
	   }  
	     
		connectwebservice($xmldoc->saveXML(), '',$sts,$des,$rows,$detail); 
        
		$sordby = $detail['sort']; 
		$sordtype = $detail['desc'];
		$record = $detail['rowspage']; 
		$totalrow = $detail['rows']; 
		
	   if ($detail['pages'])
		$totalpages = $detail['pages'];   
		
		$responce->page = $page;
		$responce->total = $totalpages;
		
		if ($detail['pages'])
		$responce->records = $totalrow ; 
	
		// if ( $detail['tanggal_stok'])
			// $responce->userdata['date_sto'] = $detail['tanggal_stok']; 
		// else
			// $responce->userdata['date_sto']='';
	
		$i=0; 
		foreach ($rows as $per_row) {
			foreach ($def_col as $key => $value) {
				$responce->rows[$i][$key]=getXMLValue($per_row,$def_col[$key]['db'] );
			} 
			$i++;
 		} 
		
		echo json_encode($responce);
	}
	 
	function printReport(){
		$def_col= array();
		$spt='dbo.RPT_DYNAMIC_MUTASI_MESIN_TEST';  
		 
 		$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']:''; 
	 	$show_cancel =  isset($_POST['show_cancel'])?$_POST['show_cancel']:'';		
    	 
		$page = isset($_POST['page'])?$_POST['page']:1; // get the requested page 
		$limit = isset($_POST['rows'])?$_POST['rows']:10; // get how many rows we want to have into the grid 
		$sidx = isset($_POST['sidx'])?$_POST['sidx']:'rID'; // get index row - i.e. user click to sort 
		$sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction 
	    
		$start_date = isset($_POST['start_date'])?$_POST['start_date']:'20130128'; 
		$end_date = isset($_POST['end_date'])?$_POST['end_date']:'20130528';
		$format 	= isset($_POST['format'])?$_POST['format']:'';
 		$source =  isset($_POST['source'])?$_POST['source']:'-1';		
 		
		if ($sord == 'asc') { $sord = '0'; } else if ($sord == 'desc') { $sord = '1'; } 
 		  
		$xmldoc =  XMLbasic($spt); 
		paramView($xmldoc, $limit, $page,  $sidx, $sord); 
		AddField($xmldoc,'mode','2');
     	AddField($xmldoc,'format',$format);
		AddField($xmldoc,'start_date',$start_date);  
		AddField($xmldoc,'end_date',$end_date);   
		AddField($xmldoc,'temp_folder',sys_get_temp_dir()); 
		AddField($xmldoc,'source',$source);
 		 // if ($tipe_bc !=''){
			 // addSearchText($xmldoc,'r1','1','1',$tipe_bc);  
		 // } 
		connectwebservice($xmldoc->saveXML(), '',$sts,$des,$rows,$detail); 
        
		$namafile='';
		$sizefile=0;
	 	$data = ''; // data passed to a function or via $_POST['data']
		if ($sts=='00'){
		
			foreach ($rows as $per_row) {
				$namafile=$per_row->getElementsByTagName('xfilename')->item(0)->nodeValue;
				$xfile= $per_row->getElementsByTagName('xfile')->item(0)->nodeValue ;
				break;
			}
			saveMyFile2($xfile,$namafile); 	
			exit;
 		}else{
			echo('
				<script type="text/javascript">
					try {
						alert(\''.$des.'\'); 
					}
					catch(err) { 
					}	
				</script>
			');
			exit;
		}
	}       
}
  
?>