diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index d5f7a0b76e0c..6511e166cae3 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -885,14 +885,24 @@ public static function &exportableFields($checkPermission = TRUE) { } /** - * @param $contributionId - * @param $participantId - * @param array $financialTrxn + * Record an activity when a payment is received. + * + * @todo this is intended to be moved to payment BAO class as a protected function + * on that class. Currently being cleaned up. The addActivityForPayment doesn't really + * merit it's own function as it makes the code less rather than more readable. + * + * @param int $contributionId + * @param int $participantId + * @param string $totalAmount + * @param string $currency + * @param string $trxnDate * - * @param $financialTrxn + * @throws \CRM_Core_Exception + * @throws \CiviCRM_API3_Exception */ - protected static function recordPaymentActivity($contributionId, $participantId, $financialTrxn) { - $activityType = ($financialTrxn->total_amount < 0) ? 'Refund' : 'Payment'; + protected static function recordPaymentActivity($contributionId, $participantId, $totalAmount, $currency, $trxnDate) { + $activityType = ($totalAmount < 0) ? 'Refund' : 'Payment'; + if ($participantId) { $inputParams['id'] = $participantId; $values = []; @@ -907,8 +917,8 @@ protected static function recordPaymentActivity($contributionId, $participantId, $entityObj->find(TRUE); $title = ts('Contribution'); } - - self::addActivityForPayment($entityObj->contact_id, $financialTrxn, $activityType, $title, $contributionId); + // @todo per block above this is not a logical splitting off of functionality. + self::addActivityForPayment($entityObj->contact_id, $activityType, $title, $contributionId, $totalAmount, $currency, $trxnDate); } /** @@ -3935,7 +3945,7 @@ public static function recordAdditionalPayment($contributionId, $trxnsData, $pay } if (!empty($financialTrxn)) { - self::recordPaymentActivity($contributionId, $participantId, $financialTrxn); + self::recordPaymentActivity($contributionId, $participantId, $financialTrxn->total_amount, $financialTrxn->currency, $financialTrxn->trxn_date); return $financialTrxn; } @@ -3943,17 +3953,20 @@ public static function recordAdditionalPayment($contributionId, $trxnsData, $pay /** * @param int $targetCid - * @param $trxnObj * @param $activityType * @param string $title * @param int $contributionId + * @param string $totalAmount + * @param string $currency + * @param string $trxn_date * - * @throws CRM_Core_Exception + * @throws \CRM_Core_Exception + * @throws \CiviCRM_API3_Exception */ - public static function addActivityForPayment($targetCid, $trxnObj, $activityType, $title, $contributionId) { - $paymentAmount = CRM_Utils_Money::format($trxnObj->total_amount, $trxnObj->currency); + public static function addActivityForPayment($targetCid, $activityType, $title, $contributionId, $totalAmount, $currency, $trxn_date) { + $paymentAmount = CRM_Utils_Money::format($totalAmount, $currency); $subject = "{$paymentAmount} - Offline {$activityType} for {$title}"; - $date = CRM_Utils_Date::isoToMysql($trxnObj->trxn_date); + $date = CRM_Utils_Date::isoToMysql($trxn_date); // source record id would be the contribution id $srcRecId = $contributionId; @@ -3975,8 +3988,7 @@ public static function addActivityForPayment($targetCid, $trxnObj, $activityType $activityParams['source_contact_id'] = $id; $activityParams['target_contact_id'][] = $targetCid; } - // @todo use api. - CRM_Activity_BAO_Activity::create($activityParams); + civicrm_api3('Activity', 'create', $activityParams); } /** diff --git a/tests/phpunit/CRM/Contribute/Form/AdditionalPaymentTest.php b/tests/phpunit/CRM/Contribute/Form/AdditionalPaymentTest.php index 7143f3b4062c..efedd30dfd75 100644 --- a/tests/phpunit/CRM/Contribute/Form/AdditionalPaymentTest.php +++ b/tests/phpunit/CRM/Contribute/Form/AdditionalPaymentTest.php @@ -168,7 +168,7 @@ public function testAddPaymentForPartialyPaidContribution() { /** * Test the submit function that completes the partially paid Contribution with multiple payments. */ - public function testMultiplePaymentForPartialyPaidContribution() { + public function testMultiplePaymentForPartiallyPaidContribution() { $this->createContribution('Partially paid'); // pay additional amount @@ -184,10 +184,14 @@ public function testMultiplePaymentForPartialyPaidContribution() { 'activity_type_id' => 'Payment', 'options' => ['sort' => 'id'], 'sequential' => 1, + 'return' => ['target_contact_id', 'assignee_contact_id', 'subject'], ])['values']; $this->assertEquals(2, count($activities)); $this->assertEquals('$ 50.00 - Offline Payment for Contribution', $activities[0]['subject']); $this->assertEquals('$ 20.00 - Offline Payment for Contribution', $activities[1]['subject']); + $this->assertEquals(CRM_Core_Session::singleton()->getLoggedInContactID(), $activities[0]['source_contact_id']); + $this->assertEquals([$this->_individualId], $activities[0]['target_contact_id']); + $this->assertEquals([], $activities[0]['assignee_contact_id']); } /**