diff --git a/application/Espo/Core/Exceptions/BadRequest.php b/application/Espo/Core/Exceptions/BadRequest.php new file mode 100644 index 0000000000..29a029ad8d --- /dev/null +++ b/application/Espo/Core/Exceptions/BadRequest.php @@ -0,0 +1,11 @@ +getRecordService()->convert($data['id'], $data['records']); + + if (!empty($entity)) { + return $entity->toArray(); + } + throw new Error(); + } } diff --git a/application/Espo/Modules/Crm/Controllers/Prospect.php b/application/Espo/Modules/Crm/Controllers/Prospect.php index f71276af8c..e82d98713b 100644 --- a/application/Espo/Modules/Crm/Controllers/Prospect.php +++ b/application/Espo/Modules/Crm/Controllers/Prospect.php @@ -2,7 +2,24 @@ namespace Espo\Modules\Crm\Controllers; +use \Espo\Core\Exceptions\Error; +use \Espo\Core\Exceptions\BadRequest; + class Prospect extends \Espo\Core\Controllers\Record { + + public function actionConvert($params, $data) + { + + if (empty($data['id'])) { + throw new BadRequest(); + } + $entity = $this->getRecordService()->convert($data['id']); + + if (!empty($entity)) { + return $entity->toArray(); + } + throw new Error(); + } } diff --git a/application/Espo/Modules/Crm/Resources/layouts/Account/detailConvert.json b/application/Espo/Modules/Crm/Resources/layouts/Account/detailConvert.json new file mode 100644 index 0000000000..7341b39fb4 --- /dev/null +++ b/application/Espo/Modules/Crm/Resources/layouts/Account/detailConvert.json @@ -0,0 +1 @@ +[{"label":"Overview","rows":[[{"name":"name"},false],[{"name":"website"},{"name":"phone"}],[{"name":"email"},{"name":"fax"}],[{"name":"billingAddress"},{"name":"shippingAddress"}],[{"name":"description"},false]]},{"label":"Details","rows":[[{"name":"type"},{"name":"sicCode"}],[{"name":"industry"},false]]}] \ No newline at end of file diff --git a/application/Espo/Modules/Crm/Resources/layouts/Contact/detailConvert.json b/application/Espo/Modules/Crm/Resources/layouts/Contact/detailConvert.json new file mode 100644 index 0000000000..c334dad09b --- /dev/null +++ b/application/Espo/Modules/Crm/Resources/layouts/Contact/detailConvert.json @@ -0,0 +1 @@ +[{"label":"Overview","rows":[[{"name":"name"}],[{"name":"email"},{"name":"phone"}],[{"name":"title"},{"name":"doNotCall"}],[{"name":"address"},false],[{"name":"description"},false]]}] diff --git a/application/Espo/Modules/Crm/Resources/layouts/Opportunity/detailConvert.json b/application/Espo/Modules/Crm/Resources/layouts/Opportunity/detailConvert.json new file mode 100644 index 0000000000..cc968e5487 --- /dev/null +++ b/application/Espo/Modules/Crm/Resources/layouts/Opportunity/detailConvert.json @@ -0,0 +1 @@ +[{"label":"Overview","rows":[[{"name":"name"}],[{"name":"stage"},{"name":"amount"}],[{"name":"probability"},{"name":"leadSource"}],[{"name":"closeDate"},false],[{"name":"description"},false]]}] diff --git a/application/Espo/Modules/Crm/Resources/layouts/Prospect/detail.json b/application/Espo/Modules/Crm/Resources/layouts/Prospect/detail.json index c47d6ed5db..a16544e9f2 100644 --- a/application/Espo/Modules/Crm/Resources/layouts/Prospect/detail.json +++ b/application/Espo/Modules/Crm/Resources/layouts/Prospect/detail.json @@ -1 +1 @@ -[{"label":"Overview","rows":[[{"name":"name"},{"name":"account"}],[{"name":"email"},{"name":"phone"}],[{"name":"title"},{"name":"doNotCall"}],[{"name":"address"},{"name":"website"}],[{"name":"description"},false]]}] \ No newline at end of file +[{"label":"Overview","rows":[[{"name":"name"},{"name":"accountName"}],[{"name":"email"},{"name":"phone"}],[{"name":"title"},{"name":"doNotCall"}],[{"name":"address"},{"name":"website"}],[{"name":"description"},false]]}] diff --git a/application/Espo/Modules/Crm/Resources/layouts/Prospect/filters.json b/application/Espo/Modules/Crm/Resources/layouts/Prospect/filters.json index 2cadfc24d6..687a7b37c9 100644 --- a/application/Espo/Modules/Crm/Resources/layouts/Prospect/filters.json +++ b/application/Espo/Modules/Crm/Resources/layouts/Prospect/filters.json @@ -1 +1 @@ -["name","email","account"] \ No newline at end of file +["name","email","accountName"] diff --git a/application/Espo/Modules/Crm/Resources/layouts/Prospect/list.json b/application/Espo/Modules/Crm/Resources/layouts/Prospect/list.json index 3f5ef88ab0..d5d52bd2e5 100644 --- a/application/Espo/Modules/Crm/Resources/layouts/Prospect/list.json +++ b/application/Espo/Modules/Crm/Resources/layouts/Prospect/list.json @@ -1 +1 @@ -[{"name":"name","width":30,"link":true},{"name":"email"},{"name":"phone"},{"name":"account"},{"name":"createdAt"}] \ No newline at end of file +[{"name":"name","width":30,"link":true},{"name":"email"},{"name":"phone"},{"name":"accountName"},{"name":"createdAt"}] diff --git a/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Lead.json b/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Lead.json index c95790b861..024404a9b6 100644 --- a/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Lead.json +++ b/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Lead.json @@ -101,17 +101,17 @@ "teams": { "type": "linkMultiple" }, - "account": { + "createdAccount": { "type": "link", "disabled": true, "readOnly": true }, - "contact": { + "createdContact": { "type": "link", "disabled": true, "readOnly": true }, - "opportunity": { + "createdOpportunity": { "type": "link", "disabled": true, "readOnly": true @@ -155,15 +155,15 @@ "entity": "Task", "foreign": "parent" }, - "account": { + "createdAccount": { "type": "belongsTo", "entity": "Account" }, - "contact": { + "createdContact": { "type": "belongsTo", "entity": "Contact" }, - "opportunity": { + "createdOpportunity": { "type": "belongsTo", "entity": "Opportunity" } @@ -175,33 +175,22 @@ "email": "email", "phone": "phone", "address": "address", - "assignedUser": "assignedUser" + "assignedUser": "assignedUser", + "teams": "teams" }, "Account": { "name": "accountName", "website": "website", "email": "email", "phone": "phoneOffice", - "assignedUser": "assignedUser" + "assignedUser": "assignedUser", + "teams": "teams" }, "Opportunity": { "amount": "opportunityAmount", "leadSource": "source", - "assignedUser": "assignedUser" - } - }, - "convertLinks": { - "Account": { - "Contact": "contacts", - "Opportunity": "oppotunities" - }, - "Contact": { - "Opportunity": "oppotunities" - }, - "Lead": { - "Account": "account", - "Contact": "contact", - "Opportunity": "oppotunity" + "assignedUser": "assignedUser", + "teams": "teams" } }, "collection": { diff --git a/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Prospect.json b/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Prospect.json index fa82678b80..682fb289ce 100644 --- a/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Prospect.json +++ b/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Prospect.json @@ -20,7 +20,7 @@ "type": "varchar", "maxLength": 100 }, - "account": { + "accountName": { "type": "varchar", "maxLength": 100 }, diff --git a/application/Espo/Modules/Crm/Resources/metadata/viewDefs/Lead.json b/application/Espo/Modules/Crm/Resources/metadata/viewDefs/Lead.json index 1c02ab9b4b..24b30b678a 100644 --- a/application/Espo/Modules/Crm/Resources/metadata/viewDefs/Lead.json +++ b/application/Espo/Modules/Crm/Resources/metadata/viewDefs/Lead.json @@ -1,8 +1,8 @@ { - "viewMaps":{ + "views":{ "detail":"Crm:Lead.Detail" }, - "recordViewMaps":{ + "recordViews":{ "detail":"Crm:Lead.Record.Detail" }, "sidePanels":{ diff --git a/application/Espo/Modules/Crm/Resources/metadata/viewDefs/Meeting.json b/application/Espo/Modules/Crm/Resources/metadata/viewDefs/Meeting.json index 42d3a50ca1..4ac5aa647d 100644 --- a/application/Espo/Modules/Crm/Resources/metadata/viewDefs/Meeting.json +++ b/application/Espo/Modules/Crm/Resources/metadata/viewDefs/Meeting.json @@ -1,5 +1,5 @@ { - "viewMaps":{ + "views":{ "detail":"Crm:Meeting.Detail" }, "sidePanels":{ diff --git a/application/Espo/Modules/Crm/Resources/metadata/viewDefs/Prospect.json b/application/Espo/Modules/Crm/Resources/metadata/viewDefs/Prospect.json index 78f763c675..d4fb8edf67 100644 --- a/application/Espo/Modules/Crm/Resources/metadata/viewDefs/Prospect.json +++ b/application/Espo/Modules/Crm/Resources/metadata/viewDefs/Prospect.json @@ -1,4 +1,7 @@ { + "views":{ + "detail":"Crm:Prospect.Detail" + }, "menu":{ "detail":{ "buttons":[ diff --git a/application/Espo/Modules/Crm/Services/Lead.php b/application/Espo/Modules/Crm/Services/Lead.php new file mode 100644 index 0000000000..bb8dbdb69c --- /dev/null +++ b/application/Espo/Modules/Crm/Services/Lead.php @@ -0,0 +1,54 @@ +getEntity($id); + + if (!$this->getAcl()->check($lead, 'edit')) { + throw new Forbidden(); + } + + $entityManager = $this->getEntityManager(); + + if (!empty($recordsData['Account'])) { + $account = $entityManager->getEntity('Account'); + $account->set($recordsData['Account']); + $entityManager->saveEntity($account); + $lead->set('createdAccountId', $account->id); + } + if (!empty($recordsData['Opportunity'])) { + $opportunity = $entityManager->getEntity('Opportunity'); + $opportunity->set($recordsData['Opportunity']); + if (isset($account)) { + $opportunity->set('accountId', $account->id); + } + $entityManager->saveEntity($opportunity); + $lead->set('createdOpportunityId', $opportunity->id); + } + if (!empty($recordsData['Contact'])) { + $contact = $entityManager->getEntity('Contact'); + $contact->set($recordsData['Contact']); + if (isset($account)) { + $contact->set('accountId', $account->id); + } + $entityManager->saveEntity($contact); + if (isset($opportunity)) { + $entityManager->getRepository('Contact')->relate($contact, 'opportunities', $opportunity); + } + $lead->set('createdContactId', $contact->id); + } + + $lead->set('status', 'Converted'); + $entityManager->saveEntity($lead); + + return $lead; + } +} + diff --git a/application/Espo/Modules/Crm/Services/Prospect.php b/application/Espo/Modules/Crm/Services/Prospect.php new file mode 100644 index 0000000000..d199505086 --- /dev/null +++ b/application/Espo/Modules/Crm/Services/Prospect.php @@ -0,0 +1,31 @@ +getEntityManager(); + $prospect = $this->getEntity($id); + + if (!$this->getAcl()->check($prospect, 'delete')) { + throw new Forbidden(); + } + if (!$this->getAcl()->check('Lead', 'read')) { + throw new Forbidden(); + } + + $lead = $entityManager->getEntity('Lead'); + $lead->set($prospect->toArray()); + + $entityManager->removeEntity($prospect); + $entityManager->saveEntity($lead); + + return $lead; + } +} + diff --git a/application/Espo/ORM/EntityManager.php b/application/Espo/ORM/EntityManager.php index 71205a9520..d47062a1a1 100644 --- a/application/Espo/ORM/EntityManager.php +++ b/application/Espo/ORM/EntityManager.php @@ -55,6 +55,18 @@ class EntityManager { return $this->getRepository($name)->get($id); } + + public function saveEntity(Entity $entity) + { + $entityName = $entity->getEntityName(); + return $this->getRepository($entityName)->save($entity); + } + + public function removeEntity(Entity $entity) + { + $entityName = $entity->getEntityName(); + return $this->getRepository($entityName)->remove($entity); + } public function getRepository($name) { diff --git a/application/Espo/Resources/metadata/viewDefs/OutboundEmail.json b/application/Espo/Resources/metadata/viewDefs/OutboundEmail.json index 64409523c3..bd191631fe 100644 --- a/application/Espo/Resources/metadata/viewDefs/OutboundEmail.json +++ b/application/Espo/Resources/metadata/viewDefs/OutboundEmail.json @@ -1,5 +1,5 @@ { - "recordViewMaps":{ + "recordViews":{ "detail":"OutboundEmail.Record.Detail", "edit":"OutboundEmail.Record.Edit", "editQuick":"OutboundEmail.Record.Edit" diff --git a/application/Espo/Resources/metadata/viewDefs/Role.json b/application/Espo/Resources/metadata/viewDefs/Role.json index c160ed4517..e5f3a2976e 100644 --- a/application/Espo/Resources/metadata/viewDefs/Role.json +++ b/application/Espo/Resources/metadata/viewDefs/Role.json @@ -1,5 +1,5 @@ { - "recordViewMaps":{ + "recordViews":{ "detail":"Role.Record.Detail", "edit":"Role.Record.Edit", "editQuick":"Role.Record.Edit" diff --git a/application/Espo/Resources/metadata/viewDefs/Team.json b/application/Espo/Resources/metadata/viewDefs/Team.json index b6e949ebc6..d9b61b71a1 100644 --- a/application/Espo/Resources/metadata/viewDefs/Team.json +++ b/application/Espo/Resources/metadata/viewDefs/Team.json @@ -4,7 +4,7 @@ "create":false } }, - "recordViewMaps":{ + "recordViews":{ "detail":"Team.Detail", "edit":"Team.Edit" } diff --git a/application/Espo/Resources/metadata/viewDefs/User.json b/application/Espo/Resources/metadata/viewDefs/User.json index 9c4506cafa..00f448d9b6 100644 --- a/application/Espo/Resources/metadata/viewDefs/User.json +++ b/application/Espo/Resources/metadata/viewDefs/User.json @@ -1,5 +1,5 @@ { - "recordViewMaps":{ + "recordViews":{ "detail":"User.Record.Detail", "edit":"User.Record.Edit", "editQuick":"User.Record.Edit"