11
11
use Webkul \DataTransfer \Repositories \ImportBatchRepository ;
12
12
use Webkul \Tax \Repositories \TaxRateRepository ;
13
13
14
- class TaxRate extends AbstractImporter
14
+ class Importer extends AbstractImporter
15
15
{
16
16
/**
17
17
* Error code for non existing identifier
@@ -28,7 +28,7 @@ class TaxRate extends AbstractImporter
28
28
*/
29
29
protected array $ validColumnNames = [
30
30
'identifier ' ,
31
- 'is_zip ' ,
31
+ 'is_zip_range ' ,
32
32
'zip_code ' ,
33
33
'zip_from ' ,
34
34
'zip_to ' ,
@@ -128,13 +128,13 @@ public function validateRow(array $rowData, int $rowNumber): bool
128
128
* Validate product attributes
129
129
*/
130
130
$ validator = Validator::make ($ rowData , [
131
- 'identifier ' => 'required|string ' ,
132
- 'is_zip ' => 'sometimes ' ,
133
- 'zip_code ' => 'nullable ' ,
134
- 'zip_from ' => 'nullable|required_with:is_zip ' ,
135
- 'zip_to ' => 'nullable|required_with:is_zip,zip_from ' ,
136
- 'country ' => 'required|string ' ,
137
- 'tax_rate ' => 'required|numeric|min:0.0001 ' ,
131
+ 'identifier ' => 'required|string ' ,
132
+ 'is_zip_range ' => 'sometimes|boolean ' ,
133
+ 'zip_code ' => 'nullable|required_if:is_zip_range,0 ' ,
134
+ 'zip_from ' => 'nullable|required_if:is_zip_range,1 ' ,
135
+ 'zip_to ' => 'nullable|required_if:is_zip_range,1 ' ,
136
+ 'country ' => 'required|string ' ,
137
+ 'tax_rate ' => 'required|numeric|min:0.0001 ' ,
138
138
]);
139
139
140
140
if ($ validator ->fails ()) {
@@ -240,34 +240,16 @@ protected function saveTaxRatesData(ImportBatchContract $batch): bool
240
240
/**
241
241
* Prepare tax rates for import
242
242
*/
243
- $ this ->prepareTaxRates ($ rowData , $ taxRates );
244
- }
245
-
246
- $ this ->saveTaxRates ($ taxRates );
247
-
248
- return true ;
249
- }
250
-
251
- /**
252
- * Prepare tax rates from current batch
253
- */
254
- public function prepareTaxRates (array $ rowData , array &$ taxRates ): void
255
- {
256
- if ($ this ->isIdentifierExist ($ rowData ['identifier ' ])) {
257
- $ taxRates ['update ' ][$ rowData ['identifier ' ]] = $ rowData ;
258
- } else {
259
- $ taxRates ['insert ' ][$ rowData ['identifier ' ]] = array_merge ($ rowData , [
260
- 'created_at ' => $ rowData ['created_at ' ] ?? now (),
261
- 'updated_at ' => $ rowData ['updated_at ' ] ?? now (),
262
- ]);
243
+ if ($ this ->isIdentifierExist ($ rowData ['identifier ' ])) {
244
+ $ taxRates ['update ' ][$ rowData ['identifier ' ]] = $ rowData ;
245
+ } else {
246
+ $ taxRates ['insert ' ][$ rowData ['identifier ' ]] = array_merge ($ rowData , [
247
+ 'created_at ' => $ rowData ['created_at ' ] ?? now (),
248
+ 'updated_at ' => $ rowData ['updated_at ' ] ?? now (),
249
+ ]);
250
+ }
263
251
}
264
- }
265
252
266
- /**
267
- * Save tax rates from current batch
268
- */
269
- public function saveTaxRates (array $ taxRates ): void
270
- {
271
253
if (! empty ($ taxRates ['update ' ])) {
272
254
$ this ->updatedItemsCount += count ($ taxRates ['update ' ]);
273
255
@@ -282,6 +264,8 @@ public function saveTaxRates(array $taxRates): void
282
264
283
265
$ this ->taxRateRepository ->insert ($ taxRates ['insert ' ]);
284
266
}
267
+
268
+ return true ;
285
269
}
286
270
287
271
/**
@@ -291,4 +275,16 @@ public function isIdentifierExist(string $identifier): bool
291
275
{
292
276
return $ this ->taxRateStorage ->has ($ identifier );
293
277
}
278
+
279
+ /**
280
+ * Prepare row data to save into the database
281
+ */
282
+ protected function prepareRowForDb (array $ rowData ): array
283
+ {
284
+ $ rowData = parent ::prepareRowForDb ($ rowData );
285
+
286
+ $ rowData ['is_zip ' ] = $ rowData ['is_zip_range ' ] ?? 0 ;
287
+
288
+ return Arr::except ($ rowData , 'is_zip_range ' );
289
+ }
294
290
}
0 commit comments