|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Base mail scanner action file. |
|
4
|
|
|
* |
|
5
|
|
|
* @package App |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright YetiForce S.A. |
|
8
|
|
|
* @license YetiForce Public License 5.0 (licenses/LicenseEN.txt or yetiforce.com) |
|
9
|
|
|
* @author Mariusz Krzaczkowski <[email protected]> |
|
10
|
|
|
* @author Radosław Skrzypczak <[email protected]> |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace App\Mail\ScannerAction; |
|
14
|
|
|
|
|
15
|
|
|
use App\Mail\RecordFinder; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Base mail scanner action class. |
|
19
|
|
|
*/ |
|
20
|
|
|
class CreatedHelpDesk extends CreatedMail |
|
21
|
|
|
{ |
|
22
|
|
|
/** {@inheritdoc} */ |
|
23
|
|
|
public static $priority = 5; |
|
24
|
|
|
|
|
25
|
|
|
/** {@inheritdoc} */ |
|
26
|
|
|
public function process(): void |
|
27
|
|
|
{ |
|
28
|
|
|
if ($this->checkExceptions() || ($prefix = RecordFinder::getRecordNumberFromString($this->message->getSubject(), 'HelpDesk')) && \App\Record::getIdByRecordNumber($prefix, 'HelpDesk')) { |
|
29
|
|
|
return; |
|
30
|
|
|
} |
|
31
|
|
|
$owner = $this->account->getSource()->get('assigned_user_id'); |
|
32
|
|
|
$fromEmail = $this->message->getEmail('from'); |
|
33
|
|
|
$contactId = current(\App\Utils::flatten(RecordFinder::findByEmail($fromEmail, $this->getEmailsFields('Contacts')))); |
|
34
|
|
|
$parentId = current(\App\Utils::flatten(RecordFinder::findByEmail($fromEmail, $this->getEmailsFields('Accounts')))); |
|
35
|
|
|
if (!$parentId) { |
|
36
|
|
|
$parentId = current(\App\Utils::flatten(RecordFinder::findByEmail($fromEmail, $this->getEmailsFields('Vendors')))); |
|
37
|
|
|
} |
|
38
|
|
|
if (!$parentId && $contactId) { |
|
39
|
|
|
$parentId = \App\Record::getParentRecord($contactId, 'Contacts'); |
|
40
|
|
|
} |
|
41
|
|
|
$recordModel = \Vtiger_Record_Model::getCleanInstance('HelpDesk'); |
|
42
|
|
|
$this->loadServiceContracts($recordModel, $parentId); |
|
43
|
|
|
$recordModel->set('assigned_user_id', $owner); |
|
44
|
|
|
$recordModel->set('created_user_id', \App\User::getCurrentUserRealId()); |
|
45
|
|
|
$recordModel->setFromUserValue('ticket_title', \App\TextUtils::textTruncate($this->message->getSubject(), $recordModel->getField('ticket_title')->getMaxValue(), false)); |
|
46
|
|
|
|
|
47
|
|
|
$mailId = $this->message->getMailCrmId($this->account->getSource()->getId()); |
|
48
|
|
|
$this->message->getBody(); |
|
49
|
|
|
if ($mailId) { |
|
50
|
|
|
$this->attachments = $this->message->processData['CreatedMail']['attachments'] ?? (new \App\Db\Query())->select(['crmid' => 'documentsid'])->from('vtiger_ossmailview_files')->where(['ossmailviewid' => $mailId])->all(); |
|
|
|
|
|
|
51
|
|
|
} elseif ($this->message->hasAttachments()) { |
|
52
|
|
|
$this->message->saveAttachments([ |
|
53
|
|
|
'assigned_user_id' => $owner, |
|
54
|
|
|
'modifiedby' => $owner, |
|
55
|
|
|
]); |
|
56
|
|
|
} |
|
57
|
|
|
$recordModel->set('description', \App\TextUtils::htmlTruncate($this->message->getBody(true), $recordModel->getField('description')->getMaxValue())); |
|
58
|
|
|
$recordModel->set('ticketstatus', \Config\Modules\OSSMailScanner::$helpdeskCreateDefaultStatus); |
|
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
if ($contactId) { |
|
61
|
|
|
$recordModel->ext['relations'][] = [ |
|
62
|
|
|
'relatedModule' => 'Contacts', |
|
63
|
|
|
'relatedRecords' => [$contactId], |
|
64
|
|
|
]; |
|
65
|
|
|
} |
|
66
|
|
|
if ($mailId) { |
|
67
|
|
|
$recordModel->ext['relations'][] = [ |
|
68
|
|
|
'reverse' => true, |
|
69
|
|
|
'relatedModule' => 'OSSMailView', |
|
70
|
|
|
'relatedRecords' => [$mailId], |
|
71
|
|
|
'params' => $this->message->getDate(), |
|
72
|
|
|
]; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
foreach ($this->attachments as $file) { |
|
76
|
|
|
$recordModel->ext['relations'][] = [ |
|
77
|
|
|
'relatedModule' => 'Documents', |
|
78
|
|
|
'relatedRecords' => [$file['crmid']], |
|
79
|
|
|
]; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$recordModel->save(); |
|
83
|
|
|
$this->message->setProcessData($this->getName(), ['crmid' => $recordModel->getId()]); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Find service contracts and init data. |
|
88
|
|
|
* |
|
89
|
|
|
* @param Vtiger_Record_Model $recordModel |
|
|
|
|
|
|
90
|
|
|
* @param int|bool $parentId |
|
91
|
|
|
* |
|
92
|
|
|
* @return void |
|
93
|
|
|
*/ |
|
94
|
|
|
private function loadServiceContracts(\Vtiger_Record_Model $recordModel, $parentId) |
|
95
|
|
|
{ |
|
96
|
|
|
if (!$parentId) { |
|
97
|
|
|
return; |
|
98
|
|
|
} |
|
99
|
|
|
$recordModel->set('parent_id', $parentId); |
|
100
|
|
|
$queryGenerator = new \App\QueryGenerator('ServiceContracts'); |
|
101
|
|
|
$queryGenerator->setFields(['id', 'contract_priority']); |
|
102
|
|
|
$queryGenerator->addNativeCondition(['vtiger_servicecontracts.sc_related_to' => $parentId]); |
|
103
|
|
|
$queryGenerator->permissions = false; |
|
104
|
|
|
$queryGenerator->addCondition('contract_status', 'In Progress', 'e'); |
|
105
|
|
|
$dataReader = $queryGenerator->createQuery()->createCommand()->query(); |
|
106
|
|
|
if (1 === $dataReader->count()) { |
|
107
|
|
|
$serviceContracts = $dataReader->read(); |
|
108
|
|
|
$recordModel->set('servicecontractsid', $serviceContracts['id']); |
|
109
|
|
|
if (\App\Fields\Picklist::isExists('ticketpriorities', $serviceContracts['contract_priority'])) { |
|
110
|
|
|
$recordModel->set('ticketpriorities', $serviceContracts['contract_priority']); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
$dataReader->close(); |
|
114
|
|
|
unset($dataReader, $queryGenerator); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|