AbstractItems.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Model\Order\Pdf\Items;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. /**
  9. * Sales Order Pdf Items renderer Abstract
  10. *
  11. * @api
  12. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  13. * @since 100.0.2
  14. */
  15. abstract class AbstractItems extends \Magento\Framework\Model\AbstractModel
  16. {
  17. /**
  18. * Order model
  19. *
  20. * @var \Magento\Sales\Model\Order
  21. */
  22. protected $_order;
  23. /**
  24. * Source model (invoice, shipment, creditmemo)
  25. *
  26. * @var \Magento\Framework\Model\AbstractModel
  27. */
  28. protected $_source;
  29. /**
  30. * Item object
  31. *
  32. * @var \Magento\Framework\DataObject
  33. */
  34. protected $_item;
  35. /**
  36. * Pdf object
  37. *
  38. * @var \Magento\Sales\Model\Order\Pdf\AbstractPdf
  39. */
  40. protected $_pdf;
  41. /**
  42. * Pdf current page
  43. *
  44. * @var \Zend_Pdf_Page
  45. */
  46. protected $_pdfPage;
  47. /**
  48. * Tax data
  49. *
  50. * @var \Magento\Tax\Helper\Data
  51. */
  52. protected $_taxData;
  53. /**
  54. * @var \Magento\Framework\Filesystem\Directory\ReadInterface
  55. */
  56. protected $_rootDirectory;
  57. /**
  58. * @var \Magento\Framework\Filter\FilterManager
  59. */
  60. protected $filterManager;
  61. /**
  62. * @param \Magento\Framework\Model\Context $context
  63. * @param \Magento\Framework\Registry $registry
  64. * @param \Magento\Tax\Helper\Data $taxData
  65. * @param \Magento\Framework\Filesystem $filesystem ,
  66. * @param \Magento\Framework\Filter\FilterManager $filterManager
  67. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  68. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  69. * @param array $data
  70. */
  71. public function __construct(
  72. \Magento\Framework\Model\Context $context,
  73. \Magento\Framework\Registry $registry,
  74. \Magento\Tax\Helper\Data $taxData,
  75. \Magento\Framework\Filesystem $filesystem,
  76. \Magento\Framework\Filter\FilterManager $filterManager,
  77. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  78. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  79. array $data = []
  80. ) {
  81. $this->filterManager = $filterManager;
  82. $this->_taxData = $taxData;
  83. $this->_rootDirectory = $filesystem->getDirectoryRead(DirectoryList::ROOT);
  84. parent::__construct($context, $registry, $resource, $resourceCollection, $data);
  85. }
  86. /**
  87. * Set order model
  88. *
  89. * @param \Magento\Sales\Model\Order $order
  90. * @return $this
  91. */
  92. public function setOrder(\Magento\Sales\Model\Order $order)
  93. {
  94. $this->_order = $order;
  95. return $this;
  96. }
  97. /**
  98. * Set Source model
  99. *
  100. * @param \Magento\Framework\Model\AbstractModel $source
  101. * @return $this
  102. */
  103. public function setSource(\Magento\Framework\Model\AbstractModel $source)
  104. {
  105. $this->_source = $source;
  106. return $this;
  107. }
  108. /**
  109. * Set item object
  110. *
  111. * @param \Magento\Framework\DataObject $item
  112. * @return $this
  113. */
  114. public function setItem(\Magento\Framework\DataObject $item)
  115. {
  116. $this->_item = $item;
  117. return $this;
  118. }
  119. /**
  120. * Set Pdf model
  121. *
  122. * @param \Magento\Sales\Model\Order\Pdf\AbstractPdf $pdf
  123. * @return $this
  124. */
  125. public function setPdf(\Magento\Sales\Model\Order\Pdf\AbstractPdf $pdf)
  126. {
  127. $this->_pdf = $pdf;
  128. return $this;
  129. }
  130. /**
  131. * Set current page
  132. *
  133. * @param \Zend_Pdf_Page $page
  134. * @return $this
  135. */
  136. public function setPage(\Zend_Pdf_Page $page)
  137. {
  138. $this->_pdfPage = $page;
  139. return $this;
  140. }
  141. /**
  142. * Retrieve order object
  143. *
  144. * @throws \Magento\Framework\Exception\LocalizedException
  145. * @return \Magento\Sales\Model\Order
  146. */
  147. public function getOrder()
  148. {
  149. if (null === $this->_order) {
  150. throw new \Magento\Framework\Exception\LocalizedException(__('The order object is not specified.'));
  151. }
  152. return $this->_order;
  153. }
  154. /**
  155. * Retrieve source object
  156. *
  157. * @throws \Magento\Framework\Exception\LocalizedException
  158. * @return \Magento\Framework\Model\AbstractModel
  159. */
  160. public function getSource()
  161. {
  162. if (null === $this->_source) {
  163. throw new \Magento\Framework\Exception\LocalizedException(__('The source object is not specified.'));
  164. }
  165. return $this->_source;
  166. }
  167. /**
  168. * Retrieve item object
  169. *
  170. * @throws \Magento\Framework\Exception\LocalizedException
  171. * @return \Magento\Framework\DataObject
  172. */
  173. public function getItem()
  174. {
  175. if (null === $this->_item) {
  176. throw new \Magento\Framework\Exception\LocalizedException(__('An item object is not specified.'));
  177. }
  178. return $this->_item;
  179. }
  180. /**
  181. * Retrieve Pdf model
  182. *
  183. * @throws \Magento\Framework\Exception\LocalizedException
  184. * @return \Magento\Sales\Model\Order\Pdf\AbstractPdf
  185. */
  186. public function getPdf()
  187. {
  188. if (null === $this->_pdf) {
  189. throw new \Magento\Framework\Exception\LocalizedException(__('A PDF object is not specified.'));
  190. }
  191. return $this->_pdf;
  192. }
  193. /**
  194. * Retrieve Pdf page object
  195. *
  196. * @throws \Magento\Framework\Exception\LocalizedException
  197. * @return \Zend_Pdf_Page
  198. */
  199. public function getPage()
  200. {
  201. if (null === $this->_pdfPage) {
  202. throw new \Magento\Framework\Exception\LocalizedException(__('A PDF page object is not specified.'));
  203. }
  204. return $this->_pdfPage;
  205. }
  206. /**
  207. * Draw item line
  208. *
  209. * @return void
  210. */
  211. abstract public function draw();
  212. /**
  213. * Format option value process
  214. *
  215. * @param array|string $value
  216. * @return string
  217. */
  218. protected function _formatOptionValue($value)
  219. {
  220. $order = $this->getOrder();
  221. $resultValue = '';
  222. if (is_array($value)) {
  223. if (isset($value['qty'])) {
  224. $resultValue .= $this->filterManager->sprintf($value['qty'], ['format' => '%d']) . ' x ';
  225. }
  226. $resultValue .= $value['title'];
  227. if (isset($value['price'])) {
  228. $resultValue .= " " . $order->formatPrice($value['price']);
  229. }
  230. return $resultValue;
  231. } else {
  232. return $value;
  233. }
  234. }
  235. /**
  236. * Get array of arrays with item prices information for display in PDF
  237. *
  238. * Format: array(
  239. * $index => array(
  240. * 'label' => $label,
  241. * 'price' => $price,
  242. * 'subtotal' => $subtotal
  243. * )
  244. * )
  245. *
  246. * @return array
  247. */
  248. public function getItemPricesForDisplay()
  249. {
  250. $order = $this->getOrder();
  251. $item = $this->getItem();
  252. if ($this->_taxData->displaySalesBothPrices()) {
  253. $prices = [
  254. [
  255. 'label' => __('Excl. Tax') . ':',
  256. 'price' => $order->formatPriceTxt($item->getPrice()),
  257. 'subtotal' => $order->formatPriceTxt($item->getRowTotal()),
  258. ],
  259. [
  260. 'label' => __('Incl. Tax') . ':',
  261. 'price' => $order->formatPriceTxt($item->getPriceInclTax()),
  262. 'subtotal' => $order->formatPriceTxt($item->getRowTotalInclTax())
  263. ],
  264. ];
  265. } elseif ($this->_taxData->displaySalesPriceInclTax()) {
  266. $prices = [
  267. [
  268. 'price' => $order->formatPriceTxt($item->getPriceInclTax()),
  269. 'subtotal' => $order->formatPriceTxt($item->getRowTotalInclTax()),
  270. ],
  271. ];
  272. } else {
  273. $prices = [
  274. [
  275. 'price' => $order->formatPriceTxt($item->getPrice()),
  276. 'subtotal' => $order->formatPriceTxt($item->getRowTotal()),
  277. ],
  278. ];
  279. }
  280. return $prices;
  281. }
  282. /**
  283. * Retrieve item options
  284. *
  285. * @return array
  286. */
  287. public function getItemOptions()
  288. {
  289. $result = [];
  290. $options = $this->getItem()->getOrderItem()->getProductOptions();
  291. if ($options) {
  292. if (isset($options['options'])) {
  293. $result = array_merge($result, $options['options']);
  294. }
  295. if (isset($options['additional_options'])) {
  296. $result = array_merge($result, $options['additional_options']);
  297. }
  298. if (isset($options['attributes_info'])) {
  299. $result = array_merge($result, $options['attributes_info']);
  300. }
  301. }
  302. return $result;
  303. }
  304. /**
  305. * Set font as regular
  306. *
  307. * @param int $size
  308. * @return \Zend_Pdf_Resource_Font
  309. */
  310. protected function _setFontRegular($size = 7)
  311. {
  312. $font = \Zend_Pdf_Font::fontWithPath(
  313. $this->_rootDirectory->getAbsolutePath('lib/internal/GnuFreeFont/FreeSerif.ttf')
  314. );
  315. $this->getPage()->setFont($font, $size);
  316. return $font;
  317. }
  318. /**
  319. * Set font as bold
  320. *
  321. * @param int $size
  322. * @return \Zend_Pdf_Resource_Font
  323. */
  324. protected function _setFontBold($size = 7)
  325. {
  326. $font = \Zend_Pdf_Font::fontWithPath(
  327. $this->_rootDirectory->getAbsolutePath('lib/internal/GnuFreeFont/FreeSerifBold.ttf')
  328. );
  329. $this->getPage()->setFont($font, $size);
  330. return $font;
  331. }
  332. /**
  333. * Set font as italic
  334. *
  335. * @param int $size
  336. * @return \Zend_Pdf_Resource_Font
  337. */
  338. protected function _setFontItalic($size = 7)
  339. {
  340. $font = \Zend_Pdf_Font::fontWithPath(
  341. $this->_rootDirectory->getAbsolutePath('lib/internal/GnuFreeFont/FreeSerifItalic.ttf')
  342. );
  343. $this->getPage()->setFont($font, $size);
  344. return $font;
  345. }
  346. /**
  347. * Return item Sku
  348. *
  349. * @param mixed $item
  350. * @return mixed
  351. */
  352. public function getSku($item)
  353. {
  354. if ($item->getOrderItem()->getProductOptionByCode('simple_sku')) {
  355. return $item->getOrderItem()->getProductOptionByCode('simple_sku');
  356. } else {
  357. return $item->getSku();
  358. }
  359. }
  360. }