UpdateTaxRegionId.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Tax\Setup\Patch\Data;
  7. use Magento\Directory\Model\RegionFactory;
  8. use Magento\Framework\Api\Search\SearchCriteriaFactory;
  9. use Magento\Tax\Api\TaxRateRepositoryInterface;
  10. use Magento\Framework\App\ResourceConnection;
  11. use Magento\Framework\Setup\Patch\DataPatchInterface;
  12. use Magento\Framework\Setup\Patch\PatchVersionInterface;
  13. use Magento\Tax\Setup\TaxSetupFactory;
  14. class UpdateTaxRegionId implements DataPatchInterface, PatchVersionInterface
  15. {
  16. /**
  17. * @var \Magento\Framework\Setup\ModuleDataSetupInterface
  18. */
  19. private $moduleDataSetup;
  20. /**
  21. * @var TaxRateRepositoryInterface
  22. */
  23. private $taxRateRepository;
  24. /**
  25. * @var SearchCriteriaFactory
  26. */
  27. private $searchCriteriaFactory;
  28. /**
  29. * @var RegionFactory
  30. */
  31. private $regionFactory;
  32. /**
  33. * UpdateTaxRegionId constructor.
  34. * @param \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup
  35. * @param TaxRateRepositoryInterface $taxRateRepository
  36. * @param SearchCriteriaFactory $searchCriteriaFactory
  37. * @param RegionFactory $regionFactory
  38. */
  39. public function __construct(
  40. \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup,
  41. TaxRateRepositoryInterface $taxRateRepository,
  42. SearchCriteriaFactory $searchCriteriaFactory,
  43. \Magento\Directory\Model\RegionFactory $regionFactory
  44. ) {
  45. $this->moduleDataSetup = $moduleDataSetup;
  46. $this->taxRateRepository = $taxRateRepository;
  47. $this->searchCriteriaFactory = $searchCriteriaFactory;
  48. $this->regionFactory = $regionFactory;
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function apply()
  54. {
  55. $this->moduleDataSetup->getConnection()->startSetup();
  56. //Update the tax_region_id
  57. $taxRateList = $this->taxRateRepository->getList($this->searchCriteriaFactory->create());
  58. /** @var \Magento\Tax\Api\Data\TaxRateInterface $taxRateData */
  59. foreach ($taxRateList->getItems() as $taxRateData) {
  60. $regionCode = $this->parseRegionFromTaxCode($taxRateData->getCode());
  61. if ($regionCode) {
  62. /** @var \Magento\Directory\Model\Region $region */
  63. $region = $this->regionFactory->create();
  64. $region->loadByCode($regionCode, $taxRateData->getTaxCountryId());
  65. if ($taxRateData->getTaxPostcode() === null) {
  66. $taxRateData->setTaxPostcode('*');
  67. }
  68. $taxRateData->setTaxRegionId($region->getRegionId());
  69. $this->taxRateRepository->save($taxRateData);
  70. }
  71. }
  72. $this->moduleDataSetup->getConnection()->endSetup();
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. public static function getDependencies()
  78. {
  79. return [
  80. UpdateTaxClassAttributeVisibility::class
  81. ];
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public static function getVersion()
  87. {
  88. return '2.0.3';
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function getAliases()
  94. {
  95. return [];
  96. }
  97. /**
  98. * Parse region from tax code.
  99. *
  100. * @param string $taxCode
  101. * @return string
  102. */
  103. private function parseRegionFromTaxCode($taxCode)
  104. {
  105. $result = '';
  106. $parts = explode('-', $taxCode, 3);
  107. if (isset($parts[1])) {
  108. $result = $parts[1];
  109. }
  110. return $result;
  111. }
  112. }