From fa577a4fa9c2c6d6983457b81d836ebcf63ebaea Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Thu, 23 Feb 2023 12:40:41 +0200 Subject: [PATCH] ref --- .../Espo/Modules/Crm/Entities/Contact.php | 17 ++++ .../Espo/Modules/Crm/Entities/Lead.php | 7 ++ .../Modules/Crm/Hooks/Account/TargetList.php | 63 ++++++++++++ .../Modules/Crm/Hooks/Contact/Accounts.php | 96 +++++++++++++++++++ .../Modules/Crm/Hooks/Contact/TargetList.php | 63 ++++++++++++ .../Modules/Crm/Hooks/Lead/ConvertedAt.php | 62 ++++++++++++ .../Modules/Crm/Hooks/Lead/TargetList.php | 63 ++++++++++++ .../Espo/Modules/Crm/Repositories/Account.php | 18 +--- .../Espo/Modules/Crm/Repositories/Contact.php | 77 +-------------- .../Espo/Modules/Crm/Repositories/Lead.php | 32 +------ 10 files changed, 384 insertions(+), 114 deletions(-) create mode 100644 application/Espo/Modules/Crm/Hooks/Account/TargetList.php create mode 100644 application/Espo/Modules/Crm/Hooks/Contact/Accounts.php create mode 100644 application/Espo/Modules/Crm/Hooks/Contact/TargetList.php create mode 100644 application/Espo/Modules/Crm/Hooks/Lead/ConvertedAt.php create mode 100644 application/Espo/Modules/Crm/Hooks/Lead/TargetList.php diff --git a/application/Espo/Modules/Crm/Entities/Contact.php b/application/Espo/Modules/Crm/Entities/Contact.php index 735b081576..3ae7344f84 100644 --- a/application/Espo/Modules/Crm/Entities/Contact.php +++ b/application/Espo/Modules/Crm/Entities/Contact.php @@ -37,6 +37,9 @@ class Contact extends Person { public const ENTITY_TYPE = 'Contact'; + /** + * An assigned user. + */ public function getAssignedUser(): ?Link { /** @var ?Link */ @@ -52,15 +55,29 @@ class Contact extends Person return $this->getValueObject('account'); } + /** + * Accounts. + */ public function getAccounts(): LinkMultiple { /** @var LinkMultiple */ return $this->getValueObject('accounts'); } + /** + * Teams. + */ public function getTeams(): LinkMultiple { /** @var LinkMultiple */ return $this->getValueObject('teams'); } + + /** + * A title (for a primary account). + */ + public function getTitle(): ?string + { + return $this->get('title'); + } } diff --git a/application/Espo/Modules/Crm/Entities/Lead.php b/application/Espo/Modules/Crm/Entities/Lead.php index c997b9271d..0cd9b106ef 100644 --- a/application/Espo/Modules/Crm/Entities/Lead.php +++ b/application/Espo/Modules/Crm/Entities/Lead.php @@ -29,6 +29,7 @@ namespace Espo\Modules\Crm\Entities; +use Espo\Core\Field\DateTime; use Espo\Core\Field\Link; use Espo\Core\Field\LinkMultiple; @@ -129,4 +130,10 @@ class Lead extends \Espo\Core\Entities\Person /** @var ?Link */ return $this->getValueObject('createdOpportunity'); } + + public function getConvertedAt(): ?DateTime + { + /** @var ?DateTime */ + return $this->getValueObject('convertedAt'); + } } diff --git a/application/Espo/Modules/Crm/Hooks/Account/TargetList.php b/application/Espo/Modules/Crm/Hooks/Account/TargetList.php new file mode 100644 index 0000000000..ce3702e7e7 --- /dev/null +++ b/application/Espo/Modules/Crm/Hooks/Account/TargetList.php @@ -0,0 +1,63 @@ + + */ +class TargetList implements AfterSave +{ + public function __construct(private EntityManager $entityManager) {} + + public function afterSave(Entity $entity, SaveOptions $options): void + { + if (!$options->get(SaveOption::IMPORT)) { + return; + } + + $targetListId = $entity->get('targetListId'); + + if (!$targetListId) { + return; + } + + $this->entityManager + ->getRDBRepositoryByClass(Account::class) + ->getRelation($entity, 'targetLists') + ->relateById($targetListId); + } +} diff --git a/application/Espo/Modules/Crm/Hooks/Contact/Accounts.php b/application/Espo/Modules/Crm/Hooks/Contact/Accounts.php new file mode 100644 index 0000000000..dc388b70f6 --- /dev/null +++ b/application/Espo/Modules/Crm/Hooks/Contact/Accounts.php @@ -0,0 +1,96 @@ + + */ +class Accounts implements AfterSave +{ + public function __construct(private EntityManager $entityManager) {} + + /** + * @param Contact $entity + */ + public function afterSave(Entity $entity, SaveOptions $options): void + { + $accountIdChanged = $entity->isAttributeChanged('accountId'); + $titleChanged = $entity->isAttributeChanged('title'); + + /** @var ?string $fetchedAccountId */ + $fetchedAccountId = $entity->getFetched('accountId'); + $accountId = $entity->getAccount()?->getId(); + $title = $entity->getTitle(); + + $relation = $this->entityManager + ->getRDBRepositoryByClass(Contact::class) + ->getRelation($entity, 'accounts'); + + if (!$accountId && $fetchedAccountId) { + $relation->unrelateById($fetchedAccountId); + + return; + } + + if (!$accountIdChanged && !$titleChanged) { + return; + } + + if (!$accountId) { + return; + } + + $accountContact = $this->entityManager + ->getRDBRepository('AccountContact') + ->select(['role']) + ->where([ + 'accountId' => $accountId, + 'contactId' => $entity->getId(), + 'deleted' => false, + ]) + ->findOne(); + + if (!$accountContact && $accountIdChanged) { + $relation->relateById($accountId, ['role' => $title]); + + return; + } + + if ($titleChanged && $accountContact && $title !== $accountContact->get('role')) { + $relation->updateColumnsById($accountId, ['role' => $title]); + } + } +} diff --git a/application/Espo/Modules/Crm/Hooks/Contact/TargetList.php b/application/Espo/Modules/Crm/Hooks/Contact/TargetList.php new file mode 100644 index 0000000000..f829150b1b --- /dev/null +++ b/application/Espo/Modules/Crm/Hooks/Contact/TargetList.php @@ -0,0 +1,63 @@ + + */ +class TargetList implements AfterSave +{ + public function __construct(private EntityManager $entityManager) {} + + public function afterSave(Entity $entity, SaveOptions $options): void + { + if (!$options->get(SaveOption::IMPORT)) { + return; + } + + $targetListId = $entity->get('targetListId'); + + if (!$targetListId) { + return; + } + + $this->entityManager + ->getRDBRepositoryByClass(Contact::class) + ->getRelation($entity, 'targetLists') + ->relateById($targetListId); + } +} diff --git a/application/Espo/Modules/Crm/Hooks/Lead/ConvertedAt.php b/application/Espo/Modules/Crm/Hooks/Lead/ConvertedAt.php new file mode 100644 index 0000000000..34d5718e31 --- /dev/null +++ b/application/Espo/Modules/Crm/Hooks/Lead/ConvertedAt.php @@ -0,0 +1,62 @@ + + */ +class ConvertedAt implements BeforeSave +{ + /** + * @param Lead $entity + */ + public function beforeSave(Entity $entity, SaveOptions $options): void + { + if (!$entity->isAttributeChanged('status')) { + return; + } + + if ($entity->getConvertedAt() ) { + return; + } + + if ($entity->getStatus() !== Lead::STATUS_CONVERTED) { + return; + } + + $entity->setValueObject('convertedAt', DateTime::createNow()); + } +} diff --git a/application/Espo/Modules/Crm/Hooks/Lead/TargetList.php b/application/Espo/Modules/Crm/Hooks/Lead/TargetList.php new file mode 100644 index 0000000000..bad610546c --- /dev/null +++ b/application/Espo/Modules/Crm/Hooks/Lead/TargetList.php @@ -0,0 +1,63 @@ + + */ +class TargetList implements AfterSave +{ + public function __construct(private EntityManager $entityManager) {} + + public function afterSave(Entity $entity, SaveOptions $options): void + { + if (!$options->get(SaveOption::IMPORT)) { + return; + } + + $targetListId = $entity->get('targetListId'); + + if (!$targetListId) { + return; + } + + $this->entityManager + ->getRDBRepositoryByClass(Lead::class) + ->getRelation($entity, 'targetLists') + ->relateById($targetListId); + } +} diff --git a/application/Espo/Modules/Crm/Repositories/Account.php b/application/Espo/Modules/Crm/Repositories/Account.php index dda00fa218..bdf8706ba9 100644 --- a/application/Espo/Modules/Crm/Repositories/Account.php +++ b/application/Espo/Modules/Crm/Repositories/Account.php @@ -29,19 +29,11 @@ namespace Espo\Modules\Crm\Repositories; -use Espo\ORM\Entity; +use Espo\Core\Repositories\Database; +use Espo\Modules\Crm\Entities\Account as AccountEntity; /** - * @extends \Espo\Core\Repositories\Database<\Espo\Modules\Crm\Entities\Account> + * @extends Database */ -class Account extends \Espo\Core\Repositories\Database -{ - public function afterSave(Entity $entity, array $options = []) - { - parent::afterSave($entity, $options); - - if ($entity->has('targetListId')) { - $this->relate($entity, 'targetLists', $entity->get('targetListId')); - } - } -} +class Account extends Database +{} diff --git a/application/Espo/Modules/Crm/Repositories/Contact.php b/application/Espo/Modules/Crm/Repositories/Contact.php index 472800d1e0..3767e64f04 100644 --- a/application/Espo/Modules/Crm/Repositories/Contact.php +++ b/application/Espo/Modules/Crm/Repositories/Contact.php @@ -29,79 +29,10 @@ namespace Espo\Modules\Crm\Repositories; -use Espo\ORM\Entity; +use Espo\Core\Repositories\Database; /** - * @extends \Espo\Core\Repositories\Database<\Espo\Modules\Crm\Entities\Lead> + * @extends Database<\Espo\Modules\Crm\Entities\Contact> */ -class Contact extends \Espo\Core\Repositories\Database -{ - public function afterSave(Entity $entity, array $options = []) - { - parent::afterSave($entity, $options); - - $this->handleAfterSaveAccounts($entity); - - if ($entity->has('targetListId')) { - $this->relate($entity, 'targetLists', $entity->get('targetListId')); - } - } - - protected function handleAfterSaveAccounts(Entity $entity): void - { - $accountIdChanged = $entity->has('accountId') && - $entity->get('accountId') != $entity->getFetched('accountId'); - - $accountId = null; - - $titleChanged = $entity->has('title') && $entity->get('title') != $entity->getFetched('title'); - - if ($accountIdChanged) { - $accountId = $entity->get('accountId'); - - if (empty($accountId)) { - $this->unrelate($entity, 'accounts', $entity->getFetched('accountId')); - - return; - } - } - - if ($titleChanged) { - if (empty($accountId)) { - $accountId = $entity->getFetched('accountId'); - - if (empty($accountId)) { - return; - } - } - } - - if ($accountIdChanged || $titleChanged) { - $accountContact = $this->entityManager - ->getRDBRepository('AccountContact') - ->select(['role']) - ->where([ - 'accountId' => $accountId, - 'contactId' => $entity->getId(), - 'deleted' => false, - ]) - ->findOne(); - - if (!$accountContact) { - if ($accountIdChanged) { - $this->relate($entity, 'accounts', $accountId, [ - 'role' => $entity->get('title') - ]); - } - - return; - } - - if ($titleChanged && $entity->get('title') != $accountContact->get('role')) { - $this->updateRelation($entity, 'accounts', $accountId, [ - 'role' => $entity->get('title'), - ]); - } - } - } -} +class Contact extends Database +{} diff --git a/application/Espo/Modules/Crm/Repositories/Lead.php b/application/Espo/Modules/Crm/Repositories/Lead.php index 36d9f6e120..589a2b7c11 100644 --- a/application/Espo/Modules/Crm/Repositories/Lead.php +++ b/application/Espo/Modules/Crm/Repositories/Lead.php @@ -29,34 +29,10 @@ namespace Espo\Modules\Crm\Repositories; -use Espo\ORM\Entity; +use Espo\Core\Repositories\Database; /** - * @extends \Espo\Core\Repositories\Database<\Espo\Modules\Crm\Entities\Lead> + * @extends Database<\Espo\Modules\Crm\Entities\Lead> */ -class Lead extends \Espo\Core\Repositories\Database -{ - public function beforeSave(Entity $entity, array $options = []) - { - if ( - !$entity->get('convertedAt') && - $entity->get('status') === 'Converted' && - $entity->isAttributeChanged('status') - ) { - $convertedAt = date('Y-m-d H:i:s'); - - $entity->set('convertedAt', $convertedAt); - } - - parent::beforeSave($entity, $options); - } - - public function afterSave(Entity $entity, array $options = []) - { - parent::afterSave($entity, $options); - - if ($entity->has('targetListId')) { - $this->relate($entity, 'targetLists', $entity->get('targetListId')); - } - } -} +class Lead extends Database +{}