TaxConfigProvider.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Tax\Model;
  7. use Magento\Checkout\Model\ConfigProviderInterface;
  8. use Magento\Tax\Helper\Data as TaxHelper;
  9. use Magento\Framework\App\Config\ScopeConfigInterface;
  10. use Magento\Checkout\Model\Session as CheckoutSession;
  11. class TaxConfigProvider implements ConfigProviderInterface
  12. {
  13. /**
  14. * @var TaxHelper
  15. */
  16. protected $taxHelper;
  17. /**
  18. * @var Config
  19. */
  20. protected $taxConfig;
  21. /**
  22. * @var ScopeConfigInterface
  23. */
  24. protected $scopeConfig;
  25. /**
  26. * @var CheckoutSession
  27. */
  28. protected $checkoutSession;
  29. /**
  30. * @param TaxHelper $taxHelper
  31. * @param Config $taxConfig
  32. * @param CheckoutSession $checkoutSession
  33. * @param ScopeConfigInterface $scopeConfig
  34. */
  35. public function __construct(
  36. TaxHelper $taxHelper,
  37. Config $taxConfig,
  38. CheckoutSession $checkoutSession,
  39. ScopeConfigInterface $scopeConfig
  40. ) {
  41. $this->taxHelper = $taxHelper;
  42. $this->taxConfig = $taxConfig;
  43. $this->checkoutSession = $checkoutSession;
  44. $this->scopeConfig = $scopeConfig;
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function getConfig()
  50. {
  51. $defaultRegionId = $this->scopeConfig->getValue(
  52. \Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_REGION,
  53. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  54. );
  55. // prevent wrong assignment on shipping rate estimation requests
  56. if (0 == $defaultRegionId) {
  57. $defaultRegionId = null;
  58. }
  59. return [
  60. 'isDisplayShippingPriceExclTax' => $this->isDisplayShippingPriceExclTax(),
  61. 'isDisplayShippingBothPrices' => $this->isDisplayShippingBothPrices(),
  62. 'reviewShippingDisplayMode' => $this->getDisplayShippingMode(),
  63. 'reviewItemPriceDisplayMode' => $this->getReviewItemPriceDisplayMode(),
  64. 'reviewTotalsDisplayMode' => $this->getReviewTotalsDisplayMode(),
  65. 'includeTaxInGrandTotal' => $this->isTaxDisplayedInGrandTotal(),
  66. 'isFullTaxSummaryDisplayed' => $this->isFullTaxSummaryDisplayed(),
  67. 'isZeroTaxDisplayed' => $this->taxConfig->displayCartZeroTax(),
  68. 'reloadOnBillingAddress' => $this->reloadOnBillingAddress(),
  69. 'defaultCountryId' => $this->scopeConfig->getValue(
  70. \Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_COUNTRY,
  71. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  72. ),
  73. 'defaultRegionId' => $defaultRegionId,
  74. 'defaultPostcode' => $this->scopeConfig->getValue(
  75. \Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_POSTCODE,
  76. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  77. ),
  78. ];
  79. }
  80. /**
  81. * Shipping mode: 'both', 'including', 'excluding'
  82. *
  83. * @return string
  84. */
  85. public function getDisplayShippingMode()
  86. {
  87. if ($this->taxConfig->displayCartShippingBoth()) {
  88. return 'both';
  89. }
  90. if ($this->taxConfig->displayCartShippingExclTax()) {
  91. return 'excluding';
  92. }
  93. return 'including';
  94. }
  95. /**
  96. * Return flag whether to display shipping price excluding tax
  97. *
  98. * @return bool
  99. */
  100. public function isDisplayShippingPriceExclTax()
  101. {
  102. return $this->taxHelper->displayShippingPriceExcludingTax();
  103. }
  104. /**
  105. * Return flag whether to display shipping price including and excluding tax
  106. *
  107. * @return bool
  108. */
  109. public function isDisplayShippingBothPrices()
  110. {
  111. return $this->taxHelper->displayShippingBothPrices();
  112. }
  113. /**
  114. * Get review item price display mode
  115. *
  116. * @return string 'both', 'including', 'excluding'
  117. */
  118. public function getReviewItemPriceDisplayMode()
  119. {
  120. if ($this->taxHelper->displayCartBothPrices()) {
  121. return 'both';
  122. }
  123. if ($this->taxHelper->displayCartPriceExclTax()) {
  124. return 'excluding';
  125. }
  126. return 'including';
  127. }
  128. /**
  129. * Get review item price display mode
  130. *
  131. * @return string 'both', 'including', 'excluding'
  132. */
  133. public function getReviewTotalsDisplayMode()
  134. {
  135. if ($this->taxConfig->displayCartSubtotalBoth()) {
  136. return 'both';
  137. }
  138. if ($this->taxConfig->displayCartSubtotalExclTax()) {
  139. return 'excluding';
  140. }
  141. return 'including';
  142. }
  143. /**
  144. * Show tax details in checkout totals section flag
  145. *
  146. * @return bool
  147. */
  148. public function isFullTaxSummaryDisplayed()
  149. {
  150. return $this->taxHelper->displayFullSummary();
  151. }
  152. /**
  153. * Display tax in grand total section or not
  154. *
  155. * @return bool
  156. */
  157. public function isTaxDisplayedInGrandTotal()
  158. {
  159. return $this->taxConfig->displayCartTaxWithGrandTotal();
  160. }
  161. /**
  162. * Reload totals(taxes) on billing address update
  163. *
  164. * @return bool
  165. */
  166. protected function reloadOnBillingAddress()
  167. {
  168. $quote = $this->checkoutSession->getQuote();
  169. $configValue = $this->scopeConfig->getValue(
  170. Config::CONFIG_XML_PATH_BASED_ON,
  171. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  172. );
  173. return 'billing' == $configValue || $quote->isVirtual();
  174. }
  175. }