/Pdf/Model/Items/Abstract.php

https://bitbucket.org/a_parkhomenko/magento-tcpdf · PHP · 305 lines · 141 code · 25 blank · 139 comment · 18 complexity · c150ba6e788f833024a330704c560301 MD5 · raw file

  1. <?php
  2. /**
  3. * Magento
  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. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Sales
  23. * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Sales Order Pdf Items renderer Abstract
  28. *
  29. * @category Mage
  30. * @package Mage_Sales
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. abstract class Wm_Pdf_Model_Items_Abstract extends Mage_Core_Model_Abstract
  34. {
  35. /**
  36. * Order model
  37. *
  38. * @var Mage_Sales_Model_Order
  39. */
  40. protected $_order;
  41. /**
  42. * Source model (invoice, shipment, creditmemo)
  43. *
  44. * @var Mage_Core_Model_Abstract
  45. */
  46. protected $_source;
  47. /**
  48. * Item object
  49. *
  50. * @var Varien_Object
  51. */
  52. protected $_item;
  53. /**
  54. * Pdf object
  55. *
  56. * @var Mage_Sales_Model_Order_Pdf_Abstract
  57. */
  58. protected $_pdf;
  59. /**
  60. * Set order model
  61. *
  62. * @param Mage_Sales_Model_Order $order
  63. * @return Mage_Sales_Model_Order_Pdf_Items_Abstract
  64. */
  65. public function setOrder(Mage_Sales_Model_Order $order)
  66. {
  67. $this->_order = $order;
  68. return $this;
  69. }
  70. /**
  71. * Set Source model
  72. *
  73. * @param Mage_Core_Model_Abstract $source
  74. * @return Mage_Sales_Model_Order_Pdf_Items_Abstract
  75. */
  76. public function setSource(Mage_Core_Model_Abstract $source)
  77. {
  78. $this->_source = $source;
  79. return $this;
  80. }
  81. /**
  82. * Set item object
  83. *
  84. * @param Varien_Object $item
  85. * @return Mage_Sales_Model_Order_Pdf_Items_Abstract
  86. */
  87. public function setItem(Varien_Object $item)
  88. {
  89. $this->_item = $item;
  90. return $this;
  91. }
  92. /**
  93. * Set Pdf model
  94. *
  95. * @param Pdf $pdf
  96. * @return Wm_Pdf_Model_Items_Abstract
  97. */
  98. public function setPdf(Pdf $pdf)
  99. {
  100. $this->_pdf = $pdf;
  101. return $this;
  102. }
  103. /**
  104. * Retrieve order object
  105. *
  106. * @throws Mage_Core_Exception
  107. * @return Mage_Sales_Model_Order
  108. */
  109. public function getOrder()
  110. {
  111. if (is_null($this->_order)) {
  112. Mage::throwException(Mage::helper('sales')->__('Order object is not specified.'));
  113. }
  114. return $this->_order;
  115. }
  116. /**
  117. * Retrieve source object
  118. *
  119. * @throws Mage_Core_Exception
  120. * @return Mage_Core_Model_Abstract
  121. */
  122. public function getSource()
  123. {
  124. if (is_null($this->_source)) {
  125. Mage::throwException(Mage::helper('sales')->__('Source object is not specified.'));
  126. }
  127. return $this->_source;
  128. }
  129. /**
  130. * Retrieve item object
  131. *
  132. * @throws Mage_Core_Exception
  133. * @return Varien_Object
  134. */
  135. public function getItem()
  136. {
  137. if (is_null($this->_item)) {
  138. Mage::throwException(Mage::helper('sales')->__('Item object is not specified.'));
  139. }
  140. return $this->_item;
  141. }
  142. /**
  143. * Retrieve Pdf model
  144. *
  145. * @throws Mage_Core_Exception
  146. * @return Mage_Sales_Model_Order_Pdf_Abstract
  147. */
  148. public function getPdf()
  149. {
  150. if (is_null($this->_pdf)) {
  151. Mage::throwException(Mage::helper('sales')->__('PDF object is not specified.'));
  152. }
  153. return $this->_pdf;
  154. }
  155. /**
  156. * Draw item line
  157. *
  158. */
  159. abstract public function draw();
  160. /**
  161. * Format option value process
  162. *
  163. * @param $value
  164. * @return string
  165. */
  166. protected function _formatOptionValue($value)
  167. {
  168. $order = $this->getOrder();
  169. $resultValue = '';
  170. if (is_array($value)) {
  171. if (isset($value['qty'])) {
  172. $resultValue .= sprintf('%d', $value['qty']) . ' x ';
  173. }
  174. $resultValue .= $value['title'];
  175. if (isset($value['price'])) {
  176. $resultValue .= " " . $order->formatPrice($value['price']);
  177. }
  178. return $resultValue;
  179. } else {
  180. return $value;
  181. }
  182. }
  183. /**
  184. * Get array of arrays with item prices information for display in PDF
  185. * array(
  186. * $index => array(
  187. * 'label' => $label,
  188. * 'price' => $price,
  189. * 'subtotal' => $subtotal
  190. * )
  191. * )
  192. * @return array
  193. */
  194. public function getItemPricesForDisplay()
  195. {
  196. $order = $this->getOrder();
  197. $item = $this->getItem();
  198. if (Mage::helper('tax')->displaySalesBothPrices()) {
  199. $prices = array(
  200. array(
  201. 'label' => Mage::helper('tax')->__('Excl. Tax') . ':',
  202. 'price' => $order->formatPriceTxt($item->getPrice()),
  203. 'subtotal' => $order->formatPriceTxt($item->getRowTotal())
  204. ),
  205. array(
  206. 'label' => Mage::helper('tax')->__('Incl. Tax') . ':',
  207. 'price' => $order->formatPriceTxt($item->getPriceInclTax()),
  208. 'subtotal' => $order->formatPriceTxt($item->getRowTotalInclTax())
  209. ),
  210. );
  211. } elseif (Mage::helper('tax')->displaySalesPriceInclTax()) {
  212. $prices = array(array(
  213. 'price' => $order->formatPriceTxt($item->getPriceInclTax()),
  214. 'subtotal' => $order->formatPriceTxt($item->getRowTotalInclTax()),
  215. ));
  216. } else {
  217. $prices = array(array(
  218. 'price' => $order->formatPriceTxt($item->getPrice()),
  219. 'subtotal' => $order->formatPriceTxt($item->getRowTotal()),
  220. ));
  221. }
  222. return $prices;
  223. }
  224. /**
  225. * Retrieve item options
  226. *
  227. * @return Varien_Object
  228. */
  229. public function getItemOptions() {
  230. $result = new Varien_Object();
  231. if ($options = $this->getItem()->getOrderItem()->getProductOptions()) {
  232. if (isset($options['options'])) {
  233. $result->addData($options['options']);
  234. }
  235. if (isset($options['additional_options'])) {
  236. $result->addData($options['additional_options']);
  237. }
  238. if (isset($options['attributes_info'])) {
  239. /**
  240. * add additional options to object
  241. */
  242. $count = count($options['attributes_info']);
  243. for ($i=0;$i<$count;$i++) {
  244. if (isset($options['attributes_info'][$i]['label']) &&
  245. isset($options['attributes_info'][$i]['value'])) {
  246. $label = strtolower($options['attributes_info'][$i]['label']);
  247. $value = $options['attributes_info'][$i]['value'];
  248. $result->addData(array($label => $value));
  249. }
  250. }
  251. }
  252. }
  253. return $result;
  254. }
  255. /**
  256. * Return item Sku
  257. *
  258. * @param $item
  259. * @return string
  260. */
  261. public function getSku($item)
  262. {
  263. if ($item->getOrderItem()->getProductOptionByCode('simple_sku'))
  264. return $item->getOrderItem()->getProductOptionByCode('simple_sku');
  265. else
  266. return $item->getSku();
  267. }
  268. /**
  269. * Return real item name
  270. * @param type $item
  271. * @return string
  272. */
  273. public function getName($item)
  274. {
  275. if ($item->getOrderItem()->getProductOptionByCode('simple_name'))
  276. return $item->getOrderItem()->getProductOptionByCode('simple_name');
  277. else
  278. return $item->getName();
  279. }
  280. }