﻿<?php
/*
 *
 * Copyright (c) ATRAGENE SARL
 * @website: www.atragene.com
 * @email: contact@atragene.com
 * @author: olivier.lemaitre@atragene.com
 * @company: Atragene Conseil
 * @version: 1.7.4
 * @date: 03/12/2015
 *
 */
class ControllerCheckoutEnvoismoinscher extends Controller {
	private $version_platform = VERSION;
	private $platform = 'opencart';
	private $version_lib = '1.1.4';

	public function getDepotPoint () {
		$json = array();
			
			if( isset( $_GET['service_code'] ) ) {
                $service_code_temp = $_GET['service_code'];
				$tab_service_code = explode( "_", $service_code_temp );
				$operator = $tab_service_code[0];
				$service = str_replace( "envoismoinscher.", "", $tab_service_code[1] );
				$service_chiffre = $service.'_'.$tab_service_code[2];
				$code = $tab_service_code[1].'_'.$tab_service_code[2];
				
				unset($this->session->data['shipping_method_adress']);
							
				$this->load->language('shipping/envoismoinscher');

				$userData = array(
					"login" => $this->config->get('envoismoinscher_login'),
					"password" => $this->config->get('envoismoinscher_pwd'),
					"api_key" => $this->config->get('envoismoinscher_key')
				);
							
				require_once(DIR_SYSTEM.'library/emc/WebService.php');
				require_once(DIR_SYSTEM.'library/emc/Carrier.php');
				require_once(DIR_SYSTEM.'library/emc/ContentCategory.php');
				require_once(DIR_SYSTEM.'library/emc/Country.php');
				require_once(DIR_SYSTEM.'library/emc/ListPoints.php');
				require_once(DIR_SYSTEM.'library/emc/OrderStatus.php');
				require_once(DIR_SYSTEM.'library/emc/ParcelPoint.php');
				require_once(DIR_SYSTEM.'library/emc/Quotation.php');
				require_once(DIR_SYSTEM.'library/emc/Service.php');
				require_once(DIR_SYSTEM.'library/emc/User.php');
				
				// Précision de l'expéditeur et du destinataire
				if ($this->customer->isLogged()) {
					
					$address = $this->db->query("SELECT * FROM ".DB_PREFIX."address a, ".DB_PREFIX."country c WHERE a.country_id = c.country_id AND address_id =".$this->session->data['shipping_address_id']);
	
					$to = array(
						"pays" => $address->row['iso_code_2'], 
						"code_postal" => $address->row['postcode'], 
						"ville" => $address->row['city'], 
					);
				} elseif (isset($this->session->data['guest'])) {
					
					$to = array(
						"pays" => $_SESSION['guest']['shipping']['iso_code_2'], 
						"code_postal" => $_SESSION['guest']['shipping']['postcode'], 
						"ville" => $_SESSION['guest']['shipping']['city'], 
					);
				}
						
				$lpCl = new Env_ListPoints(array("user" => $userData["login"], "pass" => $userData["password"], "key" => $userData["api_key"]));
				$lpCl->setPlatformParams($this->platform, $this->version_platform, $this->version_lib);
				$lpCl->setEnv($this->config->get('envoismoinscher_environment'));   
				
				$params = array('srv_code' => $service, 'collecte' => 'dest', 'pays' => $to['pays'], 'cp' => $to['code_postal'], 'ville' => $to['ville']);                               
				$lpCl->getListPoints($operator, $params);
				
				$tab_listpoint = '<table class="tab_pointRelais radio" id="pointRelais'.$operator.'_'.$service.'"><tbody>';
				$_SESSION['shipping_methods']['envoismoinscher']['quote'][$service_chiffre]['relai_point'] = array();
				$i = 0;
				
				if(!empty($lpCl->listPoints))
				{
					foreach ($lpCl->listPoints as $l => $liste){
						$tab_listpoint .= '<tr class="descPointsRelai"><td></td><td class="choixPointRelai"><input type="radio" name="shipping_method" value="'.$code.'.'.$liste['code'].'" id="'.$code.'.'.$liste['code'].'" /></td><td><label for="'.$code.'.'.$liste['code'].'">'.$liste['name'].'</label></td><td style="text-align: right;"><ul><li>'.$liste['address'].'</li><li>'.$liste['city'].'</li><li>'.$liste['zipcode'].'</li><li>'.$liste['country'].'</li></ul></td></tr>';
						
						$_SESSION['shipping_methods']['envoismoinscher']['quote'][$service_chiffre]['relai_point'][$liste['code']] = array();
						$_SESSION['shipping_methods']['envoismoinscher']['quote'][$service_chiffre]['relai_point'][$liste['code']]['code'] = $liste['code'];
						$_SESSION['shipping_methods']['envoismoinscher']['quote'][$service_chiffre]['relai_point'][$liste['code']]['name'] = $liste['name'];
						$_SESSION['shipping_methods']['envoismoinscher']['quote'][$service_chiffre]['relai_point'][$liste['code']]['address'] = $liste['address'];
						$_SESSION['shipping_methods']['envoismoinscher']['quote'][$service_chiffre]['relai_point'][$liste['code']]['city'] = $liste['city'];
						$_SESSION['shipping_methods']['envoismoinscher']['quote'][$service_chiffre]['relai_point'][$liste['code']]['zipcode'] = $liste['zipcode'];
						$_SESSION['shipping_methods']['envoismoinscher']['quote'][$service_chiffre]['relai_point'][$liste['code']]['country'] = $liste['country'];
						
						$i = $i+1;                                                     
					}				
					$tab_listpoint .= "</tbody></table>";
					
					$json['success'] = $tab_listpoint;
					$json['code_service'] = 'pointRelais'.$operator.'_'.$service;
					$json['first_code'] = $code;
					
				} else {				
					$json['error']['message'] = 'Les points relais de ce transporteur sont actuellement indisponibles';
				}
				
			} else {
				$json['error']['message'] = 'Service_code undefined';
			}		
						
			$this->response->setOutput(json_encode($json));
	}
}
?>