| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 9 | | | |
| 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. | |
| 13 | | | |
| 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. | |
| 18 | | | |
| 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 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | class CRM_Report_Form_Contribute_Lybunt extends CRM_Report_Form { |
| 36 | |
| 37 | |
| 38 | protected $_charts = array( |
| 39 | '' => 'Tabular', |
| 40 | 'barChart' => 'Bar Chart', |
| 41 | 'pieChart' => 'Pie Chart', |
| 42 | ); |
| 43 | |
| 44 | public $_drilldownReport = array('contribute/detail' => 'Link to Detail Report'); |
| 45 | |
| 46 | protected $lifeTime_from = NULL; |
| 47 | protected $lifeTime_where = NULL; |
| 48 | |
| 49 | function __construct() { |
| 50 | $yearsInPast = 10; |
| 51 | $yearsInFuture = 1; |
| 52 | $date = CRM_Core_SelectValues::date('custom', NULL, $yearsInPast, $yearsInFuture); |
| 53 | $count = $date['maxYear']; |
| 54 | while ($date['minYear'] <= $count) { |
| 55 | $optionYear[$date['minYear']] = $date['minYear']; |
| 56 | $date['minYear']++; |
| 57 | } |
| 58 | |
| 59 | // Check if CiviCampaign is a) enabled and b) has active campaigns |
| 60 | $config = CRM_Core_Config::singleton(); |
| 61 | $campaignEnabled = in_array("CiviCampaign", $config->enableComponents); |
| 62 | if ($campaignEnabled) { |
| 63 | $getCampaigns = CRM_Campaign_BAO_Campaign::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE); |
| 64 | $this->activeCampaigns = $getCampaigns['campaigns']; |
| 65 | asort($this->activeCampaigns); |
| 66 | } |
| 67 | |
| 68 | $this->_columns = array( |
| 69 | 'civicrm_contact' => |
| 70 | array( |
| 71 | 'dao' => 'CRM_Contact_DAO_Contact', |
| 72 | 'grouping' => 'contact-field', |
| 73 | 'fields' => |
| 74 | array( |
| 75 | 'sort_name' => |
| 76 | array('title' => ts('Donor Name'), |
| 77 | 'default' => TRUE, |
| 78 | 'required' => TRUE, |
| 79 | ), |
| 80 | 'first_name' => array( |
| 81 | 'title' => ts('First Name'), |
| 82 | ), |
| 83 | 'last_name' => array( |
| 84 | 'title' => ts('Last Name'), |
| 85 | ), |
| 86 | 'contact_type' => |
| 87 | array( |
| 88 | 'title' => ts('Contact Type'), |
| 89 | ), |
| 90 | 'contact_sub_type' => |
| 91 | array( |
| 92 | 'title' => ts('Contact SubType'), |
| 93 | ), |
| 94 | ), |
| 95 | 'filters' => |
| 96 | array( |
| 97 | 'sort_name' => |
| 98 | array('title' => ts('Donor Name'), |
| 99 | 'operator' => 'like', |
| 100 | ), |
| 101 | ), |
| 102 | ), |
| 103 | 'civicrm_email' => |
| 104 | array( |
| 105 | 'dao' => 'CRM_Core_DAO_Email', |
| 106 | 'grouping' => 'contact-field', |
| 107 | 'fields' => |
| 108 | array( |
| 109 | 'email' => |
| 110 | array('title' => ts('Email'), |
| 111 | 'default' => TRUE, |
| 112 | ), |
| 113 | ), |
| 114 | ), |
| 115 | 'civicrm_phone' => |
| 116 | array( |
| 117 | 'dao' => 'CRM_Core_DAO_Phone', |
| 118 | 'grouping' => 'contact-field', |
| 119 | 'fields' => |
| 120 | array( |
| 121 | 'phone' => |
| 122 | array('title' => ts('Phone'), |
| 123 | 'default' => TRUE, |
| 124 | ), |
| 125 | ), |
| 126 | ), |
| 127 | ) |
| 128 | + $this->addAddressFields() |
| 129 | + array( |
| 130 | 'civicrm_contribution' => |
| 131 | array( |
| 132 | 'dao' => 'CRM_Contribute_DAO_Contribution', |
| 133 | 'fields' => |
| 134 | array( |
| 135 | 'contact_id' => |
| 136 | array('title' => ts('contactId'), |
| 137 | 'no_display' => TRUE, |
| 138 | 'required' => TRUE, |
| 139 | 'no_repeat' => TRUE, |
| 140 | ), |
| 141 | 'total_amount' => |
| 142 | array('title' => ts('Total Amount'), |
| 143 | 'no_display' => TRUE, |
| 144 | 'required' => TRUE, |
| 145 | 'no_repeat' => TRUE, |
| 146 | ), |
| 147 | 'receive_date' => |
| 148 | array('title' => ts('Year'), |
| 149 | 'no_display' => TRUE, |
| 150 | 'required' => TRUE, |
| 151 | 'no_repeat' => TRUE, |
| 152 | ), |
| 153 | ), |
| 154 | 'filters' => |
| 155 | array( |
| 156 | 'yid' => |
| 157 | array( |
| 158 | 'name' => 'receive_date', |
| 159 | 'title' => ts('This Year'), |
| 160 | 'operatorType' => CRM_Report_Form::OP_SELECT, |
| 161 | 'options' => $optionYear, |
| 162 | 'default' => date('Y'), |
| 163 | ), |
| 164 | 'financial_type_id' => array( |
| 165 | 'title' => ts('Financial Type'), |
| 166 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
| 167 | 'options' => CRM_Contribute_PseudoConstant::financialType(), |
| 168 | ), |
| 169 | 'contribution_status_id' => |
| 170 | array('title' => ts('Contribution Status'), |
| 171 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
| 172 | 'options' => CRM_Contribute_PseudoConstant::contributionStatus(), |
| 173 | 'default' => array('1'), |
| 174 | ), |
| 175 | ), |
| 176 | ), |
| 177 | 'civicrm_group' => |
| 178 | array( |
| 179 | 'dao' => 'CRM_Contact_DAO_GroupContact', |
| 180 | 'alias' => 'cgroup', |
| 181 | 'filters' => |
| 182 | array( |
| 183 | 'gid' => |
| 184 | array( |
| 185 | 'name' => 'group_id', |
| 186 | 'title' => ts('Group'), |
| 187 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
| 188 | 'group' => TRUE, |
| 189 | 'options' => CRM_Core_PseudoConstant::group(), |
| 190 | ), |
| 191 | ), |
| 192 | ), |
| 193 | ); |
| 194 | |
| 195 | // If we have a campaign, build out the relevant elements |
| 196 | if ($campaignEnabled && !empty($this->activeCampaigns)) { |
| 197 | $this->_columns['civicrm_contribution']['fields']['campaign_id'] = array( |
| 198 | 'title' => ts('Campaign'), |
| 199 | 'default' => 'false', |
| 200 | ); |
| 201 | $this->_columns['civicrm_contribution']['filters']['campaign_id'] = array('title' => ts('Campaign'), |
| 202 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
| 203 | 'options' => $this->activeCampaigns, |
| 204 | ); |
| 205 | } |
| 206 | |
| 207 | $this->_tagFilter = TRUE; |
| 208 | parent::__construct(); |
| 209 | } |
| 210 | |
| 211 | function preProcess() { |
| 212 | parent::preProcess(); |
| 213 | } |
| 214 | |
| 215 | function select() { |
| 216 | |
| 217 | $this->_columnHeaders = $select = array(); |
| 218 | $current_year = $this->_params['yid_value']; |
| 219 | $previous_year = $current_year - 1; |
| 220 | |
| 221 | |
| 222 | foreach ($this->_columns as $tableName => $table) { |
| 223 | |
| 224 | if (array_key_exists('fields', $table)) { |
| 225 | foreach ($table['fields'] as $fieldName => $field) { |
| 226 | |
| 227 | if (!empty($field['required']) || !empty($this->_params['fields'][$fieldName])) { |
| 228 | if ($fieldName == 'total_amount') { |
| 229 | $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}"; |
| 230 | |
| 231 | $this->_columnHeaders["{$previous_year}"]['type'] = $field['type']; |
| 232 | $this->_columnHeaders["{$previous_year}"]['title'] = $previous_year; |
| 233 | |
| 234 | $this->_columnHeaders["civicrm_life_time_total"]['type'] = $field['type']; |
| 235 | $this->_columnHeaders["civicrm_life_time_total"]['title'] = 'LifeTime';; |
| 236 | } |
| 237 | elseif ($fieldName == 'receive_date') { |
| 238 | $select[] = self::fiscalYearOffset($field['dbAlias']) . " as {$tableName}_{$fieldName} "; |
| 239 | } |
| 240 | else { |
| 241 | $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName} "; |
| 242 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field); |
| 243 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field); |
| 244 | } |
| 245 | |
| 246 | if (!empty($field['no_display'])) { |
| 247 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['no_display'] = TRUE; |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | $this->_select = "SELECT " . implode(', ', $select) . " "; |
| 255 | } |
| 256 | |
| 257 | function from() { |
| 258 | |
| 259 | $this->_from = " |
| 260 | FROM civicrm_contribution {$this->_aliases['civicrm_contribution']} |
| 261 | INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact']} |
| 262 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id |
| 263 | {$this->_aclFrom}"; |
| 264 | |
| 265 | if ($this->isTableSelected('civicrm_email')) { |
| 266 | $this->_from .= " |
| 267 | LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']} |
| 268 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id |
| 269 | AND {$this->_aliases['civicrm_email']}.is_primary = 1"; |
| 270 | } |
| 271 | if ($this->isTableSelected('civicrm_phone')) { |
| 272 | $this->_from .= " |
| 273 | LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']} |
| 274 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND |
| 275 | {$this->_aliases['civicrm_phone']}.is_primary = 1"; |
| 276 | } |
| 277 | $this->addAddressFromClause(); |
| 278 | } |
| 279 | |
| 280 | function where() { |
| 281 | $this->_statusClause = ""; |
| 282 | $clauses = array($this->_aliases['civicrm_contribution'] . '.is_test = 0'); |
| 283 | $current_year = $this->_params['yid_value']; |
| 284 | $previous_year = $current_year - 1; |
| 285 | |
| 286 | foreach ($this->_columns as $tableName => $table) { |
| 287 | if (array_key_exists('filters', $table)) { |
| 288 | foreach ($table['filters'] as $fieldName => $field) { |
| 289 | $clause = NULL; |
| 290 | if ($fieldName == 'yid') { |
| 291 | $clause = "contribution_civireport.contact_id NOT IN |
| 292 | (SELECT distinct contri.contact_id FROM civicrm_contribution contri |
| 293 | WHERE contri.is_test = 0 AND " . self::fiscalYearOffset('contri.receive_date') . " = $current_year) AND contribution_civireport.contact_id IN (SELECT distinct contri.contact_id FROM civicrm_contribution contri |
| 294 | WHERE " . self::fiscalYearOffset('contri.receive_date') . " = $previous_year AND contri.is_test = 0)"; |
| 295 | } |
| 296 | elseif (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) { |
| 297 | $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params); |
| 298 | $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params); |
| 299 | $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params); |
| 300 | |
| 301 | if ($relative || $from || $to) { |
| 302 | $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']); |
| 303 | } |
| 304 | } |
| 305 | else { |
| 306 | $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params); |
| 307 | if ($op) { |
| 308 | $clause = $this->whereClause($field, |
| 309 | $op, |
| 310 | CRM_Utils_Array::value("{$fieldName}_value", $this->_params), |
| 311 | CRM_Utils_Array::value("{$fieldName}_min", $this->_params), |
| 312 | CRM_Utils_Array::value("{$fieldName}_max", $this->_params) |
| 313 | ); |
| 314 | if (($fieldName == 'contribution_status_id' || $fieldName == 'financial_type_id') && !empty($clause)) { |
| 315 | $this->_statusClause .= " AND " . $clause; |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | if (!empty($clause)) { |
| 321 | $clauses[] = $clause; |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | $this->_where = 'WHERE ' . implode(' AND ', $clauses); |
| 328 | |
| 329 | if ($this->_aclWhere) { |
| 330 | $this->_where .= " AND {$this->_aclWhere} "; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | function groupBy() { |
| 335 | $this->_groupBy = "GROUP BY {$this->_aliases['civicrm_contribution']}.contact_id, " . self::fiscalYearOffset($this->_aliases['civicrm_contribution'] . '.receive_date') . " WITH ROLLUP"; |
| 336 | $this->assign('chartSupported', TRUE); |
| 337 | } |
| 338 | |
| 339 | function statistics(&$rows) { |
| 340 | $statistics = parent::statistics($rows); |
| 341 | if (!empty($rows)) { |
| 342 | $select = " |
| 343 | SELECT |
| 344 | SUM({$this->_aliases['civicrm_contribution']}.total_amount ) as amount "; |
| 345 | |
| 346 | $sql = "{$select} {$this->_from} {$this->_where}"; |
| 347 | $dao = CRM_Core_DAO::executeQuery($sql); |
| 348 | if ($dao->fetch()) { |
| 349 | $statistics['counts']['amount'] = array( |
| 350 | 'value' => $dao->amount, |
| 351 | 'title' => 'Total LifeTime', |
| 352 | 'type' => CRM_Utils_Type::T_MONEY, |
| 353 | ); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | return $statistics; |
| 358 | } |
| 359 | |
| 360 | function postProcess() { |
| 361 | |
| 362 | // get ready with post process params |
| 363 | $this->beginPostProcess(); |
| 364 | |
| 365 | // get the acl clauses built before we assemble the query |
| 366 | $this->buildACLClause($this->_aliases['civicrm_contact']); |
| 367 | $this->select(); |
| 368 | $this->from(); |
| 369 | $this->where(); |
| 370 | $this->groupBy(); |
| 371 | |
| 372 | $rows = $contactIds = array(); |
| 373 | if (empty($this->_params['charts'])) { |
| 374 | $this->limit(); |
| 375 | $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}"; |
| 376 | |
| 377 | $dao = CRM_Core_DAO::executeQuery($getContacts); |
| 378 | |
| 379 | while ($dao->fetch()) { |
| 380 | $contactIds[] = $dao->cid; |
| 381 | } |
| 382 | $dao->free(); |
| 383 | $this->setPager(); |
| 384 | } |
| 385 | |
| 386 | if (!empty($contactIds) || !empty($this->_params['charts'])) { |
| 387 | if (!empty($this->_params['charts'])) { |
| 388 | $sql = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy}"; |
| 389 | } |
| 390 | else { |
| 391 | $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} "; |
| 392 | } |
| 393 | |
| 394 | $dao = CRM_Core_DAO::executeQuery($sql); |
| 395 | $current_year = $this->_params['yid_value']; |
| 396 | $previous_year = $current_year - 1; |
| 397 | |
| 398 | while ($dao->fetch()) { |
| 399 | |
| 400 | if (!$dao->civicrm_contribution_contact_id) { |
| 401 | continue; |
| 402 | } |
| 403 | |
| 404 | $row = array(); |
| 405 | foreach ($this->_columnHeaders as $key => $value) { |
| 406 | if (property_exists($dao, $key)) { |
| 407 | $rows[$dao->civicrm_contribution_contact_id][$key] = $dao->$key; |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | if ($dao->civicrm_contribution_receive_date) { |
| 412 | if ($dao->civicrm_contribution_receive_date == $previous_year) { |
| 413 | $rows[$dao->civicrm_contribution_contact_id][$dao->civicrm_contribution_receive_date] = $dao->civicrm_contribution_total_amount; |
| 414 | } |
| 415 | } |
| 416 | else { |
| 417 | $rows[$dao->civicrm_contribution_contact_id]['civicrm_life_time_total'] = $dao->civicrm_contribution_total_amount; |
| 418 | } |
| 419 | } |
| 420 | $dao->free(); |
| 421 | } |
| 422 | |
| 423 | $this->formatDisplay($rows, FALSE); |
| 424 | |
| 425 | // assign variables to templates |
| 426 | $this->doTemplateAssignment($rows); |
| 427 | |
| 428 | // do print / pdf / instance stuff if needed |
| 429 | $this->endPostProcess($rows); |
| 430 | } |
| 431 | |
| 432 | function buildChart(&$rows) { |
| 433 | |
| 434 | $graphRows = array(); |
| 435 | $count = 0; |
| 436 | $display = array(); |
| 437 | |
| 438 | $current_year = $this->_params['yid_value']; |
| 439 | $previous_year = $current_year - 1; |
| 440 | $interval[$previous_year] = $previous_year; |
| 441 | $interval['life_time'] = 'Life Time'; |
| 442 | |
| 443 | foreach ($rows as $key => $row) { |
| 444 | $display['life_time'] = CRM_Utils_Array::value('life_time', $display) + $row['civicrm_life_time_total']; |
| 445 | $display[$previous_year] = CRM_Utils_Array::value($previous_year, $display) + $row[$previous_year]; |
| 446 | } |
| 447 | |
| 448 | $config = CRM_Core_Config::Singleton(); |
| 449 | $graphRows['value'] = $display; |
| 450 | $chartInfo = array('legend' => ts('Lybunt Report'), |
| 451 | 'xname' => ts('Year'), |
| 452 | 'yname' => ts('Amount (%1)', array(1 => $config->defaultCurrency)), |
| 453 | ); |
| 454 | if ($this->_params['charts']) { |
| 455 | // build chart. |
| 456 | CRM_Utils_OpenFlashChart::reportChart($graphRows, $this->_params['charts'], $interval, $chartInfo); |
| 457 | $this->assign('chartType', $this->_params['charts']); |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | function alterDisplay(&$rows) { |
| 462 | // custom code to alter rows |
| 463 | $entryFound = FALSE; |
| 464 | |
| 465 | foreach ($rows as $rowNum => $row) { |
| 466 | //Convert Display name into link |
| 467 | if (array_key_exists('civicrm_contact_sort_name', $row) && |
| 468 | array_key_exists('civicrm_contribution_contact_id', $row) |
| 469 | ) { |
| 470 | $url = CRM_Report_Utils_Report::getNextUrl('contribute/detail', |
| 471 | 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contribution_contact_id'], |
| 472 | $this->_absoluteUrl, $this->_id, $this->_drilldownReport |
| 473 | ); |
| 474 | $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url; |
| 475 | $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contribution Details for this Contact."); |
| 476 | $entryFound = TRUE; |
| 477 | } |
| 478 | |
| 479 | // convert campaign_id to campaign title |
| 480 | if (array_key_exists('civicrm_contribution_campaign_id', $row)) { |
| 481 | if ($value = $row['civicrm_contribution_campaign_id']) { |
| 482 | $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns[$value]; |
| 483 | $entryFound = TRUE; |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'contribute/detail', 'List all contribution(s)') ? TRUE : $entryFound; |
| 488 | |
| 489 | // skip looking further in rows, if first row itself doesn't |
| 490 | // have the column we need |
| 491 | if (!$entryFound) { |
| 492 | break; |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | // Override "This Year" $op options |
| 498 | function getOperationPair($type = "string", $fieldName = NULL) { |
| 499 | if ($fieldName == 'yid') { |
| 500 | return array('calendar' => ts('Is Calendar Year'), 'fiscal' => ts('Fiscal Year Starting')); |
| 501 | } |
| 502 | return parent::getOperationPair($type, $fieldName); |
| 503 | } |
| 504 | } |
| 505 | |