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

Get the template contribution instead of the first contribution when using repeattransaction #17455

Closed
Closed
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
17 changes: 14 additions & 3 deletions CRM/Contribute/BAO/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -4417,7 +4417,20 @@ public static function completeOrder($input, &$ids, $objects, $transaction = NUL
$transaction = new CRM_Core_Transaction();
}
$contribution = $objects['contribution'];
$primaryContributionID = $contribution->id ?? $objects['first_contribution']->id;

$recurContrib = $objects['contributionRecur'] ?? NULL;
$recurringContributionID = (empty($recurContrib->id)) ? NULL : $recurContrib->id;
if ($recurringContributionID) {
$contribution = new CRM_Contribute_BAO_Contribution();
$contribution->copyValues(CRM_Contribute_BAO_ContributionRecur::getTemplateContribution($recurringContributionID));
$primaryContributionID = $contribution->id;
unset($contribution->id, $contribution->invoice_id);
isset($input['receive_date']) ? $contribution->receive_date = $input['receive_date'] : NULL;
}
else {
$primaryContributionID = $contribution->id ?? $objects['first_contribution']->id;
}

// The previous details are used when calculating line items so keep it before any code that 'does something'
if (!empty($contribution->id)) {
$input['prevContribution'] = CRM_Contribute_BAO_Contribution::getValues(['id' => $contribution->id]);
Expand All @@ -4441,8 +4454,6 @@ public static function completeOrder($input, &$ids, $objects, $transaction = NUL
}

$participant = $objects['participant'] ?? NULL;
$recurContrib = $objects['contributionRecur'] ?? NULL;
$recurringContributionID = (empty($recurContrib->id)) ? NULL : $recurContrib->id;
$event = $objects['event'] ?? NULL;

$paymentProcessorId = '';
Expand Down
2 changes: 1 addition & 1 deletion CRM/Contribute/BAO/ContributionRecur.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ public static function supportsFinancialTypeChange($id) {
*
* @param int $id
* @param array $overrides
* Parameters that should be overriden. Add unit tests if using parameters other than total_amount & financial_type_id.
* Parameters that should be overridden. Add unit tests if using parameters other than total_amount & financial_type_id.
*
* @return array
*
Expand Down
8 changes: 8 additions & 0 deletions tests/phpunit/CRM/Contribute/BAO/ContributionRecurTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ public function testGetTemplateContributionNewTemplate() {
'contribution_recur_id' => $contributionRecur['id'],
'total_amount' => '3.00',
'financial_type_id' => 1,
'source' => 'Template Contribution',
'payment_instrument_id' => 1,
'currency' => 'USD',
'contact_id' => $this->individualCreate(),
Expand All @@ -237,6 +238,7 @@ public function testGetTemplateContributionNewTemplate() {
'contribution_recur_id' => $contributionRecur['id'],
'total_amount' => '3.00',
'financial_type_id' => 1,
'source' => 'Non-template Contribution',
'payment_instrument_id' => 1,
'currency' => 'USD',
'contact_id' => $this->individualCreate(),
Expand All @@ -246,6 +248,12 @@ public function testGetTemplateContributionNewTemplate() {
$fetchedTemplate = CRM_Contribute_BAO_ContributionRecur::getTemplateContribution($contributionRecur['id']);
// Fetched template should be the is_template, not the latest contrib
$this->assertEquals($fetchedTemplate['id'], $templateContrib['id']);

$repeatContribution = $this->callAPISuccess('Contribution', 'repeattransaction', [
'contribution_status_id' => "Completed",
'contribution_recur_id' => $contributionRecur['id'],
]);
$this->assertEquals($repeatContribution['values'][$repeatContribution['id']]['source'], $templateContrib['values'][$templateContrib['id']]['source']);
}

/**
Expand Down