346 lines
12 KiB
PHP
Raw Permalink Normal View History

<?php
require_once(dirname(__FILE__).'/../../config/config.inc.php');
require_once(dirname(__FILE__).'/../../init.php');
include_once(dirname(__FILE__).'/mondialrelay.php');
$controller->preProcess();
$useSSL = TRUE;
$mrSelection = new MrSelection();
$mrSelection->run();
class MrSelection {
private $mr_module = false;
private $id_lang = 2;
public function run(){
global $smarty;
$this->displayHeader();
$this->process();
$this->displayFooter();
}
public function displayHeader(){
Tools::addCSS(_THEME_CSS_DIR_.'addresses.css');
Tools::addCSS(_PS_CSS_DIR_.'jquery.fancybox-1.3.4.css', 'screen');
// Adding JS files
Tools::addJS(_THEME_JS_DIR_.'tools.js');
Tools::addJS(_PS_JS_DIR_.'jquery/jquery.fancybox-1.3.4.js');
if ((int)(Configuration::get('PS_BLOCK_CART_AJAX')) OR Configuration::get('PS_ORDER_PROCESS_TYPE') == 1)
{
Tools::addJS(_THEME_JS_DIR_.'cart-summary.js');
Tools::addJS(_PS_JS_DIR_.'jquery/jquery-typewatch.pack.js');
}
$_POST['controller'] = 'order';
$_POST['step'] = 2;
include(dirname(__FILE__).'/../../header.php');
}
public function displayHeaderMobile(){
}
public function hookExtraCarrier($address, $cart){
global $smarty;
if (!MondialRelay::isAccountSet())
return '';
$preSelectedRelay = $this->mr_module->getRelayPointSelected($cart->id);
$account_shop = MondialRelay::getAccountDetail();
$id_carrier = (int) $cart->id_carrier;
$carriersList = $this->_getCarriers((int) $cart->id_carrier);
$country = new Country($address->id_country);
$id_zone = Address::getZoneById((int)($address->id));
/* Check if the defined carrier are ok */
foreach ($carriersList as $k => $row)
{
/* For now works only with single shipping (>= 1.5 compatibility) */
if (method_exists($cart, 'carrierIsSelected'))
{
if ($cart->carrierIsSelected($row['id_carrier'], $params['address']->id))
$id_carrier = $row['id_carrier'];
}
/* Temporary carrier for some test */
$carrier = new Carrier((int)($row['id_carrier']));
if ((($carrier->getShippingMethod() == Carrier::SHIPPING_METHOD_WEIGHT) && $carrier->getMaxDeliveryPriceByWeight($id_zone) === false) || (($carrier->getShippingMethod() == Carrier::SHIPPING_METHOD_PRICE) && $carrier->getMaxDeliveryPriceByPrice($id_zone) === false))
unset($carriersList[$k]);
else if ($row['range_behavior'])
{
/* Get id zone */
$id_zone = Address::getZoneById((int) $address->id);
if (($carrier->getShippingMethod() == Carrier::SHIPPING_METHOD_WEIGHT && (!Carrier::checkDeliveryPriceByWeight($row['id_carrier'], $cart->getTotalWeight(), $id_zone))) ||
($carrier->getShippingMethod() == Carrier::SHIPPING_METHOD_PRICE &&
(!Carrier::checkDeliveryPriceByPrice($row['id_carrier'], $cart->getOrderTotal(true, MondialRelay::BOTH_WITHOUT_SHIPPING), $id_zone, $cart->id_currency) ||
!Carrier::checkDeliveryPriceByPrice($row['id_carrier'], $cart->getOrderTotal(true, MondialRelay::BOTH_WITHOUT_SHIPPING), $id_zone, $cart->id_currency))))
unset($carriersList[$k]);
}
}
$carrier = null;
if ($id_carrier && ($method = MondialRelay::getMethodByIdCarrier($id_carrier)))
{
$carrier = new Carrier((int)$id_carrier);
/* Add dynamically a new field */
$carrier->id_mr_method = $method['id_mr_method'];
$carrier->mr_dlv_mode = $method['dlv_mode'];
}
if (Configuration::get('PS_SSL_ENABLED') || (!empty($_SERVER['HTTPS']) && Tools::strtolower($_SERVER['HTTPS']) != 'off'))
$ssl = 'true';
else $ssl = 'false';
$smarty->assign(array(
'id_cart' => (int) $cart->id,
'address' => $address,
'weight' => (float) $cart->getTotalWeight(),
'id_address_delivery' => (int) $address->id,
'account_shop' => $account_shop,
'country' => $country,
'ssl' => $ssl,
'MR_Data'=> MRTools::jsonEncode(array(
'carrier_list' => $carriersList,
'carrier' => $carrier,
'PS_VERSION' => _PS_VERSION_,
'pre_selected_relay' => isset($preSelectedRelay['MR_selected_num']) ? $preSelectedRelay['MR_selected_num'] : -1,
))
));
if (Configuration::get('MONDIAL_RELAY_MODE') == 'widget' && !_PS_MOBILE_)
return $this->mr_module->fetchTemplate('/views/templates/front/', 'selection_process_widget');
else
return $this->mr_module->fetchTemplate('/views/templates/front/', 'selection_process');
}
public function process(){
global $cookie, $smarty;
$errors = array();
if (!$cookie->isLogged())
Tools::redirect('authentication.php?back=/modules/mondialrelay/selection.php?id_order=' . Tools::getValue('id_order') . '&key=' . Tools::getValue('key'));
if (!Tools::getValue('id_order') || !Tools::getValue('key')){
$smarty->assign(array('error' => 'Paramètres invalides.'));
return $this->display();
}
$order = new Order((int) Tools::getValue('id_order'));
if (!Validate::isLoadedObject($order) || $order->secure_key != Tools::getValue('key')){
$smarty->assign(array('error' => 'Paramètres invalides.'));
return $this->display();
}
$mr_carriers = array();
foreach (Db::getInstance()->executeS('
SELECT c.id_carrier
FROM `'._DB_PREFIX_.'mr_method` m
LEFT JOIN `'._DB_PREFIX_.'carrier` c
ON c.`id_carrier` = m.`id_carrier`
') as $row) {
$mr_carriers[] = (int) $row['id_carrier'];
}
if (!in_array($order->id_carrier, $mr_carriers)){
return $this->displayError('Un mode de livraison autre que Mondial Relay a été choisi pour cette commande.');
}
$this->id_lang = (int) $order->id_lang;
$customer = new Customer((int) $order->id_customer);
if ((int) $customer->id != (int) $cookie->id_customer){
$smarty->assign(array('error' => 'Merci de vous connecter avec le compte ayant passé cette commande afin de choisir votre point relais.'));
return $this->display();
}
$cart = new Cart((int) $order->id_cart);
$this->mr_module = new MondialRelay();
$address = new Address((int) $order->id_address_delivery);
$extraCarrier = $this->hookExtraCarrier($address, $cart);
$groups = $customer->getGroups();
$id_zone = Address::getZoneById((int)($address->id));
$carriers = array();
$_carriers = $this->getCarriersForOrder($order->id_carrier, $cart);
foreach ($_carriers as $carrier) {
if(stristr($carrier['name'], 'Mondial Relay') !== FALSE){
$carriers[] = $carrier;
}
}
if (Tools::isSubmit('submit_mr_selection')){
// update mr_selected
// $this->addMrSelected();
if (!Db::getInstance()->getValue('SELECT `MR_Selected_Num` FROM `' . _DB_PREFIX_ . 'mr_selected` WHERE `id_cart` = ' . (int) $cart->id)){
$smarty->assign(array(
'error' => 'Vous devez sélectionner un point relais dans la liste.',
'display_conf' => false,
));
}
else{
$smarty->assign(array(
'display_conf' => true,
));
return $this->display();
}
}
$smarty->assign(array(
'mobile' => _PS_MOBILE_,
'display_form' => true,
'carriers' => $carriers,
'id_order' => (int) Tools::getValue('id_order'),
'key' => Tools::getValue('key'),
'weight' => (float) $cart->getTotalWeight(),
'id_address_delivery' => (int) $address->id,
'id_cart' => (int) $order->id_cart,
'extraCarrier' => $extraCarrier
));
return $this->display();
}
public function displayError($error){
global $smarty;
$smarty->assign(array('error' => $error));
return $this->display();
}
public function display(){
echo Module::display(dirname(__FILE__).'/mondialrelay.php', 'selection.tpl');
}
public function displayFooter(){
include(dirname(__FILE__).'/../../footer.php');
}
public function addMrSelected(){
return true;
$db = Db::getInstance();
// insutance
$sql = 'SELECT insurance FROM '._DB_PREFIX_.'mr_method WHERE id_mr_method = '.(int)$this->_params['id_mr_method'];
$insurance = $db->getValue($sql);
$query = 'SELECT `id_mr_selected` FROM `'._DB_PREFIX_.'mr_selected` WHERE `id_cart` = '.(int)$this->_params['id_cart'].' ';
// Not exist and needed for database
unset($this->_params['relayPointInfo']['permaLinkDetail']);
// Update if Exist else add a new entry
if ($db->getRow($query))
{
$query = 'UPDATE `'._DB_PREFIX_.'mr_selected`
SET `id_method` = '.(int)$this->_params['id_mr_method'].',
`MR_insurance` = '.(int)$insurance.',';
if (is_array($this->_params['relayPointInfo']))
foreach ($this->_params['relayPointInfo'] as $nameKey => $value)
$query .= '`MR_Selected_'.MRTools::bqSQL($nameKey).'` = "'.pSQL($value).'", ';
else // Clean the existing relay point data
$query .= '
MR_Selected_Num = NULL,
MR_Selected_LgAdr1 = NULL,
MR_Selected_LgAdr2 = NULL,
MR_Selected_LgAdr3 = NULL,
MR_Selected_LgAdr4 = NULL,
MR_Selected_CP = NULL,
MR_Selected_Pays = NULL,
MR_Selected_Ville = NULL, ';
$query = rtrim($query, ', ').' WHERE `id_cart` = '.(int)$this->_params['id_cart'];
}
else
{
$query = 'INSERT INTO `'._DB_PREFIX_.'mr_selected`
(`id_customer`, `id_method`, `id_cart`, MR_insurance, ';
if (is_array($this->_params['relayPointInfo']))
foreach ($this->_params['relayPointInfo'] as $nameKey => $value)
$query .= '`MR_Selected_'.MRTools::bqSQL($nameKey).'`, ';
$query = rtrim($query, ', ').') VALUES (
'.(int)$this->_params['id_customer'].',
'.(int)$this->_params['id_mr_method'].',
'.(int)$this->_params['id_cart'].',
'.(int)$insurance.', ';
if (is_array($this->_params['relayPointInfo']))
foreach ($this->_params['relayPointInfo'] as $nameKey => $value)
$query .= '"'.pSQL($value).'", ';
$query = rtrim($query, ', ').')';
}
$db->execute($query);
}
public function _getCarriers($id_carrier){
/* Query don't use the external_module_name to keep the */
/* 1.3 compatibility */
$query = '
SELECT c.id_carrier, c.range_behavior, m.id_mr_method,
m.dlv_mode, cl.delay
FROM `'._DB_PREFIX_.'mr_method` m
LEFT JOIN `'._DB_PREFIX_.'carrier` c
ON c.`id_carrier` = m.`id_carrier`
LEFT JOIN `'._DB_PREFIX_.'carrier_lang` cl
ON c.`id_carrier` = cl.`id_carrier`
LEFT JOIN `'._DB_PREFIX_.'mr_method_shop` ms
ON m.`id_mr_method` = ms.`id_mr_method`
WHERE c.`id_carrier` = ' . (int) $id_carrier . '
AND ms.`id_shop` = 1
AND cl.id_lang = '.$this->id_lang.'
-- AND c.`active` = 1
-- AND m.`is_deleted` = 0';
$carriers = Db::getInstance()->executeS($query);
if (!is_array($carriers))
$carriers = array();
return $carriers;
}
public function getCarriersForOrder($id_carrier, $cart)
{
$sql = '
SELECT c.*, cl.delay
FROM `'._DB_PREFIX_.'carrier` c
LEFT JOIN `'._DB_PREFIX_.'carrier_lang` cl ON (c.`id_carrier` = cl.`id_carrier` AND cl.`id_lang` = '.(int)($id_lang).')
WHERE c.`id_carrier` = ' . $id_carrier;
$result = Db::getInstance()->ExecuteS($sql);
if (is_array($result) AND count($result))
{
foreach ($result as $key => $carrier)
if ($carrier['name'] == '0')
$result[$key]['name'] = Configuration::get('PS_SHOP_NAME');
}
else
$result = array();
$resultsArray = array();
foreach ($result AS $k => $row)
{
$carrier = new Carrier((int)$row['id_carrier']);
$shippingMethod = $carrier->getShippingMethod();
$row['name'] = (strval($row['name']) != '0' ? $row['name'] : Configuration::get('PS_SHOP_NAME'));
$row['price'] = ($shippingMethod == Carrier::SHIPPING_METHOD_FREE ? 0 : $cart->getOrderShippingCost((int)$row['id_carrier']));
$row['price_tax_exc'] = ($shippingMethod == Carrier::SHIPPING_METHOD_FREE ? 0 : $cart->getOrderShippingCost((int)$row['id_carrier'], false));
$row['img'] = file_exists(_PS_SHIP_IMG_DIR_.(int)($row['id_carrier']).'.jpg') ? _THEME_SHIP_DIR_.(int)($row['id_carrier']).'.jpg' : '';
// If price is false, then the carrier is unavailable (carrier module)
if ($row['price'] === false)
{
unset($result[$k]);
continue;
}
$resultsArray[] = $row;
}
return $resultsArray;
}
}