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 $this->_columns
= array(
59 'dao' => 'CRM_Contact_DAO_Contact',
60 'grouping' => 'contact-field',
64 array('title' => ts('Donor Name'),
67 'first_name' => array('title' => ts('First Name'),
69 'last_name' => array('title' => ts('Last Name'),
75 array('title' => ts('Donor Name'),
82 'dao' => 'CRM_Core_DAO_Email',
83 'grouping' => 'contact-field',
87 array('title' => ts('Email'),
94 'dao' => 'CRM_Core_DAO_Phone',
95 'grouping' => 'contact-field',
99 array('title' => ts('Phone'),
104 'civicrm_contribution' =>
106 'dao' => 'CRM_Contribute_DAO_Contribution',
110 array('title' => ts('contactId'),
111 'no_display' => TRUE,
116 array('title' => ts('Total Amount'),
117 'no_display' => TRUE,
122 array('title' => ts('Year'),
123 'no_display' => TRUE,
132 'name' => 'receive_date',
133 'title' => ts('This Year'),
134 'operatorType' => CRM_Report_Form
::OP_SELECT
,
135 'options' => $optionYear,
136 'default' => date('Y'),
138 'financial_type_id' =>
139 array('title' => ts('Financial Type'),
140 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
141 'options' => CRM_Contribute_PseudoConstant
::financialType(),
143 'contribution_status_id' =>
144 array('title' => ts('Contribution Status'),
145 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
146 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
147 'default' => array('1'),
153 'dao' => 'CRM_Contact_DAO_GroupContact',
159 'name' => 'group_id',
160 'title' => ts('Group'),
161 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
163 'options' => CRM_Core_PseudoConstant
::group(),
169 $this->_tagFilter
= TRUE;
170 parent
::__construct();
173 function preProcess() {
174 parent
::preProcess();
179 $this->_columnHeaders
= array();
180 $current_year = $this->_params
['yid_value'];
181 $previous_year = $current_year - 1;
182 $previous_pyear = $current_year - 2;
183 $previous_ppyear = $current_year - 3;
184 $upTo_year = $current_year - 4;
186 foreach ($this->_columns
as $tableName => $table) {
187 if (array_key_exists('fields', $table)) {
188 foreach ($table['fields'] as $fieldName => $field) {
190 if (CRM_Utils_Array
::value('required', $field) ||
191 CRM_Utils_Array
::value($fieldName, $this->_params
['fields'])
193 if ($fieldName == 'total_amount') {
194 $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}";
196 $this->_columnHeaders
["civicrm_upto_{$upTo_year}"]['type'] = $field['type'];
197 $this->_columnHeaders
["civicrm_upto_{$upTo_year}"]['title'] = "Up To $upTo_year";
199 $this->_columnHeaders
["{$previous_ppyear}"]['type'] = $field['type'];
200 $this->_columnHeaders
["{$previous_ppyear}"]['title'] = $previous_ppyear;
202 $this->_columnHeaders
["{$previous_pyear}"]['type'] = $field['type'];
203 $this->_columnHeaders
["{$previous_pyear}"]['title'] = $previous_pyear;
205 $this->_columnHeaders
["{$previous_year}"]['type'] = $field['type'];
206 $this->_columnHeaders
["{$previous_year}"]['title'] = $previous_year;
208 $this->_columnHeaders
["civicrm_life_time_total"]['type'] = $field['type'];
209 $this->_columnHeaders
["civicrm_life_time_total"]['title'] = 'LifeTime';;
211 elseif ($fieldName == 'receive_date') {
212 $select[] = self
::fiscalYearOffset($field['dbAlias']) . " as {$tableName}_{$fieldName}";
215 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
216 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
217 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
219 if (CRM_Utils_Array
::value('no_display', $field)) {
220 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['no_display'] = TRUE;
227 $this->_select
= "SELECT " . implode(', ', $select) . " ";
233 FROM civicrm_contribution {$this->_aliases['civicrm_contribution']}
234 INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
235 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id
237 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
238 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id
239 AND {$this->_aliases['civicrm_email']}.is_primary = 1
240 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
241 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
242 {$this->_aliases['civicrm_phone']}.is_primary = 1 ";
246 $this->_statusClause
= "";
247 $clauses = array($this->_aliases
['civicrm_contribution'] . '.is_test = 0');
248 foreach ($this->_columns
as $tableName => $table) {
249 if (array_key_exists('filters', $table)) {
250 foreach ($table['filters'] as $fieldName => $field) {
252 if ($fieldName == 'yid') {
253 $clause = "contribution_civireport.contact_id NOT IN
254 (SELECT distinct cont.id FROM civicrm_contact cont, civicrm_contribution contri
255 WHERE cont.id = contri.contact_id AND " . self
::fiscalYearOffset('contri.receive_date') . " = {$this->_params['yid_value']} AND contri.is_test = 0 )";
257 elseif (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
258 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
259 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
260 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
262 if ($relative ||
$from ||
$to) {
263 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
267 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
269 $clause = $this->whereClause($field,
271 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
272 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
273 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
275 if (($fieldName == 'contribution_status_id' ||
$fieldName == 'financial_type_id') && !empty($clause)) {
276 $this->_statusClause
.= " AND " . $clause;
281 if (!empty($clause)) {
282 $clauses[] = $clause;
288 $this->_where
= "WHERE " . implode(' AND ', $clauses);
290 if ($this->_aclWhere
) {
291 $this->_where
.= " AND {$this->_aclWhere} ";
296 $this->assign('chartSupported', TRUE);
297 $this->_groupBy
= "Group BY {$this->_aliases['civicrm_contribution']}.contact_id, " . self
::fiscalYearOffset($this->_aliases
['civicrm_contribution'] . '.receive_date') . " WITH ROLLUP ";
300 function statistics(&$rows) {
301 $statistics = parent
::statistics($rows);
306 SUM({$this->_aliases['civicrm_contribution']}.total_amount ) as amount ";
308 $sql = "{$select} {$this->_from} {$this->_where}";
309 $dao = CRM_Core_DAO
::executeQuery($sql);
311 $statistics['counts']['amount'] = array(
312 'value' => $dao->amount
,
313 'title' => 'Total LifeTime',
314 'type' => CRM_Utils_Type
::T_MONEY
,
321 function postProcess() {
322 // get ready with post process params
323 $this->beginPostProcess();
325 $this->buildACLClause($this->_aliases
['civicrm_contact']);
331 $rows = $contactIds = array();
332 if (!CRM_Utils_Array
::value('charts', $this->_params
)) {
334 $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}";
336 $dao = CRM_Core_DAO
::executeQuery($getContacts);
338 while ($dao->fetch()) {
339 $contactIds[] = $dao->cid
;
345 if (!empty($contactIds) || CRM_Utils_Array
::value('charts', $this->_params
)) {
346 if (CRM_Utils_Array
::value('charts', $this->_params
)) {
347 $sql = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy}";
350 $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} ";
353 $current_year = $this->_params
['yid_value'];
354 $previous_year = $current_year - 1;
355 $previous_pyear = $current_year - 2;
356 $previous_ppyear = $current_year - 3;
357 $upTo_year = $current_year - 4;
359 $rows = $row = array();
360 $dao = CRM_Core_DAO
::executeQuery($sql);
361 $contributionSum = 0;
363 while ($dao->fetch()) {
364 if (!$dao->civicrm_contribution_contact_id
) {
368 foreach ($this->_columnHeaders
as $key => $value) {
369 if (property_exists($dao, $key)) {
370 $rows[$dao->civicrm_contribution_contact_id
][$key] = $dao->$key;
373 if ($dao->civicrm_contribution_receive_date
) {
374 if ($dao->civicrm_contribution_receive_date
> $upTo_year) {
375 $contributionSum +
= $dao->civicrm_contribution_total_amount
;
376 $rows[$dao->civicrm_contribution_contact_id
][$dao->civicrm_contribution_receive_date
] = $dao->civicrm_contribution_total_amount
;
380 $rows[$dao->civicrm_contribution_contact_id
]['civicrm_life_time_total'] = $dao->civicrm_contribution_total_amount
;
381 if (($dao->civicrm_contribution_total_amount
- $contributionSum) > 0) {
382 $rows[$dao->civicrm_contribution_contact_id
]["civicrm_upto_{$upTo_year}"] = $dao->civicrm_contribution_total_amount
- $contributionSum;
384 $contributionSum = 0;
389 // format result set.
390 $this->formatDisplay($rows, FALSE);
392 // assign variables to templates
393 $this->doTemplateAssignment($rows);
395 // do print / pdf / instance stuff if needed
396 $this->endPostProcess($rows);
399 function buildChart(&$rows) {
400 $graphRows = array();
402 $current_year = $this->_params
['yid_value'];
403 $previous_year = $current_year - 1;
404 $previous_two_year = $current_year - 2;
405 $previous_three_year = $current_year - 3;
406 $upto = $current_year - 4;
408 $interval[$previous_year] = $previous_year;
409 $interval[$previous_two_year] = $previous_two_year;
410 $interval[$previous_three_year] = $previous_three_year;
411 $interval["upto_{$upto}"] = "Up To {$upto}";
413 foreach ($rows as $key => $row) {
414 $display["upto_{$upto}"] = CRM_Utils_Array
::value("upto_{$upto}", $display) + CRM_Utils_Array
::value("civicrm_upto_{$upto}", $row);
415 $display[$previous_year] = CRM_Utils_Array
::value($previous_year, $display) + CRM_Utils_Array
::value($previous_year, $row);
416 $display[$previous_two_year] = CRM_Utils_Array
::value($previous_two_year, $display) + CRM_Utils_Array
::value($previous_two_year, $row);
417 $display[$previous_three_year] = CRM_Utils_Array
::value($previous_three_year, $display) + CRM_Utils_Array
::value($previous_three_year, $row);
420 $graphRows['value'] = $display;
421 $config = CRM_Core_Config
::Singleton();
423 'legend' => 'Sybunt Report',
425 'yname' => "Amount ({$config->defaultCurrency})",
427 if ($this->_params
['charts']) {
429 CRM_Utils_OpenFlashChart
::reportChart($graphRows, $this->_params
['charts'], $interval, $chartInfo);
430 $this->assign('chartType', $this->_params
['charts']);
434 function alterDisplay(&$rows) {
436 foreach ($rows as $rowNum => $row) {
437 //Convert Display name into link
438 if (array_key_exists('civicrm_contact_sort_name', $row) &&
439 array_key_exists('civicrm_contribution_contact_id', $row)
441 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
442 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contribution_contact_id'],
443 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
445 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
446 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contribution Details for this Contact.");
451 // Override "This Year" $op options
452 static function getOperationPair($type = "string", $fieldName = NULL) {
453 if ($fieldName == 'yid') {
454 return array('calendar' => ts('Is Calendar Year'), 'fiscal' => ts('Fiscal Year Starting'));
456 return parent
::getOperationPair($type, $fieldName);