Renderer.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Tax\Block\Item\Price;
  7. use Magento\Framework\Pricing\PriceCurrencyInterface;
  8. use Magento\Framework\Pricing\Render as PricingRender;
  9. use Magento\Framework\View\Element\Template\Context;
  10. use Magento\Quote\Model\Quote\Item\AbstractItem as QuoteItem;
  11. use Magento\Sales\Model\Order\CreditMemo\Item as CreditMemoItem;
  12. use Magento\Sales\Model\Order\Invoice\Item as InvoiceItem;
  13. use Magento\Sales\Model\Order\Item as OrderItem;
  14. use Magento\Tax\Helper\Data as TaxHelper;
  15. /**
  16. * Item price render block
  17. *
  18. * @api
  19. * @author Magento Core Team <core@magentocommerce.com>
  20. * @since 100.0.2
  21. */
  22. class Renderer extends \Magento\Framework\View\Element\Template
  23. {
  24. /**
  25. * @var \Magento\Tax\Helper\Data
  26. */
  27. protected $taxHelper;
  28. /**
  29. * @var QuoteItem|OrderItem|InvoiceItem|CreditMemoItem
  30. */
  31. protected $item;
  32. /**
  33. * @var string|int|null
  34. */
  35. protected $storeId = null;
  36. /**
  37. * Set the display area, e.g., cart, sales, etc.
  38. *
  39. * @var string
  40. */
  41. protected $zone = null;
  42. /**
  43. * @var PriceCurrencyInterface
  44. */
  45. protected $priceCurrency;
  46. /**
  47. * @param Context $context
  48. * @param TaxHelper $taxHelper
  49. * @param PriceCurrencyInterface $priceCurrency
  50. * @param array $data
  51. */
  52. public function __construct(
  53. Context $context,
  54. TaxHelper $taxHelper,
  55. PriceCurrencyInterface $priceCurrency,
  56. array $data = []
  57. ) {
  58. $this->priceCurrency = $priceCurrency;
  59. $this->taxHelper = $taxHelper;
  60. if (isset($data['zone'])) {
  61. $this->zone = $data['zone'];
  62. }
  63. parent::__construct($context, $data);
  64. }
  65. /**
  66. * Set item for render
  67. *
  68. * @param QuoteItem|OrderItem|InvoiceItem|CreditMemoItem $item
  69. * @return $this
  70. */
  71. public function setItem($item)
  72. {
  73. $this->item = $item;
  74. $this->storeId = $item->getStoreId();
  75. return $this;
  76. }
  77. /**
  78. * Get display zone
  79. *
  80. * @return string|null
  81. */
  82. public function getZone()
  83. {
  84. return $this->zone;
  85. }
  86. /**
  87. * Set display zone
  88. *
  89. * @param string $zone
  90. * @return $this
  91. */
  92. public function setZone($zone)
  93. {
  94. $this->zone = $zone;
  95. return $this;
  96. }
  97. /**
  98. * @return int|null|string
  99. */
  100. public function getStoreId()
  101. {
  102. return $this->storeId;
  103. }
  104. /**
  105. * Get quote or order item
  106. *
  107. * @return CreditMemoItem|InvoiceItem|OrderItem|QuoteItem
  108. */
  109. public function getItem()
  110. {
  111. return $this->item;
  112. }
  113. /**
  114. * Return whether display setting is to display price including tax
  115. *
  116. * @return bool
  117. */
  118. public function displayPriceInclTax()
  119. {
  120. switch ($this->zone) {
  121. case PricingRender::ZONE_CART:
  122. return $this->taxHelper->displayCartPriceInclTax($this->storeId);
  123. case PricingRender::ZONE_EMAIL:
  124. case PricingRender::ZONE_SALES:
  125. return $this->taxHelper->displaySalesPriceInclTax($this->storeId);
  126. default:
  127. return $this->taxHelper->displayCartPriceInclTax($this->storeId);
  128. }
  129. }
  130. /**
  131. * Return whether display setting is to display price excluding tax
  132. *
  133. * @return bool
  134. */
  135. public function displayPriceExclTax()
  136. {
  137. switch ($this->zone) {
  138. case PricingRender::ZONE_CART:
  139. return $this->taxHelper->displayCartPriceExclTax($this->storeId);
  140. case PricingRender::ZONE_EMAIL:
  141. case PricingRender::ZONE_SALES:
  142. return $this->taxHelper->displaySalesPriceExclTax($this->storeId);
  143. default:
  144. return $this->taxHelper->displayCartPriceExclTax($this->storeId);
  145. }
  146. }
  147. /**
  148. * Return whether display setting is to display both price including tax and price excluding tax
  149. *
  150. * @return bool
  151. */
  152. public function displayBothPrices()
  153. {
  154. switch ($this->zone) {
  155. case PricingRender::ZONE_CART:
  156. return $this->taxHelper->displayCartBothPrices($this->storeId);
  157. case PricingRender::ZONE_EMAIL:
  158. case PricingRender::ZONE_SALES:
  159. return $this->taxHelper->displaySalesBothPrices($this->storeId);
  160. default:
  161. return $this->taxHelper->displayCartBothPrices($this->storeId);
  162. }
  163. }
  164. /**
  165. * Format price
  166. *
  167. * @param float $price
  168. * @return string
  169. */
  170. public function formatPrice($price)
  171. {
  172. $item = $this->getItem();
  173. if ($item instanceof QuoteItem) {
  174. return $this->priceCurrency->format(
  175. $price,
  176. true,
  177. PriceCurrencyInterface::DEFAULT_PRECISION,
  178. $item->getStore()
  179. );
  180. } elseif ($item instanceof OrderItem) {
  181. return $item->getOrder()->formatPrice($price);
  182. } else {
  183. return $item->getOrderItem()->getOrder()->formatPrice($price);
  184. }
  185. }
  186. /**
  187. * Get item price in display currency or order currency depending
  188. * on item type
  189. *
  190. * @return float
  191. */
  192. public function getItemDisplayPriceExclTax()
  193. {
  194. $item = $this->getItem();
  195. if ($item instanceof QuoteItem) {
  196. return $item->getCalculationPrice();
  197. } else {
  198. return $item->getPrice();
  199. }
  200. }
  201. /**
  202. * Return the total amount minus discount
  203. *
  204. * @param OrderItem|InvoiceItem|CreditmemoItem $item
  205. * @return mixed
  206. */
  207. public function getTotalAmount($item)
  208. {
  209. $totalAmount = $item->getRowTotal()
  210. - $item->getDiscountAmount()
  211. + $item->getTaxAmount()
  212. + $item->getDiscountTaxCompensationAmount();
  213. return $totalAmount;
  214. }
  215. /**
  216. * Return the total amount minus discount
  217. *
  218. * @param OrderItem|InvoiceItem|CreditmemoItem $item
  219. * @return mixed
  220. */
  221. public function getBaseTotalAmount($item)
  222. {
  223. $totalAmount = $item->getBaseRowTotal()
  224. - $item->getBaseDiscountAmount()
  225. + $item->getBaseTaxAmount()
  226. + $item->getBaseDiscountTaxCompensationAmount();
  227. return $totalAmount;
  228. }
  229. }