Skip to content

Commit

Permalink
Merge pull request #21470 from mattwire/templatecontributioncreate
Browse files Browse the repository at this point in the history
dev/financial#6 Fix creating of template contribution when it has custom data
  • Loading branch information
eileenmcnaughton authored Mar 14, 2022
2 parents a63abfc + b0ccbba commit 18e8018
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
20 changes: 8 additions & 12 deletions CRM/Contribute/BAO/ContributionRecur.php
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ public static function ensureTemplateContributionExists(int $id) {

// Retrieve the most recently added contribution
$mostRecentContribution = Contribution::get(FALSE)
->addSelect('custom.*', 'id', 'contact_id', 'campaign_id', 'financial_type_id', 'currency', 'source', 'amount_level', 'address_id', 'on_behalf', 'source_contact_id', 'tax_amount', 'contribution_page_id', 'total_amount', 'is_test')
->addWhere('contribution_recur_id', '=', $id)
->addWhere('is_template', '=', 0)
// we need this line otherwise the is test contribution don't work.
Expand All @@ -461,26 +462,21 @@ public static function ensureTemplateContributionExists(int $id) {
// relevant values to ensure the activity reflects that.
$relatedContact = CRM_Contribute_BAO_Contribution::getOnbehalfIds($mostRecentContribution['id']);

$templateContributionParams = [];
$templateContributionParams['is_test'] = $mostRecentContribution['is_test'];
$templateContributionParams = $mostRecentContribution;
unset($templateContributionParams['id']);
$templateContributionParams['is_template'] = '1';
$templateContributionParams['contribution_status_id:name'] = 'Template';
$templateContributionParams['skipRecentView'] = TRUE;
$templateContributionParams['contribution_recur_id'] = $id;
$templateContributionParams['line_item'] = $mostRecentContribution['line_item'];
$templateContributionParams['status_id'] = 'Template';
foreach (['contact_id', 'campaign_id', 'financial_type_id', 'currency', 'source', 'amount_level', 'address_id', 'on_behalf', 'source_contact_id', 'tax_amount', 'contribution_page_id', 'total_amount'] as $fieldName) {
if (isset($mostRecentContribution[$fieldName])) {
$templateContributionParams[$fieldName] = $mostRecentContribution[$fieldName];
}
}
if (!empty($relatedContact['individual_id'])) {
$templateContributionParams['on_behalf'] = TRUE;
$templateContributionParams['source_contact_id'] = $relatedContact['individual_id'];
}
$templateContributionParams['source'] = $templateContributionParams['source'] ?? ts('Recurring contribution');
$templateContribution = civicrm_api3('Contribution', 'create', $templateContributionParams);
$temporaryObject = new CRM_Contribute_BAO_Contribution();
$temporaryObject->copyCustomFields($mostRecentContribution['id'], $templateContribution['id']);
$templateContribution = Contribution::create(FALSE)
->setValues($templateContributionParams)
->execute()
->first();
// Add new soft credit against current $contribution.
CRM_Contribute_BAO_ContributionRecur::addrecurSoftCredit($templateContributionParams['contribution_recur_id'], $templateContribution['id']);
return $templateContribution['id'];
Expand Down
23 changes: 23 additions & 0 deletions tests/phpunit/CRM/Contribute/BAO/ContributionRecurTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ public function testGetTemplateContributionMatchTest(): void {
public function testCreateTemplateContributionFromFirstContributionTest(): void {
$custom_group = $this->customGroupCreate(['extends' => 'Contribution', 'name' => 'template']);
$custom_field = $this->customFieldCreate(['custom_group_id' => $custom_group['id'], 'name' => 'field']);
$custom_field2 = $this->customFieldCreate(['custom_group_id' => $custom_group['id'], 'name' => 'field2', 'label' => 'Field 2']);

$contributionRecur = $this->callAPISuccess('contribution_recur', 'create', $this->_params);
// Create a first test contrib
Expand All @@ -246,6 +247,7 @@ public function testCreateTemplateContributionFromFirstContributionTest(): void
'contribution_status_id' => 1,
'receive_date' => $date->format('YmdHis'),
'custom_' . $custom_field['id'] => 'First Contribution',
'custom_' . $custom_field2['id'] => 'First Contribution custom field 2',
]);
$date->modify('+2 days');
$secondContrib = $this->callAPISuccess('Contribution', 'create', [
Expand All @@ -258,6 +260,7 @@ public function testCreateTemplateContributionFromFirstContributionTest(): void
'contribution_status_id' => 1,
'receive_date' => $date->format('YmdHis'),
'custom_' . $custom_field['id'] => 'Second and most recent Contribution',
'custom_' . $custom_field2['id'] => 'Second and most recent Contribution field 2',
]);

$date->modify('-1 week');
Expand All @@ -271,8 +274,13 @@ public function testCreateTemplateContributionFromFirstContributionTest(): void
'contribution_status_id' => 1,
'receive_date' => $date->format('YmdHis'),
'custom_' . $custom_field['id'] => 'Third Contribution',
'custom_' . $custom_field2['id'] => 'Third Contribution field 2',
]);

// Register "contribution create" hook
$this->hookClass->setHook('civicrm_post', array($this, 'implementHookPost'));
\Civi::$statics['testCreateTemplateContributionFromFirstContributionTest']['custom_field_id'] = $custom_field['id'];

// Make sure a template contribution exists.
$templateContributionId = CRM_Contribute_BAO_ContributionRecur::ensureTemplateContributionExists($contributionRecur['id']);
$fetchedTemplate = CRM_Contribute_BAO_ContributionRecur::getTemplateContribution($contributionRecur['id']);
Expand All @@ -294,10 +302,25 @@ public function testCreateTemplateContributionFromFirstContributionTest(): void
$templateContribution = $templateContribution->first();
$this->assertNotNull($templateContribution['template.field']);
$this->assertEquals('Second and most recent Contribution', $templateContribution['template.field']);
$this->assertEquals('Template contribution custom data inserted by hook', $templateContribution['template.field2']);
$this->callAPISuccess('CustomField', 'delete', ['id' => $custom_field['id']]);
$this->callAPISuccess('CustomGroup', 'delete', ['id' => $custom_group['id']]);
}

public function implementHookPost($op, $objectName, $objectId, &$objectRef) {
if ($objectName !== 'Contribution') {
return;
}
if ($op !== 'create') {
return;
}

// Simulate an extension updating the custom data on the new contribution
$contributionParams['entity_id'] = $objectId;
$contributionParams['custom_2'] = 'Template contribution custom data inserted by hook';
civicrm_api3('CustomValue', 'create', $contributionParams);
}

/**
* Test that is_template contribution is used where available
*
Expand Down

0 comments on commit 18e8018

Please sign in to comment.