Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Resolve address): use field dalsiUdaje #42

Merged
merged 1 commit into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 67 additions & 18 deletions src/Ares/Core/JsonToDataTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

class JsonToDataTransformer
{
private const RegisterPriority = ['rzp', 'res', 'vr'];


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

$addressExists = isset($json->sidlo) && self::updateAddress($data, $json->sidlo);

if ($addressExists === false && isset($json->dalsiUdaje[0]->sidlo[0]->sidlo)) {
$addressExists = self::updateAddress($data, $json->dalsiUdaje[0]->sidlo[0]->sidlo);
}

if ($addressExists === false && isset($json->sidlo->textovaAdresa)) {
[
'zip' => $data->zip,
'street' => $data->street,
'house_number' => $data->house_number,
'city' => $data->city,
'country' => $country,
] = Helper::parseAddress($json->sidlo->textovaAdresa);
$data->country ??= $country;
}
self::resolveAddress($data, $json);

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


private static function resolveAddress(Data $data, stdClass $json): void
{
$addressExists = isset($json->sidlo) && self::updateAddress($data, $json->sidlo);

if ($addressExists === false) {
$additionalData = isset($json->dalsiUdaje) ? self::prepareForAddress($json->dalsiUdaje) : [];
Copy link

@pionl pionl Nov 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pro lepší čitelnost bych to hodil do private funkce a využil bych early returns.

if ($additionalData !== []) {
foreach (self::RegisterPriority as $register) {
$key = self::keyForAddress($register, $json->pravniForma);
if (isset($additionalData[$key])) {
$addressExists = self::updateAddress($data, $additionalData[$key]);
if ($addressExists === true) {
break;
}
}
}
}
}

if ($addressExists === false && isset($json->sidlo->textovaAdresa)) {
[
'zip' => $data->zip,
'street' => $data->street,
'house_number' => $data->house_number,
'city' => $data->city,
'country' => $country,
] = Helper::parseAddress($json->sidlo->textovaAdresa);
$data->country ??= $country;
}
}


private static function isAddressFilled(Data $data): bool
{
return $data->zip !== null
|| $data->street !== null
if ($data->zip === null) {
return false;
}

return $data->street !== null
|| $data->country !== null
|| $data->country_code !== null
|| $data->city !== null
Expand All @@ -82,4 +104,31 @@ private static function isAddressFilled(Data $data): bool
|| $data->house_number !== null;
}


/**
* @param array<stdClass> $dalsiUdaje
* @return array<stdClass>
*/
private static function prepareForAddress(array $dalsiUdaje): array
{
$out = [];
foreach ($dalsiUdaje as $record) {
$x = self::keyForAddress($record->datovyZdroj, $record->pravniForma);
foreach ($record->sidlo ?? [] as $sidlo) {
if ($sidlo?->primarniZaznam === true && isset($sidlo->sidlo)) {
$out[$x] = $sidlo->sidlo;
break;
}
}
}

return $out;
}


private static function keyForAddress(string $datovyZdroj, string $pravniForma): string
{
return "$datovyZdroj|$pravniForma";
}

}
44 changes: 44 additions & 0 deletions tests/fixtures/ares/26005492.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"active": true,
"city": "Olešnice",
"company": "TANUS s.r.o.",
"created": "2004-02-28T00:00:00+01:00",
"dissolved": null,
"city_district": "Hoděčín",
"city_post": null,
"in": "26005492",
"is_person": false,
"legal_form_code": 112,
"house_number": "26",
"street": null,
"district": "Rychnov nad Kněžnou",
"tin": "CZ26005492",
"vat_payer": true,
"zip": "51721",
"country": "Česká republika",
"country_code": "CZ",
"nace": [
"74",
"461",
"772",
"791",
"47790",
"49410"
],
"sources": {
"stavZdrojeVr": true,
"stavZdrojeRes": true,
"stavZdrojeRzp": true,
"stavZdrojeNrpzs": "NEEXISTUJICI",
"stavZdrojeRpsh": "NEEXISTUJICI",
"stavZdrojeRcns": "NEEXISTUJICI",
"stavZdrojeSzr": "NEEXISTUJICI",
"stavZdrojeDph": true,
"stavZdrojeSd": "NEEXISTUJICI",
"stavZdrojeIr": "NEEXISTUJICI",
"stavZdrojeCeu": "NEEXISTUJICI",
"stavZdrojeRs": "NEEXISTUJICI",
"stavZdrojeRed": true,
"stavZdrojeMonitor": "NEEXISTUJICI"
}
}
1 change: 1 addition & 0 deletions tests/src/E2E/Ares/CoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ protected static function getMask(): string
protected function provideCore(): array
{
return [
['26005492'], // read address from sidlo
['26577321'], // address from dalsiUdaje[0]->sidlo[0]->sidlo
['25528351'], // diff address
['67909442'], // create date does not exist
Expand Down
Loading