Skip to content

Commit

Permalink
Merge pull request #167 from mjwconsult/clarifyvars
Browse files Browse the repository at this point in the history
Rename Xero 'ID' vars for clarity. If invoice UUID is passed in add it to the invoice
  • Loading branch information
eileenmcnaughton authored Oct 31, 2024
2 parents 587ed28 + 33fa53c commit 3d92acb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
12 changes: 6 additions & 6 deletions CRM/Civixero/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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'] ?? '',
Expand Down Expand Up @@ -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);
Expand Down
23 changes: 15 additions & 8 deletions CRM/Civixero/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand All @@ -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',
Expand Down Expand Up @@ -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,
Expand All @@ -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);
}

/**
Expand Down

0 comments on commit 3d92acb

Please sign in to comment.