* @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_PasswordManager.php'; require_once _PS_MODULE_DIR_.'tntofficiel/libraries/helper/TNTOfficiel_ShipmentHelper.php'; require_once _PS_MODULE_DIR_.'tntofficiel/libraries/pdf/manifest/TNTOfficiel_ManifestPDF.php'; require_once _PS_MODULE_DIR_.'tntofficiel/libraries/pdf/manifest/TNTOfficiel_ManifestPDFGenerator.php'; require_once _PS_MODULE_DIR_.'tntofficiel/libraries/pdf/manifest/HTMLTemplateTNTOfficielManifest.php'; require_once _PS_MODULE_DIR_.'tntofficiel/libraries/pdf/manifest/TNTOfficiel_ManifestPDFCreator.php'; require_once _PS_MODULE_DIR_.'tntofficiel/libraries/exceptions/TNTOfficiel_MaxPackageWeightException.php'; // Controller class AdminTNTOfficielController extends ModuleAdminController { /** * Constructor. */ public function __construct() { TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__)); // _PS_MODULE_DIR_.$this->module->name.'/views/templates/admin/' // default : 'content.tpl'; $this->template = 'AdminTNTOfficiel.tpl'; parent::__construct(); } /** * Add css files. */ public function setMedia() { TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__)); $this->addCSS(_MODULE_DIR_.$this->module->name.'/views/css/AdminTNTOfficiel.css', 'all'); $this->addCSS(_MODULE_DIR_.$this->module->name.'/views/css/style.css'); parent::setMedia(); } /** * {@inheritdoc} */ public function createTemplate($tpl_name) { TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__)); $this->context->smarty->assign(array( 'username' => Configuration::get('TNT_CARRIER_USERNAME'), 'account' => Configuration::get('TNT_CARRIER_ACCOUNT'), 'password' => TNTOfficiel_PasswordManager::getHash(), 'middlewareUrl' => Configuration::get('TNT_CARRIER_MIDDLEWARE_SHORT_URL'), )); if (file_exists($this->getTemplatePath().$tpl_name) && $this->viewAccess()) { return $this->context->smarty->createTemplate($this->getTemplatePath().$tpl_name, $this->context->smarty); } return parent::createTemplate($tpl_name); } /** * Generate the manifest for an order (download). */ public function processGetManifest() { TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__)); $objManifestPDF = new TNTOfficiel_ManifestPDFCreator(); $intOrderID = (int)Tools::getValue('id_order'); $arrOrderIDList = array($intOrderID); $objManifestPDF->createManifest($arrOrderIDList); // We want to be sure that displaying PDF is the last thing this controller will do. exit; } /** * Downloads an archive containing all the logs files. * /_admin/index.php?controller=AdminTNTOfficiel&action=downloadLogs */ public function processDownloadLogs() { TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__)); $strLogPath = _PS_MODULE_DIR_.TNTOfficiel::MODULE_NAME.'/'.TNTOfficiel::LOG_DIR; $zipName = 'logs.zip'; // Remove Existing. if (file_exists($strLogPath.$zipName)) { unlink($strLogPath.$zipName); } // Create Zip file. TNTOfficiel_Logger::getZip($strLogPath.$zipName); // Download. header('Content-Type: application/zip'); header('Content-disposition: attachment; filename='.$zipName); header('Content-Length: '.filesize($strLogPath.$zipName)); // Get Content. readfile($strLogPath.$zipName); // We want to be sure that displaying PDF is the last thing this controller will do. exit; } /** * Add a parcel. * * @return array */ public function displayAjaxAddParcel() { TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__)); $fltWeight = (float)Tools::getValue('weight'); $intOderID = (int)Tools::getValue('orderId'); $arrResult = array(); try { $arrResult['parcel'] = TNTOfficiel_ParcelsHelper::getInstance()->addParcel($intOderID, $fltWeight, false); $arrResult['result'] = true; } catch (TNTOfficiel_MaxPackageWeightException $objException) { $arrResult['result'] = false; $arrResult['error'] = $objException->getMessage(); } catch (Exception $objException) { $objFileLogger = new FileLogger(); $objFileLogger->setFilename(_PS_ROOT_DIR_.'/log/'.date('Ymd').'_tnt_exception.log'); $objFileLogger->logError($objException->getMessage()); $arrResult['result'] = false; } echo Tools::jsonEncode($arrResult); return true; } /** * Remove a parcel. * * @return array */ public function displayAjaxRemoveParcel() { TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__)); $intParcelID = (int)Tools::getValue('parcelId'); $arrResult = array(); try { $arrResult['result'] = TNTOfficiel_ParcelsHelper::getInstance()->removeParcel($intParcelID); } catch (Exception $objException) { $objFileLogger = new FileLogger(); $objFileLogger->setFilename(_PS_ROOT_DIR_.'/log/'.date('Ymd').'_tnt_exception.log'); $objFileLogger->logError($objException->getMessage()); $arrResult['result'] = false; } echo Tools::jsonEncode($arrResult); return true; } /** * Update a parcel. * * @return array */ public function displayAjaxUpdateParcel() { TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__)); $intParcelID = (int)Tools::getValue('parcelId'); $fltWeight = (float)Tools::getValue('weight'); $intOderID = (int)Tools::getValue('orderId'); try { $arrResult = TNTOfficiel_ParcelsHelper::getInstance()->updateParcel($intParcelID, $fltWeight, $intOderID); } catch (TNTOfficiel_MaxPackageWeightException $objException) { $arrResult['result'] = false; $arrResult['error'] = $objException->getMessage(); } catch (Exception $objException) { $objFileLogger = new FileLogger(); $objFileLogger->setFilename(_PS_ROOT_DIR_.'/log/'.date('Ymd').'_tnt_exception.log'); $objFileLogger->logError($objException->getMessage()); $arrResult['result'] = false; } echo Tools::jsonEncode($arrResult); return true; } /** * Checks the shipping. */ public function displayAjaxCheckShippingDateValid() { TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__)); $intOrderID = (int)Tools::getValue('orderId'); $strShippingDate = pSQL(Tools::getValue('shippingDate')); $arrPostDate = explode('/', $strShippingDate); $strFormatedShippingDate = $arrPostDate[2].'-'.$arrPostDate[1].'-'.$arrPostDate[0]; $objTNTShipmentHelper = TNTOfficiel_ShipmentHelper::getInstance(); $arrMDWShippingDate = $objTNTShipmentHelper->checkSaveShipmentDate($intOrderID, $strFormatedShippingDate); if (!is_array($arrMDWShippingDate)) { echo Tools::jsonEncode(array('error' => $arrMDWShippingDate)); return false; } if (array_key_exists('error', $arrMDWShippingDate)) { if ($arrMDWShippingDate['error'] == 1) { echo Tools::jsonEncode(array('error' => $arrMDWShippingDate['message'])); return false; } else { $arrMDWShippingDate['shippingDate'] = $strFormatedShippingDate; /* $tntDate['dueDate'] = ""; */ } } $arrTNTOrder = TNTOfficiel_OrderHelper::getInstance()->getOrderData($intOrderID); Db::getInstance()->update( 'tntofficiel_order', array( 'shipping_date' => pSQL($arrMDWShippingDate['shippingDate']), 'due_date' => pSQL($arrMDWShippingDate['dueDate']) ), 'id_tntofficiel_order = '.(int)$arrTNTOrder['id_tntofficiel_order'] ); if (!$arrMDWShippingDate['dueDate']) { $arrResult = array(); } else { $tempDate = explode('-', $arrMDWShippingDate['dueDate']); $arrResult = array('dueDate' => $tempDate[2].'/'.$tempDate[1].'/'.$tempDate[0]); } if (array_key_exists('message', $arrMDWShippingDate)) { $arrResult['mdwMessage'] = $arrMDWShippingDate['message']; } echo Tools::jsonEncode($arrResult); return true; } }