DefaultTotal.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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\Total;
  7. /**
  8. * Sales Order Total PDF model
  9. *
  10. * @method \Magento\Sales\Model\Order getOrder()
  11. */
  12. class DefaultTotal extends \Magento\Framework\DataObject
  13. {
  14. /**
  15. * @var \Magento\Tax\Helper\Data
  16. */
  17. protected $_taxHelper;
  18. /**
  19. * @var \Magento\Tax\Model\Calculation
  20. */
  21. protected $_taxCalculation;
  22. /**
  23. * @var \Magento\Tax\Model\ResourceModel\Sales\Order\Tax\CollectionFactory
  24. */
  25. protected $_taxOrdersFactory;
  26. /**
  27. * Initialize dependencies
  28. *
  29. * @param \Magento\Tax\Helper\Data $taxHelper
  30. * @param \Magento\Tax\Model\Calculation $taxCalculation
  31. * @param \Magento\Tax\Model\ResourceModel\Sales\Order\Tax\CollectionFactory $ordersFactory
  32. * @param array $data
  33. */
  34. public function __construct(
  35. \Magento\Tax\Helper\Data $taxHelper,
  36. \Magento\Tax\Model\Calculation $taxCalculation,
  37. \Magento\Tax\Model\ResourceModel\Sales\Order\Tax\CollectionFactory $ordersFactory,
  38. array $data = []
  39. ) {
  40. $this->_taxHelper = $taxHelper;
  41. $this->_taxCalculation = $taxCalculation;
  42. $this->_taxOrdersFactory = $ordersFactory;
  43. parent::__construct($data);
  44. }
  45. /**
  46. * Get array of arrays with totals information for display in PDF
  47. * array(
  48. * $index => array(
  49. * 'amount' => $amount,
  50. * 'label' => $label,
  51. * 'font_size'=> $font_size
  52. * )
  53. * )
  54. *
  55. * @return array
  56. */
  57. public function getTotalsForDisplay()
  58. {
  59. $amount = $this->getOrder()->formatPriceTxt($this->getAmount());
  60. if ($this->getAmountPrefix()) {
  61. $amount = $this->getAmountPrefix() . $amount;
  62. }
  63. $title = __($this->getTitle());
  64. if ($this->getTitleSourceField()) {
  65. $label = $title . ' (' . $this->getTitleDescription() . '):';
  66. } else {
  67. $label = $title . ':';
  68. }
  69. $fontSize = $this->getFontSize() ? $this->getFontSize() : 7;
  70. $total = ['amount' => $amount, 'label' => $label, 'font_size' => $fontSize];
  71. return [$total];
  72. }
  73. /**
  74. * Get array of arrays with tax information for display in PDF
  75. * array(
  76. * $index => array(
  77. * 'amount' => $amount,
  78. * 'label' => $label,
  79. * 'font_size'=> $font_size
  80. * )
  81. * )
  82. *
  83. * @return array
  84. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  85. */
  86. public function getFullTaxInfo()
  87. {
  88. $fontSize = $this->getFontSize() ? $this->getFontSize() : 7;
  89. $taxClassAmount = $this->_taxHelper->getCalculatedTaxes($this->getSource());
  90. if (!empty($taxClassAmount)) {
  91. foreach ($taxClassAmount as &$tax) {
  92. $percent = $tax['percent'] ? ' (' . $tax['percent'] . '%)' : '';
  93. $tax['amount'] = $this->getAmountPrefix() . $this->getOrder()->formatPriceTxt($tax['tax_amount']);
  94. $tax['label'] = __($tax['title']) . $percent . ':';
  95. $tax['font_size'] = $fontSize;
  96. }
  97. } else {
  98. /** @var $orders \Magento\Tax\Model\ResourceModel\Sales\Order\Tax\Collection */
  99. $orders = $this->_taxOrdersFactory->create();
  100. $rates = $orders->loadByOrder($this->getOrder())->toArray();
  101. $fullInfo = $this->_taxCalculation->reproduceProcess($rates['items']);
  102. $tax_info = [];
  103. if ($fullInfo) {
  104. foreach ($fullInfo as $info) {
  105. if (isset($info['hidden']) && $info['hidden']) {
  106. continue;
  107. }
  108. $_amount = $info['amount'];
  109. foreach ($info['rates'] as $rate) {
  110. $percent = $rate['percent'] ? ' (' . $rate['percent'] . '%)' : '';
  111. $tax_info[] = [
  112. 'amount' => $this->getAmountPrefix() . $this->getOrder()->formatPriceTxt($_amount),
  113. 'label' => __($rate['title']) . $percent . ':',
  114. 'font_size' => $fontSize,
  115. ];
  116. }
  117. }
  118. }
  119. $taxClassAmount = $tax_info;
  120. }
  121. return $taxClassAmount;
  122. }
  123. /**
  124. * Check if we can display total information in PDF
  125. *
  126. * @return bool
  127. */
  128. public function canDisplay()
  129. {
  130. $amount = $this->getAmount();
  131. return $this->getDisplayZero() === 'true' || $amount != 0;
  132. }
  133. /**
  134. * Get Total amount from source
  135. *
  136. * @return float
  137. */
  138. public function getAmount()
  139. {
  140. return $this->getSource()->getDataUsingMethod($this->getSourceField());
  141. }
  142. /**
  143. * Get title description from source
  144. *
  145. * @return mixed
  146. */
  147. public function getTitleDescription()
  148. {
  149. return $this->getSource()->getOrder()->getData($this->getTitleSourceField());
  150. }
  151. }