Skip to content

Commit

Permalink
Support syncing a specific location type for email instead of always …
Browse files Browse the repository at this point in the history
…using primary
  • Loading branch information
mattwire committed Jun 19, 2022
1 parent d50201f commit cf4469d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
17 changes: 16 additions & 1 deletion CRM/Civixero/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ public function push($params) {
try {
$records = civicrm_api3('account_contact', 'get', [
'accounts_needs_update' => 1,
'api.contact.get' => 1,
'plugin' => $this->_plugin,
'connector_id' => $params['connector_id'],
]
Expand All @@ -110,6 +109,22 @@ public function push($params) {
//@todo pass limit through from params to get call
foreach ($records['values'] as $record) {
try {
// Get the contact data. This includes the "Primary" email as $contact['email'] if set.
$contact = civicrm_api3('Contact', 'get', ['id' => $record['contact_id']]);
// See if we have an email for the preferred location type?
$locationTypeToSync = (int)\Civi::settings()->get('xero_sync_location_type');
if ($locationTypeToSync !== 0) {
$email = \Civi\Api4\Email::get(FALSE)
->addWhere('contact_id', '=', $record['contact_id'])
->addWhere('location_type_id', '=', $locationTypeToSync)
->execute()
->first();
if (!empty($email['email'])) {
// Yes, we have an email. "Overwrite" the primary email in the data passed to Xero.
$contact['email'] = $email['email'];
}
}

$accountsContactID = !empty($record['accounts_contact_id']) ? $record['accounts_contact_id'] : NULL;
$accountsContact = $this->mapToAccounts($record['api.contact.get']['values'][0], $accountsContactID);
if ($accountsContact === FALSE) {
Expand Down
23 changes: 23 additions & 0 deletions settings/Civixero.setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
'AUTHORISED' => 'Approved',
];

$locationTypes = \Civi\Api4\LocationType::get(FALSE)
->addSelect('id', 'display_name')
->execute();
$locTypes = [0 => ts('- Primary -')];
foreach ($locationTypes as $locationType) {
$locTypes[$locationType['id']] = $locationType['display_name'];
}

return [
// Removed settings.
// xero_key, xero_public_certificate.
Expand Down Expand Up @@ -141,4 +149,19 @@
'quick_form_type' => 'Element',
'html_attributes' => ['Inclusive' => 'Inclusive', 'Exclusive' => 'Exclusive'],
],
'xero_sync_location_type' => [
'group_name' => 'Xero Settings',
'group' => 'xero',
'name' => 'xero_sync_location_type',
'type' => 'Int',
'is_domain' => 1,
'is_contact' => 0,
'default' => 0,
'title' => 'CiviCRM location type to sync to Xero (will fallback to - Primary - if location type is empty)',
'description' => 'Select the preferred location type to sync to Xero. Will fallback to "Primary" if not set.',
'help_text' => '',
'html_type' => 'Select',
'quick_form_type' => 'Element',
'html_attributes' => $locTypes,
],
];

0 comments on commit cf4469d

Please sign in to comment.