<?php
//error_reporting(E_ALL);
	class WarrantyView extends Slot {

	var $ModuleID = 13;
	var $SectionID = 13;
	var $flag = -1;	

	function __construct($pquery) {
		parent::__construct($pquery);
		$this->Cache = 0;
		$this->Passing = TRUE;
	}

	function init() {
		if(isset($this->Target[2]) && $this->Target[2] =='capture') {   makecapture();			exit;	 }
//------export
if(isset($this->Target[2]) && $this->Target[2] =='export') { 
echo "~product_sku~^~product_parent_sku~^~category_path~^~product_name~^~product_s_desc~^~product_desc~^~product_price~^~product_full_image~^~product_thumb_image~^~product_availability~^~product_available_date~^~product_currency~^~product_discount~^~product_discount_date_end~^~product_discount_date_start~^~product_packaging~^~product_publish~^~product_special~^~product_tax~^~product_url~"."\n";
$products  = $this->Db->SelectAny("select         product.id as id,
						  pid as product_sku, 
					           '' as product_parent_sku,
  'ATN products/Thermal Imaging' as category_path,
					         name as product_name,
					  short_desc  as product_s_desc,
					  full_desc   as product_desc,
					        path2 as product_full_image,
 			          'resized/' || path2 as product_thumb_image,
					           '' as product_availability, 
                                '22/11/2012 13:00:00' as product_available_date,
                                                'USD' as product_currency,
                                              '0.00 ' as product_discount,
       		                                   '' as product_discount_date_end,
                                                   '' as product_discount_date_start,
                                                    0 as product_packaging,
						  'N' as product_publish,
                                                  'N' as product_special,
						   '' as product_tax,
						path2 as product_url
					  from product where  is_active = 1 and
						( 

product.pid like 'TIWSMT321C' or
product.pid like 'TIWSMT322C' or
product.pid like 'TIWSMT323C' or
product.pid like 'TIWSMT324B' or
product.pid like 'TIWSMT321D' or
product.pid like 'TIWSMT322D' or
product.pid like 'TIWSMT323D' or
product.pid like 'TIWSMT324A' or
product.pid like 'TIWSMT642B' or
product.pid like 'TIWSMT643B' or
product.pid like 'ACTITHRMVCBL' or
product.pid like 'TIMPOD31C' or
product.pid like 'TIMPOD31D' or
product.pid like 'TIMPOD32C' or
product.pid like 'TIMPOD32D' or
product.pid like 'TIMPOD6WB' or
product.pid like 'TIMPOD61B' or
product.pid like 'TIWSOD31C' or
product.pid like 'TIWSOD31D' or
product.pid like 'TIWSOD32C' or
product.pid like 'TIWSOD32D' or
product.pid like 'TIWSOD6WB' or
product.pid like 'TIWSOD61B' 
			       )  ");

		global $ROOT_PATH;
	$i = 0;  $j =1;
	foreach($products as $key => $value) {
		$price  = $this->Db->SelectAny("select * from price where ownerid = ".$value['id']." and current = 1" );
		if (isset($price[0]['value'] ))  $value['product_price'] = $price[0]['value'];
						else $value['product_price'] = 0.0;

                $specs =  "";
		$parameter  = $this->Db->SelectAny("select ownerid,  parameter.name || '  ' || parameter.value as spec from parameter where ownerid = ".$value['id']);
		foreach($parameter as $pkey => $pvalue) 
				$specs .= $pvalue['spec'].", "    .'<br>'; 
				$specs = str_replace( array( "\n", "\r" ), '', $specs);
				$value['product_desc'] = str_replace( array( "\n", "\r" ), '', $value['product_desc']);
				$value['product_s_desc'] = str_replace( array( "\n", "\r" ), '', $value['product_s_desc']);
		                $value['product_desc'] = $value['product_desc'] . $specs;
		if ( ($i%4)  == 0  ) {  
				if (isset($handle)) { fclose($handle);   @chmod($ROOT_PATH.'/css/'.$j.'.csv',0664);  $j++;  }
				$handle = fopen($ROOT_PATH.'/css/'.$j.'.csv',"w");
				$line = "~product_sku~^~product_parent_sku~^~category_path~^~product_name~^~product_s_desc~^~product_desc~^~product_price~^~product_full_image~^~product_thumb_image~^~product_availability~^~product_available_date~^~product_currency~^~product_discount~^~product_discount_date_end~^~product_discount_date_start~^~product_packaging~^~product_publish~^~product_special~^~product_tax~^~product_url~"."\n";
				fwrite($handle, $line);
		}
		$line = "~".$value['product_sku']."~^~".$value['product_parent_sku']."~^~".$value['category_path']."~^~".$value['product_name']."~^~".$value['product_s_desc']."~^~".$value['product_desc']."~^~".$value['product_price']."~^~".$value['product_full_image']."~^~".$value['product_thumb_image']."~^~".$value['product_availability']."~^~".$value['product_available_date']."~^~".$value['product_currency']."~^~".$value['product_discount']."~^~".$value['product_discount_date_end']."~^~".$value['product_discount_date_start']."~^~".$value['product_packaging']."~^~".$value['product_publish']."~^~".$value['product_special']."~^~".$value['product_tax']."~^~".$value['product_url']."~"."\n";
		fwrite($handle, $line);
	$i++;
	}
exit;
}
//------------









		if(!empty($this->Request)) {
			if ($this->validateForm()) {
				if ($this->WarrantyRegister()) {
						$this->Db->query("INSERT INTO customer_warranty (	  id,      first_name,             last_name)
						 values (nextval('customer_warranty_id_seq'), '".trim($this->Request['firstName'])."', '".trim($this->Request['lastName'])."' )");
                				$this->Action->processEvent($this->SectionID,'open2',array($this->Request),$this) and
						header('Location: '.$this->Home.'/onlinewarrantyregistration/1'.$this->SID) and
						$this->flag=1 ;
				} else {
              				$this->Action->processEvent($this->SectionID,'open2',array($this->Request),$this) and
					header('Location: '.$this->Home.'/onlinewarrantyregistration/2'.$this->SID) and
					$this->flag=0;
       				 	}
			}

		}
	if (isset($this->Target[2]))  $this->Target[2] ==1?	$this->flag=1:	$this->flag=0; 
	}

	function fillElements() {
				if ($this->flag==1 ) { $this->TempFillEls['thanks'][0] = array(); 	
						$this->TempFillEls['date'] = inputText('date', '' , 19, '" style="width: 130px;" readonly', 19);
				}
				elseif ($this->flag==0 ) {	$this->TempFillEls['sorry'][0] = array(); 	
						$this->TempFillEls['firstName'] = !empty(	$this->Request['firstName'])? $this->Request['firstName']: '';
						$this->TempFillEls['lastName'] = !empty(	$this->Request['lastName'])? $this->Request['lastName']: '';
						$this->TempFillEls['address1'] = !empty(	$this->Request['address1'])? $this->Request['address1']: '';
						$this->TempFillEls['address2'] = !empty(	$this->Request['address2'])? $this->Request['address2']: '';
						$this->TempFillEls['city'] = !empty(		$this->Request['city'])? $this->Request['city']: '';
						$this->TempFillEls['zipcode'] = !empty(		$this->Request['zipcode'])? $this->Request['zipcode']: '';
						$this->TempFillEls['phone'] = !empty(		$this->Request['phone'])? $this->Request['phone']: '';
						$this->TempFillEls['email'] = !empty(		$this->Request['email'])? $this->Request['email']: '';
						$this->TempFillEls['unit'] = !empty(		$this->Request['unit'])? $this->Request['unit']: '';
						$this->TempFillEls['serial'] = !empty(		$this->Request['serial'])? $this->Request['serial']: '';
						$this->TempFillEls['date'] = !empty(		$this->Request['date'])?   inputText('date', $this->Request['date'] , 19, '" style="width: 130px;" readonly', 19):  inputText('date', '' , 19, '" style="width: 130px;" readonly', 19) ;
						$this->TempFillEls['store'] = !empty(		$this->Request['store'])? $this->Request['store']: '';
				}
				else {
					$this->TempFillEls['describe'][0] = array();
					$this->TempFillEls['firstName'] = !empty(	$this->Request['firstName'])? $this->Request['firstName']: '';
					$this->TempFillEls['lastName'] = !empty(	$this->Request['lastName'])? $this->Request['lastName']: '';
					$this->TempFillEls['address1'] = !empty(	$this->Request['address1'])? $this->Request['address1']: '';
					$this->TempFillEls['address2'] = !empty(	$this->Request['address2'])? $this->Request['address2']: '';
					$this->TempFillEls['city'] = !empty(		$this->Request['city'])? $this->Request['city']: '';
					$this->TempFillEls['zipcode'] = !empty(		$this->Request['zipcode'])? $this->Request['zipcode']: '';
					$this->TempFillEls['phone'] = !empty(		$this->Request['phone'])? $this->Request['phone']: '';
					$this->TempFillEls['email'] = !empty(		$this->Request['email'])? $this->Request['email']: '';
					$this->TempFillEls['unit'] = !empty(		$this->Request['unit'])? $this->Request['unit']: '';
					$this->TempFillEls['serial'] = !empty(		$this->Request['serial'])? $this->Request['serial']: '';
					$this->TempFillEls['date'] = !empty(		$this->Request['date'])?   inputText('date', $this->Request['date'] , 19, '" style="width: 130px;" readonly', 19):  inputText('date', '' , 19, '" style="width: 130px;" readonly', 19) ;
					$this->TempFillEls['store'] = !empty(		$this->Request['store'])? $this->Request['store']: '';
				} 

		$this->country = $this->Action->Server->getGuide('country',true);
		$this->state = $this->Action->Server->getGuide('state', true); 
		$this->TempFillEls['country'] = inputSelect('country', $this->country,    !empty($this->Request['country'])? $this->Request['country']: '250' );
		$this->TempFillEls['state'] = inputSelect('state', mergeArrays(array('0' => 'Please select'),$this->state),   !empty($this->Request['state'])? $this->Request['state']: '0'    );
		$path = $this->Home."/onlinewarrantyregistration/";
		$this->TempFillEls['callback_true']  = $path."1";
		$this->TempFillEls['callback_false'] = $path."2";

		if(count($this->errors) > 0) $this->errors['header'] = $this->globalLangContent['error_view']['header_error'];
		foreach($this->errors as $k => $v) 
				$this->TempFillEls[$k.'_error'] = '<strong class=star>'.$v.'</strong><p>' ;

	}
	
	function validateForm() {
		if($_SESSION["vercode"] != $this->getRequestField('vercode') ) $this->errors['vercode'] = 'Security code is wrong';
		if( !validate(@$this->Request['firstName']) ||
			!validate(@$this->Request['lastName']) ||
			!validate(@$this->Request['address1']) ||
			!validate(@$this->Request['city'])	||
			!validate(@$this->Request['zipcode']) ||
			!validate(@$this->Request['phone']) ||
			!is_Email(@$this->Request['email']) ||
			!validate(@$this->Request['unit']) ||
			!validate(@$this->Request['serial']) ||
			!validate(@$this->Request['date']) ||
			!validate(@$this->Request['store'])	) return FALSE;
		if(sizeof($this->errors) > 0) return FALSE;
		return TRUE;
	}
	
	function getCustno($lastname) {
		$selected = $this->Db->SelectAny("select count(id) as number from customer_warranty where last_name like '".strtoupper(substr($lastname,0,3))."%'");
	if ($selected[0]['number'] < 9) return  strtoupper(substr($lastname,0,3)).'00'.($selected[0]['number']+1);
		elseif ($selected[0]['number'] >=9 && $selected[0]['number'] <99 ) return  strtoupper(substr($lastname,0,3)).'0'.($selected[0]['number']+1);
			else return  strtoupper(substr($lastname,0,3)).($selected[0]['number']+1);
	}
	
	function WarrantyRegister() { 
		global $TOOLS_PATH;
		ini_set("soap.wsdl_cache_enabled", "0");
		include_once($TOOLS_PATH.'/nusoap.php');
		$this->country = $this->Action->Server->getGuide('country',true);
		$this->state = $this->Action->Server->getGuide('state', true); 
		$client = new SoapClient("http://oa.atncorp.com:8080/web-service/serverwsdl.php?wsdl",array("trace" => 1, "exceptions" => 0));
		$client->response_timeout = 300;
		$client->soap_defencoding = 'UTF-8';
		$client->decode_utf8 = false;
		
		$err = $client->getError();
		if ($err)  return false;
		//*************************************************************//
		$input_data_parameters = array(						'first_name'=> $this->Request['firstName'],
										 	'last_name'	=> $this->Request['lastName'],	
											'address1'	=> $this->Request['address1'],	
											'address2'	=> $this->Request['address2'],	
										   		'city'	=> $this->Request['city'],	
											   'country'	=> $this->country[$this->Request['country']],
												'state'	=> $this->state[$this->Request['state']],
   											'zip_code'	=> $this->Request['zipcode'],	
										   'phone'		=> $this->Request['phone'],
										   'email'		=> $this->Request['email'],
	   									  'unit_purchased'	=> $this->Request['unit'],
										  'serial_number'	=> $this->Request['serial'],
										 'date_purchase'	=> $this->Request['date'],
								  			'store'		=> $this->Request['store'],
								  			'custno'	=> $this->getCustno(trim($this->Request['lastName'])),
									  );
		$result = $client->call('RequestWarranty', array('clientstructure' => $input_data_parameters));
		//*************************************************************//
		if ($client->fault) {
			//echo '<h2>Fault</h2><pre>';
			return false;// print_r($result);
			//echo '</pre>';
		} else {	$err = $client->getError();
					if ($err)  return false;//echo '<h2>Error</h2><pre>'.$err.'</pre>';
							   else {	//echo '<pre>';
										//------------RESULT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
										if ($result['status'] ==1) return TRUE; else return false;
										//echo '</pre>';
									 }
			    }
	}

}
?>