forked from eileenmcnaughton/nz.co.fuzion.civixero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCivixero.php
executable file
·501 lines (462 loc) · 13.8 KB
/
Civixero.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
<?php
require_once 'Civixero.civix.php';
/**
* Implementation of hook_civicrm_config
*
* @param \CRM_Core_Config $config
*/
function civixero_civicrm_config(&$config) {
_civixero_civix_civicrm_config($config);
require_once __DIR__ . '/vendor/autoload.php';
}
/**
* Implementation of hook_civicrm_xmlMenu
*
* @param $files array(string)
*/
function civixero_civicrm_xmlMenu(&$files) {
_civixero_civix_civicrm_xmlMenu($files);
}
/**
* Implementation of hook_civicrm_install
*/
function civixero_civicrm_install() {
return _civixero_civix_civicrm_install();
}
/**
* Implementation of hook_civicrm_uninstall
*/
function civixero_civicrm_uninstall() {
return _civixero_civix_civicrm_uninstall();
}
/**
* Implementation of hook_civicrm_enable
*/
function civixero_civicrm_enable() {
_civixero_civix_civicrm_enable();
}
/**
* Implementation of hook_civicrm_disable
*/
function civixero_civicrm_disable() {
return _civixero_civix_civicrm_disable();
}
/**
* Implementation of hook_civicrm_upgrade
*
* @param $op string, the type of operation being performed; 'check' or 'enqueue'
* @param $queue CRM_Queue_Queue, (for 'enqueue') the modifiable list of pending up upgrade tasks
*
* @return mixed based on op. for 'check', returns array(boolean) (TRUE if upgrades are pending)
* for 'enqueue', returns void
*/
function civixero_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
return _civixero_civix_civicrm_upgrade($op, $queue);
}
/**
* Implements hook_civicrm_managed().
*
* Generate a list of entities to create/deactivate/delete when this module
* is installed, disabled, uninstalled.
*
* @param array $entities
* Existing entity array
*/
function civixero_civicrm_managed(&$entities) {
if (civixero_is_extension_installed('nz.co.fuzion.connectors')) {
$entities[] = [
'name' => 'CiviXero connector Type',
'entity' => 'ConnectorType',
'module' => 'nz.co.fuzion.civixero',
'params' => [
'name' => 'CiviXero',
'description' => 'CiviXero connector information',
'module' => 'accountsync',
'function' => 'credentials',
'plugin' => 'xero',
'field1_label' => 'Xero Key',
'field2_label' => 'Xero Secret',
'field3_label' => 'Xero Public Certificate Path',
'field4_label' => 'Xero Private Key Path',
'field5_label' => 'Settings',
'version' => 3,
],
];
}
_civixero_civix_civicrm_managed($entities);
}
/**
* Is a given extension installed.
*
* Currently adding very roughly just to support checking if connectors is installed.
*
* I like to snaffle hacks into their own function for easy later fixing :-)
*
* @param string $extension
*
* @return bool
* @todo - test using CRM_Extension_System::singleton()->getManager()->getStatus($key)
*
*/
function civixero_is_extension_installed($extension) {
if ($extension === 'nz.co.fuzion.connectors') {
if (function_exists('connectors_civicrm_entityTypes')) {
return TRUE;
}
}
return FALSE;
}
/**
* Implements hook_civicrm_caseTypes().
*
* Generate a list of case-types
*
* Note: This hook only runs in CiviCRM 4.4+.
*
* @param array $caseTypes
*/
function civixero_civicrm_caseTypes(&$caseTypes) {
_civixero_civix_civicrm_caseTypes($caseTypes);
}
/**
* Implements hook_civicrm_config().
*
* @param $metaDataFolders
*/
function civixero_civicrm_alterSettingsFolders(&$metaDataFolders) {
$extRoot = __DIR__ . DIRECTORY_SEPARATOR;
$extDir = $extRoot . 'settings';
if (!in_array($extDir, $metaDataFolders, TRUE)) {
$metaDataFolders[] = $extDir;
}
}
/**
* Implements hook_civicrm_navigationMenu().
*
* Adds entries to the navigation menu.
*
* @param array $menu
*/
function civixero_civicrm_navigationMenu(&$menu) {
_Civixero_civix_insert_navigation_menu($menu, 'Administer', [
'label' => 'Xero',
'name' => 'Xero',
'url' => NULL,
'permission' => 'administer CiviCRM',
'operator' => NULL,
'separator' => NULL,
]);
_Civixero_civix_insert_navigation_menu($menu, 'Administer/Xero', [
'label' => 'Xero Settings',
'name' => 'Xero Settings',
'url' => 'civicrm/xero/settings',
'permission' => 'administer CiviCRM',
'operator' => NULL,
'separator' => 0,
]);
_Civixero_civix_insert_navigation_menu($menu, 'Administer/Xero', [
'label' => 'Xero Error Logs',
'name' => 'XeroErrorLogs',
'url' => NULL,
'permission' => 'administer CiviCRM',
'operator' => NULL,
'separator' => 1,
]);
_Civixero_civix_insert_navigation_menu($menu, 'Administer/Xero', [
'label' => 'Synchronize contacts',
'name' => 'Contact Sync',
'url' => 'civicrm/a/#/accounts/contact/sync',
'permission' => 'administer CiviCRM',
'operator' => NULL,
'separator' => 1,
]);
_Civixero_civix_insert_navigation_menu($menu, 'Administer/Xero/XeroErrorLogs', [
'label' => 'Contact Errors',
'name' => 'Contact Errors',
'url' => 'civicrm/xero/errorlog',
'permission' => 'administer CiviCRM',
'operator' => NULL,
'separator' => 0,
]);
_Civixero_civix_insert_navigation_menu($menu, 'Administer/Xero/XeroErrorLogs', [
'label' => 'Invoice Errors',
'name' => 'Invoice Errors',
'url' => 'civicrm/xero/errorlog?for=invoice',
'permission' => 'administer CiviCRM',
'operator' => NULL,
'separator' => 0,
]);
_Civixero_civix_insert_navigation_menu($menu, 'Administer/Xero/', [
'label' => 'Xero Authorize',
'name' => 'Xero Authorize',
'url' => 'civicrm/xero/authorize',
'permission' => 'administer CiviCRM',
'operator' => NULL,
'separator' => 0,
]);
}
/**
* Gettings contributions of sinlge contact
*
* @param int $contactid
*
* @return array
* @throws \CiviCRM_API3_Exception
*/
function getContactContributions($contactid) {
$contributions = civicrm_api3('Contribution', 'get', [
'contact_id' => $contactid,
'return' => ['contribution_id'],
'sequential' => TRUE,
])['values'];
$contributions = array_column($contributions, 'id');
return $contributions;
}
/**
* Get AccountInvoice data for contributions with errors.
*
* @param array $contributions
*
* @return array
*
* @throws \CiviCRM_API3_Exception
*/
function getErroredInvoicesOfContributions($contributions) {
return civicrm_api3('AccountInvoice', 'get', [
'plugin' => 'xero',
'sequential' => TRUE,
'contribution_id' => ['IN' => $contributions],
'error_data' => ['<>' => ''],
]);
}
/**
* Implementation of hook_civicrm_check.
*
* Add a check to the status page. Check if there are any account contact or invoice sync errors.
*
* @param $page
*
* @throws \CiviCRM_API3_Exception
*/
function civixero_civicrm_check(&$messages) {
$accountContactErrors = civicrm_api3("AccountContact", "getcount", [
"error_data" => ["NOT LIKE" => "%error_cleared%"],
"plugin" => "xero",
]);
$accountInvoiceErrors = civicrm_api3("AccountInvoice", "getcount", [
"error_data" => ["NOT LIKE" => "%error_cleared%"],
"plugin" => "xero",
]);
$errorMessage = "";
$errorsPageUrl = CRM_Utils_System::url('civicrm/xero/errorlog');
if ($accountContactErrors > 0) {
$errorMessage .= 'Found ' . $accountContactErrors . ' contact sync errors. <a href="' . $errorsPageUrl . '" target="_blank">Click here</a> to resolve them.';
if ($accountInvoiceErrors > 0) {
$errorMessage .= "<br><br>";
}
}
if ($accountInvoiceErrors > 0) {
$errorMessage .= 'Found ' . $accountInvoiceErrors . ' invoice sync errors. <a href="' . $errorsPageUrl . '?for=invoice" target="_blank">Click here</a> to resolve them.';
}
if ($accountInvoiceErrors > 0 || $accountContactErrors > 0) {
$messages[] = new CRM_Utils_Check_Message(
'civixero_sync_errors',
$errorMessage,
'Xero Sync Errors',
\Psr\Log\LogLevel::ERROR,
'fa-refresh'
);
}
$clientID = Civi::settings()->get('xero_client_id');
$clientSecret = Civi::settings()->get('xero_client_secret');
$accessTokenData = Civi::settings()->get('xero_access_token');
if (!$clientID || !$clientSecret) {
$messages[] = new CRM_Utils_Check_Message(
'civixero_clientrequired',
ts('Please configure a Client ID and Client Secret from your Xero app.'),
ts('Missing Xero App Details'),
\Psr\Log\LogLevel::WARNING,
'fa-flag'
);
}
elseif (empty($accessTokenData['access_token'])) {
$messages[] = new CRM_Utils_Check_Message(
'civixero_authorizationrequired',
ts('Please Authorize with Xero to enable a connection.'),
ts('Xero Authorization Required'),
\Psr\Log\LogLevel::WARNING,
'fa-flag'
);
}
}
/**
* Implements hook pageRun().
*
* Add Xero links to contact summary
*
* @param $page
*
* @throws \CRM_Core_Exception
*/
function civixero_civicrm_pageRun(&$page) {
$pageName = get_class($page);
if ($pageName !== 'CRM_Contact_Page_View_Summary' || !CRM_Core_Permission::check('view all contacts')) {
return;
}
if (($contactID = $page->getVar('_contactId')) != FALSE) {
CRM_Civixero_Page_Inline_ContactSyncStatus::addContactSyncStatusBlock($page, $contactID);
CRM_Civixero_Page_Inline_ContactSyncLink::addContactSyncLinkBlock($page, $contactID);
CRM_Civixero_Page_Inline_InvoiceSyncLink::addInvoiceSyncLinkBlock($page, $contactID);
CRM_Civixero_Page_Inline_ContactSyncErrors::addContactSyncErrorsBlock($page, $contactID);
CRM_Civixero_Page_Inline_InvoiceSyncErrors::addInvoiceSyncErrorsBlock($page, $contactID);
CRM_Core_Region::instance('contact-basic-info-left')->add([
'template' => 'CRM/Civixero/ContactSyncBlock.tpl',
]);
}
CRM_Core_Resources::singleton()->addScriptFile('nz.co.fuzion.civixero', 'js/civixero_errors.js');
}
/**
* Get available connectors.
*
* @return string
*/
function _civixero_get_connectors() {
static $connectors = [];
if (empty($connectors)) {
try {
$connectors = civicrm_api3('connector', 'get', ['connector_type_id' => 'CiviXero']);
$connectors = $connectors['values'];
}
catch (CiviCRM_API3_Exception $e) {
$connectors = [0 => 0];
}
}
return $connectors;
}
/**
* @param $objectName
* @param array $headers
* @param $values
* @param $selector
*/
function civixero_civicrm_searchColumns($objectName, &$headers, &$values) {
if ($objectName === 'contribution') {
foreach ($values as &$value) {
try {
$invoiceID = civicrm_api3('AccountInvoice', 'getvalue', [
'plugin' => 'xero',
'contribution_id' => $value['contribution_id'],
'return' => 'accounts_invoice_id',
]);
$value['contribution_status'] .= "<a href='https://go.xero.com/AccountsReceivable/View.aspx?InvoiceID=" . $invoiceID . "'> <p>Xero</p></a>";
}
catch (Exception $e) {
continue;
}
}
}
}
/**
* Map xero accounts data to generic data.
*
* @param array $accountsData
* @param string $entity
* @param string $plugin
*/
function civixero_civicrm_mapAccountsData(&$accountsData, $entity, $plugin) {
if ($plugin !== 'xero' || $entity !== 'contact') {
return;
}
$accountsData['civicrm_formatted'] = [];
$mappedFields = [
'Name' => 'display_name',
'FirstName' => 'first_name',
'LastName' => 'last_name',
'EmailAddress' => 'email',
];
foreach ($mappedFields as $xeroField => $civicrmField) {
if (!empty($accountsData[$xeroField])) {
$accountsData['civicrm_formatted'][$civicrmField] = $accountsData[$xeroField];
}
}
if (is_array($accountsData['Addresses']) && is_array($accountsData['Addresses']['Address'])) {
foreach ($accountsData['Addresses']['Address'] as $address) {
if (count($address) > 1) {
$addressMappedFields = [
'AddressLine1' => 'street_address',
'City' => 'city',
'PostalCode' => 'postal_code',
];
foreach ($addressMappedFields as $xeroField => $civicrmField) {
if (!empty($address[$xeroField])) {
$accountsData['civicrm_formatted'][$civicrmField] = $address[$xeroField];
}
}
break;
}
}
}
if (is_array($accountsData['Phones']) && is_array($accountsData['Phones']['Phone'])) {
foreach ($accountsData['Phones']['Phone'] as $address) {
if (count($address) > 1) {
$addressMappedFields = [
'PhoneNumber' => 'phone',
];
foreach ($addressMappedFields as $xeroField => $civicrmField) {
if (!empty($address[$xeroField])) {
$accountsData['civicrm_formatted'][$civicrmField] = $address[$xeroField];
}
}
break;
}
}
}
}
/**
* Implements hook_civicrm_accountsync_plugins().
*
* @param $plugins
*/
function civixero_civicrm_accountsync_plugins(&$plugins) {
$plugins[] = 'xero';
}
/**
* Implements hook_civicrm_contactSummaryBlocks().
*
* @link https://github.com/civicrm/org.civicrm.contactlayout
*
* @param $blocks
*/
function civixero_civicrm_contactSummaryBlocks(&$blocks) {
$blocks += [
'civixeroblock' => [
'title' => ts('Civi Xero'),
'blocks' => [],
],
];
$blocks['civixeroblock']['blocks']['contactsyncstatus'] = [
'title' => ts('Contact Sync Status'),
'tpl_file' => 'CRM/Civixero/Page/Inline/ContactSyncStatus.tpl',
'edit' => FALSE,
];
$blocks['civixeroblock']['blocks']['contactsyncerrors'] = [
'title' => ts('Contact Sync Errors'),
'tpl_file' => 'CRM/Civixero/Page/Inline/ContactSyncErrors.tpl',
'edit' => FALSE,
];
$blocks['civixeroblock']['blocks']['invoicesyncerrors'] = [
'title' => ts('Invoice Sync Errors'),
'tpl_file' => 'CRM/Civixero/Page/Inline/InvoiceSyncErrors.tpl',
'edit' => FALSE,
];
$blocks['civixeroblock']['blocks']['invoicesynclink'] = [
'title' => ts('Invoice Sync Link'),
'tpl_file' => 'CRM/Civixero/Page/Inline/InvoiceSyncLink.tpl',
'edit' => FALSE,
];
$blocks['civixeroblock']['blocks']['contactsynclink'] = [
'title' => ts('Contact Sync Link'),
'tpl_file' => 'CRM/Civixero/Page/Inline/ContactSyncLink.tpl',
'edit' => FALSE,
];
}