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

class CT_report_wip 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($str, '',$sts,$des,$rows,$detail);
		$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" ) );
		
		
        $this->load->view('reports/view_report_wip',$data);
    }
    
    function loadreport(){ 
		$def_col= array();
		$spt='dbo.RPT_DYNAMIC_WIP';  
		$def_col['rownumber']=array("db" => "rn" ,"sc" => "rn","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"=>"text", "bypassvalue"=>"");
		$def_col['r4']=array("db" => "r4" ,"sc" => "r4","ctype"=>"int", "bypassvalue"=>"");
		$def_col['r5']=array("db" => "r5" ,"sc" => "r5","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']:'';		
		 
		$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']:'r1'; // 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='r1'; //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->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(){
 		$start_date = isset($_POST['start_date'])?$_POST['start_date']:'';
		$end_date = isset($_POST['end_date'])?$_POST['end_date']:'';
		$format 	= isset($_POST['format'])?$_POST['format']:'';
 		$mode		= "2";
		$source =  isset($_POST['source'])?$_POST['source']:'';		
		
		$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']:'r1'; // get index row - i.e. user click to sort
        $sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction
        if ($sord == 'asc') { $sord = '0'; } else if ($sord == 'desc') { $sord = '1'; }
		 
		$xmldata = XMLbasic('dbo.RPT_DYNAMIC_WIP','0');
        paramView($xmldata, $limit, $page,  $sidx, $sord);        
		AddField($xmldata,'data_by',$this->session->userdata('DX_user_id'));
    	AddField($xmldata,'start_date',$start_date);   
	    AddField($xmldata,'end_date',$end_date);
     	AddField($xmldata,'mode',$mode);
		AddField($xmldata,'format',$format);
		AddField($xmldata,'temp_folder',sys_get_temp_dir()); 
		AddField($xmldata,'source',$source);
		
		$str = $xmldata->saveXML(); 
		connectwebservice($str, '',$stsno,$stsdes,$rows,$detail);
		$namafile='';
		$sizefile=0;
	 	$data = ''; // data passed to a function or via $_POST['data']
		if ($stsno=='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(\''.$stsdes.'\'); 
									}
									catch(err) { 
									}
								
					</script>
					');
		    exit;
		}
 	}
	  
        
}
    



?>