<?php
class ControllerShippingMondialrelay extends Controller {
	private $error = array(); 
	
	public function index() {   
                /**
                 *  CHARGEMENT DES MODULES
                 */
		$this->load->language('shipping/mondialrelay');		
                $this->document->setTitle($this->language->get('heading_title'));
		
		$this->load->model('setting/setting');
				
		$this->load->model('shipping/mondialrelay');		
        
		$this->document->addStyle(HTTPS_SERVER . 'view/stylesheet/mondialrelay.css');
		$this->document->addScript(HTTPS_SERVER . 'view/javascript/jquery/jquery-ui-1.8.18.custom.min.js');
		
		$this->model_shipping_mondialrelay->checkinstall();   
		
		$this->data['onglete'] = '';
		
		/**
		 *  Si validation :
		 */
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
			/**
			 *  Si validation du formulaire de configuration
			 */
			if (isset($this->request->post['MR_actiontype']) && $this->request->post['MR_actiontype'] == 'laconfig' ) {
				$this->model_setting_setting->editSetting('mondialrelay', $this->request->post);		
				
				$this->session->data['success'] = $this->language->get('text_success');
					
				$this->redirect($this->url->link('extension/shipping', 'token=' . $this->session->data['token'], 'SSL'));
			}
			
			
			/**
			 *  Si validation du formulaire de suppression
			 */
			if (isset($this->request->post['MR_actiontype']) && $this->request->post['MR_actiontype'] == 'supprlignes' ) {
				
				if(isset($this->request->post['ligne2'])) {
					$lignes 		= $this->request->post['ligne2'];  
					$lignes_text 	= implode(', ', $lignes);
				
					// On update le poids
					$this->db->query(" DELETE FROM `" . DB_PREFIX . "mr_ptrelais_commande` " .
									 " WHERE id_mr_rel_pt_order IN (" . $this->db->escape($lignes_text) . ")");
				}
			}
			
			/**
			 *  Si validation du formulaire d'etiquettes
			 */
			if (isset($this->request->post['MR_actiontype']) && $this->request->post['MR_actiontype'] == 'lesetiquettes' ) {
				
				if(isset($this->request->post['ligne'])) {
					$lignes 		= $this->request->post['ligne'];  
					$poids_colis 	= $this->request->post['MR_poids_saisi_gr'];  
					$lignes_text 	= implode(', ', $lignes);
				
					// On update le poids
					$this->db->query(" UPDATE `" . DB_PREFIX . "mr_ptrelais_commande` " .
									 " SET poids_colis = '" . $this->db->escape($poids_colis) . "' " .
									 " WHERE id_mr_rel_pt_order IN (" . $this->db->escape($lignes_text) . ")");
		
					// Variables globales
					$Etiquette_CRT 			= 0;
					$Etiquette_NbColis		= 1;
					$Etiquette_poidsColis	= $poids_colis;
					$Etiquette_enseigne		= $this->request->post['Et_MR_enseigne_webservice'];  
					$Etiquette_mode_coll	= $this->request->post['Et_MR_col_mode'];  
					$Etiquette_mode_livr	= $this->request->post['Et_MR_dlv_mode'];  
					$Etiquette_exp_lang		= $this->request->post['Et_MR_language'];
					$Etiquette_exp_ad1		= $this->formatMaj($this->request->post['Et_MR_exp_ad1']);
					$Etiquette_exp_ad3		= $this->formatMaj($this->request->post['Et_MR_exp_ad3']);
					$Etiquette_exp_ville	= $this->request->post['Et_MR_exp_ville'];
					$Etiquette_exp_cp		= $this->request->post['Et_MR_exp_cp'];
					$Etiquette_exp_pays		= $this->request->post['Et_MR_exp_pays'];
					$Etiquette_exp_tel		= $this->request->post['Et_MR_exp_tel'];
					
					foreach($lignes as $idligneMR) {
						// Recuperation des infos concernant le destinataire
						$Dest_langage 	= '';
						$Dest_ad1 		= '';
						$Dest_ad3 		= '';
						$Dest_ville 	= '';
						$Dest_cp 		= '';
						$Dest_pays 		= '';
						
						// On interroge table client via order => payement
						$query = $this->db->query(" SELECT DISTINCT * " .
												  " FROM `" . DB_PREFIX . "order` p " . 
												  " 	LEFT JOIN `" . DB_PREFIX . "mr_ptrelais_commande` mr ON (p.order_id = mr.id_order) " .
												  " 	LEFT JOIN `" . DB_PREFIX . "language` lg ON (p.language_id = lg.language_id) " .
												  " 	LEFT JOIN `" . DB_PREFIX . "country` ps ON (p.payment_country_id = ps.country_id) " .
												  " WHERE mr.id_mr_rel_pt_order = '" . (int)$idligneMR . "'");
						
						$Destinataire = array();
						$Etiquette_LIV_REL = '';
						$Etiquette_LIV_Pays = '';
						foreach ($query->rows as $resultat_order) {
							$Destinataire = array();
							$Destinataire['Dest_langage'] 	= $this->formatMaj($resultat_order['code']);
							$Destinataire['Dest_ad1'] 		= 'M. ' . $this->formatMaj($resultat_order['payment_lastname']) . ' ' . $this->formatMaj($resultat_order['payment_firstname']);
							$Destinataire['Dest_ad3'] 		= $this->formatMaj($resultat_order['payment_address_1']);
							$Destinataire['Dest_ville'] 	= $this->formatMaj($resultat_order['payment_city']);
							$Destinataire['Dest_cp'] 		= $this->formatMaj($resultat_order['payment_postcode']);
							$Destinataire['Dest_pays'] 		= $this->formatMaj($resultat_order['iso_code_2']);					
							if ($Destinataire['Dest_pays'] == 'FX') {
								$Destinataire['Dest_pays'] = 'FR';
							}
							if ($Destinataire['Dest_pays'] == '') {
								$Destinataire['Dest_pays'] = 'FR';
							}
							$Etiquette_LIV_REL		 		= $this->formatMaj($resultat_order['id_mr_num_point_relais']);
							$Etiquette_LIV_Pays		 		= $this->formatMaj($resultat_order['pays_livraison']);
						}		
						
						
						// On appelle le webservice Mondial Relay : WSI2_CreationEtiquette qui permet de generer les etiquettes et URLs
						require_once $this->racine() . 'system/library/nusoap.php';
						$client = new nusoap_client($this->config->get('MR_webservice_url'), true);
						$client->soap_defencoding = 'utf-8';
						
						$secure = strtoupper(md5($Etiquette_enseigne . 
													$Etiquette_mode_coll . 
													$Etiquette_mode_livr . 
													$Etiquette_exp_lang . 
													$Etiquette_exp_ad1 . 
													$Etiquette_exp_ad3 . 
													$Etiquette_exp_ville . 
													$Etiquette_exp_cp . 
													$Etiquette_exp_pays . 
													$Etiquette_exp_tel .  
													$Destinataire['Dest_langage'] .  
													$Destinataire['Dest_ad1'] .  
													str_replace('ï', 'I', $Destinataire['Dest_ad3']) .  
													$Destinataire['Dest_ville'] .  
													$Destinataire['Dest_cp'] .  
													$Destinataire['Dest_pays'] .  
													$Etiquette_poidsColis . 
													$Etiquette_NbColis . 
													$Etiquette_CRT . 
													$Etiquette_LIV_Pays . 
													$Etiquette_LIV_REL . 
													$this->config->get('MR_insurance') . 
													$this->config->get('MR_webservice_key')));
						$params = array(
							'Enseigne' 		=> $Etiquette_enseigne,
							'ModeCol' 		=> $Etiquette_mode_coll,
							'ModeLiv' 		=> $Etiquette_mode_livr,
							'Expe_Langage' 	=> $Etiquette_exp_lang,
							'Expe_Ad1' 		=> $Etiquette_exp_ad1,
							'Expe_Ad3' 		=> $Etiquette_exp_ad3,
							'Expe_Ville' 	=> $Etiquette_exp_ville,
							'Expe_CP' 		=> $Etiquette_exp_cp,
							'Expe_Pays' 	=> $Etiquette_exp_pays,
							'Expe_Tel1' 	=> $Etiquette_exp_tel,
							'Dest_Langage' 	=> $Destinataire['Dest_langage'],
							'Dest_Ad1' 		=> $Destinataire['Dest_ad1'],
							'Dest_Ad3' 		=> str_replace('ï', 'I', $Destinataire['Dest_ad3']),
							'Dest_Ville' 	=> $Destinataire['Dest_ville'],
							'Dest_CP' 		=> $Destinataire['Dest_cp'],
							'Dest_Pays' 	=> $Destinataire['Dest_pays'],
							'Poids' 		=> $Etiquette_poidsColis,
							'NbColis' 		=> $Etiquette_NbColis,
							'CRT_Valeur'	=> $Etiquette_CRT,
							'LIV_Rel_Pays'	=> $Etiquette_LIV_Pays,
							'LIV_Rel'		=> $Etiquette_LIV_REL,
							'Assurance'		=> $this->config->get('MR_insurance'),
							'Security' 		=> $secure
							);
							
						$result = $client->call('WSI2_CreationEtiquette', 
								$params, 
								'http://www.mondialrelay.fr/webservice/', 
								'http://www.mondialrelay.fr/webservice/WSI2_CreationEtiquette');
						
						if (!$client->fault) {
							$err = $client->getError();
							if (!$err) { 
								$result = $result['WSI2_CreationEtiquetteResult'];
								
								if (($errorNumber = $result['STAT']) != 0) {
									// ERREUR
									$tabError = array();
									$tabError[1] = "Enseigne invalide";
									$tabError[2] = "Numero d'enseigne vide ou inexistant";
									$tabError[3] = "Numero de compte enseigne invalide";
									$tabError[4] = "Erreur type 4";
									$tabError[5] = "Numero de dossier enseigne invalide";
									$tabError[6] = "Erreur type 6";
									$tabError[7] = "Numero de client enseigne invalide";
									$tabError[8] = "Mot de passe ou hachage invalide";
									$tabError[9] = "Ville non reconnu ou non unique";
									$tabError[10] = "Type de collecte invalide";
									$tabError[11] = "Numero de Relais de Collecte invalide";
									$tabError[12] = "Pays de Relais de collecte invalide";
									$tabError[13] = "Type de livraison invalide";
									$tabError[14] = "Numero de Relais de livraison invalide";
									$tabError[15] = "Pays de Relais de livraison invalide";
									$tabError[16] = "Erreur type 16";
									$tabError[17] = "Erreur type 17";
									$tabError[18] = "Erreur type 18";
									$tabError[19] = "Erreur type 19";
									$tabError[20] = "Poids du colis invalide";
									$tabError[21] = "Taille (Longueur + Hauteur) du colis invalide";
									$tabError[22] = "Taille du Colis invalide";
									$tabError[23] = "Erreur type 23";
									$tabError[24] = "Numero d'expedition ou de suivi invalide";
									$tabError[25] = "Erreur type 25";
									$tabError[26] = "Temps de montage invalide";
									$tabError[27] = "Mode de collecte ou de livraison invalide";
									$tabError[28] = "Mode de collecte invalide";
									$tabError[29] = "Mode de livraison invalide";
									$tabError[30] = "Adresse (L1) invalide";
									$tabError[31] = "Adresse (L2) invalide";
									$tabError[32] = "Erreur type 32";
									$tabError[33] = "Adresse (L3) invalide";
									$tabError[34] = "Adresse (L4) invalide";
									$tabError[35] = "Ville invalide";
									$tabError[36] = "Code postal invalide";
									$tabError[37] = "Pays invalide";
									$tabError[38] = "Numero de telephone invalide";
									$tabError[39] = "Adresse e-mail invalide";
									$tabError[40] = "Parametres manquants";
									$tabError[41] = "Erreur type 41";
									$tabError[42] = "Montant CRT invalide";
									$tabError[43] = "Devise CRT invalide";
									$tabError[44] = "Valeur du colis invalide";
									$tabError[45] = "Devise de la valeur du colis invalide";
									$tabError[46] = "Plage de numero d'expedition epuisee";
									$tabError[47] = "Nombre de colis invalide";
									$tabError[48] = "Multi-Colis Relais Interdit";
									$tabError[49] = "Action invalide";
									$tabError[50] = "Erreur type 50";
									$tabError[51] = "Erreur type 51";
									$tabError[52] = "Erreur type 52";
									$tabError[53] = "Erreur type 53";
									$tabError[54] = "Erreur type 54";
									$tabError[55] = "Erreur type 55";
									$tabError[56] = "Erreur type 56";
									$tabError[57] = "Erreur type 57";
									$tabError[58] = "Erreur type 58";
									$tabError[59] = "Erreur type 59";
									$tabError[60] = "Champ texte libre invalide";
									$tabError[61] = "Top avisage invalide";
									$tabError[62] = "Instruction de livraison invalide";
									$tabError[63] = "Assurance invalide";
									$tabError[64] = "Temps de montage invalide";
									$tabError[65] = "Top rendez-vous invalide";
									$tabError[66] = "Top reprise invalide";
									$tabError[67] = "Latitude invalide";
									$tabError[68] = "Longitude invalide";
									$tabError[69] = "Code Enseigne invalide";
									$tabError[70] = "Numero de Point Relais invalide";
									$tabError[71] = "Nature de point de vente non valide";
									$tabError[72] = "Erreur type 72";
									$tabError[73] = "Erreur type 73";
									$tabError[74] = "Langue invalide";
									$tabError[75] = "Erreur type 75";
									$tabError[76] = "Erreur type 76";
									$tabError[77] = "Erreur type 77";
									$tabError[78] = "Pays de Collecte invalide";
									$tabError[79] = "Pays de Livraison invalide";
									$tabError[80] = "Code tracing : Colis enregistre";
									$tabError[81] = "Code tracing : Colis en traitement chez Mondial Relay";
									$tabError[82] = "Code tracing : Colis livre";
									$tabError[83] = "Code tracing : Anomalie";
									$tabError[84] = "Erreur type 84";
									$tabError[85] = "Erreur type 85";
									$tabError[86] = "Erreur type 86";
									$tabError[87] = "Erreur type 87";
									$tabError[88] = "Erreur type 88";
									$tabError[89] = "Erreur type 89";
									$tabError[90] = "Erreur type 90";
									$tabError[91] = "Erreur type 91";
									$tabError[92] = "Erreur type 92";
									$tabError[93] = "Aucun element retourne par le plan de tri";
									$tabError[94] = "Colis Inexistant";
									$tabError[95] = "Compte Enseigne non active";
									$tabError[96] = "Type d'enseigne incorrect en Base";
									$tabError[97] = "Cle de securite invalide";
									$tabError[98] = "Erreur generique";
									$tabError[99] = "Erreur generique du service";
								
									$this->data['onglete'] .= '<div style="width:500px;word-wrap:break-word;"><span style="color:red;">ERREUR<br/><br/>' . $tabError[$errorNumber] . '</span><br/><br/>';
								} else {
									$numExpedition	 = $result['ExpeditionNum'];
									$urlEtiquette	 = $result['URL_Etiquette'];
								
									$this->db->query(" UPDATE `" . DB_PREFIX . "mr_ptrelais_commande` " .
													 " SET num_expe = '" . $this->db->escape($numExpedition) . "', " .
													 "     url_pdf_etiq = 'http://www.mondialrelay.fr" . $this->db->escape($urlEtiquette) . "', " .
													 "     status = 'OK', " .
													 "     date_creation_etiquette = NOW() " .
													 " WHERE id_mr_rel_pt_order = '" . (int)$idligneMR . "'");
		
								}
							}
						}
						
						/* DEBUGGAGE */
						$this->data['onglete'] .= '<h2>Request</h2><pre>' . htmlspecialchars($client->request, ENT_QUOTES) . '</pre>';
						$this->data['onglete'] .= '<h2>Response</h2><pre>' . htmlspecialchars($client->response, ENT_QUOTES) . '</pre>';
						$this->data['onglete'] .= '<h2>Debug</h2><pre>' . htmlspecialchars($client->getDebug(), ENT_QUOTES) . '</pre>';
						/**/
						$this->data['onglete'] .= '</div>';
					}
				}
			}
		}
		
		
		
		
		/**
		 *  Filtre et tri des tableaux
		 */
		$this->data['onglet'] = '';
		
		if (isset($this->request->post['onglet'])) {
			$this->data['onglet'] = $this->request->post['onglet'];
		} else { 
			$this->data['onglet'] = 'tab-content';	
		}
		
		$this->data['commandes'] = array();
		$tri = array();
		if (isset($this->request->get['histo_sort']) && $this->request->get['histo_sort'] == 't1.id_mr_rel_pt_order') {
			$tri['colonne'] = $this->request->get['histo_sort'];
			$tri['tri'] = $this->request->get['histo_by'];
			if ($tri['tri'] == 'ASC') { $tri_1 = 'DESC'; } else { $tri_1 = 'ASC'; }
		} else { 
			$tri_1 = 'DESC';
		}		
		$this->data['sort_2_1'] = $this->url->link('shipping/mondialrelay', 'token=' . $this->session->data['token'] . '&histo_sort=t1.id_mr_rel_pt_order&histo_by=' . $tri_1 . '&ongl=tab-historique', 'SSL');
		$this->data['sort_1_1'] = $this->url->link('shipping/mondialrelay', 'token=' . $this->session->data['token'] . '&histo_sort=t1.id_mr_rel_pt_order&histo_by=' . $tri_1 . '&ongl=tab-generertickets', 'SSL');
		
		if (isset($this->request->get['histo_sort']) && $this->request->get['histo_sort'] == 't1.id_mr_num_point_relais') {
			$tri['colonne'] = $this->request->get['histo_sort'];
			$tri['tri'] = $this->request->get['histo_by'];
			if ($tri['tri'] == 'ASC') { $tri_2 = 'DESC'; } else { $tri_2 = 'ASC'; }
		} else { 
			$tri_2 = 'DESC';
		}		
		$this->data['sort_2_2'] = $this->url->link('shipping/mondialrelay', 'token=' . $this->session->data['token'] . '&histo_sort=t1.id_mr_num_point_relais&histo_by=' . $tri_2 . '&ongl=tab-historique', 'SSL');
		$this->data['sort_1_2'] = $this->url->link('shipping/mondialrelay', 'token=' . $this->session->data['token'] . '&histo_sort=t1.id_mr_num_point_relais&histo_by=' . $tri_2 . '&ongl=tab-generertickets', 'SSL');
				
		if (isset($this->request->get['histo_sort']) && $this->request->get['histo_sort'] == 't1.id_order') {
			$tri['colonne'] = $this->request->get['histo_sort'];
			$tri['tri'] = $this->request->get['histo_by'];
			if ($tri['tri'] == 'ASC') { $tri_3 = 'DESC'; } else { $tri_3 = 'ASC'; }
		} else { 
			$tri_3 = 'DESC';
		}		
		$this->data['sort_2_3'] = $this->url->link('shipping/mondialrelay', 'token=' . $this->session->data['token'] . '&histo_sort=t1.status&histo_by=' . $tri_3 . '&ongl=tab-historique', 'SSL');
		$this->data['sort_1_3'] = $this->url->link('shipping/mondialrelay', 'token=' . $this->session->data['token'] . '&histo_sort=t1.status&histo_by=' . $tri_3 . '&ongl=tab-generertickets', 'SSL');
						
		if (isset($this->request->get['histo_sort']) && $this->request->get['histo_sort'] == 't1.status') {
			$tri['colonne'] = $this->request->get['histo_sort'];
			$tri['tri'] = $this->request->get['histo_by'];
			if ($tri['tri'] == 'ASC') { $tri_4 = 'DESC'; } else { $tri_4 = 'ASC'; }
		} else { 
			$tri_4 = 'DESC';
		}		
		$this->data['sort_2_4'] = $this->url->link('shipping/mondialrelay', 'token=' . $this->session->data['token'] . '&histo_sort=t1.status&histo_by=' . $tri_4 . '&ongl=tab-historique', 'SSL');
		$this->data['sort_1_4'] = $this->url->link('shipping/mondialrelay', 'token=' . $this->session->data['token'] . '&histo_sort=t1.status&histo_by=' . $tri_4 . '&ongl=tab-generertickets', 'SSL');
		
						
				
						
		if (isset($this->request->get['histo_sort']) && $this->request->get['histo_sort'] == 't1.num_expe') {
			$tri['colonne'] = $this->request->get['histo_sort'];
			$tri['tri'] = $this->request->get['histo_by'];
			if ($tri['tri'] == 'ASC') { $tri_6 = 'DESC'; } else { $tri_6 = 'ASC'; }
		} else { 
			$tri_6 = 'DESC';
		}		
		$this->data['sort_2_6'] = $this->url->link('shipping/mondialrelay', 'token=' . $this->session->data['token'] . '&histo_sort=t1.num_expe&histo_by=' . $tri_6 . '&ongl=tab-historique', 'SSL');
		$this->data['sort_1_6'] = $this->url->link('shipping/mondialrelay', 'token=' . $this->session->data['token'] . '&histo_sort=t1.num_expe&histo_by=' . $tri_6 . '&ongl=tab-generertickets', 'SSL');
				
						
		if (isset($this->request->get['histo_sort']) && $this->request->get['histo_sort'] == 't1.url_pdf_etiq') {
			$tri['colonne'] = $this->request->get['histo_sort'];
			$tri['tri'] = $this->request->get['histo_by'];
			if ($tri['tri'] == 'ASC') { $tri_7 = 'DESC'; } else { $tri_7 = 'ASC'; }
		} else { 
			$tri_7 = 'DESC';
		}		
		$this->data['sort_2_7'] = $this->url->link('shipping/mondialrelay', 'token=' . $this->session->data['token'] . '&histo_sort=t1.url_pdf_etiq&histo_by=' . $tri_7 . '&ongl=tab-historique', 'SSL');
		$this->data['sort_1_7'] = $this->url->link('shipping/mondialrelay', 'token=' . $this->session->data['token'] . '&histo_sort=t1.url_pdf_etiq&histo_by=' . $tri_7 . '&ongl=tab-generertickets', 'SSL');
				
						
		if (isset($this->request->get['histo_sort']) && $this->request->get['histo_sort'] == 't1.date_creation_etiquette') {
			$tri['colonne'] = $this->request->get['histo_sort'];
			$tri['tri'] = $this->request->get['histo_by'];
			if ($tri['tri'] == 'ASC') { $tri_8 = 'DESC'; } else { $tri_8 = 'ASC'; }
		} else { 
			$tri_8 = 'DESC';
		}	
		$this->data['sort_2_8'] = $this->url->link('shipping/mondialrelay', 'token=' . $this->session->data['token'] . '&histo_sort=t1.date_creation_etiquette&histo_by=' . $tri_8 . '&ongl=tab-historique', 'SSL');
		$this->data['sort_1_8'] = $this->url->link('shipping/mondialrelay', 'token=' . $this->session->data['token'] . '&histo_sort=t1.date_creation_etiquette&histo_by=' . $tri_8 . '&ongl=tab-generertickets', 'SSL');
		
				
						
		if (isset($this->request->get['histo_sort']) && $this->request->get['histo_sort'] == 't1.poids_colis') {
			$tri['colonne'] = $this->request->get['histo_sort'];
			$tri['tri'] = $this->request->get['histo_by'];
			if ($tri['tri'] == 'ASC') { $tri_9 = 'DESC'; } else { $tri_9 = 'ASC'; }
		} else { 
			$tri_9 = 'DESC';
		}	
		$this->data['sort_1_9'] = $this->url->link('shipping/mondialrelay', 'token=' . $this->session->data['token'] . '&histo_sort=t1.poids_colis&histo_by=' . $tri_9 . '&ongl=tab-generertickets', 'SSL');
		 
		
		
		/**
		 *  Onglet Historique
		 */
		$this->data['commandes'] = array();
		$les_resultats = $this->model_shipping_mondialrelay->getMondialRelayOrders($tri);
		if ($les_resultats) {
			foreach ($les_resultats as $le_resultat) {
				$this->data['commandes'][] = array(
												'id_mr_rel_pt_order'      	=> $le_resultat['id_mr_rel_pt_order'],
												'id_mr_num_point_relais'  	=> $le_resultat['id_mr_num_point_relais'],
												'id_order'      			=> $le_resultat['id_order'],
												'status'      				=> $le_resultat['status'],
												'date_creation'      		=> $le_resultat['date_creation'],
												'date_etiquette'      		=> ($le_resultat['date_creation_etiquette'] != '0000-00-00 00:00:00') ? $le_resultat['date_creation_etiquette']:'-',
												'num_expe'      			=> ($le_resultat['num_expe'] > 0) ? $le_resultat['num_expe']:'-',
												'url_pdf_etiq'      		=> ($le_resultat['url_pdf_etiq'] != '') ? $le_resultat['url_pdf_etiq']:'-'
											);
			}
        } 

		/**
		 *  Onglet Generation des etiquettes
		 */
		$this->data['commandes_sans_etiquette'] = array();
		$les_resultats_ss_etiq = $this->model_shipping_mondialrelay->getMondialRelayOrdersSansEtiquette($tri);
		if($les_resultats_ss_etiq) {
			foreach ($les_resultats_ss_etiq as $le_resultat) {
				$this->data['commandes_sans_etiquette'][] = array(
												'id_mr_rel_pt_order'      	=> $le_resultat['id_mr_rel_pt_order'],
												'id_mr_num_point_relais'  	=> $le_resultat['id_mr_num_point_relais'],
												'id_order'      			=> $le_resultat['id_order'],
												'status'      				=> $le_resultat['status'],
												'date_creation'      		=> $le_resultat['date_creation'],
												'poids_colis'      			=> $le_resultat['poids_colis'],
												'date_etiquette'      		=> ($le_resultat['date_creation_etiquette'] != '0000-00-00 00:00:00') ? $le_resultat['date_creation_etiquette']:'-',
												'num_expe'      			=> ($le_resultat['num_expe'] > 0) ? $le_resultat['num_expe']:'-',
												'url_pdf_etiq'      		=> ($le_resultat['url_pdf_etiq'] != '') ? $le_resultat['url_pdf_etiq']:'-'
											);
			}
		}
                
		/**
		 *  CHARGEMENT DES TEXTES
		 */
		$this->data['heading_title'] = $this->language->get('heading_title');
		
		$this->data['text_gratuit'] 		= $this->language->get('text_gratuit');
		$this->data['text_enabled'] 		= $this->language->get('text_enabled');
		$this->data['text_disabled'] 		= $this->language->get('text_disabled');
		$this->data['entry_status'] 		= $this->language->get('entry_status');
		$this->data['entry_sort_order'] 	= $this->language->get('entry_sort_order');
		
		$this->data['txt_tab_configuration']            = $this->language->get('txt_tab_configuration');
		$this->data['txt_tab_genererTickets']           = $this->language->get('txt_tab_genererTickets');
		$this->data['txt_tab_historique']               = $this->language->get('txt_tab_historique');
		$this->data['txt_tab_information']              = $this->language->get('txt_tab_information');
		
		$this->data['txt_info_version']               	= $this->language->get('txt_info_version');
		$this->data['txt_info_auteur']               	= $this->language->get('txt_info_auteur');
		$this->data['txt_date_version']               	= $this->language->get('txt_date_version');
		
		$this->data['txt_MR_enseigne_webservice']       = $this->language->get('txt_MR_enseigne_webservice');
		$this->data['txt_MR_code_marque']               = $this->language->get('txt_MR_code_marque');
		$this->data['txt_MR_webservice_key']            = $this->language->get('txt_MR_webservice_key');
		$this->data['txt_MR_language']                  = $this->language->get('txt_MR_language');
		$this->data['txt_MR_weight_coefficient']        = $this->language->get('txt_MR_weight_coefficient');
		$this->data['txt_MR_name']                      = $this->language->get('txt_MR_name');
		$this->data['txt_MR_delay']                     = $this->language->get('txt_MR_delay');
		$this->data['txt_MR_dlv_mode']                  = $this->language->get('txt_MR_dlv_mode');
		$this->data['txt_MR_col_mode']                  = $this->language->get('txt_MR_col_mode');
		$this->data['txt_MR_insurance']                 = $this->language->get('txt_MR_insurance');
		$this->data['txt_MR_country_list']              = $this->language->get('txt_MR_country_list');
		$this->data['txt_MR_webservice_url']            = $this->language->get('txt_MR_webservice_url');
		$this->data['txt_non_modif']            	    = $this->language->get('txt_non_modif');
		$this->data['txt_cout']            	    		= $this->language->get('txt_cout');
		
		$this->data['txt_MR_prenom']            	    = $this->language->get('txt_MR_prenom');
		$this->data['txt_MR_civilite']            	    = $this->language->get('txt_MR_civilite');
		$this->data['txt_MR_rueexpe']            	    = $this->language->get('txt_MR_rueexpe');
		$this->data['txt_MR_villeexpe']            	    = $this->language->get('txt_MR_villeexpe');
		$this->data['txt_MR_cpexpe']            	    = $this->language->get('txt_MR_cpexpe');
		$this->data['txt_MR_paysexpe']            	    = $this->language->get('txt_MR_paysexpe');
		$this->data['txt_MR_telexpe']            	    = $this->language->get('txt_MR_telexpe');
		
		$this->data['txt_commande_ID']            	    = $this->language->get('txt_commande_ID');
		$this->data['txt_commande_client']            	= $this->language->get('txt_commande_client');
		$this->data['txt_commande_total']            	= $this->language->get('txt_commande_total');
		$this->data['txt_commande_total_envoi']         = $this->language->get('txt_commande_total_envoi');
		$this->data['txt_commande_date']            	= $this->language->get('txt_commande_date');
		$this->data['txt_poids_gramme']            	    = $this->language->get('txt_poids_gramme');
		$this->data['txt_MR_number']            	    = $this->language->get('txt_MR_number');
		$this->data['txt_MR_country']            	    = $this->language->get('txt_MR_country');
		$this->data['txt_commande_exp_number']          = $this->language->get('txt_commande_exp_number');
		$this->data['txt_commande_details']            	= $this->language->get('txt_commande_details');
		$this->data['txt_commande_num_point_relais']    = $this->language->get('txt_commande_num_point_relais');
		$this->data['txt_commande_status']            	= $this->language->get('txt_commande_status');
		$this->data['txt_commande_no_result']           = $this->language->get('txt_commande_no_result');
		$this->data['txt_commande_customer_id']         = $this->language->get('txt_commande_customer_id');
		$this->data['txt_commande_r1']          		= $this->language->get('txt_commande_r1');
		$this->data['txt_commande_r2']           		= $this->language->get('txt_commande_r2');
		$this->data['txt_commande_r3']           		= $this->language->get('txt_commande_r3');
		$this->data['txt_commande_r4']           		= $this->language->get('txt_commande_r4');
		$this->data['btn_etiquettes']           		= $this->language->get('btn_etiquettes');
		$this->data['btn_del']           				= $this->language->get('btn_del');

                
                
		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_cancel'] = $this->language->get('button_cancel');

 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

                
                
                /**
                 *  BREADCRUMBS
                 */
  		$this->data['breadcrumbs'] = array();
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_shipping'),
			'href'      => $this->url->link('extension/shipping', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);		
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('shipping/mondialrelay', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
                
                
		

                
                
                /**
                 *  ACTIONS
                 */
		$this->data['action'] = $this->url->link('shipping/mondialrelay', 'token=' . $this->session->data['token'], 'SSL');		
		$this->data['cancel'] = $this->url->link('extension/shipping', 'token=' . $this->session->data['token'], 'SSL');
		

                
                
                /**
                 *  Initialisation DES VARIABLES IHM 
                 */
		if (isset($this->request->post['MR_est_gratuit'])) {
			$this->data['MR_est_gratuit'] = $this->request->post['MR_est_gratuit'];
		} elseif ($this->config->get('MR_est_gratuit')) {
			$this->data['MR_est_gratuit'] = $this->config->get('MR_est_gratuit');
		} else {
			$this->data['MR_est_gratuit'] = '60';	
		}
		
		if (isset($this->request->post['MR_enseigne_webservice'])) {
			$this->data['MR_enseigne_webservice'] = $this->request->post['MR_enseigne_webservice'];
		} elseif ($this->config->get('MR_enseigne_webservice')) {
			$this->data['MR_enseigne_webservice'] = $this->config->get('MR_enseigne_webservice');
		} else {
			$this->data['MR_enseigne_webservice'] = 'BDTEST12';	
		}
                
                
		if (isset($this->request->post['MR_code_marque'])) {
			$this->data['MR_code_marque'] = $this->request->post['MR_code_marque'];
		} elseif ($this->config->get('MR_code_marque')) {
			$this->data['MR_code_marque'] = $this->config->get('MR_code_marque');
		} else {
			$this->data['MR_code_marque'] = '11';	
		}
                
                
		if (isset($this->request->post['MR_webservice_key'])) {
			$this->data['MR_webservice_key'] = $this->request->post['MR_webservice_key'];
		} elseif ($this->config->get('MR_webservice_key')) {
			$this->data['MR_webservice_key'] = $this->config->get('MR_webservice_key');
		} else {
			$this->data['MR_webservice_key'] = 'MRT_2012';	
		}
                
                
		if (isset($this->request->post['MR_language'])) {
			$this->data['MR_language'] = $this->request->post['MR_language'];
		} elseif ($this->config->get('MR_language')) {
			$this->data['MR_language'] = $this->config->get('MR_language');
		} else {
			$this->data['MR_language'] = '';	
		}
                
                
		if (isset($this->request->post['MR_weight_coefficient'])) {
			$this->data['MR_weight_coefficient'] = $this->request->post['MR_weight_coefficient'];
		} elseif ($this->config->get('MR_weight_coefficient')) {
			$this->data['MR_weight_coefficient'] = $this->config->get('MR_weight_coefficient');
		} else {
			$this->data['MR_weight_coefficient'] = '1';	
		}
             
                
                
		if (isset($this->request->post['MR_civilite'])) {
			$this->data['MR_civilite'] = $this->request->post['MR_civilite'];
		} elseif ($this->config->get('MR_civilite')) {
			$this->data['MR_civilite'] = $this->config->get('MR_civilite');
		} else {
			$this->data['MR_civilite'] = 'MR';	
		}   
                
                
		if (isset($this->request->post['MR_name'])) {
			$this->data['MR_name'] = $this->request->post['MR_name'];
		} elseif ($this->config->get('MR_name')) {
			$this->data['MR_name'] = $this->config->get('MR_name');
		} else {
			$this->data['MR_name'] = '';	
		}
                
                
                
		if (isset($this->request->post['MR_prenom'])) {
			$this->data['MR_prenom'] = $this->request->post['MR_prenom'];
		} elseif ($this->config->get('MR_prenom')) {
			$this->data['MR_prenom'] = $this->config->get('MR_prenom');
		} else {
			$this->data['MR_prenom'] = '';	
		}
                
                
                
		if (isset($this->request->post['MR_rue'])) {
			$this->data['MR_rue'] = $this->request->post['MR_rue'];
		} elseif ($this->config->get('MR_rue')) {
			$this->data['MR_rue'] = $this->config->get('MR_rue');
		} else {
			$this->data['MR_rue'] = '';	
		}   
                
                
		if (isset($this->request->post['MR_cpexe'])) {
			$this->data['MR_cpexe'] = $this->request->post['MR_cpexe'];
		} elseif ($this->config->get('MR_cpexe')) {
			$this->data['MR_cpexe'] = $this->config->get('MR_cpexe');
		} else {
			$this->data['MR_cpexe'] = '';	
		}  
                
		if (isset($this->request->post['MR_villeexe'])) {
			$this->data['MR_villeexe'] = $this->request->post['MR_villeexe'];
		} elseif ($this->config->get('MR_villeexe')) {
			$this->data['MR_villeexe'] = $this->config->get('MR_villeexe');
		} else {
			$this->data['MR_villeexe'] = '';	
		}
		
		if (isset($this->request->post['MR_telexe'])) {
			$this->data['MR_telexe'] = $this->request->post['MR_telexe'];
		} elseif ($this->config->get('MR_telexe')) {
			$this->data['MR_telexe'] = $this->config->get('MR_telexe');
		} else {
			$this->data['MR_telexe'] = '';	
		}
                
                
		if (isset($this->request->post['MR_delay'])) {
			$this->data['MR_delay'] = $this->request->post['MR_delay'];
		} elseif ($this->config->get('MR_delay')) {
			$this->data['MR_delay'] = $this->config->get('MR_delay');
		} else {
			$this->data['MR_delay'] = '';	
		}
                
                
		if (isset($this->request->post['MR_dlv_mode'])) {
			$this->data['MR_dlv_mode'] = $this->request->post['MR_dlv_mode'];
		} elseif ($this->config->get('MR_dlv_mode')) {
			$this->data['MR_dlv_mode'] = $this->config->get('MR_dlv_mode');
		} else {
			$this->data['MR_dlv_mode'] = '24R';	
		}
                
                
		if (isset($this->request->post['MR_col_mode'])) {
			$this->data['MR_col_mode'] = $this->request->post['MR_col_mode'];
		} elseif ($this->config->get('MR_col_mode')) {
			$this->data['MR_col_mode'] = $this->config->get('MR_col_mode');
		} else {
			$this->data['MR_col_mode'] = 'CCC';	
		}
                
                
		if (isset($this->request->post['MR_insurance'])) {
			$this->data['MR_insurance'] = $this->request->post['MR_insurance'];
		} elseif ($this->config->get('MR_insurance')) {
			$this->data['MR_insurance'] = $this->config->get('MR_insurance');
		} else {
			$this->data['MR_insurance'] = '0';	
		}
                
               
                
		if (isset($this->request->post['MR_country_list'])) {
			$this->data['MR_country_list'] = $this->request->post['MR_country_list'];
		} elseif ($this->config->get('MR_country_list')) {
			$this->data['MR_country_list'] = $this->config->get('MR_country_list');
		} else {
			$this->data['MR_country_list'] = 'FR';	
		}     
                
               
                
		if (isset($this->request->post['MR_webservice_url'])) {
			$this->data['MR_webservice_url'] = $this->request->post['MR_webservice_url'];
		} elseif ($this->config->get('MR_webservice_url')) {
			$this->data['MR_webservice_url'] = $this->config->get('MR_webservice_url');
		} else {
			$this->data['MR_webservice_url'] = 'http://www.mondialrelay.fr/WebService/Web_Services.asmx?WSDL';	
		}
		
		
		if (isset($this->request->post['MR_cout'])) {
			$this->data['MR_cout'] = $this->request->post['MR_cout'];
		} elseif ($this->config->get('MR_cout')) {
			$this->data['MR_cout'] = $this->config->get('MR_cout');
		} else {
			$this->data['MR_cout'] = '0';	
		}
                
                
		if (isset($this->request->post['mondialrelay_sort_order'])) {
			$this->data['mondialrelay_sort_order'] = $this->request->post['mondialrelay_sort_order'];
		} elseif ($this->config->get('mondialrelay_sort_order')) {
			$this->data['mondialrelay_sort_order'] = $this->config->get('mondialrelay_sort_order');
		} else {
			$this->data['mondialrelay_sort_order'] = '1';	
		}
		
		
		if (isset($this->request->post['mondialrelay_status'])) {
			$this->data['mondialrelay_status'] = $this->request->post['mondialrelay_status'];
		} else {
			$this->data['mondialrelay_status'] = $this->config->get('mondialrelay_status');
		}
		
                
		$this->data['entry_tax_class'] = $this->language->get('entry_tax_class');
		$this->data['entry_geo_zone'] = $this->language->get('entry_geo_zone');
		$this->data['text_none'] = $this->language->get('text_none');
		$this->data['text_all_zones'] = $this->language->get('text_all_zones');
				
		$this->load->model('localisation/tax_class');		
		$this->data['tax_classes'] = $this->model_localisation_tax_class->getTaxClasses();
		
		$this->load->model('localisation/geo_zone');		
		$this->data['geo_zones'] = $this->model_localisation_geo_zone->getGeoZones();
                

		if (isset($this->request->post['mondialrelay_tax_class_id'])) {
			$this->data['mondialrelay_tax_class_id'] = $this->request->post['mondialrelay_tax_class_id'];
		} else {
			$this->data['mondialrelay_tax_class_id'] = $this->config->get('mondialrelay_tax_class_id');
		}

		if (isset($this->request->post['mondialrelay_geo_zone_id'])) {
			$this->data['mondialrelay_geo_zone_id'] = $this->request->post['mondialrelay_geo_zone_id'];
		} else {
			$this->data['mondialrelay_geo_zone_id'] = $this->config->get('mondialrelay_geo_zone_id');
		}
				
				
                /**
                 *  GENERATION DE LA SORTIE
                 */
		$this->template = 'shipping/mondialrelay.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
	}
	
        
	private function validate() {
		if (!$this->user->hasPermission('modify', 'shipping/mondialrelay')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}
		
		if (!$this->error) {
			return true;
		} else {
			return false;
		}	
	}
	
	
	private function racine() { // 
		$repdepuisracine = '';
		$nbSlashes = substr_count($_SERVER['SCRIPT_NAME'], '/');
		$nbSlashes--;
		$remontee = '';
		for ($i = 0; $i < $nbSlashes; $i++) {
			$remontee .= '../';
		}
		return $remontee . $repdepuisracine;
	}	
	
	function stripAccentsUtf8($string)
	{
		$string = mb_strtolower($string, 'UTF-8');
		$string = str_replace(
			array(
				'à', 'â', 'ä', 'á', 'ã', 'å',
				'î', 'ï', 'ì', 'í',
				'ô', 'ö', 'ò', 'ó', 'õ', 'ø',
				'ù', 'û', 'ü', 'ú',
				'é', 'è', 'ê', 'ë',
				'ç', 'ÿ', 'ñ',
			),
			array(
				'a', 'a', 'a', 'a', 'a', 'a',
				'i', 'i', 'i', 'i',
				'o', 'o', 'o', 'o', 'o', 'o',
				'u', 'u', 'u', 'u',
				'e', 'e', 'e', 'e',
				'c', 'y', 'n',
			),
			$string
		);

		return $string;
	}
	
	private function removeaccents($string){
		$string	= 	$this->stripAccentsUtf8($string);
		return $string;
	}
	  
	private function formatMaj($string){
		$string= strtoupper($this->removeaccents($string));
		return $string;
	}
}
?>