-
Notifications
You must be signed in to change notification settings - Fork 3
/
OpportunitiesHooks.php
197 lines (156 loc) · 5.88 KB
/
OpportunitiesHooks.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
/*********************************************************************************
* This code was developed by:
* Audox Ingeniería Ltda.
* You can contact us at:
* Web: www.audox.cl
* Email: info@audox.cl
* Skype: audox.ingenieria
********************************************************************************/
class OpportunitiesHooks{
function afterDelete(&$bean, $event, $arguments=''){
}
function afterRelationshipAdd(&$bean, $event, $arguments=''){
}
function afterRelationshipDelete(&$bean, $event, $arguments=''){
}
function afterRestore(&$bean, $event, $arguments=''){
}
function afterRetrieve(&$bean, $event, $arguments=''){
}
function afterSave(&$bean, $event, $arguments=''){
// Create Task and Call for new Opportunities
$this->CreateTaskAndCallForNewOpportunity($bean);
}
function beforeDelete(&$bean, $event, $arguments=''){
}
function beforeRelationshipAdd(&$bean, $event, $arguments=''){
}
function beforeRelationshipDelete(&$bean, $event, $arguments=''){
}
function beforeRestore(&$bean, $event, $arguments=''){
}
function beforeSave(&$bean, $event, $arguments=''){
// Send an Email to all users within "Sales Manager" role when an Opportunity greater than certain amount change from "Qualification" to "Negotiation"
$this->NotifySalesManagers($bean);
// Send data to ERP or other external app when an Opportunity change to "Closed Won"
$this->SendToERP($bean);
// Create Up Selling Tasks
$this->CreateUpSellingTasks($bean);
// Create Cross Selling Opportunities
$this->CreateCrossSellingOpportunities($bean);
}
function handleException(&$bean, $event, $arguments=''){
}
function processRecord(&$bean, $event, $arguments=''){
}
function SendEmail($emailsTo, $emailSubject, $emailBody){
$emailObj = new Email();
$defaults = $emailObj->getSystemDefaultEmail();
$mail = new SugarPHPMailer();
$mail->setMailerForSystem();
$mail->From = $defaults['email'];
$mail->FromName = $defaults['name'];
$mail->ClearAllRecipients();
$mail->ClearReplyTos();
$mail->Subject=from_html($emailSubject);
$mail->Body=$emailBody;
$mail->AltBody=from_html($emailBody);
$mail->prepForOutbound();
foreach($emailsTo as &$value){
$mail->AddAddress($value);
}
if(@$mail->Send()){
}
}
function CreateTaskAndCallForNewOpportunity($bean){
$timeDate = new TimeDate();
if(empty($bean->fetched_row['id'])){
$task = new Task();
$task->name = "Send Proposal";
$task->priority = "High";
$task->status = "Not Started";
$task->date_due = $timeDate->getNow(true)->modify("+1 days")->asDb();
$task->parent_type = "Opportunities";
$task->parent_id = $bean->id;
$task->assigned_user_id = $bean->assigned_user_id;
$task->save();
$call = new Call();
$call->name = "Follow up";
$call->direction = "Outbound";
$call->status = "Planned";
$call->duration_hours = 0;
$call->duration_minutes = 15;
$call->date_start = $timeDate->getNow(true)->modify("+2 days")->asDb();
$call->parent_type = "Opportunities";
$call->parent_id = $bean->id;
$call->assigned_user_id = $bean->assigned_user_id;
$call->save();
}
}
function CreateUpSellingTasks($bean){
if($bean->fetched_row['name'] !== $bean->name && $bean->name === "Basic Product/Service"){
$task = new Task();
$task->name = "Offer Premium Product/Service";
$task->parent_type = "Opportunities";
$task->parent_id = $bean->id;
$task->assigned_user_id = $bean->assigned_user_id;
$task->save();
SugarApplication::appendErrorMessage('You have a new Task for Up Selling Opportunity');
}
}
function CreateCrossSellingOpportunities($bean){
if($bean->fetched_row['name'] !== $bean->name && $bean->name === "Product/Service A"){
$opportunity = new Opportunity();
$opportunity->name = "Product/Service B";
$opportunity->account_id = $bean->account_id;
$opportunity->sales_stage = "Prospecting";
$opportunity->assigned_user_id = $bean->assigned_user_id;
$opportunity->save();
SugarApplication::appendErrorMessage('You have a new Opportunity for Cross Selling Opportunity');
}
}
function NotifySalesManagers($bean){
global $sugar_config;
$amount_limit = 1000;
if($bean->sales_stage === "Negotiation/Review" && $bean->fetched_row['sales_stage'] === "Proposal/Price Quote" && $bean->amount >= $amount_limit){
SugarApplication::appendErrorMessage('You have changed the opportunity '.$bean->name.' (greater than '.$amount_limit.') to Negotiation/Review.');
$emailsTo = array();
$emailSubject = "Opportunity Alert";
$emailBody = "The Opportunity ".$bean->name." has changed to Negotiation/Review<br />
You can see the opportunity here:<br />
<a href=\"".$sugar_config['site_url']."/index.php?module=Opportunities&action=DetailView&record=".$bean->id."\">".$bean->name."</a>";
$role_id = "<sales-manager-role-id>";
$aclrole = new ACLRole();
if(!is_null($aclrole->retrieve($role_id))){
$users = $aclrole->get_linked_beans('users','User');
foreach($users as $user){
$emailsTo[] = $user->email1;
}
}
$this->SendEmail($emailsTo, $emailSubject, $emailBody);
}
}
function SendToERP($bean){
if($bean->sales_stage === "Closed Won" && ($bean->sales_stage != $bean->fetched_row['sales_stage'])){
$account = new Account();
if(!is_null($account->retrieve($bean->account_id))){
$url = "<your-erp-rest-url>";
$fields = array(
'account_id' => $account->id,
'account_name' => $account->name,
'opportunity_id' => $bean->id,
'opportunity_name' => $bean->name,
'opportunity_amount' => $bean->amount,
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $fields);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
SugarApplication::appendErrorMessage('The Opportunity '.$bean->name.' has been sent to ERP');
}
}
}
}
?>