name = 'colissimorelay'; $this->tab = 'shipping_logistics'; $this->version = '2.3.8'; $this->author = 'Agencya'; $this->module_key = 'd0800c724dd81ee58f3d8556e8f9f981'; $config = Configuration::getMultiple(array('COLISSIMORELAY_NUM_CONTRAT', 'COLISSIMORELAY_PASS')); if (isset($config['COLISSIMORELAY_NUM_CONTRAT'])) { $this->colissimo_num_contrat = $config['COLISSIMORELAY_NUM_CONTRAT']; } if (isset($config['COLISSIMORELAY_PASS'])) { $this->colissimo_pass = $config['COLISSIMORELAY_PASS']; } $this->bootstrap = true; parent::__construct(); $this->url = $this->context->shop->getBaseURL(true).'modules/'.$this->name.'/'; $this->displayName = $this->l('Colissimo • Relay'); $this->description = $this->l('Allow your customers to select a pickup point.'); $this->confirmUninstall = $this->l('Are you sure to uninstall this module ?'); if (!isset($this->colissimo_num_contrat) || !isset($this->colissimo_pass) || empty($this->colissimo_num_contrat) || empty($this->colissimo_pass)) { $this->warning = $this->l('Please enter your La Poste account number and your password in module configuration to use this module.'); } $this->ps_versions_compliancy = array('min' => '1.5.0.0', 'max' => _PS_VERSION_); // translations // $this->l('Closed'); } /** * @return bool */ public function install() { Configuration::updateValue('COLISSIMORELAY_MAPPING_CARRIERS', serialize(array())); Configuration::updateValue('COLISSIMORELAY_NUM_CONTRAT', ''); Configuration::updateValue('COLISSIMORELAY_PASS', ''); Configuration::updateValue('COLISSIMORELAY_GMAPS_API_KEY', ''); if (version_compare(_PS_VERSION_, '1.6.0.9', '<')) { return ( parent::install() && $this->installDB() && $this->registerHook('extraCarrier') && $this->registerHook('actionValidateOrder') && $this->registerHook('displayHeader') && $this->registerHook('displayBackOfficeHeader') && $this->registerHook('adminOrder') && $this->registerHook('actionCarrierProcess') ); } else { return ( parent::install() && $this->installDB() && $this->registerHook('extraCarrier') && $this->registerHook('actionValidateOrder') && $this->registerHook('displayHeader') && $this->registerHook('displayBackOfficeHeader') && $this->registerHook('displayAdminOrderTabShip') && $this->registerHook('displayAdminOrderContentShip') && $this->registerHook('actionCarrierProcess') ); } } /** * @return bool */ public function installDB() { return Db::getInstance()->execute( 'CREATE TABLE IF NOT EXISTS`'._DB_PREFIX_.'colissimo_pickup` ( `id_pickup` INT(10) NOT NULL AUTO_INCREMENT, `id_cart` INT(10) NOT NULL, `id_point_pickup` INT(10) NOT NULL, `date_add` DATETIME NOT NULL, `date_upd` DATETIME NOT NULL, PRIMARY KEY ( `id_pickup` ) ) ENGINE = '._MYSQL_ENGINE_.' DEFAULT CHARSET = utf8;' ); } /** * @return bool */ public function uninstall() { if (Shop::isFeatureActive()) { Shop::setContext(Shop::CONTEXT_ALL); } Configuration::deleteByName('COLISSIMORELAY_MAPPING_CARRIERS'); Configuration::deleteByName('COLISSIMORELAY_NUM_CONTRAT'); Configuration::deleteByName('COLISSIMORELAY_PASS'); Configuration::deleteByName('COLISSIMORELAY_GMAPS_API_KEY'); Db::getInstance()->execute('DROP TABLE IF EXISTS `'._DB_PREFIX_.'colissimo_pickup`'); return parent::uninstall(); } /** * @param array $params */ public function hookHeader($params) { if (in_array(Tools::strtolower($this->context->controller->php_self), array('order', 'order-opc', 'orderopc'))) { try { // if merchant hasnt configured the module if (!isset($this->colissimo_num_contrat) || !isset($this->colissimo_pass) || empty($this->colissimo_num_contrat) || empty($this->colissimo_pass)) { throw new PrestaShopModuleException($this->l('Colissimo pickup module is not configured.')); } // load customer address $address = new Address((int)$params['cart']->id_address_delivery); if (Validate::isLoadedObject($address)) { $customer_address = array( 'address1' => $address->address1, 'postcode' => $address->postcode, 'city' => $address->city, 'country' => $address->country, 'countryCode' => Country::getIsoById($address->id_country), ); } else { $customer_address = null; } // get list of pickup carriers ID mapped in module configuration $pickupCarriers = array(); $colissimoCarriers = Tools::unSerialize(Configuration::get('COLISSIMORELAY_MAPPING_CARRIERS')); $carriers = Carrier::getCarriers($this->context->language->id, false, false, false, null, Carrier::ALL_CARRIERS); foreach ($carriers as $carrier) { if (array_key_exists((int)$carrier['id_reference'], $colissimoCarriers)) { if ((int)$colissimoCarriers[(int)$carrier['id_reference']] !== 0) { $pickupCarriers[] = (int)$carrier['id_carrier']; } } } // if no pickup carriers available, return if (empty($pickupCarriers)) { return; } $this->context->controller->addCSS($this->_path . 'views/css/colissimo_carrier.css'); $this->context->controller->addJquery(); $this->context->controller->addJqueryPlugin('fancybox'); if (version_compare(_PS_VERSION_, '1.7', '>=')) { $this->context->controller->addJS($this->_path . 'views/js/colissimo_carrier_17.js'); } else { $this->context->controller->addJS($this->_path . 'views/js/colissimo_carrier.js'); } $this->context->smarty->assign( array( 'colissimoError' => false, 'colissimo_token' => Tools::substr(Tools::encrypt('colissimorelay/index'), 0, 10), 'id_cart' => $params['cart']->id, 'id_carrier' => $params['cart']->id_carrier ?: 0, 'customer_address' => $customer_address, 'customer_address_json' => json_encode($customer_address), 'maps_api_key' => (string)Configuration::get('COLISSIMORELAY_GMAPS_API_KEY'), 'pickup_carriers' => json_encode($pickupCarriers), 'available_countries' => json_encode(ColissimoRelay_LaPoste::getAvailableCountries()), 'is_logged' => (int)$this->context->customer->isLogged(), ) ); if (version_compare(_PS_VERSION_, '1.7', '<')) { $this->context->smarty->assign( array( 'user_lang' => Tools::strtolower($this->context->language->iso_code), ) ); } } catch (Exception $exception) { $this->context->smarty->assign( array( 'colissimoError' => $exception->getMessage(), ) ); } if (version_compare(_PS_VERSION_, '1.7', '>=')) { return $this->display(__FILE__, 'header_17.tpl'); } else { return $this->display(__FILE__, 'header.tpl'); } } } /** * @param array $params */ public function hookDisplayBackOfficeHeader($params) { if (Tools::strtolower(Tools::getValue('controller')) == 'adminorders' && Tools::getIsset('id_order')) { $id_carrier = (int)$params['cart']->id_carrier; if (ColissimoRelay_LaPoste::getProductIdByCarrierId($id_carrier)) { $this->context->controller->addCSS($this->_path . 'views/css/colissimorelay.css'); $this->context->controller->addCSS($this->_path . 'views/css/colissimo_carrier.css'); $this->context->controller->addJquery(); $this->context->controller->addJqueryPlugin('fancybox'); $this->context->controller->addJS($this->_path . 'views/js/colissimo_order.js'); try { // if merchant hasnt configured the module if (!isset($this->colissimo_num_contrat) || !isset($this->colissimo_pass) || empty($this->colissimo_num_contrat) || empty($this->colissimo_pass)) { throw new PrestaShopModuleException($this->l('Colissimo pickup module is not configured.')); } // load customer address $order = new Order((int)Tools::getValue('id_order')); $address = new Address($order->id_address_delivery); if (!Validate::isLoadedObject($address)) { throw new PrestaShopModuleException('Can\'t load Address object.'); } $this->context->smarty->assign( array( 'colissimoError' => false, 'colissimo_token' => Tools::substr(Tools::encrypt('colissimorelay/index'), 0, 10), 'id_cart' => $params['cart']->id, 'id_carrier' => $params['cart']->id_carrier ?: 0, 'customer_address' => array( 'address1' => $address->address1, 'postcode' => $address->postcode, 'city' => $address->city, 'country' => $address->country, ), 'customer_address_json' => json_encode( array( 'address1' => $address->address1, 'postcode' => $address->postcode, 'city' => $address->city, 'countryCode' => Country::getIsoById($address->id_country), ) ), 'maps_api_key' => Configuration::get('COLISSIMORELAY_GMAPS_API_KEY'), 'pickup_carriers' => 0, 'available_countries' => json_encode(ColissimoRelay_LaPoste::getAvailableCountries()), 'modules_dir' => _MODULE_DIR_, 'user_lang' => Tools::strtolower($this->context->language->iso_code), ) ); } catch (Exception $exception) { $this->context->smarty->assign( array( 'colissimoError' => $exception->getMessage(), ) ); } return $this->display(__FILE__, 'header.tpl'); } } } /** * @param array $params * @return string */ public function hookExtraCarrier($params) { if (version_compare(_PS_VERSION_, '1.7', '<')) { return $this->display(__FILE__, 'carrier-extra.tpl'); } } /** * @param array $params * @return string */ public function hookDisplayAdminOrderTabShip($params) { if (Tools::version_compare(_PS_VERSION_, '1.6', '<')) { $id_carrier = (int)$params['order']->id_carrier; } else { $id_carrier = (int)$params['cart']->id_carrier; } if (ColissimoRelay_LaPoste::getProductIdByCarrierId($id_carrier)) { return $this->display(__FILE__, 'order-shipping-tab.tpl'); } } /** * @param array $params * @return string */ public function hookDisplayAdminOrderContentShip($params) { if (Tools::version_compare(_PS_VERSION_, '1.6.0.9', '<')) { $order = new Order((int)$params['id_order']); $id_cart = (int)$order->id_cart; $id_carrier = (int)$order->id_carrier; unset($order); } else { $id_cart = (int)$params['cart']->id; $id_carrier = (int)$params['cart']->id_carrier; } if (ColissimoRelay_LaPoste::getProductIdByCarrierId($id_carrier)) { $pickup = new ColissimoPickup(ColissimoPickup::getIdByCartId($id_cart)); if (!Validate::isLoadedObject($pickup)) { $pickup = null; } $this->context->smarty->assign( array( 'colissimo_img' => $this->url.'views/img/', 'pickup' => $pickup, ) ); if (Tools::version_compare(_PS_VERSION_, '1.6', '<')) { return $this->display(__FILE__, 'admin-order.tpl'); } else { return $this->display(__FILE__, 'order-shipping-content.tpl'); } } } /** * @param array $params * @return string */ public function hookAdminOrder($params) { return $this->hookDisplayAdminOrderContentShip($params); } /** * Throw error if customer didn't selected a pickup point * * @param array $params */ public function hookActionCarrierProcess($params) { if (version_compare(_PS_VERSION_, '1.7', '<') && get_class($this->context->controller) === 'OrderController') { if (ColissimoRelay_LaPoste::isPickup(ColissimoRelay_LaPoste::getProductIdByCarrierId((int)$params['cart']->id_carrier)) && !ColissimoPickup::getIdByCartId((int)$params['cart']->id)) { die($this->l('Erreur: Vous n\'avez choisi aucun point relais sur votre commande. Merci de désactiver tout bloqueur de publicité ou de script. En cas de doute, merci de nous contacter.')); } } } /** * Update cart address with pickup point information * * @param array $params */ public function hookActionValidateOrder($params) { if (ColissimoRelay_LaPoste::isPickup(ColissimoRelay_LaPoste::getProductIdByCarrierId((int)$params['cart']->id_carrier))) { $cart = $params['cart']; $order = $params['order']; $pickup = new ColissimoPickup(ColissimoPickup::getIdByCartId($cart->id)); if (!Validate::isLoadedObject($pickup) || $pickup->id_point_pickup === 0) { return; } $ws = new Colissimo_PickupWebservice(); $point_infos = $ws->getDeliveryPoint(sprintf('%06s', $pickup->id_point_pickup)); if ($cart->id_address_invoice === $cart->id_address_delivery) { $address = new Address(); } else { $address = new Address($cart->id_address_delivery); } $customer = new Customer($cart->id_customer); $address->firstname = $customer->firstname; $address->lastname = $customer->lastname; $address->id_customer = $cart->id_customer; $address->id_country = Country::getByIso((string)$point_infos->codePays); $address->alias = Tools::substr((string)$point_infos->nom, 0, 32); $address->address1 = Tools::substr((string)$point_infos->adresse1 . ' ' . (string)$point_infos->adresse2, 0, 35); $address->address2 = (string)$point_infos->nom; $address->postcode = (string)$point_infos->codePostal; $address->city = (string)$point_infos->localite; if ($cart->id_address_invoice === $cart->id_address_delivery) { $old_address = new Address($cart->id_address_delivery); $address->phone = $old_address->phone; $address->phone_mobile = $old_address->phone_mobile; $address->add(); } else { $address->update(); } $cart->id_address_delivery = $address->id; $cart->update(); $order->id_address_delivery = $address->id; $order->update(); Db::getInstance()->execute( 'UPDATE `'._DB_PREFIX_.'cart_product` SET `id_address_delivery` = '.(int)$address->id.' WHERE `id_cart` = '.(int)$cart->id ); Db::getInstance()->execute( 'UPDATE `'._DB_PREFIX_.'customization` SET `id_address_delivery` = '.(int)$address->id.' WHERE `id_cart` = '.(int)$cart->id ); } } /** * @return string */ public function getContent() { $message = false; if (Tools::isSubmit('submit'.$this->name)) { Configuration::updateValue('COLISSIMORELAY_MAPPING_CARRIERS', serialize(Tools::getValue('COLISSIMORELAY_MAPPING_CARRIERS'))); Configuration::updateValue('COLISSIMORELAY_NUM_CONTRAT', Tools::getValue('COLISSIMORELAY_NUM_CONTRAT')); Configuration::updateValue('COLISSIMORELAY_PASS', Tools::getValue('COLISSIMORELAY_PASS')); Configuration::updateValue('COLISSIMORELAY_GMAPS_API_KEY', Tools::getValue('COLISSIMORELAY_GMAPS_API_KEY')); $message = $this->displayConfirmation($this->l('The configuration has been updated.')); } $this->context->smarty->assign( array( 'is_configured' => (!isset($this->colissimo_num_contrat) || !isset($this->colissimo_pass) || empty($this->colissimo_num_contrat) || empty($this->colissimo_pass)), 'tracking_conf' => $this->context->link->getAdminLink('AdminModules').'&configure=colissimosuivi', ) ); if (Tools::version_compare(_PS_VERSION_, '1.6', '<')) { return $message.$this->display(__FILE__, 'configuration_infos.tpl').$this->renderForm(); } else { return $message.$this->display(__FILE__, 'configuration_infos.tpl').$this->renderForm(); } } /** * @return string */ public function renderForm() { $default_lang = (int)Configuration::get('PS_LANG_DEFAULT'); $fields_form = array(); $fields_form[0]['form'] = array( 'legend' => array( 'title' => $this->l('Carriers configuration'), 'icon' => 'icon-truck' ), ); $fields_form[0]['form']['input'] = array(); foreach (Carrier::getCarriers($this->context->language->id, false, false, false, null, Carrier::ALL_CARRIERS) as $carrier) { $fields_form[0]['form']['input'][] = array( 'type' => 'select', 'label' => $carrier['name'], 'name' => 'COLISSIMORELAY_MAPPING_CARRIERS['.$carrier['id_reference'].']', 'options' => array( 'query' => ColissimoRelay_LaPoste::getProducts(), 'id' => 'id', 'name' => 'name' ), ); } $fields_form[0]['form']['submit'] = array( 'title' => $this->l('Save'), ); $fields_form[2]['form'] = array( 'legend' => array( 'title' => $this->l('La Poste contract configuration'), 'icon' => 'icon-file' ), 'input' => array( array( 'type' => 'text', 'label' => $this->l('Coliposte login'), 'name' => 'COLISSIMORELAY_NUM_CONTRAT', 'required' => true ), array( 'type' => 'text', 'label' => $this->l('Coliposte password'), 'name' => 'COLISSIMORELAY_PASS', 'required' => true ), array( 'type' => 'text', 'label' => $this->l('Google Maps API key'), 'name' => 'COLISSIMORELAY_GMAPS_API_KEY', 'desc' => ''.$this->l('Click here to generate your Google Maps API.').'', ) ), 'submit' => array( 'title' => $this->l('Save'), ) ); $helper = new HelperForm(); $helper->module = $this; $helper->name_controller = $this->name; $helper->token = Tools::getAdminTokenLite('AdminModules'); $helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name; $helper->default_form_language = $default_lang; $helper->allow_employee_form_lang = $default_lang; $helper->title = $this->displayName; $helper->show_toolbar = true; $helper->toolbar_scroll = true; $helper->submit_action = 'submit'.$this->name; $helper->toolbar_btn = array( 'save' => array( 'desc' => $this->l('Save'), 'href' => AdminController::$currentIndex.'&configure='.$this->name.'&save'.$this->name.'&token='.Tools::getAdminTokenLite('AdminModules'), ), 'back' => array( 'href' => AdminController::$currentIndex.'&token='.Tools::getAdminTokenLite('AdminModules'), 'desc' => $this->l('Back to list') ) ); $current = Tools::unSerialize(Configuration::get('COLISSIMORELAY_MAPPING_CARRIERS')); foreach (Carrier::getCarriers($this->context->language->id, false, false, false, null, Carrier::ALL_CARRIERS) as $carrier) { if (isset($current[$carrier['id_reference']])) { $helper->fields_value['COLISSIMORELAY_MAPPING_CARRIERS['.$carrier['id_reference'].']'] = $current[$carrier['id_reference']]; } else { $helper->fields_value['COLISSIMORELAY_MAPPING_CARRIERS['.$carrier['id_reference'].']'] = 0; } } $helper->fields_value['COLISSIMORELAY_NUM_CONTRAT'] = Configuration::get('COLISSIMORELAY_NUM_CONTRAT'); $helper->fields_value['COLISSIMORELAY_PASS'] = Configuration::get('COLISSIMORELAY_PASS'); $helper->fields_value['COLISSIMORELAY_GMAPS_API_KEY'] = Configuration::get('COLISSIMORELAY_GMAPS_API_KEY'); return $helper->generateForm($fields_form); } }