<?php

if (! defined ( 'BASEPATH' ))
	exit ( 'No direct script access allowed' );
/**
 * Report Class
 *
 * @author dzungnv02
 *        
 */
class Report extends MX_Controller {
	private $_server = null;
	
	/**
	 * 
	 */
	function __construct() {
		parent::__construct ();
		ini_set ( "soap.wsdl_cache_enabled", "1" );
		$this->_server = new SoapServer ( NULL, array (
				'soap_version' => SOAP_1_2,
				'encoding' => 'UTF-8',
				'uri' => 'http://tempuri.org/' 
		) );
	}
	
	/**
	 * 
	 */
	function __destruct() {
		ini_set ( "soap.wsdl_cache_enabled", "0" );
	}
	
	/**
	 * 
	 */
	public function index() {
		if ($this->uri->segment ( 3 ) == "wsdl") {
			header ( 'Content-Type: text/xml; charset: utf-8' );
			$this->load->view ( 'reportwsdl' );
			return;
		} else {
			header ( 'Content-Type: text/html; charset: utf-8' );
			echo '<div>See Report service <a href="', base_url (), 'violetservice/report/wsdl">WSDL page</a></div>';
		}
	}
	
	/**
	 * 
	 * @return multitype:string number |string
	 */
	public function get() {
		
		/**
		 * 
		 * @return multitype:string number
		 */
		function revenueReport() {
			$aryArgs = func_get_args ();
			list ( $username, $password, $fromDate, $toDate ) = $aryArgs;
			
			$CI = & get_instance ();
			if (! $toDate)
				$toDate = date ( 'Y-m-d' );
			$message = validate ( $aryArgs );
			$success = $message == '' ? 1 : 0; // 0:error; 1:Success;
			
			if ($success == 1) {
				$CI->load->model ( 'Services_model' );
				$CI->load->model ( 'admin/Reportmodel', 'objReportModel' );
				$from = explode ( '-', $fromDate );
				$to = explode ( '-', $toDate );
				$input = array (
						'year' => $from [0],
						'month' => $from [1],
						'to_year' => $to [0],
						'to_month' => $to [1],
						'to_date' => $to [2] 
				);
				$result = $CI->objReportModel->exportProvince ( $input );
				
				if (count ( $result ) > 0 && is_array ( $result )) {
					// 1|20140915|A076|0|0|0|100000|0|0
					$aryTmp = array ();
					$aryResult = array ();
					foreach ( $result as $provinceCode => $record ) {
						// No|toDate|ProvCode|TBngay|LKTBThang|TongTBNam|DTngay|LuyKeTBThang|TongDTNam
						$aryTmp [] = $record ['stt'];
						$aryTmp [] = implode ( '', $to );
						$aryTmp [] = $record ['ma'];
						$aryTmp [] = $record ['thue_bao_ngay'];
						$aryTmp [] = $record ['thue_bao_thang'];
						$aryTmp [] = $record ['thue_bao_nam'];
						$aryTmp [] = $record ['doanh_thu_ngay'];
						$aryTmp [] = $record ['doanh_thu_thang'];
						$aryTmp [] = $record ['doanh_thu_nam'];
						$aryResult [] = implode ( '|', $aryTmp );
						$aryTmp = array ();
					}
					
					$message = implode ( "\n", $aryResult );
					$success = 1;
				}
			}
			
			return array (
					$message,
					$success,
					date ( 'YmdHis' ) 
			);
		}
		
		/**
		 * 
		 * @return multitype:string number
		 */
		function subscribersReport() {
			$aryArgs = func_get_args ();
			list ( $username, $password, $fromDate, $toDate ) = $aryArgs;
			
			$CI = & get_instance ();
			$message = validate ( $aryArgs );
			$success = $message == '' ? 1 : 0; // 0:error; 1:Success;
			
			if ($success == 1) {
				$CI->load->model ( 'Services_model' );
				$CI->load->model ( 'admin/Reportmodel', 'objReportModel' );
				$from = explode ( '-', $fromDate );
				$to = explode ( '-', $toDate );
				$input = array (
						'year' => $from [0],
						'month' => $from [1],
						'date' => $from [2],
						'to_year' => $to [0],
						'to_month' => $to [1],
						'to_date' => $to [2] 
				);
				$result = $CI->objReportModel->exportPackage ( $input );
				
				if (count ( $result ) > 0 && is_array ( $result )) {
					$aryTmp = array ();
					$aryResult = array ();
					foreach ( $result as $provinceCode => $record ) {
						$aryTmp [] = $record ['stt'];
						$aryTmp [] = implode ( '', $to );
						$aryTmp [] = $record ['ma'];
						$aryTmp [] = $record ['tong_so_VIP 1'];
						$aryTmp [] = $record ['ti_le_VIP 1'];
						$aryTmp [] = $record ['tong_so_VIP 3'];
						$aryTmp [] = $record ['ti_le_VIP 3'];
						$aryTmp [] = $record ['tong_so_VIP 6'];
						$aryTmp [] = $record ['ti_le_VIP 6'];
						$aryTmp [] = $record ['tong_so_VIP 12'];
						$aryTmp [] = $record ['ti_le_VIP 12'];
						$aryTmp [] = $record ['tong_so_tb'];
						$aryResult [] = implode ( '|', $aryTmp );
						$aryTmp = array ();
					}
					
					$message = implode ( "\n", $aryResult );
					$success = 1;
				}
			}
			
			$CI->load->model ( 'Services_model' );
			$CI->load->model ( 'admin/Reportmodel', 'objReportModel' );
			
			return array (
					$message,
					$success,
					date ( 'YmdHis' ) 
			);
		}
		
		/**
		 * 
		 * @param unknown $aryArgs
		 * @return string
		 */
		function validate($aryArgs) {
			list ( $username, $password, $fromDate, $toDate ) = $aryArgs;
			
			$CI = & get_instance ();
			$predefUsername = $CI->config->item ( 'report_username' );
			$predefPassword = $CI->config->item ( 'report_password' );
			
			$message = '';
			
			if (! $username) {
				$message = 'Username is required!';
			} else if (! $password) {
				$message = 'Password is required!';
			} else if ($username != $predefUsername) {
				$message = 'Username is not corrected!';
			} else if ($password != $predefPassword) {
				$message = 'Password is not corrected!';
			} else if (! $fromDate) {
				$message = 'fromDate is required!';
			} else if (! preg_match ( '/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/', $fromDate )) {
				$message = 'fromDate is not corrected!';
			} else if ($toDate && ! preg_match ( '/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/', $toDate )) {
				$message = 'toDate is not corrected!';
			}
			
			return $message;
		}
		
		$this->_server->addFunction ( array (
				'revenueReport',
				'subscribersReport' 
		) );
		
		try {
			ob_start ();
			$soapOutput = '';
			$request = '';
			$this->_server->handle ();
			
			if (ob_get_length () > 0) {
				$soapOutput = ob_get_clean ();
			}
			
			if ($soapOutput != '') {
				error_log ( var_export ( $soapOutput, true ) . "\n\n\n", 3, '/srv/www/sbg/log/reportservicexml.log' );
				
				$response = preg_match ( '/revenueReportResponse/', $soapOutput ) ? 'revenueReportResponse' : (preg_match ( '/subscribersReportResponse/', $soapOutput ) ? 'subscribersReportResponse' : '');
				
				$pattern = '/<[^>]*[^\/]>/i';
				$aryOutput = preg_split ( $pattern, $soapOutput, - 1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE );
				error_log ( var_export ( $aryOutput, true ) . "\n\n\n", 3, '/srv/www/sbg/log/reportservice.log' );
				$aryData = array ();
				$aryData ['message'] = $aryOutput [1];
				$aryData ['success'] = $aryOutput [2];
				$aryData ['receiveTime'] = $aryOutput [3];
				$aryData ['response'] = $response;
				$xml = $this->load->view ( 'report', $aryData, TRUE );
				header ( 'Content-Type: text/xml; charset: utf-8' );
				echo $xml;
			} else {
				header ( 'Content-Type: text/html; charset: utf-8' );
				echo 'See service <a href="' . base_url () . 'violetservice/report/wsdl">WSDL page</a>';
			}
		} catch ( Exception $e ) {
			$this->_server->fault ( 'Sender', $e->getMessage () );
		}
	}
}