161 lines
6.6 KiB
PHP
Raw Normal View History

2017-09-12 10:48:51 +02:00
<?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/tntofficiel.php';
require_once _PS_MODULE_DIR_.'tntofficiel/libraries/TNTOfficiel_Debug.php';
require_once _PS_MODULE_DIR_.'tntofficiel/libraries/TNTOfficiel_Carrier.php';
require_once _PS_MODULE_DIR_.'tntofficiel/libraries/helper/TNTOfficiel_CarrierHelper.php';
require_once _PS_MODULE_DIR_.'tntofficiel/classes/TNTOfficielCart.php';
class TNTOfficielCarrierModuleFrontController extends ModuleFrontController
{
/**
* TNTOfficielCarrierModuleFrontController 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;
}
/**
* Store in session the tnt carrier name and store its price.
*
* @return string
*/
public function displayAjaxStoreProductPrice()
{
TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__));
$objContext = $this->context;
$objCart = $objContext->cart;
$intCartID = (int)$objCart->id;
$strProductCode = pSQL(Tools::getValue('productCode'));
$objTNTCarrierHelper = TNTOfficiel_CarrierHelper::getInstance();
$arrAvailableTNTCarriers = $objTNTCarrierHelper->getMDWTNTCarrierList($objContext->customer);
$boolCarrierIsValid = false;
$arrTNTCarrier = null;
if (!empty($arrAvailableTNTCarriers) && !empty($arrAvailableTNTCarriers['products'])) {
foreach ($arrAvailableTNTCarriers['products'] as $arrTNTCarrier) {
$arrTNTCarrier['label'] = '';
if (strpos($arrTNTCarrier['type'], 'ENTERPRISE') !== false) {
if (strpos($arrTNTCarrier['code'], 'A') !== false) {
$arrTNTCarrier['label'] = '09:00 Express en Entreprise';
} elseif (strpos($arrTNTCarrier['code'], 'J') !== false) {
$arrTNTCarrier['label'] = 'En entreprise';
} elseif (strpos($arrTNTCarrier['code'], 'M') !== false) {
$arrTNTCarrier['label'] = '12:00 Express en Entreprise';
} elseif (strpos($arrTNTCarrier['code'], 'T') !== false) {
$arrTNTCarrier['label'] = '10:00 Express en Entreprise';
}
} elseif (strpos($arrTNTCarrier['type'], 'INDIVIDUAL') !== false) {
if (strpos($arrTNTCarrier['code'], 'AZ') !== false) {
$arrTNTCarrier['label'] = '09:00 Express à domicile';
} elseif (strpos($arrTNTCarrier['code'], 'JZ') !== false) {
$arrTNTCarrier['label'] = 'À domicile';
} elseif (strpos($arrTNTCarrier['code'], 'MZ') !== false) {
$arrTNTCarrier['label'] = '12:00 Express à domicile';
} elseif (strpos($arrTNTCarrier['code'], 'TZ') !== false) {
$arrTNTCarrier['label'] = '10:00 Express à domicile';
}
} elseif (strpos($arrTNTCarrier['type'], 'DROPOFFPOINT') !== false) {
$arrTNTCarrier['label'] = 'En Relais Colis®';
} elseif (strpos($arrTNTCarrier['type'], 'DEPOT') !== false) {
$arrTNTCarrier['label'] = 'Dépôt restant';
}
// Found current TNT carrier code.
if ($arrTNTCarrier['code'].'_'.$arrTNTCarrier['type'] == $strProductCode) {
// Load TNT cart info or create a new one for it's ID.
$objTNTCartModel = TNTOfficielCart::loadCartID($intCartID);
$boolResult = false;
if (Validate::isLoadedObject($objTNTCartModel)) {
$objTNTCartModel->hydrate(array(
'carrier_code' => $strProductCode,
'carrier_label' => pSQL($arrTNTCarrier['label']),
'delivery_price' => pSQL($arrTNTCarrier['price'])
));
$boolResult = $objTNTCartModel->save();
}
$boolCarrierIsValid = true;
break;
}
}
}
echo Tools::jsonEncode(array(
'result' => $boolCarrierIsValid && $boolResult,
'product' => $arrTNTCarrier,
));
return true;
}
/**
* Check TNT data before payment process.
*
* @return array
*/
public function displayAjaxCheckProductCode()
{
TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__));
$objContext = $this->context;
$objCart = $objContext->cart;
//$intCartID = (int)$objCart->id;
//$strCarrierType = pSQL(Tools::getValue('product'));
$arrCarrierCode = (array)Tools::getValue('deliveryOptionTnt');
$strErrorMsg = null;
$arrResult = array(
'error' => 0,
TNTOfficiel::MODULE_NAME => 1
);
if (!$objCart) {
$strErrorMsg = 'No cart object';
} elseif ($objCart->id_carrier == 0) {
$strErrorMsg = 'No carrier selected';
} elseif (!$objCart->id_address_delivery) {
$strErrorMsg = 'No delivery address selected';
}
// If an error.
if ($strErrorMsg !== null) {
$arrResult = array('error' => 1, 'msg' => $strErrorMsg);
} else {
$boolIsTntCarrier = TNTOfficiel_Carrier::isTNTOfficielCarrierID($objCart->id_carrier);
if (!$boolIsTntCarrier) {
// The preselected carrier is not TNT, don't handle.
$arrResult = array('error' => 0, TNTOfficiel::MODULE_NAME => 0);
} elseif (($boolIsTntCarrier && !Tools::getIsset('product'))
|| !$arrCarrierCode
|| !$objCart->delivery_option
) {
// TNT is preselected and no service selected (individual, enterprise, dropoffpoint or depot) -> Error.
$arrResult = array('error' => 1);
}
}
echo Tools::jsonEncode($arrResult);
return true;
}
}