Rule.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Tax\Model\ResourceModel\Calculation;
  7. /**
  8. * Tax rule resource model
  9. *
  10. * @author Magento Core Team <core@magentocommerce.com>
  11. */
  12. class Rule extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  13. {
  14. /**
  15. * Resource initialization
  16. *
  17. * @return void
  18. */
  19. protected function _construct()
  20. {
  21. $this->_init('tax_calculation_rule', 'tax_calculation_rule_id');
  22. }
  23. /**
  24. * Initialize unique fields
  25. *
  26. * @return \Magento\Tax\Model\ResourceModel\Calculation\Rule
  27. */
  28. protected function _initUniqueFields()
  29. {
  30. $this->_uniqueFields = [['field' => ['code'], 'title' => __('Code')]];
  31. return $this;
  32. }
  33. /**
  34. * Fetches rules by rate, customer tax classes and product tax classes. Returns array of rule codes.
  35. *
  36. * @param array $rateId
  37. * @param array $customerTaxClassIds
  38. * @param array $productTaxClassIds
  39. * @return array
  40. */
  41. public function fetchRuleCodes($rateId, $customerTaxClassIds, $productTaxClassIds)
  42. {
  43. $connection = $this->getConnection();
  44. $select = $connection->select()
  45. ->from(['main' => $this->getTable('tax_calculation')], null)
  46. ->joinLeft(
  47. ['d' => $this->getTable('tax_calculation_rule')],
  48. 'd.tax_calculation_rule_id = main.tax_calculation_rule_id',
  49. ['d.code']
  50. )
  51. ->where('main.tax_calculation_rate_id in (?)', $rateId)
  52. ->where('main.customer_tax_class_id in (?)', $customerTaxClassIds)
  53. ->where('main.product_tax_class_id in (?)', $productTaxClassIds)
  54. ->distinct(true);
  55. return $connection->fetchCol($select);
  56. }
  57. }