Creditmemo.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 creditmemo pdf default items renderer
  11. */
  12. class Creditmemo extends AbstractItems
  13. {
  14. /**
  15. * Core string
  16. *
  17. * @var \Magento\Framework\Stdlib\StringUtils
  18. */
  19. protected $string;
  20. /**
  21. * Constructor
  22. *
  23. * @param \Magento\Framework\Model\Context $context
  24. * @param \Magento\Framework\Registry $registry
  25. * @param \Magento\Tax\Helper\Data $taxData
  26. * @param \Magento\Framework\Filesystem $filesystem
  27. * @param \Magento\Framework\Filter\FilterManager $filterManager
  28. * @param \Magento\Framework\Stdlib\StringUtils $string
  29. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  30. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  31. * @param array $data
  32. * @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
  33. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  34. */
  35. public function __construct(
  36. \Magento\Framework\Model\Context $context,
  37. \Magento\Framework\Registry $registry,
  38. \Magento\Tax\Helper\Data $taxData,
  39. \Magento\Framework\Filesystem $filesystem,
  40. \Magento\Framework\Filter\FilterManager $filterManager,
  41. \Magento\Framework\Stdlib\StringUtils $string,
  42. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  43. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  44. array $data = [],
  45. Json $serializer = null
  46. ) {
  47. $this->string = $string;
  48. parent::__construct(
  49. $context,
  50. $registry,
  51. $taxData,
  52. $filesystem,
  53. $filterManager,
  54. $resource,
  55. $resourceCollection,
  56. $data,
  57. $serializer
  58. );
  59. }
  60. /**
  61. * Draw item line
  62. *
  63. * @return void
  64. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  65. * @SuppressWarnings(PHPMD.NPathComplexity)
  66. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  67. */
  68. public function draw()
  69. {
  70. $order = $this->getOrder();
  71. $item = $this->getItem();
  72. $pdf = $this->getPdf();
  73. $page = $this->getPage();
  74. $items = $this->getChildren($item);
  75. $prevOptionId = '';
  76. $drawItems = [];
  77. $leftBound = 35;
  78. $rightBound = 565;
  79. foreach ($items as $childItem) {
  80. $x = $leftBound;
  81. $line = [];
  82. $attributes = $this->getSelectionAttributes($childItem);
  83. if (is_array($attributes)) {
  84. $optionId = $attributes['option_id'];
  85. } else {
  86. $optionId = 0;
  87. }
  88. if (!isset($drawItems[$optionId])) {
  89. $drawItems[$optionId] = ['lines' => [], 'height' => 15];
  90. }
  91. // draw selection attributes
  92. if ($childItem->getOrderItem()->getParentItem()) {
  93. if ($prevOptionId != $attributes['option_id']) {
  94. $line[0] = [
  95. 'font' => 'italic',
  96. 'text' => $this->string->split($attributes['option_label'], 38, true, true),
  97. 'feed' => $x,
  98. ];
  99. $drawItems[$optionId] = ['lines' => [$line], 'height' => 15];
  100. $line = [];
  101. $prevOptionId = $attributes['option_id'];
  102. }
  103. }
  104. // draw product titles
  105. if ($childItem->getOrderItem()->getParentItem()) {
  106. $feed = $x + 5;
  107. $name = $this->getValueHtml($childItem);
  108. } else {
  109. $feed = $x;
  110. $name = $childItem->getName();
  111. }
  112. $line[] = ['text' => $this->string->split($name, 35, true, true), 'feed' => $feed];
  113. $x += 220;
  114. // draw SKUs
  115. if (!$childItem->getOrderItem()->getParentItem()) {
  116. $text = [];
  117. foreach ($this->string->split($item->getSku(), 17) as $part) {
  118. $text[] = $part;
  119. }
  120. $line[] = ['text' => $text, 'feed' => $x];
  121. }
  122. $x += 100;
  123. // draw prices
  124. if ($this->canShowPriceInfo($childItem)) {
  125. // draw Total(ex)
  126. $text = $order->formatPriceTxt($childItem->getRowTotal());
  127. $line[] = ['text' => $text, 'feed' => $x, 'font' => 'bold', 'align' => 'right', 'width' => 50];
  128. $x += 50;
  129. // draw Discount
  130. $text = $order->formatPriceTxt(-$childItem->getDiscountAmount());
  131. $line[] = ['text' => $text, 'feed' => $x, 'font' => 'bold', 'align' => 'right', 'width' => 50];
  132. $x += 50;
  133. // draw QTY
  134. $text = $childItem->getQty() * 1;
  135. $line[] = [
  136. 'text' => $childItem->getQty() * 1,
  137. 'feed' => $x,
  138. 'font' => 'bold',
  139. 'align' => 'center',
  140. 'width' => 30,
  141. ];
  142. $x += 30;
  143. // draw Tax
  144. $text = $order->formatPriceTxt($childItem->getTaxAmount());
  145. $line[] = ['text' => $text, 'feed' => $x, 'font' => 'bold', 'align' => 'right', 'width' => 45];
  146. $x += 45;
  147. // draw Total(inc)
  148. $text = $order->formatPriceTxt(
  149. $childItem->getRowTotal() + $childItem->getTaxAmount() - $childItem->getDiscountAmount()
  150. );
  151. $line[] = ['text' => $text, 'feed' => $rightBound, 'font' => 'bold', 'align' => 'right'];
  152. }
  153. $drawItems[$optionId]['lines'][] = $line;
  154. }
  155. // custom options
  156. $options = $item->getOrderItem()->getProductOptions();
  157. if ($options) {
  158. if (isset($options['options'])) {
  159. foreach ($options['options'] as $option) {
  160. $lines = [];
  161. $lines[][] = [
  162. 'text' => $this->string->split(
  163. $this->filterManager->stripTags($option['label']),
  164. 40,
  165. true,
  166. true
  167. ),
  168. 'font' => 'italic',
  169. 'feed' => $leftBound,
  170. ];
  171. if ($option['value']) {
  172. $text = [];
  173. $printValue = isset(
  174. $option['print_value']
  175. ) ? $option['print_value'] : $this->filterManager->stripTags(
  176. $option['value']
  177. );
  178. $values = explode(', ', $printValue);
  179. foreach ($values as $value) {
  180. foreach ($this->string->split($value, 30, true, true) as $subValue) {
  181. $text[] = $subValue;
  182. }
  183. }
  184. $lines[][] = ['text' => $text, 'feed' => $leftBound + 5];
  185. }
  186. $drawItems[] = ['lines' => $lines, 'height' => 15];
  187. }
  188. }
  189. }
  190. $page = $pdf->drawLineBlocks($page, $drawItems, ['table_header' => true]);
  191. $this->setPage($page);
  192. }
  193. }