Skip to content

Commit

Permalink
feat(Resolve address): use field dalsiUdaje
Browse files Browse the repository at this point in the history
  • Loading branch information
h4kuna committed Nov 10, 2024
1 parent b15be7e commit e6d9a52
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 18 deletions.
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) : [];
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

0 comments on commit e6d9a52

Please sign in to comment.