/
pass1.php
156 lines (118 loc) · 3.86 KB
/
pass1.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
<?php
define('COUNT_CREATED', 'COUNT_CREATED');
define('COUNT_EXISTING', 'COUNT_EXISTING');
define('COUNT_NEXT_PASS', 'COUNT_NEXT_PASS');
define('LOGGING_CHANNEL', 'pass1');
define('LOGGING_LEVEL', 'DEBUG');
define('LOGGING_FORK_TO_STDOUT', true);
define('DRY_RUN', true);
require __DIR__ . '/bootstrap.php';
use \RuntimeException;
use \SObject;
use DaybreakStudios\Common\IO\CsvFileReader;
use DaybreakStudios\Common\IO\IOException;
use DaybreakStudios\Common\Utility\Counter;
use DaybreakStudios\Salesforce\Client;
$logger = getActiveLogger();
$client = new Client($config['sf.username'], $config['sf.token'], SYSTEM_WSDL_FILE);
if (!file_exists(SYSTEM_INVALIDS_FILE))
throw new IOException(sprintf(MSG_FILE_MISSING, SYSTEM_INVALIDS_FILE));
$f = fopen(SYSTEM_INVALIDS_FILE, 'r');
if ($f === false)
throw new IOException(sprintf(MSG_FILE_NOT_READABLE, SYSTEM_INVALIDS_FILE));
$reader = new CsvFileReader($f);
$reader->addField('phone', 'cleanPhone');
$invalids = [];
while (!$reader->eof())
$invalids[$reader->read()->phone] = null;
$logger->info(sprintf(MSG_INVALIDS_READ, sizeof($invalids), sizeof($invalids) !== 1 ? 's' : ''));
$reader->close();
if (!file_exists(SYSTEM_CSV_FILE))
throw new IOException('Could not locate ' . SYSTEM_CSV_FILE);
$f = fopen(SYSTEM_CSV_FILE, 'r');
if ($f === false)
throw new IOException('Could not open ' . SYSTEM_CSV_FILE . ' for reading');
$reader = new CsvFileReader($f);
$reader->addFields($config['csv.fields']);
$pos = 0;
$counter = new Counter(true, [
COUNT_CREATED,
COUNT_EXISTING,
COUNT_NEXT_PASS,
]);
while (!$reader->eof() && ++$pos) {
$row = $reader->read();
$logger->debug('-- Read CSV row', [ $row ]);
$qb = $client->createQueryBuilder();
$qb
->select('Id')
->from('Account')
->where('Name = :dealership');
$account = $qb
->setMaxResults(1)
->setParameter('dealership', $row->dealership)
->getQuery()
->getOneOrNullResult();
if ($account === null) {
$counter->inc(COUNT_NEXT_PASS);
continue;
}
$logger->debug('Found Account with Id ' . $account->Id, [ $account ]);
$qb = $client->createQueryBuilder();
$qb
->select('Id')
->from('Contact')
->where('AccountId = :id')
->andWhere($qb->expr()->orX(
'Phone like :phone',
'Phone_2__c like :phone',
'Phone like :alt',
'Phone_2__c like :alt'
));
$contact = $qb
->setMaxResults(1)
->setParameter('id', $account->Id)
->setParameter('phone', getPhoneLikeStatement($row->phone))
->setParameter('alt', getPhoneLikeStatement($row->altPhone))
->getQuery()
->getOneOrNullResult();
if ($contact !== null) {
$counter->inc(COUNT_EXISTING);
continue;
}
$sob = new SObject();
$sob->type = 'Contact';
$sob->fields = [
'AccountId' => $account->Id,
'FirstName' => $row->firstName,
'LastName' => $row->lastName,
'Phone' => $row->phone,
'Phone_2__c' => $row->altPhone,
'Job_Title__c' => $row->position,
'Street' => $row->street,
'City' => $row->city,
'State' => $row->state,
'PostalCode' => $row->zip,
];
$logger->info('Creating new Contact on Account (' . $account->Id . ')', [ $sob ]);
if (!DRY_RUN) {
$result = $client->create($sob);
if (sizeof($result) === 0)
throw new RuntimeException(sprintf(MSG_SF_API_UNKNOWN_ERROR, $pos));
else if (!$result[0]->success)
throw getSalesforceException($result[0]);
$logger->debug(sprintf(MSG_SF_CONTACT_CREATED, $result[0]->id), [ $result ]);
} else
$logger->info(sprintf(MSG_SF_CONTACT_CREATED . ' from row %d', $row->phone, $pos));
$counter->inc(COUNT_CREATED);
}
$logger->info(sprintf(MSG_PASS_SUMMARY,
1,
$counter->get(COUNT_CREATED),
$counter->get(COUNT_CREATED) !== 1 ? 's' : '',
$counter->get(COUNT_EXISTING),
$counter->get(COUNT_EXISTING) !== 1 ? 's' : '',
$counter->get(COUNT_NEXT_PASS),
$counter->get(COUNT_NEXT_PASS) !== 1 ? 's' : ''
));
?>