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_Sybunt
extends CRM_Report_Form
{
39 protected $_charts = array(
41 'barChart' => 'Bar Chart',
42 'pieChart' => 'Pie Chart',
44 public $_drilldownReport = array('contribute/detail' => 'Link to Detail Report');
46 protected $_add2groupSupported = FALSE; function __construct() {
49 $date = CRM_Core_SelectValues
::date('custom', NULL, $yearsInPast, $yearsInFuture);
50 $count = $date['maxYear'];
51 while ($date['minYear'] <= $count) {
52 $optionYear[$date['minYear']] = $date['minYear'];
56 // Check if CiviCampaign is a) enabled and b) has active campaigns
57 $config = CRM_Core_Config
::singleton();
58 $campaignEnabled = in_array("CiviCampaign", $config->enableComponents
);
59 if ($campaignEnabled) {
60 $getCampaigns = CRM_Campaign_BAO_Campaign
::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
61 $this->activeCampaigns
= $getCampaigns['campaigns'];
62 asort($this->activeCampaigns
);
65 $this->_columns
= array(
68 'dao' => 'CRM_Contact_DAO_Contact',
69 'grouping' => 'contact-field',
73 array('title' => ts('Donor Name'),
76 'first_name' => array('title' => ts('First Name'),
78 'last_name' => array('title' => ts('Last Name'),
84 array('title' => ts('Donor Name'),
91 'dao' => 'CRM_Core_DAO_Email',
92 'grouping' => 'contact-field',
96 array('title' => ts('Email'),
103 'dao' => 'CRM_Core_DAO_Phone',
104 'grouping' => 'contact-field',
108 array('title' => ts('Phone'),
113 'civicrm_contribution' =>
115 'dao' => 'CRM_Contribute_DAO_Contribution',
119 array('title' => ts('contactId'),
120 'no_display' => TRUE,
125 array('title' => ts('Total Amount'),
126 'no_display' => TRUE,
131 array('title' => ts('Year'),
132 'no_display' => TRUE,
141 'name' => 'receive_date',
142 'title' => ts('This Year'),
143 'operatorType' => CRM_Report_Form
::OP_SELECT
,
144 'options' => $optionYear,
145 'default' => date('Y'),
147 'financial_type_id' =>
148 array('title' => ts('Financial Type'),
149 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
150 'options' => CRM_Contribute_PseudoConstant
::financialType(),
152 'contribution_status_id' =>
153 array('title' => ts('Contribution Status'),
154 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
155 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
156 'default' => array('1'),
162 'dao' => 'CRM_Contact_DAO_GroupContact',
168 'name' => 'group_id',
169 'title' => ts('Group'),
170 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
172 'options' => CRM_Core_PseudoConstant
::group(),
178 // If we have a campaign, build out the relevant elements
179 if ($campaignEnabled && !empty($this->activeCampaigns
)) {
180 $this->_columns
['civicrm_contribution']['fields']['campaign_id'] = array(
181 'title' => ts('Campaign'),
182 'default' => 'false',
184 $this->_columns
['civicrm_contribution']['filters']['campaign_id'] = array('title' => ts('Campaign'),
185 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
186 'options' => $this->activeCampaigns
,
190 $this->_tagFilter
= TRUE;
191 parent
::__construct();
194 function preProcess() {
195 parent
::preProcess();
200 $this->_columnHeaders
= array();
201 $current_year = $this->_params
['yid_value'];
202 $previous_year = $current_year - 1;
203 $previous_pyear = $current_year - 2;
204 $previous_ppyear = $current_year - 3;
205 $upTo_year = $current_year - 4;
207 foreach ($this->_columns
as $tableName => $table) {
208 if (array_key_exists('fields', $table)) {
209 foreach ($table['fields'] as $fieldName => $field) {
211 if (CRM_Utils_Array
::value('required', $field) ||
212 CRM_Utils_Array
::value($fieldName, $this->_params
['fields'])
214 if ($fieldName == 'total_amount') {
215 $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}";
217 $this->_columnHeaders
["civicrm_upto_{$upTo_year}"]['type'] = $field['type'];
218 $this->_columnHeaders
["civicrm_upto_{$upTo_year}"]['title'] = "Up To $upTo_year";
220 $this->_columnHeaders
["{$previous_ppyear}"]['type'] = $field['type'];
221 $this->_columnHeaders
["{$previous_ppyear}"]['title'] = $previous_ppyear;
223 $this->_columnHeaders
["{$previous_pyear}"]['type'] = $field['type'];
224 $this->_columnHeaders
["{$previous_pyear}"]['title'] = $previous_pyear;
226 $this->_columnHeaders
["{$previous_year}"]['type'] = $field['type'];
227 $this->_columnHeaders
["{$previous_year}"]['title'] = $previous_year;
229 $this->_columnHeaders
["civicrm_life_time_total"]['type'] = $field['type'];
230 $this->_columnHeaders
["civicrm_life_time_total"]['title'] = 'LifeTime';;
232 elseif ($fieldName == 'receive_date') {
233 $select[] = self
::fiscalYearOffset($field['dbAlias']) . " as {$tableName}_{$fieldName}";
236 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
237 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
238 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
240 if (CRM_Utils_Array
::value('no_display', $field)) {
241 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['no_display'] = TRUE;
248 $this->_select
= "SELECT " . implode(', ', $select) . " ";
254 FROM civicrm_contribution {$this->_aliases['civicrm_contribution']}
255 INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
256 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id
258 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
259 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id
260 AND {$this->_aliases['civicrm_email']}.is_primary = 1
261 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
262 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
263 {$this->_aliases['civicrm_phone']}.is_primary = 1 ";
267 $this->_statusClause
= "";
268 $clauses = array($this->_aliases
['civicrm_contribution'] . '.is_test = 0');
269 foreach ($this->_columns
as $tableName => $table) {
270 if (array_key_exists('filters', $table)) {
271 foreach ($table['filters'] as $fieldName => $field) {
273 if ($fieldName == 'yid') {
274 $clause = "contribution_civireport.contact_id NOT IN
275 (SELECT distinct cont.id FROM civicrm_contact cont, civicrm_contribution contri
276 WHERE cont.id = contri.contact_id AND " . self
::fiscalYearOffset('contri.receive_date') . " = {$this->_params['yid_value']} AND contri.is_test = 0 )";
278 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' ||
$fieldName == 'financial_type_id') && !empty($clause)) {
297 $this->_statusClause
.= " AND " . $clause;
302 if (!empty($clause)) {
303 $clauses[] = $clause;
309 $this->_where
= "WHERE " . implode(' AND ', $clauses);
311 if ($this->_aclWhere
) {
312 $this->_where
.= " AND {$this->_aclWhere} ";
317 $this->assign('chartSupported', TRUE);
318 $this->_groupBy
= "Group BY {$this->_aliases['civicrm_contribution']}.contact_id, " . self
::fiscalYearOffset($this->_aliases
['civicrm_contribution'] . '.receive_date') . " WITH ROLLUP ";
321 function statistics(&$rows) {
322 $statistics = parent
::statistics($rows);
327 SUM({$this->_aliases['civicrm_contribution']}.total_amount ) as amount ";
329 $sql = "{$select} {$this->_from} {$this->_where}";
330 $dao = CRM_Core_DAO
::executeQuery($sql);
332 $statistics['counts']['amount'] = array(
333 'value' => $dao->amount
,
334 'title' => 'Total LifeTime',
335 'type' => CRM_Utils_Type
::T_MONEY
,
342 function postProcess() {
343 // get ready with post process params
344 $this->beginPostProcess();
346 $this->buildACLClause($this->_aliases
['civicrm_contact']);
352 $rows = $contactIds = array();
353 if (!CRM_Utils_Array
::value('charts', $this->_params
)) {
355 $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}";
357 $dao = CRM_Core_DAO
::executeQuery($getContacts);
359 while ($dao->fetch()) {
360 $contactIds[] = $dao->cid
;
366 if (!empty($contactIds) || CRM_Utils_Array
::value('charts', $this->_params
)) {
367 if (CRM_Utils_Array
::value('charts', $this->_params
)) {
368 $sql = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy}";
371 $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} ";
374 $current_year = $this->_params
['yid_value'];
375 $previous_year = $current_year - 1;
376 $previous_pyear = $current_year - 2;
377 $previous_ppyear = $current_year - 3;
378 $upTo_year = $current_year - 4;
380 $rows = $row = array();
381 $dao = CRM_Core_DAO
::executeQuery($sql);
382 $contributionSum = 0;
384 while ($dao->fetch()) {
385 if (!$dao->civicrm_contribution_contact_id
) {
389 foreach ($this->_columnHeaders
as $key => $value) {
390 if (property_exists($dao, $key)) {
391 $rows[$dao->civicrm_contribution_contact_id
][$key] = $dao->$key;
394 if ($dao->civicrm_contribution_receive_date
) {
395 if ($dao->civicrm_contribution_receive_date
> $upTo_year) {
396 $contributionSum +
= $dao->civicrm_contribution_total_amount
;
397 $rows[$dao->civicrm_contribution_contact_id
][$dao->civicrm_contribution_receive_date
] = $dao->civicrm_contribution_total_amount
;
401 $rows[$dao->civicrm_contribution_contact_id
]['civicrm_life_time_total'] = $dao->civicrm_contribution_total_amount
;
402 if (($dao->civicrm_contribution_total_amount
- $contributionSum) > 0) {
403 $rows[$dao->civicrm_contribution_contact_id
]["civicrm_upto_{$upTo_year}"] = $dao->civicrm_contribution_total_amount
- $contributionSum;
405 $contributionSum = 0;
410 // format result set.
411 $this->formatDisplay($rows, FALSE);
413 // assign variables to templates
414 $this->doTemplateAssignment($rows);
416 // do print / pdf / instance stuff if needed
417 $this->endPostProcess($rows);
420 function buildChart(&$rows) {
421 $graphRows = array();
423 $current_year = $this->_params
['yid_value'];
424 $previous_year = $current_year - 1;
425 $previous_two_year = $current_year - 2;
426 $previous_three_year = $current_year - 3;
427 $upto = $current_year - 4;
429 $interval[$previous_year] = $previous_year;
430 $interval[$previous_two_year] = $previous_two_year;
431 $interval[$previous_three_year] = $previous_three_year;
432 $interval["upto_{$upto}"] = "Up To {$upto}";
434 foreach ($rows as $key => $row) {
435 $display["upto_{$upto}"] = CRM_Utils_Array
::value("upto_{$upto}", $display) + CRM_Utils_Array
::value("civicrm_upto_{$upto}", $row);
436 $display[$previous_year] = CRM_Utils_Array
::value($previous_year, $display) + CRM_Utils_Array
::value($previous_year, $row);
437 $display[$previous_two_year] = CRM_Utils_Array
::value($previous_two_year, $display) + CRM_Utils_Array
::value($previous_two_year, $row);
438 $display[$previous_three_year] = CRM_Utils_Array
::value($previous_three_year, $display) + CRM_Utils_Array
::value($previous_three_year, $row);
441 $graphRows['value'] = $display;
442 $config = CRM_Core_Config
::Singleton();
444 'legend' => 'Sybunt Report',
446 'yname' => "Amount ({$config->defaultCurrency})",
448 if ($this->_params
['charts']) {
450 CRM_Utils_OpenFlashChart
::reportChart($graphRows, $this->_params
['charts'], $interval, $chartInfo);
451 $this->assign('chartType', $this->_params
['charts']);
455 function alterDisplay(&$rows) {
457 foreach ($rows as $rowNum => $row) {
458 //Convert Display name into link
459 if (array_key_exists('civicrm_contact_sort_name', $row) &&
460 array_key_exists('civicrm_contribution_contact_id', $row)
462 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
463 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contribution_contact_id'],
464 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
466 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
467 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contribution Details for this Contact.");
470 // convert campaign_id to campaign title
471 if (array_key_exists('civicrm_contribution_campaign_id', $row)) {
472 if ($value = $row['civicrm_contribution_campaign_id']) {
473 $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns
[$value];
480 // Override "This Year" $op options
481 function getOperationPair($type = "string", $fieldName = NULL) {
482 if ($fieldName == 'yid') {
483 return array('calendar' => ts('Is Calendar Year'), 'fiscal' => ts('Fiscal Year Starting'));
485 return parent
::getOperationPair($type, $fieldName);