added faulty owner view

This commit is contained in:
Luca Haid
2025-05-20 13:44:55 +02:00
parent 90987baec0
commit 288727f465
9 changed files with 506 additions and 4 deletions

View File

@@ -237,4 +237,179 @@ class ConstructionConsentProject extends mfBaseModel {
//var_dump($filter, $where);exit;
return $where;
}
/**
* Checks if there are faulty owner entries in the Construction Consent system
* for a specific project
*
* A faulty entry is defined as one where:
* - Title is empty AND (first name is empty OR last name is empty OR city is empty OR zip code is invalid)
* - For Austrian addresses, zip code must be exactly 4 digits
* - For other countries, zip code must not be empty
*
* @param int $projectId The ID of the construction consent project to check
* @return bool Returns true if faulty entries exist, false otherwise
*/
public static function hasFaultyOwnerEntries(int $projectId): bool {
if (empty($projectId)) return false;
$db = FronkDB::singleton();
$sql = "SELECT 1
FROM ConstructionConsentOwner cco
JOIN ConstructionConsent cc ON cc.id = cco.constructionconsent_id
WHERE cc.constructionconsentproject_id = $projectId
AND (
(
(
(cco.company IS NOT NULL AND TRIM(cco.company) <> '') OR
(
(cco.firstname IS NOT NULL AND TRIM(cco.firstname) <> '') AND
(cco.lastname IS NOT NULL AND TRIM(cco.lastname) <> '')
)
) OR
(cco.city IS NULL OR TRIM(cco.city) = '')
)
AND
(
(
(LOWER(cco.country) = 'österreich' OR LOWER(cco.country) = 'austria') AND
(cco.zip IS NULL OR cco.zip NOT REGEXP '^[0-9]{4}')
)
OR
/* For other countries: ZIP must not be empty */
(
(LOWER(cco.country) != 'österreich' AND LOWER(cco.country) != 'austria') AND
(cco.zip IS NULL OR TRIM(cco.zip) = '')
)
)
)
LIMIT 1";
$res = $db->query($sql);
return ($res->num_rows > 0);
}
public static function getFaultyOwnerEntries($projectId = null) {
$faultyEntries = [];
$db = FronkDB::singleton();
$whereClause = "";
if (!empty($projectId)) {
$projectId = (int)$projectId;
$whereClause = "WHERE cc.constructionconsentproject_id = $projectId";
}
$sql = "
SELECT
cco.id as owner_id,
cco.firstname,
cco.lastname,
cco.title,
cco.street,
cco.zip,
cco.city,
cco.country,
cco.email,
cco.phone,
cco.status,
cco.result,
cc.id as consent_id,
cc.name as building_name,
cc.object_type,
ccp.id as project_id,
ccp.name as project_name,
cco.create,
cco.edit
FROM
ConstructionConsentOwner cco
JOIN
ConstructionConsent cc ON cc.id = cco.constructionconsent_id
JOIN
ConstructionConsentProject ccp ON ccp.id = cc.constructionconsentproject_id
$whereClause
AND (
(
(cco.company IS NOT NULL AND TRIM(cco.company) <> '') OR
(
(cco.firstname IS NOT NULL AND TRIM(cco.firstname) <> '') AND
(cco.lastname IS NOT NULL AND TRIM(cco.lastname) <> '')
)
) OR
(cco.city IS NULL OR TRIM(cco.city) = '')
) -- Fixed parenthesis balance here
AND (
(
(LOWER(cco.country) = 'österreich' OR LOWER(cco.country) = 'austria') AND
(cco.zip IS NULL OR cco.zip NOT REGEXP '^[0-9]{4}')
)
OR
(
(LOWER(cco.country) != 'österreich' AND LOWER(cco.country) != 'austria') AND
(cco.zip IS NULL OR TRIM(cco.zip) = '')
)
)
ORDER BY
ccp.name, cc.name, cco.lastname, cco.firstname
";
$res = $db->query($sql);
while($data = $res->fetch_assoc()) {
$errors = [];
// Check if company is empty, OR if both firstname and lastname are empty
if (empty(trim($data['company']))) {
if (empty(trim($data['firstname']))) {
$errors[] = 'firstname';
}
if (empty(trim($data['lastname']))) {
$errors[] = 'lastname';
}
}
// Existing city check (assuming it's still relevant for faulty entries)
if (empty(trim($data['city']))) {
$errors[] = 'city';
}
// Check ZIP based on country
$isAustria = (strtolower($data['country']) === 'österreich' || strtolower($data['country']) === 'austria');
if ($isAustria && (!isset($data['zip']) || !preg_match('/^[0-9]{4}$/', $data['zip']))) {
$errors[] = 'zip';
} elseif (!$isAustria && empty(trim($data['zip']))) {
$errors[] = 'zip';
}
$faultyEntries[] = [
'owner_id' => $data['owner_id'],
'consent_id' => $data['consent_id'],
'project_id' => $data['project_id'],
'project_name' => $data['project_name'],
'building_name' => $data['building_name'],
'object_type' => $data['object_type'],
'title' => $data['title'],
'firstname' => $data['firstname'],
'lastname' => $data['lastname'],
'street' => $data['street'],
'zip' => $data['zip'],
'city' => $data['city'],
'country' => $data['country'],
'email' => $data['email'],
'phone' => $data['phone'],
'status' => $data['status'],
'result' => $data['result'],
'create' => $data['create'],
'edit' => $data['edit'],
'errors' => $errors
];
}
return $faultyEntries;
}
}