<?php class CT_ms_avg_pabrikasi 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');
		} 
	}
	
	function index(){	
		$this->load->view('ms_avg/view_ms_avg_pabrikasi');
	} 
	
	function view_posting(){	
	
		$data['x'] ='';
		$xmldoc =  XMLbasic('json');  
		wh_exec_sp_xml($xmldoc, 'dbo.MG_MS_INFO_STA',$sts,$des,$rows,$detail); 
		// var_dump($rows);die();
  		if ($rows != null){
			foreach ($rows as $per_row) {
				 $data['sta_avg_date']	=	getXMLValue($per_row,'rID'); 
				$data['last_avg_date']	=	getXMLValue($per_row,'rlast'); 
				break;
			}
		} 
 		$this->load->view('ms_avg/view_ms_avg_posting_pabrikasi',$data);
	} 
	
	function view_edit_item(){
		$row_data=isset($_POST['row_data'])?$_POST['row_data']:null; 
		$data['x'] ='';
		if   (empty($row_data)==false ) {
			$data['row_data'] =$row_data;
		 
		}  
 		$this->load->view('ms_avg/view_ms_avg_new',$data);
	} 

	function last_avg(){  
	
		$xmldoc =  XMLbasic('json');  
		wh_exec_sp_array($xmldoc, 'dbo.MG_MS_INFO_STA',$sts,$des,$rows,$detail); 
	
		$sta_date ='';
		$last_date ='';
		
		if ($sts=='00'){
			if ($rows != null){
				foreach ($rows as $per_row) {
					$sta_date	=	getXMLValue($per_row,'rID'); 
					$last_date	=	getXMLValue($per_row,'rlast'); 
					break;
					$i++;
				}
			}
		} 
		
		echo json_encode(array('valid'=>($sts=='00'?true:false)
			,'stsno' => $sts
			, 'stsmsg' => $des
			, 'sta_date' => $sta_date
			, 'last_date' => $last_date
		)); 		 
	}
	
	function preview_avg(){
		
	 	$format			= isset($_POST['format'])?$_POST['format']:'xls';
		$stsno			= "00";
		$stsdes			= "Sukses";
		$end_date = isset($_POST['end_date'])?$_POST['end_date']:'20130528';
		$with_detail_daily = isset($_POST['with_detail_daily'])?$_POST['with_detail_daily']:'0';
	
		$xmldata = XMLbasic('MS_MS_ITEM_FINAL_AVG_REPORT_ALL','0');    
		AddField($xmldata,'mode','2');  
		AddField($xmldata,'format',$format);	
		AddField($xmldata,'end_date',$end_date);	
		AddField($xmldata,'with_detail_daily',$with_detail_daily);	
		//AddField($xmldata,'temp_folder',"d:\\x\\"); 
		AddField($xmldata,'temp_folder',sys_get_temp_dir()); 
	
		$str = $xmldata->saveXML();
		//print_r($str);die();
		$rows;
		connectwebservice($xmldata->saveXML(), '',$stsno,$stsdes,$rows,$detail);
	 	$namafile='';
		$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;
			} 
			//  		print_r($xfile);die();
			saveMyFile2($xfile,$namafile); 
				
			exit;
		}
		
		$responce = array("sts" => $stsno, "desc" => ' '.$stsdes, "filename" => ''.$savedFile, "filenamex" => ''.$namafile );
		echo json_encode($responce); 
	}

	function print_avg(){
		
	 	$header_id				= isset($_POST['header_id'])?$_POST['header_id']:'-100';
		$with_detail_daily 	= isset($_POST['with_detail_daily'])?$_POST['with_detail_daily']:'0';
		$format					= isset($_POST['format'])?$_POST['format']:'xls';
		$stsno					= "00";
		$stsdes					= "Sukses";  
		$xmldata = XMLbasic('MS_MS_ITEM_FINAL_AVG_REPORT_ALL_PABRIKASI');    
		AddField($xmldata,'mode','2');  
		AddField($xmldata,'format',$format);	 
		AddField($xmldata,'header_id',$header_id);	
		AddField($xmldata,'with_detail_daily',$with_detail_daily);	
		//AddField($xmldata,'temp_folder',"d:\\x\\"); 
		AddField($xmldata,'temp_folder',sys_get_temp_dir()); 
	
		$str = $xmldata->saveXML();
 		$rows;
		$old = ini_get('default_socket_timeout');
		ini_set('default_socket_timeout', 3600);
     	connectwebservice($xmldata->saveXML(), '',$stsno,$stsdes,$rows,$detail);
		ini_set('default_socket_timeout', $old);
		$namafile='';
		$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;
		}
		
		$responce = array("sts" => $stsno, "desc" => ' '.$stsdes, "filename" => ''.$savedFile, "filenamex" => ''.$namafile );
		echo json_encode($responce);
	}

	function loaddata(){; 
		$def_col= array();    
		$spt='dbo.MG_MS_ITEM_FINAL_AVG'; 
		$def_col['rID']=array("db" => "rID" ,"sc" => "rID","ctype"=>"int", "bypassvalue"=>"");
		$def_col['r1']=array("db" => "r1" ,"sc" => "r1","ctype"=>"text", "bypassvalue"=>"");
		$def_col['r2']=array("db" => "r2" ,"sc" => "r2","ctype"=>"date", "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"=>"int", "bypassvalue"=>"-100");
		$def_col['r8']=array("db" => "r8" ,"sc" => "r7","ctype"=>"int", "bypassvalue"=>"-100");
		$def_col['r9']=array("db" => "r9" ,"sc" => "r9","ctype"=>"text", "bypassvalue"=>"");
		$def_col['r10']=array("db" => "r10" ,"sc" => "r10","ctype"=>"date", "bypassvalue"=>"");
		$def_col['r11']=array("db" => "r11" ,"sc" => "r11","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']:''; 
	 	 
		$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 
  	  
		if ($sord == 'asc') { $sord = '0'; } else if ($sord == 'desc') { $sord = '1'; } 
 		 
		if (array_key_exists($sidx, $def_col)) {  
			$fsidx=$def_col[$sidx];
			$sidx=$fsidx['db'];
		}else{
			$sidx='rID'; //DEFAULT SORT KALU GA ADA
		}
        $sidx=$sidx.'='. $sord;
		  
		$xmldoc =  XMLbasic($spt); 
		paramViewJSON($xmldoc, $limit, $page,  $sidx, $sord); 
		//	AddField($xmldoc ,'ms_type_id',$ms_type_id); 
		 
		$adaSearchStatus=0;
        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');  
									  if (($fcr['sc'])=='r7' || ($fcr['sc'])=='r8'){
										$adaSearchStatus=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'); 
							} 
						} 
                 } 
             } 
	   }  
  
		if ($adaSearchStatus==0){
			addSearchNum($xmldoc,'r7','-1','3','-1'); 
		}
		
  
		$str = $xmldoc->saveXML();   
		// print_r($str);die();
		 wh_exec_sp_json($xmldoc, $spt,$sts,$des,$rows,$detail);  
  		echo $rows; 
	}
		
	function create_avg(){  
		$final_avg_name = isset($_POST['avg_name'])?$_POST['avg_name']:''; // get the requested page 
		$info = isset($_POST['info'])?$_POST['info']:''; // get the requested page 
		$business_date = isset($_POST['business_date'])?$_POST['business_date']:''; // get the requested page 
	   
		$xmldoc =  XMLbasic('json'); 
		AddField($xmldoc ,'final_avg_name',$final_avg_name );
		AddField($xmldoc ,'info',$info );
		AddField($xmldoc ,'business_date',$business_date ); 
					
		$str = $xmldoc->saveXML();
		print_r($str);die();
		wh_exec_sp_array($xmldoc, 'dbo.MG_MS_ITEM_FINAL_AVG_PABRIKASI_CREATE',$sts,$des,$rows,$detail); 
		echo json_encode(array('valid'=>($sts=='00'?true:false)
			,'stsno' => $sts
			, 'stsmsg' => $des
			, 'detail' => $detail)
		); 
	}  
		
	function cancel_avg(){
		
		$info = isset($_POST['info'])?$_POST['info']:''; // get the requested page 
		$header_id = isset($_POST['header_id'])?$_POST['header_id']:''; // get the requested page 
	   
		$xmldoc =  XMLbasic('json'); 
		AddField($xmldoc ,'info',$info );
		AddField($xmldoc ,'header_id',$header_id ); 
				
				
		$str = $xmldoc->saveXML();
		//print_r($str);die();
		wh_exec_sp_array($xmldoc, 'dbo.MG_MS_ITEM_FINAL_AVG_CANCEL',$sts,$des,$rows,$detail); 
		echo json_encode(array('valid'=>($sts=='00'?true:false)
			,'stsno' => $sts
			, 'stsmsg' => $des
			, 'detail' => $detail)
		); 
	}  
				
				
		
 }