TaxAdjustment.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Weee\Pricing;
  7. use Magento\Catalog\Pricing\Price\CustomOptionPriceInterface;
  8. use Magento\Framework\Pricing\Adjustment\AdjustmentInterface;
  9. use Magento\Framework\Pricing\SaleableInterface;
  10. use Magento\Framework\Pricing\PriceCurrencyInterface;
  11. use Magento\Weee\Helper\Data as WeeeHelper;
  12. use Magento\Tax\Helper\Data as TaxHelper;
  13. /**
  14. * Weee tax pricing adjustment
  15. */
  16. class TaxAdjustment implements AdjustmentInterface
  17. {
  18. /**
  19. * Adjustment code weee
  20. */
  21. const ADJUSTMENT_CODE = 'weee_tax';
  22. /**
  23. * Weee helper
  24. *
  25. * @var WeeeHelper
  26. */
  27. protected $weeeHelper;
  28. /**
  29. * @var TaxHelper
  30. */
  31. protected $taxHelper;
  32. /**
  33. * Sort order
  34. *
  35. * @var int|null
  36. */
  37. protected $sortOrder;
  38. /**
  39. * @var PriceCurrencyInterface
  40. */
  41. protected $priceCurrency;
  42. /**
  43. * Constructor
  44. *
  45. * @param WeeeHelper $weeeHelper
  46. * @param TaxHelper $taxHelper
  47. * @param PriceCurrencyInterface $priceCurrency
  48. * @param int $sortOrder
  49. */
  50. public function __construct(
  51. WeeeHelper $weeeHelper,
  52. TaxHelper $taxHelper,
  53. PriceCurrencyInterface $priceCurrency,
  54. $sortOrder = null
  55. ) {
  56. $this->weeeHelper = $weeeHelper;
  57. $this->taxHelper = $taxHelper;
  58. $this->priceCurrency = $priceCurrency;
  59. $this->sortOrder = $sortOrder;
  60. }
  61. /**
  62. * Get adjustment code
  63. *
  64. * @return string
  65. */
  66. public function getAdjustmentCode()
  67. {
  68. return self::ADJUSTMENT_CODE;
  69. }
  70. /**
  71. * Define if adjustment is included in base price
  72. * (FPT is excluded from base price)
  73. *
  74. * @return bool
  75. */
  76. public function isIncludedInBasePrice()
  77. {
  78. return false;
  79. }
  80. /**
  81. * Define if adjustment is included in display price
  82. *
  83. * @return bool
  84. */
  85. public function isIncludedInDisplayPrice()
  86. {
  87. if ($this->taxHelper->displayPriceExcludingTax()) {
  88. return false;
  89. }
  90. if ($this->weeeHelper->isEnabled() == true &&
  91. $this->weeeHelper->isTaxable() == true &&
  92. $this->weeeHelper->typeOfDisplay([\Magento\Weee\Model\Tax::DISPLAY_EXCL]) == false) {
  93. return true;
  94. } else {
  95. return false;
  96. }
  97. }
  98. /**
  99. * Extract adjustment amount from the given amount value
  100. *
  101. * @param float $amount
  102. * @param SaleableInterface $saleableItem
  103. * @param null|array $context
  104. * @return float
  105. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  106. */
  107. public function extractAdjustment($amount, SaleableInterface $saleableItem, $context = [])
  108. {
  109. return 0;
  110. }
  111. /**
  112. * Apply adjustment amount and return result value
  113. *
  114. * @param float $amount
  115. * @param SaleableInterface $saleableItem
  116. * @param null|array $context
  117. * @return float
  118. */
  119. public function applyAdjustment($amount, SaleableInterface $saleableItem, $context = [])
  120. {
  121. if (isset($context[CustomOptionPriceInterface::CONFIGURATION_OPTION_FLAG])) {
  122. return $amount;
  123. }
  124. return $amount + $this->getAmount($saleableItem);
  125. }
  126. /**
  127. * Check if adjustment should be excluded from calculations along with the given adjustment
  128. *
  129. * @param string $adjustmentCode
  130. * @return bool
  131. */
  132. public function isExcludedWith($adjustmentCode)
  133. {
  134. return (($adjustmentCode == self::ADJUSTMENT_CODE) ||
  135. ($adjustmentCode == \Magento\Tax\Pricing\Adjustment::ADJUSTMENT_CODE));
  136. }
  137. /**
  138. * Obtain amount
  139. *
  140. * @param SaleableInterface $saleableItem
  141. * @return float
  142. */
  143. protected function getAmount(SaleableInterface $saleableItem)
  144. {
  145. $weeeTaxAmount = 0;
  146. $attributes = $this->weeeHelper->getProductWeeeAttributes($saleableItem, null, null, null, true, false);
  147. if ($attributes != null) {
  148. foreach ($attributes as $attribute) {
  149. $weeeTaxAmount += $attribute->getData('tax_amount');
  150. }
  151. }
  152. $weeeTaxAmount = $this->priceCurrency->convert($weeeTaxAmount);
  153. return $weeeTaxAmount;
  154. }
  155. /**
  156. * Return sort order position
  157. *
  158. * @return int
  159. */
  160. public function getSortOrder()
  161. {
  162. return $this->sortOrder;
  163. }
  164. }