diff --git a/application/ConstructionConsent/ConstructionConsentController.php b/application/ConstructionConsent/ConstructionConsentController.php index da26b9bec..383a85f43 100644 --- a/application/ConstructionConsent/ConstructionConsentController.php +++ b/application/ConstructionConsent/ConstructionConsentController.php @@ -1055,41 +1055,44 @@ class ConstructionConsentController extends mfBaseController { foreach ($importData as $ownerData) { $consentFilter = ["constructionconsentproject_id" => $consentProject->id, "kg" => $ownerData["KG-EZ"] ?? null, "ez" => $ownerData["EZ"] ?? null, "lnr" => $ownerData["LNR"] ?? null]; - if (!($consentRecord = ConstructionConsent::getFirst(array_filter($consentFilter)))) { + if (!($consentRecords = ConstructionConsent::search(array_filter($consentFilter)))) { $counts["skipped"]++; continue; } - $birthdate = (isset($ownerData["GEB"]) && preg_match("/^\d{4}-\d{2}-\d{2}$/", $ownerData["GEB"])) ? $ownerData["GEB"] : null; - $ownerFilter = ["constructionconsent_id" => $consentRecord->id, "firstname" => $ownerData["VN"] ?? null, "lastname" => $ownerData["NN"] ?? null]; - if ($birthdate) { $ownerFilter["birthdate"] = $birthdate; } + foreach ($consentRecords as $consentRecord) { + $birthdate = (isset($ownerData["GEB"]) && preg_match("/^\d{4}-\d{2}-\d{2}$/", $ownerData["GEB"])) ? $ownerData["GEB"] : null; + $ownerFilter = ["constructionconsent_id" => $consentRecord->id, "firstname" => $ownerData["VN"] ?? null, "lastname" => $ownerData["NN"] ?? null]; + if ($birthdate) { $ownerFilter["birthdate"] = $birthdate; } + + $isNew = !($ownerRecord = ConstructionConsentOwner::getFirst(array_filter($ownerFilter))); + if ($isNew) { + $ownerRecord = new ConstructionConsentOwner(); + $ownerRecord->constructionconsent_id = $consentRecord->id; + $ownerRecord->create_by = $this->me->id; + } + + $addressKey = null; foreach(array_keys($ownerData) as $k) { if(strpos($k,"ADR") === 0) {$addressKey=$k; break;} } + $street = $city = $postcode = $country = null; + if ($addressKey && isset($ownerData[$addressKey])) { + $addressParts = explode(", ", str_replace('"', '', $ownerData[$addressKey])); + $street = $addressParts[0] ?? null; + if (isset($addressParts[1])) { list($postcode, $city) = array_pad(explode(" ", trim($addressParts[1]), 2), 2, null); } + $country = $addressParts[2] ?? null; + } + + $ownerRecord->firstname = $ownerData["VN"] ?? null; + $ownerRecord->lastname = (($ownerData["JUR"] ?? 'nein') === "ja") ? ($ownerData['BEZ'] ?? $ownerData["NN"]) : ($ownerData["NN"] ?? null); + $ownerRecord->birthdate = $birthdate; + $ownerRecord->street = $street; + $ownerRecord->city = $city; + $ownerRecord->zip = $postcode; + $ownerRecord->country = $country; + $ownerRecord->edit_by = $this->me->id; + $ownerRecord->save(); + + $counts[$isNew ? "created" : "updated"]++; - $isNew = !($ownerRecord = ConstructionConsentOwner::getFirst(array_filter($ownerFilter))); - if ($isNew) { - $ownerRecord = new ConstructionConsentOwner(); - $ownerRecord->constructionconsent_id = $consentRecord->id; - $ownerRecord->create_by = $this->me->id; } - - $addressKey = null; foreach(array_keys($ownerData) as $k) { if(strpos($k,"ADR") === 0) {$addressKey=$k; break;} } - $street = $city = $postcode = $country = null; - if ($addressKey && isset($ownerData[$addressKey])) { - $addressParts = explode(", ", str_replace('"', '', $ownerData[$addressKey])); - $street = $addressParts[0] ?? null; - if (isset($addressParts[1])) { list($postcode, $city) = array_pad(explode(" ", trim($addressParts[1]), 2), 2, null); } - $country = $addressParts[2] ?? null; - } - - $ownerRecord->firstname = $ownerData["VN"] ?? null; - $ownerRecord->lastname = (($ownerData["JUR"] ?? 'nein') === "ja") ? ($ownerData['BEZ'] ?? $ownerData["NN"]) : ($ownerData["NN"] ?? null); - $ownerRecord->birthdate = $birthdate; - $ownerRecord->street = $street; - $ownerRecord->city = $city; - $ownerRecord->zip = $postcode; - $ownerRecord->country = $country; - $ownerRecord->edit_by = $this->me->id; - $ownerRecord->save(); - - $counts[$isNew ? "created" : "updated"]++; } self::returnJson(["status" => "OK", "counts" => $counts]);