From 921bca19bdecae2a9c9bcd8d2320e6c69ab47160 Mon Sep 17 00:00:00 2001 From: Dave Greenberg Date: Thu, 28 Aug 2014 13:14:49 -0700 Subject: [PATCH] CRM-15158 - Fix handling of payments in Refund workflows. ---------------------------------------- * CRM-15158: https://issues.civicrm.org/jira/browse/CRM-15158 --- CRM/Contribute/BAO/Contribution.php | 28 +++++++++++++++---- CRM/Event/BAO/Participant.php | 9 +++++- .../CRM/Contribute/Form/Contribution.tpl | 9 +++--- 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index 0831b26f0a..eee3a4d96a 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -3139,7 +3139,17 @@ WHERE contribution_id = %1 "; $toFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($contributionDAO->financial_type_id, $relationTypeId); $trxnId = CRM_Core_BAO_FinancialTrxn::getBalanceTrxnAmt($contributionId, $contributionDAO->financial_type_id); - $trxnId = $trxnId['trxn_id']; + if (!empty($trxnId)) { + $trxnId = $trxnId['trxn_id']; + } + elseif (!empty($contributionDAO->payment_instrument_id)) { + $trxnId = CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($contributionDAO->payment_instrument_id); + } + else { + $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('financial_account_type', NULL, " AND v.name LIKE 'Asset' ")); + $queryParams = array(1 => array($relationTypeId, 'Integer')); + $trxnId = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_financial_account WHERE is_default = 1 AND financial_account_type_id = %1", $queryParams); + } // update statuses // criteria for updates contribution total_amount == financial_trxns of partial_payments @@ -3156,15 +3166,21 @@ WHERE eft.entity_table = 'civicrm_contribution' // update statuses if ($contributionDAO->total_amount == $sumOfPayments) { - // update contribution status - $contributionUpdate['id'] = $contributionId; - $contributionUpdate['contribution_status_id'] = $statusId; - $contributionUpdate['skipLineItem'] = TRUE; + // update contribution status and + // clean cancel info (if any) if prev. contribution was updated in case of 'Refunded' => 'Completed' + $contributionDAO->contribution_status_id = $statusId; + $contributionDAO->cancel_date = 'null'; + $contributionDAO->cancel_reason = NULL; + $contributionDAO->save(); + + //Change status of financial record too + $financialTrxn->status_id = $statusId; + $financialTrxn->save(); + // note : not using the self::add method, // the reason because it performs 'status change' related code execution for financial records // which in 'Partial Paid' => 'Completed' is not useful, instead specific financial record updates // are coded below i.e. just updating financial_item status to 'Paid' - $contributionDetails = CRM_Core_DAO::setFieldValue('CRM_Contribute_BAO_Contribution', $contributionId, 'contribution_status_id', $statusId); if ($participantId) { // update participant status diff --git a/CRM/Event/BAO/Participant.php b/CRM/Event/BAO/Participant.php index 000ba93761..f33b33a8b6 100644 --- a/CRM/Event/BAO/Participant.php +++ b/CRM/Event/BAO/Participant.php @@ -1984,6 +1984,9 @@ WHERE (li.entity_table = 'civicrm_participant' AND li.entity_id = {$participantI $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); $partiallyPaidStatusId = array_search('Partially paid', $contributionStatuses); $pendngRefundStatusId = array_search('Pending refund', $contributionStatuses); + $completedStatusId = array_search('Completed', $contributionStatuses); + + $updatedContributionDAO = new CRM_Contribute_BAO_Contribution(); if ($balanceAmt) { if ($balanceAmt > 0 && $paidAmount != 0) { @@ -1992,10 +1995,14 @@ WHERE (li.entity_table = 'civicrm_participant' AND li.entity_id = {$participantI elseif ($balanceAmt < 0 && $paidAmount != 0) { $contributionStatusVal = $pendngRefundStatusId; } + elseif ($paidAmount == 0) { + $contributionStatusVal = $completedStatusId; + $updatedContributionDAO->cancel_date = 'null'; + $updatedContributionDAO->cancel_reason = NULL; + } // update contribution status and total amount without trigger financial code // as this is handled in current BAO function used for change selection - $updatedContributionDAO = new CRM_Contribute_BAO_Contribution(); $updatedContributionDAO->id = $contributionId; $updatedContributionDAO->contribution_status_id = $contributionStatusVal; $updatedContributionDAO->total_amount = $updatedAmount; diff --git a/templates/CRM/Contribute/Form/Contribution.tpl b/templates/CRM/Contribute/Form/Contribution.tpl index dbde692575..b18562fccc 100644 --- a/templates/CRM/Contribute/Form/Contribution.tpl +++ b/templates/CRM/Contribute/Form/Contribution.tpl @@ -482,19 +482,20 @@ showHideCancelInfo(cj('#contribution_status_id')); cj('#contribution_status_id').change(function() { - showHideCancelInfo(this); + showHideCancelInfo(cj('#contribution_status_id')); } ); }); function showHideCancelInfo(obj) { - contributionStatus = cj(obj).val(); - if (contributionStatus == 3 || contributionStatus == 7) { + if (obj.find(":selected").text() == 'Refunded' || obj.find(":selected").text() == 'Cancelled') { cj('#cancelInfo').show( ); + cj('#total_amount').attr('readonly', true); } else { - status(); + status(); cj('#cancelInfo').hide( ); + cj("#total_amount").removeAttr('readonly'); } } -- 2.25.1