DefaultInvoice.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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\Invoice;
  7. /**
  8. * Sales Order Invoice Pdf default items renderer
  9. */
  10. class DefaultInvoice extends \Magento\Sales\Model\Order\Pdf\Items\AbstractItems
  11. {
  12. /**
  13. * Core string
  14. *
  15. * @var \Magento\Framework\Stdlib\StringUtils
  16. */
  17. protected $string;
  18. /**
  19. * @param \Magento\Framework\Model\Context $context
  20. * @param \Magento\Framework\Registry $registry
  21. * @param \Magento\Tax\Helper\Data $taxData
  22. * @param \Magento\Framework\Filesystem $filesystem
  23. * @param \Magento\Framework\Filter\FilterManager $filterManager
  24. * @param \Magento\Framework\Stdlib\StringUtils $string
  25. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  26. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  27. * @param array $data
  28. */
  29. public function __construct(
  30. \Magento\Framework\Model\Context $context,
  31. \Magento\Framework\Registry $registry,
  32. \Magento\Tax\Helper\Data $taxData,
  33. \Magento\Framework\Filesystem $filesystem,
  34. \Magento\Framework\Filter\FilterManager $filterManager,
  35. \Magento\Framework\Stdlib\StringUtils $string,
  36. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  37. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  38. array $data = []
  39. ) {
  40. $this->string = $string;
  41. parent::__construct(
  42. $context,
  43. $registry,
  44. $taxData,
  45. $filesystem,
  46. $filterManager,
  47. $resource,
  48. $resourceCollection,
  49. $data
  50. );
  51. }
  52. /**
  53. * Draw item line
  54. *
  55. * @return void
  56. */
  57. public function draw()
  58. {
  59. $order = $this->getOrder();
  60. $item = $this->getItem();
  61. $pdf = $this->getPdf();
  62. $page = $this->getPage();
  63. $lines = [];
  64. // draw Product name
  65. $lines[0] = [['text' => $this->string->split($item->getName(), 35, true, true), 'feed' => 35]];
  66. // draw SKU
  67. $lines[0][] = [
  68. 'text' => $this->string->split($this->getSku($item), 17),
  69. 'feed' => 290,
  70. 'align' => 'right',
  71. ];
  72. // draw QTY
  73. $lines[0][] = ['text' => $item->getQty() * 1, 'feed' => 435, 'align' => 'right'];
  74. // draw item Prices
  75. $i = 0;
  76. $prices = $this->getItemPricesForDisplay();
  77. $feedPrice = 395;
  78. $feedSubtotal = $feedPrice + 170;
  79. foreach ($prices as $priceData) {
  80. if (isset($priceData['label'])) {
  81. // draw Price label
  82. $lines[$i][] = ['text' => $priceData['label'], 'feed' => $feedPrice, 'align' => 'right'];
  83. // draw Subtotal label
  84. $lines[$i][] = ['text' => $priceData['label'], 'feed' => $feedSubtotal, 'align' => 'right'];
  85. $i++;
  86. }
  87. // draw Price
  88. $lines[$i][] = [
  89. 'text' => $priceData['price'],
  90. 'feed' => $feedPrice,
  91. 'font' => 'bold',
  92. 'align' => 'right',
  93. ];
  94. // draw Subtotal
  95. $lines[$i][] = [
  96. 'text' => $priceData['subtotal'],
  97. 'feed' => $feedSubtotal,
  98. 'font' => 'bold',
  99. 'align' => 'right',
  100. ];
  101. $i++;
  102. }
  103. // draw Tax
  104. $lines[0][] = [
  105. 'text' => $order->formatPriceTxt($item->getTaxAmount()),
  106. 'feed' => 495,
  107. 'font' => 'bold',
  108. 'align' => 'right',
  109. ];
  110. // custom options
  111. $options = $this->getItemOptions();
  112. if ($options) {
  113. foreach ($options as $option) {
  114. // draw options label
  115. $lines[][] = [
  116. 'text' => $this->string->split($this->filterManager->stripTags($option['label']), 40, true, true),
  117. 'font' => 'italic',
  118. 'feed' => 35,
  119. ];
  120. // Checking whether option value is not null
  121. if ($option['value'] !== null) {
  122. if (isset($option['print_value'])) {
  123. $printValue = $option['print_value'];
  124. } else {
  125. $printValue = $this->filterManager->stripTags($option['value']);
  126. }
  127. $values = explode(', ', $printValue);
  128. foreach ($values as $value) {
  129. $lines[][] = ['text' => $this->string->split($value, 30, true, true), 'feed' => 40];
  130. }
  131. }
  132. }
  133. }
  134. $lineBlock = ['lines' => $lines, 'height' => 20];
  135. $page = $pdf->drawLineBlocks($page, [$lineBlock], ['table_header' => true]);
  136. $this->setPage($page);
  137. }
  138. }