feat: Add termination functionality for rental agreements
- Introduced new entity `CKuendigung` for managing terminations. - Added fields for termination details including date, reason, type, and status. - Implemented backend service to initiate terminations from rental agreements. - Created frontend handler for termination actions with confirmation dialog. - Updated metadata and layouts for `CKuendigung` to support new functionality. - Added internationalization support for English and German languages. - Enhanced existing entities to establish relationships with terminations.
This commit is contained in:
@@ -368,4 +368,140 @@ class CVmhMietverhltnis extends \Espo\Services\Record
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initiate termination (Kündigung) from Mietverhältnis
|
||||
*
|
||||
* @param string $mietverhaeltnisId
|
||||
* @return array
|
||||
* @throws NotFound
|
||||
* @throws Forbidden
|
||||
*/
|
||||
public function initiateTermination(string $mietverhaeltnisId): array
|
||||
{
|
||||
// 1. Load Mietverhältnis
|
||||
$mietverhaeltnis = $this->entityManager->getEntity('CVmhMietverhltnis', $mietverhaeltnisId);
|
||||
if (!$mietverhaeltnis) {
|
||||
throw new NotFound('Mietverhältnis not found');
|
||||
}
|
||||
|
||||
// 2. ACL Checks
|
||||
if (!$this->acl->check($mietverhaeltnis, 'read')) {
|
||||
throw new Forbidden('No read access to Mietverhältnis');
|
||||
}
|
||||
|
||||
if (!$this->acl->checkScope('CKuendigung', 'create')) {
|
||||
throw new Forbidden('No create access to Kündigung');
|
||||
}
|
||||
|
||||
// 3. Start Transaction
|
||||
$this->entityManager->getTransactionManager()->start();
|
||||
|
||||
try {
|
||||
// 4. Prepare data for new Kündigung
|
||||
$data = new \stdClass();
|
||||
$data->name = 'Kündigung - ' . $mietverhaeltnis->get('name');
|
||||
|
||||
// Copy assignedUser and teams
|
||||
if ($mietverhaeltnis->get('assignedUserId')) {
|
||||
$data->assignedUserId = $mietverhaeltnis->get('assignedUserId');
|
||||
}
|
||||
|
||||
$teamsIds = $mietverhaeltnis->getLinkMultipleIdList('teams');
|
||||
if (!empty($teamsIds)) {
|
||||
$data->teamsIds = $teamsIds;
|
||||
}
|
||||
|
||||
// Pre-fill dates from Mietverhältnis
|
||||
if ($mietverhaeltnis->get('auszugsfrist')) {
|
||||
$data->kuendigungsfrist = $mietverhaeltnis->get('auszugsfrist');
|
||||
}
|
||||
|
||||
// Pre-fill Kündigungsgrund from Mietverhältnis
|
||||
if ($mietverhaeltnis->get('kndigungsgrundWohnraum')) {
|
||||
$data->kuendigungsgrund = $mietverhaeltnis->get('kndigungsgrundWohnraum');
|
||||
}
|
||||
|
||||
// Pre-fill Besorgnis nicht rechtzeitiger Auszug
|
||||
if ($mietverhaeltnis->get('besorgnisNichtRechtzeitigerAuszug')) {
|
||||
$data->besorgnisNichtRechtzeitigerAuszug = $mietverhaeltnis->get('besorgnisNichtRechtzeitigerAuszug');
|
||||
}
|
||||
|
||||
// 5. Create Kündigung entity
|
||||
$kuendigung = $this->entityManager->createEntity('CKuendigung', (array)$data);
|
||||
|
||||
if (!$kuendigung) {
|
||||
throw new \RuntimeException('Failed to create Kündigung');
|
||||
}
|
||||
|
||||
$kuendigungRepo = $this->entityManager->getRepository('CKuendigung');
|
||||
|
||||
// 6. Link Mietverhältnis to Kündigung
|
||||
$kuendigungRepo
|
||||
->getRelation($kuendigung, 'vmhMietverhltnis')
|
||||
->relate($mietverhaeltnis);
|
||||
|
||||
// 7. Get and link Mietobjekt
|
||||
$mietobjekt = $this->entityManager
|
||||
->getRepository('CVmhMietverhltnis')
|
||||
->getRelation($mietverhaeltnis, 'vmhMietobjekt')
|
||||
->findOne();
|
||||
|
||||
if ($mietobjekt) {
|
||||
$kuendigungRepo
|
||||
->getRelation($kuendigung, 'mietobjekt')
|
||||
->relate($mietobjekt);
|
||||
}
|
||||
|
||||
// 8. Get Vermieter from Mietverhältnis
|
||||
$vermieterBeteiligte = $this->entityManager
|
||||
->getRepository('CVmhMietverhltnis')
|
||||
->getRelation($mietverhaeltnis, 'vmhbeteiligtevermieter')
|
||||
->find();
|
||||
|
||||
foreach ($vermieterBeteiligte as $vermieter) {
|
||||
// Link as Vermieter
|
||||
$kuendigungRepo
|
||||
->getRelation($kuendigung, 'vermieter')
|
||||
->relate($vermieter);
|
||||
}
|
||||
|
||||
// 9. Get Mieter from Mietverhältnis
|
||||
$mieterBeteiligte = $this->entityManager
|
||||
->getRepository('CVmhMietverhltnis')
|
||||
->getRelation($mietverhaeltnis, 'vmhbeteiligtemieter')
|
||||
->find();
|
||||
|
||||
foreach ($mieterBeteiligte as $mieter) {
|
||||
// Link as Mieter
|
||||
$kuendigungRepo
|
||||
->getRelation($kuendigung, 'mieter')
|
||||
->relate($mieter);
|
||||
}
|
||||
|
||||
// 10. Link Portal Contacts
|
||||
$portalContacts = $this->entityManager
|
||||
->getRepository('CVmhMietverhltnis')
|
||||
->getRelation($mietverhaeltnis, 'contactsMietverhltnis')
|
||||
->find();
|
||||
|
||||
foreach ($portalContacts as $contact) {
|
||||
$kuendigungRepo
|
||||
->getRelation($kuendigung, 'contactsKuendigung')
|
||||
->relate($contact);
|
||||
}
|
||||
|
||||
// 11. Commit transaction
|
||||
$this->entityManager->getTransactionManager()->commit();
|
||||
|
||||
return [
|
||||
'id' => $kuendigung->getId(),
|
||||
'name' => $kuendigung->get('name')
|
||||
];
|
||||
} catch (\Exception $e) {
|
||||
// Rollback on any error
|
||||
$this->entityManager->getTransactionManager()->rollback();
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user