Adjustment.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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\PriceCurrencyInterface;
  10. use Magento\Framework\Pricing\SaleableInterface;
  11. use Magento\Weee\Helper\Data as WeeeHelper;
  12. /**
  13. * Weee pricing adjustment
  14. */
  15. class Adjustment implements AdjustmentInterface
  16. {
  17. /**
  18. * Adjustment code weee
  19. */
  20. const ADJUSTMENT_CODE = 'weee';
  21. /**
  22. * Weee helper
  23. *
  24. * @var WeeeHelper
  25. */
  26. protected $weeeHelper;
  27. /**
  28. * Sort order
  29. *
  30. * @var int|null
  31. */
  32. protected $sortOrder;
  33. /**
  34. * @var PriceCurrencyInterface
  35. */
  36. protected $priceCurrency;
  37. /**
  38. * Constructor
  39. *
  40. * @param WeeeHelper $weeeHelper
  41. * @param PriceCurrencyInterface $priceCurrency
  42. * @param int $sortOrder
  43. */
  44. public function __construct(WeeeHelper $weeeHelper, PriceCurrencyInterface $priceCurrency, $sortOrder = null)
  45. {
  46. $this->weeeHelper = $weeeHelper;
  47. $this->priceCurrency = $priceCurrency;
  48. $this->sortOrder = $sortOrder;
  49. }
  50. /**
  51. * Get adjustment code
  52. *
  53. * @return string
  54. */
  55. public function getAdjustmentCode()
  56. {
  57. return self::ADJUSTMENT_CODE;
  58. }
  59. /**
  60. * Define if adjustment is included in base price
  61. * (FPT is excluded from base price)
  62. *
  63. * @return bool
  64. */
  65. public function isIncludedInBasePrice()
  66. {
  67. return false;
  68. }
  69. /**
  70. * Define if adjustment is included in display price
  71. *
  72. * @return bool
  73. */
  74. public function isIncludedInDisplayPrice()
  75. {
  76. return $this->weeeHelper->typeOfDisplay(
  77. [
  78. \Magento\Weee\Model\Tax::DISPLAY_INCL,
  79. \Magento\Weee\Model\Tax::DISPLAY_INCL_DESCR,
  80. \Magento\Weee\Model\Tax::DISPLAY_EXCL_DESCR_INCL,
  81. ]
  82. );
  83. }
  84. /**
  85. * Extract adjustment amount from the given amount value
  86. *
  87. * @param float $amount
  88. * @param SaleableInterface $saleableItem
  89. * @param null|array $context
  90. * @return float
  91. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  92. */
  93. public function extractAdjustment($amount, SaleableInterface $saleableItem, $context = [])
  94. {
  95. return 0;
  96. }
  97. /**
  98. * Apply adjustment amount and return result value
  99. *
  100. * @param float $amount
  101. * @param SaleableInterface $saleableItem
  102. * @param null|array $context
  103. * @return float
  104. */
  105. public function applyAdjustment($amount, SaleableInterface $saleableItem, $context = [])
  106. {
  107. if (isset($context[CustomOptionPriceInterface::CONFIGURATION_OPTION_FLAG])) {
  108. return $amount;
  109. }
  110. return $amount + $this->getAmount($saleableItem);
  111. }
  112. /**
  113. * Check if adjustment should be excluded from calculations along with the given adjustment
  114. *
  115. * @param string $adjustmentCode
  116. * @return bool
  117. */
  118. public function isExcludedWith($adjustmentCode)
  119. {
  120. return (($adjustmentCode == self::ADJUSTMENT_CODE) ||
  121. ($adjustmentCode == \Magento\Tax\Pricing\Adjustment::ADJUSTMENT_CODE));
  122. }
  123. /**
  124. * Obtain amount
  125. *
  126. * @param SaleableInterface $saleableItem
  127. * @return float
  128. */
  129. protected function getAmount(SaleableInterface $saleableItem)
  130. {
  131. $weeeAmount = $this->weeeHelper->getAmountExclTax($saleableItem);
  132. $weeeAmount = $this->priceCurrency->convert($weeeAmount);
  133. return $weeeAmount;
  134. }
  135. /**
  136. * Return sort order position
  137. *
  138. * @return int
  139. */
  140. public function getSortOrder()
  141. {
  142. return $this->sortOrder;
  143. }
  144. }