Skip to content

Commit

Permalink
Fix missing amount in soft credit mode
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Sep 23, 2018
1 parent 0434d0f commit 8f5a807
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 20 deletions.
49 changes: 29 additions & 20 deletions CRM/Report/Form/Contribute/Detail.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ public function __construct() {
'contribution_id' => array(
'name' => 'id',
'required' => TRUE,
'default' => TRUE,
'title' => ts('Contribution'),
),
),
Expand All @@ -262,6 +263,7 @@ public function __construct() {
'dao' => 'CRM_Contribute_DAO_ContributionSoft',
'fields' => array(
'soft_credit_type_id' => array('title' => ts('Soft Credit Type')),
'soft_credit_amount' => ['title' => ts('Soft Credit amount'), 'name' => 'amount', 'type' => CRM_Utils_Type::T_MONEY]
),
'filters' => array(
'soft_credit_type_id' => array(
Expand All @@ -272,6 +274,12 @@ public function __construct() {
'type' => CRM_Utils_Type::T_STRING,
),
),
'group_bys' => array(
'soft_credit_id' => array(
'name' => 'id',
'title' => ts('Soft Credit'),
),
),
),
'civicrm_financial_trxn' => array(
'dao' => 'CRM_Financial_DAO_FinancialTrxn',
Expand Down Expand Up @@ -376,18 +384,7 @@ public function from() {
ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id
AND {$this->_aliases['civicrm_contribution']}.is_test = 0";

if (CRM_Utils_Array::value('contribution_or_soft_value', $this->_params) ==
'both'
) {
$this->_from .= "\n LEFT JOIN civicrm_contribution_soft contribution_soft_civireport
ON contribution_soft_civireport.contribution_id = {$this->_aliases['civicrm_contribution']}.id";
}
elseif (CRM_Utils_Array::value('contribution_or_soft_value', $this->_params) ==
'soft_credits_only'
) {
$this->_from .= "\n INNER JOIN civicrm_contribution_soft contribution_soft_civireport
ON contribution_soft_civireport.contribution_id = {$this->_aliases['civicrm_contribution']}.id";
}
$this->joinContributionToSoftCredit();
$this->appendAdditionalFromJoins();
}

Expand Down Expand Up @@ -551,13 +548,10 @@ public function beginPostProcessCommon() {

$select = str_ireplace('contribution_civireport.total_amount', 'contribution_soft_civireport.amount', $this->_select);
$select = str_ireplace("'Contribution' as", "'Soft Credit' as", $select);
// We really don't want to join soft credit in if not required.
if (!empty($this->_groupBy) && !$this->noDisplayContributionOrSoftColumn) {
$this->_groupBy .= ', contribution_soft_civireport.amount';
}

// we inner join with temp1 to restrict soft contributions to those in temp1 table.
// no group by here as we want to display as many soft credit rows as actually exist.
$sql = "{$select} {$this->_from} {$this->_where}";
$sql = "{$select} {$this->_from} {$this->_where} $this->_groupBy";
$tempQuery = "CREATE TEMPORARY TABLE civireport_contribution_detail_temp2 {$this->_databaseAttributes} AS {$sql}";
$this->executeReportQuery($tempQuery);
$this->temporaryTables['civireport_contribution_detail_temp2'] = ['name' => 'civireport_contribution_detail_temp2', 'temporary' => TRUE];
Expand Down Expand Up @@ -606,12 +600,11 @@ public function beginPostProcessCommon() {
/**
* Store group bys into array - so we can check elsewhere what is grouped.
*
* If we are generating a table of soft credits we do not want to be using
* group by.
* If we are generating a table of soft credits we need to group by them.
*/
protected function storeGroupByArray() {
if ($this->queryMode === 'SoftCredit') {
$this->_groupByArray = [];
$this->_groupByArray = [$this->_aliases['civicrm_contribution_soft'] . '.id'];
}
else {
parent::storeGroupByArray();
Expand Down Expand Up @@ -975,4 +968,20 @@ public function appendAdditionalFromJoins() {
$this->addFinancialTrxnFromClause();
}

/**
* Add join to the soft credit table.
*/
protected function joinContributionToSoftCredit() {
if (!CRM_Utils_Array::value('contribution_or_soft_value', $this->_params) && !$this->isTableSelected('civicrm_contribution_soft')) {
return;
}
$joinType = ' LEFT ';
if (CRM_Utils_Array::value('contribution_or_soft_value', $this->_params) == 'soft_credits_only') {
$joinType = ' INNER ';
}
$this->_from .= "
$joinType JOIN civicrm_contribution_soft {$this->_aliases['civicrm_contribution_soft']}
ON {$this->_aliases['civicrm_contribution_soft']}.contribution_id = {$this->_aliases['civicrm_contribution']}.id
";
}
}
25 changes: 25 additions & 0 deletions tests/phpunit/api/v3/ReportTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,31 @@ public function testContributionDetailSoftCredits() {
);
}

/**
* Test the amount column is populated on soft credit details.
*/
public function testContributionDetailSoftCreditsOnly() {
$contactID = $this->individualCreate();
$contactID2 = $this->individualCreate();
$this->contributionCreate(['contact_id' => $contactID, 'api.ContributionSoft.create' => ['amount' => 5, 'contact_id' => $contactID2]]);
$template = 'contribute/detail';
$rows = $this->callAPISuccess('report_template', 'getrows', array(
'report_id' => $template,
'contribution_or_soft_value' => 'soft_credits_only',
'fields' => [
'sort_name' => '1',
'email' => '1',
'financial_type_id' => '1',
'receive_date' => '1',
'total_amount' => '1',
],
'options' => array('metadata' => ['sql', 'labels']),
));
foreach (array_keys($rows['metadata']['labels']) as $header) {
$this->assertTrue(!empty($rows['values'][0][$header]));
}
}

/**
* Test the group filter works on the various reports.
*
Expand Down

0 comments on commit 8f5a807

Please sign in to comment.