5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.3 |
7 +--------------------------------------------------------------------+
8 | Copyright CiviCRM LLC (c) 2004-2013 |
9 +--------------------------------------------------------------------+
10 | This file is a part of CiviCRM. |
12 | CiviCRM is free software; you can copy, modify, and distribute it |
13 | under the terms of the GNU Affero General Public License |
14 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
16 | CiviCRM is distributed in the hope that it will be useful, but |
17 | WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
19 | See the GNU Affero General Public License for more details. |
21 | You should have received a copy of the GNU Affero General Public |
22 | License and the CiviCRM Licensing Exception along |
23 | with this program; if not, contact CiviCRM LLC |
24 | at info[AT]civicrm[DOT]org. If you have questions about the |
25 | GNU Affero General Public License or the licensing of CiviCRM, |
26 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
27 +--------------------------------------------------------------------+
33 * @copyright CiviCRM LLC (c) 2004-2013
37 class CRM_Report_Form_Contribute_Lybunt
extends CRM_Report_Form
{
40 protected $_charts = array(
42 'barChart' => 'Bar Chart',
43 'pieChart' => 'Pie Chart',
46 public $_drilldownReport = array('contribute/detail' => 'Link to Detail Report');
48 protected $lifeTime_from = NULL;
49 protected $lifeTime_where = NULL; 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('title' => ts('First Name'),
82 'last_name' => array('title' => ts('Last Name'),
88 array('title' => ts('Donor Name'),
95 'dao' => 'CRM_Core_DAO_Email',
96 'grouping' => 'contact-field',
100 array('title' => ts('Email'),
107 'dao' => 'CRM_Core_DAO_Phone',
108 'grouping' => 'contact-field',
112 array('title' => ts('Phone'),
117 'civicrm_contribution' =>
119 'dao' => 'CRM_Contribute_DAO_Contribution',
123 array('title' => ts('contactId'),
124 'no_display' => TRUE,
129 array('title' => ts('Total Amount'),
130 'no_display' => TRUE,
135 array('title' => ts('Year'),
136 'no_display' => TRUE,
145 'name' => 'receive_date',
146 'title' => ts('This Year'),
147 'operatorType' => CRM_Report_Form
::OP_SELECT
,
148 'options' => $optionYear,
149 'default' => date('Y'),
151 'financial_type_id' => array(
152 'title' => ts('Financial Type'),
153 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
154 'options' => CRM_Contribute_PseudoConstant
::financialType(),
156 'contribution_status_id' =>
157 array('title' => ts('Contribution Status'),
158 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
159 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
160 'default' => array('1'),
166 'dao' => 'CRM_Contact_DAO_GroupContact',
172 'name' => 'group_id',
173 'title' => ts('Group'),
174 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
176 'options' => CRM_Core_PseudoConstant
::group(),
182 // If we have a campaign, build out the relevant elements
183 if ($campaignEnabled && !empty($this->activeCampaigns
)) {
184 $this->_columns
['civicrm_contribution']['fields']['campaign_id'] = array(
185 'title' => ts('Campaign'),
186 'default' => 'false',
188 $this->_columns
['civicrm_contribution']['filters']['campaign_id'] = array('title' => ts('Campaign'),
189 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
190 'options' => $this->activeCampaigns
,
194 $this->_tagFilter
= TRUE;
195 parent
::__construct();
198 function preProcess() {
199 parent
::preProcess();
204 $this->_columnHeaders
= $select = array();
205 $current_year = $this->_params
['yid_value'];
206 $previous_year = $current_year - 1;
209 foreach ($this->_columns
as $tableName => $table) {
211 if (array_key_exists('fields', $table)) {
212 foreach ($table['fields'] as $fieldName => $field) {
214 if (CRM_Utils_Array
::value('required', $field) ||
215 CRM_Utils_Array
::value($fieldName, $this->_params
['fields'])
217 if ($fieldName == 'total_amount') {
218 $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}";
220 $this->_columnHeaders
["{$previous_year}"]['type'] = $field['type'];
221 $this->_columnHeaders
["{$previous_year}"]['title'] = $previous_year;
223 $this->_columnHeaders
["civicrm_life_time_total"]['type'] = $field['type'];
224 $this->_columnHeaders
["civicrm_life_time_total"]['title'] = 'LifeTime';;
226 elseif ($fieldName == 'receive_date') {
227 $select[] = self
::fiscalYearOffset($field['dbAlias']) . " as {$tableName}_{$fieldName} ";
230 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName} ";
231 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
232 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
235 if (CRM_Utils_Array
::value('no_display', $field)) {
236 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['no_display'] = TRUE;
243 $this->_select
= "SELECT " . implode(', ', $select) . " ";
249 FROM civicrm_contribution {$this->_aliases['civicrm_contribution']}
250 INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
251 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id
253 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
254 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND
255 {$this->_aliases['civicrm_email']}.is_primary = 1
256 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
257 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
258 {$this->_aliases['civicrm_phone']}.is_primary = 1 ";
262 $this->_statusClause
= "";
263 $clauses = array($this->_aliases
['civicrm_contribution'] . '.is_test = 0');
264 $current_year = $this->_params
['yid_value'];
265 $previous_year = $current_year - 1;
267 foreach ($this->_columns
as $tableName => $table) {
268 if (array_key_exists('filters', $table)) {
269 foreach ($table['filters'] as $fieldName => $field) {
271 if ($fieldName == 'yid') {
272 $clause = "contribution_civireport.contact_id NOT IN
273 (SELECT distinct contri.contact_id FROM civicrm_contribution contri
274 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
275 WHERE " . self
::fiscalYearOffset('contri.receive_date') . " = $previous_year AND contri.is_test = 0)";
277 elseif (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
278 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
279 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
280 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
282 if ($relative ||
$from ||
$to) {
283 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
287 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
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
)
295 if (($fieldName == 'contribution_status_id' ||
$fieldName == 'financial_type_id') && !empty($clause)) {
296 $this->_statusClause
.= " AND " . $clause;
301 if (!empty($clause)) {
302 $clauses[] = $clause;
308 $this->_where
= 'WHERE ' . implode(' AND ', $clauses);
310 if ($this->_aclWhere
) {
311 $this->_where
.= " AND {$this->_aclWhere} ";
316 $this->_groupBy
= "GROUP BY {$this->_aliases['civicrm_contribution']}.contact_id, " . self
::fiscalYearOffset($this->_aliases
['civicrm_contribution'] . '.receive_date') . " WITH ROLLUP";
317 $this->assign('chartSupported', TRUE);
320 function statistics(&$rows) {
321 $statistics = parent
::statistics($rows);
325 SUM({$this->_aliases['civicrm_contribution']}.total_amount ) as amount ";
327 $sql = "{$select} {$this->_from} {$this->_where}";
328 $dao = CRM_Core_DAO
::executeQuery($sql);
330 $statistics['counts']['amount'] = array(
331 'value' => $dao->amount
,
332 'title' => 'Total LifeTime',
333 'type' => CRM_Utils_Type
::T_MONEY
,
341 function postProcess() {
343 // get ready with post process params
344 $this->beginPostProcess();
346 // get the acl clauses built before we assemble the query
347 $this->buildACLClause($this->_aliases
['civicrm_contact']);
353 $rows = $contactIds = array();
354 if (!CRM_Utils_Array
::value('charts', $this->_params
)) {
356 $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}";
358 $dao = CRM_Core_DAO
::executeQuery($getContacts);
360 while ($dao->fetch()) {
361 $contactIds[] = $dao->cid
;
367 if (!empty($contactIds) || CRM_Utils_Array
::value('charts', $this->_params
)) {
368 if (CRM_Utils_Array
::value('charts', $this->_params
)) {
369 $sql = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy}";
372 $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} ";
375 $dao = CRM_Core_DAO
::executeQuery($sql);
376 $current_year = $this->_params
['yid_value'];
377 $previous_year = $current_year - 1;
379 while ($dao->fetch()) {
381 if (!$dao->civicrm_contribution_contact_id
) {
386 foreach ($this->_columnHeaders
as $key => $value) {
387 if (property_exists($dao, $key)) {
388 $rows[$dao->civicrm_contribution_contact_id
][$key] = $dao->$key;
392 if ($dao->civicrm_contribution_receive_date
) {
393 if ($dao->civicrm_contribution_receive_date
== $previous_year) {
394 $rows[$dao->civicrm_contribution_contact_id
][$dao->civicrm_contribution_receive_date
] = $dao->civicrm_contribution_total_amount
;
398 $rows[$dao->civicrm_contribution_contact_id
]['civicrm_life_time_total'] = $dao->civicrm_contribution_total_amount
;
404 $this->formatDisplay($rows, FALSE);
406 // assign variables to templates
407 $this->doTemplateAssignment($rows);
409 // do print / pdf / instance stuff if needed
410 $this->endPostProcess($rows);
413 function buildChart(&$rows) {
415 $graphRows = array();
419 $current_year = $this->_params
['yid_value'];
420 $previous_year = $current_year - 1;
421 $interval[$previous_year] = $previous_year;
422 $interval['life_time'] = 'Life Time';
424 foreach ($rows as $key => $row) {
425 $display['life_time'] = CRM_Utils_Array
::value('life_time', $display) +
$row['civicrm_life_time_total'];
426 $display[$previous_year] = CRM_Utils_Array
::value($previous_year, $display) +
$row[$previous_year];
429 $config = CRM_Core_Config
::Singleton();
430 $graphRows['value'] = $display;
431 $chartInfo = array('legend' => ts('Lybunt Report'),
432 'xname' => ts('Year'),
433 'yname' => ts('Amount (%1)', array(1 => $config->defaultCurrency
)),
435 if ($this->_params
['charts']) {
437 CRM_Utils_OpenFlashChart
::reportChart($graphRows, $this->_params
['charts'], $interval, $chartInfo);
438 $this->assign('chartType', $this->_params
['charts']);
442 function alterDisplay(&$rows) {
443 foreach ($rows as $rowNum => $row) {
444 //Convert Display name into link
445 if (array_key_exists('civicrm_contact_sort_name', $row) &&
446 array_key_exists('civicrm_contribution_contact_id', $row)
448 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
449 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contribution_contact_id'],
450 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
452 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
453 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contribution Details for this Contact.");
456 // convert campaign_id to campaign title
457 if (array_key_exists('civicrm_contribution_campaign_id', $row)) {
458 if ($value = $row['civicrm_contribution_campaign_id']) {
459 $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns
[$value];
466 // Override "This Year" $op options
467 static function getOperationPair($type = "string", $fieldName = NULL) {
468 if ($fieldName == 'yid') {
469 return array('calendar' => ts('Is Calendar Year'), 'fiscal' => ts('Fiscal Year Starting'));
471 return parent
::getOperationPair($type, $fieldName);