<?php
class ModelShippingMondialrelay extends Model {
	function getQuote($address) {
		$this->load->language('shipping/mondialrelay');
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "zone_to_geo_zone WHERE geo_zone_id = '" . (int)$this->config->get('mondialrelay_geo_zone_id') . "' AND country_id = '" . (int)$address['country_id'] . "' AND (zone_id = '" . (int)$address['zone_id'] . "' OR zone_id = '0')");
	
		if (!$this->config->get('mondialrelay_geo_zone_id')) {
			$status = true;
		} elseif ($query->num_rows) {
			$status = true;
		} else {
			$status = false;
		}
		
		
		$method_data = array();
	

		if ($status) {
			$quote_data = array();
			
			$this->load->model('account/address');		
			$this->load->model('localisation/country');		
			$addresse 			= $address;
			$MR_enseigne 		= $this->config->get('MR_enseigne_webservice');
			$MR_code_marque 	= $this->config->get('MR_code_marque');
			$MR_webservice_key 	= $this->config->get('MR_webservice_key');
			
			
			$isocodeMR = '';
			$c_id = (int)$addresse['country_id'];	
			$countryMR = $this->model_localisation_country->getCountry($c_id);		
			$isocodeMR = $countryMR['iso_code_2'];			
			if ($countryMR['iso_code_2'] == 'FX') {
				$isocodeMR = 'FR';
			}
			if ($countryMR['iso_code_2'] == '') {
				$isocodeMR = 'FR';
			}						
			$cp_code = $addresse['postcode']; 		
			
			
						
      		$securityCode =  md5('<' . $MR_enseigne . $MR_code_marque . '>' . $cp_code . 'FR' . '<' . $this->config->get('MR_webservice_key') . '>');
			
			$urlRetourIframe = $_SERVER['HTTP_HOST'] . '%2findex.php%3froute%3dcheckout%2fcheckout%26'; 
			// %2f => / 
			// %3f => ?
			// %3d => =
			// %26 => &
			// %7 => {
			// %7d => } 
			
			$formSearch = '
<img src="catalog/view/theme/' . $this->config->get('config_template') . '/image/logo_mondialrelay_mini.png" alt="mondial relay" style="vertical-align:middle;"/>&nbsp; 
				 Saisissez votre code postal pour trouver un point relais : &nbsp;&nbsp;&nbsp;<input type="text" name="mr_cp_search" id="mr_cp_search" value="' . $cp_code . '" />
				<input type="hidden" name="mr_pays_search" id="mr_pays_search" value="' . $isocodeMR . '" />
				<br/><br/> <a id="search_mondialrelay"class="button">Cliquez ici pour rechercher un point relais proche de chez vous</a>';
			
			if($this->cart->getSubTotal() >= $this->config->get('MR_est_gratuit')) {
				$cost = 0;
			} else {
				$cost = $this->config->get('MR_cout');
			}
			
			$quote_data['mondialrelay'] = array(
        		'code'         => 'mondialrelay.mondialrelay',
        		'title'        => $this->language->get('text_description') . $cost,
        		'cost'         => $cost,
        		'tax_class_id' => 0,
				'text'         => $this->currency->format($cost),
				//'mr_frame'   => 'http://www.mondialrelay.com/lg_fr/espaces/url/frame_pr_kit.aspx?ens=' . $MR_enseigne . $MR_code_marque . '&cp=' . $addresse['postcode'] . '&pays=' . $addresse['iso_code_2'] . '&crc=' . $securityCode . '&url=' . $urlRetourIframe . 'relais%3d%7brelais%7d%26pays%3d%7bpays%7d'
				'MR_code_html'    => $formSearch
			);

      		$method_data = array(
        		'code'       => 'mondialrelay',
        		'title'      => $this->language->get('text_title'),
        		'quote'      => $quote_data,
				'sort_order' => $this->config->get('mondialrelay_sort_order'),
        		'error'      => false
      		);
		}
	
		return $method_data;
	}
	
	function saveChoixClient($commandeId, $code_pt_relais) {		
        if ($code_pt_relais != null && $code_pt_relais != '') {
			$req = " INSERT INTO `" . DB_PREFIX . "mr_ptrelais_commande` (id_order, id_mr_num_point_relais, status) VALUES('" . $commandeId . "', '" . $code_pt_relais . "', 'ATT') ";
			$query = $this->db->query($req);
		}
		return $query;
	}
	
	function mysql_table_exists($table) {
		$tables = $this->db->query('SHOW TABLES');
		foreach($tables as $tablename) { 
			if($table == $tablename) {
				return 1; 
			} 
		}
		return 0;
	}
}
?>