From 82eaa99afd402b3bb964cd7326bd1b36dba6e2c1 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Thu, 23 Jan 2014 16:52:10 +0200 Subject: [PATCH] dev --- application/Espo/Core/Mail/Sender.php | 35 ++++++-- .../Espo/Modules/Crm/Services/Activities.php | 84 +++++++++++++++++-- .../Espo/Modules/Crm/Services/Call.php | 14 ++++ .../Espo/Modules/Crm/Services/Lead.php | 24 ++++++ .../Espo/Modules/Crm/Services/Meeting.php | 33 ++++++++ application/Espo/ORM/DB/Mapper.php | 3 + application/Espo/ORM/Repository.php | 2 +- application/Espo/Services/Email.php | 13 ++- 8 files changed, 190 insertions(+), 18 deletions(-) create mode 100644 application/Espo/Modules/Crm/Services/Call.php create mode 100644 application/Espo/Modules/Crm/Services/Meeting.php diff --git a/application/Espo/Core/Mail/Sender.php b/application/Espo/Core/Mail/Sender.php index 76743e9f8e..90811c4387 100644 --- a/application/Espo/Core/Mail/Sender.php +++ b/application/Espo/Core/Mail/Sender.php @@ -19,16 +19,32 @@ class Sender protected $config; protected $transport; + + protected $isGlobal = false; public function __construct($config) { - $this->config = $config; - $this->transport = new SmtpTransport(); - $this->setupGlobal(); + $this->config = $config; + $this->useGlobal(); + } + + public function useSmtp($smtp) + { + $this->isGlobal = false; + + // TODO setup smtp + + return $this; } - protected function setupGlobal() + public function useGlobal() { + if ($this->isGlobal) { + return $this; + } + + $this->transport = new SmtpTransport(); + $config = $this->config; $opts = array( @@ -48,6 +64,8 @@ class Sender $options = new SmtpOptions($opts); $this->transport->setOptions($options); + + $this->isGlobal = true; return $this; } @@ -119,11 +137,9 @@ class Sender } $parts[] = $attachment; } - } - - - $body->setParts($parts); + } + $body->setParts($parts); $message->setBody($body); try { @@ -133,7 +149,8 @@ class Sender } catch (\Exception $e) { throw new Error($e->getMessage(), 500); } - + + $this->useGlobal(); } } diff --git a/application/Espo/Modules/Crm/Services/Activities.php b/application/Espo/Modules/Crm/Services/Activities.php index 69af41faca..3ad9fd58f7 100644 --- a/application/Espo/Modules/Crm/Services/Activities.php +++ b/application/Espo/Modules/Crm/Services/Activities.php @@ -89,6 +89,32 @@ class Activities extends \Espo\Core\Services\Base "; } + if ($scope == 'Account') { + $tQu = $qu; + $qu = " + SELECT meeting.id AS 'id', meeting.name AS 'name', meeting.date_start AS 'dateStart', meeting.date_end AS 'dateEnd', 'Meeting' AS '_scope', + meeting.assigned_user_id AS assignedUserId, TRIM(CONCAT(user.first_name, ' ', user.last_name)) AS assignedUserName, + meeting.parent_type AS 'parentType', meeting.parent_id AS 'parentId', meeting.status AS status + FROM `meeting` + LEFT JOIN `user` ON user.id = meeting.assigned_user_id + JOIN contact_meeting ON + meeting.id = contact_meeting.meeting_id AND + contact_meeting.deleted = 0 + JOIN contact ON + contact_meeting.contact_id = contact.id AND + contact.deleted = 0 + WHERE + meeting.deleted = 0 AND + meeting.status {$op} ('". implode("', '", $notIn) . "') AND + contact.account_id = ".$this->getPDO()->quote($id)." + "; + $qu = " + {$tQu} + UNION + {$qu} + "; + } + return $qu; } @@ -137,32 +163,76 @@ class Activities extends \Espo\Core\Services\Base "; } + if ($scope == 'Account') { + $tQu = $qu; + $qu = " + SELECT call.id AS 'id', call.name AS 'name', call.date_start AS 'dateStart', call.date_end AS 'dateEnd', 'Call' AS '_scope', + call.assigned_user_id AS assignedUserId, TRIM(CONCAT(user.first_name, ' ', user.last_name)) AS assignedUserName, + call.parent_type AS 'parentType', call.parent_id AS 'parentId', call.status AS status + FROM `call` + LEFT JOIN `user` ON user.id = call.assigned_user_id + JOIN call_contact ON + call.id = call_contact.call_id AND + call_contact.deleted = 0 + JOIN contact ON + call_contact.contact_id = contact.id AND + contact.deleted = 0 + WHERE + call.deleted = 0 AND + call.status {$op} ('". implode("', '", $notIn) . "') AND + contact.account_id = ".$this->getPDO()->quote($id)." + "; + $qu = " + {$tQu} + UNION + {$qu} + "; + } + return $qu; } protected function getEmailQuery($scope, $id, $op = 'IN', $notIn = array()) { $qu = " - SELECT email.id AS 'id', email.name AS 'name', email.date_sent AS 'dateStart', '' AS 'dateEnd', 'Email' AS '_scope', - email.assigned_user_id AS assignedUserId, TRIM(CONCAT(user.first_name, ' ', user.last_name)) AS assignedUserName, - email.parent_type AS 'parentType', email.parent_id AS 'parentId', email.status AS status + SELECT DISTINCT + email.id AS 'id', email.name AS 'name', email.date_sent AS 'dateStart', '' AS 'dateEnd', 'Email' AS '_scope', + email.assigned_user_id AS assignedUserId, TRIM(CONCAT(user.first_name, ' ', user.last_name)) AS assignedUserName, + email.parent_type AS 'parentType', email.parent_id AS 'parentId', email.status AS status FROM `email` LEFT JOIN `user` ON user.id = email.assigned_user_id "; + + if ($this->isPerson($scope)) { + $qu .= " + JOIN email_email_address ON + email_email_address.email_id = email.id AND + email_email_address.deleted = 0 + JOIN entity_email_address ON + entity_email_address.email_address_id = email_email_address.email_address_id AND + entity_email_address.entity_id = ".$this->getPDO()->quote($id)." AND + entity_email_address.entity_type = ".$this->getPDO()->quote($scope)." AND + entity_email_address.deleted = 0 + "; + } + $qu .= " WHERE email.deleted = 0 - "; - - $qu .= " - AND email.parent_type = ".$this->getPDO()->quote($scope)." AND email.parent_id = ".$this->getPDO()->quote($id)." "; + if (!$this->isPerson($scope)) { + $qu .= " + AND email.parent_type = ".$this->getPDO()->quote($scope)." AND email.parent_id = ".$this->getPDO()->quote($id)." + "; + } + if (!empty($notIn)) { $qu .= " AND email.status {$op} ('". implode("', '", $notIn) . "') "; } + return $qu; } diff --git a/application/Espo/Modules/Crm/Services/Call.php b/application/Espo/Modules/Crm/Services/Call.php new file mode 100644 index 0000000000..59c2a053b3 --- /dev/null +++ b/application/Espo/Modules/Crm/Services/Call.php @@ -0,0 +1,14 @@ +getRepository('Contact')->relate($contact, 'opportunities', $opportunity); } $lead->set('createdContactId', $contact->id); + + + } $lead->set('status', 'Converted'); $entityManager->saveEntity($lead); + + if ($meetings = $lead->get('meetings')) { + foreach ($meetings as $meeting) { + if (!empty($contact)) { + $entityManager->getRepository('Meeting')->relate($meeting, 'contacts', $contact); + } + if (!empty($opportunity)) { + $entityManager->getRepository('Opportunity')->relate($opportunity, 'meetings', $meeting); + } + } + } + if ($calls = $lead->get('calls')) { + foreach ($calls as $call) { + if (!empty($contact)) { + $entityManager->getRepository('Call')->relate($call, 'contacts', $contact); + } + if (!empty($opportunity)) { + $entityManager->getRepository('Opportunity')->relate($opportunity, 'calls', $call); + } + } + } return $lead; } diff --git a/application/Espo/Modules/Crm/Services/Meeting.php b/application/Espo/Modules/Crm/Services/Meeting.php new file mode 100644 index 0000000000..9cc8e8d9c3 --- /dev/null +++ b/application/Espo/Modules/Crm/Services/Meeting.php @@ -0,0 +1,33 @@ +get('assignedUserId'); + if ($assignedUserId && $entity->has('usersIds')) { + $usersIds = $entity->get('usersIds'); + if (!is_array($usersIds)) { + $usersIds = array(); + } + if (!in_array($assignedUserId, $usersIds)) { + $usersIds[] = $assignedUserId; + $entity->set('usersIds', $usersIds); + $hash = $entity->get('usersNames'); + if ($hash instanceof \stdClass) { + $hash->assignedUserId = $entity->get('assignedUserName'); + $entity->set('usersNames', $hash); + } + } + } + return parent::storeEntity($entity); + } +} + diff --git a/application/Espo/ORM/DB/Mapper.php b/application/Espo/ORM/DB/Mapper.php index e442905133..c5d8cac6ca 100644 --- a/application/Espo/ORM/DB/Mapper.php +++ b/application/Espo/ORM/DB/Mapper.php @@ -343,8 +343,11 @@ abstract class Mapper implements IMapper } $dataArr = array(); + + $sql = $this->composeSelectQuery($this->toDb($relEntity->getEntityName()), $selectPart, $joinsPart, $wherePart, $orderPart, $offset, $limit); + $ps = $this->pdo->query($sql); if ($ps) { if (!$totalCount) { diff --git a/application/Espo/ORM/Repository.php b/application/Espo/ORM/Repository.php index 39e0d2e8e2..fb78000ce3 100644 --- a/application/Espo/ORM/Repository.php +++ b/application/Espo/ORM/Repository.php @@ -186,7 +186,7 @@ class Repository $entityName = $entity->relations[$relationName]['entity']; $this->handleSelectParams($params, $entityName); - $this->mapper->countRelated($entity, $relationName, $params); + return $this->mapper->countRelated($entity, $relationName, $params); } public function relate(Entity $entity, $relationName, $foreign) diff --git a/application/Espo/Services/Email.php b/application/Espo/Services/Email.php index f532ad8490..0ab42c54e1 100644 --- a/application/Espo/Services/Email.php +++ b/application/Espo/Services/Email.php @@ -24,7 +24,18 @@ class Email extends Record $entity = parent::createEntity($data); if ($entity && $entity->get('status') == 'Sending') { - $sent = $this->getMailSender()->send($entity); + $emailSender = $this->getMailSender(); + + if (strtolower($this->getUser()->get('emailAddress')) == strtolower($entity->get('from'))) { + //$emailSender->useSmtp(); // TODO use smtp + } else { + if (!$this->getConfig()->get('outboundEmailIsShared')) { + throw new Error('Can not use system smtp. outboundEmailIsShared is false.'); + } + } + + $emailSender->send($entity); + $this->getEntityManager()->saveEntity($entity); }