diff --git a/application/Espo/Modules/Crm/Repositories/Meeting.php b/application/Espo/Modules/Crm/Repositories/Meeting.php index 0184b06de7..b949019c5c 100644 --- a/application/Espo/Modules/Crm/Repositories/Meeting.php +++ b/application/Espo/Modules/Crm/Repositories/Meeting.php @@ -36,16 +36,18 @@ class Meeting extends \Espo\Core\Repositories\Event { protected function beforeSave(Entity $entity, array $options = array()) { + if (!$entity->isNew() && $entity->isAttributeChanged('parentId')) { + $entity->set('accountId', null); + } + $parentId = $entity->get('parentId'); $parentType = $entity->get('parentType'); - if (!empty($parentId) || !empty($parentType)) { + if ($parentId && $parentType) { $parent = $this->getEntityManager()->getEntity($parentType, $parentId); - if (!empty($parent)) { + if ($parent) { $accountId = null; if ($parent->getEntityType() == 'Account') { $accountId = $parent->id; - } else if ($parent->get('accountId')) { - $accountId = $parent->get('accountId'); } else if ($parent->getEntityType() == 'Lead') { if ($parent->get('status') == 'Converted') { if ($parent->get('createdAccountId')) { @@ -53,7 +55,10 @@ class Meeting extends \Espo\Core\Repositories\Event } } } - if (!empty($accountId)) { + if (!$accountId && $parent->get('accountId') && $parent->getRelationParam('account', 'entity') == 'Account') { + $accountId = $parent->get('accountId'); + } + if ($accountId) { $entity->set('accountId', $accountId); } } diff --git a/application/Espo/Modules/Crm/Repositories/Task.php b/application/Espo/Modules/Crm/Repositories/Task.php index ddb32c1ffc..0ccc7ac75e 100644 --- a/application/Espo/Modules/Crm/Repositories/Task.php +++ b/application/Espo/Modules/Crm/Repositories/Task.php @@ -103,26 +103,46 @@ class Task extends \Espo\Core\Repositories\Event } } + if (!$entity->isNew() && $entity->isAttributeChanged('parentId')) { + $entity->set('accountId', null); + $entity->set('contactId', null); + } + $parentId = $entity->get('parentId'); $parentType = $entity->get('parentType'); - if (!empty($parentId) || !empty($parentType)) { + if ($parentId && $parentType) { $parent = $this->getEntityManager()->getEntity($parentType, $parentId); - if (!empty($parent)) { + if ($parent) { $accountId = null; + $contactId = null; if ($parent->getEntityType() == 'Account') { $accountId = $parent->id; - } else if ($parent->get('accountId')) { - $accountId = $parent->get('accountId'); } else if ($parent->getEntityType() == 'Lead') { if ($parent->get('status') == 'Converted') { if ($parent->get('createdAccountId')) { $accountId = $parent->get('createdAccountId'); } + if ($parent->get('createdContactId')) { + $contactId = $parent->get('createdContactId'); + } } + } else if ($parent->getEntityType() == 'Contact') { + $contactId = $parent->id; } - if (!empty($accountId)) { + + if (!$accountId && $parent->get('accountId') && $parent->getRelationParam('account', 'entity') == 'Account') { + $accountId = $parent->get('accountId'); + } + if (!$contactId && $parent->get('contactId') && $parent->getRelationParam('contact', 'entity') == 'Contact') { + $contactId = $parent->get('contactId'); + } + + if ($accountId) { $entity->set('accountId', $accountId); } + if ($contactId) { + $entity->set('contactId', $contactId); + } } } diff --git a/application/Espo/Modules/Crm/Resources/i18n/en_US/Contact.json b/application/Espo/Modules/Crm/Resources/i18n/en_US/Contact.json index c85c3df114..90b86b9d7c 100644 --- a/application/Espo/Modules/Crm/Resources/i18n/en_US/Contact.json +++ b/application/Espo/Modules/Crm/Resources/i18n/en_US/Contact.json @@ -31,6 +31,7 @@ "account": "Account (Primary)", "accounts": "Accounts", "casesPrimary": "Cases (Primary)", + "tasksPrimary": "Tasks (expanded)", "portalUser": "Portal User", "originalLead": "Original Lead", "documents": "Documents" diff --git a/application/Espo/Modules/Crm/Resources/i18n/en_US/Task.json b/application/Espo/Modules/Crm/Resources/i18n/en_US/Task.json index 93d9981b6d..c3adf414c2 100644 --- a/application/Espo/Modules/Crm/Resources/i18n/en_US/Task.json +++ b/application/Espo/Modules/Crm/Resources/i18n/en_US/Task.json @@ -16,7 +16,9 @@ "reminders": "Reminders" }, "links": { - "attachments": "Attachments" + "attachments": "Attachments", + "account": "Account", + "contact": "Contact" }, "options": { "status": { diff --git a/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Contact.json b/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Contact.json index c6cede1479..e26e4ac7e2 100644 --- a/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Contact.json +++ b/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Contact.json @@ -395,6 +395,12 @@ "entity": "Document", "foreign": "contacts", "audited": true + }, + "tasksPrimary": { + "type": "hasMany", + "entity": "Task", + "foreign": "contact", + "layoutRelationshipsDisabled": true } }, "collection": { diff --git a/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Task.json b/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Task.json index d32248a9dd..b7535a4310 100644 --- a/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Task.json +++ b/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Task.json @@ -66,6 +66,10 @@ "type": "link", "readOnly": true }, + "contact": { + "type": "link", + "readOnly": true + }, "createdAt": { "type": "datetime", "readOnly": true @@ -126,6 +130,10 @@ "account": { "type": "belongsTo", "entity": "Account" + }, + "contact": { + "type": "belongsTo", + "entity": "Contact" } }, "collection": { diff --git a/application/Espo/Repositories/Email.php b/application/Espo/Repositories/Email.php index 0ce50ee4b6..83e852e010 100644 --- a/application/Espo/Repositories/Email.php +++ b/application/Espo/Repositories/Email.php @@ -249,17 +249,23 @@ class Email extends \Espo\Core\ORM\Repositories\RDB $entity->setLinkMultipleColumn('users', 'isRead', $entity->get('createdById'), true); } + if (!$entity->isNew() && $entity->isAttributeChanged('parentId')) { + $entity->set('accountId', null); + } + $parentId = $entity->get('parentId'); $parentType = $entity->get('parentType'); - if (!empty($parentId) || !empty($parentType)) { + if ($parentId && $parentType) { $parent = $this->getEntityManager()->getEntity($parentType, $parentId); - if (!empty($parent)) { + if ($parent) { + $accountId = null; if ($parent->getEntityType() == 'Account') { $accountId = $parent->id; - } else if ($parent->has('accountId')) { + } + if (!$accountId && $parent->get('accountId') && $parent->getRelationParam('account', 'entity') == 'Account') { $accountId = $parent->get('accountId'); } - if (!empty($accountId)) { + if ($accountId) { $account = $this->getEntityManager()->getEntity('Account', $accountId); if ($account) { $entity->set('accountId', $accountId);