diff --git a/CRM/Civixero/Contact.php b/CRM/Civixero/Contact.php index 861047b..8a6145a 100644 --- a/CRM/Civixero/Contact.php +++ b/CRM/Civixero/Contact.php @@ -196,8 +196,8 @@ public function push(array $params, int $limit = 10): bool { $contact = array_merge($contact, $contactAddress); } - $accountsContactID = !empty($record['accounts_contact_id']) ? $record['accounts_contact_id'] : NULL; - $accountsContact = $this->mapToAccounts($contact, $accountsContactID); + $xeroContactUUID = !empty($record['accounts_contact_id']) ? $record['accounts_contact_id'] : NULL; + $accountsContact = $this->mapToAccounts($contact, $xeroContactUUID); if ($accountsContact === FALSE) { $result = FALSE; $responseErrors = []; @@ -436,12 +436,12 @@ public function getContactsRequiringPushUpdate(array $params, int $limit): array * * @param array $contact * Contact Array as returned from API - * @param string|null $accountsContactID + * @param string|null $xeroContactUUID * * @return array|bool * Contact Object/ array as expected by accounts package */ - protected function mapToAccounts(array $contact, ?string $accountsContactID) { + protected function mapToAccounts(array $contact, ?string $xeroContactUUID) { $new_contact = [ 'Name' => $contact['display_name'] . ' - ' . $contact['id'], 'FirstName' => $contact['first_name'] ?? '', @@ -470,8 +470,8 @@ protected function mapToAccounts(array $contact, ?string $accountsContactID) { ], ], ]; - if (!empty($accountsContactID)) { - $new_contact['ContactID'] = $accountsContactID; + if (!empty($xeroContactUUID)) { + $new_contact['ContactID'] = $xeroContactUUID; } $proceed = TRUE; CRM_Accountsync_Hook::accountPushAlterMapped('contact', $contact, $proceed, $new_contact); diff --git a/CRM/Civixero/Invoice.php b/CRM/Civixero/Invoice.php index 645b712..0f96a61 100644 --- a/CRM/Civixero/Invoice.php +++ b/CRM/Civixero/Invoice.php @@ -230,13 +230,14 @@ public function push($params, $limit = 10) { * - receive date * - source * - contact_id - * @param int $accountsID + * @param ?string $xeroInvoiceUUID + * The Xero invoice uuid. * * @return array|bool * Contact Object/ array as expected by accounts package * @throws \CRM_Core_Exception */ - protected function mapToAccounts($invoiceData, $accountsID) { + protected function mapToAccounts(array $invoiceData, ?string $xeroInvoiceUUID) { // Get the tax mode from the CiviCRM setting. This should be 'exclusive' if // tax is enabled (but for historical reasons we force that later on). $line_amount_types = Civi::settings()->get('xero_tax_mode'); @@ -288,6 +289,11 @@ protected function mapToAccounts($invoiceData, $accountsID) { 'LineAmountTypes' => $line_amount_types, 'LineItems' => ['LineItem' => $lineItems], ]; + if (!empty($xeroInvoiceUUID)) { + if (!empty($xeroContactUUID)) { + $new_invoice['InvoiceID'] = $xeroContactUUID; + } + } /* Use due date and period from the invoice settings when available. */ $invoiceDueDate = Civi::settings()->get('invoice_due_date'); @@ -310,14 +316,15 @@ protected function mapToAccounts($invoiceData, $accountsID) { * Map fields for a cancelled contribution to be updated to Xero. * * @param int $contributionID - * @param string|null $accounts_invoice_id + * @param ?string $xeroInvoiceUUID + * The Xero invoice uuid. * * @return array */ - protected function mapCancelled(int $contributionID, ?string $accounts_invoice_id): array { + protected function mapCancelled(int $contributionID, ?string $xeroInvoiceUUID): array { return [ 'Invoice' => [ - 'InvoiceID' => $accounts_invoice_id, + 'InvoiceID' => $xeroInvoiceUUID, 'InvoiceNumber' => $contributionID, 'Type' => 'ACCREC', 'Reference' => 'Cancelled', @@ -455,7 +462,7 @@ protected function getAccountsInvoice(array $record) { return FALSE; } - $accountsInvoiceID = $record['accounts_invoice_id'] ?? NULL; + $xeroInvoiceUUID = $record['accounts_invoice_id'] ?? NULL; $contributionID = $record['contribution_id']; $civiCRMInvoice = civicrm_api3('AccountInvoice', 'getderived', [ 'id' => $contributionID, @@ -467,10 +474,10 @@ protected function getAccountsInvoice(array $record) { $cancelledStatuses = ['Failed', 'Cancelled']; if (empty($civiCRMInvoice) || in_array($contributionStatus, $cancelledStatuses)) { - return $this->mapCancelled($contributionID, $accountsInvoiceID); + return $this->mapCancelled($contributionID, $xeroInvoiceUUID); } - return $this->mapToAccounts($civiCRMInvoice, $accountsInvoiceID); + return $this->mapToAccounts($civiCRMInvoice, $xeroInvoiceUUID); } /**