3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
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
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;
48 public function __construct() {
49 self
::validRelationships();
51 $config = CRM_Core_Config
::singleton();
52 $campaignEnabled = in_array("CiviCampaign", $config->enableComponents
);
53 if ($campaignEnabled) {
54 $getCampaigns = CRM_Campaign_BAO_Campaign
::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
55 $this->activeCampaigns
= $getCampaigns['campaigns'];
56 asort($this->activeCampaigns
);
59 $this->_columns
= array(
60 'civicrm_contact_household' => array(
61 'dao' => 'CRM_Contact_DAO_Contact',
63 'household_name' => array(
64 'title' => ts('Household Name'),
71 'contact_type' => array(
72 'title' => ts('Contact Type'),
74 'contact_sub_type' => array(
75 'title' => ts('Contact Subtype'),
79 'household_name' => array(
80 'title' => ts('Household Name'),
82 'is_deleted' => array(
84 'type' => CRM_Utils_Type
::T_BOOLEAN
,
87 'grouping' => 'household-fields',
89 'civicrm_line_item' => array(
90 'dao' => 'CRM_Price_DAO_LineItem',
92 'civicrm_relationship' => array(
93 'dao' => 'CRM_Contact_DAO_Relationship',
95 'relationship_type_id' => array(
96 'title' => ts('Relationship Type'),
100 'relationship_type_id' => array(
101 'title' => ts('Relationship Type'),
102 'type' => CRM_Utils_Type
::T_INT
,
103 'operatorType' => CRM_Report_Form
::OP_SELECT
,
104 'options' => $this->relationTypes
,
105 'default' => key($this->relationTypes
),
108 'grouping' => 'household-fields',
110 'civicrm_contact' => array(
111 'dao' => 'CRM_Contact_DAO_Contact',
113 'sort_name' => array(
114 'title' => ts('Contact Name'),
118 'no_display' => TRUE,
122 'grouping' => 'contact-fields',
124 'civicrm_contribution' => array(
125 'dao' => 'CRM_Contribute_DAO_Contribution',
127 'total_amount' => array(
128 'title' => ts('Amount'),
132 'no_display' => TRUE,
135 'contribution_status_id' => array(
136 'title' => ts('Contribution Status'),
139 'check_number' => array(
140 'title' => ts('Check Number'),
144 'no_display' => TRUE,
146 'financial_type_id' => array(
147 'title' => ts('Financial Type'),
150 'receive_date' => array('default' => TRUE),
151 'receipt_date' => NULL,
154 'receive_date' => array('operatorType' => CRM_Report_Form
::OP_DATE
),
155 'total_amount' => array('title' => ts('Amount Between')),
157 'title' => ts('Currency'),
158 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
159 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
161 'type' => CRM_Utils_Type
::T_STRING
,
163 'contribution_status_id' => array(
164 'title' => ts('Contribution Status'),
165 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
166 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
167 'default' => array(1),
169 'financial_type_id' => array(
170 'title' => ts('Financial Type'),
171 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
172 'options' => CRM_Contribute_PseudoConstant
::financialType(),
175 'grouping' => 'contri-fields',
177 'civicrm_financial_trxn' => array(
178 'dao' => 'CRM_Financial_DAO_FinancialTrxn',
180 'card_type_id' => array(
181 'title' => ts('Credit Card Type'),
182 'dbAlias' => 'GROUP_CONCAT(financial_trxn_civireport.card_type_id SEPARATOR ",")',
186 'card_type_id' => array(
187 'title' => ts('Credit Card Type'),
188 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
189 'options' => CRM_Financial_DAO_FinancialTrxn
::buildOptions('card_type_id'),
191 'type' => CRM_Utils_Type
::T_STRING
,
195 'civicrm_address' => array(
196 'dao' => 'CRM_Core_DAO_Address',
198 'street_address' => NULL,
200 'postal_code' => NULL,
201 'state_province_id' => array(
202 'title' => ts('State/Province'),
204 'country_id' => array(
205 'title' => ts('Country'),
208 'grouping' => 'contact-fields',
210 'civicrm_email' => array(
211 'dao' => 'CRM_Core_DAO_Email',
215 'grouping' => 'contact-fields',
219 if ($campaignEnabled && !empty($this->activeCampaigns
)) {
220 $this->_columns
['civicrm_contribution']['fields']['campaign_id'] = array(
221 'title' => ts('Campaign'),
222 'default' => 'false',
224 $this->_columns
['civicrm_contribution']['filters']['campaign_id'] = array(
225 'title' => ts('Campaign'),
226 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
227 'options' => $this->activeCampaigns
,
228 'type' => CRM_Utils_Type
::T_INT
,
231 $this->_currencyColumn
= 'civicrm_contribution_currency';
232 parent
::__construct();
235 public function preProcess() {
236 parent
::preProcess();
239 public function select() {
240 $this->_columnHeaders
= $select = array();
241 foreach ($this->_columns
as $tableName => $table) {
242 if (array_key_exists('fields', $table)) {
243 foreach ($table['fields'] as $fieldName => $field) {
244 if (!empty($field['required']) ||
245 !empty($this->_params
['fields'][$fieldName])
247 if ($tableName == 'civicrm_address') {
248 $this->_addressField
= TRUE;
250 elseif ($tableName == 'civicrm_email') {
251 $this->_emailField
= TRUE;
254 if (!empty($field['statistics'])) {
255 foreach ($field['statistics'] as $stat => $label) {
256 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}_{$stat}";
257 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
258 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
259 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
263 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
265 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
266 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
272 $this->_selectClauses
= $select;
273 $this->_select
= "SELECT " . implode(', ', $select) . " ";
276 public function from() {
280 FROM civicrm_relationship {$this->_aliases['civicrm_relationship']}
281 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact_household']} ON
282 ({$this->_aliases['civicrm_contact_household']}.id = {$this->_aliases['civicrm_relationship']}.$this->householdContact AND {$this->_aliases['civicrm_contact_household']}.contact_type='Household')
283 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']} ON
284 ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_relationship']}.$this->otherContact )
286 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']} ON
287 ({$this->_aliases['civicrm_contribution']}.contact_id = {$this->_aliases['civicrm_relationship']}.$this->otherContact ) AND {$this->_aliases['civicrm_contribution']}.is_test = 0 ";
289 if ($this->_addressField
) {
291 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']} ON
292 {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND
293 {$this->_aliases['civicrm_address']}.is_primary = 1\n ";
295 if ($this->_emailField
) {
297 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']} ON
298 {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND
299 {$this->_aliases['civicrm_email']}.is_primary = 1\n ";
302 // for credit card type
303 $this->addFinancialTrxnFromClause();
306 public function where() {
308 foreach ($this->_columns
as $tableName => $table) {
309 if (array_key_exists('filters', $table)) {
310 foreach ($table['filters'] as $fieldName => $field) {
312 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
313 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
314 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
315 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
316 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
319 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
321 if ($fieldName == 'relationship_type_id') {
322 $clause = "{$this->_aliases['civicrm_relationship']}.relationship_type_id=" . $this->relationshipId
;
325 $clause = $this->whereClause($field,
327 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
328 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
329 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
335 if (!empty($clause)) {
336 $clauses[] = $clause;
342 if (empty($clauses)) {
343 $this->_where
= "WHERE ( 1 )";
346 $this->_where
= "WHERE " . implode(' AND ', $clauses);
349 if ($this->_aclWhere
) {
350 $this->_where
.= " AND {$this->_aclWhere} ";
354 public function groupBy() {
356 "{$this->_aliases['civicrm_relationship']}.$this->householdContact",
357 "{$this->_aliases['civicrm_relationship']}.$this->otherContact",
358 "{$this->_aliases['civicrm_contribution']}.id",
359 "{$this->_aliases['civicrm_relationship']}.relationship_type_id",
361 $this->_groupBy
= CRM_Contact_BAO_Query
::getGroupByFromSelectColumns($this->_selectClauses
, $groupBy);
364 public function orderBy() {
365 $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";
373 public function statistics(&$rows) {
374 $statistics = parent
::statistics($rows);
376 //hack filter display for relationship type
377 $type = substr($this->_params
['relationship_type_id_value'], -3);
378 foreach ($statistics['filters'] as $id => $value) {
380 $value['title'] == 'Relationship Type' &&
381 isset($this->relationTypes
[$this->relationshipId
. '_' . $type])
383 $statistics['filters'][$id]['value'] = 'Is equal to ' .
384 $this->relationTypes
[$this->relationshipId
. '_' . $type];
390 public function postProcess() {
392 $this->beginPostProcess();
393 $this->buildACLClause(array(
394 $this->_aliases
['civicrm_contact'],
395 $this->_aliases
['civicrm_contact_household'],
397 $sql = $this->buildQuery(TRUE);
400 $this->buildRows($sql, $rows);
401 $this->formatDisplay($rows);
402 $this->doTemplateAssignment($rows);
403 $this->endPostProcess($rows);
407 * Set variables to be accessed by API and form layer in processing.
409 public function beginPostProcessCommon() {
410 $getRelationship = $this->_params
['relationship_type_id_value'];
411 $type = substr($getRelationship, -3);
412 $this->relationshipId
= intval((substr($getRelationship, 0, strpos($getRelationship, '_'))));
413 if ($type == 'b_a') {
414 $this->householdContact
= 'contact_id_b';
415 $this->otherContact
= 'contact_id_a';
418 $this->householdContact
= 'contact_id_a';
419 $this->otherContact
= 'contact_id_b';
423 public function validRelationships() {
424 $this->relationTypes
= $relationTypes = array();
426 $params = array('contact_type_b' => 'Household', 'version' => 3);
427 $typesA = civicrm_api('relationship_type', 'get', $params);
428 if (empty($typesA['is_error'])) {
429 foreach ($typesA['values'] as $rel) {
430 $relationTypes[$rel['id']][$rel['id'] . '_b_a'] = $rel['label_b_a'];
433 $params = array('contact_type_a' => 'Household', 'version' => 3);
434 $typesB = civicrm_api('relationship_type', 'get', $params);
435 if (empty($typesB['is_error'])) {
436 foreach ($typesB['values'] as $rel) {
437 $relationTypes[$rel['id']][$rel['id'] . '_a_b'] = $rel['label_a_b'];
438 //$this->relationTypes[$rel['id'].'_a_b'] = $rel['label_a_b'];
442 ksort($relationTypes);
443 foreach ($relationTypes as $relationship) {
444 foreach ($relationship as $index => $label) {
445 $this->relationTypes
[$index] = $label;
451 * Alter display of rows.
453 * Iterate through the rows retrieved via SQL and make changes for display purposes,
454 * such as rendering contacts as links.
457 * Rows generated by SQL, with an array for each row.
459 public function alterDisplay(&$rows) {
460 $type = substr($this->_params
['relationship_type_id_value'], -3);
463 $flagHousehold = $flagContact = 0;
464 foreach ($rows as $rowNum => $row) {
466 //replace retionship id by relationship name
467 if (array_key_exists('civicrm_relationship_relationship_type_id', $row)) {
468 if ($value = $row['civicrm_relationship_relationship_type_id']) {
469 $rows[$rowNum]['civicrm_relationship_relationship_type_id'] = $this->relationTypes
[$value . '_' . $type];
474 //remove duplicate Organization names
475 if (array_key_exists('civicrm_contact_household_household_name', $row) &&
476 $this->_outputMode
!= 'csv'
478 if ($value = $row['civicrm_contact_household_household_name']) {
480 $priviousHousehold = $value;
483 if ($priviousHousehold == $value) {
485 $priviousHousehold = $value;
489 $priviousHousehold = $value;
493 if ($flagHousehold == 1) {
494 $rows[$rowNum]['civicrm_contact_household_household_name'] = "";
497 $url = CRM_Utils_System
::url('civicrm/contact/view',
498 'reset=1&cid=' . $rows[$rowNum]['civicrm_contact_household_id'],
502 $rows[$rowNum]['civicrm_contact_household_household_name'] = "<a href='$url' title='" . ts('View contact summary for this househould') . "'>" . $value . '</a>';
508 //remove duplicate Contact names and relationship type
509 if (array_key_exists('civicrm_contact_id', $row) &&
510 $this->_outputMode
!= 'csv'
512 if ($value = $row['civicrm_contact_id']) {
514 $priviousContact = $value;
517 if ($priviousContact == $value) {
519 $priviousContact = $value;
523 $priviousContact = $value;
527 if ($flagContact == 1 && $flagHousehold == 1) {
528 $rows[$rowNum]['civicrm_contact_sort_name'] = "";
529 $rows[$rowNum]['civicrm_relationship_relationship_type_id'] = "";
536 if (array_key_exists('civicrm_contribution_contribution_status_id', $row)) {
537 if ($value = $row['civicrm_contribution_contribution_status_id']) {
538 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = CRM_Contribute_PseudoConstant
::contributionStatus($value);
542 if (array_key_exists('civicrm_contribution_financial_type_id', $row)) {
543 if ($value = $row['civicrm_contribution_financial_type_id']) {
544 $rows[$rowNum]['civicrm_contribution_financial_type_id'] = CRM_Contribute_PseudoConstant
::financialType($value);
548 // handle state province
549 if (array_key_exists('civicrm_address_state_province_id', $row)) {
550 if ($value = $row['civicrm_address_state_province_id']) {
551 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvinceAbbreviation($value, FALSE);
557 if (array_key_exists('civicrm_address_country_id', $row)) {
558 if ($value = $row['civicrm_address_country_id']) {
559 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
564 // convert display name to links
565 if (array_key_exists('civicrm_contact_sort_name', $row) &&
566 $rows[$rowNum]['civicrm_contact_sort_name'] &&
567 array_key_exists('civicrm_contact_id', $row)
569 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
570 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
571 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
573 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
574 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts('View contribution details for this individual');
579 if (!empty($row['civicrm_contribution_total_amount'])) {
580 $row['civicrm_contribution_total_amount'] = CRM_Utils_Money
::format($row['civicrm_contribution_total_amount'], $row['civicrm_contribution_currency']);
583 if (!empty($row['civicrm_financial_trxn_card_type_id'])) {
584 $rows[$rowNum]['civicrm_financial_trxn_card_type_id'] = $this->getLabels($row['civicrm_financial_trxn_card_type_id'], 'CRM_Financial_DAO_FinancialTrxn', 'card_type_id');
588 // Contribution amount links to view contribution
589 if (($value = CRM_Utils_Array
::value('civicrm_contribution_total_amount', $row)) &&
590 CRM_Core_Permission
::check('access CiviContribute')
592 $url = CRM_Utils_System
::url("civicrm/contact/view/contribution",
593 "reset=1&id=" . $row['civicrm_contribution_id'] . "&cid=" .
594 $row['civicrm_contact_id'] .
595 "&action=view&context=contribution&selectedChild=contribute",
598 $rows[$rowNum]['civicrm_contribution_total_amount_link'] = $url;
599 $rows[$rowNum]['civicrm_contribution_total_amount_hover'] = ts("View this contribution.");
603 // convert campaign_id to campaign title
604 if (array_key_exists('civicrm_contribution_campaign_id', $row)) {
605 if ($value = $row['civicrm_contribution_campaign_id']) {
606 $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns
[$value];
611 // skip looking further in rows, if first row itself doesn't