From 3445cc37328c8df119a2e81e0614e1307e42c2b7 Mon Sep 17 00:00:00 2001 From: SG Date: Mon, 30 Nov 2015 11:11:08 +0100 Subject: [PATCH 1/2] Ported Invoice PDF to Order PDF --- .../community/FireGento/Pdf/Helper/Data.php | 42 ++ .../FireGento/Pdf/Model/Engine/Abstract.php | 18 +- .../Pdf/Model/Engine/Creditmemo/Default.php | 7 +- .../Pdf/Model/Engine/Invoice/Default.php | 4 - .../Pdf/Model/Engine/Order/Default.php | 384 ++++++++++++++++++ .../Pdf/Model/Engine/Shipment/Default.php | 4 - .../FireGento/Pdf/Model/Items/Default.php | 58 ++- .../FireGento/Pdf/Model/Observer.php | 15 + .../community/FireGento/Pdf/Model/Order.php | 75 ++++ .../Adminhtml/Sales/Order/PdfController.php | 56 +++ .../Pdf/controllers/Sales/OrderController.php | 7 +- .../community/FireGento/Pdf/etc/config.xml | 12 + .../community/FireGento/Pdf/etc/system.xml | 66 ++- 13 files changed, 721 insertions(+), 27 deletions(-) create mode 100644 src/app/code/community/FireGento/Pdf/Model/Engine/Order/Default.php create mode 100644 src/app/code/community/FireGento/Pdf/Model/Order.php create mode 100644 src/app/code/community/FireGento/Pdf/controllers/Adminhtml/Sales/Order/PdfController.php diff --git a/src/app/code/community/FireGento/Pdf/Helper/Data.php b/src/app/code/community/FireGento/Pdf/Helper/Data.php index 9b81105..73d8a03 100644 --- a/src/app/code/community/FireGento/Pdf/Helper/Data.php +++ b/src/app/code/community/FireGento/Pdf/Helper/Data.php @@ -29,21 +29,32 @@ class FireGento_Pdf_Helper_Data extends Mage_Core_Helper_Abstract { const XML_PATH_FIREGENTO_PDF_LOGO_POSITION = 'sales_pdf/firegento_pdf/logo_position'; + + const XML_PATH_SALES_PDF_ORDER_SHOW_CUSTOMER_NUMBER = 'sales_pdf/order/show_customer_number'; const XML_PATH_SALES_PDF_INVOICE_SHOW_CUSTOMER_NUMBER = 'sales_pdf/invoice/show_customer_number'; const XML_PATH_SALES_PDF_SHIPMENT_SHOW_CUSTOMER_NUMBER = 'sales_pdf/shipment/show_customer_number'; const XML_PATH_SALES_PDF_CREDITMEMO_SHOW_CUSTOMER_NUMBER = 'sales_pdf/creditmemo/show_customer_number'; + + const XML_PATH_SALES_PDF_ORDER_SHOW_CUSTOMER_VATNUMBER = 'sales_pdf/order/show_customer_vatnumber'; const XML_PATH_SALES_PDF_INVOICE_SHOW_CUSTOMER_VATNUMBER = 'sales_pdf/invoice/show_customer_vatnumber'; const XML_PATH_SALES_PDF_SHIPMENT_SHOW_CUSTOMER_VATNUMBER = 'sales_pdf/shipment/show_customer_vatnumber'; const XML_PATH_SALES_PDF_CREDITMEMO_SHOW_CUSTOMER_VATNUMBER = 'sales_pdf/creditmemo/show_customer_vatnumber'; + + const XML_PATH_SALES_PDF_ORDER_GUEST_ORDER_CUSTOMER_NUMBER = 'sales_pdf/order/guestorder_customer_number'; const XML_PATH_SALES_PDF_INVOICE_GUEST_ORDER_CUSTOMER_NUMBER = 'sales_pdf/invoice/guestorder_customer_number'; const XML_PATH_SALES_PDF_SHIPMENT_GUEST_ORDER_CUSTOMER_NUMBER = 'sales_pdf/shipment/guestorder_customer_number'; const XML_PATH_SALES_PDF_CREDITMEMO_GUEST_ORDER_CUSTOMER_NUMBER = 'sales_pdf/creditmemo/guestorder_customer_number'; + + const XML_PATH_SALES_PDF_ORDER_FILENAME_EXPORT_PATTERN = 'sales_pdf/order/filename_export_pattern'; 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_ORDER_EXPORT_PATTERN_FOR_MULTIPLE_DOCUMENTS = 'sales_pdf/order/filename_export_pattern_for_multiple_documents'; 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_FIREGENTO_PDF_PAGE_SIZE = 'sales_pdf/firegento_pdf/page_size'; const XML_PATH_REGULAR_FONT = 'sales_pdf/firegento_pdf_fonts/regular_font'; @@ -125,21 +136,30 @@ public function isLogoFullWidth($store) public function showCustomerNumber($mode = 'invoice', $store) { switch ($mode) { + case 'order': + return Mage::getStoreConfigFlag( + self::XML_PATH_SALES_PDF_ORDER_SHOW_CUSTOMER_NUMBER, + $store + ); + break; case 'invoice': return Mage::getStoreConfigFlag( self::XML_PATH_SALES_PDF_INVOICE_SHOW_CUSTOMER_NUMBER, $store ); + break; case 'shipment': return Mage::getStoreConfigFlag( self::XML_PATH_SALES_PDF_SHIPMENT_SHOW_CUSTOMER_NUMBER, $store ); + break; case 'creditmemo': return Mage::getStoreConfigFlag( self::XML_PATH_SALES_PDF_CREDITMEMO_SHOW_CUSTOMER_NUMBER, $store ); + break; } return true; // backwards compatibility @@ -156,21 +176,30 @@ public function showCustomerNumber($mode = 'invoice', $store) public function showCustomerVATNumber($mode = 'invoice', $store) { switch ($mode) { + case 'order': + return Mage::getStoreConfigFlag( + self::XML_PATH_SALES_PDF_ORDER_SHOW_CUSTOMER_VATNUMBER, + $store + ); + break; case 'invoice': return Mage::getStoreConfigFlag( self::XML_PATH_SALES_PDF_INVOICE_SHOW_CUSTOMER_VATNUMBER, $store ); + break; case 'shipment': return Mage::getStoreConfigFlag( self::XML_PATH_SALES_PDF_SHIPMENT_SHOW_CUSTOMER_VATNUMBER, $store ); + break; case 'creditmemo': return Mage::getStoreConfigFlag( self::XML_PATH_SALES_PDF_CREDITMEMO_SHOW_CUSTOMER_VATNUMBER, $store ); + break; } return true; // backwards compatibility @@ -187,6 +216,11 @@ public function showCustomerVATNumber($mode = 'invoice', $store) public function getGuestorderCustomerNo($mode = 'invoice', $store) { switch ($mode) { + case 'order': + return trim(Mage::getStoreConfigFlag( + self::XML_PATH_SALES_PDF_ORDER_GUEST_ORDER_CUSTOMER_NUMBER, + $store + )); case 'invoice': return trim(Mage::getStoreConfigFlag( self::XML_PATH_SALES_PDF_INVOICE_GUEST_ORDER_CUSTOMER_NUMBER, @@ -249,6 +283,10 @@ public function getScaledImageSize($image, $maxWidth, $maxHeight) public function getExportPattern($type) { switch ($type) { + case 'order': + return Mage::getStoreConfig( + self::XML_PATH_SALES_PDF_ORDER_FILENAME_EXPORT_PATTERN + ); case 'invoice': return Mage::getStoreConfig( self::XML_PATH_SALES_PDF_INVOICE_FILENAME_EXPORT_PATTERN @@ -276,6 +314,10 @@ public function getExportPattern($type) public function getExportPatternForMultipleDocuments($type) { switch ($type) { + case 'order': + return Mage::getStoreConfig( + self::XML_PATH_SALES_PDF_ORDER_FILENAME_EXPORT_PATTERN_FOR_MULTIPLE_DOCUMENTS + ); case 'invoice': return Mage::getStoreConfig( self::XML_PATH_SALES_PDF_INVOICE_FILENAME_EXPORT_PATTERN_FOR_MULTIPLE_DOCUMENTS diff --git a/src/app/code/community/FireGento/Pdf/Model/Engine/Abstract.php b/src/app/code/community/FireGento/Pdf/Model/Engine/Abstract.php index 3e8fbc2..123b8c1 100644 --- a/src/app/code/community/FireGento/Pdf/Model/Engine/Abstract.php +++ b/src/app/code/community/FireGento/Pdf/Model/Engine/Abstract.php @@ -475,11 +475,18 @@ protected function insertHeader(&$page, $order, $document) if ($mode == 'invoice') { $title = 'Invoice'; + $numberTitle = 'Invoice number:'; } elseif ($mode == 'shipment') { $title = 'Packingslip'; - } else { + $numberTitle = 'Shipment number:'; + } elseif($mode == 'creditmemo') { $title = 'Creditmemo'; + $numberTitle = 'Creditmemo number:'; + } else { + $title = 'Order'; + $numberTitle = 'Order number:'; } + $page->drawText(Mage::helper('firegento_pdf')->__($title), $this->margin['left'], $this->y, $this->encoding); @@ -493,15 +500,6 @@ protected function insertHeader(&$page, $order, $document) $width = 80; $numberOfLines = 0; - - // Invoice/shipment/creditmemo Number - if ($mode == 'invoice') { - $numberTitle = 'Invoice number:'; - } elseif ($mode == 'shipment') { - $numberTitle = 'Shipment number:'; - } else { - $numberTitle = 'Creditmemo number:'; - } $page->drawText( Mage::helper('firegento_pdf')->__($numberTitle), ($this->margin['right'] - $labelRightOffset), $this->y, diff --git a/src/app/code/community/FireGento/Pdf/Model/Engine/Creditmemo/Default.php b/src/app/code/community/FireGento/Pdf/Model/Engine/Creditmemo/Default.php index b51f8c9..266cfb4 100644 --- a/src/app/code/community/FireGento/Pdf/Model/Engine/Creditmemo/Default.php +++ b/src/app/code/community/FireGento/Pdf/Model/Engine/Creditmemo/Default.php @@ -94,14 +94,13 @@ public function getPdf($creditmemos = array()) // Add footer $this->_addFooter($page, $creditmemo->getStore()); - - if ($creditmemo->getStoreId()) { - Mage::app()->getLocale()->revert(); - } } $this->_afterGetPdf(); + if ($creditmemo->getStoreId()) { + Mage::app()->getLocale()->revert(); + } return $pdf; } diff --git a/src/app/code/community/FireGento/Pdf/Model/Engine/Invoice/Default.php b/src/app/code/community/FireGento/Pdf/Model/Engine/Invoice/Default.php index 0abcb5e..29a310a 100644 --- a/src/app/code/community/FireGento/Pdf/Model/Engine/Invoice/Default.php +++ b/src/app/code/community/FireGento/Pdf/Model/Engine/Invoice/Default.php @@ -98,10 +98,6 @@ public function getPdf($invoices = array()) // Add footer $this->_addFooter($page, $invoice->getStore()); - - if ($invoice->getStoreId()) { - Mage::app()->getLocale()->revert(); - } } $this->_afterGetPdf(); diff --git a/src/app/code/community/FireGento/Pdf/Model/Engine/Order/Default.php b/src/app/code/community/FireGento/Pdf/Model/Engine/Order/Default.php new file mode 100644 index 0000000..a71dad5 --- /dev/null +++ b/src/app/code/community/FireGento/Pdf/Model/Engine/Order/Default.php @@ -0,0 +1,384 @@ + + * @copyright 2014 FireGento Team (http://www.firegento.com) + * @license http://opensource.org/licenses/gpl-3.0 GNU General Public License, version 3 (GPLv3) + */ +/** + * Default invoice rendering engine. + * + * @category FireGento + * @package FireGento_Pdf + * @author FireGento Team + */ +class FireGento_Pdf_Model_Engine_Order_Default extends FireGento_Pdf_Model_Engine_Abstract +{ + + /** + * constructor to set mode to invoice + */ + public function __construct() + { + parent::__construct(); + $this->setMode('order'); + } + + /** + * Return PDF document + * + * @param array $orders invoices to render pdfs for + * + * @return Zend_Pdf + */ + public function getPdf($orders = array()) + { + + $this->_beforeGetPdf(); + + $this->_initRenderer('order'); + + $pdf = new Zend_Pdf(); + $this->_setPdf($pdf); + + foreach($orders as $order) + { + /** + * @var Mage_Sales_Model_Order $order + */ + // pagecounter is 0 at the beginning, because it is incremented in newPage() + $this->pagecounter = 0; + if ($order->getStoreId()) { + Mage::app()->getLocale()->emulate($order->getStoreId()); + Mage::app()->setCurrentStore($order->getStoreId()); + } + //$order = $order->getOrder(); + $this->setOrder($order); + + $page = $this->newPage(); + + $this->insertAddressesAndHeader($page, $order, $order); + + $this->_setFontRegular($page, 9); + $this->insertTableHeader($page); + + $this->y -= 20; + + $position = 0; + + foreach($order->getAllItems() as $item) + { + /** + * @var Mage_Sales_Model_Order_Item $item + */ + if($item->getParentItem()) { + continue; + } + + $showFooter = Mage::getStoreConfig('sales_pdf/firegento_pdf/show_footer'); + if ($this->y < 50 || ($showFooter == 1 && $this->y < 100)) { + $page = $this->newPage(array()); + } + + $position++; + $page = $this->_drawItem($item, $page, $order, $position); + } + + /* add line after items */ + $page->drawLine($this->margin['left'], $this->y + 5, $this->margin['right'], $this->y + 5); + + /* add totals */ + $page = $this->insertTotals($page, $order); + + /* add note */ + $page = $this->_insertNote($page, $order, $order); + + // Add footer + $this->_addFooter($page, $order->getStore()); + } + + $this->_afterGetPdf(); + + return $pdf; + } + + /** + * Draw + * + * @param Varien_Object $item creditmemo/shipping/invoice to draw + * @param Zend_Pdf_Page $page Current page object of Zend_Pdf + * @param Mage_Sales_Model_Order $order order to get infos from + * @param int $position position in table + * + * @return Zend_Pdf_Page + */ + protected function _drawItem( + Varien_Object $item, + Zend_Pdf_Page $page, + Mage_Sales_Model_Order $order, + $position = 1 + ) { + $type = $item->getProductType(); + + $renderer = $this->_getRenderer($type); + $renderer->setOrder($order); + $renderer->setItem($item); + $renderer->setPdf($this); + $renderer->setPage($page); + $renderer->setRenderedModel($this); + + $renderer->draw($position); + + return $renderer->getPage(); + } + + /** + * Insert Table Header for Items + * + * @param Zend_Pdf_Page &$page current page object of Zend_PDF + * + * @return void + */ + protected function insertTableHeader(&$page) + { + $page->setFillColor($this->colors['grey1']); + $page->setLineColor($this->colors['grey1']); + $page->setLineWidth(1); + $page->drawRectangle($this->margin['left'], $this->y, $this->margin['right'], $this->y - 15); + + $page->setFillColor($this->colors['black']); + $font = $this->_setFontRegular($page, 9); + + $this->y -= 11; + $page->drawText(Mage::helper('firegento_pdf')->__('Pos'), $this->margin['left'] + 3, $this->y, $this->encoding); + $page->drawText( + Mage::helper('firegento_pdf')->__('No.'), $this->margin['left'] + 25, $this->y, $this->encoding + ); + $page->drawText( + Mage::helper('firegento_pdf')->__('Description'), $this->margin['left'] + 130, $this->y, $this->encoding + ); + + $columns = array(); + $columns['price'] = array( + 'label' => Mage::helper('firegento_pdf')->__('Price'), + '_width' => 60 + ); + $columns['price_incl_tax'] = array( + 'label' => Mage::helper('firegento_pdf')->__('Price (incl. tax)'), + '_width' => 60 + ); + $columns['qty'] = array( + 'label' => Mage::helper('firegento_pdf')->__('Qty'), + '_width' => 40 + ); + $columns['tax'] = array( + 'label' => Mage::helper('firegento_pdf')->__('Tax'), + '_width' => 50 + ); + $columns['tax_rate'] = array( + 'label' => Mage::helper('firegento_pdf')->__('Tax rate'), + '_width' => 50 + ); + $columns['subtotal'] = array( + 'label' => Mage::helper('firegento_pdf')->__('Total'), + '_width' => 50 + ); + $columns['subtotal_incl_tax'] = array( + 'label' => Mage::helper('firegento_pdf')->__('Total (incl. tax)'), + '_width' => 70 + ); + // draw price, tax, and subtotal in specified order + $columnsOrder = explode(',', Mage::getStoreConfig('sales_pdf/invoice/item_price_column_order')); + // draw starting from right + $columnsOrder = array_reverse($columnsOrder); + $columnOffset = 0; + foreach ($columnsOrder as $columnName) { + $columnName = trim($columnName); + if (array_key_exists($columnName, $columns)) { + $column = $columns[$columnName]; + $labelWidth = $this->widthForStringUsingFontSize($column['label'], $font, 9); + $page->drawText( + $column['label'], + $this->margin['right'] - $columnOffset - $labelWidth, + $this->y, + $this->encoding + ); + $columnOffset += $column['_width']; + } + } + } + + + /** + * Insert Totals Block + * + * @param object $page Current page object of Zend_Pdf + * @param object $source Fields of footer + * + * @return Zend_Pdf_Page + */ + protected function insertTotals($page, $source) + { + $this->y -= 15; + + $order = $source->getOrder(); + if(!is_object($order)) { + $order = $source; + $source->setOrder($order); + } + + parent::insertTotals($page, $source); + + return $page; + /* + $totalTax = 0; + $shippingTaxRate = 0; + $shippingTaxAmount = $order->getShippingTaxAmount(); + + if ($shippingTaxAmount > 0) { + $shippingTaxRate + = $order->getShippingTaxAmount() * 100 + / ($order->getShippingInclTax() + - $order->getShippingTaxAmount()); + } + + $groupedTax = array(); + + $items['items'] = array(); + foreach ($source->getAllItems() as $item) { + if ($item->getOrderItem()->getParentItem()) { + continue; + } + $items['items'][] = $item->getOrderItem()->toArray(); + } + + array_push( + $items['items'], array( + 'row_invoiced' => $order->getShippingInvoiced(), + 'tax_inc_subtotal' => false, + 'tax_percent' => $shippingTaxRate, + 'tax_amount' => $shippingTaxAmount + ) + ); + + foreach ($items['items'] as $item) { + $_percent = null; + if (!isset($item['tax_amount'])) { + $item['tax_amount'] = 0; + } + if (!isset($item['row_invoiced'])) { + $item['row_invoiced'] = 0; + } + if (!isset($item['price'])) { + $item['price'] = 0; + } + if (!isset($item['tax_inc_subtotal'])) { + $item['tax_inc_subtotal'] = 0; + } + if (((float)$item['tax_amount'] > 0) + && ((float)$item['row_invoiced'] > 0) + ) { + $_percent = round($item["tax_percent"], 0); + } + if (!array_key_exists('tax_inc_subtotal', $item) + || $item['tax_inc_subtotal'] + ) { + $totalTax += $item['tax_amount']; + } + if (($item['tax_amount']) && $_percent) { + if (!array_key_exists((int)$_percent, $groupedTax)) { + $groupedTax[$_percent] = $item['tax_amount']; + } else { + $groupedTax[$_percent] += $item['tax_amount']; + } + } + } + + $totals = $this->_getTotalsList($source); + + $lineBlock = array( + 'lines' => array(), + 'height' => 20 + ); + + foreach ($totals as $total) { + $total->setOrder($order)->setSource($source); + + if ($total->canDisplay()) { + $total->setFontSize(10); + // fix Magento 1.8 bug, so that taxes for shipping do not appear twice + // see https://github.com/firegento/firegento-pdf/issues/106 + $uniqueTotalsForDisplay = array_map( + 'unserialize', array_unique(array_map('serialize', + $total->getTotalsForDisplay())) + ); + foreach ($uniqueTotalsForDisplay as $totalData) { + $label = $this->fixNumberFormat($totalData['label']); + $lineBlock['lines'][] = array( + array( + 'text' => $label, + 'feed' => $this->margin['right'] - 70, + 'align' => 'right', + 'font_size' => $totalData['font_size'] + ), + array( + 'text' => $totalData['amount'], + 'feed' => $this->margin['right'], + 'align' => 'right', + 'font_size' => $totalData['font_size'] + ), + ); + } + } + } + $page = $this->drawLineBlocks($page, array($lineBlock)); + + return $page; + */ + } + + + + /** + * Initialize renderer process + * + * @param string $type renderer type to be initialized + * + * @return void + */ + protected function _initRenderer($type) + { + parent::_initRenderer($type); + + $this->_renderers['default'] = array( + 'model' => 'firegento_pdf/items_default', + 'renderer' => null + ); + $this->_renderers['grouped'] = array( + 'model' => 'firegento_pdf/items_grouped', + 'renderer' => null + ); + $this->_renderers['bundle'] = array( + 'model' => 'firegento_pdf/items_bundle', + 'renderer' => null + ); + $this->_renderers['downloadable'] = array( + 'model' => 'firegento_pdf/items_downloadable', + 'renderer' => null + ); + } + +} diff --git a/src/app/code/community/FireGento/Pdf/Model/Engine/Shipment/Default.php b/src/app/code/community/FireGento/Pdf/Model/Engine/Shipment/Default.php index 0236b19..63eacd3 100644 --- a/src/app/code/community/FireGento/Pdf/Model/Engine/Shipment/Default.php +++ b/src/app/code/community/FireGento/Pdf/Model/Engine/Shipment/Default.php @@ -96,10 +96,6 @@ public function getPdf($shipments = array()) // Add footer $this->_addFooter($page, $shipment->getStore()); - - if ($shipment->getStoreId()) { - Mage::app()->getLocale()->revert(); - } } $this->_afterGetPdf(); diff --git a/src/app/code/community/FireGento/Pdf/Model/Items/Default.php b/src/app/code/community/FireGento/Pdf/Model/Items/Default.php index 1613d39..156cce8 100644 --- a/src/app/code/community/FireGento/Pdf/Model/Items/Default.php +++ b/src/app/code/community/FireGento/Pdf/Model/Items/Default.php @@ -27,6 +27,55 @@ */ class FireGento_Pdf_Model_Items_Default extends Mage_Sales_Model_Order_Pdf_Items_Invoice_Default { + /** + * Return item Sku + * + * @param $item + * @return mixed + */ + public function getSku($item) + { + if($item->getSku()) + { + return $item->getSku(); + + } elseif($item->getOrderItem()->getProductOptionByCode('simple_sku')) { + + return $item->getOrderItem()->getProductOptionByCode('simple_sku'); + } + } + + /** + * Retrieve item options + * + * @return array + */ + public function getItemOptions() + { + $result = array(); + + if(is_object($this->getItem()->getOrderItem())) + { + $options = $this->getItem()->getOrderItem()->getProductOptions(); + } else { + $options = $this->getItem()->getProductOptions(); + } + + if (is_array($options)) + { + if (isset($options['options'])) { + $result = array_merge($result, $options['options']); + } + if (isset($options['additional_options'])) { + $result = array_merge($result, $options['additional_options']); + } + if (isset($options['attributes_info'])) { + $result = array_merge($result, $options['attributes_info']); + } + } + return $result; + } + /** * Draw item line. * @@ -36,12 +85,15 @@ class FireGento_Pdf_Model_Items_Default extends Mage_Sales_Model_Order_Pdf_Items */ public function draw($position = 1) { - $order = $this->getOrder(); - $item = $this->getItem(); $pdf = $this->getPdf(); $page = $this->getPage(); + $order = $this->getOrder(); + $item = $this->getItem(); + if(!is_object($item->getOrderItem())) { + $item->setOrderItem($item); + $item->setQty($item->getQtyOrdered()); + } $lines = array(); - $fontSize = 9; // draw Position Number diff --git a/src/app/code/community/FireGento/Pdf/Model/Observer.php b/src/app/code/community/FireGento/Pdf/Model/Observer.php index 5cfce64..50374b6 100644 --- a/src/app/code/community/FireGento/Pdf/Model/Observer.php +++ b/src/app/code/community/FireGento/Pdf/Model/Observer.php @@ -27,6 +27,21 @@ */ class FireGento_Pdf_Model_Observer { + public function adminhtmlBlockHtmlBefore(Varien_Event_Observer $observer) + { + $block = $observer->getEvent()->getBlock(); + if ($block instanceof Mage_Adminhtml_Block_Sales_Order_View) + { + $onClick = "setLocation('".Mage::helper("adminhtml")->getUrl('adminhtml/sales_order_pdf/print/order_id/'.$block->getOrderId())."')"; + $block->addButton('print_order_pdf', array( + 'label' => Mage::helper('sales')->__('Print'), + 'onclick' => $onClick, + 'class' => 'save' + )); + } + } + + /** * Add notes to invoice document. * diff --git a/src/app/code/community/FireGento/Pdf/Model/Order.php b/src/app/code/community/FireGento/Pdf/Model/Order.php new file mode 100644 index 0000000..6e9c07a --- /dev/null +++ b/src/app/code/community/FireGento/Pdf/Model/Order.php @@ -0,0 +1,75 @@ + + * @copyright 2014 FireGento Team (http://www.firegento.com) + * @license http://opensource.org/licenses/gpl-3.0 GNU General Public License, version 3 (GPLv3) + */ +/** + * Invoice model rewrite. + * + * The invoice model serves as a proxy to the actual PDF engine as set via + * backend configuration. + * + * @category FireGento + * @package FireGento_Pdf + * @author FireGento Team + */ +class FireGento_Pdf_Model_Order +{ + + /** + * The actual PDF engine responsible for rendering the file. + * + * @var Mage_Sales_Model_Order_Pdf_Abstract + */ + private $_engine; + + /** + * get pdf rendering engine + * + * @return Mage_Sales_Model_Order_Pdf_Abstract|Mage_Sales_Model_Order_Pdf_Invoice + */ + protected function getEngine() + { + if (!$this->_engine) + { + $modelClass = Mage::getStoreConfig('sales_pdf/order/engine'); + $engine = Mage::getModel($modelClass); + //die(get_class($engine)); + if (!$engine) { + // Fallback to Magento standard invoice layout. + $engine = new Mage_Sales_Model_Order_Pdf_Invoice(); + } + $this->_engine = $engine; + } + + return $this->_engine; + } + + /** + * get pdf for invoices + * + * @param array|Varien_Data_Collection $invoices invoices to render pdfs for + * + * @return mixed + */ + public function getPdf($orders = array()) + { + return $this->getEngine()->getPdf($orders); + } + +} diff --git a/src/app/code/community/FireGento/Pdf/controllers/Adminhtml/Sales/Order/PdfController.php b/src/app/code/community/FireGento/Pdf/controllers/Adminhtml/Sales/Order/PdfController.php new file mode 100644 index 0000000..1a12d51 --- /dev/null +++ b/src/app/code/community/FireGento/Pdf/controllers/Adminhtml/Sales/Order/PdfController.php @@ -0,0 +1,56 @@ + + * @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/OrderController.php'); + +/** + * Class FireGento_Pdf_Adminhtml_Sales_Order_InvoiceController + * + * @category FireGento + * @package FireGento_Pdf + * @author FireGento Team + */ +class FireGento_Pdf_Adminhtml_Sales_Order_PdfController + extends Mage_Adminhtml_Sales_OrderController +{ + + /** + * Create pdf for current invoice + */ + public function printAction() + { + $this->_initOrder(); + if ($orderId = $this->getRequest()->getParam('order_id')) + { + if($order = Mage::getModel('sales/order')->load($orderId)) + { + $pdf = Mage::getModel('firegento_pdf/order')->getPdf(array($order)); + $this->_prepareDownloadResponse( + Mage::helper('firegento_pdf')->getExportFilename('order', $order), + $pdf->render(), 'application/pdf' + ); + } + } else { + $this->_forward('noRoute'); + } + } + +} diff --git a/src/app/code/community/FireGento/Pdf/controllers/Sales/OrderController.php b/src/app/code/community/FireGento/Pdf/controllers/Sales/OrderController.php index b8f1999..24d6ffe 100644 --- a/src/app/code/community/FireGento/Pdf/controllers/Sales/OrderController.php +++ b/src/app/code/community/FireGento/Pdf/controllers/Sales/OrderController.php @@ -32,9 +32,14 @@ class FireGento_Pdf_Sales_OrderController extends Mage_Sales_OrderController { protected $_types = array( - 'invoice', 'creditmemo', 'shipment' + 'order', 'invoice', 'creditmemo', 'shipment' ); + public function printOrderAction() + { + $this->printDocument('order'); + } + /** * Print PDF Invoice Action * diff --git a/src/app/code/community/FireGento/Pdf/etc/config.xml b/src/app/code/community/FireGento/Pdf/etc/config.xml index cc934dd..06d98de 100644 --- a/src/app/code/community/FireGento/Pdf/etc/config.xml +++ b/src/app/code/community/FireGento/Pdf/etc/config.xml @@ -108,6 +108,7 @@ 650 + @@ -140,6 +141,17 @@ + + + + + singleton + FireGento_Pdf_Model_Observer + adminhtmlBlockHtmlBefore + + + + diff --git a/src/app/code/community/FireGento/Pdf/etc/system.xml b/src/app/code/community/FireGento/Pdf/etc/system.xml index ecadbad..19e5436 100644 --- a/src/app/code/community/FireGento/Pdf/etc/system.xml +++ b/src/app/code/community/FireGento/Pdf/etc/system.xml @@ -176,6 +176,70 @@ + + + text + 1 + 1 + 1 + 250 + + + + select + firegento_pdf/system_config_source_order_engine + 0 + 1 + 1 + 1 + + + + select + adminhtml/system_config_source_yesno + 35 + 1 + 1 + 1 + + + + + 36 + 1 + 1 + 1 + 1 + + + + select + adminhtml/system_config_source_yesno + 45 + 1 + 1 + 1 + + + + text + 120 + 1 + 1 + 1 + available placeholders: {{invoice_id}}, {{order_id}}, {{customer_id}}, {{customer_name}}, {{customer_firstname}}, {{customer_lastname}} and strftime() date formats like "%Y" + + + + text + 140 + 1 + 1 + 1 + available placeholder: strftime() date formats like "%Y" + + + 300 @@ -207,7 +271,7 @@ 1 - + select adminhtml/system_config_source_yesno 45 From 1082f197daf8ba0269a00a2b46fd9f35460ae84a Mon Sep 17 00:00:00 2001 From: SG Date: Tue, 1 Dec 2015 09:54:18 +0100 Subject: [PATCH 2/2] Adjustment for better utilization of existing structure --- .../Pdf/Model/Engine/Order/Default.php | 44 ++-- .../FireGento/Pdf/Model/Items/Default.php | 58 +---- .../Pdf/Model/Items/Order/Default.php | 233 ++++++++++++++++++ .../System/Config/Source/Order/Engine.php | 67 +++++ 4 files changed, 320 insertions(+), 82 deletions(-) create mode 100644 src/app/code/community/FireGento/Pdf/Model/Items/Order/Default.php create mode 100644 src/app/code/community/FireGento/Pdf/Model/System/Config/Source/Order/Engine.php diff --git a/src/app/code/community/FireGento/Pdf/Model/Engine/Order/Default.php b/src/app/code/community/FireGento/Pdf/Model/Engine/Order/Default.php index a71dad5..86f4271 100644 --- a/src/app/code/community/FireGento/Pdf/Model/Engine/Order/Default.php +++ b/src/app/code/community/FireGento/Pdf/Model/Engine/Order/Default.php @@ -65,7 +65,6 @@ public function getPdf($orders = array()) Mage::app()->getLocale()->emulate($order->getStoreId()); Mage::app()->setCurrentStore($order->getStoreId()); } - //$order = $order->getOrder(); $this->setOrder($order); $page = $this->newPage(); @@ -73,12 +72,12 @@ public function getPdf($orders = array()) $this->insertAddressesAndHeader($page, $order, $order); $this->_setFontRegular($page, 9); + $this->insertTableHeader($page); $this->y -= 20; $position = 0; - foreach($order->getAllItems() as $item) { /** @@ -221,12 +220,11 @@ protected function insertTableHeader(&$page) } } - /** * Insert Totals Block * * @param object $page Current page object of Zend_Pdf - * @param object $source Fields of footer + * @param Mage_Sales_Model_Abstract $source * * @return Zend_Pdf_Page */ @@ -234,40 +232,30 @@ protected function insertTotals($page, $source) { $this->y -= 15; - $order = $source->getOrder(); - if(!is_object($order)) { - $order = $source; - $source->setOrder($order); - } - - parent::insertTotals($page, $source); - - return $page; - /* $totalTax = 0; $shippingTaxRate = 0; - $shippingTaxAmount = $order->getShippingTaxAmount(); + $shippingTaxAmount = $source->getShippingTaxAmount(); if ($shippingTaxAmount > 0) { $shippingTaxRate - = $order->getShippingTaxAmount() * 100 - / ($order->getShippingInclTax() - - $order->getShippingTaxAmount()); + = $source->getShippingTaxAmount() * 100 + / ($source->getShippingInclTax() + - $source->getShippingTaxAmount()); } $groupedTax = array(); $items['items'] = array(); foreach ($source->getAllItems() as $item) { - if ($item->getOrderItem()->getParentItem()) { + if ($item->getParentItem()) { continue; } - $items['items'][] = $item->getOrderItem()->toArray(); + $items['items'][] = $item->toArray(); } array_push( $items['items'], array( - 'row_invoiced' => $order->getShippingInvoiced(), + 'row_invoiced' => $source->getShippingInvoiced(), 'tax_inc_subtotal' => false, 'tax_percent' => $shippingTaxRate, 'tax_amount' => $shippingTaxAmount @@ -315,9 +303,12 @@ protected function insertTotals($page, $source) ); foreach ($totals as $total) { - $total->setOrder($order)->setSource($source); + /**@var Mage_Sales_Model_Order_Pdf_Total_Default $total */ + /**@var Mage_Tax_Model_Sales_Pdf_Subtotal $total */ + $total->setOrder($source)->setSource($source); - if ($total->canDisplay()) { + if ($total->canDisplay()) + { $total->setFontSize(10); // fix Magento 1.8 bug, so that taxes for shipping do not appear twice // see https://github.com/firegento/firegento-pdf/issues/106 @@ -325,7 +316,8 @@ protected function insertTotals($page, $source) 'unserialize', array_unique(array_map('serialize', $total->getTotalsForDisplay())) ); - foreach ($uniqueTotalsForDisplay as $totalData) { + foreach ($uniqueTotalsForDisplay as $totalData) + { $label = $this->fixNumberFormat($totalData['label']); $lineBlock['lines'][] = array( array( @@ -347,11 +339,9 @@ protected function insertTotals($page, $source) $page = $this->drawLineBlocks($page, array($lineBlock)); return $page; - */ } - /** * Initialize renderer process * @@ -364,7 +354,7 @@ protected function _initRenderer($type) parent::_initRenderer($type); $this->_renderers['default'] = array( - 'model' => 'firegento_pdf/items_default', + 'model' => 'firegento_pdf/items_order_default', 'renderer' => null ); $this->_renderers['grouped'] = array( diff --git a/src/app/code/community/FireGento/Pdf/Model/Items/Default.php b/src/app/code/community/FireGento/Pdf/Model/Items/Default.php index 156cce8..1613d39 100644 --- a/src/app/code/community/FireGento/Pdf/Model/Items/Default.php +++ b/src/app/code/community/FireGento/Pdf/Model/Items/Default.php @@ -27,55 +27,6 @@ */ class FireGento_Pdf_Model_Items_Default extends Mage_Sales_Model_Order_Pdf_Items_Invoice_Default { - /** - * Return item Sku - * - * @param $item - * @return mixed - */ - public function getSku($item) - { - if($item->getSku()) - { - return $item->getSku(); - - } elseif($item->getOrderItem()->getProductOptionByCode('simple_sku')) { - - return $item->getOrderItem()->getProductOptionByCode('simple_sku'); - } - } - - /** - * Retrieve item options - * - * @return array - */ - public function getItemOptions() - { - $result = array(); - - if(is_object($this->getItem()->getOrderItem())) - { - $options = $this->getItem()->getOrderItem()->getProductOptions(); - } else { - $options = $this->getItem()->getProductOptions(); - } - - if (is_array($options)) - { - if (isset($options['options'])) { - $result = array_merge($result, $options['options']); - } - if (isset($options['additional_options'])) { - $result = array_merge($result, $options['additional_options']); - } - if (isset($options['attributes_info'])) { - $result = array_merge($result, $options['attributes_info']); - } - } - return $result; - } - /** * Draw item line. * @@ -85,15 +36,12 @@ public function getItemOptions() */ public function draw($position = 1) { - $pdf = $this->getPdf(); - $page = $this->getPage(); $order = $this->getOrder(); $item = $this->getItem(); - if(!is_object($item->getOrderItem())) { - $item->setOrderItem($item); - $item->setQty($item->getQtyOrdered()); - } + $pdf = $this->getPdf(); + $page = $this->getPage(); $lines = array(); + $fontSize = 9; // draw Position Number diff --git a/src/app/code/community/FireGento/Pdf/Model/Items/Order/Default.php b/src/app/code/community/FireGento/Pdf/Model/Items/Order/Default.php new file mode 100644 index 0000000..9c5f9d2 --- /dev/null +++ b/src/app/code/community/FireGento/Pdf/Model/Items/Order/Default.php @@ -0,0 +1,233 @@ + + * @copyright 2015 FireGento Team (http://www.firegento.com) + * @license http://opensource.org/licenses/gpl-3.0 GNU General Public License, version 3 (GPLv3) + */ +/** + * Default item model rewrite. + * + * @category FireGento + * @package FireGento_Pdf + * @author FireGento Team + */ +class FireGento_Pdf_Model_Items_Order_Default extends Mage_Sales_Model_Order_Pdf_Items_Abstract +{ + /** + * Return item Sku + * + * @param $item + * @return mixed + */ + public function getSku($item) + { + if($item->getSku()) + { + return $item->getSku(); + + } elseif($item->getOrderItem()->getProductOptionByCode('simple_sku')) { + + return $item->getOrderItem()->getProductOptionByCode('simple_sku'); + } + } + + /** + * Retrieve item options + * + * @return array + */ + public function getItemOptions() + { + $result = array(); + + if(is_object($this->getItem()->getOrderItem())) + { + $options = $this->getItem()->getOrderItem()->getProductOptions(); + } else { + $options = $this->getItem()->getProductOptions(); + } + + if (is_array($options)) + { + if (isset($options['options'])) { + $result = array_merge($result, $options['options']); + } + if (isset($options['additional_options'])) { + $result = array_merge($result, $options['additional_options']); + } + if (isset($options['attributes_info'])) { + $result = array_merge($result, $options['attributes_info']); + } + } + return $result; + } + + /** + * Draw item line. + * + * @param int $position position of the product + * + * @return void + */ + public function draw($position = 1) + { + $pdf = $this->getPdf(); + $page = $this->getPage(); + $order = $this->getOrder(); + $item = $this->getItem(); + if(!is_object($item->getOrderItem())) { + $item->setOrderItem($item); + $item->setQty($item->getQtyOrdered()); + } + $lines = array(); + $fontSize = 9; + + // draw Position Number + $lines[0] = array( + array( + 'text' => $position, + 'feed' => $pdf->margin['left'] + 10, + 'align' => 'right', + 'font_size' => $fontSize + ) + ); + + // draw SKU + $lines[0][] = array( + 'text' => Mage::helper('core/string')->str_split($this->getSku($item), 19), + 'feed' => $pdf->margin['left'] + 25, + 'font_size' => $fontSize + ); + + // draw Product name + $lines[0][] = array( + 'text' => Mage::helper('core/string')->str_split($item->getName(), 40, true, true), + 'feed' => $pdf->margin['left'] + 130, + 'font_size' => $fontSize + ); + + $options = $this->getItemOptions(); + if ($options) { + foreach ($options as $option) { + $optionTxt = $option['label'] . ': '; + // append option value + if (isset($option['value'])) { + $optionTxt .= isset($option['print_value']) ? $option['print_value'] : strip_tags($option['value']); + } + $optionArray = $pdf->_prepareText($optionTxt, $page, $pdf->getFontRegular(), $fontSize, 215); + $lines[][] = array( + 'text' => $optionArray, + 'feed' => $pdf->margin['left'] + 135 + ); + } + } + + $columns = array(); + // prepare qty + $columns['qty'] = array( + 'text' => $item->getQty() * 1, + 'align' => 'right', + 'font_size' => $fontSize, + '_width' => 30 + ); + + // prepare price + $columns['price'] = array( + 'text' => $order->formatPriceTxt($item->getPrice()), + 'align' => 'right', + 'font_size' => $fontSize, + '_width' => 60 + ); + + // prepare price_incl_tax + $columns['price_incl_tax'] = array( + 'text' => $order->formatPriceTxt($item->getPriceInclTax()), + 'align' => 'right', + 'font_size' => $fontSize, + '_width' => 60 + ); + + // prepare tax + $columns['tax'] = array( + 'text' => $order->formatPriceTxt($item->getTaxAmount() + $item->getHiddenTaxAmount()), + 'align' => 'right', + 'font_size' => $fontSize, + '_width' => 50 + ); + + // prepare tax_rate + $columns['tax_rate'] = array( + 'text' => round($item->getOrderItem()->getTaxPercent(), 2) . '%', + 'align' => 'right', + 'font_size' => $fontSize, + '_width' => 50 + ); + + // prepare subtotal + $columns['subtotal'] = array( + 'text' => $order->formatPriceTxt($item->getRowTotal()), + 'align' => 'right', + 'font_size' => $fontSize, + '_width' => 50 + ); + + // prepare subtotal_incl_tax + $columns['subtotal_incl_tax'] = array( + 'text' => $order->formatPriceTxt($item->getRowTotalInclTax()), + 'align' => 'right', + 'font_size' => $fontSize, + '_width' => 70 + ); + + // draw columns in specified order + $columnsOrder = explode(',', Mage::getStoreConfig('sales_pdf/invoice/item_price_column_order')); + // draw starting from right + $columnsOrder = array_reverse($columnsOrder); + $columnOffset = 0; + foreach ($columnsOrder as $columnName) { + $columnName = trim($columnName); + if (array_key_exists($columnName, $columns)) { + $column = $columns[$columnName]; + $column['feed'] = $pdf->margin['right'] - $columnOffset; + $columnOffset += $column['_width']; + unset($column['_width']); + $lines[0][] = $column; + } + } + + if (Mage::getStoreConfig('sales_pdf/invoice/show_item_discount') && 0 < $item->getDiscountAmount()) { + // print discount + $text = Mage::helper('firegento_pdf')->__( + 'You get a discount of %s.', + $order->formatPriceTxt($item->getDiscountAmount()) + ); + $lines[][] = array( + 'text' => $text, + 'align' => 'right', + 'feed' => $pdf->margin['right'] - $columnOffset + ); + } + + $lineBlock = array( + 'lines' => $lines, + 'height' => 15 + ); + + $page = $pdf->drawLineBlocks($page, array($lineBlock), array('table_header' => true)); + $this->setPage($page); + } +} diff --git a/src/app/code/community/FireGento/Pdf/Model/System/Config/Source/Order/Engine.php b/src/app/code/community/FireGento/Pdf/Model/System/Config/Source/Order/Engine.php new file mode 100644 index 0000000..4253a0b --- /dev/null +++ b/src/app/code/community/FireGento/Pdf/Model/System/Config/Source/Order/Engine.php @@ -0,0 +1,67 @@ + + * @copyright 2014 FireGento Team (http://www.firegento.com) + * @license http://opensource.org/licenses/gpl-3.0 GNU General Public License, version 3 (GPLv3) + */ +/** + * Pdf creation engine source model. + * + * @category FireGento + * @package FireGento_Pdf + * @author FireGento Team + */ +class FireGento_Pdf_Model_System_Config_Source_Order_Engine +{ + /** + * Config xpath to pdf engine node + * + */ + const XML_PATH_PDF_ENGINE = 'global/pdf/firegento_invoice_engines'; + + /** + * Return array of possible engines. + * + * @return array + */ + public function toOptionArray() + { + // load default engines shipped with Mage_Sales and FireGento_Pdf + $engines = array( + 'firegento_pdf/engine_order_default' => Mage::helper('firegento_pdf')->__('Standard FireGento') + ); + + // load additional engines provided by third party extensions + $engineNodes = Mage::app()->getConfig()->getNode(self::XML_PATH_PDF_ENGINE); + if ($engineNodes && $engineNodes->hasChildren()) { + foreach ($engineNodes->children() as $engineName => $engineNode) { + $className = (string) $engineNode->class; + $engineLabel = Mage::helper('firegento_pdf')->__((string) $engineNode->label); + $engines[$className] = $engineLabel; + } + } + + $options = array(); + foreach ($engines as $k => $v) { + $options[] = array( + 'value' => $k, + 'label' => $v + ); + } + return $options; + } +}