3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2018
33 class CRM_Report_Form_Contribute_HouseholdSummary
extends CRM_Report_Form
{
35 public $_drilldownReport = array('contribute/detail' => 'Link to Detail Report');
37 protected $_summary = NULL;
42 public function __construct() {
43 self
::validRelationships();
45 $config = CRM_Core_Config
::singleton();
46 $campaignEnabled = in_array("CiviCampaign", $config->enableComponents
);
47 if ($campaignEnabled) {
48 $getCampaigns = CRM_Campaign_BAO_Campaign
::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
49 $this->activeCampaigns
= $getCampaigns['campaigns'];
50 asort($this->activeCampaigns
);
53 $this->_columns
= array(
54 'civicrm_contact_household' => array(
55 'dao' => 'CRM_Contact_DAO_Contact',
57 'household_name' => array(
58 'title' => ts('Household Name'),
65 'contact_type' => array(
66 'title' => ts('Contact Type'),
68 'contact_sub_type' => array(
69 'title' => ts('Contact Subtype'),
73 'household_name' => array(
74 'title' => ts('Household Name'),
76 'is_deleted' => array(
78 'type' => CRM_Utils_Type
::T_BOOLEAN
,
81 'grouping' => 'household-fields',
83 'civicrm_line_item' => array(
84 'dao' => 'CRM_Price_DAO_LineItem',
86 'civicrm_relationship' => array(
87 'dao' => 'CRM_Contact_DAO_Relationship',
89 'relationship_type_id' => array(
90 'title' => ts('Relationship Type'),
94 'relationship_type_id' => array(
95 'title' => ts('Relationship Type'),
96 'type' => CRM_Utils_Type
::T_INT
,
97 'operatorType' => CRM_Report_Form
::OP_SELECT
,
98 'options' => $this->relationTypes
,
99 'default' => key($this->relationTypes
),
102 'grouping' => 'household-fields',
104 'civicrm_contact' => array(
105 'dao' => 'CRM_Contact_DAO_Contact',
107 'sort_name' => array(
108 'title' => ts('Contact Name'),
112 'no_display' => TRUE,
116 'grouping' => 'contact-fields',
118 'civicrm_contribution' => array(
119 'dao' => 'CRM_Contribute_DAO_Contribution',
121 'total_amount' => array(
122 'title' => ts('Amount'),
126 'no_display' => TRUE,
129 'contribution_status_id' => array(
130 'title' => ts('Contribution Status'),
133 'check_number' => array(
134 'title' => ts('Check Number'),
138 'no_display' => TRUE,
140 'financial_type_id' => array(
141 'title' => ts('Financial Type'),
144 'receive_date' => array('default' => TRUE),
145 'receipt_date' => NULL,
148 'receive_date' => array('operatorType' => CRM_Report_Form
::OP_DATE
),
149 'total_amount' => array('title' => ts('Amount Between')),
151 'title' => ts('Currency'),
152 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
153 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
155 'type' => CRM_Utils_Type
::T_STRING
,
157 'contribution_status_id' => array(
158 'title' => ts('Contribution Status'),
159 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
160 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
161 'default' => array(1),
163 'financial_type_id' => array(
164 'title' => ts('Financial Type'),
165 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
166 'options' => CRM_Contribute_PseudoConstant
::financialType(),
169 'grouping' => 'contri-fields',
171 'civicrm_financial_trxn' => array(
172 'dao' => 'CRM_Financial_DAO_FinancialTrxn',
174 'card_type_id' => array(
175 'title' => ts('Credit Card Type'),
176 'dbAlias' => 'GROUP_CONCAT(financial_trxn_civireport.card_type_id SEPARATOR ",")',
180 'card_type_id' => array(
181 'title' => ts('Credit Card Type'),
182 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
183 'options' => CRM_Financial_DAO_FinancialTrxn
::buildOptions('card_type_id'),
185 'type' => CRM_Utils_Type
::T_STRING
,
189 'civicrm_address' => array(
190 'dao' => 'CRM_Core_DAO_Address',
192 'street_address' => NULL,
194 'postal_code' => NULL,
195 'state_province_id' => array(
196 'title' => ts('State/Province'),
198 'country_id' => array(
199 'title' => ts('Country'),
202 'grouping' => 'contact-fields',
204 'civicrm_email' => array(
205 'dao' => 'CRM_Core_DAO_Email',
209 'grouping' => 'contact-fields',
213 if ($campaignEnabled && !empty($this->activeCampaigns
)) {
214 $this->_columns
['civicrm_contribution']['fields']['campaign_id'] = array(
215 'title' => ts('Campaign'),
216 'default' => 'false',
218 $this->_columns
['civicrm_contribution']['filters']['campaign_id'] = array(
219 'title' => ts('Campaign'),
220 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
221 'options' => $this->activeCampaigns
,
222 'type' => CRM_Utils_Type
::T_INT
,
225 $this->_currencyColumn
= 'civicrm_contribution_currency';
226 parent
::__construct();
229 public function select() {
230 // @todo remove this & use parent select.
231 $this->_columnHeaders
= $select = array();
232 foreach ($this->_columns
as $tableName => $table) {
233 if (array_key_exists('fields', $table)) {
234 foreach ($table['fields'] as $fieldName => $field) {
235 if (!empty($field['required']) ||
236 !empty($this->_params
['fields'][$fieldName])
239 if (!empty($field['statistics'])) {
240 foreach ($field['statistics'] as $stat => $label) {
241 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}_{$stat}";
242 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
243 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
244 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
248 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
250 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
251 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
257 $this->_selectClauses
= $select;
258 $this->_select
= "SELECT " . implode(', ', $select) . " ";
261 public function from() {
263 FROM civicrm_relationship {$this->_aliases['civicrm_relationship']}
264 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact_household']} ON
265 ({$this->_aliases['civicrm_contact_household']}.id = {$this->_aliases['civicrm_relationship']}.$this->householdContact AND {$this->_aliases['civicrm_contact_household']}.contact_type='Household')
266 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']} ON
267 ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_relationship']}.$this->otherContact )
269 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']} ON
270 ({$this->_aliases['civicrm_contribution']}.contact_id = {$this->_aliases['civicrm_relationship']}.$this->otherContact ) AND {$this->_aliases['civicrm_contribution']}.is_test = 0 ";
272 $this->joinAddressFromContact();
273 $this->joinEmailFromContact();
275 // for credit card type
276 $this->addFinancialTrxnFromClause();
279 public function where() {
281 foreach ($this->_columns
as $tableName => $table) {
282 if (array_key_exists('filters', $table)) {
283 foreach ($table['filters'] as $fieldName => $field) {
285 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
286 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
287 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
288 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
289 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
292 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
294 if ($fieldName == 'relationship_type_id') {
295 $clause = "{$this->_aliases['civicrm_relationship']}.relationship_type_id=" . $this->relationshipId
;
298 $clause = $this->whereClause($field,
300 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
301 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
302 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
308 if (!empty($clause)) {
309 $clauses[] = $clause;
315 if (empty($clauses)) {
316 $this->_where
= "WHERE ( 1 )";
319 $this->_where
= "WHERE " . implode(' AND ', $clauses);
322 if ($this->_aclWhere
) {
323 $this->_where
.= " AND {$this->_aclWhere} ";
327 public function groupBy() {
329 "{$this->_aliases['civicrm_relationship']}.$this->householdContact",
330 "{$this->_aliases['civicrm_relationship']}.$this->otherContact",
331 "{$this->_aliases['civicrm_contribution']}.id",
332 "{$this->_aliases['civicrm_relationship']}.relationship_type_id",
334 $this->_groupBy
= CRM_Contact_BAO_Query
::getGroupByFromSelectColumns($this->_selectClauses
, $groupBy);
337 public function orderBy() {
338 $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";
346 public function statistics(&$rows) {
347 $statistics = parent
::statistics($rows);
349 //hack filter display for relationship type
350 $type = substr($this->_params
['relationship_type_id_value'], -3);
351 foreach ($statistics['filters'] as $id => $value) {
353 $value['title'] == 'Relationship Type' &&
354 isset($this->relationTypes
[$this->relationshipId
. '_' . $type])
356 $statistics['filters'][$id]['value'] = 'Is equal to ' .
357 $this->relationTypes
[$this->relationshipId
. '_' . $type];
363 public function postProcess() {
365 $this->beginPostProcess();
366 $this->buildACLClause(array(
367 $this->_aliases
['civicrm_contact'],
368 $this->_aliases
['civicrm_contact_household'],
370 $sql = $this->buildQuery(TRUE);
373 $this->buildRows($sql, $rows);
374 $this->formatDisplay($rows);
375 $this->doTemplateAssignment($rows);
376 $this->endPostProcess($rows);
380 * Set variables to be accessed by API and form layer in processing.
382 public function beginPostProcessCommon() {
383 $getRelationship = $this->_params
['relationship_type_id_value'];
384 $type = substr($getRelationship, -3);
385 $this->relationshipId
= intval((substr($getRelationship, 0, strpos($getRelationship, '_'))));
386 if ($type == 'b_a') {
387 $this->householdContact
= 'contact_id_b';
388 $this->otherContact
= 'contact_id_a';
391 $this->householdContact
= 'contact_id_a';
392 $this->otherContact
= 'contact_id_b';
396 public function validRelationships() {
397 $this->relationTypes
= $relationTypes = array();
399 $params = array('contact_type_b' => 'Household', 'version' => 3);
400 $typesA = civicrm_api('relationship_type', 'get', $params);
401 if (empty($typesA['is_error'])) {
402 foreach ($typesA['values'] as $rel) {
403 $relationTypes[$rel['id']][$rel['id'] . '_b_a'] = $rel['label_b_a'];
406 $params = array('contact_type_a' => 'Household', 'version' => 3);
407 $typesB = civicrm_api('relationship_type', 'get', $params);
408 if (empty($typesB['is_error'])) {
409 foreach ($typesB['values'] as $rel) {
410 $relationTypes[$rel['id']][$rel['id'] . '_a_b'] = $rel['label_a_b'];
411 //$this->relationTypes[$rel['id'].'_a_b'] = $rel['label_a_b'];
415 ksort($relationTypes);
416 foreach ($relationTypes as $relationship) {
417 foreach ($relationship as $index => $label) {
418 $this->relationTypes
[$index] = $label;
424 * Alter display of rows.
426 * Iterate through the rows retrieved via SQL and make changes for display purposes,
427 * such as rendering contacts as links.
430 * Rows generated by SQL, with an array for each row.
432 public function alterDisplay(&$rows) {
433 $type = substr($this->_params
['relationship_type_id_value'], -3);
436 $flagHousehold = $flagContact = 0;
437 foreach ($rows as $rowNum => $row) {
439 //replace retionship id by relationship name
440 if (array_key_exists('civicrm_relationship_relationship_type_id', $row)) {
441 if ($value = $row['civicrm_relationship_relationship_type_id']) {
442 $rows[$rowNum]['civicrm_relationship_relationship_type_id'] = $this->relationTypes
[$value . '_' . $type];
447 //remove duplicate Organization names
448 if (array_key_exists('civicrm_contact_household_household_name', $row) &&
449 $this->_outputMode
!= 'csv'
451 if ($value = $row['civicrm_contact_household_household_name']) {
453 $priviousHousehold = $value;
456 if ($priviousHousehold == $value) {
458 $priviousHousehold = $value;
462 $priviousHousehold = $value;
466 if ($flagHousehold == 1) {
467 $rows[$rowNum]['civicrm_contact_household_household_name'] = "";
470 $url = CRM_Utils_System
::url('civicrm/contact/view',
471 'reset=1&cid=' . $rows[$rowNum]['civicrm_contact_household_id'],
475 $rows[$rowNum]['civicrm_contact_household_household_name'] = "<a href='$url' title='" . ts('View contact summary for this househould') . "'>" . $value . '</a>';
481 //remove duplicate Contact names and relationship type
482 if (array_key_exists('civicrm_contact_id', $row) &&
483 $this->_outputMode
!= 'csv'
485 if ($value = $row['civicrm_contact_id']) {
487 $priviousContact = $value;
490 if ($priviousContact == $value) {
492 $priviousContact = $value;
496 $priviousContact = $value;
500 if ($flagContact == 1 && $flagHousehold == 1) {
501 $rows[$rowNum]['civicrm_contact_sort_name'] = "";
502 $rows[$rowNum]['civicrm_relationship_relationship_type_id'] = "";
509 if (array_key_exists('civicrm_contribution_contribution_status_id', $row)) {
510 if ($value = $row['civicrm_contribution_contribution_status_id']) {
511 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = CRM_Contribute_PseudoConstant
::contributionStatus($value);
515 if (array_key_exists('civicrm_contribution_financial_type_id', $row)) {
516 if ($value = $row['civicrm_contribution_financial_type_id']) {
517 $rows[$rowNum]['civicrm_contribution_financial_type_id'] = CRM_Contribute_PseudoConstant
::financialType($value);
521 // handle state province
522 if (array_key_exists('civicrm_address_state_province_id', $row)) {
523 if ($value = $row['civicrm_address_state_province_id']) {
524 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvinceAbbreviation($value, FALSE);
530 if (array_key_exists('civicrm_address_country_id', $row)) {
531 if ($value = $row['civicrm_address_country_id']) {
532 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
537 // convert display name to links
538 if (array_key_exists('civicrm_contact_sort_name', $row) &&
539 $rows[$rowNum]['civicrm_contact_sort_name'] &&
540 array_key_exists('civicrm_contact_id', $row)
542 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
543 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
544 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
546 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
547 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts('View contribution details for this individual');
552 if (!empty($row['civicrm_contribution_total_amount'])) {
553 $row['civicrm_contribution_total_amount'] = CRM_Utils_Money
::format($row['civicrm_contribution_total_amount'], $row['civicrm_contribution_currency']);
556 if (!empty($row['civicrm_financial_trxn_card_type_id'])) {
557 $rows[$rowNum]['civicrm_financial_trxn_card_type_id'] = $this->getLabels($row['civicrm_financial_trxn_card_type_id'], 'CRM_Financial_DAO_FinancialTrxn', 'card_type_id');
561 // Contribution amount links to view contribution
562 if (($value = CRM_Utils_Array
::value('civicrm_contribution_total_amount', $row)) &&
563 CRM_Core_Permission
::check('access CiviContribute')
565 $url = CRM_Utils_System
::url("civicrm/contact/view/contribution",
566 "reset=1&id=" . $row['civicrm_contribution_id'] . "&cid=" .
567 $row['civicrm_contact_id'] .
568 "&action=view&context=contribution&selectedChild=contribute",
571 $rows[$rowNum]['civicrm_contribution_total_amount_link'] = $url;
572 $rows[$rowNum]['civicrm_contribution_total_amount_hover'] = ts("View this contribution.");
576 // convert campaign_id to campaign title
577 if (array_key_exists('civicrm_contribution_campaign_id', $row)) {
578 if ($value = $row['civicrm_contribution_campaign_id']) {
579 $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns
[$value];
584 // skip looking further in rows, if first row itself doesn't