Shipment.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 shipment pdf items renderer
  11. */
  12. class Shipment 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 $string
  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 $string,
  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 = $string;
  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. $item = $this->getItem();
  69. $pdf = $this->getPdf();
  70. $page = $this->getPage();
  71. $this->_setFontRegular();
  72. $shipItems = $this->getChildren($item);
  73. $items = array_merge([$item->getOrderItem()], $item->getOrderItem()->getChildrenItems());
  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->getParentItem()) {
  88. if ($prevOptionId != $attributes['option_id']) {
  89. $line[0] = [
  90. 'font' => 'italic',
  91. 'text' => $this->string->split($attributes['option_label'], 60, true, true),
  92. 'feed' => 60,
  93. ];
  94. $drawItems[$optionId] = ['lines' => [$line], 'height' => 15];
  95. $line = [];
  96. $prevOptionId = $attributes['option_id'];
  97. }
  98. }
  99. if ($this->isShipmentSeparately() && $childItem->getParentItem() ||
  100. !$this->isShipmentSeparately() && !$childItem->getParentItem()
  101. ) {
  102. if (isset($shipItems[$childItem->getId()])) {
  103. $qty = $shipItems[$childItem->getId()]->getQty() * 1;
  104. } elseif ($childItem->getIsVirtual()) {
  105. $qty = __('N/A');
  106. } else {
  107. $qty = 0;
  108. }
  109. } else {
  110. $qty = '';
  111. }
  112. $line[] = ['text' => $qty, 'feed' => 35];
  113. // draw Name
  114. if ($childItem->getParentItem()) {
  115. $feed = 65;
  116. $name = $this->getValueHtml($childItem);
  117. } else {
  118. $feed = 60;
  119. $name = $childItem->getName();
  120. }
  121. $text = [];
  122. foreach ($this->string->split($name, 60, true, true) as $part) {
  123. $text[] = $part;
  124. }
  125. $line[] = ['text' => $text, 'feed' => $feed];
  126. // draw SKUs
  127. $text = [];
  128. foreach ($this->string->split($childItem->getSku(), 25) as $part) {
  129. $text[] = $part;
  130. }
  131. $line[] = ['text' => $text, 'feed' => 440];
  132. $drawItems[$optionId]['lines'][] = $line;
  133. }
  134. // custom options
  135. $options = $item->getOrderItem()->getProductOptions();
  136. if ($options) {
  137. if (isset($options['options'])) {
  138. foreach ($options['options'] as $option) {
  139. $lines = [];
  140. $lines[][] = [
  141. 'text' => $this->string->split(
  142. $this->filterManager->stripTags($option['label']),
  143. 70,
  144. true,
  145. true
  146. ),
  147. 'font' => 'italic',
  148. 'feed' => 60,
  149. ];
  150. if ($option['value']) {
  151. $text = [];
  152. $printValue = isset(
  153. $option['print_value']
  154. ) ? $option['print_value'] : $this->filterManager->stripTags(
  155. $option['value']
  156. );
  157. $values = explode(', ', $printValue);
  158. foreach ($values as $value) {
  159. foreach ($this->string->split($value, 50, true, true) as $subValue) {
  160. $text[] = $subValue;
  161. }
  162. }
  163. $lines[][] = ['text' => $text, 'feed' => 65];
  164. }
  165. $drawItems[] = ['lines' => $lines, 'height' => 15];
  166. }
  167. }
  168. }
  169. $page = $pdf->drawLineBlocks($page, $drawItems, ['table_header' => true]);
  170. $this->setPage($page);
  171. }
  172. }