3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
35 class CRM_Report_Form_Contribute_Sybunt
extends CRM_Report_Form
{
37 protected $_charts = array(
39 'barChart' => 'Bar Chart',
40 'pieChart' => 'Pie Chart',
42 public $_drilldownReport = array('contribute/detail' => 'Link to Detail Report');
44 protected $_add2groupSupported = FALSE;
46 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(
77 'title' => ts('First Name'),
80 'title' => ts('Last Name'),
84 'title' => ts('Contact Type'),
88 'title' => ts('Contact SubType'),
94 array('title' => ts('Donor Name'),
101 'dao' => 'CRM_Core_DAO_Email',
102 'grouping' => 'contact-field',
106 array('title' => ts('Email'),
113 'dao' => 'CRM_Core_DAO_Phone',
114 'grouping' => 'contact-field',
118 array('title' => ts('Phone'),
124 +
$this->addAddressFields()
126 'civicrm_contribution' =>
128 'dao' => 'CRM_Contribute_DAO_Contribution',
132 array('title' => ts('contactId'),
133 'no_display' => TRUE,
138 array('title' => ts('Total Amount'),
139 'no_display' => TRUE,
144 array('title' => ts('Year'),
145 'no_display' => TRUE,
154 'name' => 'receive_date',
155 'title' => ts('This Year'),
156 'operatorType' => CRM_Report_Form
::OP_SELECT
,
157 'options' => $optionYear,
158 'default' => date('Y'),
160 'financial_type_id' =>
161 array('title' => ts('Financial Type'),
162 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
163 'options' => CRM_Contribute_PseudoConstant
::financialType(),
165 'contribution_status_id' =>
166 array('title' => ts('Contribution Status'),
167 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
168 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
169 'default' => array('1'),
175 'dao' => 'CRM_Contact_DAO_GroupContact',
181 'name' => 'group_id',
182 'title' => ts('Group'),
183 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
185 'options' => CRM_Core_PseudoConstant
::group(),
191 // If we have a campaign, build out the relevant elements
192 if ($campaignEnabled && !empty($this->activeCampaigns
)) {
193 $this->_columns
['civicrm_contribution']['fields']['campaign_id'] = array(
194 'title' => ts('Campaign'),
195 'default' => 'false',
197 $this->_columns
['civicrm_contribution']['filters']['campaign_id'] = array('title' => ts('Campaign'),
198 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
199 'options' => $this->activeCampaigns
,
203 $this->_tagFilter
= TRUE;
204 parent
::__construct();
207 function preProcess() {
208 parent
::preProcess();
213 $this->_columnHeaders
= array();
214 $current_year = $this->_params
['yid_value'];
215 $previous_year = $current_year - 1;
216 $previous_pyear = $current_year - 2;
217 $previous_ppyear = $current_year - 3;
218 $upTo_year = $current_year - 4;
220 foreach ($this->_columns
as $tableName => $table) {
221 if (array_key_exists('fields', $table)) {
222 foreach ($table['fields'] as $fieldName => $field) {
224 if (CRM_Utils_Array
::value('required', $field) ||
225 CRM_Utils_Array
::value($fieldName, $this->_params
['fields'])
227 if ($fieldName == 'total_amount') {
228 $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}";
230 $this->_columnHeaders
["civicrm_upto_{$upTo_year}"]['type'] = $field['type'];
231 $this->_columnHeaders
["civicrm_upto_{$upTo_year}"]['title'] = "Up To $upTo_year";
233 $this->_columnHeaders
["{$previous_ppyear}"]['type'] = $field['type'];
234 $this->_columnHeaders
["{$previous_ppyear}"]['title'] = $previous_ppyear;
236 $this->_columnHeaders
["{$previous_pyear}"]['type'] = $field['type'];
237 $this->_columnHeaders
["{$previous_pyear}"]['title'] = $previous_pyear;
239 $this->_columnHeaders
["{$previous_year}"]['type'] = $field['type'];
240 $this->_columnHeaders
["{$previous_year}"]['title'] = $previous_year;
242 $this->_columnHeaders
["civicrm_life_time_total"]['type'] = $field['type'];
243 $this->_columnHeaders
["civicrm_life_time_total"]['title'] = 'LifeTime';;
245 elseif ($fieldName == 'receive_date') {
246 $select[] = self
::fiscalYearOffset($field['dbAlias']) . " as {$tableName}_{$fieldName}";
249 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
250 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
251 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
253 if (CRM_Utils_Array
::value('no_display', $field)) {
254 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['no_display'] = TRUE;
261 $this->_select
= "SELECT " . implode(', ', $select) . " ";
267 FROM civicrm_contribution {$this->_aliases['civicrm_contribution']}
268 INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
269 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id
272 if ($this->isTableSelected('civicrm_email')) {
274 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
275 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id
276 AND {$this->_aliases['civicrm_email']}.is_primary = 1";
278 if ($this->isTableSelected('civicrm_phone')) {
280 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
281 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
282 {$this->_aliases['civicrm_phone']}.is_primary = 1";
284 $this->addAddressFromClause();
288 $this->_statusClause
= "";
289 $clauses = array($this->_aliases
['civicrm_contribution'] . '.is_test = 0');
290 foreach ($this->_columns
as $tableName => $table) {
291 if (array_key_exists('filters', $table)) {
292 foreach ($table['filters'] as $fieldName => $field) {
294 if ($fieldName == 'yid') {
295 $clause = "contribution_civireport.contact_id NOT IN
296 (SELECT distinct cont.id FROM civicrm_contact cont, civicrm_contribution contri
297 WHERE cont.id = contri.contact_id AND " . self
::fiscalYearOffset('contri.receive_date') . " = {$this->_params['yid_value']} AND contri.is_test = 0 )";
299 elseif (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
300 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
301 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
302 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
304 if ($relative ||
$from ||
$to) {
305 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
309 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
311 $clause = $this->whereClause($field,
313 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
314 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
315 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
317 if (($fieldName == 'contribution_status_id' ||
$fieldName == 'financial_type_id') && !empty($clause)) {
318 $this->_statusClause
.= " AND " . $clause;
323 if (!empty($clause)) {
324 $clauses[] = $clause;
330 $this->_where
= "WHERE " . implode(' AND ', $clauses);
332 if ($this->_aclWhere
) {
333 $this->_where
.= " AND {$this->_aclWhere} ";
338 $this->assign('chartSupported', TRUE);
339 $this->_groupBy
= "Group BY {$this->_aliases['civicrm_contribution']}.contact_id, " . self
::fiscalYearOffset($this->_aliases
['civicrm_contribution'] . '.receive_date') . " WITH ROLLUP ";
342 function statistics(&$rows) {
343 $statistics = parent
::statistics($rows);
348 SUM({$this->_aliases['civicrm_contribution']}.total_amount ) as amount ";
350 $sql = "{$select} {$this->_from} {$this->_where}";
351 $dao = CRM_Core_DAO
::executeQuery($sql);
353 $statistics['counts']['amount'] = array(
354 'value' => $dao->amount
,
355 'title' => 'Total LifeTime',
356 'type' => CRM_Utils_Type
::T_MONEY
,
363 function postProcess() {
364 // get ready with post process params
365 $this->beginPostProcess();
367 $this->buildACLClause($this->_aliases
['civicrm_contact']);
373 $rows = $contactIds = array();
374 if (!CRM_Utils_Array
::value('charts', $this->_params
)) {
376 $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}";
378 $dao = CRM_Core_DAO
::executeQuery($getContacts);
380 while ($dao->fetch()) {
381 $contactIds[] = $dao->cid
;
387 if (!empty($contactIds) || CRM_Utils_Array
::value('charts', $this->_params
)) {
388 if (CRM_Utils_Array
::value('charts', $this->_params
)) {
389 $sql = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy}";
392 $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} ";
395 $current_year = $this->_params
['yid_value'];
396 $previous_year = $current_year - 1;
397 $previous_pyear = $current_year - 2;
398 $previous_ppyear = $current_year - 3;
399 $upTo_year = $current_year - 4;
401 $rows = $row = array();
402 $dao = CRM_Core_DAO
::executeQuery($sql);
403 $contributionSum = 0;
405 while ($dao->fetch()) {
406 if (!$dao->civicrm_contribution_contact_id
) {
410 foreach ($this->_columnHeaders
as $key => $value) {
411 if (property_exists($dao, $key)) {
412 $rows[$dao->civicrm_contribution_contact_id
][$key] = $dao->$key;
415 if ($dao->civicrm_contribution_receive_date
) {
416 if ($dao->civicrm_contribution_receive_date
> $upTo_year) {
417 $contributionSum +
= $dao->civicrm_contribution_total_amount
;
418 $rows[$dao->civicrm_contribution_contact_id
][$dao->civicrm_contribution_receive_date
] = $dao->civicrm_contribution_total_amount
;
422 $rows[$dao->civicrm_contribution_contact_id
]['civicrm_life_time_total'] = $dao->civicrm_contribution_total_amount
;
423 if (($dao->civicrm_contribution_total_amount
- $contributionSum) > 0) {
424 $rows[$dao->civicrm_contribution_contact_id
]["civicrm_upto_{$upTo_year}"] = $dao->civicrm_contribution_total_amount
- $contributionSum;
426 $contributionSum = 0;
431 // format result set.
432 $this->formatDisplay($rows, FALSE);
434 // assign variables to templates
435 $this->doTemplateAssignment($rows);
437 // do print / pdf / instance stuff if needed
438 $this->endPostProcess($rows);
441 function buildChart(&$rows) {
442 $graphRows = array();
444 $current_year = $this->_params
['yid_value'];
445 $previous_year = $current_year - 1;
446 $previous_two_year = $current_year - 2;
447 $previous_three_year = $current_year - 3;
448 $upto = $current_year - 4;
450 $interval[$previous_year] = $previous_year;
451 $interval[$previous_two_year] = $previous_two_year;
452 $interval[$previous_three_year] = $previous_three_year;
453 $interval["upto_{$upto}"] = "Up To {$upto}";
455 foreach ($rows as $key => $row) {
456 $display["upto_{$upto}"] = CRM_Utils_Array
::value("upto_{$upto}", $display) + CRM_Utils_Array
::value("civicrm_upto_{$upto}", $row);
457 $display[$previous_year] = CRM_Utils_Array
::value($previous_year, $display) + CRM_Utils_Array
::value($previous_year, $row);
458 $display[$previous_two_year] = CRM_Utils_Array
::value($previous_two_year, $display) + CRM_Utils_Array
::value($previous_two_year, $row);
459 $display[$previous_three_year] = CRM_Utils_Array
::value($previous_three_year, $display) + CRM_Utils_Array
::value($previous_three_year, $row);
462 $graphRows['value'] = $display;
463 $config = CRM_Core_Config
::Singleton();
465 'legend' => 'Sybunt Report',
467 'yname' => "Amount ({$config->defaultCurrency})",
469 if ($this->_params
['charts']) {
471 CRM_Utils_OpenFlashChart
::reportChart($graphRows, $this->_params
['charts'], $interval, $chartInfo);
472 $this->assign('chartType', $this->_params
['charts']);
476 function alterDisplay(&$rows) {
477 // custom code to alter rows
480 foreach ($rows as $rowNum => $row) {
481 //Convert Display name into link
482 if (array_key_exists('civicrm_contact_sort_name', $row) &&
483 array_key_exists('civicrm_contribution_contact_id', $row)
485 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
486 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contribution_contact_id'],
487 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
489 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
490 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contribution Details for this Contact.");
494 // convert campaign_id to campaign title
495 if (array_key_exists('civicrm_contribution_campaign_id', $row)) {
496 if ($value = $row['civicrm_contribution_campaign_id']) {
497 $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns
[$value];
502 $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'contribute/detail', 'List all contribution(s)') ?
TRUE : $entryFound;
504 // skip looking further in rows, if first row itself doesn't
505 // have the column we need
512 // Override "This Year" $op options
513 function getOperationPair($type = "string", $fieldName = NULL) {
514 if ($fieldName == 'yid') {
515 return array('calendar' => ts('Is Calendar Year'), 'fiscal' => ts('Fiscal Year Starting'));
517 return parent
::getOperationPair($type, $fieldName);