Skip to content

Commit

Permalink
Merge pull request #157 from mjwconsult/invoicetweaks
Browse files Browse the repository at this point in the history
Invoice pull fixes.
  • Loading branch information
eileenmcnaughton authored Aug 21, 2024
2 parents 3876129 + cb8966b commit 2969ed4
Showing 1 changed file with 35 additions and 8 deletions.
43 changes: 35 additions & 8 deletions CRM/Civixero/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ public function pull(array $params): int {
// So check if anything actually changed before updating.
if ($accountInvoiceParams[$key] !== $accountInvoice[$key]) {
// Something changed, update AccountInvoice in DB
if (!empty($accountInvoice['contribution_id'])) {
// If the accountInvoice already has a contribution ID don't try to overwrite it with the one we derived from InvoiceNumber.
// Probably we manually reconciled it at some point.
unset($accountInvoiceParams['contribution_id']);
}
$newAccountInvoice = AccountInvoice::update(FALSE)
->setValues($accountInvoiceParams)
->addWhere('id', '=', $accountInvoice['id'])
Expand All @@ -131,15 +136,14 @@ public function pull(array $params): int {
}
}
}
if (!empty($params['create_contributions_in_civicrm'])) {
$this->createContributionFromAccountsInvoice($invoice, $accountInvoiceParams);
}
}
catch (CRM_Core_Exception $e) {
$errors[] = E::ts('Failed to store %1 (%2)', [1 => $invoice['InvoiceNumber'], 2 => $invoice['InvoiceID']])
. E::ts(' with error ') . $e->getMessage();
}

if (!empty($params['create_contributions_in_civicrm'])) {
$this->createContributionFromAccountsInvoice($invoice, $accountInvoiceParams);
}
}
}
if ($errors) {
Expand Down Expand Up @@ -681,12 +685,15 @@ private function createContributionFromAccountsInvoice(array $invoice, array $ac
->execute()
->first();
if (!empty($accountInvoiceParams['contribution_id'])) {
// \Civi::log()->error(__FUNCTION__ . ': AccountsInvoice is already linked to a contribution: ' . print_r($invoice, TRUE));
// \Civi::log('civixero')->debug(__FUNCTION__ . ': AccountsInvoice is already linked to a contribution: ' . print_r($invoice, TRUE));
return FALSE;
}

$accountsContactID = $invoice['Contact']['ContactID'] ?? NULL;
if (empty($accountsContactID)) {
\Civi::log()->error(__FUNCTION__ . ': missing ContactID in AccountsInvoice: ' . print_r($invoice, TRUE));
$errorMessage = __FUNCTION__ . ': missing ContactID in AccountsInvoice';
$this->recordAccountInvoiceError($accountInvoiceParams['id'], $errorMessage);
\Civi::log('civixero')->debug($errorMessage . ': ' . print_r($invoice, TRUE));
return FALSE;
}

Expand All @@ -695,11 +702,15 @@ private function createContributionFromAccountsInvoice(array $invoice, array $ac
->execute()
->first();
if (empty($accountContact)) {
\Civi::log()->error(__FUNCTION__ . ': no AccountsContact found: ' . print_r($invoice, TRUE));
$errorMessage = __FUNCTION__ . ': no AccountsContact found';
$this->recordAccountInvoiceError($accountInvoiceParams['id'], $errorMessage);
\Civi::log('civixero')->debug($errorMessage . ': ' . print_r($invoice, TRUE));
return FALSE;
}
if (empty($accountContact['contact_id'])) {
\Civi::log()->error(__FUNCTION__ . ': AccountsContact is not matched to a CiviCRM Contact ID: ' . print_r($invoice, TRUE));
$errorMessage = __FUNCTION__ . ': AccountsContact is not matched to a CiviCRM Contact ID';
$this->recordAccountInvoiceError($accountInvoiceParams['id'], $errorMessage);
\Civi::log('civixero')->debug($errorMessage . ': ' . print_r($invoice, TRUE));
return FALSE;
}

Expand Down Expand Up @@ -744,4 +755,20 @@ private function createContributionFromAccountsInvoice(array $invoice, array $ac
return TRUE;
}

/**
* @param string $accountInvoiceID
* @param string $message
*
* @return void
* @throws \CRM_Core_Exception
* @throws \Civi\API\Exception\UnauthorizedException
*/
private function recordAccountInvoiceError(string $accountInvoiceID, string $message) {
AccountInvoice::update(FALSE)
->addValue('error_data', $message)
->addValue('is_error_resolved', FALSE)
->addWhere('id', '=', $accountInvoiceID)
->execute();
}

}

0 comments on commit 2969ed4

Please sign in to comment.