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


class CT_report_in_aw_2340 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" ) );
			
		// $xmldoc =  XMLbasic('dbo.MG_DATA_FILTER_ROW_FROM_TO');
		// AddField($xmldoc,'modefilter','2');  
		// $str = $xmldoc->saveXML(); 
		// connectwebserviceRowJSON($str, '',$sts,$des,$rows,$detail); 
		$data['from_sub'] = '';

	
	 
			 $this->load->view('reports/view_report_in_aw_2340',$data);
	 
    }
    
    function loadreport(){
		$def_col= array();
		$spt='dbo.RPT_DYNAMIC_BARANG_PER_DOKUMENT_IN_AW_2340_FIX';  
		$def_col['rn']=array("db" => "rn" ,"sc" => "rn","ctype"=>"int", "bypassvalue"=>"");
		$def_col['rgroup']=array("db" => "rgroup" ,"sc" => "rgroup","ctype"=>"text", "bypassvalue"=>"");
		$def_col['rno']=array("db" => "rno" ,"sc" => "rno","ctype"=>"int", "bypassvalue"=>"");
		$def_col['rspan']=array("db" => "rspan" ,"sc" => "rspan","ctype"=>"int", "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"=>"date", "bypassvalue"=>"");
		$def_col['r4']=array("db" => "r4" ,"sc" => "r4","ctype"=>"text", "bypassvalue"=>"");
		$def_col['r5']=array("db" => "r5" ,"sc" => "r5","ctype"=>"date", "bypassvalue"=>"");
		$def_col['r6']=array("db" => "r6" ,"sc" => "r6","ctype"=>"text", "bypassvalue"=>"");
		$def_col['r7']=array("db" => "r7" ,"sc" => "r7","ctype"=>"text", "bypassvalue"=>"");
		$def_col['r8']=array("db" => "r8" ,"sc" => "r8","ctype"=>"text", "bypassvalue"=>"");
		$def_col['r9']=array("db" => "r9" ,"sc" => "r9","ctype"=>"text", "bypassvalue"=>"-100");
		$def_col['r10']=array("db" => "r10" ,"sc" => "r10","ctype"=>"int", "bypassvalue"=>"-100");
		$def_col['r11']=array("db" => "r11" ,"sc" => "r11","ctype"=>"text", "bypassvalue"=>"");
		$def_col['r12']=array("db" => "r12" ,"sc" => "r12","ctype"=>"int", "bypassvalue"=>""); 
		$def_col['r13']=array("db" => "r13" ,"sc" => "r13","ctype"=>"text", "bypassvalue"=>"");
		$def_col['r14']=array("db" => "r14" ,"sc" => "r14","ctype"=>"date", "bypassvalue"=>"");
		
		$def_col['r15']=array("db" => "r15" ,"sc" => "r15","ctype"=>"date", "bypassvalue"=>"");
		$def_col['r16']=array("db" => "r16" ,"sc" => "r16","ctype"=>"text", "bypassvalue"=>"");
		$def_col['r17']=array("db" => "r17" ,"sc" => "r17","ctype"=>"text", "bypassvalue"=>"");
		$def_col['r18']=array("db" => "r18" ,"sc" => "r18","ctype"=>"text", "bypassvalue"=>"");
		$def_col['r19']=array("db" => "r19" ,"sc" => "r19","ctype"=>"text", "bypassvalue"=>"");
		$def_col['r20']=array("db" => "r20" ,"sc" => "r20","ctype"=>"int", "bypassvalue"=>"");
		
		$def_col['r21']=array("db" => "r21" ,"sc" => "r21","ctype"=>"text", "bypassvalue"=>"");
		$def_col['r22']=array("db" => "r22" ,"sc" => "r22","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']:'';		
     	$bc_type =  isset($_POST['bc_type'])?$_POST['bc_type']:'';		
     	$source = 'aw2340';		
       	 
        $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']:'r5'; // 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='r5'; //DEFAULT SORT KALU GA ADA  
			 
		$xmldoc =  XMLbasic($spt); 
		paramView($xmldoc, $limit, $page,  $sidx, $sord); 
		AddField($xmldoc,'source',$source);
 		AddField($xmldoc,'bc_type',$bc_type);
 		AddField($xmldoc,'mode','1');
     	AddField($xmldoc,'format','');
        AddField($xmldoc,'start_date',$start_date);  
        AddField($xmldoc,'end_date',$end_date);   
		
        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->records = $totalrow ; 
		
       	if ($detail['pages'])
		     $responce->total = $totalpages;
	 
		$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_BARANG_PER_DOKUMENT_IN_AW_2340_FIX';  
		 
 		$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']:'';		
       	$bc_type =  isset($_POST['bc_type'])?$_POST['bc_type']:'';		
     	$source = 'aw2340';	
    	 
        $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']:'';
 		$tipe_bc 	= isset($_POST['tipe_bc'])?$_POST['tipe_bc']:'';
 		
		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);
 		AddField($xmldoc,'bc_type',$bc_type);
 		 // 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;
		}
	}
	 
	
     
     
    
    
        
}
    


?>