From e6d9a52956a0051bf8bef8669dc898cd3e4f68e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milan=20Mat=C4=9Bj=C4=8Dek?= Date: Sun, 10 Nov 2024 11:20:10 +0100 Subject: [PATCH] feat(Resolve address): use field dalsiUdaje --- src/Ares/Core/JsonToDataTransformer.php | 85 +++++++++++++++++++------ tests/fixtures/ares/26005492.json | 44 +++++++++++++ tests/src/E2E/Ares/CoreTest.php | 1 + 3 files changed, 112 insertions(+), 18 deletions(-) create mode 100644 tests/fixtures/ares/26005492.json diff --git a/src/Ares/Core/JsonToDataTransformer.php b/src/Ares/Core/JsonToDataTransformer.php index b1e1729..bb2b63a 100644 --- a/src/Ares/Core/JsonToDataTransformer.php +++ b/src/Ares/Core/JsonToDataTransformer.php @@ -9,6 +9,8 @@ class JsonToDataTransformer { + private const RegisterPriority = ['rzp', 'res', 'vr']; + public function transform(stdClass $json): Data { @@ -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; @@ -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 @@ -82,4 +104,31 @@ private static function isAddressFilled(Data $data): bool || $data->house_number !== null; } + + /** + * @param array $dalsiUdaje + * @return array + */ + 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"; + } + } diff --git a/tests/fixtures/ares/26005492.json b/tests/fixtures/ares/26005492.json new file mode 100644 index 0000000..189a531 --- /dev/null +++ b/tests/fixtures/ares/26005492.json @@ -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" + } +} \ No newline at end of file diff --git a/tests/src/E2E/Ares/CoreTest.php b/tests/src/E2E/Ares/CoreTest.php index 757f549..eb2da26 100644 --- a/tests/src/E2E/Ares/CoreTest.php +++ b/tests/src/E2E/Ares/CoreTest.php @@ -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