Skip to content

Commit

Permalink
Merge pull request #13694 from eileenmcnaughton/extract_refund
Browse files Browse the repository at this point in the history
Extract record refund function
  • Loading branch information
eileenmcnaughton authored Feb 25, 2019
2 parents 3291b80 + d3726f0 commit bad045b
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 35 deletions.
37 changes: 2 additions & 35 deletions CRM/Contribute/BAO/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -3903,43 +3903,10 @@ public static function recordAdditionalPayment($contributionId, $trxnsData, $pay
}
}
elseif ($paymentType == 'refund') {
$trxnsData['total_amount'] = $trxnsData['net_amount'] = -$trxnsData['total_amount'];
$trxnsData['from_financial_account_id'] = $arAccountId;
$trxnsData['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Refunded');
// record the entry
$financialTrxn = CRM_Contribute_BAO_Contribution::recordFinancialAccounts($params, $trxnsData);

// note : not using the self::add method,
// the reason because it performs 'status change' related code execution for financial records
// which in 'Pending Refund' => 'Completed' is not useful, instead specific financial record updates
// are coded below i.e. just updating financial_item status to 'Paid'
if ($updateStatus) {
CRM_Core_DAO::setFieldValue('CRM_Contribute_BAO_Contribution', $contributionId, 'contribution_status_id', $completedStatusId);
}
// add financial item entry
$lineItems = CRM_Price_BAO_LineItem::getLineItemsByContributionID($contributionDAO->id);
if (!empty($lineItems)) {
foreach ($lineItems as $lineItemId => $lineItemValue) {
// don't record financial item for cancelled line-item
if ($lineItemValue['qty'] == 0) {
continue;
}
$paid = $lineItemValue['line_total'] * ($financialTrxn->total_amount / $contributionDAO->total_amount);
$addFinancialEntry = array(
'transaction_date' => $financialTrxn->trxn_date,
'contact_id' => $contributionDAO->contact_id,
'amount' => round($paid, 2),
'currency' => $contributionDAO->currency,
'status_id' => $paidStatus,
'entity_id' => $lineItemId,
'entity_table' => 'civicrm_line_item',
);
$trxnIds['id'] = $financialTrxn->id;
CRM_Financial_BAO_FinancialItem::create($addFinancialEntry, NULL, $trxnIds);
}
}
$financialTrxn = CRM_Financial_BAO_Payment::recordRefundPayment($contributionId, $trxnsData, $updateStatus);
if ($participantId) {
// update participant status
// @todo this doesn't make sense...
$participantStatuses = CRM_Event_PseudoConstant::participantStatus();
$ids = CRM_Event_BAO_Participant::getParticipantIds($contributionId);
foreach ($ids as $val) {
Expand Down
63 changes: 63 additions & 0 deletions CRM/Financial/BAO/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,4 +289,67 @@ public static function filterUntestedTemplateVariables($params) {
return $filteredParams;
}

/**
* @param $contributionId
* @param $trxnData
* @param $updateStatus
* - deprecate this param
*
* @todo - make this protected once recordAdditionalPayment no longer calls it.
*
* @return CRM_Financial_DAO_FinancialTrxn
*/
public static function recordRefundPayment($contributionId, $trxnData, $updateStatus) {
$getInfoOf['id'] = $contributionId;
$defaults = array();
$contributionDAO = CRM_Contribute_BAO_Contribution::retrieve($getInfoOf, $defaults, CRM_Core_DAO::$_nullArray);

// build params for recording financial trxn entry
$params['contribution'] = $contributionDAO;
$params = array_merge($defaults, $params);
$params['skipLineItem'] = TRUE;
$params['payment_instrument_id'] = CRM_Utils_Array::value('payment_instrument_id', $trxnData, CRM_Utils_Array::value('payment_instrument_id', $params));

$paidStatus = CRM_Core_PseudoConstant::getKey('CRM_Financial_DAO_FinancialItem', 'status_id', 'Paid');
$arAccountId = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($contributionDAO->financial_type_id, 'Accounts Receivable Account is');
$completedStatusId = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed');

$trxnData['total_amount'] = $trxnData['net_amount'] = -$trxnData['total_amount'];
$trxnData['from_financial_account_id'] = $arAccountId;
$trxnData['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Refunded');
// record the entry
$financialTrxn = CRM_Contribute_BAO_Contribution::recordFinancialAccounts($params, $trxnData);

// note : not using the self::add method,
// the reason because it performs 'status change' related code execution for financial records
// which in 'Pending Refund' => 'Completed' is not useful, instead specific financial record updates
// are coded below i.e. just updating financial_item status to 'Paid'
if ($updateStatus) {
CRM_Core_DAO::setFieldValue('CRM_Contribute_BAO_Contribution', $contributionId, 'contribution_status_id', $completedStatusId);
}
// add financial item entry
$lineItems = CRM_Price_BAO_LineItem::getLineItemsByContributionID($contributionDAO->id);
if (!empty($lineItems)) {
foreach ($lineItems as $lineItemId => $lineItemValue) {
// don't record financial item for cancelled line-item
if ($lineItemValue['qty'] == 0) {
continue;
}
$paid = $lineItemValue['line_total'] * ($financialTrxn->total_amount / $contributionDAO->total_amount);
$addFinancialEntry = [
'transaction_date' => $financialTrxn->trxn_date,
'contact_id' => $contributionDAO->contact_id,
'amount' => round($paid, 2),
'currency' => $contributionDAO->currency,
'status_id' => $paidStatus,
'entity_id' => $lineItemId,
'entity_table' => 'civicrm_line_item',
];
$trxnIds = ['id' => $financialTrxn->id];
CRM_Financial_BAO_FinancialItem::create($addFinancialEntry, NULL, $trxnIds);
}
}
return $financialTrxn;
}

}

0 comments on commit bad045b

Please sign in to comment.