AddTaxAttributeAndTaxClasses.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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\App\ResourceConnection;
  9. use Magento\Framework\Setup\Patch\DataPatchInterface;
  10. use Magento\Framework\Setup\Patch\PatchVersionInterface;
  11. use Magento\Tax\Setup\TaxSetup;
  12. use Magento\Tax\Setup\TaxSetupFactory;
  13. /**
  14. * Class AddTacAttributeAndTaxClasses
  15. * @package Magento\Tax\Setup\Patch
  16. */
  17. class AddTaxAttributeAndTaxClasses implements DataPatchInterface, PatchVersionInterface
  18. {
  19. /**
  20. * @param TaxSetupFactory $taxSetupFactory
  21. */
  22. private $taxSetupFactory;
  23. /**
  24. * @param RegionFactory $directoryRegionFactory
  25. */
  26. private $directoryRegionFactory;
  27. /**
  28. * @var \Magento\Framework\Setup\ModuleDataSetupInterface
  29. */
  30. private $moduleDataSetup;
  31. /**
  32. * AddTacAttributeAndTaxClasses constructor.
  33. * @param TaxSetupFactory $taxSetupFactory
  34. * @param RegionFactory $directoryRegionFactory
  35. * @param \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup
  36. */
  37. public function __construct(
  38. TaxSetupFactory $taxSetupFactory,
  39. RegionFactory $directoryRegionFactory,
  40. \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup
  41. ) {
  42. $this->taxSetupFactory = $taxSetupFactory;
  43. $this->directoryRegionFactory = $directoryRegionFactory;
  44. $this->moduleDataSetup = $moduleDataSetup;
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function apply()
  50. {
  51. /** @var TaxSetup $taxSetup */
  52. $taxSetup = $this->taxSetupFactory->create(['resourceName' => 'tax_setup', 'setup' => $this->moduleDataSetup]);
  53. /**
  54. * Add tax_class_id attribute to the 'eav_attribute' table
  55. */
  56. $taxSetup->addAttribute(
  57. \Magento\Catalog\Model\Product::ENTITY,
  58. 'tax_class_id',
  59. [
  60. 'group' => 'Product Details',
  61. 'sort_order' => 40,
  62. 'type' => 'int',
  63. 'backend' => '',
  64. 'frontend' => '',
  65. 'label' => 'Tax Class',
  66. 'input' => 'select',
  67. 'class' => '',
  68. 'source' => \Magento\Tax\Model\TaxClass\Source\Product::class,
  69. 'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_WEBSITE,
  70. 'visible' => true,
  71. 'required' => false,
  72. 'user_defined' => false,
  73. 'default' => '2',
  74. 'searchable' => true,
  75. 'filterable' => false,
  76. 'comparable' => false,
  77. 'visible_on_front' => false,
  78. 'visible_in_advanced_search' => false,
  79. 'used_in_product_listing' => true,
  80. 'unique' => false,
  81. 'apply_to' => implode(',', $taxSetup->getTaxableItems()),
  82. 'is_used_in_grid' => true,
  83. 'is_visible_in_grid' => false,
  84. 'is_filterable_in_grid' => true,
  85. ]
  86. );
  87. /**
  88. * install tax classes
  89. */
  90. $data = [
  91. [
  92. 'class_id' => 2,
  93. 'class_name' => 'Taxable Goods',
  94. 'class_type' => \Magento\Tax\Model\ClassModel::TAX_CLASS_TYPE_PRODUCT,
  95. ],
  96. [
  97. 'class_id' => 3,
  98. 'class_name' => 'Retail Customer',
  99. 'class_type' => \Magento\Tax\Model\ClassModel::TAX_CLASS_TYPE_CUSTOMER
  100. ],
  101. ];
  102. foreach ($data as $row) {
  103. $this->moduleDataSetup->getConnection()->insertForce(
  104. $this->moduleDataSetup->getTable('tax_class'),
  105. $row
  106. );
  107. }
  108. /**
  109. * install tax calculation rates
  110. */
  111. /** @var \Magento\Directory\Model\Region $region */
  112. $region = $this->directoryRegionFactory->create();
  113. $data = [
  114. [
  115. 'tax_calculation_rate_id' => 1,
  116. 'tax_country_id' => 'US',
  117. 'tax_region_id' => $region->loadByCode('CA', 'US')->getRegionId(),
  118. 'tax_postcode' => '*',
  119. 'code' => 'US-CA-*-Rate 1',
  120. 'rate' => '8.2500',
  121. ],
  122. [
  123. 'tax_calculation_rate_id' => 2,
  124. 'tax_country_id' => 'US',
  125. 'tax_region_id' => $region->loadByCode('NY', 'US')->getRegionId(),
  126. 'tax_postcode' => '*',
  127. 'code' => 'US-NY-*-Rate 1',
  128. 'rate' => '8.3750'
  129. ],
  130. ];
  131. foreach ($data as $row) {
  132. $this->moduleDataSetup->getConnection()->insertForce(
  133. $this->moduleDataSetup->getTable('tax_calculation_rate'),
  134. $row
  135. );
  136. }
  137. }
  138. /**
  139. * {@inheritdoc}
  140. */
  141. public static function getDependencies()
  142. {
  143. return [];
  144. }
  145. /**
  146. * {@inheritdoc}
  147. */
  148. public static function getVersion()
  149. {
  150. return '2.0.0';
  151. }
  152. /**
  153. * {@inheritdoc}
  154. */
  155. public function getAliases()
  156. {
  157. return [];
  158. }
  159. }