PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC ] ); $results = [ 'consents_created' => 0, 'owners_created' => 0, 'errors' => [] ]; // Process each entry foreach ($entries as $index => $entry) { try { // Normalize data $kg = (int)$entry['kg']; $gst = (string)$entry['gst']; $ez = (string)$entry['ez']; $street = substr($entry['street'], 0, 255); // Find or create ConstructionConsent $stmt = $pdo->prepare(" SELECT id FROM ConstructionConsent WHERE kg = ? AND gst = ? AND ez = ? "); $stmt->execute([$kg, $gst, $ez]); $consent = $stmt->fetch(); if (!$consent) { $stmt = $pdo->prepare(" INSERT INTO ConstructionConsent ( constructionconsentproject_id, adb_strasse_id, object_type, name, ez, kg, gst, create_by, edit_by, `create`, `edit` ) VALUES ( 1, ?, ?, ?, ?, ?, ?, 145, 145, UNIX_TIMESTAMP(), UNIX_TIMESTAMP() ) "); $stmt->execute([137947, "street", "GST: " . $gst, $ez, $kg, $gst]); $consent_id = $pdo->lastInsertId(); $results['consents_created']++; } else { $consent_id = $consent['id']; } // Prepare owner data $firstname = !isset($entry['firstname']) ? null : substr($entry['firstname'], 0, 255); $lastname = substr($entry['lastname'] ?? '', 0, 255); $street = substr($entry['street'], 0, 64); $zip = substr($entry['plz'], 0, 32); $city = substr($entry['ort'], 0, 64); // Determine owner status and result $status = match(strtolower($entry['status'])) { 'sent' => 'sent', 'disallowed' => 'returned', 'signed' => 'returned', default => 'new' }; $result = match(strtolower($entry['status'])) { 'disallowed' => 'denied', 'signed' => 'accepted', default => 'open' }; // Check existing owner $stmt = $pdo->prepare(" SELECT id FROM ConstructionConsentOwner WHERE constructionconsent_id = ? AND ( (firstname = ? AND lastname = ? AND street = ?) OR (firstname IS NULL AND lastname = ? AND street = ?)) "); $stmt->execute([$consent_id, $firstname, $lastname, $street, $lastname, $street]); if (!$stmt->fetch()) { $stmt = $pdo->prepare(" INSERT INTO ConstructionConsentOwner ( constructionconsent_id, firstname, lastname, street, zip, city, status, result, create_by, edit_by, `create`, `edit` ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, 145, 145, UNIX_TIMESTAMP(), UNIX_TIMESTAMP() ) "); $stmt->execute([ $consent_id, $firstname, $lastname, $street, $zip, $city, $status, $result ]); $results['owners_created']++; } } catch (Exception $e) { $results['errors'][] = [ 'index' => $index, 'message' => $e->getMessage(), 'entry' => $entry ]; } } // Show results echo "

Processing Results:

"; echo "

New consents created: " . $results['consents_created'] . "

"; echo "

New owners created: " . $results['owners_created'] . "

"; if (!empty($results['errors'])) { echo "

Errors:

"; foreach ($results['errors'] as $error) { echo "

Error at index " . $error['index'] . ": " . $error['message'] . "

"; echo "
" . print_r($error['entry'], true) . "
"; } } } catch (Exception $e) { echo "

Error: " . $e->getMessage() . "

"; } } ?> Construction Consent Importer

Upload Construction Consent JSON