diff --git a/application/Espo/Modules/Crm/Repositories/Meeting.php b/application/Espo/Modules/Crm/Repositories/Meeting.php index 54e9684492..a825ae5195 100644 --- a/application/Espo/Modules/Crm/Repositories/Meeting.php +++ b/application/Espo/Modules/Crm/Repositories/Meeting.php @@ -42,16 +42,35 @@ class Meeting extends \Espo\Core\Repositories\Event $parentId = $entity->get('parentId'); $parentType = $entity->get('parentType'); - if ($parentId && $parentType) { - $parent = $this->getEntityManager()->getEntity($parentType, $parentId); + + if ($entity->isAttributeChanged('parentId') || $entity->isAttributeChanged('parentType')) { + $parent = null; + if ($parentId && $parentType) { + if ($this->getEntityManager()->hasRepository($parentType)) { + $columnList = ['id', 'name']; + if ($this->getEntityManager()->getMetadata()->get($parentType, ['fields', 'accountId'])) { + $columnList[] = 'accountId'; + } + if ($parentType === 'Lead') { + $columnList[] = 'status'; + $columnList[] = 'createdAccountId'; + $columnList[] = 'createdAccountName'; + } + $parent = $this->getEntityManager()->getRepository($parentType)->select($columnList)->get($parentId); + } + } + $accountId = null; + $accountName = null; + if ($parent) { - $accountId = null; if ($parent->getEntityType() == 'Account') { $accountId = $parent->id; + $accountName = $parent->get('name'); } else if ($parent->getEntityType() == 'Lead') { if ($parent->get('status') == 'Converted') { if ($parent->get('createdAccountId')) { $accountId = $parent->get('createdAccountId'); + $accountName = $parent->get('createdAccountName'); } } } @@ -60,6 +79,18 @@ class Meeting extends \Espo\Core\Repositories\Event } if ($accountId) { $entity->set('accountId', $accountId); + $entity->set('accountName', $accountName); + } + } + + if ( + $entity->get('accountId') + && + !$entity->get('accountName') + ) { + $account = $this->getEntityManager()->getRepository('Account')->select(['id', 'name'])->get($entity->get('accountId')); + if ($account) { + $entity->set('accountName', $account->get('name')); } } } diff --git a/application/Espo/Modules/Crm/Repositories/Task.php b/application/Espo/Modules/Crm/Repositories/Task.php index 92a92d694f..670c33934b 100644 --- a/application/Espo/Modules/Crm/Repositories/Task.php +++ b/application/Espo/Modules/Crm/Repositories/Task.php @@ -106,28 +106,58 @@ class Task extends \Espo\Core\Repositories\Event if (!$entity->isNew() && $entity->isAttributeChanged('parentId')) { $entity->set('accountId', null); $entity->set('contactId', null); + $entity->set('accountName', null); + $entity->set('contactName', null); } $parentId = $entity->get('parentId'); $parentType = $entity->get('parentType'); - if ($parentId && $parentType) { - $parent = $this->getEntityManager()->getEntity($parentType, $parentId); + + if ($entity->isAttributeChanged('parentId') || $entity->isAttributeChanged('parentType')) { + $parent = null; + if ($parentId && $parentType) { + if ($this->getEntityManager()->hasRepository($parentType)) { + $columnList = ['id', 'name']; + if ($this->getEntityManager()->getMetadata()->get($parentType, ['fields', 'accountId'])) { + $columnList[] = 'accountId'; + } + if ($this->getEntityManager()->getMetadata()->get($parentType, ['fields', 'contactId'])) { + $columnList[] = 'contactId'; + } + if ($parentType === 'Lead') { + $columnList[] = 'status'; + $columnList[] = 'createdAccountId'; + $columnList[] = 'createdAccountName'; + $columnList[] = 'createdContactId'; + $columnList[] = 'createdContactName'; + } + $parent = $this->getEntityManager()->getRepository($parentType)->select($columnList)->get($parentId); + } + } + + $accountId = null; + $contactId = null; + $accountName = null; + $contactName = null; + if ($parent) { - $accountId = null; - $contactId = null; if ($parent->getEntityType() == 'Account') { $accountId = $parent->id; + $accountName = $parent->get('name'); } else if ($parent->getEntityType() == 'Lead') { if ($parent->get('status') == 'Converted') { if ($parent->get('createdAccountId')) { $accountId = $parent->get('createdAccountId'); + $accountName = $parent->get('createdAccountName'); } if ($parent->get('createdContactId')) { $contactId = $parent->get('createdContactId'); + $contactName = $parent->get('createdContactName'); } } } else if ($parent->getEntityType() == 'Contact') { $contactId = $parent->id; + $contactName = $parent->get('name'); } if (!$accountId && $parent->get('accountId') && $parent->getRelationParam('account', 'entity') == 'Account') { @@ -136,16 +166,38 @@ class Task extends \Espo\Core\Repositories\Event if (!$contactId && $parent->get('contactId') && $parent->getRelationParam('contact', 'entity') == 'Contact') { $contactId = $parent->get('contactId'); } + } - if ($accountId) { - $entity->set('accountId', $accountId); + $entity->set('accountId', $accountId); + $entity->set('accountName', $accountName); + + $entity->set('contactId', $contactId); + $entity->set('contactName', $contactName); + + if ( + $entity->get('accountId') + && + !$entity->get('accountName') + ) { + $account = $this->getEntityManager()->getRepository('Account')->select(['id', 'name'])->get($entity->get('accountId')); + if ($account) { + $entity->set('accountName', $account->get('name')); } - if ($contactId) { - $entity->set('contactId', $contactId); + } + + if ( + $entity->get('contactId') + && + !$entity->get('contactName') + ) { + $contact = $this->getEntityManager()->getRepository('Contact')->select(['id', 'name'])->get($entity->get('contactId')); + if ($contact) { + $entity->set('contactName', $contact->get('name')); } } } + parent::beforeSave($entity, $options); } } diff --git a/application/Espo/ORM/Metadata.php b/application/Espo/ORM/Metadata.php index 81d62e8f31..52db0916e4 100644 --- a/application/Espo/ORM/Metadata.php +++ b/application/Espo/ORM/Metadata.php @@ -38,12 +38,15 @@ class Metadata $this->data = $data; } - public function get($entityType) + public function get($entityType, $key = null, $default = null) { if (!array_key_exists($entityType, $this->data)) { return null; } - return $this->data[$entityType]; + $data = $this->data[$entityType]; + if (!$key) return $data; + + return \Espo\Core\Utils\Util::getValueByKey($data, $key, $default); } public function has($entityType)