Adjustment.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Tax\Pricing\Render;
  7. use Magento\Framework\Pricing\PriceCurrencyInterface;
  8. use Magento\Framework\Pricing\Render\AbstractAdjustment;
  9. use Magento\Framework\View\Element\Template;
  10. /**
  11. * @method string getIdSuffix()
  12. * @method string getDisplayLabel()
  13. */
  14. class Adjustment extends AbstractAdjustment
  15. {
  16. /**
  17. * @var \Magento\Tax\Helper\Data
  18. */
  19. protected $taxHelper;
  20. /**
  21. * @param Template\Context $context
  22. * @param PriceCurrencyInterface $priceCurrency
  23. * @param \Magento\Tax\Helper\Data $helper
  24. * @param array $data
  25. */
  26. public function __construct(
  27. Template\Context $context,
  28. PriceCurrencyInterface $priceCurrency,
  29. \Magento\Tax\Helper\Data $helper,
  30. array $data = []
  31. ) {
  32. $this->taxHelper = $helper;
  33. parent::__construct($context, $priceCurrency, $data);
  34. }
  35. /**
  36. * @return string
  37. */
  38. protected function apply()
  39. {
  40. if ($this->displayBothPrices()) {
  41. if ($this->getZone() !== \Magento\Framework\Pricing\Render::ZONE_ITEM_OPTION) {
  42. $this->amountRender->setPriceDisplayLabel(__('Incl. Tax'));
  43. }
  44. $this->amountRender->setPriceWrapperCss('price-including-tax');
  45. $this->amountRender->setPriceId(
  46. $this->buildIdWithPrefix('price-including-tax-')
  47. );
  48. } elseif ($this->displayPriceExcludingTax()) {
  49. $this->amountRender->setDisplayValue(
  50. $this->amountRender->getAmount()->getValue($this->getAdjustmentCode())
  51. );
  52. if ($this->taxHelper->priceIncludesTax() && $this->amountRender->getPriceType() == 'finalPrice') {
  53. // for dynamic calculations of prices with any options, use the base price amount
  54. $this->amountRender->setPriceType('basePrice');
  55. }
  56. }
  57. return $this->toHtml();
  58. }
  59. /**
  60. * Obtain code of adjustment type
  61. *
  62. * @return string
  63. */
  64. public function getAdjustmentCode()
  65. {
  66. return \Magento\Tax\Pricing\Adjustment::ADJUSTMENT_CODE;
  67. }
  68. /**
  69. * Define if both prices should be displayed
  70. *
  71. * @return bool
  72. */
  73. public function displayBothPrices()
  74. {
  75. return $this->taxHelper->displayBothPrices();
  76. }
  77. /**
  78. * Obtain display amount excluding tax
  79. *
  80. * @param array $exclude
  81. * @param bool $includeContainer
  82. * @return string
  83. */
  84. public function getDisplayAmountExclTax($exclude = null, $includeContainer = false)
  85. {
  86. //If exclude is not supplied, use the default
  87. if ($exclude === null) {
  88. $exclude = $this->getDefaultExclusions();
  89. }
  90. return $this->formatCurrency(
  91. $this->getRawAmount($exclude),
  92. $includeContainer
  93. );
  94. }
  95. /**
  96. * Obtain raw amount value (without formatting)
  97. *
  98. * @param array $exclude
  99. * @return float
  100. */
  101. public function getRawAmount($exclude = null)
  102. {
  103. //If exclude is not supplied, use the default
  104. if ($exclude === null) {
  105. $exclude = $this->getDefaultExclusions();
  106. }
  107. return $this->amountRender->getAmount()->getValue($exclude);
  108. }
  109. /**
  110. * Returns the list of default exclusions
  111. *
  112. * @return array
  113. */
  114. public function getDefaultExclusions()
  115. {
  116. return [$this->getAdjustmentCode()];
  117. }
  118. /**
  119. * Obtain display amount
  120. *
  121. * @param bool $includeContainer
  122. * @return string
  123. */
  124. public function getDisplayAmount($includeContainer = true)
  125. {
  126. return $this->formatCurrency($this->getRawAmount([]), $includeContainer);
  127. }
  128. /**
  129. * Build identifier with prefix
  130. *
  131. * @param string $prefix
  132. * @return string
  133. */
  134. public function buildIdWithPrefix($prefix)
  135. {
  136. $priceId = $this->getPriceId();
  137. if (!$priceId) {
  138. $priceId = $this->getSaleableItem()->getId();
  139. }
  140. return $prefix . $priceId . $this->getIdSuffix();
  141. }
  142. /**
  143. * Should be displayed price including tax
  144. *
  145. * @return bool
  146. */
  147. public function displayPriceIncludingTax()
  148. {
  149. return $this->taxHelper->displayPriceIncludingTax();
  150. }
  151. /**
  152. * Should be displayed price excluding tax
  153. *
  154. * @return bool
  155. */
  156. public function displayPriceExcludingTax()
  157. {
  158. return $this->taxHelper->displayPriceExcludingTax();
  159. }
  160. }