Renderer.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Weee\Block\Item\Price;
  7. use Magento\Framework\Pricing\PriceCurrencyInterface;
  8. use Magento\Sales\Model\Order\CreditMemo\Item as CreditMemoItem;
  9. use Magento\Sales\Model\Order\Invoice\Item as InvoiceItem;
  10. use Magento\Sales\Model\Order\Item as OrderItem;
  11. use Magento\Weee\Model\Tax as WeeeDisplayConfig;
  12. /**
  13. * Item price render block
  14. *
  15. * @api
  16. * @author Magento Core Team <core@magentocommerce.com>
  17. * @since 100.0.2
  18. */
  19. class Renderer extends \Magento\Tax\Block\Item\Price\Renderer
  20. {
  21. /**
  22. * @var \Magento\Weee\Helper\Data
  23. */
  24. protected $weeeHelper;
  25. /**
  26. * @param \Magento\Framework\View\Element\Template\Context $context
  27. * @param \Magento\Tax\Helper\Data $taxHelper
  28. * @param PriceCurrencyInterface $priceCurrency
  29. * @param \Magento\Weee\Helper\Data $weeeHelper
  30. * @param array $data
  31. */
  32. public function __construct(
  33. \Magento\Framework\View\Element\Template\Context $context,
  34. \Magento\Tax\Helper\Data $taxHelper,
  35. PriceCurrencyInterface $priceCurrency,
  36. \Magento\Weee\Helper\Data $weeeHelper,
  37. array $data = []
  38. ) {
  39. $this->weeeHelper = $weeeHelper;
  40. parent::__construct($context, $taxHelper, $priceCurrency, $data);
  41. $this->_isScopePrivate = true;
  42. }
  43. /**
  44. * Whether to display weee details together with price
  45. *
  46. * @return bool
  47. */
  48. public function displayPriceWithWeeeDetails()
  49. {
  50. if (!$this->weeeHelper->isEnabled($this->getStoreId())) {
  51. return false;
  52. }
  53. $displayWeeeDetails = $this->weeeHelper->typeOfDisplay(
  54. [WeeeDisplayConfig::DISPLAY_INCL_DESCR, WeeeDisplayConfig::DISPLAY_EXCL_DESCR_INCL],
  55. $this->getZone(),
  56. $this->getStoreId()
  57. );
  58. if (!$displayWeeeDetails) {
  59. return false;
  60. }
  61. if ($this->weeeHelper->getWeeeTaxAppliedAmount($this->getItem()) <= 0) {
  62. return false;
  63. }
  64. return true;
  65. }
  66. /**
  67. * Return the flag whether to include weee in the price
  68. *
  69. * @return bool|int
  70. */
  71. public function getIncludeWeeeFlag()
  72. {
  73. $includeWeee = $this->weeeHelper->typeOfDisplay(
  74. [WeeeDisplayConfig::DISPLAY_INCL_DESCR, WeeeDisplayConfig::DISPLAY_INCL],
  75. $this->getZone(),
  76. $this->getStoreId()
  77. );
  78. return $includeWeee;
  79. }
  80. /**
  81. * Get display price for unit price including tax. The Weee amount will be added to unit price including tax
  82. * depending on Weee display setting
  83. *
  84. * @return float
  85. */
  86. public function getUnitDisplayPriceInclTax()
  87. {
  88. $priceInclTax = $this->getItem()->getPriceInclTax();
  89. if (!$this->weeeHelper->isEnabled($this->getStoreId())) {
  90. return $priceInclTax;
  91. }
  92. if ($this->getIncludeWeeeFlag()) {
  93. return $priceInclTax + $this->weeeHelper->getWeeeTaxInclTax($this->getItem());
  94. }
  95. return $priceInclTax;
  96. }
  97. /**
  98. * Get base price for unit price including tax. The Weee amount will be added to unit price including tax
  99. * depending on Weee display setting
  100. *
  101. * @return float
  102. */
  103. public function getBaseUnitDisplayPriceInclTax()
  104. {
  105. $basePriceInclTax = $this->getItem()->getBasePriceInclTax();
  106. if (!$this->weeeHelper->isEnabled($this->getStoreId())) {
  107. return $basePriceInclTax;
  108. }
  109. if ($this->getIncludeWeeeFlag()) {
  110. return $basePriceInclTax + $this->weeeHelper->getBaseWeeeTaxInclTax($this->getItem());
  111. }
  112. return $basePriceInclTax;
  113. }
  114. /**
  115. * Get display price for row total including tax. The Weee amount will be added to row total including tax
  116. * depending on Weee display setting
  117. *
  118. * @return float
  119. */
  120. public function getRowDisplayPriceInclTax()
  121. {
  122. $rowTotalInclTax = $this->getItem()->getRowTotalInclTax();
  123. if (!$this->weeeHelper->isEnabled($this->getStoreId())) {
  124. return $rowTotalInclTax;
  125. }
  126. if ($this->getIncludeWeeeFlag()) {
  127. return $rowTotalInclTax + $this->weeeHelper->getRowWeeeTaxInclTax($this->getItem());
  128. }
  129. return $rowTotalInclTax;
  130. }
  131. /**
  132. * Get base price for row total including tax. The Weee amount will be added to row total including tax
  133. * depending on Weee display setting
  134. *
  135. * @return float
  136. */
  137. public function getBaseRowDisplayPriceInclTax()
  138. {
  139. $baseRowTotalInclTax = $this->getItem()->getBaseRowTotalInclTax();
  140. if (!$this->weeeHelper->isEnabled($this->getStoreId())) {
  141. return $baseRowTotalInclTax;
  142. }
  143. if ($this->getIncludeWeeeFlag()) {
  144. return $baseRowTotalInclTax + $this->weeeHelper->getBaseRowWeeeTaxInclTax($this->getItem());
  145. }
  146. return $baseRowTotalInclTax;
  147. }
  148. /**
  149. * Get display price for unit price excluding tax. The Weee amount will be added to unit price
  150. * depending on Weee display setting
  151. *
  152. * @return float
  153. */
  154. public function getUnitDisplayPriceExclTax()
  155. {
  156. $priceExclTax = $this->getItemDisplayPriceExclTax();
  157. if (!$this->weeeHelper->isEnabled($this->getStoreId())) {
  158. return $priceExclTax;
  159. }
  160. if ($this->getIncludeWeeeFlag()) {
  161. return $priceExclTax + $this->weeeHelper->getWeeeTaxAppliedAmount($this->getItem());
  162. }
  163. return $priceExclTax;
  164. }
  165. /**
  166. * Get base price for unit price excluding tax. The Weee amount will be added to unit price
  167. * depending on Weee display setting
  168. *
  169. * @return float
  170. */
  171. public function getBaseUnitDisplayPriceExclTax()
  172. {
  173. $orderItem = $this->getItem();
  174. if ($orderItem instanceof InvoiceItem || $orderItem instanceof CreditMemoItem) {
  175. $orderItem = $orderItem->getOrderItem();
  176. }
  177. $qty = $orderItem->getQtyOrdered();
  178. $basePriceExclTax = $orderItem->getBaseRowTotal() / $qty;
  179. if (!$this->weeeHelper->isEnabled($this->getStoreId())) {
  180. return $basePriceExclTax;
  181. }
  182. if ($this->getIncludeWeeeFlag()) {
  183. return $basePriceExclTax + $this->getItem()->getBaseWeeeTaxAppliedAmount();
  184. }
  185. return $basePriceExclTax;
  186. }
  187. /**
  188. * Get display price for row total excluding tax. The Weee amount will be added to row total
  189. * depending on Weee display setting
  190. *
  191. * @return float
  192. */
  193. public function getRowDisplayPriceExclTax()
  194. {
  195. $rowTotalExclTax = $this->getItem()->getRowTotal();
  196. if (!$this->weeeHelper->isEnabled($this->getStoreId())) {
  197. return $rowTotalExclTax;
  198. }
  199. if ($this->getIncludeWeeeFlag()) {
  200. return $rowTotalExclTax + $this->weeeHelper->getWeeeTaxAppliedRowAmount($this->getItem());
  201. }
  202. return $rowTotalExclTax;
  203. }
  204. /**
  205. * Get base price for row total excluding tax. The Weee amount will be added to row total
  206. * depending on Weee display setting
  207. *
  208. * @return float
  209. */
  210. public function getBaseRowDisplayPriceExclTax()
  211. {
  212. $baseRowTotalExclTax = $this->getItem()->getBaseRowTotal();
  213. if (!$this->weeeHelper->isEnabled($this->getStoreId())) {
  214. return $baseRowTotalExclTax;
  215. }
  216. if ($this->getIncludeWeeeFlag()) {
  217. return $baseRowTotalExclTax + $this->getItem()->getBaseWeeeTaxAppliedRowAmnt();
  218. }
  219. return $baseRowTotalExclTax;
  220. }
  221. /**
  222. * Get final unit display price including tax, this will add Weee amount to unit price include tax
  223. *
  224. * @return float
  225. */
  226. public function getFinalUnitDisplayPriceInclTax()
  227. {
  228. $priceInclTax = $this->getItem()->getPriceInclTax();
  229. if (!$this->weeeHelper->isEnabled($this->getStoreId())) {
  230. return $priceInclTax;
  231. }
  232. return $priceInclTax + $this->weeeHelper->getWeeeTaxInclTax($this->getItem());
  233. }
  234. /**
  235. * Get base final unit display price including tax, this will add Weee amount to unit price include tax
  236. *
  237. * @return float
  238. */
  239. public function getBaseFinalUnitDisplayPriceInclTax()
  240. {
  241. $basePriceInclTax = $this->getItem()->getBasePriceInclTax();
  242. if (!$this->weeeHelper->isEnabled($this->getStoreId())) {
  243. return $basePriceInclTax;
  244. }
  245. return $basePriceInclTax + $this->weeeHelper->getBaseWeeeTaxInclTax($this->getItem());
  246. }
  247. /**
  248. * Get final row display price including tax, this will add weee amount to rowTotalInclTax
  249. *
  250. * @return float
  251. */
  252. public function getFinalRowDisplayPriceInclTax()
  253. {
  254. $rowTotalInclTax = $this->getItem()->getRowTotalInclTax();
  255. if (!$this->weeeHelper->isEnabled($this->getStoreId())) {
  256. return $rowTotalInclTax;
  257. }
  258. return $rowTotalInclTax + $this->weeeHelper->getRowWeeeTaxInclTax($this->getItem());
  259. }
  260. /**
  261. * Get base final row display price including tax, this will add weee amount to rowTotalInclTax
  262. *
  263. * @return float
  264. */
  265. public function getBaseFinalRowDisplayPriceInclTax()
  266. {
  267. $baseRowTotalInclTax = $this->getItem()->getBaseRowTotalInclTax();
  268. if (!$this->weeeHelper->isEnabled($this->getStoreId())) {
  269. return $baseRowTotalInclTax;
  270. }
  271. return $baseRowTotalInclTax + $this->weeeHelper->getBaseRowWeeeTaxInclTax($this->getItem());
  272. }
  273. /**
  274. * Get final unit display price excluding tax
  275. *
  276. * @return float
  277. */
  278. public function getFinalUnitDisplayPriceExclTax()
  279. {
  280. $priceExclTax = $this->getItemDisplayPriceExclTax();
  281. if (!$this->weeeHelper->isEnabled($this->getStoreId())) {
  282. return $priceExclTax;
  283. }
  284. return $priceExclTax + $this->weeeHelper->getWeeeTaxAppliedAmount($this->getItem());
  285. }
  286. /**
  287. * Get base final unit display price excluding tax
  288. *
  289. * @return float
  290. */
  291. public function getBaseFinalUnitDisplayPriceExclTax()
  292. {
  293. $orderItem = $this->getItem();
  294. if ($orderItem instanceof InvoiceItem || $orderItem instanceof CreditMemoItem) {
  295. $orderItem = $orderItem->getOrderItem();
  296. }
  297. $qty = $orderItem->getQtyOrdered();
  298. $basePriceExclTax = $orderItem->getBaseRowTotal() / $qty;
  299. if (!$this->weeeHelper->isEnabled($this->getStoreId())) {
  300. return $basePriceExclTax;
  301. }
  302. return $basePriceExclTax + $this->getItem()->getBaseWeeeTaxAppliedAmount();
  303. }
  304. /**
  305. * Get final row display price excluding tax, this will add Weee amount to rowTotal
  306. *
  307. * @return float
  308. */
  309. public function getFinalRowDisplayPriceExclTax()
  310. {
  311. $rowTotalExclTax = $this->getItem()->getRowTotal();
  312. if (!$this->weeeHelper->isEnabled($this->getStoreId())) {
  313. return $rowTotalExclTax;
  314. }
  315. return $rowTotalExclTax + $this->weeeHelper->getWeeeTaxAppliedRowAmount($this->getItem());
  316. }
  317. /**
  318. * Get base final row display price excluding tax, this will add Weee amount to rowTotal
  319. *
  320. * @return float
  321. */
  322. public function getBaseFinalRowDisplayPriceExclTax()
  323. {
  324. $baseRowTotalExclTax = $this->getItem()->getBaseRowTotal();
  325. if (!$this->weeeHelper->isEnabled($this->getStoreId())) {
  326. return $baseRowTotalExclTax;
  327. }
  328. return $baseRowTotalExclTax + $this->getItem()->getBaseWeeeTaxAppliedRowAmnt();
  329. }
  330. /**
  331. * Whether to display final price that include Weee amounts
  332. *
  333. * @return bool
  334. */
  335. public function displayFinalPrice()
  336. {
  337. $flag = $this->weeeHelper->typeOfDisplay(
  338. WeeeDisplayConfig::DISPLAY_EXCL_DESCR_INCL,
  339. $this->getZone(),
  340. $this->getStoreId()
  341. );
  342. if (!$flag) {
  343. return false;
  344. }
  345. if ($this->weeeHelper->getWeeeTaxAppliedAmount($this->getItem()) <= 0) {
  346. return false;
  347. }
  348. return true;
  349. }
  350. /**
  351. * Return the total amount minus discount
  352. *
  353. * @param OrderItem|InvoiceItem|CreditMemoItem $item
  354. * @return mixed
  355. */
  356. public function getTotalAmount($item)
  357. {
  358. $totalAmount = $item->getRowTotal()
  359. - $item->getDiscountAmount()
  360. + $item->getTaxAmount()
  361. + $item->getDiscountTaxCompensationAmount()
  362. + $this->weeeHelper->getRowWeeeTaxInclTax($item);
  363. return $totalAmount;
  364. }
  365. /**
  366. * Return the total amount minus discount
  367. *
  368. * @param OrderItem|InvoiceItem|CreditMemoItem $item
  369. * @return mixed
  370. */
  371. public function getBaseTotalAmount($item)
  372. {
  373. $totalAmount = $item->getBaseRowTotal()
  374. - $item->getBaseDiscountAmount()
  375. + $item->getBaseTaxAmount()
  376. + $item->getBaseDiscountTaxCompensationAmount()
  377. + $this->weeeHelper->getBaseRowWeeeTaxInclTax($item);
  378. return $totalAmount;
  379. }
  380. }