From: Jitendra Purohit Date: Wed, 13 May 2020 12:52:39 +0000 (+0530) Subject: dev/core#1758 - Fix check number and trxn id values on record payment form X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=fdd77f884a5b8c4d8142c82a7d53d81ed4764f2f;p=civicrm-core.git dev/core#1758 - Fix check number and trxn id values on record payment form --- diff --git a/CRM/Financial/BAO/Payment.php b/CRM/Financial/BAO/Payment.php index 8a06f7f26c..049d675e71 100644 --- a/CRM/Financial/BAO/Payment.php +++ b/CRM/Financial/BAO/Payment.php @@ -163,10 +163,41 @@ class CRM_Financial_BAO_Payment { // change status to refunded. self::updateContributionStatus($contribution['id'], 'Refunded'); } + self::updateRelatedContribution($params, $params['contribution_id']); CRM_Contribute_BAO_Contribution::recordPaymentActivity($params['contribution_id'], CRM_Utils_Array::value('participant_id', $params), $params['total_amount'], $trxn->currency, $trxn->trxn_date); return $trxn; } + /** + * Function to update contribution's check_number and trxn_id by + * concatenating values from financial trxn's check_number and trxn_id respectively + * + * @param array $params + * @param int $contributionID + */ + public static function updateRelatedContribution($params, $contributionID) { + $contributionDAO = new CRM_Contribute_DAO_Contribution(); + $contributionDAO->id = $contributionID; + $contributionDAO->find(TRUE); + + foreach (['trxn_id', 'check_number'] as $fieldName) { + if (!empty($params[$fieldName])) { + $values = []; + if (!empty($contributionDAO->$fieldName)) { + $values = explode(',', $contributionDAO->$fieldName); + } + // if submitted check_number or trxn_id value is + // already present then ignore else add to $values array + if (!in_array($params[$fieldName], $values)) { + $values[] = $params[$fieldName]; + } + $contributionDAO->$fieldName = implode(',', $values); + } + } + + $contributionDAO->save(); + } + /** * Send an email confirming a payment that has been received. * diff --git a/CRM/Financial/Form/PaymentEdit.php b/CRM/Financial/Form/PaymentEdit.php index f131ef7677..b40f1325a3 100644 --- a/CRM/Financial/Form/PaymentEdit.php +++ b/CRM/Financial/Form/PaymentEdit.php @@ -214,7 +214,7 @@ class CRM_Financial_Form_PaymentEdit extends CRM_Core_Form { civicrm_api3('FinancialTrxn', 'create', $submittedValues); } - self::updateRelatedContribution($submittedValues, $this->_contributionID); + CRM_Financial_BAO_Payment::updateRelatedContribution($submittedValues, $this->_contributionID); } /** @@ -230,35 +230,6 @@ class CRM_Financial_Form_PaymentEdit extends CRM_Core_Form { $this->submit($params); } - /** - * Function to update contribution's check_number and trxn_id by - * concatenating values from financial trxn's check_number and trxn_id respectively - * - * @param array $params - * @param int $contributionID - */ - public static function updateRelatedContribution($params, $contributionID) { - $contributionDAO = new CRM_Contribute_DAO_Contribution(); - $contributionDAO->id = $contributionID; - $contributionDAO->find(TRUE); - - foreach (['trxn_id', 'check_number'] as $fieldName) { - if (!empty($params[$fieldName])) { - if (!empty($contributionDAO->$fieldName)) { - $values = explode(',', $contributionDAO->$fieldName); - // if submitted check_number or trxn_id value is - // already present then ignore else add to $values array - if (!in_array($params[$fieldName], $values)) { - $values[] = $params[$fieldName]; - } - $contributionDAO->$fieldName = implode(',', $values); - } - } - } - - $contributionDAO->save(); - } - /** * Get payment fields */ diff --git a/tests/phpunit/api/v3/PaymentTest.php b/tests/phpunit/api/v3/PaymentTest.php index 927e11bb16..52a15df535 100644 --- a/tests/phpunit/api/v3/PaymentTest.php +++ b/tests/phpunit/api/v3/PaymentTest.php @@ -874,14 +874,28 @@ class api_v3_PaymentTest extends CiviUnitTestCase { 'contribution_status_id' => 2, ]; $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams); + $checkNumber1 = 'C111'; $this->callAPISuccess('Payment', 'create', [ 'contribution_id' => $contribution['id'], 'total_amount' => 50, - 'payment_instrument_id' => 'Cash', + 'payment_instrument_id' => 'Check', + 'check_number' => $checkNumber1, ]); $payments = $this->callAPISuccess('Payment', 'get', ['contribution_id' => $contribution['id']])['values']; $this->assertCount(1, $payments); $this->validateAllPayments(); + + $checkNumber2 = 'C222'; + $this->callAPISuccess('Payment', 'create', [ + 'contribution_id' => $contribution['id'], + 'total_amount' => 20, + 'payment_instrument_id' => 'Check', + 'check_number' => $checkNumber2, + ]); + $expectedConcatanatedCheckNumbers = implode(',', [$checkNumber1, $checkNumber2]); + //Assert check number is concatenated on the main contribution. + $contributionValues = $this->callAPISuccess('Contribution', 'getsingle', ['id' => $contribution['id']]); + $this->assertEquals($expectedConcatanatedCheckNumbers, $contributionValues['check_number']); } /**