Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REF] Cleanup input & output on paymentActivityCreate & improve test #14269

Merged
merged 1 commit into from
May 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 28 additions & 16 deletions CRM/Contribute/BAO/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -3935,25 +3945,28 @@ 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;
}

}

/**
* @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;

Expand All @@ -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);
}

/**
Expand Down
6 changes: 5 additions & 1 deletion tests/phpunit/CRM/Contribute/Form/AdditionalPaymentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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']);
}

/**
Expand Down