4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2014
36 class CRM_Report_Form_Contribute_HouseholdSummary
extends CRM_Report_Form
{
38 protected $_addressField = FALSE;
40 protected $_emailField = FALSE;
42 public $_drilldownReport = array('contribute/detail' => 'Link to Detail Report');
44 protected $_summary = NULL;
52 function __construct() {
53 self
::validRelationships();
55 $config = CRM_Core_Config
::singleton();
56 $campaignEnabled = in_array("CiviCampaign", $config->enableComponents
);
57 if ($campaignEnabled) {
58 $getCampaigns = CRM_Campaign_BAO_Campaign
::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
59 $this->activeCampaigns
= $getCampaigns['campaigns'];
60 asort($this->activeCampaigns
);
63 $this->_columns
= array(
64 'civicrm_contact_household' => array(
65 'dao' => 'CRM_Contact_DAO_Contact',
69 'title' => ts('Household Name'),
76 'contact_type' => array(
77 'title' => ts('Contact Type'),
79 'contact_sub_type' => array(
80 'title' => ts('Contact SubType'),
84 'household_name' => array('title' => ts('Household Name')),
86 'grouping' => 'household-fields',
88 'civicrm_relationship' => array(
89 'dao' => 'CRM_Contact_DAO_Relationship',
91 'relationship_type_id' => array(
92 'title' => ts('Relationship Type'),
96 'relationship_type_id' => array(
97 'title' => ts('Relationship Type'),
98 'type' => CRM_Utils_Type
::T_INT
,
99 'operatorType' => CRM_Report_Form
::OP_SELECT
,
100 'options' => $this->relationTypes
,
101 'default' => key($this->relationTypes
),
104 'grouping' => 'household-fields',
106 'civicrm_contact' => array(
107 'dao' => 'CRM_Contact_DAO_Contact',
109 'sort_name' => array(
110 'title' => ts('Contact Name'),
114 'no_display' => TRUE,
118 'grouping' => 'contact-fields',
120 'civicrm_contribution' => array(
121 'dao' => 'CRM_Contribute_DAO_Contribution',
123 'total_amount' => array(
124 'title' => ts('Amount'),
128 'no_display' => TRUE,
131 'contribution_status_id' => array(
132 'title' => 'Contribution Status',
135 'check_number' => array(
136 'title' => ts('Check Number'),
140 'no_display' => TRUE,
143 'receive_date' => array('default' => TRUE),
144 'receipt_date' => NULL,
147 'receive_date' => array('operatorType' => CRM_Report_Form
::OP_DATE
),
148 'total_amount' => array('title' => ts('Amount Between')),
150 'title' => 'Currency',
151 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
152 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
154 'type' => CRM_Utils_Type
::T_STRING
,
156 'contribution_status_id' => array(
157 'title' => ts('Contribution Status'),
158 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
159 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
160 'default' => array(1),
163 'grouping' => 'contri-fields',
165 'civicrm_address' => array(
166 'dao' => 'CRM_Core_DAO_Address',
168 'street_address' => NULL,
170 'postal_code' => NULL,
171 'state_province_id' => array(
172 'title' => ts('State/Province'),
174 'country_id' => array(
175 'title' => ts('Country'),
178 'grouping' => 'contact-fields',
180 'civicrm_email' => array(
181 'dao' => 'CRM_Core_DAO_Email',
185 'grouping' => 'contact-fields',
189 if ($campaignEnabled && !empty($this->activeCampaigns
)) {
190 $this->_columns
['civicrm_contribution']['fields']['campaign_id'] = array(
191 'title' => 'Campaign',
192 'default' => 'false',
194 $this->_columns
['civicrm_contribution']['filters']['campaign_id'] = array(
195 'title' => ts('Campaign'),
196 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
197 'options' => $this->activeCampaigns
,
200 $this->_currencyColumn
= 'civicrm_contribution_currency';
201 parent
::__construct();
204 function preProcess() {
205 parent
::preProcess();
209 $this->_columnHeaders
= $select = array();
210 foreach ($this->_columns
as $tableName => $table) {
211 if (array_key_exists('fields', $table)) {
212 foreach ($table['fields'] as $fieldName => $field) {
213 if (!empty($field['required']) ||
!empty($this->_params
['fields'][$fieldName])) {
214 if ($tableName == 'civicrm_address') {
215 $this->_addressField
= TRUE;
217 elseif ($tableName == 'civicrm_email') {
218 $this->_emailField
= TRUE;
221 if (!empty($field['statistics'])) {
222 foreach ($field['statistics'] as $stat => $label) {
223 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}_{$stat}";
224 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
225 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
226 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
230 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
232 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
233 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
239 $this->_select
= "SELECT " . implode(', ', $select) . " ";
246 FROM civicrm_relationship {$this->_aliases['civicrm_relationship']}
247 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact_household']} ON
248 ({$this->_aliases['civicrm_contact_household']}.id = {$this->_aliases['civicrm_relationship']}.$this->householdContact AND {$this->_aliases['civicrm_contact_household']}.contact_type='Household')
249 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']} ON
250 ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_relationship']}.$this->otherContact )
252 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']} ON
253 ({$this->_aliases['civicrm_contribution']}.contact_id = {$this->_aliases['civicrm_relationship']}.$this->otherContact ) AND {$this->_aliases['civicrm_contribution']}.is_test = 0 ";
255 if ($this->_addressField
) {
257 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']} ON
258 {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND
259 {$this->_aliases['civicrm_address']}.is_primary = 1\n ";
261 if ($this->_emailField
) {
263 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']} ON
264 {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND
265 {$this->_aliases['civicrm_email']}.is_primary = 1\n ";
271 foreach ($this->_columns
as $tableName => $table) {
272 if (array_key_exists('filters', $table)) {
273 foreach ($table['filters'] as $fieldName => $field) {
275 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
276 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
277 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
278 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
280 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
283 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
285 if ($fieldName == 'relationship_type_id') {
286 $clause = "{$this->_aliases['civicrm_relationship']}.relationship_type_id=" . $this->relationshipId
;
289 $clause = $this->whereClause($field,
291 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
292 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
293 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
299 if (!empty($clause)) {
300 $clauses[] = $clause;
306 if (empty($clauses)) {
307 $this->_where
= "WHERE ( 1 )";
310 $this->_where
= "WHERE " . implode(' AND ', $clauses);
313 if ($this->_aclWhere
) {
314 $this->_where
.= " AND {$this->_aclWhere} ";
319 $this->_groupBy
= " GROUP BY {$this->_aliases['civicrm_relationship']}.$this->householdContact, {$this->_aliases['civicrm_relationship']}.$this->otherContact , {$this->_aliases['civicrm_contribution']}.id, {$this->_aliases['civicrm_relationship']}.relationship_type_id ";
323 $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";
331 function statistics(&$rows) {
332 $statistics = parent
::statistics($rows);
334 //hack filter display for relationship type
335 $type = substr($this->_params
['relationship_type_id_value'], -3);
336 foreach ($statistics['filters'] as $id => $value) {
338 $value['title'] == 'Relationship Type' &&
339 isset($this->relationTypes
[$this->relationshipId
. '_' . $type])
341 $statistics['filters'][$id]['value'] = 'Is equal to ' .
342 $this->relationTypes
[$this->relationshipId
. '_' . $type];
348 function postProcess() {
350 $this->beginPostProcess();
351 $this->buildACLClause(array($this->_aliases
['civicrm_contact'], $this->_aliases
['civicrm_contact_household']));
352 $sql = $this->buildQuery(TRUE);
355 $this->buildRows($sql, $rows);
356 $this->formatDisplay($rows);
357 $this->doTemplateAssignment($rows);
358 $this->endPostProcess($rows);
362 * Set variables to be accessed by API and form layer in processing
364 function beginPostProcessCommon() {
365 $getRelationship = $this->_params
['relationship_type_id_value'];
366 $type = substr($getRelationship, -3);
367 $this->relationshipId
= intval((substr($getRelationship, 0, strpos($getRelationship, '_'))));
368 if ($type == 'b_a') {
369 $this->householdContact
= 'contact_id_b';
370 $this->otherContact
= 'contact_id_a';
373 $this->householdContact
= 'contact_id_a';
374 $this->otherContact
= 'contact_id_b';
378 function validRelationships() {
379 $this->relationTypes
= $relationTypes = array();
381 $params = array('contact_type_b' => 'Household', 'version' => 3);
382 $typesA = civicrm_api('relationship_type', 'get', $params);
383 if (empty($typesA['is_error'])) {
384 foreach ($typesA['values'] as $rel) {
385 $relationTypes[$rel['id']][$rel['id'] . '_b_a'] = $rel['label_b_a'];
388 $params = array('contact_type_a' => 'Household', 'version' => 3);
389 $typesB = civicrm_api('relationship_type', 'get', $params);
390 if (empty($typesB['is_error'])) {
391 foreach ($typesB['values'] as $rel) {
392 $relationTypes[$rel['id']][$rel['id'] . '_a_b'] = $rel['label_a_b'];
393 //$this->relationTypes[$rel['id'].'_a_b'] = $rel['label_a_b'];
397 ksort($relationTypes);
398 foreach ($relationTypes as $relationship) {
399 foreach ($relationship as $index => $label) {
400 $this->relationTypes
[$index] = $label;
408 function alterDisplay(&$rows) {
409 // custom code to alter rows
410 $type = substr($this->_params
['relationship_type_id_value'], -3);
413 $flagHousehold = $flagContact = 0;
414 foreach ($rows as $rowNum => $row) {
416 //replace retionship id by relationship name
417 if (array_key_exists('civicrm_relationship_relationship_type_id', $row)) {
418 if ($value = $row['civicrm_relationship_relationship_type_id']) {
419 $rows[$rowNum]['civicrm_relationship_relationship_type_id'] = $this->relationTypes
[$value . '_' . $type];
424 //remove duplicate Organization names
425 if (array_key_exists('civicrm_contact_household_household_name', $row) && $this->_outputMode
!= 'csv') {
426 if ($value = $row['civicrm_contact_household_household_name']) {
428 $priviousHousehold = $value;
431 if ($priviousHousehold == $value) {
433 $priviousHousehold = $value;
437 $priviousHousehold = $value;
441 if ($flagHousehold == 1) {
442 $rows[$rowNum]['civicrm_contact_household_household_name'] = "";
445 $url = CRM_Utils_System
::url('civicrm/contact/view',
446 'reset=1&cid=' . $rows[$rowNum]['civicrm_contact_household_id'],
450 $rows[$rowNum]['civicrm_contact_household_household_name'] =
451 "<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) && $this->_outputMode
!= 'csv') {
459 if ($value = $row['civicrm_contact_id']) {
461 $priviousContact = $value;
464 if ($priviousContact == $value) {
466 $priviousContact = $value;
470 $priviousContact = $value;
474 if ($flagContact == 1 && $flagHousehold == 1) {
475 $rows[$rowNum]['civicrm_contact_sort_name'] = "";
476 $rows[$rowNum]['civicrm_relationship_relationship_type_id'] = "";
483 if (array_key_exists('civicrm_contribution_contribution_status_id', $row)) {
484 if ($value = $row['civicrm_contribution_contribution_status_id']) {
485 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = CRM_Contribute_PseudoConstant
::contributionStatus($value);
489 // handle state province
490 if (array_key_exists('civicrm_address_state_province_id', $row)) {
491 if ($value = $row['civicrm_address_state_province_id']) {
492 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvinceAbbreviation($value, FALSE);
498 if (array_key_exists('civicrm_address_country_id', $row)) {
499 if ($value = $row['civicrm_address_country_id']) {
500 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
505 // convert display name to links
506 if (array_key_exists('civicrm_contact_sort_name', $row) &&
507 $rows[$rowNum]['civicrm_contact_sort_name'] &&
508 array_key_exists('civicrm_contact_id', $row)
510 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
511 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
512 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
514 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
515 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts('View contribution details for this individual');
520 if (!empty($row['civicrm_contribution_total_amount'])) {
521 $row['civicrm_contribution_total_amount'] = CRM_Utils_Money
::format($row['civicrm_contribution_total_amount'], $row['civicrm_contribution_currency']);
524 // Contribution amount links to view contribution
525 if (($value = CRM_Utils_Array
::value('civicrm_contribution_total_amount', $row)) &&
526 CRM_Core_Permission
::check('access CiviContribute')
528 $url = CRM_Utils_System
::url("civicrm/contact/view/contribution",
529 "reset=1&id=" . $row['civicrm_contribution_id'] . "&cid=" . $row['civicrm_contact_id'] .
530 "&action=view&context=contribution&selectedChild=contribute",
533 $rows[$rowNum]['civicrm_contribution_total_amount_link'] = $url;
534 $rows[$rowNum]['civicrm_contribution_total_amount_hover'] = ts("View this contribution.");
538 // convert campaign_id to campaign title
539 if (array_key_exists('civicrm_contribution_campaign_id', $row)) {
540 if ($value = $row['civicrm_contribution_campaign_id']) {
541 $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns
[$value];
546 // skip looking further in rows, if first row itself doesn't