Adjustment.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Weee\Pricing\Render;
  7. use Magento\Framework\Pricing\PriceCurrencyInterface;
  8. use Magento\Framework\Pricing\Render\AbstractAdjustment;
  9. use Magento\Framework\View\Element\Template;
  10. use Magento\Weee\Model\Tax;
  11. /**
  12. * Weee Price Adjustment that handles Weee specific amount and its display
  13. */
  14. class Adjustment extends AbstractAdjustment
  15. {
  16. /**
  17. * Weee helper
  18. *
  19. * @var \Magento\Weee\Helper\Data
  20. */
  21. protected $weeeHelper;
  22. /**
  23. * @var float
  24. */
  25. protected $finalAmount;
  26. /**
  27. * Constructor
  28. *
  29. * @param Template\Context $context
  30. * @param PriceCurrencyInterface $priceCurrency
  31. * @param \Magento\Weee\Helper\Data $weeeHelper
  32. * @param array $data
  33. */
  34. public function __construct(
  35. Template\Context $context,
  36. PriceCurrencyInterface $priceCurrency,
  37. \Magento\Weee\Helper\Data $weeeHelper,
  38. array $data = []
  39. ) {
  40. $this->weeeHelper = $weeeHelper;
  41. parent::__construct($context, $priceCurrency, $data);
  42. }
  43. /**
  44. * @return null
  45. */
  46. protected function apply()
  47. {
  48. $weeeAmount = $this->amountRender->getAmount()->getAdjustmentAmount($this->getAdjustmentCode());
  49. $weeeTaxAmount =
  50. $this->amountRender->getAmount()->getAdjustmentAmount(\Magento\Weee\Pricing\TaxAdjustment::ADJUSTMENT_CODE);
  51. $this->finalAmount = $this->amountRender->getDisplayValue();
  52. if ($this->typeOfDisplay([Tax::DISPLAY_EXCL_DESCR_INCL])) {
  53. $this->amountRender->setDisplayValue(
  54. $this->amountRender->getDisplayValue() - $weeeAmount - $weeeTaxAmount
  55. );
  56. }
  57. return $this->toHtml();
  58. }
  59. /**
  60. * @return float
  61. */
  62. public function getRawFinalAmount()
  63. {
  64. return $this->finalAmount;
  65. }
  66. /**
  67. * Obtain adjustment code
  68. *
  69. * @return string
  70. */
  71. public function getAdjustmentCode()
  72. {
  73. return \Magento\Weee\Pricing\Adjustment::ADJUSTMENT_CODE;
  74. }
  75. /**
  76. * @return float
  77. */
  78. public function getFinalAmount()
  79. {
  80. return $this->formatCurrency($this->finalAmount);
  81. }
  82. /**
  83. * Get weee amount
  84. *
  85. * @return float
  86. */
  87. protected function getWeeeAmount()
  88. {
  89. $product = $this->getSaleableItem();
  90. return $this->weeeHelper->getAmountExclTax($product);
  91. }
  92. /**
  93. * Define if adjustment should be shown with including tax, description
  94. *
  95. * @return bool
  96. */
  97. public function showInclDescr()
  98. {
  99. return $this->isWeeeShown() && $this->getWeeeAmount() && $this->typeOfDisplay(Tax::DISPLAY_INCL_DESCR);
  100. }
  101. /**
  102. * Define if adjustment should be shown with including tax, excluding tax, description
  103. *
  104. * @return bool
  105. */
  106. public function showExclDescrIncl()
  107. {
  108. return $this->isWeeeShown() && $this->getWeeeAmount() && $this->typeOfDisplay(Tax::DISPLAY_EXCL_DESCR_INCL);
  109. }
  110. /**
  111. * Obtain Weee tax attributes
  112. *
  113. * @return array|\Magento\Framework\DataObject[]
  114. */
  115. public function getWeeeTaxAttributes()
  116. {
  117. return $this->isWeeeShown() ? $this->getWeeeAttributesForDisplay() : [];
  118. }
  119. /**
  120. * Render Weee tax attributes name
  121. *
  122. * @param \Magento\Framework\DataObject $attribute
  123. * @return string
  124. */
  125. public function renderWeeeTaxAttributeName(\Magento\Framework\DataObject $attribute)
  126. {
  127. return $attribute->getData('name');
  128. }
  129. /**
  130. * Render Weee tax attributes value
  131. *
  132. * @param \Magento\Framework\DataObject $attribute
  133. * @return string
  134. */
  135. public function renderWeeeTaxAttribute(\Magento\Framework\DataObject $attribute)
  136. {
  137. return $this->convertAndFormatCurrency($attribute->getData('amount'));
  138. }
  139. /**
  140. * Render Weee tax attributes value with tax
  141. *
  142. * @param \Magento\Framework\DataObject $attribute
  143. * @return string
  144. */
  145. public function renderWeeeTaxAttributeWithTax(\Magento\Framework\DataObject $attribute)
  146. {
  147. return $this->convertAndFormatCurrency(
  148. $attribute->getData('amount_excl_tax') + $attribute->getData('tax_amount')
  149. );
  150. }
  151. /**
  152. * Render Weee tax attributes value without tax
  153. *
  154. * @param \Magento\Framework\DataObject $attribute
  155. * @return string
  156. */
  157. public function renderWeeeTaxAttributeWithoutTax(\Magento\Framework\DataObject $attribute)
  158. {
  159. $price = $attribute->getData('amount_excl_tax');
  160. return ($price > 0) ? $this->convertAndFormatCurrency($price): $this->convertAndFormatCurrency(0);
  161. }
  162. /**
  163. * Returns display type for price accordingly to current zone
  164. *
  165. * @param int|int[]|null $compareTo
  166. * @param \Magento\Store\Model\Store|null $store
  167. * @return bool|int
  168. */
  169. protected function typeOfDisplay($compareTo = null, $store = null)
  170. {
  171. return $this->weeeHelper->typeOfDisplay($compareTo, $this->getZone(), $store);
  172. }
  173. /**
  174. * Get Weee attributes for display
  175. *
  176. * @return \Magento\Framework\DataObject[]
  177. */
  178. protected function getWeeeAttributesForDisplay()
  179. {
  180. $product = $this->getSaleableItem();
  181. return $this->weeeHelper->getProductWeeeAttributesForDisplay($product);
  182. }
  183. /**
  184. * Returns whether Weee should be displayed
  185. *
  186. * @return bool
  187. */
  188. protected function isWeeeShown()
  189. {
  190. $isWeeeShown = $this->typeOfDisplay([Tax::DISPLAY_INCL_DESCR, Tax::DISPLAY_EXCL_DESCR_INCL]);
  191. return $isWeeeShown;
  192. }
  193. /**
  194. * Returns whether tax should be shown (according to Price Display Settings)
  195. *
  196. * @return int
  197. */
  198. public function getTaxDisplayConfig()
  199. {
  200. $showPriceWithTax = $this->weeeHelper->getTaxDisplayConfig();
  201. return $showPriceWithTax;
  202. }
  203. /**
  204. * Returns whether price includes tax
  205. *
  206. * @return bool
  207. */
  208. public function isPriceIncludesTax()
  209. {
  210. $showPriceWithTax = $this->weeeHelper->displayTotalsInclTax();
  211. return $showPriceWithTax;
  212. }
  213. }