Skip to content

Commit

Permalink
Merge pull request #1 from seamuslee001/wahyukodar-master
Browse files Browse the repository at this point in the history
Fix up errors in to_account_id and ensure that financial_item is enti…
  • Loading branch information
wahyukodar authored Sep 9, 2016
2 parents e723092 + bf4385c commit 62cd50b
Showing 1 changed file with 35 additions and 13 deletions.
48 changes: 35 additions & 13 deletions CRM/Contribute/BAO/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ public static function add(&$params, $ids = array()) {
// CRM-16189
CRM_Financial_BAO_FinancialAccount::checkFinancialTypeHasDeferred($params, $contributionID);

if (!isset($params['tax_amount']) && $setPrevContribution && (isset($params['total_amount']) || isset
($params['financial_type_id']))) {
if (!isset($params['tax_amount']) && $setPrevContribution && (isset($params['total_amount']) ||
isset($params['financial_type_id']))) {
$params = CRM_Contribute_BAO_Contribution::checkTaxAmount($params);
}

Expand Down Expand Up @@ -3462,29 +3462,32 @@ public static function updateFinancialAccounts(&$params, $context = NULL, $skipT
$itemAmount = $params['trxnParams']['total_amount'] + $cancelledTaxAmount;
}
elseif ($context == 'changePaymentInstrument') {
$deferredFinancialAccount = CRM_Utils_Array::value('deferred_financial_account_id', $params);
if (empty($deferredFinancialAccount)) {
$relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Deferred Revenue Account is' "));
$deferredFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($params['prevContribution']->financial_type_id, $relationTypeId);
}
$lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($params['prevContribution']->id, 'DESC', FALSE, NULL, $deferredFinancialAccount);
if ($params['trxnParams']['total_amount'] < 0) {
$deferredFinancialAccount = CRM_Utils_Array::value('deferred_financial_account_id', $params);
if (empty($deferredFinancialAccount)) {
$relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Deferred Revenue Account is' "));
$deferredFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($params['prevContribution']->financial_type_id, $relationTypeId);
}
$lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($params['prevContribution']->id, 'DESC', FALSE, NULL, $deferredFinancialAccount);
if (!empty($lastFinancialTrxnId['financialTrxnId'])) {
$params['trxnParams']['to_financial_account_id'] = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialTrxn', $lastFinancialTrxnId['financialTrxnId'], 'to_financial_account_id');
if ($params['total_amount'] > 0) {
$params['trxnParams']['to_financial_account_id'] = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialTrxn', $lastFinancialTrxnId['financialTrxnId'], 'to_financial_account_id');
$params['trxnParams']['payment_instrument_id'] = $params['prevContribution']->payment_instrument_id;
}
else {
$params['trxnParams']['to_financial_account_id'] = $params['to_financial_account_id'];
$params['trxnParams']['payment_instrument_id'] = $params['contribution']->payment_instrument_id;
}
}
}
else {
$params['trxnParams']['to_financial_account_id'] = $params['to_financial_account_id'];
if ($params['total_amount'] < 0) {
$params['trxnParams']['net_amount'] = $params['trxnParams']['total_amount'];
$params['trxnParams']['payment_instrument_id'] = $params['prevContribution']->payment_instrument_id;
$params['trxnParams']['to_financial_account_id'] = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialTrxn', $lastFinancialTrxnId['financialTrxnId'], 'to_financial_account_id');
}
else {
$params['trxnParams']['to_financial_account_id'] = $params['to_financial_account_id'];
$params['trxnParams']['payment_instrument_id'] = $params['contribution']->payment_instrument_id;
}
}
Expand Down Expand Up @@ -3532,7 +3535,6 @@ public static function updateFinancialAccounts(&$params, $context = NULL, $skipT
return;
}
}

$trxn = CRM_Core_BAO_FinancialTrxn::create($params['trxnParams']);
$params['entity_id'] = $trxn->id;
if ($context != 'changePaymentInstrument') {
Expand Down Expand Up @@ -3602,6 +3604,28 @@ public static function updateFinancialAccounts(&$params, $context = NULL, $skipT
}
}
}
if ($context == 'changePaymentInstrument') {
foreach ($params['line_item'] as $lineitems) {
foreach ($lineitems as $fieldValueId => $fieldValues) {
$prevFinancialItem = CRM_Financial_BAO_FinancialItem::getPreviousFinancialItem($fieldValues['id']);
if (!CRM_Utils_Rule::currencyCode($trxn->currency)) {
$trxn->currency = CRM_Core_Config::singleton()->defaultCurrency;
}

// save to entity_financial_trxn table
$entityFinancialTrxnParams = array(
'entity_table' => "civicrm_financial_item",
'entity_id' => $prevFinancialItem->id,
'financial_trxn_id' => $trxn->id,
'amount' => $trxn->total_amount,
'currency' => $trxn->currency,
);
$entityTrxn = new CRM_Financial_DAO_EntityFinancialTrxn();
$entityTrxn->copyValues($entityFinancialTrxnParams);
$entityTrxn->save();
}
}
}
CRM_Core_BAO_FinancialTrxn::createDeferredTrxn(CRM_Utils_Array::value('line_item', $params), $params['contribution'], TRUE, $context);
}

Expand Down Expand Up @@ -4061,14 +4085,12 @@ public static function getPaymentInfo($id, $component, $getTrxnInfo = FALSE, $us
WHERE con.id = %1 AND ft.to_financial_account_id <> %3
GROUP BY ft.id";

$queryParams = array(
1 => array($contributionId, 'Integer'),
2 => array($feeFinancialAccount, 'Integer'),
3 => array($arAccount, 'Integer'),
);
$resultDAO = CRM_Core_DAO::executeQuery($sql, $queryParams);

$statuses = CRM_Contribute_PseudoConstant::contributionStatus();

while ($resultDAO->fetch()) {
Expand Down

0 comments on commit 62cd50b

Please sign in to comment.