Add AdvowareAkte and AIKnowledge creation for Kündigungen; synchronize between Kündigungen and Räumungsklagen; update localization and metadata files

This commit is contained in:
2026-03-23 21:29:26 +01:00
parent 672645673f
commit ea4738d9eb
19 changed files with 502 additions and 18 deletions

View File

@@ -264,7 +264,14 @@ class CVmhRumungsklage extends \Espo\Services\Record
->relate($beklagter);
}
// 7. Collect all documents from Mietverhältnisse, Kündigungen, Mietobjekte and Beteiligte
// 7. Create or link AdvowareAkte and AIKnowledge (BEFORE document duplication!)
$this->createOrLinkAdvowareAkteAndAIKnowledge(
$raeumungsklage,
$alleKuendigungen,
$raeumungsklagenRepo
);
// 8. Collect all documents from Mietverhältnisse, Kündigungen, Mietobjekte and Beteiligte
$alleLinkedDokumente = [];
// Get CDokumente service for duplication
@@ -391,6 +398,128 @@ class CVmhRumungsklage extends \Espo\Services\Record
];
}
/**
* Create or link AdvowareAkte and create AIKnowledge for Räumungsklage
*
* @param object $raeumungsklage The created Räumungsklage entity
* @param array $alleKuendigungen All related Kündigungen
* @param object $raeumungsklagenRepo Repository for relations
*/
private function createOrLinkAdvowareAkteAndAIKnowledge(
$raeumungsklage,
array $alleKuendigungen,
$raeumungsklagenRepo
): void {
$advowareAkte = null;
// 1. Check if any Kündigung has an existing AdvowareAkte (belongsTo relationship - get via field)
foreach ($alleKuendigungen as $kuendigung) {
$existingAkteId = $kuendigung->get('advowareAktenId');
if ($existingAkteId) {
$existingAkte = $this->entityManager->getEntity('CAdvowareAkten', $existingAkteId);
if ($existingAkte) {
$advowareAkte = $existingAkte;
$GLOBALS['log']->info("CVmhRumungsklage: Using existing AdvowareAkte from Kündigung: {$existingAkte->getId()}");
break; // Use first found Akte
}
}
}
// 2. If no existing Akte found, create new one
if (!$advowareAkte) {
// Collect Aktennummer and Aktenzeichen from Kündigungen
$aktennummer = null;
$aktenzeichen = null;
foreach ($alleKuendigungen as $kuendigung) {
if (!$aktennummer && $kuendigung->get('aktennr')) {
$aktennummer = $kuendigung->get('aktennr');
}
if (!$aktenzeichen && $kuendigung->get('advowareAktenzeichen')) {
$aktenzeichen = $kuendigung->get('advowareAktenzeichen');
}
if ($aktennummer && $aktenzeichen) {
break; // Found both
}
}
// Generate if not found
if (!$aktennummer) {
$aktennummer = time(); // Simple timestamp-based generation
}
if (!$aktenzeichen) {
$aktenzeichen = 'AZ-' . date('Y-m-d-His');
}
// Create new AdvowareAkte
$advowareAkteData = [
'name' => 'Advoware Akte - ' . $raeumungsklage->get('name'),
'aktennummer' => $aktennummer,
'aktenzeichen' => $aktenzeichen,
'syncStatus' => 'unclean',
'assignedUserId' => $raeumungsklage->get('assignedUserId')
];
// Copy teams
$teamsIds = $raeumungsklage->getLinkMultipleIdList('teams');
if (!empty($teamsIds)) {
$advowareAkteData['teamsIds'] = $teamsIds;
}
$advowareAkte = $this->entityManager->createEntity('CAdvowareAkten', $advowareAkteData);
if ($advowareAkte) {
$GLOBALS['log']->info("CVmhRumungsklage: Created new AdvowareAkte: {$advowareAkte->getId()}");
// Link new Akte to ALL Kündigungen (belongsTo relationship - set field directly)
foreach ($alleKuendigungen as $kuendigung) {
try {
$kuendigung->set('advowareAktenId', $advowareAkte->getId());
$this->entityManager->saveEntity($kuendigung);
$GLOBALS['log']->info("CVmhRumungsklage: Linked new AdvowareAkte to Kündigung: {$kuendigung->getId()}");
} catch (\Exception $e) {
$GLOBALS['log']->warning("CVmhRumungsklage: Could not link AdvowareAkte to Kündigung: " . $e->getMessage());
}
}
} else {
$GLOBALS['log']->error('CVmhRumungsklage: Failed to create AdvowareAkte');
}
}
// 3. Link AdvowareAkte to Räumungsklage (hasOne relationship - set field directly)
if ($advowareAkte) {
$raeumungsklage->set('advowareAktenId', $advowareAkte->getId());
$this->entityManager->saveEntity($raeumungsklage);
$GLOBALS['log']->info("CVmhRumungsklage: Linked AdvowareAkte to Räumungsklage");
}
// 4. Create AIKnowledge
$aiKnowledgeData = [
'name' => 'AI Knowledge - ' . $raeumungsklage->get('name'),
'aktivierungsstatus' => 'deactivated',
'syncStatus' => 'unclean',
'assignedUserId' => $raeumungsklage->get('assignedUserId')
];
// Copy teams
$teamsIds = $raeumungsklage->getLinkMultipleIdList('teams');
if (!empty($teamsIds)) {
$aiKnowledgeData['teamsIds'] = $teamsIds;
}
$aiKnowledge = $this->entityManager->createEntity('CAIKnowledge', $aiKnowledgeData);
if ($aiKnowledge) {
// Link AIKnowledge to Räumungsklage (hasOne relationship - set field directly)
$raeumungsklage->set('aIKnowledgeId', $aiKnowledge->getId());
$this->entityManager->saveEntity($raeumungsklage);
$GLOBALS['log']->info("CVmhRumungsklage: Created and linked AIKnowledge: {$aiKnowledge->getId()}");
} else {
$GLOBALS['log']->error('CVmhRumungsklage: Failed to create AIKnowledge');
}
}
/**
* Log Räumungsklage creation to source entity stream
*