Skip to content

Commit

Permalink
Merge pull request #224 from norgeindian/filename-patterns
Browse files Browse the repository at this point in the history
extended filename patterns, fixes #215
  • Loading branch information
sprankhub committed Mar 11, 2015
2 parents f741a1e + e6b6b17 commit c6fcebc
Show file tree
Hide file tree
Showing 6 changed files with 360 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/app/code/community/FireGento/Pdf/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ 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_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 @@ -191,6 +194,32 @@ public function getExportPattern($type)
return true;
}

/**
* Return export pattern for multiple documents config value
*
* @param string $type the type of this document like invoice, shipment or creditmemo
*
* @return string
*/
public function getExportPatternForMultipleDocuments($type)
{
switch ($type) {
case 'invoice':
return Mage::getStoreConfig(
self::XML_PATH_SALES_PDF_INVOICE_FILENAME_EXPORT_PATTERN_FOR_MULTIPLE_DOCUMENTS
);
case 'shipment':
return Mage::getStoreConfig(
self::XML_PATH_SALES_PDF_SHIPMENT_FILENAME_EXPORT_PATTERN_FOR_MULTIPLE_DOCUMENTS
);
case 'creditmemo':
return Mage::getStoreConfig(
self::XML_PATH_SALES_PDF_CREDITMEMO_FILENAME_EXPORT_PATTERN_FOR_MULTIPLE_DOCUMENTS
);
}
return true;
}

/**
* Gets the variables which can be used as a placeholder in the filename.
*
Expand Down Expand Up @@ -263,6 +292,27 @@ public function getExportFilename($type, $model)
return strtr($path, $vars);
}

/**
* 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
*
* @return string the filename of the exported file
*/
public function getExportFilenameForMultipleDocuments($type)
{
$type = (!$type) ? 'invoice' : $type;
$pattern = $this->getExportPatternForMultipleDocuments($type);
if (!$pattern) {
$date = Mage::getSingleton('core/date');
$pattern = $type . $date->date('Y-m-d_H-i-s');
}
if (substr($pattern, -4) != '.pdf') {
$pattern = $pattern . '.pdf';
}
return strftime($pattern);
}

/**
* Returns the path where the fonts reside.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

require('Mage/Adminhtml/controllers/Sales/CreditmemoController.php');

class FireGento_Pdf_Adminhtml_Sales_CreditmemoController
extends Mage_Adminhtml_Sales_CreditmemoController
{
public function pdfcreditmemosAction()
{
$creditmemosIds = $this->getRequest()->getPost('creditmemo_ids');
if (sizeof($creditmemosIds) > 1) {
$invoices = Mage::getResourceModel('sales/order_creditmemo_collection')
->addAttributeToSelect('*')
->addAttributeToFilter('entity_id', array('in' => $creditmemosIds))
->load();
if (!isset($pdf)) {
$pdf = Mage::getModel('sales/order_pdf_creditmemo')->getPdf($invoices);
} else {
$pages = Mage::getModel('sales/order_pdf_creditmemo')->getPdf($invoices);
$pdf->pages = array_merge($pdf->pages, $pages->pages);
}
return $this->_prepareDownloadResponse(
Mage::helper('firegento_pdf')
->getExportFilenameForMultipleDocuments('creditmemo'),
$pdf->render(), 'application/pdf'
);
} else if (sizeof($creditmemosIds) == 1) {
$creditmemoId = $creditmemosIds[0];
if ($invoice = Mage::getModel('sales/order_creditmemo')
->load($creditmemoId)
) {
$pdf = Mage::getModel('sales/order_pdf_creditmemo')
->getPdf(array($invoice));
return $this->_prepareDownloadResponse(
Mage::helper('firegento_pdf')
->getExportFilename('creditmemo', $invoice),
$pdf->render(), 'application/pdf'
);
}
}

$this->_redirect('*/*/');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

require('Mage/Adminhtml/controllers/Sales/InvoiceController.php');

class FireGento_Pdf_Adminhtml_Sales_InvoiceController
extends Mage_Adminhtml_Sales_InvoiceController
{
public function pdfinvoicesAction()
{
$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->_prepareDownloadResponse(
Mage::helper('firegento_pdf')
->getExportFilenameForMultipleDocuments('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('*/*/');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php

require('Mage/Adminhtml/controllers/Sales/OrderController.php');

class FireGento_Pdf_Adminhtml_Sales_OrderController
extends Mage_Adminhtml_Sales_OrderController
{
public function pdfinvoicesAction()
{
$orderIds = $this->getRequest()->getPost('order_ids');
$flag = false;
if (!empty($orderIds)) {
$invoice = null;
foreach ($orderIds as $orderId) {
$invoices = Mage::getResourceModel('sales/order_invoice_collection')
->setOrderFilter($orderId)
->load();
if ($invoices->getSize() == 1) {
$invoice = $invoices->getFirstItem();
}
if ($invoices->getSize() > 0) {
$flag = true;
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);
// there is more than one invoice
$invoice = null;
}
}
}
if ($flag) {
if ($invoice != null) {
return $this->_prepareDownloadResponse(
Mage::helper('firegento_pdf')
->getExportFilename('invoice', $invoice),
$pdf->render(), 'application/pdf'
);
}
return $this->_prepareDownloadResponse(
Mage::helper('firegento_pdf')
->getExportFilenameForMultipleDocuments('invoice'),
$pdf->render(), 'application/pdf'
);
} else {
$this->_getSession()->addError(
$this->__('There are no printable documents related to selected orders.')
);
$this->_redirect('*/*/');
}
}
$this->_redirect('*/*/');
}

public function pdfshipmentsAction()
{
$orderIds = $this->getRequest()->getPost('order_ids');
$flag = false;
if (!empty($orderIds)) {
$shipment = null;
foreach ($orderIds as $orderId) {
$shipments = Mage::getResourceModel('sales/order_shipment_collection')
->setOrderFilter($orderId)
->load();
if ($shipments->getSize() == 1) {
$shipment = $shipments->getFirstItem();
}
if ($shipments->getSize() > 0) {
$flag = true;
if (!isset($pdf)) {
$pdf = Mage::getModel('sales/order_pdf_shipment')->getPdf($shipments);
} else {
$pages = Mage::getModel('sales/order_pdf_shipment')->getPdf($shipments);
$pdf->pages = array_merge($pdf->pages, $pages->pages);
// there is more than one invoice
$shipment = null;
}
}
}
if ($flag) {
if ($shipment != null) {
return $this->_prepareDownloadResponse(
Mage::helper('firegento_pdf')
->getExportFilename('shipment', $shipment),
$pdf->render(), 'application/pdf'
);
}
return $this->_prepareDownloadResponse(
Mage::helper('firegento_pdf')
->getExportFilenameForMultipleDocuments('shipment'),
$pdf->render(), 'application/pdf'
);
} else {
$this->_getSession()->addError(
$this->__('There are no printable documents related to selected orders.')
);
$this->_redirect('*/*/');
}
}
$this->_redirect('*/*/');
}

public function pdfcreditmemosAction()
{
$orderIds = $this->getRequest()->getPost('order_ids');
$flag = false;
if (!empty($orderIds)) {
$creditmemo = null;
foreach ($orderIds as $orderId) {
$creditmemos = Mage::getResourceModel('sales/order_creditmemo_collection')
->setOrderFilter($orderId)
->load();
if ($creditmemos->getSize() == 1) {
$creditmemo = $creditmemos->getFirstItem();
}
if ($creditmemos->getSize() > 0) {
$flag = true;
if (!isset($pdf)) {
$pdf = Mage::getModel('sales/order_pdf_creditmemo')->getPdf($creditmemos);
} else {
$pages = Mage::getModel('sales/order_pdf_creditmemo')->getPdf($creditmemos);
$pdf->pages = array_merge($pdf->pages, $pages->pages);
// there is more than one invoice
$creditmemo = null;
}
}
}
if ($flag) {
if ($creditmemo != null) {
return $this->_prepareDownloadResponse(
Mage::helper('firegento_pdf')
->getExportFilename('creditmemo', $creditmemo),
$pdf->render(), 'application/pdf'
);
}
return $this->_prepareDownloadResponse(
Mage::helper('firegento_pdf')
->getExportFilenameForMultipleDocuments('creditmemo'),
$pdf->render(), 'application/pdf'
);
} else {
$this->_getSession()->addError(
$this->__('There are no printable documents related to selected orders.')
);
$this->_redirect('*/*/');
}
}
$this->_redirect('*/*/');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

require('Mage/Adminhtml/controllers/Sales/ShipmentController.php');

class FireGento_Pdf_Adminhtml_Sales_ShipmentController
extends Mage_Adminhtml_Sales_ShipmentController
{
public function pdfshipmentsAction()
{
$shipmentIds = $this->getRequest()->getPost('shipment_ids');
if (sizeof($shipmentIds) > 1) {
$shipments = Mage::getResourceModel('sales/order_shipment_collection')
->addAttributeToSelect('*')
->addAttributeToFilter('entity_id', array('in' => $shipmentIds))
->load();
if (!isset($pdf)) {
$pdf = Mage::getModel('sales/order_pdf_shipment')->getPdf($shipments);
} else {
$pages = Mage::getModel('sales/order_pdf_shipment')->getPdf($shipments);
$pdf->pages = array_merge($pdf->pages, $pages->pages);
}
return $this->_prepareDownloadResponse(
Mage::helper('firegento_pdf')
->getExportFilenameForMultipleDocuments('shipment'),
$pdf->render(), 'application/pdf'
);
} else if (sizeof($shipmentIds) == 1) {
$shipmentId = $shipmentIds[0];
if ($shipment = Mage::getModel('sales/order_shipment')
->load($shipmentId)
) {
$pdf = Mage::getModel('sales/order_pdf_invoice')
->getPdf(array($shipment));
return $this->_prepareDownloadResponse(
Mage::helper('firegento_pdf')
->getExportFilename('shipment', $shipment),
$pdf->render(), 'application/pdf'
);
}
}

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

0 comments on commit c6fcebc

Please sign in to comment.