-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWebToOpportunity.php
88 lines (75 loc) · 3.19 KB
/
WebToOpportunity.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
<?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
********************************************************************************/
// http://<your-crm-url>/index.php?entryPoint=WebToOpportunity&account_name=Audox Ingeniería Ltda.&account_website=www.audox.cl&contact_first_name=Javier&contact_last_name=Núñez&contact_email=janunez@audox.cl&contact_mobile=+56 9 9675 0572&opportunity_name=CRM Consulting Services&opportunity_amount=2000
if(!defined('sugarEntry')) define('sugarEntry', true);
global $db;
global $current_user;
$timeDate = new TimeDate();
$current_user->id = 1;
$account_name = $_REQUEST['account_name'];
$account_website = $_REQUEST['account_website'];
$contact_first_name = $_REQUEST['contact_first_name'];
$contact_last_name = $_REQUEST['contact_last_name'];
$contact_email = $_REQUEST['contact_email'];
$contact_mobile = $_REQUEST['contact_mobile'];
$opportunity_name = $_REQUEST['opportunity_name'];
$opportunity_amount = $_REQUEST['opportunity_amount'];
// Search account by url domain and Update it or Create it
$account = new Account();
if(!is_null($account->retrieve_by_string_fields(array('name' => $account_name)))){
if(empty($account->name)) $account->name = $account_name;
if(empty($account->website)) $account->website = $account_website;
if(empty($account->assigned_user_id)) $account->assigned_user_id = 1;
$account->save();
}
else{
$account->name = $account_name;
$account->website = $account_website;
$account->assigned_user_id = 1;
$account->save();
}
// Search contact by email and Update it or Create it
$query = "SELECT contacts.id FROM contacts WHERE contacts.deleted=0 AND contacts.id IN (
SELECT eabr.bean_id
FROM email_addr_bean_rel eabr JOIN email_addresses ea
ON (ea.id = eabr.email_address_id)
WHERE eabr.bean_module = 'Contacts' AND ea.email_address = '".$contact_email."' AND eabr.primary_address = 1 AND eabr.deleted=0)";
$result = $db->query($query);
$row = $db->fetchByAssoc($result);
$contact = new Contact();
if(!is_null($contact->retrieve($row['id']))){
if(empty($contact->first_name)) $contact->first_name = $contact_first_name;
if(empty($contact->last_name)) $contact->last_name = $contact_last_name;
if(empty($contact->mobile)) $contact->mobile = $contact_mobile;
if(empty($contact->assigned_user_id)) $contact->assigned_user_id = 1;
$contact->save();
}
else{
$contact->first_name = $contact_first_name;
$contact->last_name = $contact_last_name;
$contact->email1 = $contact_email;
$contact->mobile = $contact_mobile;
$contact->assigned_user_id = 1;
$contact->save();
$contact->load_relationship('accounts');
$contact->accounts->add($account->id);
}
// Create the Opportunity
$opportunity = new Opportunity();
$opportunity->name = $opportunity_name;
$opportunity->amount = $opportunity_amount;
$opportunity->date_closed = $timeDate->getNow(true)->asDbDate();
$opportunity->sales_stage = "Closed Won";
$opportunity->account_id = $account->id;
$opportunity->assigned_user_id = 1;
$opportunity->save();
$opportunity->load_relationship('contacts');
$opportunity->contacts->add($contact->id);
?>