PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

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

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