PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/code/Model/Sales/Order/Pdf/Invoice.php

https://bitbucket.org/OSdave/spanishformatinvoice
PHP | 385 lines | 257 code | 71 blank | 57 comment | 26 complexity | e798ad0d0bcd6dbec510bbff8803abb8 MD5 | raw file
  1. <?php
  2. /**
  3. * OSdave's Spanish Invoice
  4. *
  5. * Invoice Pdf Model
  6. *
  7. * @author david.parloir@gmail.com
  8. * @author David Parloir
  9. * @package Osdave
  10. * @subpackage Invoice
  11. * @copyright Copyright (c) David Parloir 2011
  12. * @copyright Licensed under the Creative Commons "Attribution-Noncommercial-Share Alike" License
  13. * http://creativecommons.org/licenses/by-nc-sa/3.0/us/
  14. */
  15. class Osdave_Invoice_Model_Sales_Order_Pdf_Invoice extends Mage_Sales_Model_Order_Pdf_Invoice
  16. {
  17. public function getPdf($invoices = array())
  18. {
  19. $this->_beforeGetPdf();
  20. $this->_initRenderer('invoice');
  21. $pdf = new Zend_Pdf();
  22. $this->_setPdf($pdf);
  23. $style = new Zend_Pdf_Style();
  24. $this->_setFontBold($style, 10);
  25. foreach ($invoices as $invoice) {
  26. if ($invoice->getStoreId()) {
  27. Mage::app()->getLocale()->emulate($invoice->getStoreId());
  28. }
  29. $page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
  30. $pdf->pages[] = $page;
  31. $order = $invoice->getOrder();
  32. /* Add image */
  33. $this->insertLogo($page, $invoice->getStore());
  34. /* Add address */
  35. $this->insertAddress($page, $invoice->getStore());
  36. /* Add head */
  37. $this->insertOrder($page, $order, Mage::getStoreConfigFlag(self::XML_PATH_SALES_PDF_INVOICE_PUT_ORDER_ID, $order->getStoreId()));
  38. $page->setFillColor(new Zend_Pdf_Color_GrayScale(1));
  39. $this->_setFontRegular($page);
  40. $page->drawText(Mage::helper('sales')->__('Invoice # ') . $invoice->getIncrementId(), 35, 780, 'UTF-8');
  41. /* Add table */
  42. $page->setFillColor(new Zend_Pdf_Color_RGB(0.93, 0.92, 0.92));
  43. $page->setLineColor(new Zend_Pdf_Color_GrayScale(0.5));
  44. $page->setLineWidth(0.5);
  45. $page->drawRectangle(25, $this->y, 570, $this->y - 15);
  46. $this->y -=10;
  47. /* Add table head */
  48. $page->setFillColor(new Zend_Pdf_Color_RGB(0.4, 0.4, 0.4));
  49. $page->drawText(Mage::helper('sales')->__('Products'), 35, $this->y, 'UTF-8');
  50. $page->drawText(Mage::helper('sales')->__('SKU'), 255, $this->y, 'UTF-8');
  51. $page->drawText(Mage::helper('sales')->__('Price'), 380, $this->y, 'UTF-8');
  52. $page->drawText(Mage::helper('sales')->__('Qty'), 405, $this->y, 'UTF-8');
  53. $page->drawText(Mage::helper('invoice')->__('Tax Class'), 440, $this->y, 'UTF-8');
  54. $page->drawText(Mage::helper('invoice')->__('Tax Amount'), 490, $this->y, 'UTF-8');
  55. $page->drawText(Mage::helper('sales')->__('Subtotal'), 535, $this->y, 'UTF-8');
  56. $this->y -=15;
  57. $page->setFillColor(new Zend_Pdf_Color_GrayScale(0));
  58. /* Add body */
  59. foreach ($invoice->getAllItems() as $item) {
  60. if ($item->getOrderItem()->getParentItem()) {
  61. continue;
  62. }
  63. if ($this->y < 15) {
  64. $page = $this->newPage(array('table_header' => true));
  65. }
  66. /* Draw item */
  67. $page = $this->_drawItem($item, $page, $order);
  68. }
  69. /* Add totals */
  70. $page = $this->insertTotals($page, $invoice);
  71. /* Add Page Number */
  72. $numPage = 1;
  73. $this->_setFontRegular($page, 5); //smaller font
  74. foreach ($pdf->pages as $pageItem) {
  75. $pageItem->drawText(Mage::helper('invoice')->__('Page %d of %d', $numPage, sizeof($pdf->pages)), 10, 10, 'UTF-8');
  76. $numPage++;
  77. }
  78. if ($invoice->getStoreId()) {
  79. Mage::app()->getLocale()->revert();
  80. }
  81. }
  82. $this->_afterGetPdf();
  83. return $pdf;
  84. }
  85. /**
  86. * Create new page and assign to PDF object
  87. *
  88. * @param array $settings
  89. * @return Zend_Pdf_Page
  90. */
  91. public function newPage(array $settings = array())
  92. {
  93. /* Add new table head */
  94. $page = $this->_getPdf()->newPage(Zend_Pdf_Page::SIZE_A4);
  95. $this->_getPdf()->pages[] = $page;
  96. $this->y = 800;
  97. if (!empty($settings['table_header'])) {
  98. $this->_setFontRegular($page);
  99. $page->setFillColor(new Zend_Pdf_Color_RGB(0.93, 0.92, 0.92));
  100. $page->setLineColor(new Zend_Pdf_Color_GrayScale(0.5));
  101. $page->setLineWidth(0.5);
  102. $page->drawRectangle(25, $this->y, 570, $this->y - 15);
  103. $this->y -=10;
  104. $page->setFillColor(new Zend_Pdf_Color_RGB(0.4, 0.4, 0.4));
  105. $page->drawText(Mage::helper('sales')->__('Product'), 35, $this->y, 'UTF-8');
  106. $page->drawText(Mage::helper('sales')->__('SKU'), 255, $this->y, 'UTF-8');
  107. //put columns more to the left, to make space for new column
  108. $page->drawText(Mage::helper('sales')->__('Price'), 290, $this->y, 'UTF-8');
  109. $page->drawText(Mage::helper('sales')->__('Qty'), 540, $this->y, 'UTF-8');
  110. $page->drawText(Mage::helper('invoice')->__('Tax Class'), 480, $this->y, 'UTF-8');
  111. $page->drawText(Mage::helper('invoice')->__('Tax Amount'), 480, $this->y, 'UTF-8');
  112. $page->drawText(Mage::helper('sales')->__('Subtotal'), 535, $this->y, 'UTF-8');
  113. $page->setFillColor(new Zend_Pdf_Color_GrayScale(0));
  114. $this->y -=20;
  115. }
  116. return $page;
  117. }
  118. /**
  119. * Overwriting abstract methods
  120. * @see Mage_Sales_Model_Order_Pdf_Abstract::insertOrder($page, $order, $putOrderId)
  121. */
  122. protected function insertOrder(&$page, $order, $putOrderId = true)
  123. {
  124. /* @var $order Mage_Sales_Model_Order */
  125. $page->setFillColor(new Zend_Pdf_Color_GrayScale(0.5));
  126. $page->drawRectangle(25, 790, 570, 755);
  127. $page->setFillColor(new Zend_Pdf_Color_GrayScale(1));
  128. $this->_setFontRegular($page);
  129. if ($putOrderId) {
  130. $page->drawText(Mage::helper('sales')->__('Order # ') . $order->getRealOrderId(), 35, 770, 'UTF-8');
  131. }
  132. //$page->drawText(Mage::helper('sales')->__('Order Date: ') . date( 'D M j Y', strtotime( $order->getCreatedAt() ) ), 35, 760, 'UTF-8');
  133. $page->drawText(Mage::helper('sales')->__('Order Date: ') . Mage::helper('core')->formatDate($order->getCreatedAtStoreDate(), 'medium', false), 35, 760, 'UTF-8');
  134. $page->setFillColor(new Zend_Pdf_Color_Rgb(0.93, 0.92, 0.92));
  135. $page->setLineColor(new Zend_Pdf_Color_GrayScale(0.5));
  136. $page->setLineWidth(0.5);
  137. $page->drawRectangle(25, 755, 275, 730);
  138. $page->drawRectangle(275, 755, 570, 730);
  139. /* Calculate blocks info */
  140. /* Billing Address */
  141. $billingAddress = $this->_formatAddress($order->getBillingAddress()->format('pdf'));
  142. /* Payment */
  143. $paymentInfo = Mage::helper('payment')->getInfoBlock($order->getPayment())
  144. ->setIsSecureMode(true)
  145. ->toPdf();
  146. $payment = explode('{{pdf_row_separator}}', $paymentInfo);
  147. foreach ($payment as $key => $value) {
  148. if (strip_tags(trim($value)) == '') {
  149. unset($payment[$key]);
  150. }
  151. }
  152. reset($payment);
  153. /* Shipping Address and Method */
  154. if (!$order->getIsVirtual()) {
  155. /* Shipping Address */
  156. $shippingAddress = $this->_formatAddress($order->getShippingAddress()->format('pdf'));
  157. $shippingMethod = $order->getShippingDescription();
  158. }
  159. $page->setFillColor(new Zend_Pdf_Color_GrayScale(0));
  160. $this->_setFontRegular($page);
  161. $page->drawText(Mage::helper('sales')->__('SOLD TO:'), 35, 740, 'UTF-8');
  162. if (!$order->getIsVirtual()) {
  163. $page->drawText(Mage::helper('sales')->__('SHIP TO:'), 285, 740, 'UTF-8');
  164. } else {
  165. $page->drawText(Mage::helper('sales')->__('Payment Method:'), 285, 740, 'UTF-8');
  166. }
  167. /**
  168. * adding TaxVat number
  169. */
  170. $nif = $order->getCustomerTaxvat();
  171. if (!is_null($nif)) {
  172. $billingAddress[] = Mage::helper('invoice')->__('TaxVat NÂș: ') . strip_tags(ltrim($nif));
  173. }
  174. if (!$order->getIsVirtual()) {
  175. $y = 730 - (max(count($billingAddress), count($shippingAddress)) * 10 + 5);
  176. } else {
  177. $y = 730 - (count($billingAddress) * 10 + 5);
  178. }
  179. $page->setFillColor(new Zend_Pdf_Color_GrayScale(1));
  180. $page->drawRectangle(25, 730, 570, $y);
  181. $page->setFillColor(new Zend_Pdf_Color_GrayScale(0));
  182. $this->_setFontRegular($page);
  183. $this->y = 720;
  184. foreach ($billingAddress as $value) {
  185. if ($value !== '') {
  186. $page->drawText(strip_tags(ltrim($value)), 35, $this->y, 'UTF-8');
  187. $this->y -=10;
  188. }
  189. }
  190. if (!$order->getIsVirtual()) {
  191. /**
  192. * changing $this->y to $newY
  193. * billingAddres has 1 more field now (taxvat number)
  194. */
  195. $newY = 720;
  196. foreach ($shippingAddress as $value) {
  197. if ($value !== '') {
  198. $page->drawText(strip_tags(ltrim($value)), 285, $newY, 'UTF-8');
  199. $newY -=10;
  200. }
  201. }
  202. $page->setFillColor(new Zend_Pdf_Color_Rgb(0.93, 0.92, 0.92));
  203. $page->setLineWidth(0.5);
  204. $page->drawRectangle(25, $this->y, 275, $this->y - 25);
  205. $page->drawRectangle(275, $this->y, 570, $this->y - 25);
  206. $this->y -=15;
  207. $this->_setFontBold($page);
  208. $page->setFillColor(new Zend_Pdf_Color_GrayScale(0));
  209. $page->drawText(Mage::helper('sales')->__('Payment Method'), 35, $this->y, 'UTF-8');
  210. $page->drawText(Mage::helper('sales')->__('Shipping Method:'), 285, $this->y, 'UTF-8');
  211. $this->y -=10;
  212. $page->setFillColor(new Zend_Pdf_Color_GrayScale(1));
  213. $this->_setFontRegular($page);
  214. $page->setFillColor(new Zend_Pdf_Color_GrayScale(0));
  215. $paymentLeft = 35;
  216. $yPayments = $this->y - 15;
  217. } else {
  218. $yPayments = 720;
  219. $paymentLeft = 285;
  220. }
  221. foreach ($payment as $value) {
  222. if (trim($value) !== '') {
  223. $page->drawText(strip_tags(trim($value)), $paymentLeft, $yPayments, 'UTF-8');
  224. $yPayments -=10;
  225. }
  226. }
  227. if (!$order->getIsVirtual()) {
  228. $this->y -=15;
  229. $page->drawText($shippingMethod, 285, $this->y, 'UTF-8');
  230. $yShipments = $this->y;
  231. $totalShippingChargesText = "(" . Mage::helper('sales')->__('Total Shipping Charges') . " " . $order->formatPriceTxt($order->getShippingAmount()) . ")";
  232. $page->drawText($totalShippingChargesText, 285, $yShipments - 7, 'UTF-8');
  233. $yShipments -=10;
  234. $tracks = $order->getTracksCollection();
  235. if (count($tracks)) {
  236. $page->setFillColor(new Zend_Pdf_Color_Rgb(0.93, 0.92, 0.92));
  237. $page->setLineWidth(0.5);
  238. $page->drawRectangle(285, $yShipments, 510, $yShipments - 10);
  239. $page->drawLine(380, $yShipments, 380, $yShipments - 10);
  240. //$page->drawLine(510, $yShipments, 510, $yShipments - 10);
  241. $this->_setFontRegular($page);
  242. $page->setFillColor(new Zend_Pdf_Color_GrayScale(0));
  243. //$page->drawText(Mage::helper('sales')->__('Carrier'), 290, $yShipments - 7 , 'UTF-8');
  244. $page->drawText(Mage::helper('sales')->__('Title'), 290, $yShipments - 7, 'UTF-8');
  245. $page->drawText(Mage::helper('sales')->__('Number'), 385, $yShipments - 7, 'UTF-8');
  246. $yShipments -=17;
  247. $this->_setFontRegular($page, 6);
  248. foreach ($order->getTracksCollection() as $track) {
  249. $CarrierCode = $track->getCarrierCode();
  250. if ($CarrierCode != 'custom') {
  251. $carrier = Mage::getSingleton('shipping/config')->getCarrierInstance($CarrierCode);
  252. $carrierTitle = $carrier->getConfigData('title');
  253. } else {
  254. $carrierTitle = Mage::helper('sales')->__('Custom Value');
  255. }
  256. //$truncatedCarrierTitle = substr($carrierTitle, 0, 35) . (strlen($carrierTitle) > 35 ? '...' : '');
  257. $truncatedTitle = substr($track->getTitle(), 0, 45) . (strlen($track->getTitle()) > 45 ? '...' : '');
  258. //$page->drawText($truncatedCarrierTitle, 285, $yShipments , 'UTF-8');
  259. $page->drawText($truncatedTitle, 300, $yShipments, 'UTF-8');
  260. $page->drawText($track->getNumber(), 395, $yShipments, 'UTF-8');
  261. $yShipments -=7;
  262. }
  263. } else {
  264. $yShipments -= 7;
  265. }
  266. $currentY = min($yPayments, $yShipments);
  267. // replacement of Shipments-Payments rectangle block
  268. $page->drawLine(25, $this->y + 15, 25, $currentY);
  269. $page->drawLine(25, $currentY, 570, $currentY);
  270. $page->drawLine(570, $currentY, 570, $this->y + 15);
  271. $this->y = $currentY;
  272. $this->y -= 15;
  273. }
  274. }
  275. /**
  276. * Overwriting abstract methods
  277. * @see Mage_Sales_Model_Order_Pdf_Abstract::insertTotals($page, $source)
  278. */
  279. protected function insertTotals($page, $source)
  280. {
  281. $order = $source->getOrder();
  282. $totals = $this->_getTotalsList($source);
  283. $lineBlock = array(
  284. 'lines' => array(),
  285. 'height' => 15
  286. );
  287. foreach ($totals as $total) {
  288. $total->setOrder($order)
  289. ->setSource($source);
  290. if ($total->canDisplay()) {
  291. foreach ($total->getTotalsForDisplay() as $totalData) {
  292. $lineBlock['lines'][] = array(
  293. array(
  294. 'text' => $totalData['label'],
  295. 'feed' => 475,
  296. 'align' => 'right',
  297. 'font_size' => $totalData['font_size'],
  298. 'font' => 'bold'
  299. ),
  300. array(
  301. 'text' => $totalData['amount'],
  302. 'feed' => 565,
  303. 'align' => 'right',
  304. 'font_size' => $totalData['font_size'],
  305. 'font' => 'bold'
  306. ),
  307. );
  308. }
  309. }
  310. }
  311. $page = $this->drawLineBlocks($page, array($lineBlock));
  312. return $page;
  313. }
  314. }