Skip to content

Commit f3dd253

Browse files
authored
Merge pull request #42 from h4kuna/ic-26005492
feat(Resolve address): use field dalsiUdaje
2 parents b15be7e + e6d9a52 commit f3dd253

File tree

3 files changed

+112
-18
lines changed

3 files changed

+112
-18
lines changed

src/Ares/Core/JsonToDataTransformer.php

+67-18
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
class JsonToDataTransformer
1111
{
12+
private const RegisterPriority = ['rzp', 'res', 'vr'];
13+
1214

1315
public function transform(stdClass $json): Data
1416
{
@@ -24,22 +26,7 @@ public function transform(stdClass $json): Data
2426
$data->vat_payer = $data->sources[Sources::SER_NO_DPH] === true;
2527
$data->company = Strings::trimNull($json->obchodniJmeno ?? null);
2628

27-
$addressExists = isset($json->sidlo) && self::updateAddress($data, $json->sidlo);
28-
29-
if ($addressExists === false && isset($json->dalsiUdaje[0]->sidlo[0]->sidlo)) {
30-
$addressExists = self::updateAddress($data, $json->dalsiUdaje[0]->sidlo[0]->sidlo);
31-
}
32-
33-
if ($addressExists === false && isset($json->sidlo->textovaAdresa)) {
34-
[
35-
'zip' => $data->zip,
36-
'street' => $data->street,
37-
'house_number' => $data->house_number,
38-
'city' => $data->city,
39-
'country' => $country,
40-
] = Helper::parseAddress($json->sidlo->textovaAdresa);
41-
$data->country ??= $country;
42-
}
29+
self::resolveAddress($data, $json);
4330

4431
$data->nace = (array) ($json->czNace ?? []);
4532
$data->legal_form_code = (int) $json->pravniForma;
@@ -69,10 +56,45 @@ private static function updateAddress(Data $data, stdClass $sidlo): bool
6956
}
7057

7158

59+
private static function resolveAddress(Data $data, stdClass $json): void
60+
{
61+
$addressExists = isset($json->sidlo) && self::updateAddress($data, $json->sidlo);
62+
63+
if ($addressExists === false) {
64+
$additionalData = isset($json->dalsiUdaje) ? self::prepareForAddress($json->dalsiUdaje) : [];
65+
if ($additionalData !== []) {
66+
foreach (self::RegisterPriority as $register) {
67+
$key = self::keyForAddress($register, $json->pravniForma);
68+
if (isset($additionalData[$key])) {
69+
$addressExists = self::updateAddress($data, $additionalData[$key]);
70+
if ($addressExists === true) {
71+
break;
72+
}
73+
}
74+
}
75+
}
76+
}
77+
78+
if ($addressExists === false && isset($json->sidlo->textovaAdresa)) {
79+
[
80+
'zip' => $data->zip,
81+
'street' => $data->street,
82+
'house_number' => $data->house_number,
83+
'city' => $data->city,
84+
'country' => $country,
85+
] = Helper::parseAddress($json->sidlo->textovaAdresa);
86+
$data->country ??= $country;
87+
}
88+
}
89+
90+
7291
private static function isAddressFilled(Data $data): bool
7392
{
74-
return $data->zip !== null
75-
|| $data->street !== null
93+
if ($data->zip === null) {
94+
return false;
95+
}
96+
97+
return $data->street !== null
7698
|| $data->country !== null
7799
|| $data->country_code !== null
78100
|| $data->city !== null
@@ -82,4 +104,31 @@ private static function isAddressFilled(Data $data): bool
82104
|| $data->house_number !== null;
83105
}
84106

107+
108+
/**
109+
* @param array<stdClass> $dalsiUdaje
110+
* @return array<stdClass>
111+
*/
112+
private static function prepareForAddress(array $dalsiUdaje): array
113+
{
114+
$out = [];
115+
foreach ($dalsiUdaje as $record) {
116+
$x = self::keyForAddress($record->datovyZdroj, $record->pravniForma);
117+
foreach ($record->sidlo ?? [] as $sidlo) {
118+
if ($sidlo?->primarniZaznam === true && isset($sidlo->sidlo)) {
119+
$out[$x] = $sidlo->sidlo;
120+
break;
121+
}
122+
}
123+
}
124+
125+
return $out;
126+
}
127+
128+
129+
private static function keyForAddress(string $datovyZdroj, string $pravniForma): string
130+
{
131+
return "$datovyZdroj|$pravniForma";
132+
}
133+
85134
}

tests/fixtures/ares/26005492.json

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"active": true,
3+
"city": "Olešnice",
4+
"company": "TANUS s.r.o.",
5+
"created": "2004-02-28T00:00:00+01:00",
6+
"dissolved": null,
7+
"city_district": "Hoděčín",
8+
"city_post": null,
9+
"in": "26005492",
10+
"is_person": false,
11+
"legal_form_code": 112,
12+
"house_number": "26",
13+
"street": null,
14+
"district": "Rychnov nad Kněžnou",
15+
"tin": "CZ26005492",
16+
"vat_payer": true,
17+
"zip": "51721",
18+
"country": "Česká republika",
19+
"country_code": "CZ",
20+
"nace": [
21+
"74",
22+
"461",
23+
"772",
24+
"791",
25+
"47790",
26+
"49410"
27+
],
28+
"sources": {
29+
"stavZdrojeVr": true,
30+
"stavZdrojeRes": true,
31+
"stavZdrojeRzp": true,
32+
"stavZdrojeNrpzs": "NEEXISTUJICI",
33+
"stavZdrojeRpsh": "NEEXISTUJICI",
34+
"stavZdrojeRcns": "NEEXISTUJICI",
35+
"stavZdrojeSzr": "NEEXISTUJICI",
36+
"stavZdrojeDph": true,
37+
"stavZdrojeSd": "NEEXISTUJICI",
38+
"stavZdrojeIr": "NEEXISTUJICI",
39+
"stavZdrojeCeu": "NEEXISTUJICI",
40+
"stavZdrojeRs": "NEEXISTUJICI",
41+
"stavZdrojeRed": true,
42+
"stavZdrojeMonitor": "NEEXISTUJICI"
43+
}
44+
}

tests/src/E2E/Ares/CoreTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ protected static function getMask(): string
2828
protected function provideCore(): array
2929
{
3030
return [
31+
['26005492'], // read address from sidlo
3132
['26577321'], // address from dalsiUdaje[0]->sidlo[0]->sidlo
3233
['25528351'], // diff address
3334
['67909442'], // create date does not exist

0 commit comments

Comments
 (0)