Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/182 #244

Closed
wants to merge 13 commits into from
30 changes: 23 additions & 7 deletions src/app/code/community/FireGento/Pdf/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
/**
* Dummy data helper for translation issues.
*
* @category FireGento
* @package FireGento_Pdf
* @author FireGento Team <[email protected]>
* @category FireGento
* @package FireGento_Pdf
* @author FireGento Team <[email protected]>
*/
class FireGento_Pdf_Helper_Data extends Mage_Core_Helper_Abstract
{
Expand All @@ -38,10 +38,16 @@ class FireGento_Pdf_Helper_Data extends Mage_Core_Helper_Abstract
const XML_PATH_SALES_PDF_INVOICE_FILENAME_EXPORT_PATTERN = 'sales_pdf/invoice/filename_export_pattern';
const XML_PATH_SALES_PDF_SHIPMENT_FILENAME_EXPORT_PATTERN = 'sales_pdf/shipment/filename_export_pattern';
const XML_PATH_SALES_PDF_CREDITMEMO_FILENAME_EXPORT_PATTERN = 'sales_pdf/creditmemo/filename_export_pattern';
const XML_PATH_SALES_PDF_INVOICE_FILENAME_EXPORT_PATTERN_FOR_MULTIPLE_DOCUMENTS = 'sales_pdf/invoice/filename_export_pattern_for_multiple_documents';
const XML_PATH_SALES_PDF_SHIPMENT_FILENAME_EXPORT_PATTERN_FOR_MULTIPLE_DOCUMENTS = 'sales_pdf/shipment/filename_export_pattern_for_multiple_documents';
const XML_PATH_SALES_PDF_CREDITMEMO_FILENAME_EXPORT_PATTERN_FOR_MULTIPLE_DOCUMENTS = 'sales_pdf/creditmemo/filename_export_pattern_for_multiple_documents';

const XML_PATH_SALES_PDF_INVOICE_SERVE_AS_ZIP = 'sales_pdf/invoice/serve_as_zip';

const XML_PATH_SALES_PDF_FIREGENTO_PDF_PAGE_SIZE = 'sales_pdf/firegento_pdf/page_size';
const XML_PATH_SALES_PDF_INVOICE_FILENAME_EXPORT_PATTERN_FOR_MULTIPLE_DOCUMENTS
= 'sales_pdf/invoice/filename_export_pattern_for_multiple_documents';
const XML_PATH_SALES_PDF_SHIPMENT_FILENAME_EXPORT_PATTERN_FOR_MULTIPLE_DOCUMENTS
= 'sales_pdf/shipment/filename_export_pattern_for_multiple_documents';
const XML_PATH_SALES_PDF_CREDITMEMO_FILENAME_EXPORT_PATTERN_FOR_MULTIPLE_DOCUMENTS
= 'sales_pdf/creditmemo/filename_export_pattern_for_multiple_documents';

const XML_PATH_REGULAR_FONT = 'sales_pdf/firegento_pdf_fonts/regular_font';
const XML_PATH_BOLD_FONT = 'sales_pdf/firegento_pdf_fonts/bold_font';
Expand Down Expand Up @@ -336,7 +342,7 @@ public function getExportFilename($type, $model)
/**
* The filename of the exported file if multiple documents are printed at once.
*
* @param string $type the type of this document like invoice, shipment or creditmemo
* @param string $type the type of this document like invoice, shipment or creditmemo
*
* @return string the filename of the exported file
*/
Expand Down Expand Up @@ -365,6 +371,16 @@ public function getFontPath()
return Mage::getBaseDir('media') . self::FONT_PATH_IN_MEDIA;
}

/**
* Returns whether the invoice should be served as zip (or (multi-page) pdf)
*
* @return bool
*/
public function isServeInvoiceAsZip()
{
return Mage::getStoreConfigFlag(self::XML_PATH_SALES_PDF_INVOICE_SERVE_AS_ZIP);
}

public function getPageSizeConfigPath()
{
return Mage::getStoreConfig(self::XML_PATH_SALES_PDF_FIREGENTO_PDF_PAGE_SIZE);
Expand Down
130 changes: 130 additions & 0 deletions src/app/code/community/FireGento/Pdf/Model/ZippedInvoice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php
/**
* This file is part of a FireGento e.V. module.
*
* This FireGento e.V. module is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This script is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* PHP version 5
*
* @category FireGento
* @package FireGento_Pdf
* @author FireGento Team <[email protected]>
* @copyright 2014 FireGento Team (http://www.firegento.com)
* @license http://opensource.org/licenses/gpl-3.0 GNU General Public License, version 3 (GPLv3)
*/

/**
* Model to serve invoices as zip
*
* @category FireGento
* @package FireGento_Pdf
* @author FireGento Team <[email protected]>
*/
class FireGento_Pdf_Model_ZippedInvoice
{
/**
* Create a ZIP file with pdfed invoices based on orders
*
* @param string $file file the zip is written to
* @param Mage_Sales_Model_Resource_Order_Collection $orders orders to be pdfed and saved to zip
*
* @throws Zend_Pdf_Exception
*/
public function createFromOrders($file, Mage_Sales_Model_Resource_Order_Collection $orders)
{
$zip = $this->openZip($file);

foreach ($orders as $order) {
$this->addInvoicesFromOrder($zip, $order);
}

// Close and send to users
$this->closeZip($zip);
}

/**
* Create a ZIP file with pdfed invoices based on invoices
*
* @param string $file file to save the zip to
* @param Mage_Sales_Model_Resource_Order_Invoice_Collection $invoices invoices to pdf and save
*
* @throws Zend_Pdf_Exception
*/
public function create($file, Mage_Sales_Model_Resource_Order_Invoice_Collection $invoices)
{
$zip = $this->openZip($file);
foreach ($invoices as $invoice) {
$this->addInvoice($zip, $invoice);
}

// Close and send to users
$zip->close();
}

/**
* create new archive and open it for writing
*
* @param string $file name of the archive file
*
* @return ZipArchive
*/
private function openZip($file)
{
$zip = new ZipArchive();
$zip->open($file, ZipArchive::OVERWRITE);

return $zip;
}

/**
* close archive after writing
*
* @param ZipArchive $zip ZipArchive resource
*/
private function closeZip($zip)
{
$zip->close();
}

/**
* Add all the invoice from an order to the zip file
*
* @param ZipArchive $zip zip archive object to write to
* @param Mage_Sales_Model_Order $order orders to write
*
* @throws Zend_Pdf_Exception
*/
private function addInvoicesFromOrder(ZipArchive $zip, $order)
{
$zip->addFromString(
$order->getIncrementId() . '.pdf',
Mage::getModel('sales/order_pdf_invoice')->getPdf(
$order->getInvoiceCollection()
)->render()
);
}

/**
* Add an invoice the the zip file
*
* @param ZipArchive $zip zip archive object to write to
* @param Mage_Sales_Model_Order_Invoice $invoice invoice to write
*
* @throws Zend_Pdf_Exception
*/
private function addInvoice(ZipArchive $zip, $invoice)
{
$zip->addFromString(
$invoice->getOrder()->getIncrementId() . '.pdf',
Mage::getModel('sales/order_pdf_invoice')->getPdf(
array($invoice)
)->render()
);
}
}
Original file line number Diff line number Diff line change
@@ -1,44 +1,141 @@
<?php

/**
* This file is part of a FireGento e.V. module.
*
* This FireGento e.V. module is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This script is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* PHP version 5
*
* @category FireGento
* @package FireGento_Pdf
* @author FireGento Team <[email protected]>
* @copyright 2014 FireGento Team (http://www.firegento.com)
* @license http://opensource.org/licenses/gpl-3.0 GNU General Public License, version 3 (GPLv3)
*/
require('Mage/Adminhtml/controllers/Sales/InvoiceController.php');

/**
* Adminhtml Order controller to serve invoices
*
* @category FireGento
* @package FireGento_Pdf
* @author FireGento Team <[email protected]>
*/
class FireGento_Pdf_Adminhtml_Sales_InvoiceController
extends Mage_Adminhtml_Sales_InvoiceController
{
/**
* controller action to serve one or multiple invoices
*/
public function pdfinvoicesAction()
{
$invoicesIds = $this->getRequest()->getPost('invoice_ids');
if (count($invoicesIds) == 0) {
$this->_redirect('*/*/');
}

if (Mage::helper('firegento_pdf')->isServeInvoiceAsZip()) {
$this->serveInvoicesAsZip();

return;
}

$this->serveInvoicesAsPdf();

return;
}

/**
* Returns all invoices packed into a zip container
*/
private function serveInvoicesAsZip()
{
$invoiceIds = $this->getRequest()->getParam('invoice_ids');
$invoices = Mage::getResourceModel('sales/order_invoice_collection');
$invoices->addFieldToFilter('entity_id', array('in', $invoiceIds));

$file = tempnam("tmp", "zip");
Mage::getModel('firegento_pdf/zippedInvoice')->create($file, $invoices);

$name = 'invoice' . Mage::getSingleton('core/date')->date('Y-m-d_H-i-s') . '.zip';

$this->getResponse()->setHeader('Content-Type', 'application/zip', true);
$this->getResponse()->setHeader('Content-Length', filesize($file), true);
$this->getResponse()->setHeader('Content-Disposition', 'attachment; filename=' . $name, true);

$this->getResponse()->setBody(file_get_contents($file));

unlink($file);
}

/**
* serve invoices as pdf
*
* @return Mage_Core_Controller_Varien_Action
* @throws Zend_Pdf_Exception
*/
private function serveInvoicesAsPdf()
{
$invoicesIds = $this->getRequest()->getPost('invoice_ids');
if (sizeof($invoicesIds) > 1) {
$invoices = Mage::getResourceModel('sales/order_invoice_collection')
->addAttributeToSelect('*')
->addAttributeToFilter('entity_id', array('in' => $invoicesIds))
->load();
if (!isset($pdf)) {
$pdf = Mage::getModel('sales/order_pdf_invoice')->getPdf($invoices);
} else {
$pages = Mage::getModel('sales/order_pdf_invoice')->getPdf($invoices);
$pdf->pages = array_merge($pdf->pages, $pages->pages);
}
return $this->serveMultipleInvoicesAsPdf($invoicesIds);
}

return $this->serveOneInvoiceAsPdf($invoicesIds);
}

/**
* serve multiple invoices as pdf
*
* @param int[] $invoicesIds invoices to add to the pdf
*
* @return Mage_Core_Controller_Varien_Action
* @throws Zend_Pdf_Exception
*/
private function serveMultipleInvoicesAsPdf($invoicesIds)
{
$invoices = Mage::getResourceModel('sales/order_invoice_collection')
->addAttributeToSelect('*')
->addAttributeToFilter('entity_id', array('in' => $invoicesIds))
->load();

$pdf = Mage::getModel('sales/order_pdf_invoice')->getPdf($invoices);

return $this->_prepareDownloadResponse(
Mage::helper('firegento_pdf')
->getExportFilenameForMultipleDocuments('invoice'),
$pdf->render(), 'application/pdf'
);
}

/**
* serve a single invoice as pdf
*
* @param int[] $invoicesIds invoice to add to the pdf
*
* @return Mage_Core_Controller_Varien_Action
* @throws Zend_Pdf_Exception
*/
private function serveOneInvoiceAsPdf($invoicesIds)
{
$invoiceId = $invoicesIds[0];
if ($invoice = Mage::getModel('sales/order_invoice')
->load($invoiceId)
) {
$pdf = Mage::getModel('sales/order_pdf_invoice')
->getPdf(array($invoice));

return $this->_prepareDownloadResponse(
Mage::helper('firegento_pdf')
->getExportFilenameForMultipleDocuments('invoice'),
->getExportFilename('invoice', $invoice),
$pdf->render(), 'application/pdf'
);
} else if (sizeof($invoicesIds) == 1) {
$invoiceId = $invoicesIds[0];
if ($invoice = Mage::getModel('sales/order_invoice')
->load($invoiceId)
) {
$pdf = Mage::getModel('sales/order_pdf_invoice')
->getPdf(array($invoice));
return $this->_prepareDownloadResponse(
Mage::helper('firegento_pdf')
->getExportFilename('invoice', $invoice),
$pdf->render(), 'application/pdf'
);
}
}

$this->_redirect('*/*/');
}
}
Loading