* @copyright 2014-2023 Presta-Mod.pl * @license Licecnja na jedną domenę * Presta-Mod.pl Rafał Zontek */ include_once(dirname(__FILE__) . '/../../classes/PaczkomatyList.php'); class PmInpostPaczkomatyListController extends ModuleAdminController { private $lang_class = 'pminpostpaczkomatylist'; public $id_lang; public $languages; public $cat_cache; public $module_pm; public $statuses_array = array(); public $version_ps; public function __construct() { $this->cat_cache = array(); $this->id_lang = Context::getContext()->language->id; $this->bootstrap = true; $this->table = 'orders'; $this->className = 'paczkomatyList'; $this->lang = false; $this->languages = Language::getLanguages(false); $this->explicitSelect = true; parent::__construct(); $this->module_pm = Module::getInstanceByName('pminpostpaczkomaty'); $this->addRowAction('view'); $allegro = Module::getModuleIdByName('x13allegro'); if (Tools::version_compare(_PS_VERSION_, '1.7.0.0', '<=')) { $this->version_ps = '1.6'; } else { $this->version_ps = '1.7'; } if ($allegro) { $sql = ' SELECT * FROM `'._DB_PREFIX_.'xallegro_order` ORDER BY id_order DESC LIMIT 200'; $result = Db::getInstance()->executeS($sql); foreach ($result as $result_item) { $order = new Order($result_item['id_order']); $id_cart = $order->id_cart; $paczkomatyList = PaczkomatyList::getByIdCart($id_cart); $this->addDeliveryAllegro($paczkomatyList, $order->id); } } $this->bulk_actions = array( 'printpdfa4' => array( 'text' => $this->l('Print all labels', $this->lang_class).' '.$this->l('A4', $this->lang_class) ), 'printpdfa6' => array( 'text' => $this->l('Print all labels', $this->lang_class).' '.$this->l('A6P', $this->lang_class) ), 'zlecenie' => array( 'text' => $this->l('Generate transport order', $this->lang_class) ), 'markastoprinted' => array( 'text' => $this->l('Mark as prepared', $this->lang_class) ), 'markasprinted' => array( 'text' => $this->l('Mark as printed', $this->lang_class) ), ); if (!Configuration::get('PMINPOSTPACZKOMATY_SHIPX')) { unset($this->bulk_actions['zlecenie']); } $filters_where = array(); if (Tools::isSubmit('without_transport_order')) { $filters_where[] = '((zlecenie = "" AND nr_listu <> "") OR (zlecenie IS null AND nr_listu <> "")) '; } if (Tools::isSubmit('without_print_label')) { $filters_where[] = 'l.package_status = ""'; } if (Tools::isSubmit('to_print')) { $status_label = 'Prepared'; $filters_where[] = 'l.package_status = "'.$this->module_pm->translateText($status_label).'"'; } if (sizeof($filters_where)) { $this->_where = 'AND ('.implode(' OR ', $filters_where).')'; } $statuses = OrderState::getOrderStates((int)$this->context->language->id); foreach ($statuses as $status) { $this->statuses_array[$status['id_order_state']] = $status['name']; } $this->_select = ' a.*, l.`id` AS `id_orders`, l.*, a.`date_add` as `order_date`, concat(da.firstname," ",da.lastname) `customer`, osl.`name` AS `osname`, os.`color`, machine AS status,a.id_carrier '; $this->_join = ' JOIN `'._DB_PREFIX_.'pminpostpaczkomatylist` l ON l.id_cart = a.id_cart'; $this->_join .= ' JOIN `'._DB_PREFIX_.'address` da ON da.id_address = a.id_address_delivery LEFT JOIN `'._DB_PREFIX_.'order_state` os ON (os.`id_order_state` = a.`current_state`) LEFT JOIN `'._DB_PREFIX_.'order_state_lang` osl ON (os.`id_order_state` = osl.`id_order_state` AND osl.`id_lang` = '.$this->context->language->id.') '; $this->_orderBy = 'id_order'; $this->_orderWay = 'DESC'; $this->fields_list = array(); $this->fields_list['id'] = array( 'title' => $this->l('ID', $this->lang_class) .'/'. $this->l('Id ord.', $this->lang_class), 'align' => 'center', 'remove_onclick' => true, 'callback' => 'id_order_callback' ); $this->fields_list['customer'] = array( 'title' => $this->l('Customer', $this->lang_class), 'align' => 'center', 'type' => 'text', 'remove_onclick' => true, 'havingFilter' => true ); $this->fields_list['date_add'] = array( 'title' => $this->l('Date', $this->lang_class), 'type' => 'datetime', 'remove_onclick' => true, 'filter_key' => 'a!date_add', ); $this->fields_list['osname'] = array( 'title' => $this->l('Status', $this->lang_class), 'type' => 'select', 'color' => 'color', 'list' => $this->statuses_array, 'filter_key' => 'os!id_order_state', 'filter_type' => 'int', 'remove_onclick' => true, 'order_key' => 'osname' ); $this->fields_list['nr_listu'] = array( 'title' => $this->l('Tracking number', $this->lang_class), 'type' => 'text', 'remove_onclick' => true, 'filter_key' => 'l!nr_listu', 'callback' => 'shipping_number_callback' ); $this->fields_list['zlecenie'] = array( 'title' => $this->l('Transport order', $this->lang_class), 'align' => 'center', 'filter' => false, 'search' => false, 'orderby' => false, ); $this->fields_list['pack_status'] = array( 'title' => $this->l('Pack status', $this->lang_class), 'type' => 'text', 'remove_onclick' => true, 'callback' => 'packstatus_callback' ); $this->fields_list['id_carrier'] = array( 'title' => $this->l('COD', $this->lang_class), 'align' => 'text-right', 'orderby' => false, 'havingFilter' => false, 'filter' => false, 'search' => false, 'remove_onclick' => true, 'callback' => 'cod_callback' ); $this->fields_list['machine'] = array( 'title' => $this->l('Machine', $this->lang_class), 'align' => 'center', 'type' => 'text', 'remove_onclick' => true, ); $this->fields_list['status'] = array( 'title' => $this->l('Buttons', $this->lang_class), 'align' => 'text-right', 'orderby' => false, 'callback' => 'delivery_list_callback', 'havingFilter' => false, 'filter' => false, 'search' => false, 'remove_onclick' => true, ); $status_label = 'Prepared'; $states = array(); $states[$this->module_pm->translateText($status_label)] = $this->module_pm->translateText($status_label); $status_label = 'Printed'; $states[$this->module_pm->translateText($status_label)] = $this->module_pm->translateText($status_label); $this->fields_list['package_status'] = array( 'title' => $this->l('Package status', $this->lang_class), 'align' => 'center', 'filter_key' => 'l!package_status', 'search' => true, 'orderby' => true, 'type' => 'select', 'list' => $states, ); $this->fields_list['status_date'] = array( 'title' => $this->l('Label status date', $this->lang_class), 'type' => 'date', 'align' => 'center', 'filter' => true, 'search' => true, 'orderby' => true, ); } public function checkAccess() { return true; } public function initContent() { parent::initContent(); $this->assignParams(); $this->content = $this->context->smarty->fetch( dirname(__FILE__).'/../../views/templates/hook/order_list.tpl' ); } public function initPageHeaderToolbar() { if (empty($this->display)) { $this->page_header_toolbar_btn['orders'] = array( 'href' => $this->context->link->getAdminLink('PmInpostPaczkomatyZlecenia'), 'desc' => $this->l('Transport orders', $this->lang_class), 'icon' => 'process-icon-anchor', ); $this->page_header_toolbar_btn['configure'] = array( 'href' => $this->context->link->getAdminLink('AdminModules').'&configure=pminpostpaczkomaty', 'desc' => $this->l('Configure module', $this->lang_class), 'icon' => 'process-icon-modules-list' ); } parent::initPageHeaderToolbar(); } public function postProcess() { parent::postProcess(); if (Tools::isSubmit('submitBulkmarkastoprintedorders')) { $ids = Tools::getValue('ordersBox'); if (is_array($ids) && sizeof($ids)) { foreach ($ids as $id) { $paczkomatyList = new PaczkomatyList($id); $status_label = 'Prepared'; $paczkomatyList->package_status = $this->module_pm->translateText($status_label); $paczkomatyList->status_date = Date('Y-m-d H:i:s'); $paczkomatyList->save(); } } } elseif (Tools::isSubmit('submitBulkmarkasprintedorders')) { $ids = Tools::getValue('ordersBox'); if (is_array($ids) && sizeof($ids)) { foreach ($ids as $id) { $paczkomatyList = new PaczkomatyList($id); $status_label = 'Printed'; $paczkomatyList->package_status = $this->module_pm->translateText($status_label); $paczkomatyList->status_date = Date('Y-m-d H:i:s'); $paczkomatyList->save(); } } } elseif (Tools::isSubmit('submitBulkprintpdfa6orders')) { $format = 'A6P'; $this->printPdfMerge($format); } elseif (Tools::isSubmit('submitBulkprintpdfa4orders')) { $format = 'A4'; $this->printPdfMerge($format); } elseif (Tools::isSubmit('submitBulkzlecenieorders') || Tools::isSubmit('zlecenie_row')) { $this->zlecenie(); } } public function renderView() { $label = new PaczkomatyList(Tools::getValue('id_orders')); $order = new Order((Order::getOrderByCartId($label->id_cart))); $this->context->smarty->assign( array( 'label' => $label, 'post_info' => unserialize(stripcslashes($label->post_info)), 'id_order' => $order->id ) ); $this->content = $this->context->smarty->fetch(dirname(__FILE__).'/../../views/templates/admin/labelview.tpl'); } public function addDeliveryAllegro(&$paczkomatyList, $id_order) { if ($paczkomatyList->machine) { return; } $sql = ' SELECT * FROM `'._DB_PREFIX_.'xallegro_order` WHERE id_order = '.(int)$id_order; $row = Db::getInstance()->getRow($sql); if (!$row) { return ; } $checkout = @json_decode($row['checkout_form_content']); if ($checkout && isset($checkout->delivery, $checkout->delivery->method) && isset($checkout->delivery->pickupPoint, $checkout->delivery->pickupPoint->id) ) { $method = $checkout->delivery->method->name; $pickupPoint = $checkout->delivery->pickupPoint->id; if (strpos($method, 'Paczkomaty') !== false) { $paczkomatyList->machine = (string)$pickupPoint; $paczkomatyList->post_info = $paczkomatyList->save(); } } } public function assignParams() { $params = array(); if (Tools::isSubmit('without_transport_order')) { $params['without_transport_order'] = '1'; } if (Tools::isSubmit('without_print_label')) { $params['without_print_label'] = '1'; } if (Tools::isSubmit('to_print')) { $params['to_print'] = 1; } $wht_transport_ord = $this->context->link->getAdminLink('PmInpostPaczkomatyList'); $wht_print_label = $this->context->link->getAdminLink('PmInpostPaczkomatyList'); $to_print = $this->context->link->getAdminLink('PmInpostPaczkomatyList'); $copy_params = $params; if (isset($copy_params['without_transport_order'])) { unset($copy_params['without_transport_order']); } else { $copy_params['without_transport_order'] = 1; } $wht_transport_ord .= '&'.http_build_query($copy_params); $copy_params = $params; if (isset($copy_params['without_print_label'])) { unset($copy_params['without_print_label']); } else { $copy_params['without_print_label'] = 1; } $wht_print_label .= '&'.http_build_query($copy_params); $copy_params = $params; if (isset($copy_params['to_print'])) { unset($copy_params['to_print']); } else { $copy_params['to_print'] = 1; } $to_print .= '&'.http_build_query($copy_params); $this->context->smarty->assign(array( 'without_transport_order' => $wht_transport_ord, 'without_print_label' => $wht_print_label, 'to_print' => $to_print )); } public function cod_callback($ceil, $row) { $post_info = stripslashes($row['post_info']); // Tools::stripslashes($string) { return $string; } PS 1782 $cod_serialize = false; if ($post_info && $post_info != '') { $post_info = unserialize($post_info); if (is_array($post_info) && isset($post_info['pminpostorder_pobranie']) && $post_info['pminpostorder_pobranie']) { $cod_serialize = true; } } else { $id_carrier = 0; $id_cart = $row['id_cart']; $sql = 'SELECT id_carrier FROM `'._DB_PREFIX_.'orders` WHERE id_cart = '.(int)$id_cart; $id_carrier = (int)Db::getInstance()->getValue($sql); if ($id_carrier) { $cod_serialize = $this->module_pm->isSelectedCarrierCod($id_carrier); } } $this->context->smarty->assign( array( 'cod_serialize' => $cod_serialize, 'ceil' => $ceil, 'row' => $row, 'cod_id' => Configuration::get('PMINPOSTPACZKOMATY_DELIVERY_BW'), ) ); return $this->context->smarty->fetch(dirname(__FILE__).'/../../views/templates/admin/cod_callback.tpl'); } public function delivery_list_callback($ceil, $row) { $this->context->smarty->assign( array( 'ceil' => $ceil, 'row' => $row, ) ); return $this->context->smarty->fetch( dirname(__FILE__).'/../../views/templates/admin/buttons_callback.tpl' ); } public function id_order_callback($ceil, $row) { $this->context->smarty->assign( array( 'ceil' => $ceil, 'row' => $row, 'version_ps' => $this->version_ps, ) ); if ($this->version_ps == '1.7') { return $this->context->smarty->fetch(dirname(__FILE__).'/../../views/templates/admin/idorder_callback_17.tpl'); } else { return $this->context->smarty->fetch(dirname(__FILE__).'/../../views/templates/admin/idorder_callback.tpl'); } } public function packstatus_callback($ceil, $row) { $this->context->smarty->assign( array( 'ceil' => $ceil, 'row' => $row, ) ); return $this->context->smarty->fetch( dirname(__FILE__).'/../../views/templates/admin/packstatus_callback.tpl' ); } public function parseErrors(&$errors) { if (is_array($errors)) { foreach ($errors as &$item) { $item = $this->module_pm->translate($item); } } else { $errors = $item = $this->module_pm->translate($errors); } return $errors; } public function payforpack($id_order) { $order = new Order($id_order); $id_cart = (int)$order->id_cart; $paczkomatyList = PaczkomatyList::getByIdCart($id_cart); if ($paczkomatyList->status == 'Opłacona') { return true; } $shippingNumber = $paczkomatyList->nr_listu; $order = new Order($id_order); $id_order_carrier = (int)$order->getIdOrderCarrier(); $order_carrier = new OrderCarrier($id_order_carrier); $order->shipping_number = $shippingNumber; $order->update(); $order_carrier->tracking_number = pSQL($shippingNumber); if ($order_carrier->update()) { $customer = new Customer((int)$order->id_customer); $carrier = new Carrier((int)$order->id_carrier, $order->id_lang); $templateVars = array( '{followup}' => str_replace('@', $order->shipping_number, $carrier->url), '{firstname}' => $customer->firstname, '{lastname}' => $customer->lastname, '{id_order}' => $order->id, '{shipping_number}' => $order->shipping_number, '{order_name}' => $order->getUniqReference() ); if ($this->version_ps == '1.7') { try { $order_carrier->sendInTransitEmail($order); } catch (Exception $exp) { $exp->getMessage(); } } elseif (@Mail::Send( (int)$order->id_lang, 'in_transit', Mail::l('Package in transit', (int)$order->id_lang), $templateVars, $customer->email, $customer->firstname.' '.$customer->lastname, null, null, null, null, _PS_MAIL_DIR_, true, (int)$order->id_shop )) { Hook::exec( 'actionAdminOrdersTrackingNumberUpdate', array( 'order' => $order, 'customer' => $customer, 'carrier' => $carrier), null, false, true, false, $order->id_shop ); } } if (Configuration::get('PMINPOSTPACZKOMATY_OS')) { $status = (int)Configuration::get('PMINPOSTPACZKOMATY_STATUS'); $order = new Order(Order::getOrderByCartId($id_cart)); if ($status != $order->getCurrentState()) { $order->setCurrentState($status); } } $sql = ' UPDATE `'._DB_PREFIX_.'order_carrier` SET nr_listu = "'.$shippingNumber.'" WHERE id_order ='.(int)$order->id; Db::getInstance()->execute($sql); $paczkomatyList->status = 'Opłacona'; $paczkomatyList->save(); return true; } public function printPdfMerge($format) { if (Tools::isSubmit('ordersBox') && sizeof(Tools::getValue('ordersBox'))) { error_reporting(0); if (Configuration::get('PMINPOSTPACZKOMATY_SHIPX')) { $api = InpostApiShipx::getInstance(); } else { $api = InpostApi::getInstance(); } $api->setLabelType($format); $sql = ' SELECT id,nr_listu,id_cart,id FROM `'._DB_PREFIX_.'pminpostpaczkomatylist` WHERE id IN ( '.implode(',', Tools::getValue('ordersBox')).' ) AND nr_listu <> ""'; $result = Db::getInstance()->executeS($sql); $caches = array(); $orders = array(); foreach ($result as $list) { $added = false; $sql = ' SELECT nr_listu FROM `'._DB_PREFIX_.'pminpostpaczkomatylist` WHERE id = '.(int)$list['id'].' AND id_cart = '.(int)$list['id_cart']; $shippingNumber = Db::getInstance()->getValue($sql); $cache = dirname(__FILE__).'/../../cache/'.$shippingNumber.'-'. $id_order = Order::getOrderByCartId((int)$list['id_cart']); if (file_exists($cache)) { $caches[] = $cache; $orders[] = $id_order; $added = true; } else { $file = $api->getSticker($shippingNumber, 'Pdf'); if ($file) { $added = true; file_put_contents($cache, $file); $caches[] = $cache; $orders[] = $id_order; } } if ($added) { $paczkomatyList = new PaczkomatyList($list['id']); $status_label = 'Prepared'; if ($paczkomatyList->package_status == $this->module_pm->translateText($status_label)) { $status_label = 'Printed'; $paczkomatyList->package_status = $this->module_pm->translateText($status_label); $paczkomatyList->status_date = Date('Y-m-d H:i:s'); $paczkomatyList->save(); } } } include dirname(__FILE__).'/../../lib/pdfmerger/PDFMerger.php'; $pdf = new PDFMerger; if (sizeof($caches)) { foreach ($caches as $item) { $pdf->addPDF($item, 'all'); } sort($orders); if (Configuration::get('PMINPOSTPACZKOMATY_DF')) { $pdf->merge('download', $this->l('Paczkomaty', $this->lang_class).Date('_Y-m-d', $this->lang_class).'_'.$format.'.pdf', $this->lang_class); } else { $pdf->merge(); } } else { $this->errors[] = $this->l('No labels', $this->lang_class); } } else { $this->errors[] = $this->l('No labels', $this->lang_class); } } public function shipping_number_callback($ceil, $row) { $this->context->smarty->assign( array( 'ceil' => $ceil, 'row' => $row, ) ); return $this->context->smarty->fetch( dirname(__FILE__).'/../../views/templates/admin/shippingnumber_callback.tpl' ); } public function zlecenie() { $api = InpostApiShipx::getInstance(); if (Tools::isSubmit('ordersBoxa')) { $sql = ' SELECT id_pack FROM `'._DB_PREFIX_.'pminpostpaczkomatylist` WHERE id IN ('.(int)Tools::getValue('ordersBox').') AND nr_listu <> ""'; } else { $sql = ' SELECT id_pack FROM `'._DB_PREFIX_.'pminpostpaczkomatylist` WHERE id IN ('. implode( ',', Tools::getValue('ordersBox') ).') AND nr_listu <> ""'; } $result = Db::getInstance()->executeS($sql); $ids = array(); if ($result) { foreach ($result as $item) { $ids[] = $item['id_pack']; } } if (sizeof($ids)) { $result = $api->zlecenie($ids); if ($result) { $sql = ' UPDATE `'._DB_PREFIX_.'pminpostpaczkomatylist` SET zlecenie = "'.Date('Y-m-d H:i:s').'" WHERE id_pack IN('.implode(',', $ids).')'; Db::getInstance()->execute($sql); $this->context->smarty->assign( array( 'alertpaczkomaty' => $this->l('The pickup order has been generated', $this->lang_class), 'alerttype' => 'success' ) ); } else { $errors = $api->getErrors(); $this->context->smarty->assign( array( 'alertpaczkomaty' => implode('
', $this->parseErrors($errors)), 'alerttype' => 'warning' ) ); } } else { $this->context->smarty->assign( array( 'alertpaczkomaty' => $this->l('No package to pickup order', $this->lang_class), 'alerttype' => 'warning' ) ); } } // protected function l($string, $class = null, $addslashes = false, $htmlentities = true) // { // return Translate::getModuleTranslation($this->module_pm, $string, 'pminpostpaczkomatylist', ); // } }