<?php 
class Controllercheckoutconfirmacaocadastro extends Controller {
	private $error = array();
	      
  	public function index() {
		if ($this->customer->isLogged()) {
	  		//$this->redirect($this->url->link('account/account', '', 'SSL'));
    	}

    	$this->language->load('account/confirmacao_cadastro');
		
		$this->document->setTitle($this->language->get('heading_title'));
		$this->document->addScript('catalog/view/javascript/jquery/colorbox/jquery.colorbox-min.js');
		$this->document->addStyle('catalog/view/javascript/jquery/colorbox/colorbox.css');
					
		$this->load->model('account/customer');
		$this->load->model('account/address');
		
		$this->data['entrega']= $this->model_account_address->getAddressEntrega();
       
      	$this->data['breadcrumbs'] = array();

      	$this->data['breadcrumbs'][] = array(
        	'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home'),        	
        	'separator' => false
      	); 

      	$this->data['breadcrumbs'][] = array(
        	'text'      => $this->language->get('text_account'),
			'href'      => $this->url->link('account/account', '', 'SSL'),      	
        	'separator' => $this->language->get('text_separator')
      	);
		
      	$this->data['breadcrumbs'][] = array(
        	'text'      => $this->language->get('text_confirmacao_cadastro'),
			'href'      => $this->url->link('account/confirmacao_cadastro', '', 'SSL'),      	
        	'separator' => $this->language->get('text_separator')
      	);
		
    	$this->data['heading_title'] = $this->language->get('heading_title');

		$this->data['text_account_already'] = sprintf($this->language->get('text_account_already'), $this->url->link('account/login', '', 'SSL'));
		$this->data['text_your_details'] = $this->language->get('text_your_details');
    	$this->data['text_your_address'] = $this->language->get('text_your_address');
    	$this->data['text_your_password'] = $this->language->get('text_your_password');
		$this->data['text_newsletter'] = $this->language->get('text_newsletter');
		$this->data['text_yes'] = $this->language->get('text_yes');
		$this->data['text_no'] = $this->language->get('text_no');
		$this->data['text_select']   = $this->language->get('text_select');
		$this->data['text_none']     = $this->language->get('text_none');
		$this->data['text_success']  = $this->language->get('text_success');
						
    	$this->data['entry_firstname'] = $this->language->get('entry_firstname');
    	$this->data['entry_lastname'] = $this->language->get('entry_lastname');
    	$this->data['entry_local'] = $this->language->get('entry_local');
		$this->data['entry_address_1'] = $this->language->get('entry_address_1');
    	$this->data['entry_address_2'] = $this->language->get('entry_address_2');
    	$this->data['entry_postcode'] = $this->language->get('entry_postcode');
    	$this->data['entry_city'] = $this->language->get('entry_city');
    	$this->data['entry_country'] = $this->language->get('entry_country');
    	$this->data['entry_zone'] = $this->language->get('entry_zone');
		$this->data['entry_newsletter'] = $this->language->get('entry_newsletter');
    	$this->data['entry_confirm'] = $this->language->get('entry_confirm');

		$this->data['button_continue'] = $this->language->get('button_continue');
		//$this->data['action'] = $this->url->link('checkout/payment_address', '', 'SSL');
		$json['redirect'] = $this->url->link('checkout/checkout', '', 'SSL');			
    
			 
		
         if (($this->request->server['REQUEST_METHOD'] != 'POST')) {
	    	$customer_info = $this->model_account_customer->getCustomer($this->customer->getId());
        }
				
		
		if (isset($this->request->post['firstname'])) {
			$this->data['firstname'] = $this->request->post['firstname'];
		} elseif (isset($customer_info)) {
			$this->data['firstname'] = $customer_info['firstname'];
		} else {
			$this->data['firstname'] = '';
		}
		
		if (isset($this->request->post['news'])) {
						$this->data['news'] = $this->request->post['news'];
					} elseif (isset($customer_info)) {
						$this->data['news'] = $customer_info['newsletter'];
					} else {
						$this->data['news'] = '';
		}
		
		

		if (isset($this->request->post['lastname'])) {
			$this->data['lastname'] = $this->request->post['lastname'];
		} elseif (isset($customer_info)) {
			$this->data['lastname'] = $customer_info['lastname'];
		} else {
			$this->data['lastname'] = '';
		}
		
		if (isset($this->request->post['sexo'])) {
						$this->data['sexo'] = $this->request->post['sexo'];
					} elseif (isset($customer_info)) {
						$this->data['sexo'] = $customer_info['sexo'];
					} else {
						$this->data['sexo'] = '';
		}
		if (isset($this->request->post['data_nascimento'])) {
						$this->data['data_nascimento'] = $this->request->post['data_nascimento'];
					} elseif (isset($customer_info)) {
						$this->data['data_nascimento'] = $customer_info['data_nascimento'];
					} else {
						$this->data['data_nascimento'] = '';
		}
		
		$this->data['contribuinte'] = 'S';
					
		
		
		if (isset($this->request->post['perfil'])) {
						$this->data['perfil'] = $this->request->post['perfil'];
					} elseif (isset($customer_info)) {
						$this->data['perfil'] = $customer_info['perfil'];
					} else {
						$this->data['perfil'] = '';
		}
		
		
		
		$this->load->model('localisation/country');
		$this->data['countries'] = $this->model_localisation_country->getCountries();
		
		
		$this->data['entrega']= $this->model_account_address->getAddressEntrega();	
     
		
	    if (($this->request->server['REQUEST_METHOD'] != 'POST')) {
		
		$this->data['addresses']=array();
		$this->data['addresses'] = $this->model_account_address->getAddressesBrasil();
        
				
		
		
		foreach($this->data['addresses'] as $address){
		
		
		 if($address['address_id'] == $customer_info['address_id'] ){
		 
		   $this->data['pay_address_id']  = $address['address_id'];
		   $this->data['pay_address_1']   = $address['address_1'];
		   $this->data['pay_address_2']   = $address['address_2'];
		   $this->data['pay_postcode']    = $address['postcode'];
		   $this->data['pay_city']        = $address['city'];
		   $this->data['pay_country_id']  = $address['country_id'];		
		   $this->data['pay_zone_id']     = $address['zone_id'];
		   $this->data['pay_local']       = $address['local'];
	       $this->data['pay_complemento'] = $address['complemento'];
		   $this->data['pay_numero']      = $address['numero'];
		
    	}else{
		  
		   $this->data['ship_address_id']  = $address['address_id'];
		   $this->data['ship_address_1']   = $address['address_1'];
		   $this->data['ship_address_2']   = $address['address_2'];
		   $this->data['ship_postcode']    = $address['postcode'];
		   $this->data['ship_city']        = $address['city'];
		   $this->data['ship_country_id']  = $address['country_id'];		
		   $this->data['ship_zone_id']     = $address['zone_id'];
		   $this->data['ship_local']       = $address['local'];
		   $this->data['ship_complemento'] = $address['complemento'];
		   $this->data['ship_numero']      = $address['numero'];
		  	  
		  
		}
	 }
   }else{
         
		// faturamento 

		
		if (isset($this->request->post['pay_address_1'])) {
			  $this->data['pay_address_1'] = $this->request->post['pay_address_1'];
        }else{
		     $this->data['pay_address_1'] = "";
		}
        
        if (isset($this->request->post['pay_address_2'])) {
			  $this->data['pay_address_2'] = $this->request->post['pay_address_2'];
        }else{
		     $this->data['pay_address_2'] = "";
		}
       
        if (isset($this->request->post['pay_numero'])) {
			  $this->data['pay_numero'] = $this->request->post['pay_numero'];
        }else{
		     $this->data['pay_numero'] = "";
		}
    	
        if (isset($this->request->post['pay_complemento'])) {
			  $this->data['pay_complemento'] = $this->request->post['pay_complemento'];
        }else{
		     $this->data['pay_complemento'] = "";
		}		
		
		if (isset($this->request->post['pay_local'])) {
			  $this->data['pay_local'] = $this->request->post['pay_local'];
        }else{
		     $this->data['pay_local'] = "";
		}	
		
		if (isset($this->request->post['pay_city'])) {
			  $this->data['pay_city'] = $this->request->post['pay_city'];
        }else{
		     $this->data['pay_city'] = "";
		}	
		
		if (isset($this->request->post['pay_zone_id'])) {
			  $this->data['pay_zone_id'] = $this->request->post['pay_zone_id'];
        }else{
		     $this->data['pay_zone_id'] = "";
		}	
		
		if (isset($this->request->post['pay_country_id'])) {
			  $this->data['pay_country_id'] = $this->request->post['pay_country_id'];
        }else{
		     $this->data['pay_country_id'] = "";
		}	
		
		if (isset($this->request->post['pay_postcode'])) {
			  $this->data['pay_postcode'] = $this->request->post['pay_postcode'];
        }else{
		     $this->data['pay_postcode'] = "";
		}	
		
		// entrega
		    
				 
        if (isset($this->request->post['ship_address_1'])) {
			  $this->data['ship_address_1'] = $this->request->post['ship_address_1'];
        }else{
		      $this->data['ship_address_1'] = "";
		}
        
        if (isset($this->request->post['ship_address_2'])) {
			  $this->data['ship_address_2'] = $this->request->post['ship_address_2'];
        }else{
		      $this->data['ship_address_2'] = "";
		}
		
       
        if (isset($this->request->post['ship_numero'])) {
			  $this->data['ship_numero'] = $this->request->post['ship_numero'];
        }else{
		      $this->data['ship_numero'] = "";
		}
    	
        if (isset($this->request->post['ship_complemento'])) {
			  $this->data['ship_complemento'] = $this->request->post['ship_complemento'];
        }else{
		      $this->data['ship_complemento'] = "";
		}		
		
		if (isset($this->request->post['ship_local'])) {
			  $this->data['ship_local'] = $this->request->post['ship_local'];
        }else{
		      $this->data['ship_local'] = "";
		}	
		
		if (isset($this->request->post['ship_city'])) {
			  $this->data['ship_city'] = $this->request->post['ship_city'];
        }else{
		      $this->data['ship_city'] = "";
		}	
		
		if (isset($this->request->post['ship_zone_id'])) {
			  $this->data['ship_zone_id'] = $this->request->post['ship_zone_id'];
        }else{
		      $this->data['ship_zone_id'] = "";
		}	
		
		if (isset($this->request->post['ship_country_id'])) {
			  $this->data['ship_country_id'] = $this->request->post['ship_country_id'];
        }else{
		      $this->data['ship_country_id'] = "";
		}	
		
		if (isset($this->request->post['ship_postcode'])) {
			  $this->data['ship_postcode'] = $this->request->post['ship_postcode'];
        }else{
		      $this->data['ship_postcode'] = "";
		 }	
   
        }
        			 
		$this->load->model('account/customer');
		
		if (isset($this->request->post['confirm'])) {
    		$this->data['confirm'] = $this->request->post['confirm'];
		} else {
			$this->data['confirm'] = '';
		}
		
		if (isset($this->request->post['newsletter'])) {
    		$this->data['newsletter'] = $this->request->post['newsletter'];
		} else {
			$this->data['newsletter'] = '';
		}	

		if ($this->config->get('config_account_id')) {
			$this->load->model('catalog/information');
			
			$information_info = $this->model_catalog_information->getInformation($this->config->get('config_account_id'));
			
			if ($information_info) {
				$this->data['text_agree'] = $this->language->get('text_agree').'<a class="politica"><b>'.$information_info['title'].'<br></a>';//sprintf($this->language->get('text_agree'), $this->url->link('information/information/info', 'information_id=' . $this->config->get('config_account_id'), 'SSL'), $information_info['title'], $information_info['title']);
			} else {
				$this->data['text_agree'] = '';
			}
		} else {
			$this->data['text_agree'] = '';
		}
		
		//criado para buscar a politica de privacidade via ajax
    $this->data['agree_Politica_privacidade'] = $this->url->link('information/information/info&information_id=' . $this->config->get('config_account_id'), '', 'SSL');
		
		if (isset($this->request->post['agree'])) {
      		$this->data['agree'] = $this->request->post['agree'];
		} else {
			$this->data['agree'] = false;
		}
		
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/checkout/confirmacao_cadastro.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/checkout/confirmacao_cadastro.tpl';
		} else {
			$this->template = 'default/template/checkout/confirmacao_cadastro.tpl';
		}
		
		$this->children = array(
			'common/column_left',
			'common/column_right',
			'common/content_top',
			'common/content_bottom',
			'common/footer',
			'common/header'	
		);
				
		$this->response->setOutput($this->render());	
  	}
	
	
  	public function validate() {
	   //  throw new exception('HUGOSGHSDOGPSDGOPS');
		 
		 $this->language->load('account/confirmacao_cadastro');
		 $this->load->model('account/customer');
		 $this->load->model('account/address');
		 $json=array();
         $this->data['entrega']= $this->model_account_address->getAddressEntrega();         
 		  
    	if ((utf8_strlen($this->request->post['firstname']) < 1) || (utf8_strlen($this->request->post['firstname']) > 32)) {
      		$json['error']['firstname'] = $this->language->get('error_firstname');
    	}

    	if ((utf8_strlen($this->request->post['lastname']) < 1) || (utf8_strlen($this->request->post['lastname']) > 32)) {
      		$json['error']['lastname'] = $this->language->get('error_lastname');
    	}
        
		// Valida campo Data de Nascimento
					$dataparts = explode("/",$this->request->post['data_nascimento']);
					if (strlen(utf8_decode($this->request->post['data_nascimento'])) > 0) {
						// Se o array n�o tiver 3 posi��es a data esta incorreta
						if (sizeof($dataparts) != '3'){
							$json['error']['data_nascimento'] = "Aten��o: A data informada � inv�lida";
						}else{
							// Checa validade da data
							if (!checkdate($dataparts[1],$dataparts[0],$dataparts[2])) {
								$json['error']['data_nascimento'] = "Aten��o: A data informada � inv�lida";
							}
						} 
					}
		
		
		$this->load->model('localisation/country');
		
		//Valida��o para endere�o de faturamento
		
    	if ((utf8_strlen($this->request->post['pay_address_1']) < 3) || (utf8_strlen($this->request->post['pay_address_1']) > 128)) {
      		$json['error']['pay_address_1'] = $this->language->get('error_address_1');
    	}
		
		$numero = preg_replace('/([^0-9])/i','',$this->request->post['pay_numero']);
					if ((utf8_strlen($numero) < 1) || (utf8_strlen($numero) > 6)) {
					$json['error']['pay_numero'] = 'Aten��o: O n�mero deve ter entre 1 e 6 caracteres!';
		}
		

    	if ((utf8_strlen($this->request->post['pay_city']) < 2) || (utf8_strlen($this->request->post['pay_city']) > 128)) {
      		$json['error']['pay_city'] = $this->language->get('error_city');
    	}

		$country_info = $this->model_localisation_country->getCountry($this->request->post['pay_country_id']);
		
		if ($country_info) {
			if ($country_info['postcode_required'] && (utf8_strlen($this->request->post['pay_postcode']) < 2) || (utf8_strlen($this->request->post['pay_postcode']) > 10)) {
				$json['error']['pay_postcode'] = $this->language->get('error_postcode');
			}
		}
		
		if ($this->request->post['pay_country_id'] == '') {
      		$json['error']['pay_country_id'] = $this->language->get('error_country_id');
    	}
		
		if (!isset($this->request->post['pay_zone_id']) || $this->request->post['pay_zone_id'] == '') {
      		$json['error']['pay_zone_id'] = $this->language->get('error_zone');
    	}
		

        // Valida��o para endere�o de entrega
		//throw new exception($this->data['entrega']);
	if ($this->data['entrega']==1){
		
		if ((utf8_strlen($this->request->post['ship_address_1']) < 3) || (utf8_strlen($this->request->post['ship_address_1']) > 128)) {
      		$json['error']['ship_address_1'] = $this->language->get('error_address_1');
    	}
		
		$numero = preg_replace('/([^0-9])/i','',$this->request->post['ship_numero']);
					if ((utf8_strlen($numero) < 1) || (utf8_strlen($numero) > 6)) {
						$json['error']['payment_numero'] = 'Aten��o: O n�mero deve ter entre 1 e 6 caracteres!';
		}
		
		
		

    	if ((utf8_strlen($this->request->post['ship_city']) < 2) || (utf8_strlen($this->request->post['ship_city']) > 128)) {
      		$json['error']['ship_city'] = $this->language->get('error_city');
    	}

        $country_info = $this->model_localisation_country->getCountry($this->request->post['ship_country_id']);
		
		if ($country_info) {
			if ($country_info['postcode_required'] && (utf8_strlen($this->request->post['ship_postcode']) < 2) || (utf8_strlen($this->request->post['ship_postcode']) > 10)) {
				$json['error']['ship_postcode'] = $this->language->get('error_postcode');
			}
		}

    	if ($this->request->post['ship_country_id'] == '') {
      		$json['error']['ship_country'] = $this->language->get('error_country');
    	}
		
    	if (!isset($this->request->post['ship_zone_id']) || $this->request->post['ship_zone_id'] == '') {
      		$json['error']['ship_zone'] = $this->language->get('error_zone');
    	}

    }	
    	
		if ($this->config->get('config_account_id')) {
			$this->load->model('catalog/information');
			
			$information_info = $this->model_catalog_information->getInformation($this->config->get('config_account_id'));
			
			if ($information_info && !isset($this->request->post['agree'])) {
      			$json['error']['warning'] = sprintf($this->language->get('error_agree'), $information_info['title']);
			}
		}
		
    	if (!$json) {
		
					 //if (($this->request->server['REQUEST_METHOD'] == 'POST') ) {
				
					$this->model_account_customer->editCustomerBrasil($this->request->post);
					$this->model_account_address->editAddressBrasil($this->request->post);
					$this->model_account_customer->editNewsletter($this->request->post['news']);
					
					$this->session->data['success'] = $this->language->get('text_success');

					//$this->redirect($this->url->link('account/account', '', 'SSL'));
				   //}
    	}   
            $this->response->setOutput(json_encode($json));     	
    
  	}
	
	public function country() {
		$json = array();
		
		$this->load->model('localisation/country');

    	$country_info = $this->model_localisation_country->getCountry($this->request->get['country_id']);
		
		if ($country_info) {
			$this->load->model('localisation/zone');

			$json = array(
				'country_id'        => $country_info['country_id'],
				'name'              => $country_info['name'],
				'iso_code_2'        => $country_info['iso_code_2'],
				'iso_code_3'        => $country_info['iso_code_3'],
				'address_format'    => $country_info['address_format'],
				'postcode_required' => $country_info['postcode_required'],
				'zone'              => $this->model_localisation_zone->getZonesByCountryId($this->request->get['country_id']),
				'status'            => $country_info['status']		
			);
		}
		
		$this->response->setOutput(json_encode($json));
	}	
}
?>