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
35 class CRM_Report_Form_Contribute_Lybunt
extends CRM_Report_Form
{
37 protected $_charts = array(
39 'barChart' => 'Bar Chart',
40 'pieChart' => 'Pie Chart',
43 protected $_customGroupExtends = array(
49 public $_drilldownReport = array('contribute/detail' => 'Link to Detail Report');
51 protected $lifeTime_from = NULL;
52 protected $lifeTime_where = NULL;
56 public function __construct() {
57 $this->_autoIncludeIndexedFieldsAsOrderBys
= 1;
60 $date = CRM_Core_SelectValues
::date('custom', NULL, $yearsInPast, $yearsInFuture);
61 $count = $date['maxYear'];
62 while ($date['minYear'] <= $count) {
63 $optionYear[$date['minYear']] = $date['minYear'];
67 // Check if CiviCampaign is a) enabled and b) has active campaigns
68 $config = CRM_Core_Config
::singleton();
69 $campaignEnabled = in_array("CiviCampaign", $config->enableComponents
);
70 if ($campaignEnabled) {
71 $getCampaigns = CRM_Campaign_BAO_Campaign
::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
72 $this->activeCampaigns
= $getCampaigns['campaigns'];
73 asort($this->activeCampaigns
);
76 $this->_columns
= array(
77 'civicrm_contact' => array(
78 'dao' => 'CRM_Contact_DAO_Contact',
79 'grouping' => 'contact-field',
82 'title' => ts('Donor Name'),
86 'first_name' => array(
87 'title' => ts('First Name'),
89 'middle_name' => array(
90 'title' => ts('Middle Name'),
93 'title' => ts('Last Name'),
100 'title' => ts('Gender'),
102 'birth_date' => array(
103 'title' => ts('Birth Date'),
106 'title' => ts('Age'),
107 'dbAlias' => 'TIMESTAMPDIFF(YEAR, contact_civireport.birth_date, CURDATE())',
109 'contact_type' => array(
110 'title' => ts('Contact Type'),
112 'contact_sub_type' => array(
113 'title' => ts('Contact Subtype'),
116 'grouping' => 'contact-fields',
117 'order_bys' => array(
118 'sort_name' => array(
119 'title' => ts('Last Name, First Name'),
121 'default_weight' => '0',
122 'default_order' => 'ASC',
124 'first_name' => array(
125 'name' => 'first_name',
126 'title' => ts('First Name'),
128 'gender_id' => array(
129 'name' => 'gender_id',
130 'title' => ts('Gender'),
132 'birth_date' => array(
133 'name' => 'birth_date',
134 'title' => ts('Birth Date'),
136 'contact_type' => array(
137 'title' => ts('Contact Type'),
139 'contact_sub_type' => array(
140 'title' => ts('Contact Subtype'),
144 'sort_name' => array(
145 'title' => ts('Donor Name'),
146 'operator' => 'like',
149 'title' => ts('Contact ID'),
150 'no_display' => TRUE,
152 'gender_id' => array(
153 'title' => ts('Gender'),
154 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
155 'options' => CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'gender_id'),
157 'birth_date' => array(
158 'title' => ts('Birth Date'),
159 'operatorType' => CRM_Report_Form
::OP_DATE
,
161 'contact_type' => array(
162 'title' => ts('Contact Type'),
164 'contact_sub_type' => array(
165 'title' => ts('Contact Subtype'),
169 'civicrm_line_item' => array(
170 'dao' => 'CRM_Price_DAO_LineItem',
172 'civicrm_email' => array(
173 'dao' => 'CRM_Core_DAO_Email',
174 'grouping' => 'contact-field',
177 'title' => ts('Email'),
182 'civicrm_phone' => array(
183 'dao' => 'CRM_Core_DAO_Phone',
184 'grouping' => 'contact-field',
187 'title' => ts('Phone'),
193 $this->_columns +
= $this->addAddressFields();
194 $this->_columns +
= array(
195 'civicrm_contribution' => array(
196 'dao' => 'CRM_Contribute_DAO_Contribution',
198 'contact_id' => array(
199 'title' => ts('contactId'),
200 'no_display' => TRUE,
204 'total_amount' => array(
205 'title' => ts('Total Amount'),
206 'no_display' => TRUE,
210 'receive_date' => array(
211 'title' => ts('Year'),
212 'no_display' => TRUE,
219 'name' => 'receive_date',
220 'title' => ts('This Year'),
221 'operatorType' => CRM_Report_Form
::OP_SELECT
,
222 'options' => $optionYear,
223 'default' => date('Y'),
225 'financial_type_id' => array(
226 'title' => ts('Financial Type'),
227 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
228 'options' => CRM_Financial_BAO_FinancialType
::getAvailableFinancialTypes(),
230 'contribution_status_id' => array(
231 'title' => ts('Contribution Status'),
232 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
233 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
234 'default' => array('1'),
240 // If we have a campaign, build out the relevant elements
241 if ($campaignEnabled && !empty($this->activeCampaigns
)) {
242 $this->_columns
['civicrm_contribution']['fields']['campaign_id'] = array(
243 'title' => ts('Campaign'),
244 'default' => 'false',
246 $this->_columns
['civicrm_contribution']['filters']['campaign_id'] = array(
247 'title' => ts('Campaign'),
248 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
249 'options' => $this->activeCampaigns
,
253 $this->_groupFilter
= TRUE;
254 $this->_tagFilter
= TRUE;
255 parent
::__construct();
258 public function preProcess() {
259 parent
::preProcess();
262 public function select() {
264 $this->_columnHeaders
= $select = array();
265 $current_year = $this->_params
['yid_value'];
266 $previous_year = $current_year - 1;
268 foreach ($this->_columns
as $tableName => $table) {
270 if (array_key_exists('fields', $table)) {
271 foreach ($table['fields'] as $fieldName => $field) {
273 if (!empty($field['required']) ||
274 !empty($this->_params
['fields'][$fieldName])
276 if ($fieldName == 'total_amount') {
277 $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}";
279 $this->_columnHeaders
["{$previous_year}"]['type'] = $field['type'];
280 $this->_columnHeaders
["{$previous_year}"]['title'] = $previous_year;
282 $this->_columnHeaders
["civicrm_life_time_total"]['type'] = $field['type'];
283 $this->_columnHeaders
["civicrm_life_time_total"]['title'] = 'LifeTime';;
285 elseif ($fieldName == 'receive_date') {
286 $select[] = self
::fiscalYearOffset($field['dbAlias']) .
287 " as {$tableName}_{$fieldName} ";
290 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName} ";
291 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
292 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
295 if (!empty($field['no_display'])) {
296 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['no_display'] = TRUE;
303 $this->_select
= "SELECT " . implode(', ', $select) . " ";
306 public function from() {
309 FROM civicrm_contribution {$this->_aliases['civicrm_contribution']}
310 INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
311 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id
313 LEFT JOIN civicrm_line_item {$this->_aliases['civicrm_line_item']}
314 ON {$this->_aliases['civicrm_contribution']}.id = {$this->_aliases['civicrm_line_item']}.contribution_id AND
315 {$this->_aliases['civicrm_line_item']}.entity_table = 'civicrm_contribution'";
317 if ($this->isTableSelected('civicrm_email')) {
319 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
320 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id
321 AND {$this->_aliases['civicrm_email']}.is_primary = 1";
323 if ($this->isTableSelected('civicrm_phone')) {
325 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
326 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
327 {$this->_aliases['civicrm_phone']}.is_primary = 1";
329 $this->addAddressFromClause();
332 public function where() {
333 $this->_statusClause
= "";
334 $clauses = array($this->_aliases
['civicrm_contribution'] . '.is_test = 0');
335 $current_year = $this->_params
['yid_value'];
336 $previous_year = $current_year - 1;
338 foreach ($this->_columns
as $tableName => $table) {
339 if (array_key_exists('filters', $table)) {
340 foreach ($table['filters'] as $fieldName => $field) {
342 if ($fieldName == 'yid') {
343 $clause = "contribution_civireport.contact_id NOT IN
344 (SELECT distinct contri.contact_id FROM civicrm_contribution contri
345 WHERE contri.is_test = 0 AND " .
346 self
::fiscalYearOffset('contri.receive_date') . " = $current_year) AND contribution_civireport.contact_id IN (SELECT distinct contri.contact_id FROM civicrm_contribution contri
347 WHERE " . self
::fiscalYearOffset('contri.receive_date') .
348 " = $previous_year AND contri.is_test = 0) AND " . self
::fiscalYearOffset('contribution_civireport.receive_date') . " = $previous_year";
350 elseif (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
352 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
353 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
354 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
356 if ($relative ||
$from ||
$to) {
357 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
361 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
363 $clause = $this->whereClause($field,
365 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
366 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
367 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
369 if (($fieldName == 'contribution_status_id' ||
370 $fieldName == 'financial_type_id') && !empty($clause)
372 $this->_statusClause
.= " AND " . $clause;
377 if (!empty($clause)) {
378 $clauses[] = $clause;
384 $this->_where
= 'WHERE ' . implode(' AND ', $clauses);
386 if ($this->_aclWhere
) {
387 $this->_where
.= " AND {$this->_aclWhere} ";
389 CRM_Financial_BAO_FinancialType
::getAvailableFinancialTypes($financialTypes);
390 $this->_where
.= " AND {$this->_aliases['civicrm_contribution']}.financial_type_id IN (" . implode(',' , array_keys($financialTypes)) . ")";
391 $this->_where
.= " AND {$this->_aliases['civicrm_line_item']}.financial_type_id IN (" . implode(',' , array_keys($financialTypes)) . ")";
394 public function groupBy() {
395 $this->_groupBy
= "GROUP BY {$this->_aliases['civicrm_contribution']}.contact_id, " .
396 self
::fiscalYearOffset($this->_aliases
['civicrm_contribution'] .
397 '.receive_date') . " WITH ROLLUP";
398 $this->assign('chartSupported', TRUE);
406 public function statistics(&$rows) {
407 $statistics = parent
::statistics($rows);
409 $select = "SELECT SUM({$this->_aliases['civicrm_contribution']}.total_amount ) as amount ";
410 $where = "WHERE {$this->_aliases['civicrm_contact']}.id IN (" . implode(',', $this->_contactIds
) . ")
411 AND {$this->_aliases['civicrm_contribution']}.is_test = 0 {$this->_statusClause}";
413 $sql = "{$select} {$this->_from} {$where}";
414 $dao = CRM_Core_DAO
::executeQuery($sql);
416 $statistics['counts']['amount'] = array(
417 'value' => $dao->amount
,
418 'title' => 'Total LifeTime',
419 'type' => CRM_Utils_Type
::T_MONEY
,
427 public function postProcess() {
429 // get ready with post process params
430 $this->beginPostProcess();
432 // get the acl clauses built before we assemble the query
433 $this->buildACLClause($this->_aliases
['civicrm_contact']);
439 $rows = $this->_contactIds
= array();
441 $getContacts = "SELECT SQL_CALC_FOUND_ROWS {$this->_aliases['civicrm_contact']}.id as cid {$this->_from} {$this->_where} GROUP BY {$this->_aliases['civicrm_contact']}.id {$this->_limit}";
443 $dao = CRM_Core_DAO
::executeQuery($getContacts);
445 while ($dao->fetch()) {
446 $this->_contactIds
[] = $dao->cid
;
449 if (empty($this->_params
['charts'])) {
453 if (!empty($this->_contactIds
) ||
!empty($this->_params
['charts'])) {
454 $sql = "{$this->_select} {$this->_from} WHERE {$this->_aliases['civicrm_contact']}.id IN (" . implode(',', $this->_contactIds
) . ")
455 AND {$this->_aliases['civicrm_contribution']}.is_test = 0 {$this->_statusClause} {$this->_groupBy} ";
457 $dao = CRM_Core_DAO
::executeQuery($sql);
458 $current_year = $this->_params
['yid_value'];
459 $previous_year = $current_year - 1;
461 while ($dao->fetch()) {
463 if (!$dao->civicrm_contribution_contact_id
) {
468 foreach ($this->_columnHeaders
as $key => $value) {
469 if (property_exists($dao, $key)) {
470 $rows[$dao->civicrm_contribution_contact_id
][$key] = $dao->$key;
474 if ($dao->civicrm_contribution_receive_date
) {
475 if ($dao->civicrm_contribution_receive_date
== $previous_year) {
476 $rows[$dao->civicrm_contribution_contact_id
][$dao->civicrm_contribution_receive_date
] = $dao->civicrm_contribution_total_amount
;
480 $rows[$dao->civicrm_contribution_contact_id
]['civicrm_life_time_total'] = $dao->civicrm_contribution_total_amount
;
486 $this->formatDisplay($rows, FALSE);
488 // assign variables to templates
489 $this->doTemplateAssignment($rows);
491 // do print / pdf / instance stuff if needed
492 $this->endPostProcess($rows);
498 public function buildChart(&$rows) {
500 $graphRows = array();
504 $current_year = $this->_params
['yid_value'];
505 $previous_year = $current_year - 1;
506 $interval[$previous_year] = $previous_year;
507 $interval['life_time'] = 'Life Time';
509 foreach ($rows as $key => $row) {
510 $display['life_time'] = CRM_Utils_Array
::value('life_time', $display) +
511 $row['civicrm_life_time_total'];
512 $display[$previous_year] = CRM_Utils_Array
::value($previous_year, $display) +
$row[$previous_year];
515 $config = CRM_Core_Config
::Singleton();
516 $graphRows['value'] = $display;
518 'legend' => ts('Lybunt Report'),
519 'xname' => ts('Year'),
520 'yname' => ts('Amount (%1)', array(1 => $config->defaultCurrency
)),
522 if ($this->_params
['charts']) {
524 CRM_Utils_OpenFlashChart
::reportChart($graphRows, $this->_params
['charts'], $interval, $chartInfo);
525 $this->assign('chartType', $this->_params
['charts']);
530 * Alter display of rows.
532 * Iterate through the rows retrieved via SQL and make changes for display purposes,
533 * such as rendering contacts as links.
536 * Rows generated by SQL, with an array for each row.
538 public function alterDisplay(&$rows) {
541 foreach ($rows as $rowNum => $row) {
542 //Convert Display name into link
543 if (array_key_exists('civicrm_contact_sort_name', $row) &&
544 array_key_exists('civicrm_contribution_contact_id', $row)
546 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
547 'reset=1&force=1&id_op=eq&id_value=' .
548 $row['civicrm_contribution_contact_id'],
549 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
551 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
552 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contribution Details for this Contact.");
556 // convert campaign_id to campaign title
557 if (array_key_exists('civicrm_contribution_campaign_id', $row)) {
558 if ($value = $row['civicrm_contribution_campaign_id']) {
559 $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns
[$value];
564 $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'contribute/detail', 'List all contribution(s)') ?
TRUE : $entryFound;
567 if (array_key_exists('civicrm_contact_gender_id', $row)) {
568 if ($value = $row['civicrm_contact_gender_id']) {
569 $gender = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'gender_id');
570 $rows[$rowNum]['civicrm_contact_gender_id'] = $gender[$value];
575 // display birthday in the configured custom format
576 if (array_key_exists('civicrm_contact_birth_date', $row)) {
577 $birthDate = $row['civicrm_contact_birth_date'];
579 $rows[$rowNum]['civicrm_contact_birth_date'] = CRM_Utils_Date
::customFormat($birthDate, '%Y%m%d');
584 // skip looking further in rows, if first row itself doesn't
585 // have the column we need
593 * Override "This Year" $op options
594 * @param string $type
595 * @param null $fieldName
599 public function getOperationPair($type = "string", $fieldName = NULL) {
600 if ($fieldName == 'yid') {
602 'calendar' => ts('Is Calendar Year'),
603 'fiscal' => ts('Fiscal Year Starting'),
606 return parent
::getOperationPair($type, $fieldName);