3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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;
53 public function __construct() {
56 $date = CRM_Core_SelectValues
::date('custom', NULL, $yearsInPast, $yearsInFuture);
57 $count = $date['maxYear'];
58 while ($date['minYear'] <= $count) {
59 $optionYear[$date['minYear']] = $date['minYear'];
63 // Check if CiviCampaign is a) enabled and b) has active campaigns
64 $config = CRM_Core_Config
::singleton();
65 $campaignEnabled = in_array("CiviCampaign", $config->enableComponents
);
66 if ($campaignEnabled) {
67 $getCampaigns = CRM_Campaign_BAO_Campaign
::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
68 $this->activeCampaigns
= $getCampaigns['campaigns'];
69 asort($this->activeCampaigns
);
72 $this->_columns
= array(
73 'civicrm_contact' => array(
74 'dao' => 'CRM_Contact_DAO_Contact',
75 'grouping' => 'contact-field',
78 'title' => ts('Donor Name'),
82 'first_name' => array(
83 'title' => ts('First Name'),
86 'title' => ts('Last Name'),
88 'contact_type' => array(
89 'title' => ts('Contact Type'),
91 'contact_sub_type' => array(
92 'title' => ts('Contact Subtype'),
97 'title' => ts('Donor Name'),
102 'civicrm_email' => array(
103 'dao' => 'CRM_Core_DAO_Email',
104 'grouping' => 'contact-field',
107 'title' => ts('Email'),
112 'civicrm_phone' => array(
113 'dao' => 'CRM_Core_DAO_Phone',
114 'grouping' => 'contact-field',
117 'title' => ts('Phone'),
123 $this->_columns +
= $this->addAddressFields();
124 $this->_columns +
= array(
125 'civicrm_contribution' => array(
126 'dao' => 'CRM_Contribute_DAO_Contribution',
128 'contact_id' => array(
129 'title' => ts('contactId'),
130 'no_display' => TRUE,
134 'total_amount' => array(
135 'title' => ts('Total Amount'),
136 'no_display' => TRUE,
140 'receive_date' => array(
141 'title' => ts('Year'),
142 'no_display' => TRUE,
149 'name' => 'receive_date',
150 'title' => ts('This Year'),
151 'operatorType' => CRM_Report_Form
::OP_SELECT
,
152 'options' => $optionYear,
153 'default' => date('Y'),
155 'financial_type_id' => array(
156 'title' => ts('Financial Type'),
157 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
158 'options' => CRM_Contribute_PseudoConstant
::financialType(),
160 'contribution_status_id' => array(
161 'title' => ts('Contribution Status'),
162 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
163 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
164 'default' => array('1'),
170 // If we have a campaign, build out the relevant elements
171 if ($campaignEnabled && !empty($this->activeCampaigns
)) {
172 $this->_columns
['civicrm_contribution']['fields']['campaign_id'] = array(
173 'title' => ts('Campaign'),
174 'default' => 'false',
176 $this->_columns
['civicrm_contribution']['filters']['campaign_id'] = array(
177 'title' => ts('Campaign'),
178 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
179 'options' => $this->activeCampaigns
,
183 $this->_groupFilter
= TRUE;
184 $this->_tagFilter
= TRUE;
185 parent
::__construct();
188 public function preProcess() {
189 parent
::preProcess();
192 public function select() {
194 $this->_columnHeaders
= $select = array();
195 $current_year = $this->_params
['yid_value'];
196 $previous_year = $current_year - 1;
198 foreach ($this->_columns
as $tableName => $table) {
200 if (array_key_exists('fields', $table)) {
201 foreach ($table['fields'] as $fieldName => $field) {
203 if (!empty($field['required']) ||
204 !empty($this->_params
['fields'][$fieldName])
206 if ($fieldName == 'total_amount') {
207 $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}";
209 $this->_columnHeaders
["{$previous_year}"]['type'] = $field['type'];
210 $this->_columnHeaders
["{$previous_year}"]['title'] = $previous_year;
212 $this->_columnHeaders
["civicrm_life_time_total"]['type'] = $field['type'];
213 $this->_columnHeaders
["civicrm_life_time_total"]['title'] = 'LifeTime';;
215 elseif ($fieldName == 'receive_date') {
216 $select[] = self
::fiscalYearOffset($field['dbAlias']) .
217 " as {$tableName}_{$fieldName} ";
220 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName} ";
221 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
222 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
225 if (!empty($field['no_display'])) {
226 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['no_display'] = TRUE;
233 $this->_select
= "SELECT " . implode(', ', $select) . " ";
236 public function from() {
239 FROM civicrm_contribution {$this->_aliases['civicrm_contribution']}
240 INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
241 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id
244 if ($this->isTableSelected('civicrm_email')) {
246 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
247 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id
248 AND {$this->_aliases['civicrm_email']}.is_primary = 1";
250 if ($this->isTableSelected('civicrm_phone')) {
252 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
253 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
254 {$this->_aliases['civicrm_phone']}.is_primary = 1";
256 $this->addAddressFromClause();
259 public function where() {
260 $this->_statusClause
= "";
261 $clauses = array($this->_aliases
['civicrm_contribution'] . '.is_test = 0');
262 $current_year = $this->_params
['yid_value'];
263 $previous_year = $current_year - 1;
265 foreach ($this->_columns
as $tableName => $table) {
266 if (array_key_exists('filters', $table)) {
267 foreach ($table['filters'] as $fieldName => $field) {
269 if ($fieldName == 'yid') {
270 $clause = "contribution_civireport.contact_id NOT IN
271 (SELECT distinct contri.contact_id FROM civicrm_contribution contri
272 WHERE contri.is_test = 0 AND " .
273 self
::fiscalYearOffset('contri.receive_date') . " = $current_year) AND contribution_civireport.contact_id IN (SELECT distinct contri.contact_id FROM civicrm_contribution contri
274 WHERE " . self
::fiscalYearOffset('contri.receive_date') .
275 " = $previous_year AND contri.is_test = 0)";
277 elseif (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
279 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
280 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
281 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
283 if ($relative ||
$from ||
$to) {
284 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
288 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
290 $clause = $this->whereClause($field,
292 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
293 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
294 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
296 if (($fieldName == 'contribution_status_id' ||
297 $fieldName == 'financial_type_id') && !empty($clause)
299 $this->_statusClause
.= " AND " . $clause;
304 if (!empty($clause)) {
305 $clauses[] = $clause;
311 $this->_where
= 'WHERE ' . implode(' AND ', $clauses);
313 if ($this->_aclWhere
) {
314 $this->_where
.= " AND {$this->_aclWhere} ";
318 public function groupBy() {
319 $this->_groupBy
= "GROUP BY {$this->_aliases['civicrm_contribution']}.contact_id, " .
320 self
::fiscalYearOffset($this->_aliases
['civicrm_contribution'] .
321 '.receive_date') . " WITH ROLLUP";
322 $this->assign('chartSupported', TRUE);
330 public function statistics(&$rows) {
331 $statistics = parent
::statistics($rows);
335 SUM({$this->_aliases['civicrm_contribution']}.total_amount ) as amount ";
337 $sql = "{$select} {$this->_from} {$this->_where}";
338 $dao = CRM_Core_DAO
::executeQuery($sql);
340 $statistics['counts']['amount'] = array(
341 'value' => $dao->amount
,
342 'title' => 'Total LifeTime',
343 'type' => CRM_Utils_Type
::T_MONEY
,
351 public function postProcess() {
353 // get ready with post process params
354 $this->beginPostProcess();
356 // get the acl clauses built before we assemble the query
357 $this->buildACLClause($this->_aliases
['civicrm_contact']);
363 $rows = $contactIds = array();
364 if (empty($this->_params
['charts'])) {
366 $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}";
368 $dao = CRM_Core_DAO
::executeQuery($getContacts);
370 while ($dao->fetch()) {
371 $contactIds[] = $dao->cid
;
377 if (!empty($contactIds) ||
!empty($this->_params
['charts'])) {
378 if (!empty($this->_params
['charts'])) {
379 $sql = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy}";
382 $sql = "{$this->_select} {$this->_from} WHERE {$this->_aliases['civicrm_contact']}.id IN (" .
383 implode(',', $contactIds) .
384 ") AND {$this->_aliases['civicrm_contribution']}.is_test = 0 {$this->_statusClause} {$this->_groupBy} ";
387 $dao = CRM_Core_DAO
::executeQuery($sql);
388 $current_year = $this->_params
['yid_value'];
389 $previous_year = $current_year - 1;
391 while ($dao->fetch()) {
393 if (!$dao->civicrm_contribution_contact_id
) {
398 foreach ($this->_columnHeaders
as $key => $value) {
399 if (property_exists($dao, $key)) {
400 $rows[$dao->civicrm_contribution_contact_id
][$key] = $dao->$key;
404 if ($dao->civicrm_contribution_receive_date
) {
405 if ($dao->civicrm_contribution_receive_date
== $previous_year) {
406 $rows[$dao->civicrm_contribution_contact_id
][$dao->civicrm_contribution_receive_date
] = $dao->civicrm_contribution_total_amount
;
410 $rows[$dao->civicrm_contribution_contact_id
]['civicrm_life_time_total'] = $dao->civicrm_contribution_total_amount
;
416 $this->formatDisplay($rows, FALSE);
418 // assign variables to templates
419 $this->doTemplateAssignment($rows);
421 // do print / pdf / instance stuff if needed
422 $this->endPostProcess($rows);
428 public function buildChart(&$rows) {
430 $graphRows = array();
434 $current_year = $this->_params
['yid_value'];
435 $previous_year = $current_year - 1;
436 $interval[$previous_year] = $previous_year;
437 $interval['life_time'] = 'Life Time';
439 foreach ($rows as $key => $row) {
440 $display['life_time'] = CRM_Utils_Array
::value('life_time', $display) +
441 $row['civicrm_life_time_total'];
442 $display[$previous_year] = CRM_Utils_Array
::value($previous_year, $display) +
$row[$previous_year];
445 $config = CRM_Core_Config
::Singleton();
446 $graphRows['value'] = $display;
448 'legend' => ts('Lybunt Report'),
449 'xname' => ts('Year'),
450 'yname' => ts('Amount (%1)', array(1 => $config->defaultCurrency
)),
452 if ($this->_params
['charts']) {
454 CRM_Utils_OpenFlashChart
::reportChart($graphRows, $this->_params
['charts'], $interval, $chartInfo);
455 $this->assign('chartType', $this->_params
['charts']);
462 public function alterDisplay(&$rows) {
463 // custom code to alter rows
466 foreach ($rows as $rowNum => $row) {
467 //Convert Display name into link
468 if (array_key_exists('civicrm_contact_sort_name', $row) &&
469 array_key_exists('civicrm_contribution_contact_id', $row)
471 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
472 'reset=1&force=1&id_op=eq&id_value=' .
473 $row['civicrm_contribution_contact_id'],
474 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
476 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
477 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contribution Details for this Contact.");
481 // convert campaign_id to campaign title
482 if (array_key_exists('civicrm_contribution_campaign_id', $row)) {
483 if ($value = $row['civicrm_contribution_campaign_id']) {
484 $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns
[$value];
489 $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'contribute/detail', 'List all contribution(s)') ?
TRUE : $entryFound;
491 // skip looking further in rows, if first row itself doesn't
492 // have the column we need
500 * Override "This Year" $op options
501 * @param string $type
502 * @param null $fieldName
506 public function getOperationPair($type = "string", $fieldName = NULL) {
507 if ($fieldName == 'yid') {
509 'calendar' => ts('Is Calendar Year'),
510 'fiscal' => ts('Fiscal Year Starting'),
513 return parent
::getOperationPair($type, $fieldName);