This commit is contained in:
Yuri Kuznetsov
2023-02-23 12:40:41 +02:00
parent 64f2c59134
commit fa577a4fa9
10 changed files with 384 additions and 114 deletions

View File

@@ -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');
}
}

View File

@@ -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');
}
}

View File

@@ -0,0 +1,63 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2023 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Modules\Crm\Hooks\Account;
use Espo\Core\Hook\Hook\AfterSave;
use Espo\Core\ORM\Repository\Option\SaveOption;
use Espo\Modules\Crm\Entities\Account;
use Espo\ORM\Entity;
use Espo\ORM\EntityManager;
use Espo\ORM\Repository\Option\SaveOptions;
/**
* @implements AfterSave<Account>
*/
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);
}
}

View File

@@ -0,0 +1,96 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2023 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Modules\Crm\Hooks\Contact;
use Espo\Core\Hook\Hook\AfterSave;
use Espo\Modules\Crm\Entities\Contact;
use Espo\ORM\Entity;
use Espo\ORM\EntityManager;
use Espo\ORM\Repository\Option\SaveOptions;
/**
* @implements AfterSave<Contact>
*/
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]);
}
}
}

View File

@@ -0,0 +1,63 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2023 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Modules\Crm\Hooks\Contact;
use Espo\Core\Hook\Hook\AfterSave;
use Espo\Core\ORM\Repository\Option\SaveOption;
use Espo\Modules\Crm\Entities\Contact;
use Espo\ORM\Entity;
use Espo\ORM\EntityManager;
use Espo\ORM\Repository\Option\SaveOptions;
/**
* @implements AfterSave<Contact>
*/
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);
}
}

View File

@@ -0,0 +1,62 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2023 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Modules\Crm\Hooks\Lead;
use Espo\Core\Field\DateTime;
use Espo\Core\Hook\Hook\BeforeSave;
use Espo\Modules\Crm\Entities\Lead;
use Espo\ORM\Entity;
use Espo\ORM\Repository\Option\SaveOptions;
/**
* @implements BeforeSave<Lead>
*/
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());
}
}

View File

@@ -0,0 +1,63 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2023 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Modules\Crm\Hooks\Lead;
use Espo\Core\Hook\Hook\AfterSave;
use Espo\Core\ORM\Repository\Option\SaveOption;
use Espo\Modules\Crm\Entities\Lead;
use Espo\ORM\Entity;
use Espo\ORM\EntityManager;
use Espo\ORM\Repository\Option\SaveOptions;
/**
* @implements AfterSave<Lead>
*/
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);
}
}

View File

@@ -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<AccountEntity>
*/
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
{}

View File

@@ -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
{}

View File

@@ -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
{}