5
5
*/
6
6
namespace Magento \TaxSampleData \Model ;
7
7
8
+ use Magento \Framework \App \ObjectManager ;
8
9
use Magento \Framework \Setup \SampleData \Context as SampleDataContext ;
9
10
10
11
/**
11
12
* Class Tax
13
+ *
14
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
12
15
*/
13
16
class Tax
14
17
{
@@ -57,6 +60,13 @@ class Tax
57
60
*/
58
61
protected $ csvReader ;
59
62
63
+ /**
64
+ * Region collection factory.
65
+ *
66
+ * @var \Magento\Directory\Model\ResourceModel\Region\CollectionFactory
67
+ */
68
+ private $ regionCollectionFactory ;
69
+
60
70
/**
61
71
* @param SampleDataContext $sampleDataContext
62
72
* @param \Magento\Tax\Api\TaxRuleRepositoryInterface $taxRuleRepository
@@ -66,6 +76,7 @@ class Tax
66
76
* @param \Magento\Tax\Model\Calculation\RateFactory $taxRateFactory
67
77
* @param \Magento\Framework\Api\SearchCriteriaBuilder $criteriaBuilder
68
78
* @param \Magento\Framework\Api\FilterBuilder $filterBuilder
79
+ * @param \Magento\Directory\Model\ResourceModel\Region\CollectionFactory $regionCollectionFactory
69
80
*/
70
81
public function __construct (
71
82
SampleDataContext $ sampleDataContext ,
@@ -75,7 +86,8 @@ public function __construct(
75
86
\Magento \Tax \Api \Data \TaxRateInterfaceFactory $ rateFactory ,
76
87
\Magento \Tax \Model \Calculation \RateFactory $ taxRateFactory ,
77
88
\Magento \Framework \Api \SearchCriteriaBuilder $ criteriaBuilder ,
78
- \Magento \Framework \Api \FilterBuilder $ filterBuilder
89
+ \Magento \Framework \Api \FilterBuilder $ filterBuilder ,
90
+ \Magento \Directory \Model \ResourceModel \Region \CollectionFactory $ regionCollectionFactory = null
79
91
) {
80
92
$ this ->fixtureManager = $ sampleDataContext ->getFixtureManager ();
81
93
$ this ->csvReader = $ sampleDataContext ->getCsvReader ();
@@ -86,12 +98,28 @@ public function __construct(
86
98
$ this ->taxRateFactory = $ taxRateFactory ;
87
99
$ this ->criteriaBuilder = $ criteriaBuilder ;
88
100
$ this ->filterBuilder = $ filterBuilder ;
101
+ $ this ->regionCollectionFactory = $ regionCollectionFactory ?: ObjectManager::getInstance ()->get (
102
+ \Magento \Directory \Model \ResourceModel \Region \CollectionFactory::class
103
+ );
89
104
}
90
105
91
106
/**
92
107
* {@inheritdoc}
93
108
*/
94
109
public function install (array $ fixtures )
110
+ {
111
+ $ this ->createTaxRates ($ fixtures );
112
+ $ this ->createTaxRules ();
113
+ }
114
+
115
+ /**
116
+ * Create tax rates.
117
+ *
118
+ * @param array $fixtures
119
+ * @return void
120
+ * @throws \Exception if something went wrong while saving the tax rate.
121
+ */
122
+ private function createTaxRates (array $ fixtures )
95
123
{
96
124
foreach ($ fixtures as $ fileName ) {
97
125
$ fileName = $ this ->fixtureManager ->getFixture ($ fileName );
@@ -111,19 +139,28 @@ public function install(array $fixtures)
111
139
continue ;
112
140
}
113
141
$ taxRate = $ this ->rateFactory ->create ();
142
+ $ regionId = $ this ->getRegionId ($ data ['tax_region_name ' ], $ data ['tax_country_id ' ]);
114
143
$ taxRate ->setCode ($ data ['code ' ])
115
144
->setTaxCountryId ($ data ['tax_country_id ' ])
116
- ->setTaxRegionId ($ data [ ' tax_region_id ' ] )
145
+ ->setTaxRegionId ($ regionId )
117
146
->setTaxPostcode ($ data ['tax_postcode ' ])
118
147
->setRate ($ data ['rate ' ]);
119
148
$ this ->taxRateRepository ->save ($ taxRate );
120
149
}
150
+ }
151
+ }
121
152
122
- $ fixtureFile = 'Magento_TaxSampleData::fixtures/tax_rule.csv ' ;
123
- $ fixtureFileName = $ this ->fixtureManager ->getFixture ($ fixtureFile );
124
- if (!file_exists ($ fileName )) {
125
- continue ;
126
- }
153
+ /**
154
+ * Create tax rules.
155
+ *
156
+ * @return void
157
+ * @throws \Exception if something went wrong while saving the tax rule.
158
+ */
159
+ private function createTaxRules ()
160
+ {
161
+ $ fixtureFile = 'Magento_TaxSampleData::fixtures/tax_rule.csv ' ;
162
+ $ fixtureFileName = $ this ->fixtureManager ->getFixture ($ fixtureFile );
163
+ if (file_exists ($ fixtureFileName )) {
127
164
128
165
$ rows = $ this ->csvReader ->getData ($ fixtureFileName );
129
166
$ header = array_shift ($ rows );
@@ -155,6 +192,22 @@ public function install(array $fixtures)
155
192
$ this ->taxRuleRepository ->save ($ taxRule );
156
193
}
157
194
}
195
+ }
196
+
197
+ /**
198
+ * Return region Id by code or name.
199
+ *
200
+ * @param string $region
201
+ * @param string $countryId
202
+ * @return string|null
203
+ */
204
+ private function getRegionId ($ region , $ countryId )
205
+ {
206
+ $ regionCollection = $ this ->regionCollectionFactory ->create ();
207
+ $ regionCollection ->addCountryFilter ($ countryId )
208
+ ->addRegionCodeOrNameFilter ($ region )
209
+ ->setPageSize (1 );
158
210
211
+ return $ regionCollection ->getFirstItem ()->getId ();
159
212
}
160
213
}
0 commit comments