public function renderList()
 {
     $this->toolbar_title = $this->l('Order Management');
     $statuses_array = array();
     $statuses = ErpOrderState::getOrderStates((int) $this->context->language->id);
     foreach ($statuses as $status) {
         $statuses_array[$status['id_order_state']] = $status['name'];
     }
     require_once _PS_MODULE_DIR_ . 'erpillicopresta/models/ErpFeature.php';
     $this->context->smarty->assign(array('token_mr' => ModuleCore::isEnabled('mondialrelay') ? MondialRelay::getToken('back') : 'false', 'token_expeditor' => ModuleCore::isEnabled('expeditor') ? Tools::getAdminToken('AdminExpeditor' . (int) Tab::getIdFromClassName('AdminExpeditor') . (int) $this->context->employee->id) : 'false', 'id_employee' => (int) $this->context->employee->id, 'order_statuses' => $statuses_array, 'controller_status' => $this->controller_status, 'erp_feature' => ErpFeature::getFeaturesWithToken($this->context->language->iso_code), 'template_path' => $this->template_path, 'expeditor_status' => Configuration::get('EXPEDITOR_STATE_EXP'), '_module_dir_' => _MODULE_DIR_));
     $this->tpl_list_vars['has_bulk_actions'] = 'true';
     // handle may contain error messages
     $handle = Tools::getValue('handle');
     switch (trim($handle)) {
         case '':
             break;
         case 'false':
             $this->confirmations[] = $this->l('All orders have been updated') . '<br/>';
             break;
         default:
             if (!empty($handle)) {
                 // $handle = str_replace('u00e9', 'é', $handle);
                 // $handle = str_replace('u00ea', 'ê', $handle);
                 $handle = Tools::replaceAccentedChars($handle);
                 // We take note about orders with error: no valid carrier (split on order number #)
                 $orderWithoutShipping = strstr($handle, '#') != false ? true : false;
                 $errors = explode('<br/>', str_replace('#', '<br/>', $handle));
                 foreach ($errors as $key => $error) {
                     if (!empty($error)) {
                         if (!$orderWithoutShipping) {
                             $message = $error;
                         } else {
                             $message = $error;
                         }
                         $this->errors[] = Tools::displayError($message);
                     }
                 }
             }
             break;
     }
     if (Tools::getValue('linkPDF') != '' && Tools::getValue('newState') != '') {
         // if state need invoice generation
         if (ErpOrderState::invoiceAvailable(Tools::getValue('newState'))) {
             $pdf_link = new Link();
             $pdf_link = $pdf_link->getAdminLink("AdminAdvancedOrder", true) . '&submitAction=generateInvoicesPDF3&id_orders=' . Tools::getValue('linkPDF');
             $this->confirmations[] = '&nbsp;<a target="_blank" href="' . $pdf_link . '" alt="invoices">' . $this->l('Download all invoices') . '<br/></a>';
         }
         // if state need delivery slip generation
         if (ErpOrderState::deliverySlipAvailable(Tools::getValue('newState'))) {
             $pdf_link = new Link();
             $pdf_link = $pdf_link->getAdminLink("AdminAdvancedOrder", true) . '&submitAction=generateDeliverySlipsPDF2&id_orders=' . Tools::getValue('linkPDF');
             $this->confirmations[] = '&nbsp;<a target="_blank" href="' . $pdf_link . '" alt="delivery">' . $this->l('Download all delivery slip') . '<br/></a>';
         }
     }
     if (Tools::getValue('linkPDFPrint') != '') {
         if ($this->controller_status == STATUS1 && count(explode(',', Tools::getValue('linkPDFPrint'))) > ERP_ORDERFR) {
             $this->informations[] = sprintf($this->l('You are using the free version of 1-Click ERP which limits the possible number of documents to print to %d orders'), ERP_ORDERFR);
         } else {
             $invoices = '';
             $delivery = '';
             foreach (explode(',', Tools::getValue('linkPDFPrint')) as $id_order) {
                 if (ErpOrderState::invoiceAvailable(ErpOrder::getIdStateByIdOrder($id_order))) {
                     $invoices .= $id_order . ',';
                 }
                 if (ErpOrderState::deliverySlipAvailable(ErpOrder::getIdStateByIdOrder($id_order))) {
                     $delivery .= $id_order . ',';
                 }
             }
             if ($invoices != '') {
                 $pdf_link = new Link();
                 $pdf_link = $pdf_link->getAdminLink("AdminAdvancedOrder", true) . '&submitAction=generateInvoicesPDF3&id_orders=' . Tools::substr($invoices, 0, -1);
                 $this->confirmations[] = '&nbsp;<a target="_blank" href="' . $pdf_link . '" alt="invoices">' . $this->l('Download all invoices') . '</br></a>';
             }
             if ($delivery != '') {
                 $pdf_link = new Link();
                 $pdf_link = $pdf_link->getAdminLink("AdminAdvancedOrder", true) . '&submitAction=generateDeliverySlipsPDF2&id_orders=' . Tools::substr($delivery, 0, -1);
                 $this->confirmations[] = '&nbsp;<a target="_blank" href="' . $pdf_link . '" alt="delivery">' . $this->l('Download all delivery slip') . '</br></a>';
             }
             if ($invoices == '' && $delivery == '') {
                 $this->errors[] = $this->l('The selected orders have no invoice or delivery !') . '<br/>';
             }
         }
     }
     if (Tools::getValue('etiquettesMR') != '') {
         // Downlad all pdf and zip then delete and display link to zip file
         $etiquettesMR = explode(' ', Tools::getValue('etiquettesMR'));
         unset($etiquettesMR[count($etiquettesMR) - 1]);
         $zipPath = '../modules/erpillicopresta/export/mondialrelay.zip';
         $zip = new ZipArchive();
         if ($zip->open($zipPath, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE) !== true) {
             throw new Exception($this->l('Impossible to create the zip archive containing the shipping labels to Mondial Relay carrier !') . '<br/>');
         }
         foreach ($etiquettesMR as $key => $i) {
             $zip->addFromString('mondialrelay_' . $key . '.pdf', Tools::file_get_contents($i));
         }
         $zip->close();
         //Display link to dl zip file
         $this->confirmations[] = '&nbsp;<a target="_blank" href="' . $zipPath . '" alt="zip_file">' . $this->l('Download zip archive which contents all labels for Mondial Relay shipment') . '<br/></a>';
         if (Tools::getValue('deliveryNumbersMR') != '') {
             // Get all tracking numbers
             $numbers = explode(" ", Tools::getValue('deliveryNumbersMR'));
             unset($numbers[count($numbers) - 1]);
             foreach ($numbers as $number) {
                 $tabNumber = explode("-", $number);
                 $order_carrier = new OrderCarrier(ErpOrder::getIdCarrierbyIdOrder((int) $tabNumber[1]));
                 $order = new ErpOrder((int) $tabNumber[1]);
                 // Update carrier
                 $order->shipping_number = $tabNumber[0];
                 $order->update();
                 // Update order_carrier
                 $order_carrier->tracking_number = pSQL($tabNumber[0]);
                 $order_carrier->update();
             }
         }
     }
     if (Tools::getValue('expeditorCSV') != '') {
         // CSV file creation
         $csvPath = '../modules/erpillicopresta/export/expeditor_inet.csv';
         $fileCSV = fopen($csvPath, 'w');
         // Fill in file
         fwrite($fileCSV, str_replace(',', '', Tools::getValue('expeditorCSV')));
         //Close
         fclose($fileCSV);
         // link creation
         $this->confirmations[] = '&nbsp;<a target="_blank" href="' . $csvPath . '" alt="csv_file">' . $this->l('Download export file (CSV) for ExpeditorInet') . '</br></a>';
     }
     if (Tools::getValue('idOthers') != '') {
         //BEGIN Initialisations for TNT
         if (Module::isEnabled('tntcarrier')) {
             $TNTCheck = false;
             require_once _PS_MODULE_DIR_ . '/tntcarrier/classes/PackageTnt.php';
             if (class_exists('ZipArchive', false) && ($tnt_zip = new ZipArchive())) {
                 // Protection du ZIP
                 $dateday = new DateTime();
                 $uniqid_file = uniqid('file_');
                 $token = md5($dateday->getTimestamp() . $uniqid_file);
                 // Put all tnt pdf into a zip
                 $tnt_zip_path = 'erpillicopresta/export/tnt_' . date('Y-m-d_His') . '_' . $uniqid_file . $token . '.zip';
                 if ($tnt_zip->open(_PS_MODULE_DIR_ . $tnt_zip_path, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE) !== true) {
                     $this->errors[] = Tools::displayError($this->l('Failed to create a ZIP archive containing the shipping labels to TNT carrier !') . '<br/>');
                 } else {
                     // one or several id orders
                     $id_others_order_array = strpos(Tools::getValue('idOthers'), ',') !== false ? explode(',', Tools::getValue('idOthers')) : (int) Tools::getValue('idOthers');
                     // Browse all orders not in ExpeditorInet nor MondialRelay
                     foreach ((array) $id_others_order_array as $i => $id_order) {
                         // BEGIN Commande TNT
                         $id_order = (int) $id_order;
                         if (ErpOrder::isTntOrder($id_order)) {
                             // status change
                             $currOrder = new ErpOrder($id_order);
                             $currOrder->setCurrentState(4, $this->context->employee->id);
                             // Start to check that weight order is valid if not tnt crash !
                             //echo($data['poid'] * 1000);die;
                             // Get tracking number : dedicated class created for this action
                             // Execution of the hook generating the tracking number at an order opening ... So ctrl c / ctrl v to execute here
                             /*$erp_tntCarrier = new ErpTntCarrier();
                               $generate = $erp_tntCarrier->generateShipping($id_order);*/
                             $generateShipping = Hook::exec('adminOrder', array('id_order' => $id_order));
                             $tnt = new PackageTnt($id_order);
                             $tntNumber = $tnt->getShippingNumber();
                             if (count($tntNumber) == 0) {
                                 $this->errors[] = Tools::displayError($this->l('Failed to get shipping number from TNT services : you have to fit the weight of the order.'));
                                 continue;
                             }
                             $tntNumber = $tntNumber[0]['shipping_number'];
                             // Update order
                             $order_carrier = new OrderCarrier(ErpOrder::getIdCarrierbyIdOrder((int) $id_order));
                             $order = new ErpOrder((int) $id_order);
                             $order->shipping_number = $tntNumber;
                             $order->update();
                             $order_carrier->tracking_number = pSQL($tntNumber);
                             $order_carrier->update();
                             // Add pdf to zip
                             $tnt_zip->addFile(_PS_MODULE_DIR_ . '/tntcarrier/pdf/' . $tntNumber . '.pdf', $tntNumber . '.pdf');
                             $TNTCheck = true;
                         }
                         // END Order TNT
                         // SPLICE  idOther
                         if (is_array($id_others_order_array)) {
                             unset($id_others_order_array[$i]);
                         } else {
                             unset($id_others_order_array);
                         }
                     }
                     //Display dl zip link
                     $tnt_zip->close();
                     if ($TNTCheck) {
                         $this->confirmations[] = '&nbsp;<a target="_blank" href="' . _MODULE_DIR_ . $tnt_zip_path . '" alt="zip_file">' . $this->l('Download zip archive which contents all labels for TNT shipment') . '<br/></a>';
                     }
                 }
             } else {
                 $this->errors[] = Tools::displayError($this->l('Class ZipArchive does not exist !') . '<br/>');
             }
             //END Initialisations for TNT
         }
         // Display for order not  processed  : idothers
         if (isset($id_others_order_array)) {
             if (count($id_others_order_array) == 1) {
                 //var_dump($id_others_order_array);die();
                 if (is_array($id_others_order_array)) {
                     $id_others_order_array = $id_others_order_array[1];
                 }
                 $this->errors[] = Tools::displayError($this->l('The following order has not been processed : order #') . $id_others_order_array . '. ' . $this->l('Please make sure that the carrier is either TNT, ExpeditorInet, or MondialRelay and that the order fits the carrier requirements.'));
             } elseif (count($id_others_order_array) > 1) {
                 $this->errors[] = Tools::displayError($this->l('The following orders have not been processed : orders #') . implode(", ", $id_others_order_array) . '. ' . $this->l('Please make sure that the carrier is either TNT, ExpeditorInet, or MondialRelay and that the orders fit the carrier requirements.'));
             }
         }
     }
     return parent::renderList();
 }
Exemplo n.º 2
0
 public function hookorderDetailDisplayed($params)
 {
     if (!$this->active) {
         return;
     }
     global $smarty;
     $tab = $params['order']->getFields();
     $shipping_number = $tab['shipping_number'];
     $id_carrier = $tab['id_carrier'];
     $erreur = null;
     $follow = array();
     $carrierName = Db::getInstance()->getRow('SELECT external_module_name FROM `' . _DB_PREFIX_ . 'carrier` WHERE `id_carrier` = "' . (int) $id_carrier . '"');
     if ($carrierName != null && $carrierName['external_module_name'] == $this->_moduleName && $shipping_number != '') {
         $pack = new PackageTnt($params['order']->id);
         $numbers = $pack->getShippingNumber();
         $smarty->assign('numbers', $numbers);
         return $this->display(__FILE__, 'tpl/waitingFollow.tpl');
     }
 }