3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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-2017
35 class CRM_Report_Form_Contribute_HouseholdSummary
extends CRM_Report_Form
{
37 protected $_addressField = FALSE;
39 protected $_emailField = FALSE;
41 public $_drilldownReport = array('contribute/detail' => 'Link to Detail Report');
43 protected $_summary = NULL;
49 public function __construct() {
50 self
::validRelationships();
52 $config = CRM_Core_Config
::singleton();
53 $campaignEnabled = in_array("CiviCampaign", $config->enableComponents
);
54 if ($campaignEnabled) {
55 $getCampaigns = CRM_Campaign_BAO_Campaign
::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
56 $this->activeCampaigns
= $getCampaigns['campaigns'];
57 asort($this->activeCampaigns
);
60 $this->_columns
= array(
61 'civicrm_contact_household' => array(
62 'dao' => 'CRM_Contact_DAO_Contact',
64 'household_name' => array(
65 'title' => ts('Household Name'),
72 'contact_type' => array(
73 'title' => ts('Contact Type'),
75 'contact_sub_type' => array(
76 'title' => ts('Contact Subtype'),
80 'household_name' => array('title' => ts('Household Name')),
82 'grouping' => 'household-fields',
84 'civicrm_line_item' => array(
85 'dao' => 'CRM_Price_DAO_LineItem',
87 'civicrm_relationship' => array(
88 'dao' => 'CRM_Contact_DAO_Relationship',
90 'relationship_type_id' => array(
91 'title' => ts('Relationship Type'),
95 'relationship_type_id' => array(
96 'title' => ts('Relationship Type'),
97 'type' => CRM_Utils_Type
::T_INT
,
98 'operatorType' => CRM_Report_Form
::OP_SELECT
,
99 'options' => $this->relationTypes
,
100 'default' => key($this->relationTypes
),
103 'grouping' => 'household-fields',
105 'civicrm_contact' => array(
106 'dao' => 'CRM_Contact_DAO_Contact',
108 'sort_name' => array(
109 'title' => ts('Contact Name'),
113 'no_display' => TRUE,
117 'grouping' => 'contact-fields',
119 'civicrm_contribution' => array(
120 'dao' => 'CRM_Contribute_DAO_Contribution',
122 'total_amount' => array(
123 'title' => ts('Amount'),
127 'no_display' => TRUE,
130 'contribution_status_id' => array(
131 'title' => ts('Contribution Status'),
134 'check_number' => array(
135 'title' => ts('Check Number'),
139 'no_display' => TRUE,
142 'receive_date' => array('default' => TRUE),
143 'receipt_date' => NULL,
146 'receive_date' => array('operatorType' => CRM_Report_Form
::OP_DATE
),
147 'total_amount' => array('title' => ts('Amount Between')),
149 'title' => ts('Currency'),
150 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
151 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
153 'type' => CRM_Utils_Type
::T_STRING
,
155 'contribution_status_id' => array(
156 'title' => ts('Contribution Status'),
157 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
158 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
159 'default' => array(1),
162 'grouping' => 'contri-fields',
164 'civicrm_financial_trxn' => array(
165 'dao' => 'CRM_Financial_DAO_FinancialTrxn',
167 'card_type' => array(
168 'title' => ts('Credit Card Type'),
169 'dbAlias' => 'GROUP_CONCAT(financial_trxn_civireport.card_type SEPARATOR ",")',
173 'card_type' => array(
174 'title' => ts('Credit Card Type'),
175 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
176 'options' => CRM_Financial_DAO_FinancialTrxn
::buildOptions('card_type'),
178 'type' => CRM_Utils_Type
::T_STRING
,
182 'civicrm_address' => array(
183 'dao' => 'CRM_Core_DAO_Address',
185 'street_address' => NULL,
187 'postal_code' => NULL,
188 'state_province_id' => array(
189 'title' => ts('State/Province'),
191 'country_id' => array(
192 'title' => ts('Country'),
195 'grouping' => 'contact-fields',
197 'civicrm_email' => array(
198 'dao' => 'CRM_Core_DAO_Email',
202 'grouping' => 'contact-fields',
206 if ($campaignEnabled && !empty($this->activeCampaigns
)) {
207 $this->_columns
['civicrm_contribution']['fields']['campaign_id'] = array(
208 'title' => ts('Campaign'),
209 'default' => 'false',
211 $this->_columns
['civicrm_contribution']['filters']['campaign_id'] = array(
212 'title' => ts('Campaign'),
213 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
214 'options' => $this->activeCampaigns
,
215 'type' => CRM_Utils_Type
::T_INT
,
218 $this->_currencyColumn
= 'civicrm_contribution_currency';
219 parent
::__construct();
222 public function preProcess() {
223 parent
::preProcess();
226 public function select() {
227 $this->_columnHeaders
= $select = array();
228 foreach ($this->_columns
as $tableName => $table) {
229 if (array_key_exists('fields', $table)) {
230 foreach ($table['fields'] as $fieldName => $field) {
231 if (!empty($field['required']) ||
232 !empty($this->_params
['fields'][$fieldName])
234 if ($tableName == 'civicrm_address') {
235 $this->_addressField
= TRUE;
237 elseif ($tableName == 'civicrm_email') {
238 $this->_emailField
= TRUE;
241 if (!empty($field['statistics'])) {
242 foreach ($field['statistics'] as $stat => $label) {
243 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}_{$stat}";
244 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
245 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
246 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
250 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
252 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
253 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
259 $this->_selectClauses
= $select;
260 $this->_select
= "SELECT " . implode(', ', $select) . " ";
263 public function from() {
267 FROM civicrm_relationship {$this->_aliases['civicrm_relationship']}
268 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact_household']} ON
269 ({$this->_aliases['civicrm_contact_household']}.id = {$this->_aliases['civicrm_relationship']}.$this->householdContact AND {$this->_aliases['civicrm_contact_household']}.contact_type='Household')
270 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']} ON
271 ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_relationship']}.$this->otherContact )
273 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']} ON
274 ({$this->_aliases['civicrm_contribution']}.contact_id = {$this->_aliases['civicrm_relationship']}.$this->otherContact ) AND {$this->_aliases['civicrm_contribution']}.is_test = 0 ";
276 if ($this->_addressField
) {
278 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']} ON
279 {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND
280 {$this->_aliases['civicrm_address']}.is_primary = 1\n ";
282 if ($this->_emailField
) {
284 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']} ON
285 {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND
286 {$this->_aliases['civicrm_email']}.is_primary = 1\n ";
289 // for credit card type
290 $this->addFinancialTrxnFromClause();
293 public function where() {
295 foreach ($this->_columns
as $tableName => $table) {
296 if (array_key_exists('filters', $table)) {
297 foreach ($table['filters'] as $fieldName => $field) {
299 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
300 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
301 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
302 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
303 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
306 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
308 if ($fieldName == 'relationship_type_id') {
309 $clause = "{$this->_aliases['civicrm_relationship']}.relationship_type_id=" . $this->relationshipId
;
312 $clause = $this->whereClause($field,
314 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
315 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
316 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
322 if (!empty($clause)) {
323 $clauses[] = $clause;
329 if (empty($clauses)) {
330 $this->_where
= "WHERE ( 1 )";
333 $this->_where
= "WHERE " . implode(' AND ', $clauses);
336 if ($this->_aclWhere
) {
337 $this->_where
.= " AND {$this->_aclWhere} ";
341 public function groupBy() {
343 "{$this->_aliases['civicrm_relationship']}.$this->householdContact",
344 "{$this->_aliases['civicrm_relationship']}.$this->otherContact",
345 "{$this->_aliases['civicrm_contribution']}.id",
346 "{$this->_aliases['civicrm_relationship']}.relationship_type_id",
348 $this->_groupBy
= CRM_Contact_BAO_Query
::getGroupByFromSelectColumns($this->_selectClauses
, $groupBy);
351 public function orderBy() {
352 $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";
360 public function statistics(&$rows) {
361 $statistics = parent
::statistics($rows);
363 //hack filter display for relationship type
364 $type = substr($this->_params
['relationship_type_id_value'], -3);
365 foreach ($statistics['filters'] as $id => $value) {
367 $value['title'] == 'Relationship Type' &&
368 isset($this->relationTypes
[$this->relationshipId
. '_' . $type])
370 $statistics['filters'][$id]['value'] = 'Is equal to ' .
371 $this->relationTypes
[$this->relationshipId
. '_' . $type];
377 public function postProcess() {
379 $this->beginPostProcess();
380 $this->buildACLClause(array(
381 $this->_aliases
['civicrm_contact'],
382 $this->_aliases
['civicrm_contact_household'],
384 $sql = $this->buildQuery(TRUE);
387 $this->buildRows($sql, $rows);
388 $this->formatDisplay($rows);
389 $this->doTemplateAssignment($rows);
390 $this->endPostProcess($rows);
394 * Set variables to be accessed by API and form layer in processing.
396 public function beginPostProcessCommon() {
397 $getRelationship = $this->_params
['relationship_type_id_value'];
398 $type = substr($getRelationship, -3);
399 $this->relationshipId
= intval((substr($getRelationship, 0, strpos($getRelationship, '_'))));
400 if ($type == 'b_a') {
401 $this->householdContact
= 'contact_id_b';
402 $this->otherContact
= 'contact_id_a';
405 $this->householdContact
= 'contact_id_a';
406 $this->otherContact
= 'contact_id_b';
410 public function validRelationships() {
411 $this->relationTypes
= $relationTypes = array();
413 $params = array('contact_type_b' => 'Household', 'version' => 3);
414 $typesA = civicrm_api('relationship_type', 'get', $params);
415 if (empty($typesA['is_error'])) {
416 foreach ($typesA['values'] as $rel) {
417 $relationTypes[$rel['id']][$rel['id'] . '_b_a'] = $rel['label_b_a'];
420 $params = array('contact_type_a' => 'Household', 'version' => 3);
421 $typesB = civicrm_api('relationship_type', 'get', $params);
422 if (empty($typesB['is_error'])) {
423 foreach ($typesB['values'] as $rel) {
424 $relationTypes[$rel['id']][$rel['id'] . '_a_b'] = $rel['label_a_b'];
425 //$this->relationTypes[$rel['id'].'_a_b'] = $rel['label_a_b'];
429 ksort($relationTypes);
430 foreach ($relationTypes as $relationship) {
431 foreach ($relationship as $index => $label) {
432 $this->relationTypes
[$index] = $label;
438 * Alter display of rows.
440 * Iterate through the rows retrieved via SQL and make changes for display purposes,
441 * such as rendering contacts as links.
444 * Rows generated by SQL, with an array for each row.
446 public function alterDisplay(&$rows) {
447 $type = substr($this->_params
['relationship_type_id_value'], -3);
450 $flagHousehold = $flagContact = 0;
451 foreach ($rows as $rowNum => $row) {
453 //replace retionship id by relationship name
454 if (array_key_exists('civicrm_relationship_relationship_type_id', $row)) {
455 if ($value = $row['civicrm_relationship_relationship_type_id']) {
456 $rows[$rowNum]['civicrm_relationship_relationship_type_id'] = $this->relationTypes
[$value . '_' . $type];
461 //remove duplicate Organization names
462 if (array_key_exists('civicrm_contact_household_household_name', $row) &&
463 $this->_outputMode
!= 'csv'
465 if ($value = $row['civicrm_contact_household_household_name']) {
467 $priviousHousehold = $value;
470 if ($priviousHousehold == $value) {
472 $priviousHousehold = $value;
476 $priviousHousehold = $value;
480 if ($flagHousehold == 1) {
481 $rows[$rowNum]['civicrm_contact_household_household_name'] = "";
484 $url = CRM_Utils_System
::url('civicrm/contact/view',
485 'reset=1&cid=' . $rows[$rowNum]['civicrm_contact_household_id'],
489 $rows[$rowNum]['civicrm_contact_household_household_name'] = "<a href='$url' title='" . ts('View contact summary for this househould') . "'>" . $value . '</a>';
495 //remove duplicate Contact names and relationship type
496 if (array_key_exists('civicrm_contact_id', $row) &&
497 $this->_outputMode
!= 'csv'
499 if ($value = $row['civicrm_contact_id']) {
501 $priviousContact = $value;
504 if ($priviousContact == $value) {
506 $priviousContact = $value;
510 $priviousContact = $value;
514 if ($flagContact == 1 && $flagHousehold == 1) {
515 $rows[$rowNum]['civicrm_contact_sort_name'] = "";
516 $rows[$rowNum]['civicrm_relationship_relationship_type_id'] = "";
523 if (array_key_exists('civicrm_contribution_contribution_status_id', $row)) {
524 if ($value = $row['civicrm_contribution_contribution_status_id']) {
525 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = CRM_Contribute_PseudoConstant
::contributionStatus($value);
529 // handle state province
530 if (array_key_exists('civicrm_address_state_province_id', $row)) {
531 if ($value = $row['civicrm_address_state_province_id']) {
532 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvinceAbbreviation($value, FALSE);
538 if (array_key_exists('civicrm_address_country_id', $row)) {
539 if ($value = $row['civicrm_address_country_id']) {
540 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
545 // convert display name to links
546 if (array_key_exists('civicrm_contact_sort_name', $row) &&
547 $rows[$rowNum]['civicrm_contact_sort_name'] &&
548 array_key_exists('civicrm_contact_id', $row)
550 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
551 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
552 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
554 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
555 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts('View contribution details for this individual');
560 if (!empty($row['civicrm_contribution_total_amount'])) {
561 $row['civicrm_contribution_total_amount'] = CRM_Utils_Money
::format($row['civicrm_contribution_total_amount'], $row['civicrm_contribution_currency']);
564 if (!empty($row['civicrm_financial_trxn_card_type'])) {
565 $rows[$rowNum]['civicrm_financial_trxn_card_type'] = $this->getLabels($row['civicrm_financial_trxn_card_type'], 'CRM_Financial_DAO_FinancialTrxn', 'card_type');
569 // Contribution amount links to view contribution
570 if (($value = CRM_Utils_Array
::value('civicrm_contribution_total_amount', $row)) &&
571 CRM_Core_Permission
::check('access CiviContribute')
573 $url = CRM_Utils_System
::url("civicrm/contact/view/contribution",
574 "reset=1&id=" . $row['civicrm_contribution_id'] . "&cid=" .
575 $row['civicrm_contact_id'] .
576 "&action=view&context=contribution&selectedChild=contribute",
579 $rows[$rowNum]['civicrm_contribution_total_amount_link'] = $url;
580 $rows[$rowNum]['civicrm_contribution_total_amount_hover'] = ts("View this contribution.");
584 // convert campaign_id to campaign title
585 if (array_key_exists('civicrm_contribution_campaign_id', $row)) {
586 if ($value = $row['civicrm_contribution_campaign_id']) {
587 $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns
[$value];
592 // skip looking further in rows, if first row itself doesn't