* @package icirelais_shipping_mode * @copyright Bysoft 2010 * */ class icirelais_get_point { /** * The configuration key list for ICI Relais shipping mode saved in database * @access protected * @var string array of string values */ var $_configuration_key = array(); /** * @var string the customer's address */ var $address; /** * @var integer the customer's zipcode */ var $zipcode; /** * @var string the customer's city */ var $city; /** * @var string the closing period list in string format */ var $str_closing_period; /** * @var string ICI Relay's id * @access protected */ var $shipping_methode_code; //-------------------------------------------------------------------------------------------------------------- // CONSTRUCTEUR //-------------------------------------------------------------------------------------------------------------- /** * Initialize datas from database * - get configuration keys for ICI Relais shipping mode * - get the customer's address * * @param array contain the customer's id */ function icirelais_get_point($input){ // Get ICI relais configuration key from database $query = "SELECT configuration_key, configuration_value FROM configuration WHERE configuration_key LIKE 'MODULE_SHIPPING_ICIRELAIS_%'"; $get_configuration_value = tep_db_query($query); while($row = tep_db_fetch_array($get_configuration_value)){ $this->_configuration_key[$row['configuration_key']] = $row['configuration_value']; } // Get the customer's address $query = "SELECT entry_street_address, entry_postcode, entry_city FROM address_book WHERE entry_country_id = '73' AND customers_id =".$input['customer_id']; if(strlen($input['sendto']) > 0 && intval($input['sendto'])) { $query .= " AND address_book_id=".$input['sendto']; } $get_customer_address = tep_db_query($query); while($row = tep_db_fetch_array($get_customer_address)){ $this->address = $row['entry_street_address']; $this->zipcode = trim($row['entry_postcode']); $this->city = $row['entry_city']; } $this->shipping_methode_code = $input['shipping_methode_code']; require_once('includes/languages/'.$input['language'].'/modules/shipping/icirelais.php'); } //-------------------------------------------------------------------------------------------------------------- // GET ICI RELAIS POINT FROM MYPUDO OR BACKUP (DATABASE) //-------------------------------------------------------------------------------------------------------------- /** * Get the 10 nearer ici relais point from the customer's address * by the webservice MyPudo or by the database backup if the webservice is unvalable * * @return array contain the structure of the ici relais list for the front office */ function get_point(){ // Array returned with all ICI Relais points. To be complete with this function. $quote = array( 'id' => $this->shipping_methode_code, 'module' => MODULE_SHIPPING_ICIRELAIS_TEXT_TITLE, 'methods' => array()); // Zip code is mandatory if(empty($this->zipcode)){ $quote['error'] = TEXT_POSTALCODE_MISSING; return $quote; } // If empty(cp+ville) ==> do not call the webservice for nothing and get from database's backup if(empty($this->city)){ $relais_items = $this->get_point_backup(); } else { // MyPudo WEBSERVICE CALL if(extension_loaded('soap')) { $client = new SoapClient ( $this->_configuration_key['MODULE_SHIPPING_ICIRELAIS_MYPUDO_URL'], array('connection_timeout' => 30) ); $parameters = array ( 'firmID' => $this->_configuration_key['MODULE_SHIPPING_ICIRELAIS_ID_MARCHAND'], 'securityKey' => $this->_configuration_key['MODULE_SHIPPING_ICIRELAIS_SECURITY_KEY'], 'strAddress' => utf8_encode($this->address), 'strZIPCode' => $this->zipcode, 'strCity' => utf8_encode($this->city), 'requestID' => '' ); // limit end of call waiting time (coupled with connection_timeout) ini_set('default_socket_timeout', 10); $result = ( array ) $client->GetSiteIDs_By_AddressInfos_As_XmlResponse($parameters, false, true); $result = ( array ) $result ['GetSiteIDs_By_AddressInfos_As_XmlResponseResult']; } if($result){ // Get the webservice response values $xml = new SimpleXMLElement ( $result ['any'] ); $quality = $xml->xpath ( 'RESPONSE_QUALITY' ); } // Call backup database if : // - no webservice result // - soap fault exception // - quality indicator = 0 // - xml error // - no MyPudo idem if(!$result || is_soap_fault ( $result ) || ( int ) $quality [0] == 0 || $xml->xpath ( 'ERROR' ) || is_null ( $xml->xpath ( 'PUDO_ITEM' ) )){ $relais_items = $this->get_point_backup(); } else { $relais_items = $xml->xpath ( 'PUDO_ITEM' ); } } // Reorganize de ici relais points by distance $relais_items = $this->reorganize_id_list_by_distance($relais_items); ksort($relais_items); if (!$relais_items) { // No ici relais point from MyPudo nor database backup $quote['error'] = TEXT_NO_ICIRELAIS_POINT; } else { // Filter ici relais points to display the 5 first one $quote['methods'] = $this->filter_points ( $relais_items ); } return $quote; } //-------------------------------------------------------------------------------------------------------------- // GET ICI RELAIS POINT BACKUP (DATABASE) //-------------------------------------------------------------------------------------------------------------- /** * Get ici relais point from backup database * * @return array list of ici relais points id */ function get_point_backup() { $query = "SELECT relay_id, sort_order, distance FROM icibackup WHERE postal_code ='".$this->zipcode."' ORDER BY distance ASC"; $get_icibackup_id = tep_db_query($query); $i = 0; while($row = tep_db_fetch_array($get_icibackup_id)){ $relais_items[$i]['PUDO_ID'] = $row['relay_id']; $relais_items[$i]['ORDER'] = $row['sort_order']; $relais_items[$i]['DISTANCE'] = $row['distance']; $i++; } return $relais_items; } //-------------------------------------------------------------------------------------------------------------- // FILTER THE 10 ICI RELAIS POINTS //-------------------------------------------------------------------------------------------------------------- /** * Get each ici relais point detail, filter them by closing period * If the point is good and if we have less than 6 points, we prepare the display list and the detail in a html div * * @return array part of ici relais points list array */ function filter_points($relais_items){ $quote['methods'] = array(); $offset = 1; foreach ( $relais_items as $items ) { $point = array(); $this->str_closing_period = ''; // Search the ici relais point detail $query = " SELECT relay_id, shop_name, adresse_1, adresse_2, adresse_3, postal_code, city, coord_lat, coord_long, monday_opening_hours, tuesday_opening_hours, wednesday_opening_hours, thursday_opening_hours, friday_opening_hours, saturday_opening_hours, sunday_opening_hours, closing_period_start_date_1, closing_period_end_date_1, closing_period_start_date_2, closing_period_end_date_2, closing_period_start_date_3, closing_period_end_date_3 FROM icirelais WHERE relay_id LIKE '".$items['PUDO_ID']."'"; $get_point_detail = tep_db_query($query); // Only if the point is in database, we propose it if($point = tep_db_fetch_array($get_point_detail)){ // Is between closing period and closing period > 4 days ==> we don't propose le ICI Relais point if($this->is_between_closing_period ( $point, 1 ) || $this->is_between_closing_period ( $point, 2 ) || $this->is_between_closing_period ( $point, 3 )) { continue; } // Construct the closing period info the display in the point's detail if(strlen($this->str_closing_period) > 0){ $this->str_closing_period = TEXT_WARNING_ICIRELAIS_POINT_CLOSED.$this->str_closing_period; $display = 'block'; } else { $display = 'none'; } // We display only 5 ICI Relais points if ($offset <= 5) { // ICI Relais list $title = ''; // window : left column $title.= ''; $quote['methods'][] = array('id' => $point['relay_id'], // text : display in the delivery's sumup insted of title (html code on't be in the table order_total) 'text' => TEXT_ICIRELAIS_POINT . $point['shop_name'] . ' (' . $point['relay_id'] . ')', 'title' => $title, 'cost' => MODULE_SHIPPING_ICIRELAIS_COST); $offset++; } else { break; } } } return $quote['methods']; } //-------------------------------------------------------------------------------------------------------------- // OPENING HOURS FORMATTING //-------------------------------------------------------------------------------------------------------------- /** * Opening hours string list for a day * * @return string de la forme 09:00-12:30 & 14:00- 17:30 ou Fermé & 14:00- 17:30 */ function get_opening_hours ($opening_hours){ $opening_hours_tab = explode ( " ", $opening_hours ); if (($opening_hours_tab [0] == "00:00-00:00") && ($opening_hours_tab [1] == "00:00-00:00")) { $hours = TEXT_CLOSED; } else { if ($opening_hours_tab [0] == "00:00-00:00") { $hours = TEXT_CLOSED . ' & ' . $opening_hours_tab [1]; } if ($opening_hours_tab [1] == "00:00-00:00") { $hours = $opening_hours_tab [0] . ' & '.TEXT_CLOSED; } if ($opening_hours_tab [0] != "00:00-00:00" && $opening_hours_tab [1] != "00:00-00:00") $hours = $opening_hours_tab [0] . ' & ' . $opening_hours_tab [1]; } return $hours; } //-------------------------------------------------------------------------------------------------------------- // IS BETWEEN CLOSING PERIOD //-------------------------------------------------------------------------------------------------------------- /** * Determine if the potential shipping date to the ici relais point will be in a closing period * * @return boolean */ function is_between_closing_period($point,$num_period) { // ICI Relais needs 12 days of treatment + number of additional treatment day specified in the back office $max = $this->_configuration_key['MODULE_SHIPPING_ICIRELAIS_ADDITIONAL_TREATMENT_DAY'] + 12 ; //12 days of treatment for ICI Relais $now = time(); $deadline = strtotime('+'.$max.' days'); $start=strtotime($this->convertdateformat($point['closing_period_start_date_'.$num_period])); $end = strtotime($this->convertdateformat($point['closing_period_end_date_'.$num_period])); if (($deadline >= $start && $deadline <= $end) || ($now >= $start && $now <= $end) || ($now < $start && $deadline > $end) ) { // The ICI relais point is closed ! // so we conserve the closing date(s) if($this->str_closing_period) { $this->str_closing_period .= ' '.TEXT_AND.'
'; } $this->str_closing_period .= TEXT_FROM.' '.date("d/m/Y",$start).' '.TEXT_TO.' '. date("d/m/Y",$end); // even if it's closed, if the closed period is less than 4 days // we consider the it's not between closing period (so open) if($this->is_closed_more_than_4_days($point,$num_period)) { return false; } return true; }else { // The ICI relais point is open ! return false; } } //-------------------------------------------------------------------------------------------------------------- // IS CLOSED MORE THAN 4 DAYS //-------------------------------------------------------------------------------------------------------------- /** * Determine if the ici relais point is closed more than 4 days * * @return boolean */ function is_closed_more_than_4_days($point, $num_period) { $minimal_period = 4*24*60*60; // 4 days $start = strtotime($this->convertdateformat($point['closing_period_start_date_'.$num_period])); $end = strtotime($this->convertdateformat($point['closing_period_end_date_'.$num_period])); $closing_period = $end - $start; if($closing_period <= $minimal_period) { return true; } return false; } //-------------------------------------------------------------------------------------------------------------- // REORGANIZE ICI RELAIS ID LIST BY DISTANCE //-------------------------------------------------------------------------------------------------------------- /** * Determine if the potential shipping date to the ici relais point will be in a closing period * * @return array */ function reorganize_id_list_by_distance($result_items){ $sorted = array(); foreach ($result_items as $item){ $item = ( array ) $item; $distance = $item['DISTANCE']; $sorted[$distance] = $item; } return $sorted; } //-------------------------------------------------------------------------------------------------------------- // CONVERT DATE //-------------------------------------------------------------------------------------------------------------- /** * convert a date from dd/mm/aaaa to aaaa-mm-dd * * @return date */ function convertdateformat($date){ if(!empty($date)) { $date1= explode("/",$date); return $date1[2].'-'.$date1[1].'-'.$date1[0]; } } } ?>