From b8adb851ce5b45ecb01ed848f958415998b01523 Mon Sep 17 00:00:00 2001 From: jitendrapurohit Date: Wed, 8 Apr 2015 12:07:11 +0530 Subject: [PATCH] net amount sometimes incorrect for events involving partial payments. --- CRM/Contribute/BAO/Contribution.php | 13 ++++++++----- CRM/Event/BAO/Participant.php | 3 ++- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index 7e1568ebfb..2c4a800274 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -2779,7 +2779,7 @@ WHERE contribution_id = %1 "; 'trxn_date' => date('YmdHis'), 'total_amount' => $totalAmount, 'fee_amount' => CRM_Utils_Array::value('fee_amount', $params), - 'net_amount' => CRM_Utils_Array::value('net_amount', $params), + 'net_amount' => CRM_Utils_Array::value('net_amount', $params, $params['total_amount']), 'currency' => $params['contribution']->currency, 'trxn_id' => $params['contribution']->trxn_id, 'status_id' => $statusId, @@ -3297,7 +3297,7 @@ WHERE contribution_id = %1 "; // update statuses // criteria for updates contribution total_amount == financial_trxns of partial_payments - $sql = "SELECT SUM(ft.total_amount) as sum_of_payments + $sql = "SELECT SUM(ft.total_amount) as sum_of_payments, SUM(ft.net_amount) as net_amount_total FROM civicrm_financial_trxn ft LEFT JOIN civicrm_entity_financial_trxn eft ON (ft.id = eft.financial_trxn_id) @@ -3306,7 +3306,9 @@ WHERE eft.entity_table = 'civicrm_contribution' AND ft.to_financial_account_id != {$toFinancialAccount} AND ft.status_id = {$statusId} "; - $sumOfPayments = CRM_Core_DAO::singleValueQuery($sql); + $query = CRM_Core_DAO::executeQuery($sql); + $query->fetch(); + $sumOfPayments = $query->sum_of_payments; // update statuses if ($contributionDAO->total_amount == $sumOfPayments) { @@ -3315,8 +3317,9 @@ WHERE eft.entity_table = 'civicrm_contribution' $contributionDAO->contribution_status_id = $statusId; $contributionDAO->cancel_date = 'null'; $contributionDAO->cancel_reason = NULL; - $netAmount = !empty($trxnsData['net_amount']) ? $trxnsData['net_amount'] : $trxnsData['total_amount']; - $contributionDAO->net_amount = $contributionDAO->net_amount + $netAmount; + $netAmount = !empty($trxnsData['net_amount']) ? NULL : $trxnsData['total_amount']; + $contributionDAO->net_amount = $query->net_amount_total + $netAmount; + $contributionDAO->fee_amount = $contributionDAO->total_amount - $contributionDAO->net_amount; $contributionDAO->save(); //Change status of financial record too diff --git a/CRM/Event/BAO/Participant.php b/CRM/Event/BAO/Participant.php index a01cb5dbd9..fc96c69606 100644 --- a/CRM/Event/BAO/Participant.php +++ b/CRM/Event/BAO/Participant.php @@ -2093,7 +2093,8 @@ WHERE (li.entity_table = 'civicrm_participant' AND li.entity_id = {$participantI if (!$skip) { $updatedContributionDAO->contribution_status_id = $contributionStatusVal; } - $updatedContributionDAO->total_amount = $updatedAmount; + $updatedContributionDAO->total_amount = $updatedContributionDAO->net_amount = $updatedAmount; + $updatedContributionDAO->fee_amount = 0; $updatedContributionDAO->tax_amount = $taxAmount; $updatedContributionDAO->save(); // adjusted amount financial_trxn creation -- 2.25.1