Repository navigation
Expand file tree
/
Copy pathclient_import.php
More file actions
156 lines (138 loc) · 4.8 KB
/
Copy pathclient_import.php
File metadata and controls
156 lines (138 loc) · 4.8 KB
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
<?php
// Ubersmith API Example
// Client Import
// This script demonstrates how to perform the initial import of clients into Ubersmith.
// Display all errors
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(E_ALL);
// Path to Ubersmith API Configuration file
require_once __DIR__ .'/config.inc.php';
// Configuration Extension
$config = array_merge($config, [
// Parameters for API call
'params' => [
// Salesperson Admin ID (should be created before running this script)
'salesperson_id' => 0,
// Salesperson Relationship ID (should be verified before running this script)
'salesperson_relationship_id' => 1,
// Field Mapping
// (Each field corresponds to a column in the spreadsheet)
'mapping' => [
// Basic Client Info
'company' => 0,
'first' => 1,
'last' => 2,
'phone' => 3,
'email' => 4,
'address' => 9,
'city' => 10,
'state' => 11,
'zip' => 12,
// Contact Info
'tech_contact_name' => 5,
'tech_contact_email' => 6,
'misc_contact_name' => 7,
'misc_contact_email' => 8,
// Custom Data field named "thing1" should be created before running this script
'customdata_thing1' => 13,
// Custom Data field named "thing2" should be created before running this script
'customdata_thing2' => 14,
],
],
]);
// Initialize Ubersmith API Client
require_once $config['api_client_path'];
$api_client = new uber_api_client($config['domain'], $config['api_username'], $config['api_token']);
// Get CSV contents (don't forget to set its newline format to Unix LF!)
$csv = array_map('str_getcsv', file('customer_list.csv'));
// Initialize counter and begin new line for status output
$count = 0;
print "\n";
// Begin creating clients
try {
foreach ($csv as $row) {
// Prepare Client data
$data = [
'company' => '',
'first' => '',
'last' => '',
'phone' => '',
'email' => '',
'address' => '',
'city' => '',
'state' => '',
'zip' => '',
];
foreach ($data as $key => $value) {
// Fill data (default to empty string if unavailable)
$data[$key] = empty($row[$config['params']['mapping'][$key]]) ? '' : $row[$config['params']['mapping'][$key]];
}
// Add Custom Data
if (!empty($row[$config['params']['mapping']['customdata_thing1']])) {
$data['meta_thing1'] = $row[$config['params']['mapping']['customdata_thing1']];
}
if (!empty($row[$config['params']['mapping']['customdata_thing2']])) {
$data['meta_thing2'] = $row[$config['params']['mapping']['customdata_thing2']];
}
// Add Client
$client_id = $api_client->call('client.add', $data);
if (empty($client_id) || is_array($client_id)) {
print 'Error while adding Client ['. $row[$config['params']['mapping']['email']] .']: ';
var_dump($client_id);
continue;
}
$count++;
// Add Contact(s)
$contacts = [
[
'real_name' => $row[$config['params']['mapping']['tech_contact_name']],
'email' => $row[$config['params']['mapping']['tech_contact_email']],
'description' => 'Technical Contact',
],
[
'real_name' => $row[$config['params']['mapping']['misc_contact_name']],
'email' => $row[$config['params']['mapping']['misc_contact_email']],
'description' => 'Miscellaneous Contact',
],
];
foreach ($contacts as $contact) {
// Skip contacts that lack both a name and email address
if (empty($contact['real_name']) && empty($contact['email'])) {
continue;
}
// Prepare Contact data
$data = $contact;
$data['client_id'] = $client_id;
// Add Contact
$result = $api_client->call('client.contact_add', $data);
if (empty($result) || is_array($result)) {
print 'Error while adding '. $data['description'] .' for client ['. $row[7] .']: ';
var_dump($result);
continue;
}
}
if (!empty($config['params']['salesperson_id'])) {
// Prepare Salesperson data
$data = [
'client_id' => $client_id,
'person_id' => $config['params']['salesperson_id'], // Salesperson ID
'people_client_type_id' => $config['params']['salesperson_relationship_id'], // Salesperson Relationship
];
// Add Salesperson
$result = $api_client->call('uber.admin_client_relationship_add', $data);
if (empty($result) || is_array($result)) {
print 'Error while adding Salesperson for Client ['. $row[$config['params']['mapping']['email']] .']: ';
var_dump($result);
continue;
}
}
}
} catch (Exception $e) {
print 'Error: '. $e->getMessage() .' (Code '. $e->getCode() .')'."\n\n";
print $count .' clients imported.'."\n";
// Early exit so success message below doesn't get printed
exit;
}
print 'Success: '. $count .' clients imported.'."\n";
// end of script