Skip to content

Commit

Permalink
support deferred_revenue in getPartialPaymentWithType(..)
Browse files Browse the repository at this point in the history
  • Loading branch information
monishdeb committed Apr 25, 2017
1 parent 663b3b2 commit ec14568
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
23 changes: 20 additions & 3 deletions CRM/Core/BAO/FinancialTrxn.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down
24 changes: 24 additions & 0 deletions tests/phpunit/CRM/Financial/BAO/FinancialAccountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down

0 comments on commit ec14568

Please sign in to comment.