Invoice.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Bundle\Model\Sales\Order\Pdf\Items;
  7. use Magento\Framework\App\ObjectManager;
  8. use Magento\Framework\Serialize\Serializer\Json;
  9. /**
  10. * Order invoice pdf default items renderer
  11. */
  12. class Invoice extends AbstractItems
  13. {
  14. /**
  15. * @var \Magento\Framework\Stdlib\StringUtils
  16. */
  17. protected $string;
  18. /**
  19. * Constructor
  20. *
  21. * @param \Magento\Framework\Model\Context $context
  22. * @param \Magento\Framework\Registry $registry
  23. * @param \Magento\Tax\Helper\Data $taxData
  24. * @param \Magento\Framework\Filesystem $filesystem
  25. * @param \Magento\Framework\Filter\FilterManager $filterManager
  26. * @param \Magento\Framework\Stdlib\StringUtils $coreString
  27. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  28. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  29. * @param array $data
  30. * @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
  31. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  32. */
  33. public function __construct(
  34. \Magento\Framework\Model\Context $context,
  35. \Magento\Framework\Registry $registry,
  36. \Magento\Tax\Helper\Data $taxData,
  37. \Magento\Framework\Filesystem $filesystem,
  38. \Magento\Framework\Filter\FilterManager $filterManager,
  39. \Magento\Framework\Stdlib\StringUtils $coreString,
  40. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  41. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  42. array $data = [],
  43. Json $serializer = null
  44. ) {
  45. $this->string = $coreString;
  46. parent::__construct(
  47. $context,
  48. $registry,
  49. $taxData,
  50. $filesystem,
  51. $filterManager,
  52. $resource,
  53. $resourceCollection,
  54. $data,
  55. $serializer
  56. );
  57. }
  58. /**
  59. * Draw item line
  60. *
  61. * @return void
  62. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  63. * @SuppressWarnings(PHPMD.NPathComplexity)
  64. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  65. */
  66. public function draw()
  67. {
  68. $order = $this->getOrder();
  69. $item = $this->getItem();
  70. $pdf = $this->getPdf();
  71. $page = $this->getPage();
  72. $this->_setFontRegular();
  73. $items = $this->getChildren($item);
  74. $prevOptionId = '';
  75. $drawItems = [];
  76. foreach ($items as $childItem) {
  77. $line = [];
  78. $attributes = $this->getSelectionAttributes($childItem);
  79. if (is_array($attributes)) {
  80. $optionId = $attributes['option_id'];
  81. } else {
  82. $optionId = 0;
  83. }
  84. if (!isset($drawItems[$optionId])) {
  85. $drawItems[$optionId] = ['lines' => [], 'height' => 15];
  86. }
  87. if ($childItem->getOrderItem()->getParentItem()) {
  88. if ($prevOptionId != $attributes['option_id']) {
  89. $line[0] = [
  90. 'font' => 'italic',
  91. 'text' => $this->string->split($attributes['option_label'], 45, true, true),
  92. 'feed' => 35,
  93. ];
  94. $drawItems[$optionId] = ['lines' => [$line], 'height' => 15];
  95. $line = [];
  96. $prevOptionId = $attributes['option_id'];
  97. }
  98. }
  99. /* in case Product name is longer than 80 chars - it is written in a few lines */
  100. if ($childItem->getOrderItem()->getParentItem()) {
  101. $feed = 40;
  102. $name = $this->getValueHtml($childItem);
  103. } else {
  104. $feed = 35;
  105. $name = $childItem->getName();
  106. }
  107. $line[] = ['text' => $this->string->split($name, 35, true, true), 'feed' => $feed];
  108. // draw SKUs
  109. if (!$childItem->getOrderItem()->getParentItem()) {
  110. $text = [];
  111. foreach ($this->string->split($item->getSku(), 17) as $part) {
  112. $text[] = $part;
  113. }
  114. $line[] = ['text' => $text, 'feed' => 255];
  115. }
  116. // draw prices
  117. if ($this->canShowPriceInfo($childItem)) {
  118. $price = $order->formatPriceTxt($childItem->getPrice());
  119. $line[] = ['text' => $price, 'feed' => 395, 'font' => 'bold', 'align' => 'right'];
  120. $line[] = ['text' => $childItem->getQty() * 1, 'feed' => 435, 'font' => 'bold'];
  121. $tax = $order->formatPriceTxt($childItem->getTaxAmount());
  122. $line[] = ['text' => $tax, 'feed' => 495, 'font' => 'bold', 'align' => 'right'];
  123. $row_total = $order->formatPriceTxt($childItem->getRowTotal());
  124. $line[] = ['text' => $row_total, 'feed' => 565, 'font' => 'bold', 'align' => 'right'];
  125. }
  126. $drawItems[$optionId]['lines'][] = $line;
  127. }
  128. // custom options
  129. $options = $item->getOrderItem()->getProductOptions();
  130. if ($options) {
  131. if (isset($options['options'])) {
  132. foreach ($options['options'] as $option) {
  133. $lines = [];
  134. $lines[][] = [
  135. 'text' => $this->string->split(
  136. $this->filterManager->stripTags($option['label']),
  137. 40,
  138. true,
  139. true
  140. ),
  141. 'font' => 'italic',
  142. 'feed' => 35,
  143. ];
  144. if ($option['value']) {
  145. $text = [];
  146. $printValue = isset(
  147. $option['print_value']
  148. ) ? $option['print_value'] : $this->filterManager->stripTags(
  149. $option['value']
  150. );
  151. $values = explode(', ', $printValue);
  152. foreach ($values as $value) {
  153. foreach ($this->string->split($value, 30, true, true) as $subValue) {
  154. $text[] = $subValue;
  155. }
  156. }
  157. $lines[][] = ['text' => $text, 'feed' => 40];
  158. }
  159. $drawItems[] = ['lines' => $lines, 'height' => 15];
  160. }
  161. }
  162. }
  163. $page = $pdf->drawLineBlocks($page, $drawItems, ['table_header' => true]);
  164. $this->setPage($page);
  165. }
  166. }