Tax.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Weee\Model\ResourceModel;
  7. use Magento\Catalog\Model\Product;
  8. use Magento\Catalog\Model\Product\Condition\ConditionInterface;
  9. /**
  10. * Wee tax resource model
  11. *
  12. * @api
  13. * @since 100.0.2
  14. */
  15. class Tax extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  16. {
  17. /**
  18. * @var \Magento\Framework\Stdlib\DateTime
  19. */
  20. protected $dateTime;
  21. /**
  22. * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
  23. * @param \Magento\Framework\Stdlib\DateTime $dateTime
  24. * @param string $connectionName
  25. */
  26. public function __construct(
  27. \Magento\Framework\Model\ResourceModel\Db\Context $context,
  28. \Magento\Framework\Stdlib\DateTime $dateTime,
  29. $connectionName = null
  30. ) {
  31. $this->dateTime = $dateTime;
  32. parent::__construct($context, $connectionName);
  33. }
  34. /**
  35. * Resource initialization
  36. *
  37. * @return void
  38. */
  39. protected function _construct()
  40. {
  41. $this->_init('weee_tax', 'value_id');
  42. }
  43. /**
  44. * Fetch one
  45. *
  46. * @param \Magento\Framework\DB\Select|string $select
  47. * @return string
  48. */
  49. public function fetchOne($select)
  50. {
  51. return $this->getConnection()->fetchOne($select);
  52. }
  53. /**
  54. * @param int $countryId
  55. * @param int $regionId
  56. * @param int $websiteId
  57. * @return boolean
  58. */
  59. public function isWeeeInLocation($countryId, $regionId, $websiteId)
  60. {
  61. // Check if there is a weee_tax for the country and region
  62. $attributeSelect = $this->getConnection()->select();
  63. $attributeSelect->from(
  64. $this->getTable('weee_tax'),
  65. 'value'
  66. )->where(
  67. 'website_id IN(?)',
  68. [$websiteId, 0]
  69. )->where(
  70. 'country = ?',
  71. $countryId
  72. )->where(
  73. 'state = ?',
  74. $regionId
  75. )->limit(
  76. 1
  77. );
  78. $value = $this->getConnection()->fetchOne($attributeSelect);
  79. if ($value) {
  80. return true;
  81. }
  82. return false;
  83. }
  84. /**
  85. * @param int $countryId
  86. * @param int $regionId
  87. * @param int $websiteId
  88. * @param int $storeId
  89. * @param int $entityId
  90. * @return array[]
  91. */
  92. public function fetchWeeeTaxCalculationsByEntity($countryId, $regionId, $websiteId, $storeId, $entityId)
  93. {
  94. $attributeSelect = $this->getConnection()->select();
  95. $attributeSelect->from(
  96. ['eavTable' => $this->getTable('eav_attribute')],
  97. ['eavTable.attribute_code', 'eavTable.attribute_id', 'eavTable.frontend_label']
  98. )->joinLeft(
  99. ['eavLabel' => $this->getTable('eav_attribute_label')],
  100. 'eavLabel.attribute_id = eavTable.attribute_id and eavLabel.store_id = ' .((int) $storeId),
  101. 'eavLabel.value as label_value'
  102. )->joinInner(
  103. ['weeeTax' => $this->getTable('weee_tax')],
  104. 'weeeTax.attribute_id = eavTable.attribute_id',
  105. 'weeeTax.value as weee_value'
  106. )->where(
  107. 'eavTable.frontend_input = ?',
  108. 'weee'
  109. )->where(
  110. 'weeeTax.website_id IN(?)',
  111. [$websiteId, 0]
  112. )->where(
  113. 'weeeTax.country = ?',
  114. $countryId
  115. )->where(
  116. 'weeeTax.state IN(?)',
  117. [$regionId, 0]
  118. )->where(
  119. 'weeeTax.entity_id = ?',
  120. (int)$entityId
  121. );
  122. $order = ['weeeTax.state ' . \Magento\Framework\DB\Select::SQL_DESC,
  123. 'weeeTax.website_id ' . \Magento\Framework\DB\Select::SQL_DESC];
  124. $attributeSelect->order($order);
  125. $values = $this->getConnection()->fetchAll($attributeSelect);
  126. if ($values) {
  127. return $values;
  128. }
  129. return [];
  130. }
  131. }