public static function printManifest($module_instance)
 {
     $cookie = Context::getContext()->cookie;
     if (isset($cookie->dpdpoland_packages_ids)) {
         if (version_compare(_PS_VERSION_, '1.5', '<')) {
             $package_ids = unserialize(Context::getContext()->cookie->dpdpoland_packages_ids);
         } else {
             $package_ids = Tools::unSerialize(Context::getContext()->cookie->dpdpoland_packages_ids);
         }
         unset($cookie->dpdpoland_packages_ids);
         $cookie->write();
         $separated_packages = DpdPolandPackage::separatePackagesBySession($package_ids);
         $international_packages = $separated_packages['INTERNATIONAL'];
         $domestic_packages = $separated_packages['DOMESTIC'];
         $manifest_ids = array();
         if ($international_packages) {
             $manifest_ids[] = DpdPolandManifest::getManifestIdWsByPackageIdWs($international_packages[0]);
         }
         if ($domestic_packages) {
             $manifest_ids[] = DpdPolandManifest::getManifestIdWsByPackageIdWs($domestic_packages[0]);
         }
         require_once _DPDPOLAND_CONTROLLERS_DIR_ . 'manifestList.controller.php';
         $manifest_controller = new DpdPolandManifestListController();
         return $manifest_controller->printManifest($manifest_ids);
     }
     if ($package_ids = Tools::getValue('PackagesBox')) {
         if (!DpdPolandManifest::validateSenderAddresses($package_ids)) {
             $error_message = $module_instance->l('Manifests can not have different sender addresses', self::FILENAME);
             $error = $module_instance->displayError($error_message);
             return $module_instance->outputHTML($error);
         }
         $separated_packages = DpdPolandPackage::separatePackagesBySession($package_ids);
         $international_packages = $separated_packages['INTERNATIONAL'];
         $domestic_packages = $separated_packages['DOMESTIC'];
         if ($international_packages) {
             $manifest = new DpdPolandManifest();
             if (!$manifest->generateMultiple($international_packages)) {
                 $error = $module_instance->displayError(reset(DpdPolandManifestWS::$errors));
                 return $module_instance->outputHTML($error);
             }
         }
         if ($domestic_packages) {
             $manifest = new DpdPolandManifest();
             if (!$manifest->generateMultiple($domestic_packages)) {
                 $error = $module_instance->displayError(reset(DpdPolandManifestWS::$errors));
                 return $module_instance->outputHTML($error);
             }
         }
         $cookie->dpdpoland_packages_ids = serialize($package_ids);
         $redirect_uri = $module_instance->module_url . '&menu=packages_list';
         die(Tools::redirectAdmin($redirect_uri));
     }
 }
 public function generateMultiple($package_ids, $output_doc_format = 'PDF', $output_doc_page_format = 'LBL_PRINTER', $policy = 'STOP_ON_FIRST_ERROR')
 {
     $session_type = '';
     $package_number = null;
     foreach ($package_ids as $id_package_ws) {
         $package = new DpdPolandPackage((int) $id_package_ws);
         if (!$session_type || $session_type == $package->getSessionType()) {
             $session_type = $package->getSessionType();
             if ($package_number === null) {
                 $package_number = $package->payerNumber;
             } elseif ($package_number !== $package->payerNumber) {
                 $package_number = 'null';
             }
         } else {
             self::$errors[] = $this->l('Manifests of DOMESTIC shipments cannot be mixed with INTERNATIONAL shipments');
             return false;
         }
     }
     $params = array('dpdServicesParamsV1' => array('pickupAddress' => $package->getSenderAddress($package_number), 'policy' => $policy, 'session' => array('sessionType' => $session_type, 'packages' => array())), 'outputDocFormatV1' => $output_doc_format, 'outputDocPageFormatV1' => $output_doc_page_format);
     foreach ($package_ids as $id_package_ws) {
         $params['dpdServicesParamsV1']['session']['packages'][] = array('packageId' => (int) $id_package_ws);
     }
     $result = $this->generateProtocolV1($params);
     if (isset($result['session']) && isset($result['session']['statusInfo']) && $result['session']['statusInfo']['status'] == 'OK') {
         foreach ($package_ids as $id_package_ws) {
             $manifest = new DpdPolandManifest();
             $manifest->id_manifest_ws = (int) $result['documentId'];
             $manifest->id_package_ws = (int) $id_package_ws;
             if (!$manifest->save()) {
                 return false;
             }
         }
         return $result['documentData'];
     } else {
         if (isset($result['session']['statusInfo']['description'])) {
             self::$errors[] = $result['session']['statusInfo']['description'];
         } elseif (isset($result['session']['statusInfo']['status'])) {
             self::$errors[] = $result['session']['statusInfo']['status'];
         }
         return false;
     }
 }
 public function printManifest($id_manifest_ws)
 {
     if (is_array($id_manifest_ws)) {
         if (empty($id_manifest_ws)) {
             return false;
         }
         if (file_exists(_PS_MODULE_DIR_ . 'dpdpoland/manifest_duplicated.pdf') && !unlink(_PS_MODULE_DIR_ . 'dpdpoland/manifest_duplicated.pdf')) {
             $error_message = $this->l('Could not delete old PDF file. Please check module permissions');
             $error = $this->module_instance->displayError($error_message);
             return $this->module_instance->outputHTML($error);
         }
         foreach ($id_manifest_ws as $id) {
             $manifest = new DpdPolandManifest();
             $manifest->id_manifest_ws = $id;
             if ($pdf_file_contents = $manifest->generate()) {
                 if (file_exists(_PS_MODULE_DIR_ . 'dpdpoland/manifest_' . (int) $id . '.pdf') && !unlink(_PS_MODULE_DIR_ . 'dpdpoland/manifest_' . (int) $id . '.pdf')) {
                     $error_message = $this->l('Could not delete old PDF file. Please check module permissions');
                     $error = $this->module_instance->displayError($error_message);
                     return $this->module_instance->outputHTML($error);
                 }
                 $fp = fopen(_PS_MODULE_DIR_ . 'dpdpoland/manifest_' . (int) $id . '.pdf', 'a');
                 if (!$fp) {
                     $error_message = $this->l('Could not create PDF file. Please check module folder permissions');
                     $error = $this->module_instance->displayError($error_message);
                     return $this->module_instance->outputHTML($error);
                 }
                 fwrite($fp, $pdf_file_contents);
                 fclose($fp);
             } else {
                 $error_message = $this->module_instance->displayError(reset(DpdPolandManifestWS::$errors));
                 return $this->module_instance->outputHTML($error_message);
             }
         }
         include_once _PS_MODULE_DIR_ . 'dpdpoland/libraries/PDFMerger/PDFMerger.php';
         $pdf = new PDFMerger();
         foreach ($id_manifest_ws as $id) {
             $manifest_pdf_path = _PS_MODULE_DIR_ . 'dpdpoland/manifest_' . (int) $id . '.pdf';
             $pdf->addPDF($manifest_pdf_path, 'all')->addPDF($manifest_pdf_path, 'all');
         }
         $pdf->merge('file', _PS_MODULE_DIR_ . 'dpdpoland/manifest_duplicated.pdf');
         ob_end_clean();
         header('Content-type: application/pdf');
         header('Content-Disposition: attachment; filename="manifests_' . time() . '.pdf"');
         readfile(_PS_MODULE_DIR_ . 'dpdpoland/manifest_duplicated.pdf');
         $this->deletePDFFiles($id_manifest_ws);
         exit;
     }
     $manifest = new DpdPolandManifest();
     $manifest->id_manifest_ws = $id_manifest_ws;
     if ($pdf_file_contents = $manifest->generate()) {
         if (file_exists(_PS_MODULE_DIR_ . 'dpdpoland/manifest.pdf') && !unlink(_PS_MODULE_DIR_ . 'dpdpoland/manifest.pdf')) {
             $error_message = $this->l('Could not delete old PDF file. Please check module permissions');
             $error = $this->module_instance->displayError($error_message);
             return $this->module_instance->outputHTML($error);
         }
         if (file_exists(_PS_MODULE_DIR_ . 'dpdpoland/manifest_duplicated.pdf') && !unlink(_PS_MODULE_DIR_ . 'dpdpoland/manifest_duplicated.pdf')) {
             $error_message = $this->l('Could not delete old PDF file. Please check module permissions');
             $error = $this->module_instance->displayError($error_message);
             return $this->module_instance->outputHTML($error);
         }
         $fp = fopen(_PS_MODULE_DIR_ . 'dpdpoland/manifest.pdf', 'a');
         if (!$fp) {
             $error_message = $this->l('Could not create PDF file. Please check module folder permissions');
             $error = $this->module_instance->displayError($error_message);
             return $this->module_instance->outputHTML($error);
         }
         fwrite($fp, $pdf_file_contents);
         fclose($fp);
         include_once _PS_MODULE_DIR_ . 'dpdpoland/libraries/PDFMerger/PDFMerger.php';
         $pdf = new PDFMerger();
         $pdf->addPDF(_PS_MODULE_DIR_ . 'dpdpoland/manifest.pdf', 'all');
         $pdf->addPDF(_PS_MODULE_DIR_ . 'dpdpoland/manifest.pdf', 'all');
         $pdf->merge('file', _PS_MODULE_DIR_ . 'dpdpoland/manifest_duplicated.pdf');
         ob_end_clean();
         header('Content-type: application/pdf');
         header('Content-Disposition: attachment; filename="manifests_' . time() . '.pdf"');
         readfile(_PS_MODULE_DIR_ . 'dpdpoland/manifest_duplicated.pdf');
         $this->deletePDFFiles($id_manifest_ws);
         exit;
     } else {
         $this->module_instance->outputHTML($this->module_instance->displayError(reset(DpdPolandManifestWS::$errors)));
     }
 }