Adjustment.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Tax\Pricing;
  7. use Magento\Framework\Pricing\Adjustment\AdjustmentInterface;
  8. use Magento\Framework\Pricing\SaleableInterface;
  9. use Magento\Tax\Helper\Data as TaxHelper;
  10. /**
  11. * Tax pricing adjustment model
  12. */
  13. class Adjustment implements AdjustmentInterface
  14. {
  15. /**
  16. * Adjustment code tax
  17. */
  18. const ADJUSTMENT_CODE = 'tax';
  19. /**
  20. * @var TaxHelper
  21. */
  22. protected $taxHelper;
  23. /**
  24. * \Magento\Catalog\Helper\Data
  25. *
  26. * @var CatalogHelper
  27. */
  28. protected $catalogHelper;
  29. /**
  30. * @var int|null
  31. */
  32. protected $sortOrder;
  33. /**
  34. * @param TaxHelper $taxHelper
  35. * @param \Magento\Catalog\Helper\Data $catalogHelper
  36. * @param int|null $sortOrder
  37. */
  38. public function __construct(TaxHelper $taxHelper, \Magento\Catalog\Helper\Data $catalogHelper, $sortOrder = null)
  39. {
  40. $this->taxHelper = $taxHelper;
  41. $this->catalogHelper = $catalogHelper;
  42. $this->sortOrder = $sortOrder;
  43. }
  44. /**
  45. * Get adjustment code
  46. *
  47. * @return string
  48. */
  49. public function getAdjustmentCode()
  50. {
  51. return self::ADJUSTMENT_CODE;
  52. }
  53. /**
  54. * Define if adjustment is included in base price
  55. *
  56. * @return bool
  57. */
  58. public function isIncludedInBasePrice()
  59. {
  60. return $this->taxHelper->priceIncludesTax();
  61. }
  62. /**
  63. * Define if adjustment is included in display price
  64. *
  65. * @return bool
  66. */
  67. public function isIncludedInDisplayPrice()
  68. {
  69. return $this->taxHelper->displayPriceIncludingTax() || $this->taxHelper->displayBothPrices();
  70. }
  71. /**
  72. * Extract adjustment amount from the given amount value
  73. *
  74. * @param float $amount
  75. * @param SaleableInterface $saleableItem
  76. * @param null|array $context
  77. * @return float
  78. */
  79. public function extractAdjustment($amount, SaleableInterface $saleableItem, $context = [])
  80. {
  81. if ($this->taxHelper->priceIncludesTax()) {
  82. $adjustedAmount = $this->catalogHelper->getTaxPrice(
  83. $saleableItem,
  84. $amount,
  85. false,
  86. null,
  87. null,
  88. null,
  89. null,
  90. null,
  91. false
  92. );
  93. $result = $amount - $adjustedAmount;
  94. } else {
  95. $result = 0.;
  96. }
  97. return $result;
  98. }
  99. /**
  100. * Apply adjustment amount and return result value
  101. *
  102. * @param float $amount
  103. * @param SaleableInterface $saleableItem
  104. * @param null|array $context
  105. * @return float
  106. */
  107. public function applyAdjustment($amount, SaleableInterface $saleableItem, $context = [])
  108. {
  109. return $this->catalogHelper->getTaxPrice(
  110. $saleableItem,
  111. $amount,
  112. true,
  113. null,
  114. null,
  115. null,
  116. null,
  117. null,
  118. false
  119. );
  120. }
  121. /**
  122. * Check if adjustment should be excluded from calculations along with the given adjustment
  123. *
  124. * @param string $adjustmentCode
  125. * @return bool
  126. */
  127. public function isExcludedWith($adjustmentCode)
  128. {
  129. return $this->getAdjustmentCode() === $adjustmentCode;
  130. }
  131. /**
  132. * Return sort order position
  133. *
  134. * @return int
  135. */
  136. public function getSortOrder()
  137. {
  138. return $this->sortOrder;
  139. }
  140. }