3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
36 require_once 'CRM/Report/Form.php';
37 require_once 'CRM/Contribute/PseudoConstant.php';
38 require_once 'CRM/Core/OptionGroup.php';
39 require_once 'CRM/Event/BAO/Participant.php';
40 require_once 'CRM/Contact/BAO/Contact.php';
43 * Class CRM_Report_Form_Contribute_Baykeeper
45 class CRM_Report_Form_Contribute_Baykeeper
extends CRM_Report_Form
{
46 protected $_addressField = FALSE;
48 protected $_emailField = FALSE;
50 protected $_summary = NULL;
52 protected $_customGroupExtends = array('Contact', 'Contribution');
57 function __construct() {
58 $this->_columns
= array('civicrm_contact' =>
59 array('dao' => 'CRM_Contact_DAO_Contact',
61 array('display_name' =>
62 array('title' => ts('Contact Name'),
67 array('no_display' => FALSE,
68 'title' => ts('Contact ID'),
72 array('title' => ts('Contact ID'),
78 'addressee_display' => array('title' => ts('Addressee Name')),
79 'postal_greeting_display' => array('title' => ts('Greeting')),
80 'display_name_creditor' =>
81 array('title' => ts('Soft Credit Name'),
82 'name' => 'display_name',
83 'alias' => 'soft_credit',
87 array('title' => ts('Soft Credit Id'),
89 'alias' => 'soft_credit',
92 array('title' => ts('Employer Name'),
93 'name' => 'display_name',
94 'alias' => 'employer_company',
97 array('title' => ts('Employer Id'),
98 'name' => 'employer_id',
100 'do_not_mail' => array('title' => ts('Do Not Mail')),
104 array('title' => ts('Contact Name'),
105 'operator' => 'like',
108 array('title' => ts('Contact ID'),
109 'no_display' => TRUE,
112 'grouping' => 'contact-fields',
115 array('dao' => 'CRM_Core_DAO_Email',
118 array('title' => ts('Email'),
123 'grouping' => 'contact-fields',
126 array('dao' => 'CRM_Core_DAO_Phone',
129 array('title' => ts('Phone'),
134 'grouping' => 'contact-fields',
137 array('dao' => 'CRM_Core_DAO_Address',
139 array('street_address' => NULL,
140 'supplemental_address_1' => array('title' => ts('Sup Address 1'),
142 'supplemental_address_2' => array('title' => ts('Sup Address 2'),
145 'postal_code' => NULL,
146 'location_type_id' => array('title' => ts('Location Type ID'),
148 'state_province_id' =>
149 array('title' => ts('State/Province'),
152 array('title' => ts('Country'),
156 'grouping' => 'contact-fields',
158 array('country_id' =>
159 array('title' => ts('Country'),
160 'type' => CRM_Utils_Type
::T_INT
,
161 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
162 'options' => CRM_Core_PseudoConstant
::country(),
164 'state_province_id' =>
165 array('title' => ts('State/Province'),
166 'type' => CRM_Utils_Type
::T_INT
,
167 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
168 'options' => CRM_Core_PseudoConstant
::stateProvince(),
172 'civicrm_contribution' =>
173 array('dao' => 'CRM_Contribute_DAO_Contribution',
176 'contribution_id' => array(
178 'no_display' => TRUE,
181 'contribution_type_id' => array('title' => ts('Contribution Type'),
185 'receive_date' => array('default' => TRUE),
186 'receipt_date' => NULL,
187 'source' => array('title' => ts('Source')),
188 'fee_amount' => NULL,
189 'net_amount' => NULL,
190 'non_deductible_amount' => array('title' => ts('Non Deductible Amount')),
191 'total_amount' => array('title' => ts('Total Amount'),
194 'honor_contact_id' => array('title' => ts('Honor Contact ID'),
196 'honor_type_id' => array('title' => ts('Hon/Mem Type')),
199 array('receive_date' =>
200 array('operatorType' => CRM_Report_Form
::OP_DATE
),
201 'contribution_type_id' =>
202 array('title' => ts('Contribution Type'),
203 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
204 'options' => CRM_Contribute_PseudoConstant
::contributionType(),
206 'contribution_status_id' =>
207 array('title' => ts('Contribution Status'),
208 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
209 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
212 array('title' => ts('Contribution Amount')),
214 'grouping' => 'contri-fields',
216 'civicrm_contact_hon_mem' =>
217 array('dao' => 'CRM_Contact_DAO_Contact',
218 'alias' => 'hon_mem',
220 array('id' => array('title' => ts('Hon/Mem Contact ID'),
222 'no_display' => TRUE,
224 'display_name' => array('title' => ts('In Honor Of'),
228 'grouping' => 'contri-fields',
231 array('dao' => 'CRM_Core_DAO_Note',
235 array('title' => ts('Note'),
237 'no_repeat' => FALSE,
240 'grouping' => 'contri-fields',
242 'civicrm_contribution_soft' =>
243 array('dao' => 'CRM_Contribute_DAO_ContributionSoft',
245 array('contribution_id' =>
246 array('title' => ts('Contribution ID'),
247 'no_display' => TRUE,
251 array('title' => ts('Contact ID'),
252 'no_display' => TRUE,
256 array('default' => TRUE,
257 'no_display' => TRUE,
260 'grouping' => 'softcredit-fields',
262 'civicrm_group_field' =>
263 array('dao' => 'CRM_Contact_DAO_Group',
266 array('title' => ts('Groups')),
270 array('dao' => 'CRM_Contact_DAO_GroupContact',
277 array('name' => 'group_id',
278 'title' => ts('Group'),
279 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
281 'options' => CRM_Core_PseudoConstant
::group(),
285 'civicrm_contribution_ordinality' =>
286 array('dao' => 'CRM_Contribute_DAO_Contribution',
287 'alias' => 'cordinality',
289 array('ordinality' =>
290 array('title' => ts('Contribution Ordinality'),
291 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
292 'options' => array(0 => 'First by Contributor',
293 1 => 'Second or Later by Contributor',
300 $this->_options
= array('first_contribution' => array('title' => ts('First Contribution'),
301 'type' => 'checkbox',
303 'last_contribution' => array('title' => ts('Last Contribution'),
304 'type' => 'checkbox',
306 'include_nondonors' => array('title' => ts('Include non-donors?'),
307 'type' => 'checkbox',
311 $this->_tagFilter
= TRUE;
312 parent
::__construct();
315 function preProcess() {
316 parent
::preProcess();
322 $this->_columnHeaders
= array();
323 foreach ($this->_columns
as $tableName => $table) {
324 if (array_key_exists('fields', $table)) {
325 foreach ($table['fields'] as $fieldName => $field) {
326 if (!empty($field['required']) ||
!empty($this->_params
['fields'][$fieldName])) {
327 if ($tableName == 'civicrm_address') {
328 $this->_addressField
= TRUE;
330 elseif ($tableName == 'civicrm_email') {
331 $this->_emailField
= TRUE;
334 if ( $tableName == 'civicrm_group_field' && $fieldName == 'title' ) {
335 $select[] = "GROUP_CONCAT(DISTINCT {$field['dbAlias']} ORDER BY {$field['dbAlias']} ) as {$tableName}_{$fieldName}";
337 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
342 // only include statistics columns if set
343 if (!empty($field['statistics'])) {
344 foreach ($field['statistics'] as $stat => $label) {
345 switch (strtolower($stat)) {
347 $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
348 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
349 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
350 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
354 $select[] = "COUNT({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
355 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
356 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
360 $select[] = "ROUND(AVG({$field['dbAlias']}),2) as {$tableName}_{$fieldName}_{$stat}";
361 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
362 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
363 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
368 elseif ($tableName == 'civicrm_group_field' && $fieldName == 'title') {
369 $select[] = " GROUP_CONCAT(DISTINCT {$field['dbAlias']} ORDER BY {$field['dbAlias']} SEPARATOR ' | <br>') as {$tableName}_{$fieldName} ";
370 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
371 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
374 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
375 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
376 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
383 // insert first and last contribution at end
384 if (!empty($this->_params
['options']['first_contribution'])) {
385 $select[] = " '' as first_contribution";
386 $this->_columnHeaders
['first_contribution']['title'] = ts('First Contribution');
389 if (!empty($this->_params
['options']['last_contribution'])) {
390 $select[] = " '' as last_contribution";
391 $this->_columnHeaders
['last_contribution']['title'] = ts('Last Contribution');
395 $this->_select
= "SELECT " . implode(', ', $select) . " ";
399 $alias_constituent = 'constituentname';
400 $alias_creditor = 'soft_credit';
401 $alias_employer = "employer_company";
405 $hackValue = CRM_Utils_Array
::value('include_nondonors', $this->_params
['options'], 0);
406 $contribJoin = $hackValue ?
"LEFT" : "INNER";
409 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
410 $contribJoin JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
411 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id AND {$this->_aliases['civicrm_contribution']}.is_test = 0
412 $contribJoin JOIN (SELECT c.id, IF(COUNT(oc.id) = 0, 0, 1) AS ordinality FROM civicrm_contribution c LEFT JOIN civicrm_contribution oc ON c.contact_id = oc.contact_id AND oc.receive_date < c.receive_date GROUP BY c.id) {$this->_aliases['civicrm_contribution_ordinality']}
413 ON {$this->_aliases['civicrm_contribution_ordinality']}.id = {$this->_aliases['civicrm_contribution']}.id
414 LEFT JOIN civicrm_note {$this->_aliases['civicrm_note']}
415 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_note']}.contact_id AND
416 {$this->_aliases['civicrm_contribution']}.id = {$this->_aliases['civicrm_note']}.entity_id )
417 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
418 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
419 {$this->_aliases['civicrm_phone']}.is_primary = 1)
420 LEFT JOIN civicrm_contact {$alias_employer}
421 ON {$this->_aliases['civicrm_contact']}.employer_id =
423 LEFT JOIN civicrm_contribution_soft {$this->_aliases['civicrm_contribution_soft']}
424 ON {$this->_aliases['civicrm_contribution_soft']}.contribution_id =
425 {$this->_aliases['civicrm_contribution']}.id
426 LEFT JOIN civicrm_contact {$alias_creditor}
427 ON {$this->_aliases['civicrm_contribution_soft']}.contact_id =
429 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact_hon_mem']}
430 ON {$this->_aliases['civicrm_contribution']}.honor_contact_id = {$this->_aliases['civicrm_contact_hon_mem']}.id
432 // add group - concatenated
433 $this->_from
.= " LEFT JOIN civicrm_group_contact gc ON {$this->_aliases['civicrm_contact']}.id = gc.contact_id AND gc.status = 'Added'
434 LEFT JOIN civicrm_group {$this->_aliases['civicrm_group_field']} ON {$this->_aliases['civicrm_group_field']}.id = gc.group_id ";
436 if ($this->_addressField
OR (!empty($this->_params
['state_province_id_value']) OR !empty($this->_params
['country_id_value']))) {
438 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
439 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND
440 {$this->_aliases['civicrm_address']}.is_primary = 1\n";
443 if ($this->_emailField
) {
445 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
446 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND
447 {$this->_aliases['civicrm_email']}.is_primary = 1\n";
452 $this->_groupBy
= " GROUP BY {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_contribution']}.id ";
456 $this->_orderBy
= " ORDER BY {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_contribution']}.receive_date ";
464 function statistics(&$rows) {
465 $statistics = parent
::statistics($rows);
466 // because the query returns groups, the amount is multiplied by the number of groups a contact is in
467 // that's why this is disabled
468 /* SUM( {$this->_aliases['civicrm_contribution']}.total_amount ) as amount, */
472 SELECT COUNT({$this->_aliases['civicrm_contribution']}.total_amount ) as count,
473 ROUND(AVG({$this->_aliases['civicrm_contribution']}.total_amount), 2) as avg
475 $sql = "{$select} {$this->_from} {$this->_where}";
476 $dao = CRM_Core_DAO
::executeQuery($sql);
479 // because the query returns groups, the amount is multiplied by the number of groups a contact is in
480 // that's why this is disabled
481 /* SUM( {$this->_aliases['civicrm_contribution']}.total_amount ) as amount, */
485 $statistics['counts']['amount'] = array( 'value' => $dao->amount,
486 'title' => 'Total Amount',
487 'type' => CRM_Utils_Type::T_MONEY );
488 $statistics['counts']['avg'] = array( 'value' => $dao->avg,
489 'title' => 'Average',
490 'type' => CRM_Utils_Type::T_MONEY );
497 function postProcess() {
498 // get the acl clauses built before we assemble the query
499 $this->buildACLClause($this->_aliases
['civicrm_contact']);
500 parent
::postProcess();
506 function alterDisplay(&$rows) {
508 require_once 'CRM/Contribute/BAO/Contribution/Utils.php';
509 require_once 'CRM/Utils/Money.php';
510 require_once 'CRM/Utils/Date.php';
512 $config = &CRM_Core_Config
::singleton();
514 // custom code to alter rows
515 $checkList = array();
517 $display_flag = $prev_cid = $cid = 0;
518 $contributionTypes = CRM_Contribute_PseudoConstant
::contributionType();
520 foreach ($rows as $rowNum => $row) {
521 if (!empty($this->_noRepeats
) &&
522 $this->_outputMode
!= 'csv'
524 // don't repeat contact details if its same as the previous row
525 if (array_key_exists('civicrm_contact_id', $row)) {
526 if ($cid = $row['civicrm_contact_id']) {
531 if ($prev_cid == $cid) {
542 foreach ($row as $colName => $colVal) {
543 if (in_array($colName, $this->_noRepeats
)) {
544 unset($rows[$rowNum][$colName]);
553 if (array_key_exists('first_contribution', $row) ||
554 array_key_exists('last_contribution', $row)
556 $details = CRM_Contribute_BAO_Contribution_Utils
::getFirstLastDetails($row['civicrm_contact_id']);
557 if ($details['first']) {
558 $rows[$rowNum]['first_contribution'] = CRM_Utils_Money
::format($details['first']['total_amount']) . ' - ' . CRM_Utils_Date
::customFormat($details['first']['receive_date'], $config->dateformatFull
);
560 if ($details['last']) {
561 $rows[$rowNum]['last_contribution'] = CRM_Utils_Money
::format($details['last']['total_amount']) . ' - ' . CRM_Utils_Date
::customFormat($details['last']['receive_date'], $config->dateformatFull
);
565 // handle state province
566 if (array_key_exists('civicrm_address_state_province_id', $row)) {
567 if ($value = $row['civicrm_address_state_province_id']) {
568 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvince($value, FALSE);
570 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
572 "state_province_id_op=in&state_province_id_value={$value}",
573 $this->_absoluteUrl
, $this->_id
575 $rows[$rowNum]['civicrm_address_state_province_id_link'] = $url;
576 $rows[$rowNum]['civicrm_address_state_province_id_hover'] = ts("List all contribution(s) for this State.");
582 if (array_key_exists('civicrm_address_country_id', $row)) {
583 if ($value = $row['civicrm_address_country_id']) {
584 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
586 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
588 "country_id_op=in&country_id_value={$value}",
589 $this->_absoluteUrl
, $this->_id
591 $rows[$rowNum]['civicrm_address_country_id_link'] = $url;
592 $rows[$rowNum]['civicrm_address_country_id_hover'] = ts("List all contribution(s) for this Country.");
598 // convert display name to links
599 if (array_key_exists('civicrm_contact_display_name', $row) && !empty($rows[$rowNum]['civicrm_contact_display_name']) &&
600 array_key_exists('civicrm_contact_id', $row)
602 $url = CRM_Utils_System
::url("civicrm/contact/view",
603 'reset=1&cid=' . $row['civicrm_contact_id'],
606 $rows[$rowNum]['civicrm_contact_display_name_link'] = $url;
607 $rows[$rowNum]['civicrm_contact_display_name_hover'] = ts("View Contact Summary for this Contact.");
610 // convert soft credit id to link
611 if (array_key_exists('soft_credit_display_name', $row) && !empty($rows[$rowNum]['soft_credit_display_name']) &&
612 array_key_exists('id', $row)
614 $url = CRM_Utils_System
::url("civicrm/contact/view",
615 'reset=1&cid=' . $row['id'],
618 $rows[$rowNum]['soft_credit_display_name_creditor_link'] = $url;
619 $rows[$rowNum]['soft_credit_display_name_creditor_hover'] = ts("View Contact Summary for this Soft Credit.");
622 // convert hon/mem contact to link
623 if (array_key_exists('civicrm_contact_hon_mem_display_name', $row) && !empty($rows[$rowNum]['civicrm_contribution_honor_contact_id']) &&
624 array_key_exists('civicrm_contribution_honor_contact_id', $row)
626 $url = CRM_Utils_System
::url("civicrm/contact/view",
627 'reset=1&cid=' . $row['civicrm_contribution_honor_contact_id'],
630 $rows[$rowNum]['civicrm_contact_hon_mem_display_name_link'] = $url;
631 $rows[$rowNum]['civicrm_contact_hon_mem_display_name_hover'] = ts("View Contact Summary for this Honor/Memory Contribution.");
634 // honor of/memory of type
635 if ($value = CRM_Utils_Array
::value('civicrm_contribution_honor_type_id', $row) && !empty($rows[$rowNum]['civicrm_contribution_honor_type_id'])) {
636 // rather than do a join, just change the output here, since there these values are pretty static
639 if ($rows[$rowNum]['civicrm_contribution_honor_type_id'] == 1) {
640 $rows[$rowNum]['civicrm_contribution_honor_type_id'] = "In Honor Of";
642 elseif ($rows[$rowNum]['civicrm_contribution_honor_type_id'] == 2) {
643 $rows[$rowNum]['civicrm_contribution_honor_type_id'] = "In Memory Of";
646 $rows[$rowNum]['civicrm_contribution_honor_type_id'] = "n/a";
650 if ($value = CRM_Utils_Array
::value('civicrm_contribution_contribution_type_id', $row)) {
651 $rows[$rowNum]['civicrm_contribution_contribution_type_id'] = $contributionTypes[$value];
655 if (($value = CRM_Utils_Array
::value('civicrm_contribution_total_amount', $row)) &&
656 CRM_Core_Permission
::check('access CiviContribute')
658 $url = CRM_Utils_System
::url("civicrm/contact/view/contribution",
660 $row['civicrm_contribution_contribution_id'] .
662 $row['civicrm_contact_id'] .
663 "&action=view&context=contribution&selectedChild=contribute",
666 $rows[$rowNum]['civicrm_contribution_total_amount_link'] = $url;
667 $rows[$rowNum]['civicrm_contribution_total_amount_hover'] = ts("View Details of this Contribution.");
671 // skip looking further in rows, if first row itself doesn't
672 // have the column we need