Tax.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Weee\Model;
  7. use Magento\Catalog\Model\Product;
  8. use Magento\Framework\Pricing\PriceCurrencyInterface;
  9. use Magento\Store\Model\Website;
  10. use Magento\Tax\Model\Calculation;
  11. use Magento\Customer\Api\AccountManagementInterface;
  12. use Magento\Catalog\Model\Product\Type;
  13. /**
  14. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  15. * @api
  16. * @since 100.0.2
  17. */
  18. class Tax extends \Magento\Framework\Model\AbstractModel
  19. {
  20. /**
  21. * Including FPT only
  22. */
  23. const DISPLAY_INCL = 0;
  24. /**
  25. * Including FPT and FPT description
  26. */
  27. const DISPLAY_INCL_DESCR = 1;
  28. /**
  29. * Excluding FPT. Including FPT description and final price
  30. */
  31. const DISPLAY_EXCL_DESCR_INCL = 2;
  32. /**
  33. * Excluding FPT
  34. */
  35. const DISPLAY_EXCL = 3;
  36. /**
  37. * @var array|null
  38. */
  39. protected $_allAttributes = null;
  40. /**
  41. * Tax data
  42. *
  43. * @var \Magento\Tax\Helper\Data
  44. */
  45. protected $_taxData = null;
  46. /**
  47. * @var \Magento\Eav\Model\Entity\AttributeFactory
  48. */
  49. protected $_attributeFactory;
  50. /**
  51. * @var \Magento\Store\Model\StoreManagerInterface
  52. */
  53. protected $_storeManager;
  54. /**
  55. * @var \Magento\Tax\Model\CalculationFactory
  56. */
  57. protected $_calculationFactory;
  58. /**
  59. * @var \Magento\Customer\Model\Session
  60. */
  61. protected $_customerSession;
  62. /**
  63. * Weee config
  64. *
  65. * @var \Magento\Weee\Model\Config
  66. */
  67. protected $weeeConfig;
  68. /**
  69. * @var PriceCurrencyInterface
  70. */
  71. protected $priceCurrency;
  72. /**
  73. * @var AccountManagementInterface
  74. */
  75. protected $accountManagement;
  76. /**
  77. * @param \Magento\Framework\Model\Context $context
  78. * @param \Magento\Framework\Registry $registry
  79. * @param \Magento\Eav\Model\Entity\AttributeFactory $attributeFactory
  80. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  81. * @param \Magento\Tax\Model\CalculationFactory $calculationFactory
  82. * @param \Magento\Customer\Model\Session $customerSession
  83. * @param AccountManagementInterface $accountManagement
  84. * @param \Magento\Tax\Helper\Data $taxData
  85. * @param \Magento\Weee\Model\ResourceModel\Tax $resource
  86. * @param Config $weeeConfig
  87. * @param PriceCurrencyInterface $priceCurrency
  88. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  89. * @param array $data
  90. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  91. */
  92. public function __construct(
  93. \Magento\Framework\Model\Context $context,
  94. \Magento\Framework\Registry $registry,
  95. \Magento\Eav\Model\Entity\AttributeFactory $attributeFactory,
  96. \Magento\Store\Model\StoreManagerInterface $storeManager,
  97. \Magento\Tax\Model\CalculationFactory $calculationFactory,
  98. \Magento\Customer\Model\Session $customerSession,
  99. AccountManagementInterface $accountManagement,
  100. \Magento\Tax\Helper\Data $taxData,
  101. \Magento\Weee\Model\ResourceModel\Tax $resource,
  102. \Magento\Weee\Model\Config $weeeConfig,
  103. PriceCurrencyInterface $priceCurrency,
  104. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  105. array $data = []
  106. ) {
  107. $this->_attributeFactory = $attributeFactory;
  108. $this->_storeManager = $storeManager;
  109. $this->_calculationFactory = $calculationFactory;
  110. $this->_customerSession = $customerSession;
  111. $this->accountManagement = $accountManagement;
  112. $this->_taxData = $taxData;
  113. $this->weeeConfig = $weeeConfig;
  114. $this->priceCurrency = $priceCurrency;
  115. parent::__construct($context, $registry, $resource, $resourceCollection, $data);
  116. }
  117. /**
  118. * Initialize resource
  119. *
  120. * @return void
  121. */
  122. protected function _construct()
  123. {
  124. $this->_init(\Magento\Weee\Model\ResourceModel\Tax::class);
  125. }
  126. /**
  127. * @param Product $product
  128. * @param null|false|\Magento\Framework\DataObject $shipping
  129. * @param null|false|\Magento\Framework\DataObject $billing
  130. * @param Website $website
  131. * @param bool $calculateTax
  132. * @return float
  133. */
  134. public function getWeeeAmount(
  135. $product,
  136. $shipping = null,
  137. $billing = null,
  138. $website = null,
  139. $calculateTax = false
  140. ) {
  141. $amount = 0;
  142. $attributes = $this->getProductWeeeAttributes(
  143. $product,
  144. $shipping,
  145. $billing,
  146. $website,
  147. $calculateTax
  148. );
  149. foreach ($attributes as $attribute) {
  150. $amount += $attribute->getAmount();
  151. }
  152. return $amount;
  153. }
  154. /**
  155. * @param Product $product
  156. * @param null|false|\Magento\Framework\DataObject $shipping
  157. * @param null|false|\Magento\Framework\DataObject $billing
  158. * @param Website $website
  159. * @return float
  160. */
  161. public function getWeeeAmountExclTax(
  162. $product,
  163. $shipping = null,
  164. $billing = null,
  165. $website = null
  166. ) {
  167. $amountExclTax = 0;
  168. $attributes = $this->getProductWeeeAttributes(
  169. $product,
  170. $shipping,
  171. $billing,
  172. $website,
  173. true,
  174. false
  175. );
  176. if (Type::TYPE_BUNDLE !== $product->getTypeId() || $product->getPriceType()) {
  177. foreach ($attributes as $attribute) {
  178. $amountExclTax += $attribute->getAmountExclTax();
  179. }
  180. }
  181. return $amountExclTax;
  182. }
  183. /**
  184. * @param bool $forceEnabled
  185. * @return array
  186. */
  187. public function getWeeeAttributeCodes($forceEnabled = false)
  188. {
  189. return $this->getWeeeTaxAttributeCodes(null, $forceEnabled);
  190. }
  191. /**
  192. * Retrieve Wee tax attribute codes
  193. *
  194. * @param null|string|bool|int|Store $store
  195. * @param bool $forceEnabled
  196. * @return array
  197. */
  198. public function getWeeeTaxAttributeCodes($store = null, $forceEnabled = false)
  199. {
  200. if (!$forceEnabled && !$this->weeeConfig->isEnabled($store)) {
  201. return [];
  202. }
  203. if ($this->_allAttributes === null) {
  204. $this->_allAttributes = $this->_attributeFactory->create()->getAttributeCodesByFrontendType('weee');
  205. }
  206. return $this->_allAttributes;
  207. }
  208. /**
  209. * @param Product $product
  210. * @param null|false|\Magento\Quote\Model\Quote\Address $shipping
  211. * @param null|false|\Magento\Quote\Model\Quote\Address $billing
  212. * @param Website $website
  213. * @param bool $calculateTax
  214. * @param bool $round
  215. * @return \Magento\Framework\DataObject[]
  216. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  217. * @SuppressWarnings(PHPMD.NPathComplexity)
  218. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  219. */
  220. public function getProductWeeeAttributes(
  221. $product,
  222. $shipping = null,
  223. $billing = null,
  224. $website = null,
  225. $calculateTax = null,
  226. $round = true
  227. ) {
  228. $result = [];
  229. $websiteId = null;
  230. /** @var \Magento\Store\Model\Store $store */
  231. $store = null;
  232. if (!$website) {
  233. $store = $product->getStore();
  234. if ($store) {
  235. $websiteId = $store->getWebsiteId();
  236. }
  237. }
  238. if (!$websiteId) {
  239. $websiteObject = $this->_storeManager->getWebsite($website);
  240. $websiteId = $websiteObject->getId();
  241. $store = $websiteObject->getDefaultGroup()->getDefaultStore();
  242. }
  243. $allWeee = $this->getWeeeTaxAttributeCodes($store);
  244. if (!$allWeee) {
  245. return $result;
  246. }
  247. /** @var \Magento\Tax\Model\Calculation $calculator */
  248. $calculator = $this->_calculationFactory->create();
  249. $customerId = $this->_customerSession->getCustomerId();
  250. if ($shipping && $shipping->getCountryId()) {
  251. $customerTaxClass = $shipping->getQuote()->getCustomerTaxClassId();
  252. } else {
  253. // if customer logged use it default shipping and billing address
  254. if ($customerId) {
  255. $shipping = $this->accountManagement->getDefaultShippingAddress($customerId);
  256. $billing = $this->accountManagement->getDefaultBillingAddress($customerId);
  257. $customerTaxClass = null;
  258. } else {
  259. $shippingAddressArray = $this->_customerSession->getDefaultTaxShippingAddress();
  260. $billingAddressArray = $this->_customerSession->getDefaultTaxBillingAddress();
  261. if (!empty($billingAddressArray)) {
  262. $billing = new \Magento\Framework\DataObject($billingAddressArray);
  263. }
  264. if (!empty($shippingAddressArray)) {
  265. $shipping = new \Magento\Framework\DataObject($shippingAddressArray);
  266. }
  267. $customerTaxClass = $this->_customerSession->getCustomerTaxClassId();
  268. }
  269. }
  270. $rateRequest = $calculator->getRateRequest(
  271. $shipping,
  272. $billing,
  273. $customerTaxClass,
  274. $store,
  275. $customerId
  276. );
  277. $defaultRateRequest = $calculator->getDefaultRateRequest($store);
  278. $productAttributes = $this->getResource()->fetchWeeeTaxCalculationsByEntity(
  279. $rateRequest->getCountryId(),
  280. $rateRequest->getRegionId(),
  281. $websiteId,
  282. $store->getId(),
  283. $product->getId()
  284. );
  285. foreach ($productAttributes as $attribute) {
  286. $value = $attribute['weee_value'];
  287. if ($value) {
  288. $taxAmount = $amount = 0;
  289. $amount = $value;
  290. $amountExclTax = $value;
  291. if ($calculateTax && $this->weeeConfig->isTaxable($store)) {
  292. /** @var \Magento\Tax\Model\Calculation $calculator */
  293. $defaultPercent = $calculator->getRate(
  294. $defaultRateRequest->setProductClassId($product->getTaxClassId())
  295. );
  296. $currentPercent = $calculator->getRate(
  297. $rateRequest->setProductClassId($product->getTaxClassId())
  298. );
  299. if ($this->_taxData->priceIncludesTax($store)) {
  300. $amountInclTax = $value / (100 + $defaultPercent) * (100 + $currentPercent);
  301. if ($round) {
  302. $amountInclTax = $this->priceCurrency->round($amountInclTax);
  303. }
  304. $taxAmount = $amountInclTax - $amountInclTax / (100 + $currentPercent) * 100;
  305. if ($round) {
  306. $taxAmount = $this->priceCurrency->round($taxAmount);
  307. }
  308. $amountExclTax = $amountInclTax - $taxAmount;
  309. } else {
  310. $appliedRates = $this->_calculationFactory->create()->getAppliedRates($rateRequest);
  311. if (is_array($appliedRates) && count($appliedRates) > 1) {
  312. $taxAmount = 0;
  313. foreach ($appliedRates as $appliedRate) {
  314. $taxRate = $appliedRate['percent'];
  315. if ($round) {
  316. $taxAmount += $this->priceCurrency->round($value * $taxRate / 100);
  317. } else {
  318. $taxAmount += $value * $taxRate / 100;
  319. }
  320. }
  321. } else {
  322. if ($round) {
  323. $taxAmount = $this->priceCurrency->round(
  324. $value * $currentPercent / 100
  325. );
  326. } else {
  327. $taxAmount = $value * $currentPercent / 100;
  328. }
  329. }
  330. }
  331. }
  332. $one = new \Magento\Framework\DataObject();
  333. $one->setName(
  334. $attribute['label_value'] ? __($attribute['label_value']) : __($attribute['frontend_label'])
  335. )
  336. ->setAmount($amount)
  337. ->setTaxAmount($taxAmount)
  338. ->setAmountExclTax($amountExclTax)
  339. ->setCode($attribute['attribute_code']);
  340. $result[] = $one;
  341. }
  342. }
  343. return $result;
  344. }
  345. /**
  346. * @param int $countryId
  347. * @param int $regionId
  348. * @param int $websiteId
  349. * @return boolean
  350. */
  351. public function isWeeeInLocation($countryId, $regionId, $websiteId)
  352. {
  353. return $this->getResource()->isWeeeInLocation($countryId, $regionId, $websiteId);
  354. }
  355. }