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_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;
52 function __construct() {
55 $date = CRM_Core_SelectValues
::date('custom', NULL, $yearsInPast, $yearsInFuture);
56 $count = $date['maxYear'];
57 while ($date['minYear'] <= $count) {
58 $optionYear[$date['minYear']] = $date['minYear'];
62 // Check if CiviCampaign is a) enabled and b) has active campaigns
63 $config = CRM_Core_Config
::singleton();
64 $campaignEnabled = in_array("CiviCampaign", $config->enableComponents
);
65 if ($campaignEnabled) {
66 $getCampaigns = CRM_Campaign_BAO_Campaign
::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
67 $this->activeCampaigns
= $getCampaigns['campaigns'];
68 asort($this->activeCampaigns
);
71 $this->_columns
= array(
74 'dao' => 'CRM_Contact_DAO_Contact',
75 'grouping' => 'contact-field',
79 array('title' => ts('Donor Name'),
82 'first_name' => array(
83 'title' => ts('First Name'),
86 'title' => ts('Last Name'),
90 'title' => ts('Contact Type'),
94 'title' => ts('Contact SubType'),
100 array('title' => ts('Donor Name'),
101 'operator' => 'like',
107 'dao' => 'CRM_Core_DAO_Email',
108 'grouping' => 'contact-field',
112 array('title' => ts('Email'),
119 'dao' => 'CRM_Core_DAO_Phone',
120 'grouping' => 'contact-field',
124 array('title' => ts('Phone'),
130 +
$this->addAddressFields()
132 'civicrm_contribution' =>
134 'dao' => 'CRM_Contribute_DAO_Contribution',
138 array('title' => ts('contactId'),
139 'no_display' => TRUE,
144 array('title' => ts('Total Amount'),
145 'no_display' => TRUE,
150 array('title' => ts('Year'),
151 'no_display' => TRUE,
160 'name' => 'receive_date',
161 'title' => ts('This Year'),
162 'operatorType' => CRM_Report_Form
::OP_SELECT
,
163 'options' => $optionYear,
164 'default' => date('Y'),
166 'financial_type_id' =>
167 array('title' => ts('Financial Type'),
168 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
169 'options' => CRM_Contribute_PseudoConstant
::financialType(),
171 'contribution_status_id' =>
172 array('title' => ts('Contribution Status'),
173 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
174 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
175 'default' => array('1'),
181 // If we have a campaign, build out the relevant elements
182 if ($campaignEnabled && !empty($this->activeCampaigns
)) {
183 $this->_columns
['civicrm_contribution']['fields']['campaign_id'] = array(
184 'title' => ts('Campaign'),
185 'default' => 'false',
187 $this->_columns
['civicrm_contribution']['filters']['campaign_id'] = array('title' => ts('Campaign'),
188 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
189 'options' => $this->activeCampaigns
,
193 $this->_groupFilter
= TRUE;
194 $this->_tagFilter
= TRUE;
195 parent
::__construct();
198 function preProcess() {
199 parent
::preProcess();
204 $this->_columnHeaders
= array();
205 $current_year = $this->_params
['yid_value'];
206 $previous_year = $current_year - 1;
207 $previous_pyear = $current_year - 2;
208 $previous_ppyear = $current_year - 3;
209 $upTo_year = $current_year - 4;
211 foreach ($this->_columns
as $tableName => $table) {
212 if (array_key_exists('fields', $table)) {
213 foreach ($table['fields'] as $fieldName => $field) {
215 if (!empty($field['required']) ||
!empty($this->_params
['fields'][$fieldName])) {
216 if ($fieldName == 'total_amount') {
217 $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}";
219 $this->_columnHeaders
["civicrm_upto_{$upTo_year}"]['type'] = $field['type'];
220 $this->_columnHeaders
["civicrm_upto_{$upTo_year}"]['title'] = "Up To $upTo_year";
222 $this->_columnHeaders
["{$previous_ppyear}"]['type'] = $field['type'];
223 $this->_columnHeaders
["{$previous_ppyear}"]['title'] = $previous_ppyear;
225 $this->_columnHeaders
["{$previous_pyear}"]['type'] = $field['type'];
226 $this->_columnHeaders
["{$previous_pyear}"]['title'] = $previous_pyear;
228 $this->_columnHeaders
["{$previous_year}"]['type'] = $field['type'];
229 $this->_columnHeaders
["{$previous_year}"]['title'] = $previous_year;
231 $this->_columnHeaders
["civicrm_life_time_total"]['type'] = $field['type'];
232 $this->_columnHeaders
["civicrm_life_time_total"]['title'] = 'LifeTime';;
234 elseif ($fieldName == 'receive_date') {
235 $select[] = self
::fiscalYearOffset($field['dbAlias']) . " as {$tableName}_{$fieldName}";
238 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
239 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
240 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
242 if (!empty($field['no_display'])) {
243 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['no_display'] = TRUE;
250 $this->_select
= "SELECT " . implode(', ', $select) . " ";
256 FROM civicrm_contribution {$this->_aliases['civicrm_contribution']}
257 INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
258 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id
261 if ($this->isTableSelected('civicrm_email')) {
263 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
264 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id
265 AND {$this->_aliases['civicrm_email']}.is_primary = 1";
267 if ($this->isTableSelected('civicrm_phone')) {
269 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
270 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
271 {$this->_aliases['civicrm_phone']}.is_primary = 1";
273 $this->addAddressFromClause();
277 $this->_statusClause
= "";
278 $clauses = array($this->_aliases
['civicrm_contribution'] . '.is_test = 0');
279 foreach ($this->_columns
as $tableName => $table) {
280 if (array_key_exists('filters', $table)) {
281 foreach ($table['filters'] as $fieldName => $field) {
283 if ($fieldName == 'yid') {
284 $clause = "contribution_civireport.contact_id NOT IN
285 (SELECT distinct cont.id FROM civicrm_contact cont, civicrm_contribution contri
286 WHERE cont.id = contri.contact_id AND " . self
::fiscalYearOffset('contri.receive_date') . " = {$this->_params['yid_value']} AND contri.is_test = 0 )";
288 elseif (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
289 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
290 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
291 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
293 if ($relative ||
$from ||
$to) {
294 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
298 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
300 $clause = $this->whereClause($field,
302 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
303 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
304 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
306 if (($fieldName == 'contribution_status_id' ||
$fieldName == 'financial_type_id') && !empty($clause)) {
307 $this->_statusClause
.= " AND " . $clause;
312 if (!empty($clause)) {
313 $clauses[] = $clause;
319 $this->_where
= "WHERE " . implode(' AND ', $clauses);
321 if ($this->_aclWhere
) {
322 $this->_where
.= " AND {$this->_aclWhere} ";
327 $this->assign('chartSupported', TRUE);
328 $this->_groupBy
= "Group BY {$this->_aliases['civicrm_contribution']}.contact_id, " . self
::fiscalYearOffset($this->_aliases
['civicrm_contribution'] . '.receive_date') . " WITH ROLLUP ";
336 function statistics(&$rows) {
337 $statistics = parent
::statistics($rows);
342 SUM({$this->_aliases['civicrm_contribution']}.total_amount ) as amount ";
344 $sql = "{$select} {$this->_from} {$this->_where}";
345 $dao = CRM_Core_DAO
::executeQuery($sql);
347 $statistics['counts']['amount'] = array(
348 'value' => $dao->amount
,
349 'title' => 'Total LifeTime',
350 'type' => CRM_Utils_Type
::T_MONEY
,
357 function postProcess() {
358 // get ready with post process params
359 $this->beginPostProcess();
361 $this->buildACLClause($this->_aliases
['civicrm_contact']);
367 $rows = $contactIds = array();
368 if (empty($this->_params
['charts'])) {
370 $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}";
372 $dao = CRM_Core_DAO
::executeQuery($getContacts);
374 while ($dao->fetch()) {
375 $contactIds[] = $dao->cid
;
381 if (!empty($contactIds) ||
!empty($this->_params
['charts'])) {
382 if (!empty($this->_params
['charts'])) {
383 $sql = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy}";
386 $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} ";
389 $current_year = $this->_params
['yid_value'];
390 $previous_year = $current_year - 1;
391 $previous_pyear = $current_year - 2;
392 $previous_ppyear = $current_year - 3;
393 $upTo_year = $current_year - 4;
395 $rows = $row = array();
396 $dao = CRM_Core_DAO
::executeQuery($sql);
397 $contributionSum = 0;
399 while ($dao->fetch()) {
400 if (!$dao->civicrm_contribution_contact_id
) {
404 foreach ($this->_columnHeaders
as $key => $value) {
405 if (property_exists($dao, $key)) {
406 $rows[$dao->civicrm_contribution_contact_id
][$key] = $dao->$key;
409 if ($dao->civicrm_contribution_receive_date
) {
410 if ($dao->civicrm_contribution_receive_date
> $upTo_year) {
411 $contributionSum +
= $dao->civicrm_contribution_total_amount
;
412 $rows[$dao->civicrm_contribution_contact_id
][$dao->civicrm_contribution_receive_date
] = $dao->civicrm_contribution_total_amount
;
416 $rows[$dao->civicrm_contribution_contact_id
]['civicrm_life_time_total'] = $dao->civicrm_contribution_total_amount
;
417 if (($dao->civicrm_contribution_total_amount
- $contributionSum) > 0) {
418 $rows[$dao->civicrm_contribution_contact_id
]["civicrm_upto_{$upTo_year}"] = $dao->civicrm_contribution_total_amount
- $contributionSum;
420 $contributionSum = 0;
425 // format result set.
426 $this->formatDisplay($rows, FALSE);
428 // assign variables to templates
429 $this->doTemplateAssignment($rows);
431 // do print / pdf / instance stuff if needed
432 $this->endPostProcess($rows);
438 function buildChart(&$rows) {
439 $graphRows = array();
441 $current_year = $this->_params
['yid_value'];
442 $previous_year = $current_year - 1;
443 $previous_two_year = $current_year - 2;
444 $previous_three_year = $current_year - 3;
445 $upto = $current_year - 4;
447 $interval[$previous_year] = $previous_year;
448 $interval[$previous_two_year] = $previous_two_year;
449 $interval[$previous_three_year] = $previous_three_year;
450 $interval["upto_{$upto}"] = "Up To {$upto}";
452 foreach ($rows as $key => $row) {
453 $display["upto_{$upto}"] = CRM_Utils_Array
::value("upto_{$upto}", $display) + CRM_Utils_Array
::value("civicrm_upto_{$upto}", $row);
454 $display[$previous_year] = CRM_Utils_Array
::value($previous_year, $display) + CRM_Utils_Array
::value($previous_year, $row);
455 $display[$previous_two_year] = CRM_Utils_Array
::value($previous_two_year, $display) + CRM_Utils_Array
::value($previous_two_year, $row);
456 $display[$previous_three_year] = CRM_Utils_Array
::value($previous_three_year, $display) + CRM_Utils_Array
::value($previous_three_year, $row);
459 $graphRows['value'] = $display;
460 $config = CRM_Core_Config
::Singleton();
462 'legend' => 'Sybunt Report',
464 'yname' => "Amount ({$config->defaultCurrency})",
466 if ($this->_params
['charts']) {
468 CRM_Utils_OpenFlashChart
::reportChart($graphRows, $this->_params
['charts'], $interval, $chartInfo);
469 $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
514 * @param string $type
515 * @param null $fieldName
519 function getOperationPair($type = "string", $fieldName = NULL) {
520 if ($fieldName == 'yid') {
521 return array('calendar' => ts('Is Calendar Year'), 'fiscal' => ts('Fiscal Year Starting'));
523 return parent
::getOperationPair($type, $fieldName);