Skip to content

Commit 04364a4

Browse files
committed
chore: tariff codes improvements
1 parent e6610ae commit 04364a4

8 files changed

Lines changed: 120 additions & 67 deletions

File tree

resources/lang/en/tariff-codes.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
],
4444

4545
'form' => [
46-
'basic' => 'Basic',
4746
'year' => [
4847
'label' => 'Year',
4948
],
@@ -58,6 +57,12 @@
5857
],
5958
],
6059

60+
'validation' => [
61+
'code' => [
62+
'unique' => 'A tariff code with this code already exists.',
63+
],
64+
],
65+
6166
'import' => [
6267
'action_label' => 'Import Tariff Codes',
6368
'modal_heading' => 'Import CN tariff codes',

resources/lang/hr/tariff-codes.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
],
4444

4545
'form' => [
46-
'basic' => 'Osnovno',
4746
'year' => [
4847
'label' => 'Godina',
4948
],
@@ -58,6 +57,12 @@
5857
],
5958
],
6059

60+
'validation' => [
61+
'code' => [
62+
'unique' => 'Carinska šifra s ovom šifrom već postoji.',
63+
],
64+
],
65+
6166
'import' => [
6267
'action_label' => 'Uvoz carinskih šifara',
6368
'modal_heading' => 'Uvoz CN carinskih šifara',

resources/lang/sl/tariff-codes.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
],
4444

4545
'form' => [
46-
'basic' => 'Osnovno',
4746
'year' => [
4847
'label' => 'Leto',
4948
],
@@ -58,6 +57,12 @@
5857
],
5958
],
6059

60+
'validation' => [
61+
'code' => [
62+
'unique' => 'Carinska oznaka s to kodo že obstaja.',
63+
],
64+
],
65+
6166
'import' => [
6267
'action_label' => 'Uvozi carinske oznake',
6368
'modal_heading' => 'Uvoz CN carinskih oznak',

resources/lang/sr/tariff-codes.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
],
4444

4545
'form' => [
46-
'basic' => 'Osnovno',
4746
'year' => [
4847
'label' => 'Godina',
4948
],
@@ -58,6 +57,12 @@
5857
],
5958
],
6059

60+
'validation' => [
61+
'code' => [
62+
'unique' => 'Carinska šifra s ovom šifrom već postoji.',
63+
],
64+
],
65+
6166
'import' => [
6267
'action_label' => 'Uvoz carinskih šifara',
6368
'modal_heading' => 'Uvoz CN carinskih šifara',

src/Filament/Clusters/World/Resources/TariffCodeResource.php

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use BezhanSalleh\FilamentShield\Contracts\HasShieldPermissions;
66
use Eclipse\World\Filament\Clusters\World;
77
use Eclipse\World\Models\TariffCode;
8-
use Filament\Forms\Components\Section;
98
use Filament\Forms\Components\TextInput;
109
use Filament\Forms\Form;
1110
use Filament\Resources\Concerns\Translatable;
@@ -40,24 +39,27 @@ class TariffCodeResource extends Resource implements HasShieldPermissions
4039
public static function form(Form $form): Form
4140
{
4241
return $form->schema([
43-
Section::make(__('eclipse-world::tariff-codes.form.basic', [], app()->getLocale()) ?: 'Basic')
44-
->compact()
45-
->schema([
46-
TextInput::make('year')
47-
->numeric()
48-
->required()
49-
->default((int) date('Y')),
50-
TextInput::make('code')
51-
->maxLength(20)
52-
->required(),
53-
TextInput::make('name')
54-
->label(__('eclipse-world::tariff-codes.form.name.label'))
55-
->required(),
56-
TextInput::make('measure_unit')
57-
->label(__('eclipse-world::tariff-codes.form.measure_unit.label'))
58-
->nullable(),
59-
])->columns(2),
60-
]);
42+
TextInput::make('code')
43+
->maxLength(20)
44+
->required()
45+
->unique(
46+
table: 'world_tariff_codes',
47+
column: 'code',
48+
ignoreRecord: true,
49+
modifyRuleUsing: function ($rule) {
50+
return $rule->where('year', (int) date('Y'));
51+
}
52+
)
53+
->validationMessages([
54+
'unique' => __('eclipse-world::tariff-codes.validation.code.unique'),
55+
]),
56+
TextInput::make('name')
57+
->label(__('eclipse-world::tariff-codes.form.name.label'))
58+
->required(),
59+
TextInput::make('measure_unit')
60+
->label(__('eclipse-world::tariff-codes.form.measure_unit.label'))
61+
->nullable(),
62+
])->columns(2);
6163
}
6264

6365
public static function table(Table $table): Table
@@ -67,7 +69,6 @@ public static function table(Table $table): Table
6769
->defaultSort('code')
6870
->striped()
6971
->columns([
70-
TextColumn::make('year')->label(__('eclipse-world::tariff-codes.table.year.label'))->sortable()->width(90),
7172
TextColumn::make('code')->label(__('eclipse-world::tariff-codes.table.code.label'))->searchable()->sortable()->width(160),
7273
TextColumn::make('name')
7374
->label(__('eclipse-world::tariff-codes.table.name.label'))

src/Jobs/ImportTariffCodes.php

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,30 @@ protected function execute(): void
9898

9999
$multiReader = $this->csv($multiPath);
100100

101+
$codeNamesLookup = [];
102+
foreach ($multiReader->getRecords() as $row) {
103+
$raw = isset($row['CN_CODE']) ? trim((string) $row['CN_CODE']) : null;
104+
$code = $raw !== null ? $this->normalizeCode($raw) : null;
105+
if ($code === null || $code === '') {
106+
continue;
107+
}
108+
109+
foreach ($this->locales as $locale) {
110+
$col = 'NAME_'.strtoupper($locale);
111+
if (array_key_exists($col, $row)) {
112+
$val = trim((string) $row[$col]);
113+
if ($val !== '') {
114+
if (! isset($codeNamesLookup[$locale])) {
115+
$codeNamesLookup[$locale] = [];
116+
}
117+
$codeNamesLookup[$locale][$code] = $val;
118+
}
119+
}
120+
}
121+
}
122+
123+
$multiReader = $this->csv($multiPath);
124+
101125
$chunk = [];
102126
$chunkSize = 200;
103127
$codesInChunk = [];
@@ -123,7 +147,7 @@ protected function execute(): void
123147
if (array_key_exists($col, $row)) {
124148
$val = trim((string) $row[$col]);
125149
if ($val !== '') {
126-
$chunk[$code]['name'][$locale] = $val;
150+
$chunk[$code]['name'][$locale] = $this->transformCnName($val, $code, $codeNamesLookup[$locale] ?? []);
127151
}
128152
}
129153
}
@@ -277,4 +301,39 @@ private function normalizeCode(string $code): string
277301
{
278302
return str_replace(' ', '', $code);
279303
}
304+
305+
/**
306+
* Transform CN name by removing leading dashes and building hierarchical name.
307+
*/
308+
private function transformCnName(string $name, string $code, array $codeNamesLookup): string
309+
{
310+
$name = ltrim($name, '-');
311+
312+
if (empty($name)) {
313+
return $code;
314+
}
315+
316+
$hierarchicalParts = [];
317+
318+
$hierarchicalParts[] = ucfirst($name);
319+
320+
$currentCode = $code;
321+
while (strlen($currentCode) > 2) {
322+
$parentCode = substr($currentCode, 0, -2);
323+
if (strlen($parentCode) >= 2) {
324+
$parentName = $codeNamesLookup[$parentCode] ?? null;
325+
if ($parentName) {
326+
$cleanParentName = ltrim($parentName, '-');
327+
if (! empty($cleanParentName)) {
328+
$hierarchicalParts[] = ucfirst($cleanParentName);
329+
}
330+
}
331+
}
332+
$currentCode = $parentCode;
333+
}
334+
335+
$hierarchicalParts = array_reverse($hierarchicalParts);
336+
337+
return implode(' > ', $hierarchicalParts);
338+
}
280339
}

src/Models/TariffCode.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,18 @@ protected static function newFactory(): TariffCodeFactory
5151
{
5252
return TariffCodeFactory::new();
5353
}
54+
55+
/**
56+
* Boot the model.
57+
*/
58+
protected static function boot()
59+
{
60+
parent::boot();
61+
62+
static::creating(function ($tariffCode) {
63+
if (empty($tariffCode->year)) {
64+
$tariffCode->year = (int) date('Y');
65+
}
66+
});
67+
}
5468
}

tests/Feature/TariffCodeResourceTest.php

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ public static function getAvailableLocales()
7474

7575
// Test with valid data
7676
$validData = [
77-
'year' => (int) date('Y'),
7877
'code' => '0101',
7978
'name' => ['en' => 'Live horses, asses, mules and hinnies'],
8079
'measure_unit' => ['en' => 'pcs'],
@@ -86,7 +85,6 @@ public static function getAvailableLocales()
8685

8786
test('new tariff code can be created', function () {
8887
$data = [
89-
'year' => (int) date('Y'),
9088
'code' => '0101',
9189
'name' => ['en' => 'Live horses, asses, mules and hinnies'],
9290
'measure_unit' => ['en' => 'pcs'],
@@ -97,20 +95,18 @@ public static function getAvailableLocales()
9795
->assertHasNoActionErrors();
9896

9997
$tariffCode = TariffCode::where('code', $data['code'])
100-
->where('year', $data['year'])
10198
->first();
10299

103100
expect($tariffCode)->toBeObject();
104101

105-
expect($tariffCode->year)->toEqual($data['year']);
102+
expect($tariffCode->year)->toEqual((int) date('Y'));
106103
expect($tariffCode->code)->toEqual($data['code']);
107104
expect($tariffCode->name)->toEqual($data['name']);
108105
expect($tariffCode->measure_unit)->toEqual($data['measure_unit']);
109106
});
110107

111108
test('existing tariff code can be updated', function () {
112109
$tariffCode = TariffCode::factory()->create([
113-
'year' => (int) date('Y'),
114110
'code' => '0101',
115111
'name' => ['en' => 'Live horses, asses, mules and hinnies'],
116112
'measure_unit' => ['en' => 'pcs'],
@@ -172,14 +168,12 @@ public static function getAvailableLocales()
172168

173169
// Create first tariff code
174170
$firstTariffCode = TariffCode::factory()->create([
175-
'year' => $year,
176171
'code' => '0101',
177172
'name' => ['en' => 'Live horses'],
178173
]);
179174

180175
// Try to create duplicate year-code combination
181176
$duplicateData = [
182-
'year' => $year,
183177
'code' => '0101',
184178
'name' => ['en' => 'Different name'],
185179
];
@@ -197,50 +191,16 @@ public static function getAvailableLocales()
197191
->toBe(1);
198192
});
199193

200-
test('can create same code for different years', function () {
201-
$year1 = (int) date('Y');
202-
$year2 = $year1 - 1;
203-
204-
// Create tariff code with same code for first year
205-
$tariffCode1Data = [
206-
'year' => $year1,
207-
'code' => '0101',
208-
'name' => ['en' => 'Live horses'],
209-
];
210-
211-
livewire(ListTariffCodes::class)
212-
->callAction('create', $tariffCode1Data)
213-
->assertHasNoActionErrors();
214-
215-
// Create tariff code with same code for second year (should work)
216-
$tariffCode2Data = [
217-
'year' => $year2,
218-
'code' => '0101',
219-
'name' => ['en' => 'Live horses old'],
220-
];
221-
222-
livewire(ListTariffCodes::class)
223-
->callAction('create', $tariffCode2Data)
224-
->assertHasNoActionErrors();
225-
226-
// Verify both tariff codes exist
227-
expect(TariffCode::where('code', '0101')->count())->toBe(2);
228-
expect(TariffCode::where('year', $year1)->where('code', '0101')->count())->toBe(1);
229-
expect(TariffCode::where('year', $year2)->where('code', '0101')->count())->toBe(1);
230-
});
231-
232194
test('updating tariff code respects unique constraint', function () {
233195
$year = (int) date('Y');
234196

235197
// Create two tariff codes
236198
$tariffCode1 = TariffCode::factory()->create([
237-
'year' => $year,
238199
'code' => '0101',
239200
'name' => ['en' => 'Live horses'],
240201
]);
241202

242203
$tariffCode2 = TariffCode::factory()->create([
243-
'year' => $year,
244204
'code' => '0102',
245205
'name' => ['en' => 'Live cattle'],
246206
]);
@@ -256,7 +216,6 @@ public static function getAvailableLocales()
256216
$year = (int) date('Y');
257217

258218
$tariffCode = TariffCode::factory()->create([
259-
'year' => $year,
260219
'code' => '0101',
261220
'name' => ['en' => 'Live horses'],
262221
]);

0 commit comments

Comments
 (0)