|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* +*********************************************************************************** |
|
4
|
|
|
* The contents of this file are subject to the vtiger CRM Public License Version 1.0 |
|
5
|
|
|
* ("License"); You may not use this file except in compliance with the License |
|
6
|
|
|
* The Original Code is: vtiger CRM Open Source |
|
7
|
|
|
* The Initial Developer of the Original Code is vtiger. |
|
8
|
|
|
* Portions created by vtiger are Copyright (C) vtiger. |
|
9
|
|
|
* All Rights Reserved. |
|
10
|
|
|
* Contributor(s): YetiForce S.A. |
|
11
|
|
|
* *********************************************************************************** */ |
|
12
|
|
|
|
|
13
|
|
|
class Leads_Module_Model extends Vtiger_Module_Model |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Function to get list view query for popup window. |
|
17
|
|
|
* |
|
18
|
|
|
* @param string $sourceModule Parent module |
|
19
|
|
|
* @param string $field parent fieldname |
|
20
|
|
|
* @param string $record parent id |
|
21
|
|
|
* @param \App\QueryGenerator $queryGenerator |
|
22
|
|
|
*/ |
|
23
|
|
|
public function getQueryByModuleField($sourceModule, $field, $record, App\QueryGenerator $queryGenerator) |
|
|
|
|
|
|
24
|
|
|
{ |
|
25
|
|
|
if (!empty($record) && \in_array($sourceModule, ['Campaigns', 'Products', 'Services'])) { |
|
26
|
|
|
switch ($sourceModule) { |
|
27
|
|
|
case 'Campaigns': |
|
28
|
|
|
$tableName = 'vtiger_campaign_records'; |
|
29
|
|
|
$fieldName = 'crmid'; |
|
30
|
|
|
$relatedFieldName = 'campaignid'; |
|
31
|
|
|
break; |
|
32
|
|
|
case 'Products': |
|
33
|
|
|
$tableName = 'vtiger_seproductsrel'; |
|
34
|
|
|
$fieldName = 'crmid'; |
|
35
|
|
|
$relatedFieldName = 'productid'; |
|
36
|
|
|
break; |
|
37
|
|
|
default: |
|
38
|
|
|
break; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
if ('Services' === $sourceModule) { |
|
42
|
|
|
$subQuery = (new App\Db\Query()) |
|
43
|
|
|
->select(['relcrmid']) |
|
44
|
|
|
->from('vtiger_crmentityrel') |
|
45
|
|
|
->where(['crmid' => $record]); |
|
46
|
|
|
$secondSubQuery = (new App\Db\Query()) |
|
47
|
|
|
->select(['crmid']) |
|
48
|
|
|
->from('vtiger_crmentityrel') |
|
49
|
|
|
->where(['relcrmid' => $record]); |
|
50
|
|
|
$condition = ['and', ['not in', 'vtiger_leaddetails.leadid', $subQuery], ['not in', 'vtiger_leaddetails.leadid', $secondSubQuery]]; |
|
51
|
|
|
} else { |
|
52
|
|
|
$condition = ['not in', 'vtiger_leaddetails.leadid', (new App\Db\Query())->select([$fieldName])->from($tableName)->where([$relatedFieldName => $record])]; |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
$queryGenerator->addNativeCondition($condition); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Function to search accounts. |
|
60
|
|
|
* |
|
61
|
|
|
* @param Vtiger_Record_Model $recordModel |
|
62
|
|
|
* |
|
63
|
|
|
* @throws \App\Exceptions\NoPermitted |
|
64
|
|
|
* |
|
65
|
|
|
* @return bool |
|
66
|
|
|
*/ |
|
67
|
|
|
public function searchAccountsToConvert(Vtiger_Record_Model $recordModel) |
|
68
|
|
|
{ |
|
69
|
|
|
\App\Log::trace('Start ' . __METHOD__); |
|
70
|
|
|
if ($recordModel) { |
|
|
|
|
|
|
71
|
|
|
$mappingFields = Vtiger_Processes_Model::getConfig('marketing', 'conversion', 'mapping'); |
|
72
|
|
|
$mappingFields = \App\Json::decode($mappingFields); |
|
|
|
|
|
|
73
|
|
|
$query = (new App\Db\Query())->select(['vtiger_account.accountid']) |
|
74
|
|
|
->from('vtiger_account') |
|
75
|
|
|
->innerJoin('vtiger_crmentity', 'vtiger_crmentity.crmid = vtiger_account.accountid') |
|
76
|
|
|
->where(['vtiger_crmentity.deleted' => 0]); |
|
77
|
|
|
$joinTable = ['vtiger_account', 'vtiger_crmentity']; |
|
78
|
|
|
$moduleModel = Vtiger_Module_Model::getInstance('Accounts'); |
|
79
|
|
|
$focus = $moduleModel->getEntityInstance(); |
|
80
|
|
|
foreach ($mappingFields as $mappingField) { |
|
81
|
|
|
foreach ($mappingField as $leadFieldName => $accountFieldName) { |
|
82
|
|
|
$fieldModel = $moduleModel->getFieldByName($accountFieldName); |
|
83
|
|
|
if (!$fieldModel) { |
|
84
|
|
|
throw new \App\Exceptions\NoPermitted('LBL_PERMISSION_DENIED'); |
|
85
|
|
|
} |
|
86
|
|
|
$tableName = $fieldModel->get('table'); |
|
87
|
|
|
if (!\in_array($tableName, $joinTable)) { |
|
88
|
|
|
$query->innerJoin($tableName, "{$tableName}.{$focus->tab_name_index[$tableName]} = vtiger_account.accountid"); |
|
89
|
|
|
$joinTable[] = $tableName; |
|
90
|
|
|
} |
|
91
|
|
|
$query->andWhere(["{$tableName}.{$fieldModel->getColumnName()}" => $recordModel->get($leadFieldName)]); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
$query->limit(2); |
|
95
|
|
|
$dataReader = $query->createCommand()->query(); |
|
96
|
|
|
$numberRows = $dataReader->count(); |
|
97
|
|
|
if ($numberRows > 1) { |
|
98
|
|
|
$dataReader->close(); |
|
99
|
|
|
\App\Log::trace('End ' . __METHOD__); |
|
100
|
|
|
|
|
101
|
|
|
return false; |
|
102
|
|
|
} |
|
103
|
|
|
if (1 === $numberRows) { |
|
104
|
|
|
\App\Log::trace('End ' . __METHOD__); |
|
105
|
|
|
|
|
106
|
|
|
return (int) $dataReader->readColumn(0); |
|
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
\App\Log::trace('End ' . __METHOD__); |
|
110
|
|
|
|
|
111
|
|
|
return true; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Function that returns status that allow to convert Lead. |
|
116
|
|
|
* |
|
117
|
|
|
* @return <Array> array of statuses |
|
|
|
|
|
|
118
|
|
|
*/ |
|
119
|
|
|
public static function getConversionAvaibleStatuses() |
|
120
|
|
|
{ |
|
121
|
|
|
$leadConfig = Settings_MarketingProcesses_Module_Model::getConfig('lead'); |
|
122
|
|
|
|
|
123
|
|
|
return $leadConfig['convert_status']; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Function that checks if lead record can be converted. |
|
128
|
|
|
* |
|
129
|
|
|
* @param string $status - lead status |
|
130
|
|
|
* |
|
131
|
|
|
* @return bool if or not allowed to convert |
|
132
|
|
|
*/ |
|
133
|
|
|
public static function checkIfAllowedToConvert($status) |
|
134
|
|
|
{ |
|
135
|
|
|
$leadConfig = Settings_MarketingProcesses_Module_Model::getConfig('lead'); |
|
136
|
|
|
|
|
137
|
|
|
if (empty($leadConfig['convert_status'])) { |
|
138
|
|
|
return true; |
|
139
|
|
|
} |
|
140
|
|
|
return \in_array($status, $leadConfig['convert_status']); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* The function adds restrictions to the functionality of searching for records. |
|
145
|
|
|
* |
|
146
|
|
|
* @param App\Db\Query $query |
|
147
|
|
|
* @param App\RecordSearch $recordSearch |
|
148
|
|
|
* |
|
149
|
|
|
* @return void |
|
150
|
|
|
*/ |
|
151
|
|
|
public function searchRecordCondition(App\Db\Query $query, App\RecordSearch $recordSearch = null): void |
|
152
|
|
|
{ |
|
153
|
|
|
if ($recordSearch->moduleName === $this->getName()) { |
|
154
|
|
|
$query->innerJoin('vtiger_leaddetails', 'csl.crmid = vtiger_leaddetails.leadid'); |
|
155
|
|
|
$query->andWhere(['vtiger_leaddetails.converted' => 0]); |
|
156
|
|
|
} else { |
|
157
|
|
|
$query->andWhere(['not in', 'csl.crmid', (new \App\Db\Query())->select(['leadid'])->from('vtiger_leaddetails')->where(['converted' => 1])]); |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.