✏️ 正在编辑: AdminDPDFranceReturnController.php
路径:
/home/bpioifn/www/pigmentse/modules/dpdfrance/controllers/admin/AdminDPDFranceReturnController.php
提示:
您可以编辑任何文件(包括二进制文件),但请注意不当修改可能导致文件损坏。
<?php /** * 2007-2022 PrestaShop * * NOTICE OF LICENSE * * This source file is subject to the Academic Free License (AFL 3.0) * that is bundled with this package in the file LICENSE.txt. * It is also available through the world-wide-web at this URL: * http://opensource.org/licenses/afl-3.0.php * If you did not receive a copy of the license and are unable to * obtain it through the world-wide-web, please send an email * to license@prestashop.com so we can send you a copy immediately. * * DISCLAIMER * * Do not edit or add to this file if you wish to upgrade PrestaShop to newer * versions in the future. If you wish to customize PrestaShop for your * needs please refer to http://www.prestashop.com for more information. * * @author DPD France S.A.S. <support.ecommerce@dpd.fr> * @copyright 2022 DPD France S.A.S. * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) */ if (!defined('_PS_VERSION_')) { exit; } require_once(__DIR__ . '/../../vendor/PDFMerger-master/PDFMerger.php'); use PDFMerger\PDFMerger as DPDPDFMerger; class AdminDPDFranceReturnController extends ModuleAdminController { public $identifier = 'DPDFrance'; public $pdf; private $dirPdf; public function __construct() { $this->name = 'DPDFranceReturn'; $this->bootstrap = true; $this->display = 'view'; $this->meta_title = 'Gestion des retours'; $this->dirPdf = __DIR__ . "/../../pdf"; if (!is_dir($this->dirPdf)) { mkdir($this->dirPdf); } parent::__construct(); if (!$this->module->active) { Tools::redirectAdmin($this->context->link->getAdminLink('AdminHome')); } } /** * @inheritdoc * @return void */ public function initToolBarTitle() { $this->toolbar_title[] = $this->l('Orders'); $this->toolbar_title[] = $this->l('DPD return management'); } /** * @inheritdoc * @param $isNewTheme */ public function setMedia($isNewTheme = false) { parent::setMedia($isNewTheme); $this->addCSS([ _PS_MODULE_DIR_ . '/dpdfrance/views/js/admin/jquery/plugins/fancybox/jquery.fancybox.css', _PS_MODULE_DIR_ . '/dpdfrance/views/css/admin/AdminDPDFrance.css', ]); $this->addJS([ _PS_MODULE_DIR_ . '/dpdfrance/views/js/admin/jquery/plugins/marquee/jquery.marquee.min.js', _PS_MODULE_DIR_ . '/dpdfrance/views/js/admin/admin_france_pdf_return.js' ]); } public function renderView() { $this->fields_form[]['form'] = []; $helper = $this->buildHelper(); $msg = ''; /** * ? Ajax call - Export label */ if (Tools::getIsset('exportLabel')) { file_put_contents(__DIR__ . "/../../session.txt", "start"); if (Tools::getIsset('checkbox')) { $colis = Tools::getValue('checkbox'); $zplPathReturn = []; if (is_string($colis)) { $colis = explode(',', $colis); } if (!empty($colis)) { $this->pdf = new DPDPDFMerger(); foreach ($colis as $colisId) { if (Configuration::get('DPDFRANCE_FORMAT_MOD') === 'pdf') { $hasCreate[] = $this->generatedLabelReturn($this->getDpdOrder($colisId)); } else { $hasCreate[] = $this->generatedLabelReturn($this->getDpdOrder($colisId), $zplPathReturn); $zplPathReturn = array_unique($zplPathReturn); } } if (Configuration::get('DPDFRANCE_FORMAT_MOD') !== 'pdf') { if (count($zplPathReturn) > 0) { if (Configuration::get('DPDFRANCE_FORMAT_CONNECT') === 'ip') { if (($conn = fsockopen(Configuration::get('DPDFRANCE_PRINTER_IP'), Configuration::get('DPDFRANCE_PRINTER_PORT'), $errno, $errstr)) === false) { echo 'Connection Failed' . $errno . $errstr; } foreach ($zplPathReturn as $path) { fputs($conn, Tools::file_get_contents($path), Tools::strlen(Tools::file_get_contents($path))); } fclose($conn); foreach ($zplPathReturn as $path) { unlink($path); } file_put_contents(__DIR__ . "/../../session.txt", "finish"); echo "<script type='text/javascript'>"; echo "window.close();"; echo "</script>"; exit; } } else { file_put_contents(__DIR__ . "/../../session.txt", "finish"); echo "<script type='text/javascript'>"; echo "window.close();"; echo "</script>"; exit; } } file_put_contents(__DIR__ . "/../../session.txt", "finish"); ob_clean(); if (Configuration::get('DPDFRANCE_FORMAT_MOD') === 'pdf') { $this->pdf->merge('browser', 'label.pdf'); foreach (scandir($this->dirPdf) as $file) { $file = $this->dirPdf . "/" . $file; if (is_file($file)) { unlink($file); } } } else { file_put_contents(__DIR__ . "/../../session.txt", "finish"); echo "<script type='text/javascript'>"; echo "window.close();"; echo "</script>"; exit; } } } } // Display section // Error message if shipper info is missing if ((Configuration::get('DPDFRANCE_PARAM') === '0')) { echo '<div class="warnmsg">' . $this->l('Warning! Your DPD Depot code and contract number are missing. You must configure the DPD module in order to use the export and tracking features.') . '</div>'; exit; } // Calls function to get orders $order_info = []; $orderStatusList = []; $statuses = OrderState::getOrderStates((int)Context::getContext()->language->id); foreach ($statuses as $status) { $orderStatusList[$status['id_order_state']] = $status['name']; } $colis = $this->getColis(); if (!empty($colis)) { foreach ($colis as $dpdId) { $order = new Order($dpdId['id_order']); $address = new Address($order->id_address_delivery); $service = DPDTools::getService($order, Context::getContext()->language->id); $code_pays_dest = DPDTools::getIsoCodebyIdCountry($address->id_country); switch ($service) { case 'HDP_PRE': case 'PRE': $type = $code_pays_dest !== 'F' ? 'Predict Export<img src="../modules/dpdfrance/views/img/admin/service_predict.png" title="Predict Export" alt="predict export"/>' : 'Predict<img src="../modules/dpdfrance/views/img/admin/service_predict.png" title="Predict" alt="predict"/>'; $compte_chargeur = Configuration::get('DPDFRANCE_PREDICT_SHIPPER_CODE', null, null, (int)$order->id_shop); $depot_code = Configuration::get('DPDFRANCE_PREDICT_DEPOT_CODE', null, null, (int)$order->id_shop); $address_delivery = '<a class="popup" href="' . DPDTools::PREFIX_GOOGLE_URL . str_replace(' ', '+', $address->address1) . ',' . str_replace(' ', '+', $address->postcode) . '+' . str_replace(' ', '+', $address->city) . '&output=embed" target="_blank">' . ($address->company ? $address->company . '<br/>' : '') . $address->address1 . '<br/>' . $address->postcode . ' ' . $address->city . '</a>'; break; case 'REL': $type = 'Relais<img src="../modules/dpdfrance/views/img/admin/service_relais.png" title="Relais" alt="relais"/>'; $compte_chargeur = Configuration::get('DPDFRANCE_RELAIS_SHIPPER_CODE', null, null, (int)$order->id_shop); $depot_code = Configuration::get('DPDFRANCE_RELAIS_DEPOT_CODE', null, null, (int)$order->id_shop); $relay_id = ''; preg_match('/P\d{5}/i', $address->company, $matches, PREG_OFFSET_CAPTURE); if ($matches) { $relay_id = $matches[0][0]; } $address_delivery = '<a class="popup" href="'. DPDTools::PREFIX_RELAIS_URL . $relay_id . '" target="_blank">' . $address->company . '<br/>' . $address->postcode . ' ' . $address->city . '</a>'; break; case 'HDP_CLA': case 'CLA': $type = $code_pays_dest !== 'F' ? 'Classic Export<img src="../modules/dpdfrance/views/img/admin/service_world.png" title="Classic Export" alt="classic export"/>' : 'Classic<img src="../modules/dpdfrance/views/img/admin/service_dom.png" title="Classic" alt="classic"/>'; $compte_chargeur = Configuration::get('DPDFRANCE_CLASSIC_SHIPPER_CODE', null, null, (int)$order->id_shop); $depot_code = Configuration::get('DPDFRANCE_CLASSIC_DEPOT_CODE', null, null, (int)$order->id_shop); $address_delivery = '<a class="popup" href="' . DPDTools::PREFIX_GOOGLE_URL . str_replace(' ', '+', $address->address1) . ',' . str_replace(' ', '+', $address->postcode) . '+' . str_replace(' ', '+', $address->city) . '&output=embed" target="_blank">' . ($address->company ? $address->company . '<br/>' : '') . $address->address1 . '<br/>' . $address->postcode . ' ' . $address->city . '</a>'; break; } $current_state_id = $order->current_state; switch ($current_state_id) { default: $dernierstatutcolis = ''; break; case Configuration::get('DPDFRANCE_ETAPE_LIVRE', null, null, (int)$order->id_shop): $dernierstatutcolis = '<img src="../modules/dpdfrance/views/img/admin/tracking.png" title="Trace du colis"/>'; break; case Configuration::get('DPDFRANCE_ETAPE_EXPEDIEE', null, null, (int)$order->id_shop): $dernierstatutcolis = '<img src="../modules/dpdfrance/views/img/admin/tracking.png" title="Trace du colis"/>'; } $order_info[] = [ 'id' => $dpdId['id_order_dpd'], 'id_order' => $dpdId['id_order'], 'reference' => $order->reference, 'type' => $type, 'date' => date('d/m/Y H:i:s', strtotime($order->date_add)), 'nom' => $address->lastname . " " . $address->firstname, 'address' => $address_delivery, 'shipper_code' => $compte_chargeur, 'depot_code' => $depot_code, 'dernier_statut_colis' => $dernierstatutcolis, 'poids' => $order->getTotalWeight(), 'labelprint' => (!is_null($dpdId['return_print']) ? $dpdId['return_print'] : false), 'weightunit' => Configuration::get('PS_WEIGHT_UNIT', null, null, (int)$order->id_shop), "arrived" => $dpdId['arrived'] ]; } } else { $order_info['error'] = true; } /** * ? Assign smarty variables */ $this->context->smarty->assign( [ 'msg' => $msg, 'stream' => DPDTools::getDPDRssInfos(), 'token' => $this->token, 'order_info' => $order_info, 'dpdfrance_retour_option' => (int)Configuration::get('DPDFRANCE_RETOUR_OPTION', null, null, (int)Context::getContext()->shop->id), 'dpdfrance_mode_format' => Configuration::get('DPDFRANCE_FORMAT_MOD', null, null, (int)Context::getContext()->shop->id), 'dpdfrance_base_dir' => __PS_BASE_URI__ . 'modules/' . Tools::strtolower($this->identifier), 'dpdfrance_token' => Tools::hash('dpdfrance/ajax'), ] ); return $helper->generateForm($this->fields_form); } protected function buildHelper() { $helper = new HelperForm(); $helper->base_folder = 'helpers/form/'; $helper->base_tpl = 'form_return.tpl'; $helper->module = $this->module; $helper->override_folder = 'dpdfrance/'; $helper->identifier = $this->identifier; $helper->token = Tools::getAdminTokenLite('Admin' . $this->name); $helper->languages = $this->_languages; $helper->currentIndex = $this->context->link->getAdminLink('Admin' . $this->name); $helper->default_form_language = $this->default_form_language; $helper->allow_employee_form_lang = $this->allow_employee_form_lang; $helper->toolbar_scroll = true; $helper->toolbar_btn = $this->initToolbar(); $helper->background_color = 'red'; return $helper; } /** * Create label return * @param $dpdOrder * @param array $zplPathReturn * @return bool * @throws SoapFault */ public function generatedLabelReturn($dpdOrder, &$zplPathReturn = []) { $soapClient = DPDTools::getSoapInitEPrint(); if (!$soapClient) { if (Configuration::get('DPDFRANCE_FORMAT_MOD') === 'pdf') { return false; } } $print_format = Configuration::get('DPDFRANCE_FORMAT_PRINT'); $order = new Order($dpdOrder['id_order']); $service = DPDTools::getService($order, Context::getContext()->language->id); switch ($service) { case 'HDP_PRE': case 'PRE': $depot_code = Configuration::get('DPDFRANCE_PREDICT_DEPOT_CODE', null, null, (int)$order->id_shop); break; case 'REL': $depot_code = Configuration::get('DPDFRANCE_RELAIS_DEPOT_CODE', null, null, (int)$order->id_shop); break; case 'HDP_CLA': case 'CLA': $depot_code = Configuration::get('DPDFRANCE_CLASSIC_DEPOT_CODE', null, null, (int)$order->id_shop); break; } $getRetourLabel = [ "countrycode" => 250, "centernumber" => (int)$depot_code, "parcelnumber" => $dpdOrder['id_retour_order_dpd'], ]; if (Configuration::get('DPDFRANCE_FORMAT_MOD') != 'pdf') { $getRetourLabel["labelType"] = [ 'type' => Tools::strtoupper(Configuration::get('DPDFRANCE_FORMAT_MOD')) ]; } //__Override by new DATA // ? Add receiveradress countryPrefix by country ? Is it always FR ? if (!is_null($dpdOrder['override_return_street']) && !is_null($dpdOrder['override_return_zip']) && !is_null($dpdOrder['override_return_city']) && !is_null($dpdOrder['override_return_phone'])) { $getRetourLabel['receiveraddress'] = [ "name" => Configuration::get('DPDFRANCE_NOM_EXP'), "countryPrefix" => 'FR', "zipCode" => $dpdOrder['override_return_zip'], "city" => $dpdOrder['override_return_city'], "street" => $dpdOrder['override_return_street'], "phoneNumber" => $dpdOrder['override_return_phone'] ]; } if ((string)$print_format === 'a6') { $getRetourLabel["labelType"] = [ 'type' => 'PDF_A6' ]; } else if ((string)$print_format === 'a4') { $getRetourLabel["labelType"] = [ 'type' => 'PDF' ]; } try { $data = $soapClient->GetRetourLabel(["request" => $getRetourLabel]); } catch (\Exception $e) { return false; } Db::getInstance()->update( 'dpdfrance_order', array( 'return_print' => 1, ), 'id_order = '.(int)$dpdOrder['id_order'] ); if (Configuration::get('DPDFRANCE_FORMAT_MOD') === 'pdf') { $labels = $data->GetRetourLabelResult->labels->Label; if (empty($this->pdf)) { $this->pdf = new DPDPDFMerger(); } foreach ($labels as $key => $label) { $pathPdf = $this->dirPdf . "/PDF_" . $dpdOrder['id_retour_order_dpd'] . $key . ".pdf"; file_put_contents($pathPdf, $label->label); $this->pdf->addPDF($pathPdf); } return true; } else { $labels = $data->GetRetourLabelResult->labels->Label; $i = 0; foreach ($labels as $label) { $path = __DIR__ . "/../../zplLabelReturn_" . $dpdOrder['id_retour_order_dpd'] . '_' . $i . '.' . Configuration::get('DPDFRANCE_FORMAT_MOD'); file_put_contents($path, $label->label); $i++; $zplPathReturn[] = $path; } return true; } } /** * Get colis delivered whit return accept * @return array * @throws PrestaShopDatabaseException * @throws PrestaShopException * @throws SoapFault */ public function getColis() { $listColisValid = []; $sql = new DbQuery(); $sql->select('*') ->from('dpdfrance_order', 'O') ->where('id_order_dpd <> 0') ->orderBy('id_order DESC'); $listColis = Db::getInstance()->executeS($sql); $soapClientTrace = DPDTools::getSoapTraceInit(); $soapClient = DPDTools::getSoapInitEPrint(); foreach ($listColis as $colis) { $order = new Order($colis['id_order']); $service = DPDTools::getService($order, Context::getContext()->language->id); switch ($service) { case 'HDP_PRE': case 'PRE': $depot_code = Configuration::get('DPDFRANCE_PREDICT_DEPOT_CODE', null, null, (int)$order->id_shop); break; case 'REL': $depot_code = Configuration::get('DPDFRANCE_RELAIS_DEPOT_CODE', null, null, (int)$order->id_shop); break; case 'HDP_CLA': case 'CLA': $depot_code = Configuration::get('DPDFRANCE_CLASSIC_DEPOT_CODE', null, null, (int)$order->id_shop); break; } $shipmentTrace = [ 'customer_center' => '3', 'customer' => DPDTools::decrypt(Configuration::get('DPDFRANCE_WEBTRACE_LOGIN')), 'password' => DPDTools::decrypt(Configuration::get('DPDFRANCE_WEBTRACE_PASSWORD')), 'shipmentnumber' => $colis['id_shipment_number_dpd'] ]; try { $data = $soapClientTrace->getShipmentTrace($shipmentTrace); } catch (\Exception $e) { //exception on getting the shipment trace : webservice problems / no data found... continue; } $shipmentReturn = [ 'customer_center' => '3', 'customer' => DPDTools::decrypt(Configuration::get('DPDFRANCE_WEBTRACE_LOGIN')), 'password' => DPDTools::decrypt(Configuration::get('DPDFRANCE_WEBTRACE_PASSWORD')), 'shipmentnumber' => $data->getShipmentTraceResult->ShipmentNumber_Retour ]; try { $return = $soapClientTrace->getShipmentTrace($shipmentReturn); } catch (\Exception $exception) { // if no ShipmentNumber_Retour for this order continue; } $retourShipmentData = [ "countrycode" => 250, "centernumber" => (int)$depot_code, 'original_parcelnumber' => (int)$colis['id_order_dpd'] ]; try { $data2 = $soapClient->GetRetourShipmentData(["request" => $retourShipmentData]); } catch (\Exception $exception) { // if no return for this order continue; } if ($data2->GetRetourShipmentDataResult->services->expireOffset > 0) { $result = Db::getInstance()->update( 'dpdfrance_order', array( 'id_retour_order_dpd' => $data2->GetRetourShipmentDataResult->parcelnumber_retour ), 'id_order_dpd = '.(int)$colis['id_order_dpd'] ); $show = true; $colis['arrived'] = false; foreach ($data->getShipmentTraceResult->Traces as $trace) { if (is_array($trace)) { foreach ($trace as $item) { if ($item->StatusNumber == "40" || $item->StatusNumber == "400" || $item->StatusNumber == "271") { $colis['arrived'] = true; break; } } } else { if ($trace->StatusNumber == "40" || $trace->StatusNumber == "400" || $trace->StatusNumber == "271") { $colis['arrived'] = true; break; } } } foreach ($return->getShipmentTraceResult->Traces as $trace) { if (is_array($trace)) { foreach ($trace as $item) { if ($item->StatusNumber == "40" || $item->StatusNumber == "400" || $item->StatusNumber == "271") { $show = false; } } } else { if ($trace->StatusNumber == "40" || $trace->StatusNumber == "400" || $trace->StatusNumber == "271") { $show = false; } } } if ($show) { $listColisValid[] = $colis; } } } return $listColisValid; } /** * @param $colis * @return string list order dpd * @throws PrestaShopDatabaseException */ public function getDpdOrder($colis) { $sql = new DbQuery(); $sql->select('*') ->from('dpdfrance_order') ->where('id_order_dpd = '.(int)$colis); $result = Db::getInstance()->executeS($sql); if (!empty($result[0]['id_order_dpd']) || (isset ($result[0]['id_order_dpd']) && $result[0]['id_order_dpd'] === "0")) { return $result[0]; } return ''; } }
💾 保存文件
← 返回文件管理器