PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/local/Ced/CsOrder/Model/Sales/Order/Pdf/Invoice.php

https://gitlab.com/vincent.perdereau/picandparts
PHP | 352 lines | 254 code | 44 blank | 54 comment | 28 complexity | aaab84b6c15e6f06385f3ef07989bd3d MD5 | raw file
  1. <?php
  2. /**
  3. * CedCommerce
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. *
  12. * @category Ced;
  13. * @package Ced_CsOrder
  14. * @author CedCommerce Core Team <coreteam@cedcommerce.com>
  15. * @copyright Copyright CedCommerce (http://cedcommerce.com/)
  16. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  17. */
  18. class Ced_CsOrder_Model_Sales_Order_Pdf_Invoice extends Mage_Sales_Model_Order_Pdf_Invoice
  19. {
  20. /**
  21. * Return PDF document
  22. *
  23. * @param array $invoices
  24. * @return Zend_Pdf
  25. */
  26. public function getPdf($invoices = array())
  27. {
  28. $this->_beforeGetPdf();
  29. $this->_initRenderer('invoice');
  30. $pdf = new Zend_Pdf();
  31. $this->_setPdf($pdf);
  32. $style = new Zend_Pdf_Style();
  33. $this->_setFontBold($style, 10);
  34. foreach ($invoices as $invoice) {
  35. if ($invoice->getStoreId()) {
  36. Mage::app()->getLocale()->emulate($invoice->getStoreId());
  37. Mage::app()->setCurrentStore($invoice->getStoreId());
  38. }
  39. $page = $this->newPage();
  40. $vorder=Mage::getModel('csorder/vorders')->getVorderByInvoice($invoice);
  41. $this->setVorder($vorder);
  42. $order = $invoice->getOrder();
  43. /* Add image */
  44. $this->insertLogo($page, $invoice->getStore());
  45. /* Add address */
  46. $this->insertAddress($page, $invoice->getStore());
  47. /* Add head */
  48. $this->insertOrder(
  49. $page,
  50. $order,
  51. Mage::getStoreConfigFlag(self::XML_PATH_SALES_PDF_INVOICE_PUT_ORDER_ID, $order->getStoreId())
  52. );
  53. /* Add document text and number */
  54. $this->insertDocumentNumber(
  55. $page,
  56. Mage::helper('sales')->__('Invoice # ') . $invoice->getIncrementId()
  57. );
  58. /* Add table */
  59. $this->_drawHeader($page);
  60. /* Add body */
  61. $vendorId = Mage::getSingleton('customer/session')->getVendorId();
  62. foreach ($invoice->getAllItems() as $item){
  63. $vendorIdProductId = Mage::getModel('csmarketplace/vproducts')
  64. ->getVendorIdByProduct($item->getProductId());
  65. if ($item->getOrderItem()->getParentItem() || $vendorId!=$vendorIdProductId
  66. ) {
  67. continue;
  68. }
  69. /* Draw item */
  70. $this->_drawItem($item, $page, $order);
  71. $page = end($pdf->pages);
  72. }
  73. /* Add totals */
  74. $this->insertTotals($page, $invoice);
  75. if ($invoice->getStoreId()) {
  76. Mage::app()->getLocale()->revert();
  77. }
  78. }
  79. $this->_afterGetPdf();
  80. return $pdf;
  81. }
  82. /**
  83. * Insert order to pdf page
  84. *
  85. * @param Zend_Pdf_Page $page
  86. * @param Mage_Sales_Model_Order $obj
  87. * @param bool $putOrderId
  88. */
  89. protected function insertOrder(&$page, $obj, $putOrderId = true)
  90. {
  91. if ($obj instanceof Mage_Sales_Model_Order) {
  92. $shipment = null;
  93. $order = $obj;
  94. } elseif ($obj instanceof Mage_Sales_Model_Order_Shipment) {
  95. $shipment = $obj;
  96. $order = $shipment->getOrder();
  97. }
  98. $this->y = $this->y ? $this->y : 815;
  99. $top = $this->y;
  100. $page->setFillColor(new Zend_Pdf_Color_GrayScale(0.45));
  101. $page->setLineColor(new Zend_Pdf_Color_GrayScale(0.45));
  102. $page->drawRectangle(25, $top, 570, $top - 55);
  103. $page->setFillColor(new Zend_Pdf_Color_GrayScale(1));
  104. $this->setDocHeaderCoordinates(array(25, $top, 570, $top - 55));
  105. $this->_setFontRegular($page, 10);
  106. if ($putOrderId) {
  107. $page->drawText(
  108. Mage::helper('sales')->__('Order # ') . $order->getRealOrderId(), 35, ($top -= 30), 'UTF-8'
  109. );
  110. }
  111. $page->drawText(
  112. Mage::helper('sales')->__('Order Date: ') . Mage::helper('core')->formatDate(
  113. $order->getCreatedAtStoreDate(), 'medium', false
  114. ),
  115. 35,
  116. ($top -= 15),
  117. 'UTF-8'
  118. );
  119. $top -= 10;
  120. $page->setFillColor(new Zend_Pdf_Color_Rgb(0.93, 0.92, 0.92));
  121. $page->setLineColor(new Zend_Pdf_Color_GrayScale(0.5));
  122. $page->setLineWidth(0.5);
  123. $page->drawRectangle(25, $top, 275, ($top - 25));
  124. $page->drawRectangle(275, $top, 570, ($top - 25));
  125. /* Calculate blocks info */
  126. /* Billing Address */
  127. $billingAddress = $this->_formatAddress($order->getBillingAddress()->format('pdf'));
  128. /* Payment */
  129. $paymentInfo = Mage::helper('payment')->getInfoBlock($order->getPayment())
  130. ->setIsSecureMode(true)
  131. ->toPdf();
  132. $paymentInfo = htmlspecialchars_decode($paymentInfo, ENT_QUOTES);
  133. $payment = explode('{{pdf_row_separator}}', $paymentInfo);
  134. foreach ($payment as $key=>$value){
  135. if (strip_tags(trim($value)) == '') {
  136. unset($payment[$key]);
  137. }
  138. }
  139. reset($payment);
  140. /* Shipping Address and Method */
  141. if (!$order->getIsVirtual()) {
  142. /* Shipping Address */
  143. $shippingAddress = $this->_formatAddress($order->getShippingAddress()->format('pdf'));
  144. $shippingMethod = $order->getShippingDescription();
  145. }
  146. $page->setFillColor(new Zend_Pdf_Color_GrayScale(0));
  147. $this->_setFontBold($page, 12);
  148. $page->drawText(Mage::helper('sales')->__('Sold to:'), 35, ($top - 15), 'UTF-8');
  149. if (!$order->getIsVirtual()) {
  150. $page->drawText(Mage::helper('sales')->__('Ship to:'), 285, ($top - 15), 'UTF-8');
  151. } else {
  152. $page->drawText(Mage::helper('sales')->__('Payment Method:'), 285, ($top - 15), 'UTF-8');
  153. }
  154. $addressesHeight = $this->_calcAddressHeight($billingAddress);
  155. if (isset($shippingAddress)) {
  156. $addressesHeight = max($addressesHeight, $this->_calcAddressHeight($shippingAddress));
  157. }
  158. $page->setFillColor(new Zend_Pdf_Color_GrayScale(1));
  159. $page->drawRectangle(25, ($top - 25), 570, $top - 33 - $addressesHeight);
  160. $page->setFillColor(new Zend_Pdf_Color_GrayScale(0));
  161. $this->_setFontRegular($page, 10);
  162. $this->y = $top - 40;
  163. $addressesStartY = $this->y;
  164. foreach ($billingAddress as $value){
  165. if ($value !== '') {
  166. $text = array();
  167. foreach (Mage::helper('core/string')->str_split($value, 45, true, true) as $_value) {
  168. $text[] = $_value;
  169. }
  170. foreach ($text as $part) {
  171. $page->drawText(strip_tags(ltrim($part)), 35, $this->y, 'UTF-8');
  172. $this->y -= 15;
  173. }
  174. }
  175. }
  176. $addressesEndY = $this->y;
  177. if (!$order->getIsVirtual()) {
  178. $this->y = $addressesStartY;
  179. foreach ($shippingAddress as $value){
  180. if ($value!=='') {
  181. $text = array();
  182. foreach (Mage::helper('core/string')->str_split($value, 45, true, true) as $_value) {
  183. $text[] = $_value;
  184. }
  185. foreach ($text as $part) {
  186. $page->drawText(strip_tags(ltrim($part)), 285, $this->y, 'UTF-8');
  187. $this->y -= 15;
  188. }
  189. }
  190. }
  191. $addressesEndY = min($addressesEndY, $this->y);
  192. $this->y = $addressesEndY;
  193. $page->setFillColor(new Zend_Pdf_Color_Rgb(0.93, 0.92, 0.92));
  194. $page->setLineWidth(0.5);
  195. $page->drawRectangle(25, $this->y, 275, $this->y-25);
  196. $page->drawRectangle(275, $this->y, 570, $this->y-25);
  197. $this->y -= 15;
  198. $this->_setFontBold($page, 12);
  199. $page->setFillColor(new Zend_Pdf_Color_GrayScale(0));
  200. $page->drawText(Mage::helper('sales')->__('Payment Method'), 35, $this->y, 'UTF-8');
  201. $page->drawText(Mage::helper('sales')->__('Shipping Method:'), 285, $this->y , 'UTF-8');
  202. $this->y -=10;
  203. $page->setFillColor(new Zend_Pdf_Color_GrayScale(1));
  204. $this->_setFontRegular($page, 10);
  205. $page->setFillColor(new Zend_Pdf_Color_GrayScale(0));
  206. $paymentLeft = 35;
  207. $yPayments = $this->y - 15;
  208. }
  209. else {
  210. $yPayments = $addressesStartY;
  211. $paymentLeft = 285;
  212. }
  213. foreach ($payment as $value){
  214. if (trim($value) != '') {
  215. //Printing "Payment Method" lines
  216. $value = preg_replace('/<br[^>]*>/i', "\n", $value);
  217. foreach (Mage::helper('core/string')->str_split($value, 45, true, true) as $_value) {
  218. $page->drawText(strip_tags(trim($_value)), $paymentLeft, $yPayments, 'UTF-8');
  219. $yPayments -= 15;
  220. }
  221. }
  222. }
  223. if ($order->getIsVirtual()) {
  224. // replacement of Shipments-Payments rectangle block
  225. $yPayments = min($addressesEndY, $yPayments);
  226. $page->drawLine(25, ($top - 25), 25, $yPayments);
  227. $page->drawLine(570, ($top - 25), 570, $yPayments);
  228. $page->drawLine(25, $yPayments, 570, $yPayments);
  229. $this->y = $yPayments - 15;
  230. } else {
  231. $topMargin = 15;
  232. $methodStartY = $this->y;
  233. $this->y -= 15;
  234. foreach (Mage::helper('core/string')->str_split($shippingMethod, 45, true, true) as $_value) {
  235. $page->drawText(strip_tags(trim($_value)), 285, $this->y, 'UTF-8');
  236. $this->y -= 15;
  237. }
  238. $yShipments = $this->y;
  239. $totalShippingChargesText = "";
  240. $vorder = $this->getVorder();
  241. if(Mage::helper('csorder')->canShowShipmentBlock($vorder))
  242. $totalShippingChargesText = "(" . Mage::helper('sales')->__('Total Shipping Charges') . " "
  243. . $order->formatPriceTxt($order->getShippingAmount()) . ")";
  244. $page->drawText($totalShippingChargesText, 285, $yShipments - $topMargin, 'UTF-8');
  245. $yShipments -= $topMargin + 10;
  246. $tracks = array();
  247. if ($shipment) {
  248. $tracks = $shipment->getAllTracks();
  249. }
  250. if (count($tracks)) {
  251. $page->setFillColor(new Zend_Pdf_Color_Rgb(0.93, 0.92, 0.92));
  252. $page->setLineWidth(0.5);
  253. $page->drawRectangle(285, $yShipments, 510, $yShipments - 10);
  254. $page->drawLine(400, $yShipments, 400, $yShipments - 10);
  255. //$page->drawLine(510, $yShipments, 510, $yShipments - 10);
  256. $this->_setFontRegular($page, 9);
  257. $page->setFillColor(new Zend_Pdf_Color_GrayScale(0));
  258. //$page->drawText(Mage::helper('sales')->__('Carrier'), 290, $yShipments - 7 , 'UTF-8');
  259. $page->drawText(Mage::helper('sales')->__('Title'), 290, $yShipments - 7, 'UTF-8');
  260. $page->drawText(Mage::helper('sales')->__('Number'), 410, $yShipments - 7, 'UTF-8');
  261. $yShipments -= 20;
  262. $this->_setFontRegular($page, 8);
  263. foreach ($tracks as $track) {
  264. $CarrierCode = $track->getCarrierCode();
  265. if ($CarrierCode != 'custom') {
  266. $carrier = Mage::getSingleton('shipping/config')->getCarrierInstance($CarrierCode);
  267. $carrierTitle = $carrier->getConfigData('title');
  268. } else {
  269. $carrierTitle = Mage::helper('sales')->__('Custom Value');
  270. }
  271. //$truncatedCarrierTitle = substr($carrierTitle, 0, 35) . (strlen($carrierTitle) > 35 ? '...' : '');
  272. $maxTitleLen = 45;
  273. $endOfTitle = strlen($track->getTitle()) > $maxTitleLen ? '...' : '';
  274. $truncatedTitle = substr($track->getTitle(), 0, $maxTitleLen) . $endOfTitle;
  275. //$page->drawText($truncatedCarrierTitle, 285, $yShipments , 'UTF-8');
  276. $page->drawText($truncatedTitle, 292, $yShipments , 'UTF-8');
  277. $page->drawText($track->getNumber(), 410, $yShipments , 'UTF-8');
  278. $yShipments -= $topMargin - 5;
  279. }
  280. } else {
  281. $yShipments -= $topMargin - 5;
  282. }
  283. $currentY = min($yPayments, $yShipments);
  284. // replacement of Shipments-Payments rectangle block
  285. $page->drawLine(25, $methodStartY, 25, $currentY); //left
  286. $page->drawLine(25, $currentY, 570, $currentY); //bottom
  287. $page->drawLine(570, $currentY, 570, $methodStartY); //right
  288. $this->y = $currentY;
  289. $this->y -= 15;
  290. }
  291. }
  292. /**
  293. * Retrieve order model object
  294. *
  295. * @return Mage_Sales_Model_Order
  296. */
  297. public function getVorder()
  298. {
  299. return Mage::registry('current_vorder');
  300. }
  301. public function setVorder($vorder){
  302. if(Mage::registry('current_vorder'))
  303. {
  304. Mage::unregister('current_vorder');
  305. }
  306. Mage::register('current_vorder',$vorder);
  307. }
  308. }