3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
35 class CRM_Report_Form_Contribute_Lybunt
extends CRM_Report_Form
{
38 protected $_charts = array(
40 'barChart' => 'Bar Chart',
41 'pieChart' => 'Pie Chart',
44 public $_drilldownReport = array('contribute/detail' => 'Link to Detail Report');
46 protected $lifeTime_from = NULL;
47 protected $lifeTime_where = NULL;
49 function __construct() {
52 $date = CRM_Core_SelectValues
::date('custom', NULL, $yearsInPast, $yearsInFuture);
53 $count = $date['maxYear'];
54 while ($date['minYear'] <= $count) {
55 $optionYear[$date['minYear']] = $date['minYear'];
59 // Check if CiviCampaign is a) enabled and b) has active campaigns
60 $config = CRM_Core_Config
::singleton();
61 $campaignEnabled = in_array("CiviCampaign", $config->enableComponents
);
62 if ($campaignEnabled) {
63 $getCampaigns = CRM_Campaign_BAO_Campaign
::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
64 $this->activeCampaigns
= $getCampaigns['campaigns'];
65 asort($this->activeCampaigns
);
68 $this->_columns
= array(
71 'dao' => 'CRM_Contact_DAO_Contact',
72 'grouping' => 'contact-field',
76 array('title' => ts('Donor Name'),
80 'first_name' => array(
81 'title' => ts('First Name'),
84 'title' => ts('Last Name'),
88 'title' => ts('Contact Type'),
92 'title' => ts('Contact SubType'),
98 array('title' => ts('Donor Name'),
105 'dao' => 'CRM_Core_DAO_Email',
106 'grouping' => 'contact-field',
110 array('title' => ts('Email'),
117 'dao' => 'CRM_Core_DAO_Phone',
118 'grouping' => 'contact-field',
122 array('title' => ts('Phone'),
128 +
$this->addAddressFields()
130 'civicrm_contribution' =>
132 'dao' => 'CRM_Contribute_DAO_Contribution',
136 array('title' => ts('contactId'),
137 'no_display' => TRUE,
142 array('title' => ts('Total Amount'),
143 'no_display' => TRUE,
148 array('title' => ts('Year'),
149 'no_display' => TRUE,
158 'name' => 'receive_date',
159 'title' => ts('This Year'),
160 'operatorType' => CRM_Report_Form
::OP_SELECT
,
161 'options' => $optionYear,
162 'default' => date('Y'),
164 'financial_type_id' => array(
165 'title' => ts('Financial Type'),
166 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
167 'options' => CRM_Contribute_PseudoConstant
::financialType(),
169 'contribution_status_id' =>
170 array('title' => ts('Contribution Status'),
171 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
172 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
173 'default' => array('1'),
179 'dao' => 'CRM_Contact_DAO_GroupContact',
185 'name' => 'group_id',
186 'title' => ts('Group'),
187 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
189 'options' => CRM_Core_PseudoConstant
::group(),
195 // If we have a campaign, build out the relevant elements
196 if ($campaignEnabled && !empty($this->activeCampaigns
)) {
197 $this->_columns
['civicrm_contribution']['fields']['campaign_id'] = array(
198 'title' => ts('Campaign'),
199 'default' => 'false',
201 $this->_columns
['civicrm_contribution']['filters']['campaign_id'] = array('title' => ts('Campaign'),
202 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
203 'options' => $this->activeCampaigns
,
207 $this->_tagFilter
= TRUE;
208 parent
::__construct();
211 function preProcess() {
212 parent
::preProcess();
217 $this->_columnHeaders
= $select = array();
218 $current_year = $this->_params
['yid_value'];
219 $previous_year = $current_year - 1;
222 foreach ($this->_columns
as $tableName => $table) {
224 if (array_key_exists('fields', $table)) {
225 foreach ($table['fields'] as $fieldName => $field) {
227 if (!empty($field['required']) ||
!empty($this->_params
['fields'][$fieldName])) {
228 if ($fieldName == 'total_amount') {
229 $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}";
231 $this->_columnHeaders
["{$previous_year}"]['type'] = $field['type'];
232 $this->_columnHeaders
["{$previous_year}"]['title'] = $previous_year;
234 $this->_columnHeaders
["civicrm_life_time_total"]['type'] = $field['type'];
235 $this->_columnHeaders
["civicrm_life_time_total"]['title'] = 'LifeTime';;
237 elseif ($fieldName == 'receive_date') {
238 $select[] = self
::fiscalYearOffset($field['dbAlias']) . " as {$tableName}_{$fieldName} ";
241 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName} ";
242 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
243 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
246 if (!empty($field['no_display'])) {
247 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['no_display'] = TRUE;
254 $this->_select
= "SELECT " . implode(', ', $select) . " ";
260 FROM civicrm_contribution {$this->_aliases['civicrm_contribution']}
261 INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
262 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id
265 if ($this->isTableSelected('civicrm_email')) {
267 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
268 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id
269 AND {$this->_aliases['civicrm_email']}.is_primary = 1";
271 if ($this->isTableSelected('civicrm_phone')) {
273 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
274 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
275 {$this->_aliases['civicrm_phone']}.is_primary = 1";
277 $this->addAddressFromClause();
281 $this->_statusClause
= "";
282 $clauses = array($this->_aliases
['civicrm_contribution'] . '.is_test = 0');
283 $current_year = $this->_params
['yid_value'];
284 $previous_year = $current_year - 1;
286 foreach ($this->_columns
as $tableName => $table) {
287 if (array_key_exists('filters', $table)) {
288 foreach ($table['filters'] as $fieldName => $field) {
290 if ($fieldName == 'yid') {
291 $clause = "contribution_civireport.contact_id NOT IN
292 (SELECT distinct contri.contact_id FROM civicrm_contribution contri
293 WHERE contri.is_test = 0 AND " . self
::fiscalYearOffset('contri.receive_date') . " = $current_year) AND contribution_civireport.contact_id IN (SELECT distinct contri.contact_id FROM civicrm_contribution contri
294 WHERE " . self
::fiscalYearOffset('contri.receive_date') . " = $previous_year AND contri.is_test = 0)";
296 elseif (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
297 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
298 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
299 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
301 if ($relative ||
$from ||
$to) {
302 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
306 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
308 $clause = $this->whereClause($field,
310 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
311 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
312 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
314 if (($fieldName == 'contribution_status_id' ||
$fieldName == 'financial_type_id') && !empty($clause)) {
315 $this->_statusClause
.= " AND " . $clause;
320 if (!empty($clause)) {
321 $clauses[] = $clause;
327 $this->_where
= 'WHERE ' . implode(' AND ', $clauses);
329 if ($this->_aclWhere
) {
330 $this->_where
.= " AND {$this->_aclWhere} ";
335 $this->_groupBy
= "GROUP BY {$this->_aliases['civicrm_contribution']}.contact_id, " . self
::fiscalYearOffset($this->_aliases
['civicrm_contribution'] . '.receive_date') . " WITH ROLLUP";
336 $this->assign('chartSupported', TRUE);
339 function statistics(&$rows) {
340 $statistics = parent
::statistics($rows);
344 SUM({$this->_aliases['civicrm_contribution']}.total_amount ) as amount ";
346 $sql = "{$select} {$this->_from} {$this->_where}";
347 $dao = CRM_Core_DAO
::executeQuery($sql);
349 $statistics['counts']['amount'] = array(
350 'value' => $dao->amount
,
351 'title' => 'Total LifeTime',
352 'type' => CRM_Utils_Type
::T_MONEY
,
360 function postProcess() {
362 // get ready with post process params
363 $this->beginPostProcess();
365 // get the acl clauses built before we assemble the query
366 $this->buildACLClause($this->_aliases
['civicrm_contact']);
372 $rows = $contactIds = array();
373 if (empty($this->_params
['charts'])) {
375 $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}";
377 $dao = CRM_Core_DAO
::executeQuery($getContacts);
379 while ($dao->fetch()) {
380 $contactIds[] = $dao->cid
;
386 if (!empty($contactIds) ||
!empty($this->_params
['charts'])) {
387 if (!empty($this->_params
['charts'])) {
388 $sql = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy}";
391 $sql = "{$this->_select} {$this->_from} WHERE {$this->_aliases['civicrm_contact']}.id IN (" . implode(',', $contactIds) . ") AND {$this->_aliases['civicrm_contribution']}.is_test = 0 {$this->_statusClause} {$this->_groupBy} ";
394 $dao = CRM_Core_DAO
::executeQuery($sql);
395 $current_year = $this->_params
['yid_value'];
396 $previous_year = $current_year - 1;
398 while ($dao->fetch()) {
400 if (!$dao->civicrm_contribution_contact_id
) {
405 foreach ($this->_columnHeaders
as $key => $value) {
406 if (property_exists($dao, $key)) {
407 $rows[$dao->civicrm_contribution_contact_id
][$key] = $dao->$key;
411 if ($dao->civicrm_contribution_receive_date
) {
412 if ($dao->civicrm_contribution_receive_date
== $previous_year) {
413 $rows[$dao->civicrm_contribution_contact_id
][$dao->civicrm_contribution_receive_date
] = $dao->civicrm_contribution_total_amount
;
417 $rows[$dao->civicrm_contribution_contact_id
]['civicrm_life_time_total'] = $dao->civicrm_contribution_total_amount
;
423 $this->formatDisplay($rows, FALSE);
425 // assign variables to templates
426 $this->doTemplateAssignment($rows);
428 // do print / pdf / instance stuff if needed
429 $this->endPostProcess($rows);
432 function buildChart(&$rows) {
434 $graphRows = array();
438 $current_year = $this->_params
['yid_value'];
439 $previous_year = $current_year - 1;
440 $interval[$previous_year] = $previous_year;
441 $interval['life_time'] = 'Life Time';
443 foreach ($rows as $key => $row) {
444 $display['life_time'] = CRM_Utils_Array
::value('life_time', $display) +
$row['civicrm_life_time_total'];
445 $display[$previous_year] = CRM_Utils_Array
::value($previous_year, $display) +
$row[$previous_year];
448 $config = CRM_Core_Config
::Singleton();
449 $graphRows['value'] = $display;
450 $chartInfo = array('legend' => ts('Lybunt Report'),
451 'xname' => ts('Year'),
452 'yname' => ts('Amount (%1)', array(1 => $config->defaultCurrency
)),
454 if ($this->_params
['charts']) {
456 CRM_Utils_OpenFlashChart
::reportChart($graphRows, $this->_params
['charts'], $interval, $chartInfo);
457 $this->assign('chartType', $this->_params
['charts']);
461 function alterDisplay(&$rows) {
462 // custom code to alter rows
465 foreach ($rows as $rowNum => $row) {
466 //Convert Display name into link
467 if (array_key_exists('civicrm_contact_sort_name', $row) &&
468 array_key_exists('civicrm_contribution_contact_id', $row)
470 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
471 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contribution_contact_id'],
472 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
474 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
475 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contribution Details for this Contact.");
479 // convert campaign_id to campaign title
480 if (array_key_exists('civicrm_contribution_campaign_id', $row)) {
481 if ($value = $row['civicrm_contribution_campaign_id']) {
482 $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns
[$value];
487 $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'contribute/detail', 'List all contribution(s)') ?
TRUE : $entryFound;
489 // skip looking further in rows, if first row itself doesn't
490 // have the column we need
497 // Override "This Year" $op options
498 function getOperationPair($type = "string", $fieldName = NULL) {
499 if ($fieldName == 'yid') {
500 return array('calendar' => ts('Is Calendar Year'), 'fiscal' => ts('Fiscal Year Starting'));
502 return parent
::getOperationPair($type, $fieldName);