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_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');
46 public 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(
66 'civicrm_contact' => array(
67 'dao' => 'CRM_Contact_DAO_Contact',
68 'grouping' => 'contact-field',
71 'title' => ts('Donor Name'),
74 'first_name' => array(
75 'title' => ts('First Name'),
78 'title' => ts('Last Name'),
80 'contact_type' => array(
81 'title' => ts('Contact Type'),
83 'contact_sub_type' => array(
84 'title' => ts('Contact Subtype'),
89 'title' => ts('Donor Name'),
94 'civicrm_email' => array(
95 'dao' => 'CRM_Core_DAO_Email',
96 'grouping' => 'contact-field',
99 'title' => ts('Email'),
104 'civicrm_phone' => array(
105 'dao' => 'CRM_Core_DAO_Phone',
106 'grouping' => 'contact-field',
109 'title' => ts('Phone'),
115 $this->_columns +
= $this->addAddressFields();
116 $this->_columns +
= array(
117 'civicrm_contribution' => array(
118 'dao' => 'CRM_Contribute_DAO_Contribution',
120 'contact_id' => array(
121 'title' => ts('contactId'),
122 'no_display' => TRUE,
126 'total_amount' => array(
127 'title' => ts('Total Amount'),
128 'no_display' => TRUE,
132 'receive_date' => array(
133 'title' => ts('Year'),
134 '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' => array(
148 'title' => ts('Financial Type'),
149 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
150 'options' => CRM_Contribute_PseudoConstant
::financialType(),
152 'contribution_status_id' => array(
153 'title' => ts('Contribution Status'),
154 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
155 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
156 'default' => array('1'),
162 // If we have a campaign, build out the relevant elements
163 if ($campaignEnabled && !empty($this->activeCampaigns
)) {
164 $this->_columns
['civicrm_contribution']['fields']['campaign_id'] = array(
165 'title' => ts('Campaign'),
166 'default' => 'false',
168 $this->_columns
['civicrm_contribution']['filters']['campaign_id'] = array(
169 'title' => ts('Campaign'),
170 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
171 'options' => $this->activeCampaigns
,
175 $this->_groupFilter
= TRUE;
176 $this->_tagFilter
= TRUE;
177 parent
::__construct();
180 public function preProcess() {
181 parent
::preProcess();
184 public function select() {
186 $this->_columnHeaders
= array();
187 $current_year = $this->_params
['yid_value'];
188 $previous_year = $current_year - 1;
189 $previous_pyear = $current_year - 2;
190 $previous_ppyear = $current_year - 3;
191 $upTo_year = $current_year - 4;
193 foreach ($this->_columns
as $tableName => $table) {
194 if (array_key_exists('fields', $table)) {
195 foreach ($table['fields'] as $fieldName => $field) {
197 if (!empty($field['required']) ||
198 !empty($this->_params
['fields'][$fieldName])
200 if ($fieldName == 'total_amount') {
201 $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}";
203 $this->_columnHeaders
["civicrm_upto_{$upTo_year}"]['type'] = $field['type'];
204 $this->_columnHeaders
["civicrm_upto_{$upTo_year}"]['title'] = "Up To $upTo_year";
206 $this->_columnHeaders
["{$previous_ppyear}"]['type'] = $field['type'];
207 $this->_columnHeaders
["{$previous_ppyear}"]['title'] = $previous_ppyear;
209 $this->_columnHeaders
["{$previous_pyear}"]['type'] = $field['type'];
210 $this->_columnHeaders
["{$previous_pyear}"]['title'] = $previous_pyear;
212 $this->_columnHeaders
["{$previous_year}"]['type'] = $field['type'];
213 $this->_columnHeaders
["{$previous_year}"]['title'] = $previous_year;
215 $this->_columnHeaders
["civicrm_life_time_total"]['type'] = $field['type'];
216 $this->_columnHeaders
["civicrm_life_time_total"]['title'] = 'LifeTime';;
218 elseif ($fieldName == 'receive_date') {
219 $select[] = self
::fiscalYearOffset($field['dbAlias']) .
220 " as {$tableName}_{$fieldName}";
223 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
224 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
225 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
227 if (!empty($field['no_display'])) {
228 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['no_display'] = TRUE;
235 $this->_select
= "SELECT " . implode(', ', $select) . " ";
238 public function from() {
241 FROM civicrm_contribution {$this->_aliases['civicrm_contribution']}
242 INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
243 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id
246 if ($this->isTableSelected('civicrm_email')) {
248 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
249 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id
250 AND {$this->_aliases['civicrm_email']}.is_primary = 1";
252 if ($this->isTableSelected('civicrm_phone')) {
254 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
255 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
256 {$this->_aliases['civicrm_phone']}.is_primary = 1";
258 $this->addAddressFromClause();
261 public function where() {
262 $this->_statusClause
= "";
263 $clauses = array($this->_aliases
['civicrm_contribution'] . '.is_test = 0');
264 foreach ($this->_columns
as $tableName => $table) {
265 if (array_key_exists('filters', $table)) {
266 foreach ($table['filters'] as $fieldName => $field) {
268 if ($fieldName == 'yid') {
269 $clause = "contribution_civireport.contact_id NOT IN
270 (SELECT distinct cont.id FROM civicrm_contact cont, civicrm_contribution contri
271 WHERE cont.id = contri.contact_id AND " .
272 self
::fiscalYearOffset('contri.receive_date') .
273 " = {$this->_params['yid_value']} AND contri.is_test = 0 )";
275 elseif (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
277 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
278 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
279 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
281 if ($relative ||
$from ||
$to) {
282 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
286 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
288 $clause = $this->whereClause($field,
290 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
291 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
292 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
294 if (($fieldName == 'contribution_status_id' ||
295 $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} ";
316 public function groupBy() {
317 $this->assign('chartSupported', TRUE);
318 $this->_groupBy
= "Group BY {$this->_aliases['civicrm_contribution']}.contact_id, " .
319 self
::fiscalYearOffset($this->_aliases
['civicrm_contribution'] .
320 '.receive_date') . " WITH ROLLUP ";
328 public function statistics(&$rows) {
329 $statistics = parent
::statistics($rows);
334 SUM({$this->_aliases['civicrm_contribution']}.total_amount ) as amount ";
336 $sql = "{$select} {$this->_from} {$this->_where}";
337 $dao = CRM_Core_DAO
::executeQuery($sql);
339 $statistics['counts']['amount'] = array(
340 'value' => $dao->amount
,
341 'title' => 'Total LifeTime',
342 'type' => CRM_Utils_Type
::T_MONEY
,
349 public function postProcess() {
350 // get ready with post process params
351 $this->beginPostProcess();
353 $this->buildACLClause($this->_aliases
['civicrm_contact']);
359 $rows = $contactIds = array();
360 if (empty($this->_params
['charts'])) {
362 $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}";
364 $dao = CRM_Core_DAO
::executeQuery($getContacts);
366 while ($dao->fetch()) {
367 $contactIds[] = $dao->cid
;
373 if (!empty($contactIds) ||
!empty($this->_params
['charts'])) {
374 if (!empty($this->_params
['charts'])) {
375 $sql = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy}";
379 "{$this->_select} {$this->_from} WHERE {$this->_aliases['civicrm_contact']}.id IN (" .
380 implode(',', $contactIds) .
381 ") AND {$this->_aliases['civicrm_contribution']}.is_test = 0 {$this->_statusClause} {$this->_groupBy} ";
384 $current_year = $this->_params
['yid_value'];
385 $previous_year = $current_year - 1;
386 $previous_pyear = $current_year - 2;
387 $previous_ppyear = $current_year - 3;
388 $upTo_year = $current_year - 4;
390 $rows = $row = array();
391 $dao = CRM_Core_DAO
::executeQuery($sql);
392 $contributionSum = 0;
394 while ($dao->fetch()) {
395 if (!$dao->civicrm_contribution_contact_id
) {
399 foreach ($this->_columnHeaders
as $key => $value) {
400 if (property_exists($dao, $key)) {
401 $rows[$dao->civicrm_contribution_contact_id
][$key] = $dao->$key;
404 if ($dao->civicrm_contribution_receive_date
) {
405 if ($dao->civicrm_contribution_receive_date
> $upTo_year) {
406 $contributionSum +
= $dao->civicrm_contribution_total_amount
;
407 $rows[$dao->civicrm_contribution_contact_id
][$dao->civicrm_contribution_receive_date
] = $dao->civicrm_contribution_total_amount
;
411 $rows[$dao->civicrm_contribution_contact_id
]['civicrm_life_time_total'] = $dao->civicrm_contribution_total_amount
;
412 if (($dao->civicrm_contribution_total_amount
- $contributionSum) > 0
414 $rows[$dao->civicrm_contribution_contact_id
]["civicrm_upto_{$upTo_year}"]
415 = $dao->civicrm_contribution_total_amount
- $contributionSum;
417 $contributionSum = 0;
422 // format result set.
423 $this->formatDisplay($rows, FALSE);
425 // assign variables to templates
426 $this->doTemplateAssignment($rows);
428 // do print / pdf / instance stuff if needed
429 $this->endPostProcess($rows);
435 public function buildChart(&$rows) {
436 $graphRows = array();
438 $current_year = $this->_params
['yid_value'];
439 $previous_year = $current_year - 1;
440 $previous_two_year = $current_year - 2;
441 $previous_three_year = $current_year - 3;
442 $upto = $current_year - 4;
444 $interval[$previous_year] = $previous_year;
445 $interval[$previous_two_year] = $previous_two_year;
446 $interval[$previous_three_year] = $previous_three_year;
447 $interval["upto_{$upto}"] = "Up To {$upto}";
449 foreach ($rows as $key => $row) {
450 $display["upto_{$upto}"]
451 = CRM_Utils_Array
::value("upto_{$upto}", $display)
452 + CRM_Utils_Array
::value("civicrm_upto_{$upto}", $row);
453 $display[$previous_year]
454 = CRM_Utils_Array
::value($previous_year, $display)
455 + CRM_Utils_Array
::value($previous_year, $row);
456 $display[$previous_two_year]
457 = CRM_Utils_Array
::value($previous_two_year, $display)
458 + CRM_Utils_Array
::value($previous_two_year, $row);
459 $display[$previous_three_year]
460 = CRM_Utils_Array
::value($previous_three_year, $display)
461 + CRM_Utils_Array
::value($previous_three_year, $row);
464 $graphRows['value'] = $display;
465 $config = CRM_Core_Config
::Singleton();
467 'legend' => 'Sybunt Report',
469 'yname' => "Amount ({$config->defaultCurrency})",
471 if ($this->_params
['charts']) {
473 CRM_Utils_OpenFlashChart
::reportChart($graphRows, $this->_params
['charts'], $interval, $chartInfo);
474 $this->assign('chartType', $this->_params
['charts']);
481 public function alterDisplay(&$rows) {
482 // custom code to alter rows
485 foreach ($rows as $rowNum => $row) {
486 //Convert Display name into link
487 if (array_key_exists('civicrm_contact_sort_name', $row) &&
488 array_key_exists('civicrm_contribution_contact_id', $row)
490 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
491 'reset=1&force=1&id_op=eq&id_value=' .
492 $row['civicrm_contribution_contact_id'],
493 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
495 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
496 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contribution Details for this Contact.");
500 // convert campaign_id to campaign title
501 if (array_key_exists('civicrm_contribution_campaign_id', $row)) {
502 if ($value = $row['civicrm_contribution_campaign_id']) {
503 $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns
[$value];
508 $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'contribute/detail', 'List all contribution(s)') ?
TRUE : $entryFound;
510 // skip looking further in rows, if first row itself doesn't
511 // have the column we need
519 * Override "This Year" $op options
520 * @param string $type
521 * @param null $fieldName
525 public function getOperationPair($type = "string", $fieldName = NULL) {
526 if ($fieldName == 'yid') {
528 'calendar' => ts('Is Calendar Year'),
529 'fiscal' => ts('Fiscal Year Starting'),
532 return parent
::getOperationPair($type, $fieldName);