2017-08-30 11:37:48 +02:00

241 lines
9.0 KiB
PHP

<?php
/**
* TNT OFFICIAL MODULE FOR PRESTASHOP.
*
* @author GFI Informatique <www.gfi.fr>
* @copyright 2016-2017 GFI Informatique, 2016-2017 TNT
* @license https://opensource.org/licenses/MIT MIT License
*/
require_once _PS_MODULE_DIR_.'tntofficiel/classes/TNTOfficielCart.php';
require_once _PS_MODULE_DIR_.'tntofficiel/libraries/TNTOfficiel_Debug.php';
require_once _PS_MODULE_DIR_.'tntofficiel/libraries/helper/TNTOfficiel_RelayPointsHelper.php';
require_once _PS_MODULE_DIR_.'tntofficiel/libraries/helper/TNTOfficiel_ShippingHelper.php';
class TNTOfficielShippingMethodModuleFrontController extends ModuleFrontController
{
/**
* TNTOfficielShippingMethodModuleFrontController constructor.
* Controller always used for AJAX response.
*/
public function __construct()
{
TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__));
parent::__construct();
// No header/footer.
$this->ajax = true;
}
/**
* Get the relay points popup via Ajax.
* JD_DROPOFFPOINT (En Relais Colis®) : XETT
*/
public function displayAjaxBoxRelayPoints()
{
TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__));
$objContext = $this->context;
$objCart = $objContext->cart;
$intCartID = (int)$objCart->id;
$objShop = $objContext->shop;
// Load TNT cart info or create a new one for it's ID.
$objTNTCartModel = TNTOfficielCart::loadCartID($intCartID);
if (Validate::isLoadedObject($objTNTCartModel)) {
// If product code is not "En Relais Colis®".
if ($objTNTCartModel->carrier_code !== 'JD_DROPOFFPOINT') {
// Clear carrier code and delivery point.
$objTNTCartModel->carrier_code = '';
$objTNTCartModel->setDeliveryPoint(array());
// Unselect carrier from cart.
$objCart->setDeliveryOption(null);
$objCart->save();
}
$objTNTCartModel->save();
}
$strPostcode = trim(pSQL(Tools::getValue('tnt_postcode')));
$strCity = trim(pSQL(Tools::getValue('tnt_city')));
if (!$strPostcode && !$strCity) {
$objAddress = new Address((int)$objCart->id_address_delivery);
$strPostcode = $objAddress->postcode;
$strCity = $objAddress->city;
}
// Call API to get list of repositories
$objTNTRelayPointsHelper = TNTOfficiel_RelayPointsHelper::getInstance();
$arrRelayPoints = $objTNTRelayPointsHelper->getRelayPoints($strPostcode, $strCity, $objShop->id);
$arrCities = TNTOfficiel_AddressHelper::getInstance()->getCitiesFromMiddleware($strPostcode, $objShop->id);
foreach ($arrRelayPoints as $key => $item) {
$arrRelayPoints[ $key ]['schedule'] = TNTOfficiel_ShippingHelper::getInstance()->getSchedules($item);
}
/*
if (empty($relay) && !empty($cities['cities'])) {
$city = $cities['cities'][0];
$relay = $obRelayPointsHelper->getRelayPoints($postcode, $city, $objShop->id);
}
*/
// Get the relay points
$this->context->smarty->assign(
array(
'method_id' => 'relay_points',
'method_name' => 'relay-points',
'current_postcode' => $strPostcode,
'current_city' => $strCity,
'results' => $arrRelayPoints,
'cities' => $arrCities['cities'],
)
);
$this->context->smarty->display(
_PS_MODULE_DIR_.$this->module->name.'/views/templates/hook/displayCarrierList/deliveryPointsBox.tpl'
);
return true;
}
/**
* Get the repositories popup via Ajax.
* J_DEPOT (Dépôt restant) : PEX
*/
public function displayAjaxBoxDropOffPoints()
{
TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__));
$objContext = $this->context;
$objCart = $objContext->cart;
$intCartID = (int)$objCart->id;
$objShop = $objContext->shop;
// Load TNT cart info or create a new one for it's ID.
$objTNTCartModel = TNTOfficielCart::loadCartID($intCartID);
if (Validate::isLoadedObject($objTNTCartModel)) {
// If product code is not "Dépôt restant".
if ($objTNTCartModel->carrier_code !== 'J_DEPOT') {
// Clear carrier code and delivery point.
$objTNTCartModel->carrier_code = '';
$objTNTCartModel->setDeliveryPoint(array());
// Unselect carrier from cart.
$objCart->setDeliveryOption(null);
$objCart->save();
}
$objTNTCartModel->save();
}
$strPostCode = trim(pSQL(Tools::getValue('tnt_postcode')));
$strCity = trim(pSQL(Tools::getValue('tnt_city')));
if (!$strPostCode && !$strCity) {
$objAddress = new Address((int)$objCart->id_address_delivery);
$strPostCode = $objAddress->postcode;
$strCity = $objAddress->city;
}
// Call API to get list of repositories
$objTNTRelayPointsHelper = TNTOfficiel_RelayPointsHelper::getInstance();
$arrRepositories = $objTNTRelayPointsHelper->getRepositories($strPostCode, $objShop->id);
$arrCities = TNTOfficiel_AddressHelper::getInstance()->getCitiesFromMiddleware($strPostCode, $objShop->id);
foreach ($arrRepositories as $key => $repository) {
$arrRepositories[ $key ]['schedule'] = TNTOfficiel_ShippingHelper::getInstance()->getSchedules($repository);
}
$this->context->smarty->assign(
array(
'method_id' => 'repositories',
'method_name' => 'repositories',
'current_postcode' => $strPostCode,
'current_city' => $strCity,
'results' => $arrRepositories,
'cities' => $arrCities['cities'],
)
);
$this->context->smarty->display(
_PS_MODULE_DIR_.$this->module->name.'/views/templates/hook/displayCarrierList/deliveryPointsBox.tpl'
);
return true;
}
/**
* Save delivery point XETT or PEX info.
*/
public function displayAjaxSaveProductInfo()
{
TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__));
$arrDeliveryPoint = (array)Tools::getValue('product');
// Check code exist.
if (!array_key_exists('xett', $arrDeliveryPoint) && !array_key_exists('pex', $arrDeliveryPoint)) {
return false;
}
$objContext = $this->context;
$objCart = $objContext->cart;
$intCartID = (int)$objCart->id;
// Load TNT cart info or create a new one for it's ID.
$objTNTCartModel = TNTOfficielCart::loadCartID($intCartID);
if (Validate::isLoadedObject($objTNTCartModel)) {
$objTNTCartModel->setDeliveryPoint($arrDeliveryPoint);
}
$this->context->smarty->assign(
array(
'item' => $arrDeliveryPoint,
'method_id' => isset($arrDeliveryPoint['xett']) ? 'relay_point' : 'repository',
'method_name' => isset($arrDeliveryPoint['xett']) ? 'relay-point' : 'repository',
)
);
$this->context->smarty->display(
_PS_MODULE_DIR_.$this->module->name.'/views/templates/hook/displayCarrierList/deliveryPointSet.tpl'
);
return true;
}
/**
* Save current TNT shipping address.
*/
public function displayAjaxSaveProductCode()
{
TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__));
$strProductCode = pSQL(Tools::getValue('productCode'));
if ($strProductCode) {
$objContext = $this->context;
$objCart = $objContext->cart;
$intCartID = (int)$objCart->id;
// Load TNT cart info or create a new one for it's ID.
$objTNTCartModel = TNTOfficielCart::loadCartID($intCartID);
if (Validate::isLoadedObject($objTNTCartModel)) {
// Save TNT product code.
$objTNTCartModel->carrier_code = $strProductCode;
// If product code is not "Dépôt restant" or En "Relais Colis®".
if ($strProductCode !== 'JD_DROPOFFPOINT' && $strProductCode !== 'J_DEPOT') {
// Clear delivery point.
$objTNTCartModel->setDeliveryPoint(array());
}
$objTNTCartModel->save();
}
}
echo Tools::jsonEncode(array(
'success' => true
));
return true;
}
}