'contribution_id' => array(
'name' => 'id',
'required' => TRUE,
+ 'default' => TRUE,
'title' => ts('Contribution'),
),
),
'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(
'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',
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();
}
}
// 1. use main contribution query to build temp table 1
$sql = $this->buildQuery();
- $tempQuery = "CREATE TEMPORARY TABLE civireport_contribution_detail_temp1 {$this->_databaseAttributes} AS {$sql}";
- $this->temporaryTables['civireport_contribution_detail_temp1'] = ['name' => 'civireport_contribution_detail_temp1', 'temporary' => TRUE];
- $this->executeReportQuery($tempQuery);
+ $this->createTemporaryTable('civireport_contribution_detail_temp1', $sql);
$this->setPager();
// 2. customize main contribution query for soft credit, and build temp table 2 with soft credit contributions only
$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];
if ($this->isContributionBaseMode
) {
$this->executeReportQuery(
- "CREATE TEMPORARY TABLE civireport_contribution_detail_temp3 {$this->_databaseAttributes} AS (SELECT * FROM civireport_contribution_detail_temp1)"
+ "CREATE TEMPORARY TABLE civireport_contribution_detail_temp3 {$this->_databaseAttributes} AS (SELECT * FROM {$this->temporaryTables['civireport_contribution_detail_temp1']['name']})"
);
}
elseif (CRM_Utils_Array::value('contribution_or_soft_value', $this->_params) ==
}
else {
$this->executeReportQuery("CREATE TEMPORARY TABLE civireport_contribution_detail_temp3 {$this->_databaseAttributes}
-(SELECT * FROM civireport_contribution_detail_temp1)
+(SELECT * FROM {$this->temporaryTables['civireport_contribution_detail_temp1']['name']})
UNION ALL
(SELECT * FROM civireport_contribution_detail_temp2)");
}
/**
* 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();
array_key_exists('civicrm_contribution_contribution_id', $row)
) {
$query = "
-SELECT civicrm_contact_id, civicrm_contact_sort_name, civicrm_contribution_total_amount, civicrm_contribution_currency
+SELECT civicrm_contact_id, civicrm_contact_sort_name, civicrm_contribution_total_amount_sum, civicrm_contribution_currency
FROM civireport_contribution_detail_temp2
WHERE civicrm_contribution_contribution_id={$row['civicrm_contribution_contribution_id']}";
$dao = CRM_Core_DAO::executeQuery($query);
$dao->civicrm_contact_id);
$string = $string . ($string ? $separator : '') .
"<a href='{$url}'>{$dao->civicrm_contact_sort_name}</a> " .
- CRM_Utils_Money::format($dao->civicrm_contribution_total_amount, $dao->civicrm_contribution_currency);
+ CRM_Utils_Money::format($dao->civicrm_contribution_total_amount_sum, $dao->civicrm_contribution_currency);
}
$rows[$rowNum]['civicrm_contribution_soft_credits'] = $string;
}
) {
$query = "
SELECT civicrm_contact_id, civicrm_contact_sort_name
-FROM civireport_contribution_detail_temp1
+FROM {$this->temporaryTables['civireport_contribution_detail_temp1']['name']}
WHERE civicrm_contribution_contribution_id={$row['civicrm_contribution_contribution_id']}";
$dao = CRM_Core_DAO::executeQuery($query);
$string = '';
public function softCreditFrom() {
$this->_from = "
- FROM civireport_contribution_detail_temp1 temp1_civireport
+ FROM {$this->temporaryTables['civireport_contribution_detail_temp1']['name']} temp1_civireport
INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
ON temp1_civireport.civicrm_contribution_contribution_id = {$this->_aliases['civicrm_contribution']}.id
INNER JOIN civicrm_contribution_soft contribution_soft_civireport
$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
+ ";
+ }
+
}