/code/Model/Sales/Order/Pdf/Invoice.php
PHP | 385 lines | 257 code | 71 blank | 57 comment | 26 complexity | e798ad0d0bcd6dbec510bbff8803abb8 MD5 | raw file
- <?php
- /**
- * OSdave's Spanish Invoice
- *
- * Invoice Pdf Model
- *
- * @author david.parloir@gmail.com
- * @author David Parloir
- * @package Osdave
- * @subpackage Invoice
- * @copyright Copyright (c) David Parloir 2011
- * @copyright Licensed under the Creative Commons "Attribution-Noncommercial-Share Alike" License
- * http://creativecommons.org/licenses/by-nc-sa/3.0/us/
- */
- class Osdave_Invoice_Model_Sales_Order_Pdf_Invoice extends Mage_Sales_Model_Order_Pdf_Invoice
- {
- public function getPdf($invoices = array())
- {
- $this->_beforeGetPdf();
- $this->_initRenderer('invoice');
- $pdf = new Zend_Pdf();
- $this->_setPdf($pdf);
- $style = new Zend_Pdf_Style();
- $this->_setFontBold($style, 10);
- foreach ($invoices as $invoice) {
- if ($invoice->getStoreId()) {
- Mage::app()->getLocale()->emulate($invoice->getStoreId());
- }
- $page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
- $pdf->pages[] = $page;
- $order = $invoice->getOrder();
- /* Add image */
- $this->insertLogo($page, $invoice->getStore());
- /* Add address */
- $this->insertAddress($page, $invoice->getStore());
- /* Add head */
- $this->insertOrder($page, $order, Mage::getStoreConfigFlag(self::XML_PATH_SALES_PDF_INVOICE_PUT_ORDER_ID, $order->getStoreId()));
- $page->setFillColor(new Zend_Pdf_Color_GrayScale(1));
- $this->_setFontRegular($page);
- $page->drawText(Mage::helper('sales')->__('Invoice # ') . $invoice->getIncrementId(), 35, 780, 'UTF-8');
- /* Add table */
- $page->setFillColor(new Zend_Pdf_Color_RGB(0.93, 0.92, 0.92));
- $page->setLineColor(new Zend_Pdf_Color_GrayScale(0.5));
- $page->setLineWidth(0.5);
- $page->drawRectangle(25, $this->y, 570, $this->y - 15);
- $this->y -=10;
- /* Add table head */
- $page->setFillColor(new Zend_Pdf_Color_RGB(0.4, 0.4, 0.4));
- $page->drawText(Mage::helper('sales')->__('Products'), 35, $this->y, 'UTF-8');
- $page->drawText(Mage::helper('sales')->__('SKU'), 255, $this->y, 'UTF-8');
- $page->drawText(Mage::helper('sales')->__('Price'), 380, $this->y, 'UTF-8');
- $page->drawText(Mage::helper('sales')->__('Qty'), 405, $this->y, 'UTF-8');
- $page->drawText(Mage::helper('invoice')->__('Tax Class'), 440, $this->y, 'UTF-8');
- $page->drawText(Mage::helper('invoice')->__('Tax Amount'), 490, $this->y, 'UTF-8');
- $page->drawText(Mage::helper('sales')->__('Subtotal'), 535, $this->y, 'UTF-8');
- $this->y -=15;
- $page->setFillColor(new Zend_Pdf_Color_GrayScale(0));
- /* Add body */
- foreach ($invoice->getAllItems() as $item) {
- if ($item->getOrderItem()->getParentItem()) {
- continue;
- }
- if ($this->y < 15) {
- $page = $this->newPage(array('table_header' => true));
- }
- /* Draw item */
- $page = $this->_drawItem($item, $page, $order);
- }
- /* Add totals */
- $page = $this->insertTotals($page, $invoice);
- /* Add Page Number */
- $numPage = 1;
- $this->_setFontRegular($page, 5); //smaller font
- foreach ($pdf->pages as $pageItem) {
- $pageItem->drawText(Mage::helper('invoice')->__('Page %d of %d', $numPage, sizeof($pdf->pages)), 10, 10, 'UTF-8');
- $numPage++;
- }
- if ($invoice->getStoreId()) {
- Mage::app()->getLocale()->revert();
- }
- }
- $this->_afterGetPdf();
- return $pdf;
- }
- /**
- * Create new page and assign to PDF object
- *
- * @param array $settings
- * @return Zend_Pdf_Page
- */
- public function newPage(array $settings = array())
- {
- /* Add new table head */
- $page = $this->_getPdf()->newPage(Zend_Pdf_Page::SIZE_A4);
- $this->_getPdf()->pages[] = $page;
- $this->y = 800;
- if (!empty($settings['table_header'])) {
- $this->_setFontRegular($page);
- $page->setFillColor(new Zend_Pdf_Color_RGB(0.93, 0.92, 0.92));
- $page->setLineColor(new Zend_Pdf_Color_GrayScale(0.5));
- $page->setLineWidth(0.5);
- $page->drawRectangle(25, $this->y, 570, $this->y - 15);
- $this->y -=10;
- $page->setFillColor(new Zend_Pdf_Color_RGB(0.4, 0.4, 0.4));
- $page->drawText(Mage::helper('sales')->__('Product'), 35, $this->y, 'UTF-8');
- $page->drawText(Mage::helper('sales')->__('SKU'), 255, $this->y, 'UTF-8');
- //put columns more to the left, to make space for new column
- $page->drawText(Mage::helper('sales')->__('Price'), 290, $this->y, 'UTF-8');
- $page->drawText(Mage::helper('sales')->__('Qty'), 540, $this->y, 'UTF-8');
- $page->drawText(Mage::helper('invoice')->__('Tax Class'), 480, $this->y, 'UTF-8');
- $page->drawText(Mage::helper('invoice')->__('Tax Amount'), 480, $this->y, 'UTF-8');
- $page->drawText(Mage::helper('sales')->__('Subtotal'), 535, $this->y, 'UTF-8');
- $page->setFillColor(new Zend_Pdf_Color_GrayScale(0));
- $this->y -=20;
- }
- return $page;
- }
- /**
- * Overwriting abstract methods
- * @see Mage_Sales_Model_Order_Pdf_Abstract::insertOrder($page, $order, $putOrderId)
- */
- protected function insertOrder(&$page, $order, $putOrderId = true)
- {
- /* @var $order Mage_Sales_Model_Order */
- $page->setFillColor(new Zend_Pdf_Color_GrayScale(0.5));
- $page->drawRectangle(25, 790, 570, 755);
- $page->setFillColor(new Zend_Pdf_Color_GrayScale(1));
- $this->_setFontRegular($page);
- if ($putOrderId) {
- $page->drawText(Mage::helper('sales')->__('Order # ') . $order->getRealOrderId(), 35, 770, 'UTF-8');
- }
- //$page->drawText(Mage::helper('sales')->__('Order Date: ') . date( 'D M j Y', strtotime( $order->getCreatedAt() ) ), 35, 760, 'UTF-8');
- $page->drawText(Mage::helper('sales')->__('Order Date: ') . Mage::helper('core')->formatDate($order->getCreatedAtStoreDate(), 'medium', false), 35, 760, 'UTF-8');
- $page->setFillColor(new Zend_Pdf_Color_Rgb(0.93, 0.92, 0.92));
- $page->setLineColor(new Zend_Pdf_Color_GrayScale(0.5));
- $page->setLineWidth(0.5);
- $page->drawRectangle(25, 755, 275, 730);
- $page->drawRectangle(275, 755, 570, 730);
- /* Calculate blocks info */
- /* Billing Address */
- $billingAddress = $this->_formatAddress($order->getBillingAddress()->format('pdf'));
- /* Payment */
- $paymentInfo = Mage::helper('payment')->getInfoBlock($order->getPayment())
- ->setIsSecureMode(true)
- ->toPdf();
- $payment = explode('{{pdf_row_separator}}', $paymentInfo);
- foreach ($payment as $key => $value) {
- if (strip_tags(trim($value)) == '') {
- unset($payment[$key]);
- }
- }
- reset($payment);
- /* Shipping Address and Method */
- if (!$order->getIsVirtual()) {
- /* Shipping Address */
- $shippingAddress = $this->_formatAddress($order->getShippingAddress()->format('pdf'));
- $shippingMethod = $order->getShippingDescription();
- }
- $page->setFillColor(new Zend_Pdf_Color_GrayScale(0));
- $this->_setFontRegular($page);
- $page->drawText(Mage::helper('sales')->__('SOLD TO:'), 35, 740, 'UTF-8');
- if (!$order->getIsVirtual()) {
- $page->drawText(Mage::helper('sales')->__('SHIP TO:'), 285, 740, 'UTF-8');
- } else {
- $page->drawText(Mage::helper('sales')->__('Payment Method:'), 285, 740, 'UTF-8');
- }
- /**
- * adding TaxVat number
- */
- $nif = $order->getCustomerTaxvat();
- if (!is_null($nif)) {
- $billingAddress[] = Mage::helper('invoice')->__('TaxVat NÂș: ') . strip_tags(ltrim($nif));
- }
- if (!$order->getIsVirtual()) {
- $y = 730 - (max(count($billingAddress), count($shippingAddress)) * 10 + 5);
- } else {
- $y = 730 - (count($billingAddress) * 10 + 5);
- }
- $page->setFillColor(new Zend_Pdf_Color_GrayScale(1));
- $page->drawRectangle(25, 730, 570, $y);
- $page->setFillColor(new Zend_Pdf_Color_GrayScale(0));
- $this->_setFontRegular($page);
- $this->y = 720;
- foreach ($billingAddress as $value) {
- if ($value !== '') {
- $page->drawText(strip_tags(ltrim($value)), 35, $this->y, 'UTF-8');
- $this->y -=10;
- }
- }
- if (!$order->getIsVirtual()) {
- /**
- * changing $this->y to $newY
- * billingAddres has 1 more field now (taxvat number)
- */
- $newY = 720;
- foreach ($shippingAddress as $value) {
- if ($value !== '') {
- $page->drawText(strip_tags(ltrim($value)), 285, $newY, 'UTF-8');
- $newY -=10;
- }
- }
- $page->setFillColor(new Zend_Pdf_Color_Rgb(0.93, 0.92, 0.92));
- $page->setLineWidth(0.5);
- $page->drawRectangle(25, $this->y, 275, $this->y - 25);
- $page->drawRectangle(275, $this->y, 570, $this->y - 25);
- $this->y -=15;
- $this->_setFontBold($page);
- $page->setFillColor(new Zend_Pdf_Color_GrayScale(0));
- $page->drawText(Mage::helper('sales')->__('Payment Method'), 35, $this->y, 'UTF-8');
- $page->drawText(Mage::helper('sales')->__('Shipping Method:'), 285, $this->y, 'UTF-8');
- $this->y -=10;
- $page->setFillColor(new Zend_Pdf_Color_GrayScale(1));
- $this->_setFontRegular($page);
- $page->setFillColor(new Zend_Pdf_Color_GrayScale(0));
- $paymentLeft = 35;
- $yPayments = $this->y - 15;
- } else {
- $yPayments = 720;
- $paymentLeft = 285;
- }
- foreach ($payment as $value) {
- if (trim($value) !== '') {
- $page->drawText(strip_tags(trim($value)), $paymentLeft, $yPayments, 'UTF-8');
- $yPayments -=10;
- }
- }
- if (!$order->getIsVirtual()) {
- $this->y -=15;
- $page->drawText($shippingMethod, 285, $this->y, 'UTF-8');
- $yShipments = $this->y;
- $totalShippingChargesText = "(" . Mage::helper('sales')->__('Total Shipping Charges') . " " . $order->formatPriceTxt($order->getShippingAmount()) . ")";
- $page->drawText($totalShippingChargesText, 285, $yShipments - 7, 'UTF-8');
- $yShipments -=10;
- $tracks = $order->getTracksCollection();
- if (count($tracks)) {
- $page->setFillColor(new Zend_Pdf_Color_Rgb(0.93, 0.92, 0.92));
- $page->setLineWidth(0.5);
- $page->drawRectangle(285, $yShipments, 510, $yShipments - 10);
- $page->drawLine(380, $yShipments, 380, $yShipments - 10);
- //$page->drawLine(510, $yShipments, 510, $yShipments - 10);
- $this->_setFontRegular($page);
- $page->setFillColor(new Zend_Pdf_Color_GrayScale(0));
- //$page->drawText(Mage::helper('sales')->__('Carrier'), 290, $yShipments - 7 , 'UTF-8');
- $page->drawText(Mage::helper('sales')->__('Title'), 290, $yShipments - 7, 'UTF-8');
- $page->drawText(Mage::helper('sales')->__('Number'), 385, $yShipments - 7, 'UTF-8');
- $yShipments -=17;
- $this->_setFontRegular($page, 6);
- foreach ($order->getTracksCollection() as $track) {
- $CarrierCode = $track->getCarrierCode();
- if ($CarrierCode != 'custom') {
- $carrier = Mage::getSingleton('shipping/config')->getCarrierInstance($CarrierCode);
- $carrierTitle = $carrier->getConfigData('title');
- } else {
- $carrierTitle = Mage::helper('sales')->__('Custom Value');
- }
- //$truncatedCarrierTitle = substr($carrierTitle, 0, 35) . (strlen($carrierTitle) > 35 ? '...' : '');
- $truncatedTitle = substr($track->getTitle(), 0, 45) . (strlen($track->getTitle()) > 45 ? '...' : '');
- //$page->drawText($truncatedCarrierTitle, 285, $yShipments , 'UTF-8');
- $page->drawText($truncatedTitle, 300, $yShipments, 'UTF-8');
- $page->drawText($track->getNumber(), 395, $yShipments, 'UTF-8');
- $yShipments -=7;
- }
- } else {
- $yShipments -= 7;
- }
- $currentY = min($yPayments, $yShipments);
- // replacement of Shipments-Payments rectangle block
- $page->drawLine(25, $this->y + 15, 25, $currentY);
- $page->drawLine(25, $currentY, 570, $currentY);
- $page->drawLine(570, $currentY, 570, $this->y + 15);
- $this->y = $currentY;
- $this->y -= 15;
- }
- }
- /**
- * Overwriting abstract methods
- * @see Mage_Sales_Model_Order_Pdf_Abstract::insertTotals($page, $source)
- */
- protected function insertTotals($page, $source)
- {
- $order = $source->getOrder();
- $totals = $this->_getTotalsList($source);
- $lineBlock = array(
- 'lines' => array(),
- 'height' => 15
- );
- foreach ($totals as $total) {
- $total->setOrder($order)
- ->setSource($source);
- if ($total->canDisplay()) {
- foreach ($total->getTotalsForDisplay() as $totalData) {
- $lineBlock['lines'][] = array(
- array(
- 'text' => $totalData['label'],
- 'feed' => 475,
- 'align' => 'right',
- 'font_size' => $totalData['font_size'],
- 'font' => 'bold'
- ),
- array(
- 'text' => $totalData['amount'],
- 'feed' => 565,
- 'align' => 'right',
- 'font_size' => $totalData['font_size'],
- 'font' => 'bold'
- ),
- );
- }
- }
- }
- $page = $this->drawLineBlocks($page, array($lineBlock));
- return $page;
- }
- }