Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename Xero 'ID' vars for clarity. If invoice UUID is passed in add it to the invoice #167

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Comment on lines +292 to +296
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only "change". The variable was being passed in but never used, now it will be if available (same as what is done on contact).


/* 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