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

CRM-20513 Fix recur status update & et next recur date based on payme… #10294

Merged
merged 1 commit into from
May 4, 2017
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
3 changes: 2 additions & 1 deletion CRM/Contribute/BAO/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,8 @@ public static function add(&$params, $ids = array()) {
if (self::isUpdateToRecurringContribution($params)) {
CRM_Contribute_BAO_ContributionRecur::updateOnNewPayment(
(!empty($params['contribution_recur_id']) ? $params['contribution_recur_id'] : $params['prevContribution']->contribution_recur_id),
$contributionStatus[$params['contribution_status_id']]
$contributionStatus[$params['contribution_status_id']],
CRM_Utils_Array::value('receive_date', $params)
);
}

Expand Down
13 changes: 9 additions & 4 deletions CRM/Contribute/BAO/ContributionRecur.php
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,10 @@ public static function getRecurringFields() {
*
* @throws \CiviCRM_API3_Exception
*/
public static function updateOnNewPayment($recurringContributionID, $paymentStatus) {
public static function updateOnNewPayment($recurringContributionID, $paymentStatus, $effectiveDate) {
if (empty($effectiveDate)) {
$effectiveDate = date('Y-m-d');
}
if (!in_array($paymentStatus, array('Completed', 'Failed'))) {
return;
}
Expand Down Expand Up @@ -849,8 +852,8 @@ public static function updateOnNewPayment($recurringContributionID, $paymentStat
// Only update next sched date if it's empty or 'just now' because payment processors may be managing
// the scheduled date themselves as core did not previously provide any help.
if (empty($existing['next_sched_contribution_date']) || strtotime($existing['next_sched_contribution_date']) ==
strtotime(date('Y-m-d'))) {
$params['next_sched_contribution_date'] = date('Y-m-d', strtotime('+' . $existing['frequency_interval'] . ' ' . $existing['frequency_unit']));
strtotime($effectiveDate)) {
$params['next_sched_contribution_date'] = date('Y-m-d', strtotime('+' . $existing['frequency_interval'] . ' ' . $existing['frequency_unit'], strtotime($effectiveDate)));
}
}
civicrm_api3('ContributionRecur', 'create', $params);
Expand All @@ -868,7 +871,9 @@ public static function updateOnNewPayment($recurringContributionID, $paymentStat
*/
protected static function isComplete($recurringContributionID, $installments) {
$paidInstallments = CRM_Core_DAO::singleValueQuery(
'SELECT count(*) FROM civicrm_contribution WHERE id = %1',
'SELECT count(*) FROM civicrm_contribution
WHERE contribution_recur_id = %1
AND contribution_status_id = ' . CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'),
array(1 => array($recurringContributionID, 'Integer'))
);
if ($paidInstallments >= $installments) {
Expand Down
71 changes: 65 additions & 6 deletions tests/phpunit/api/v3/ContributionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2571,34 +2571,93 @@ public function testCompleteTransactionContributionPageFromAddress() {
* Test completing first transaction in a recurring series.
*
* The status should be set to 'in progress' and the next scheduled payment date calculated.
*
* @dataProvider getScheduledDateData
*
* @param array $dataSet
*
* @throws \Exception
*/
public function testCompleteTransactionSetStatusToInProgress() {
public function testCompleteTransactionSetStatusToInProgress($dataSet) {
$paymentProcessorID = $this->paymentProcessorCreate();
$contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
$contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
'contact_id' => $this->_individualId,
'installments' => '12',
'installments' => '2',
'frequency_interval' => '1',
'amount' => '500',
'contribution_status_id' => 'Pending',
'start_date' => '2012-01-01 00:00:00',
'currency' => 'USD',
'frequency_unit' => 'month',
'payment_processor_id' => $paymentProcessorID,
));
), $dataSet['data']));
$contribution = $this->callAPISuccess('contribution', 'create', array_merge(
$this->_params,
array(
'contribution_recur_id' => $contributionRecur['id'],
'contribution_status_id' => 'Pending',
'receive_date' => $dataSet['receive_date'],
))
);
$this->callAPISuccess('Contribution', 'completetransaction', array('id' => $contribution));
$this->callAPISuccess('Contribution', 'completetransaction', array(
'id' => $contribution,
'receive_date' => $dataSet['receive_date'],
));
$contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', array(
'id' => $contributionRecur['id'],
'return' => array('next_sched_contribution_date', 'contribution_status_id'),
));
$this->assertEquals(5, $contributionRecur['contribution_status_id']);
$this->assertEquals(date('Y-m-d 00:00:00', strtotime('+1 month')), $contributionRecur['next_sched_contribution_date']);
$this->assertEquals($dataSet['expected'], $contributionRecur['next_sched_contribution_date']);
$this->callAPISuccess('Contribution', 'create', array_merge(
$this->_params,
array(
'contribution_recur_id' => $contributionRecur['id'],
'contribution_status_id' => 'Completed',
)
));
$contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', array(
'id' => $contributionRecur['id'],
'return' => array('contribution_status_id'),
));
$this->assertEquals(1, $contributionRecur['contribution_status_id']);
}

/**
* Get dates for testing.
*
* @return array
*/
public function getScheduledDateData() {
$result = array();
$result[]['2016-08-31-1-month'] = array(
'data' => array(
'start_date' => '2016-08-31',
'frequency_interval' => 1,
'frequency_unit' => 'month',
),
'receive_date' => '2016-08-31',
'expected' => '2016-10-01 00:00:00',
);
$result[]['2012-01-01-1-month'] = array(
'data' => array(
'start_date' => '2012-01-01',
'frequency_interval' => 1,
'frequency_unit' => 'month',
),
'receive_date' => '2012-01-01',
'expected' => '2012-02-01 00:00:00',
);
$result[]['2012-01-01-1-month'] = array(
'data' => array(
'start_date' => '2012-01-01',
'frequency_interval' => 1,
'frequency_unit' => 'month',
),
'receive_date' => '2012-02-29',
'expected' => '2012-03-29 00:00:00',
);
return $result;
}

/**
Expand Down