diff --git a/CRM/Core/BAO/FinancialTrxn.php b/CRM/Core/BAO/FinancialTrxn.php index 53f7143b39d4..31e04c38d3d0 100644 --- a/CRM/Core/BAO/FinancialTrxn.php +++ b/CRM/Core/BAO/FinancialTrxn.php @@ -475,8 +475,24 @@ public static function getPartialPaymentWithType($entityId, $entityName = 'parti $statusId = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'); $refundStatusId = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Refunded'); - $toFinancialAccount = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($financialTypeId, 'Accounts Receivable Account is'); - $feeFinancialAccount = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($financialTypeId, 'Expense Account is'); + $filteredFinancialAccounts = array(); + $filteredFinancialAccountRel = array( + 'Accounts Receivable Account is', + 'Expense Account is', + ); + if (CRM_Contribute_BAO_Contribution::checkContributeSettings('deferred_revenue_enabled')) { + $filteredFinancialAccountRel = array_merge($filteredFinancialAccountRel, array( + 'Deferred Revenue Account is', + 'Income Account is', + )); + } + + foreach ($filteredFinancialAccountRel as $financialAccountRel) { + $financialAccount = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($financialTypeId, $financialAccountRel); + if ($financialAccount) { + $filteredFinancialAccounts[] = $financialAccount; + } + } if (empty($lineItemTotal)) { $lineItemTotal = CRM_Price_BAO_LineItem::getLineTotal($contributionId); @@ -485,9 +501,10 @@ public static function getPartialPaymentWithType($entityId, $entityName = 'parti SELECT SUM(ft.total_amount) FROM civicrm_financial_trxn ft INNER JOIN civicrm_entity_financial_trxn eft ON (ft.id = eft.financial_trxn_id AND eft.entity_table = 'civicrm_contribution' AND eft.entity_id = {$contributionId}) -WHERE ft.to_financial_account_id != {$toFinancialAccount} AND ft.to_financial_account_id != {$feeFinancialAccount} +WHERE ft.to_financial_account_id NOT IN ( " . implode(', ', $filteredFinancialAccounts) . " ) AND ft.status_id IN ({$statusId}, {$refundStatusId}) "; + $ftTotalAmt = CRM_Core_DAO::singleValueQuery($sqlFtTotalAmt); $value = 0; if (!$ftTotalAmt) { diff --git a/tests/phpunit/CRM/Financial/BAO/FinancialAccountTest.php b/tests/phpunit/CRM/Financial/BAO/FinancialAccountTest.php index ae06581e4db3..c67c76422b95 100644 --- a/tests/phpunit/CRM/Financial/BAO/FinancialAccountTest.php +++ b/tests/phpunit/CRM/Financial/BAO/FinancialAccountTest.php @@ -352,6 +352,30 @@ public function testGetAllDeferredFinancialAccount() { $this->assertEquals(array_count_values($expected), array_count_values($financialAccount), "The two arrays are not the same"); } + /** + * CRM-20037: Test balance due amount, if contribution is done using deferred Financial Type + */ + public function testBalanceDueIfDeferredRevenueEnabled() { + Civi::settings()->set('contribution_invoice_settings', array('deferred_revenue_enabled' => '1')); + $deferredFinancialTypeID = $this->_createDeferredFinancialAccount(); + + $totalAmount = 100.00; + $contribution = $this->callAPISuccess('Contribution', 'create', array( + 'contact_id' => $this->individualCreate(), + 'receive_date' => '20120511', + 'total_amount' => $totalAmount, + 'financial_type_id' => $deferredFinancialTypeID, + 'non_deductible_amount' => 10.00, + 'fee_amount' => 5.00, + 'net_amount' => 95.00, + 'source' => 'SSF', + 'contribution_status_id' => 1, + )); + $balance = CRM_Core_BAO_FinancialTrxn::getPartialPaymentWithType($contribution['id'], 'contribution', FALSE, $totalAmount); + $this->assertEquals(0.0, $balance); + Civi::settings()->revert('contribution_invoice_settings'); + } + /** * Helper function to create deferred financial account. */