From cf4469d552a4600548cc9334ce1ff67d571a41db Mon Sep 17 00:00:00 2001 From: Matthew Wire Date: Sun, 19 Jun 2022 22:38:01 +0100 Subject: [PATCH] Support syncing a specific location type for email instead of always using primary --- CRM/Civixero/Contact.php | 17 ++++++++++++++++- settings/Civixero.setting.php | 23 +++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/CRM/Civixero/Contact.php b/CRM/Civixero/Contact.php index 46477b7d..228fe99f 100644 --- a/CRM/Civixero/Contact.php +++ b/CRM/Civixero/Contact.php @@ -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'], ] @@ -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) { diff --git a/settings/Civixero.setting.php b/settings/Civixero.setting.php index 3efa2220..f2733042 100755 --- a/settings/Civixero.setting.php +++ b/settings/Civixero.setting.php @@ -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. @@ -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, + ], ];