3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Report_Form_Contribute_HouseholdSummary
extends CRM_Report_Form
{
19 public $_drilldownReport = ['contribute/detail' => 'Link to Detail Report'];
21 protected $_summary = NULL;
26 public function __construct() {
27 self
::validRelationships();
30 'civicrm_contact_household' => [
31 'dao' => 'CRM_Contact_DAO_Contact',
34 'title' => ts('Household Name'),
42 'title' => ts('Contact Type'),
44 'contact_sub_type' => [
45 'title' => ts('Contact Subtype'),
50 'title' => ts('Household Name'),
54 'type' => CRM_Utils_Type
::T_BOOLEAN
,
57 'grouping' => 'household-fields',
59 'civicrm_line_item' => [
60 'dao' => 'CRM_Price_DAO_LineItem',
62 'civicrm_relationship' => [
63 'dao' => 'CRM_Contact_DAO_Relationship',
65 'relationship_type_id' => [
66 'title' => ts('Relationship Type'),
70 'relationship_type_id' => [
71 'title' => ts('Relationship Type'),
72 'type' => CRM_Utils_Type
::T_INT
,
73 'operatorType' => CRM_Report_Form
::OP_SELECT
,
74 'options' => $this->relationTypes
,
75 'default' => key($this->relationTypes
),
78 'grouping' => 'household-fields',
80 'civicrm_contact' => [
81 'dao' => 'CRM_Contact_DAO_Contact',
84 'title' => ts('Contact Name'),
92 'grouping' => 'contact-fields',
94 'civicrm_contribution' => [
95 'dao' => 'CRM_Contribute_DAO_Contribution',
98 'title' => ts('Amount'),
102 'no_display' => TRUE,
105 'contribution_status_id' => [
106 'title' => ts('Contribution Status'),
110 'title' => ts('Check Number'),
114 'no_display' => TRUE,
116 'financial_type_id' => [
117 'title' => ts('Financial Type'),
120 'receive_date' => ['default' => TRUE],
121 'receipt_date' => NULL,
124 'receive_date' => ['operatorType' => CRM_Report_Form
::OP_DATE
],
125 'receipt_date' => ['operatorType' => CRM_Report_Form
::OP_DATE
],
126 'total_amount' => ['title' => ts('Amount Between')],
128 'title' => ts('Currency'),
129 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
130 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
132 'type' => CRM_Utils_Type
::T_STRING
,
134 'contribution_status_id' => [
135 'title' => ts('Contribution Status'),
136 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
137 'options' => CRM_Contribute_BAO_Contribution
::buildOptions('contribution_status_id', 'search'),
140 'financial_type_id' => [
141 'title' => ts('Financial Type'),
142 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
143 'options' => CRM_Contribute_PseudoConstant
::financialType(),
146 'grouping' => 'contri-fields',
148 'civicrm_financial_trxn' => [
149 'dao' => 'CRM_Financial_DAO_FinancialTrxn',
152 'title' => ts('Credit Card Type'),
153 'dbAlias' => 'GROUP_CONCAT(financial_trxn_civireport.card_type_id SEPARATOR ",")',
158 'title' => ts('Credit Card Type'),
159 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
160 'options' => CRM_Financial_DAO_FinancialTrxn
::buildOptions('card_type_id'),
162 'type' => CRM_Utils_Type
::T_STRING
,
166 'civicrm_address' => [
167 'dao' => 'CRM_Core_DAO_Address',
169 'street_address' => NULL,
171 'postal_code' => NULL,
172 'state_province_id' => [
173 'title' => ts('State/Province'),
176 'title' => ts('Country'),
179 'grouping' => 'contact-fields',
182 'dao' => 'CRM_Core_DAO_Email',
188 'title' => ts('On Hold'),
189 'type' => CRM_Utils_Type
::T_INT
,
190 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
191 'options' => ['' => ts('Any')] + CRM_Core_PseudoConstant
::emailOnHoldOptions(),
194 'grouping' => 'contact-fields',
198 // If we have a campaign, build out the relevant elements
199 $this->addCampaignFields('civicrm_contribution');
201 $this->_currencyColumn
= 'civicrm_contribution_currency';
202 parent
::__construct();
205 public function select() {
206 // @todo remove this & use parent select.
207 $this->_columnHeaders
= $select = [];
208 foreach ($this->_columns
as $tableName => $table) {
209 if (array_key_exists('fields', $table)) {
210 foreach ($table['fields'] as $fieldName => $field) {
211 if (!empty($field['required']) ||
212 !empty($this->_params
['fields'][$fieldName])
215 if (!empty($field['statistics'])) {
216 foreach ($field['statistics'] as $stat => $label) {
217 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}_{$stat}";
218 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
219 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
220 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
224 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
226 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'] ??
NULL;
227 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = $field['type'] ??
NULL;
233 $this->_selectClauses
= $select;
234 $this->_select
= "SELECT " . implode(', ', $select) . " ";
237 public function from() {
239 FROM civicrm_relationship {$this->_aliases['civicrm_relationship']}
240 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact_household']} ON
241 ({$this->_aliases['civicrm_contact_household']}.id = {$this->_aliases['civicrm_relationship']}.$this->householdContact AND {$this->_aliases['civicrm_contact_household']}.contact_type='Household')
242 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']} ON
243 ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_relationship']}.$this->otherContact )
245 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']} ON
246 ({$this->_aliases['civicrm_contribution']}.contact_id = {$this->_aliases['civicrm_relationship']}.$this->otherContact ) AND {$this->_aliases['civicrm_contribution']}.is_test = 0 AND {$this->_aliases['civicrm_contribution']}.is_template = 0 ";
248 $this->joinAddressFromContact();
249 $this->joinEmailFromContact();
251 // for credit card type
252 $this->addFinancialTrxnFromClause();
255 public function where() {
257 foreach ($this->_columns
as $tableName => $table) {
258 if (array_key_exists('filters', $table)) {
259 foreach ($table['filters'] as $fieldName => $field) {
261 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
262 $relative = $this->_params
["{$fieldName}_relative"] ??
NULL;
263 $from = $this->_params
["{$fieldName}_from"] ??
NULL;
264 $to = $this->_params
["{$fieldName}_to"] ??
NULL;
265 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
268 $op = $this->_params
["{$fieldName}_op"] ??
NULL;
270 if ($fieldName == 'relationship_type_id') {
271 $clause = "{$this->_aliases['civicrm_relationship']}.relationship_type_id=" . $this->relationshipId
;
274 $clause = $this->whereClause($field,
276 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
277 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
278 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
284 if (!empty($clause)) {
285 $clauses[] = $clause;
291 if (empty($clauses)) {
292 $this->_where
= "WHERE ( 1 )";
295 $this->_where
= "WHERE " . implode(' AND ', $clauses);
298 if ($this->_aclWhere
) {
299 $this->_where
.= " AND {$this->_aclWhere} ";
303 public function groupBy() {
305 "{$this->_aliases['civicrm_relationship']}.$this->householdContact",
306 "{$this->_aliases['civicrm_relationship']}.$this->otherContact",
307 "{$this->_aliases['civicrm_contribution']}.id",
308 "{$this->_aliases['civicrm_relationship']}.relationship_type_id",
310 $this->_groupBy
= CRM_Contact_BAO_Query
::getGroupByFromSelectColumns($this->_selectClauses
, $groupBy);
313 public function orderBy() {
314 $this->_orderBy
= " ORDER BY {$this->_aliases['civicrm_contact_household']}.household_name, {$this->_aliases['civicrm_relationship']}.$this->householdContact, {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_relationship']}.$this->otherContact";
322 public function statistics(&$rows) {
323 $statistics = parent
::statistics($rows);
325 //hack filter display for relationship type
326 $type = substr($this->_params
['relationship_type_id_value'], -3);
327 foreach ($statistics['filters'] as $id => $value) {
329 $value['title'] == 'Relationship Type' &&
330 isset($this->relationTypes
[$this->relationshipId
. '_' . $type])
332 $statistics['filters'][$id]['value'] = 'Is equal to ' .
333 $this->relationTypes
[$this->relationshipId
. '_' . $type];
339 public function postProcess() {
341 $this->beginPostProcess();
342 $this->buildACLClause([
343 $this->_aliases
['civicrm_contact'],
344 $this->_aliases
['civicrm_contact_household'],
346 $sql = $this->buildQuery(TRUE);
349 $this->buildRows($sql, $rows);
350 $this->formatDisplay($rows);
351 $this->doTemplateAssignment($rows);
352 $this->endPostProcess($rows);
356 * Set variables to be accessed by API and form layer in processing.
358 public function beginPostProcessCommon() {
359 $getRelationship = $this->_params
['relationship_type_id_value'];
360 $type = substr($getRelationship, -3);
361 $this->relationshipId
= intval((substr($getRelationship, 0, strpos($getRelationship, '_'))));
362 if ($type == 'b_a') {
363 $this->householdContact
= 'contact_id_b';
364 $this->otherContact
= 'contact_id_a';
367 $this->householdContact
= 'contact_id_a';
368 $this->otherContact
= 'contact_id_b';
372 public function validRelationships() {
373 $this->relationTypes
= $relationTypes = [];
375 $params = ['contact_type_b' => 'Household', 'version' => 3];
376 $typesA = civicrm_api('relationship_type', 'get', $params);
377 if (empty($typesA['is_error'])) {
378 foreach ($typesA['values'] as $rel) {
379 $relationTypes[$rel['id']][$rel['id'] . '_b_a'] = $rel['label_b_a'];
382 $params = ['contact_type_a' => 'Household', 'version' => 3];
383 $typesB = civicrm_api('relationship_type', 'get', $params);
384 if (empty($typesB['is_error'])) {
385 foreach ($typesB['values'] as $rel) {
386 $relationTypes[$rel['id']][$rel['id'] . '_a_b'] = $rel['label_a_b'];
387 //$this->relationTypes[$rel['id'].'_a_b'] = $rel['label_a_b'];
391 ksort($relationTypes);
392 foreach ($relationTypes as $relationship) {
393 foreach ($relationship as $index => $label) {
394 $this->relationTypes
[$index] = $label;
400 * Alter display of rows.
402 * Iterate through the rows retrieved via SQL and make changes for display purposes,
403 * such as rendering contacts as links.
406 * Rows generated by SQL, with an array for each row.
408 public function alterDisplay(&$rows) {
409 $type = substr($this->_params
['relationship_type_id_value'], -3);
412 $flagHousehold = $flagContact = 0;
413 foreach ($rows as $rowNum => $row) {
415 //replace retionship id by relationship name
416 if (array_key_exists('civicrm_relationship_relationship_type_id', $row)) {
417 if ($value = $row['civicrm_relationship_relationship_type_id']) {
418 $rows[$rowNum]['civicrm_relationship_relationship_type_id'] = $this->relationTypes
[$value . '_' . $type];
423 //remove duplicate Organization names
424 if (array_key_exists('civicrm_contact_household_household_name', $row) &&
425 $this->_outputMode
!= 'csv'
427 if ($value = $row['civicrm_contact_household_household_name']) {
429 $priviousHousehold = $value;
432 if ($priviousHousehold == $value) {
434 $priviousHousehold = $value;
438 $priviousHousehold = $value;
442 if ($flagHousehold == 1) {
443 $rows[$rowNum]['civicrm_contact_household_household_name'] = "";
446 $url = CRM_Utils_System
::url('civicrm/contact/view',
447 'reset=1&cid=' . $rows[$rowNum]['civicrm_contact_household_id'],
451 $rows[$rowNum]['civicrm_contact_household_household_name'] = "<a href='$url' title='" . ts('View contact summary for this househould') . "'>" . $value . '</a>';
457 //remove duplicate Contact names and relationship type
458 if (array_key_exists('civicrm_contact_id', $row) &&
459 $this->_outputMode
!= 'csv'
461 if ($value = $row['civicrm_contact_id']) {
463 $priviousContact = $value;
466 if ($priviousContact == $value) {
468 $priviousContact = $value;
472 $priviousContact = $value;
476 if ($flagContact == 1 && $flagHousehold == 1) {
477 $rows[$rowNum]['civicrm_contact_sort_name'] = "";
478 $rows[$rowNum]['civicrm_relationship_relationship_type_id'] = "";
485 if (array_key_exists('civicrm_contribution_contribution_status_id', $row)) {
486 if ($value = $row['civicrm_contribution_contribution_status_id']) {
487 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = CRM_Contribute_PseudoConstant
::contributionStatus($value, 'label');
491 if (array_key_exists('civicrm_contribution_financial_type_id', $row)) {
492 if ($value = $row['civicrm_contribution_financial_type_id']) {
493 $rows[$rowNum]['civicrm_contribution_financial_type_id'] = CRM_Contribute_PseudoConstant
::financialType($value);
497 // convert display name to links
498 if (array_key_exists('civicrm_contact_sort_name', $row) &&
499 $rows[$rowNum]['civicrm_contact_sort_name'] &&
500 array_key_exists('civicrm_contact_id', $row)
502 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
503 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
504 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
506 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
507 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts('View contribution details for this individual');
512 if (!empty($row['civicrm_contribution_total_amount'])) {
513 $row['civicrm_contribution_total_amount'] = CRM_Utils_Money
::format($row['civicrm_contribution_total_amount'], $row['civicrm_contribution_currency']);
516 if (!empty($row['civicrm_financial_trxn_card_type_id'])) {
517 $rows[$rowNum]['civicrm_financial_trxn_card_type_id'] = $this->getLabels($row['civicrm_financial_trxn_card_type_id'], 'CRM_Financial_DAO_FinancialTrxn', 'card_type_id');
521 // Contribution amount links to view contribution
522 if (($value = CRM_Utils_Array
::value('civicrm_contribution_total_amount', $row)) &&
523 CRM_Core_Permission
::check('access CiviContribute')
525 $url = CRM_Utils_System
::url("civicrm/contact/view/contribution",
526 "reset=1&id=" . $row['civicrm_contribution_id'] . "&cid=" .
527 $row['civicrm_contact_id'] .
528 "&action=view&context=contribution&selectedChild=contribute",
531 $rows[$rowNum]['civicrm_contribution_total_amount_link'] = $url;
532 $rows[$rowNum]['civicrm_contribution_total_amount_hover'] = ts("View this contribution.");
536 // convert campaign_id to campaign title
537 if (array_key_exists('civicrm_contribution_campaign_id', $row)) {
538 if ($value = $row['civicrm_contribution_campaign_id']) {
539 $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->campaigns
[$value];
544 $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, NULL, NULL) ?
TRUE : $entryFound;
546 // skip looking further in rows, if first row itself doesn't