Skip to content

Commit

Permalink
Merge pull request #184 from compucorp/COMCL-594-exclude-directdebit-…
Browse files Browse the repository at this point in the history
…from-receivable

COMCL-594: Exclude direct debit and offline recur payment from having account receivable payment processor
  • Loading branch information
olayiwola-compucorp authored Jul 8, 2024
2 parents f12b801 + 0050d46 commit b8c3ec3
Showing 1 changed file with 52 additions and 5 deletions.
57 changes: 52 additions & 5 deletions Civi/Financeextras/Hook/Post/ContributionCreation.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,17 @@ private function updateContribution() {
$companyRecord = \CRM_Core_DAO::executeQuery("SELECT invoice_prefix, next_invoice_number, receivable_payment_method FROM financeextras_company WHERE contact_id = {$this->ownerOrganizationId} FOR UPDATE");
$companyRecord->fetch();

$this->setInvoiceNumber($companyRecord);
$this->setPaymentinstrumentId($companyRecord);
}

/**
* Sets the contribution invoice number.
*
* @param \CRM_Core_DAO|object $companyRecord
* The company record.
*/
private function setInvoiceNumber($companyRecord) {
$invoiceNumber = $companyRecord->next_invoice_number;
if (!empty($companyRecord->invoice_prefix)) {
$invoiceNumber = $companyRecord->invoice_prefix . $companyRecord->next_invoice_number;
Expand All @@ -212,14 +223,26 @@ private function updateContribution() {
\CRM_Core_DAO::executeQuery("UPDATE financeextras_company SET next_invoice_number = {$invoiceUpdateSQLFormula} WHERE contact_id = {$this->ownerOrganizationId}");

\CRM_Core_DAO::executeQuery("UPDATE civicrm_contribution SET invoice_number = '{$invoiceNumber}' WHERE id = {$this->contribution['id']}");
}

if ($this->isContributionNotRecordingPayment()) {
\CRM_Core_DAO::executeQuery("UPDATE civicrm_contribution SET payment_instrument_id = '{$companyRecord->receivable_payment_method}' WHERE id = {$this->contribution['id']}");
/**
* Sets the payment instrument id for the contribution.
*
* @param \CRM_Core_DAO|object $companyRecord
* The company record.
*/
public function setPaymentInstrumentId($companyRecord) {
if (!$this->isContributionNotRecordingPayment()) {
return;
}

$entityFinancialTransaction = \CRM_Core_DAO::executeQuery("SELECT financial_trxn_id FROM civicrm_entity_financial_trxn WHERE entity_id = {$this->contribution['id']} AND entity_table = 'civicrm_contribution'");
$entityFinancialTransaction->fetch();
$entityFinancialTransaction = \CRM_Core_DAO::executeQuery("SELECT financial_trxn_id FROM civicrm_entity_financial_trxn WHERE entity_id = {$this->contribution['id']} AND entity_table = 'civicrm_contribution'");
$entityFinancialTransaction->fetch();

\CRM_Core_DAO::executeQuery("UPDATE civicrm_financial_trxn SET payment_instrument_id = '{$companyRecord->receivable_payment_method}' WHERE id = {$entityFinancialTransaction->financial_trxn_id}");
$financialTrnxId = $entityFinancialTransaction->financial_trxn_id;
if ($this->contributionPaymentProcessorIsValid($financialTrnxId)) {
\CRM_Core_DAO::executeQuery("UPDATE civicrm_contribution SET payment_instrument_id = '{$companyRecord->receivable_payment_method}' WHERE id = {$this->contribution['id']}");
\CRM_Core_DAO::executeQuery("UPDATE civicrm_financial_trxn SET payment_instrument_id = '{$companyRecord->receivable_payment_method}' WHERE id = {$financialTrnxId}");
}
}

Expand Down Expand Up @@ -255,4 +278,28 @@ private function isContributionNotRecordingPayment(): bool {
);
}

/**
* Checks if the contribution payment processor type is not one of the specified types.
*
* @param int $financialTrnxId
* The ID of the financial transaction.
* @return bool
* TRUE if the payment processor type is not one of the specified types, FALSE otherwise.
*/
private function contributionPaymentProcessorIsValid($financialTrnxId): bool {
$paymentProcessorTypes = ['OfflineDirectDebit', 'Manual_Recurring_Payment'];
$financialTrxn = \Civi\Api4\FinancialTrxn::get(FALSE)
->addSelect('payment_processor_id:name', 'payment_processor.payment_processor_type_id:name', 'payment_instrument_id:name')
->addJoin('PaymentProcessor AS payment_processor', 'LEFT')
->addWhere('id', '=', $financialTrnxId)
->execute()->first() ?? NULL;

if (empty($financialTrxn)) {
return TRUE;
}

return !in_array($financialTrxn['payment_processor.payment_processor_type_id:name'], $paymentProcessorTypes)
&& $financialTrxn['payment_instrument_id:name'] != 'direct_debit';
}

}

0 comments on commit b8c3ec3

Please sign in to comment.