| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.4 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2013 | |
| 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-2013 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | class CRM_Report_Form_Contribute_Detail extends CRM_Report_Form { |
| 36 | protected $_addressField = FALSE; |
| 37 | |
| 38 | protected $_emailField = FALSE; |
| 39 | protected $_emailFieldHonor = FALSE; |
| 40 | |
| 41 | protected $_nameFieldHonor = FALSE; |
| 42 | |
| 43 | protected $_summary = NULL; |
| 44 | protected $_allBatches = NULL; |
| 45 | |
| 46 | protected $_softFrom = NULL; |
| 47 | |
| 48 | protected $_customGroupExtends = array( 'Contribution'); |
| 49 | |
| 50 | function __construct() { |
| 51 | |
| 52 | // Check if CiviCampaign is a) enabled and b) has active campaigns |
| 53 | $config = CRM_Core_Config::singleton(); |
| 54 | $campaignEnabled = in_array("CiviCampaign", $config->enableComponents); |
| 55 | if ($campaignEnabled) { |
| 56 | $getCampaigns = CRM_Campaign_BAO_Campaign::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE); |
| 57 | $this->activeCampaigns = $getCampaigns['campaigns']; |
| 58 | asort($this->activeCampaigns); |
| 59 | } |
| 60 | $this->_columns = array( |
| 61 | 'civicrm_contact' => |
| 62 | array( |
| 63 | 'dao' => 'CRM_Contact_DAO_Contact', |
| 64 | 'fields' => |
| 65 | array( |
| 66 | 'sort_name' => |
| 67 | array('title' => ts('Donor Name'), |
| 68 | 'required' => TRUE, |
| 69 | ), |
| 70 | 'first_name' => array('title' => ts('First Name'), |
| 71 | ), |
| 72 | 'last_name' => array('title' => ts('Last Name'), |
| 73 | ), |
| 74 | 'id' => |
| 75 | array( |
| 76 | 'no_display' => TRUE, |
| 77 | 'required' => TRUE, |
| 78 | ), |
| 79 | 'contact_type' => |
| 80 | array( |
| 81 | 'title' => ts('Contact Type'), |
| 82 | ), |
| 83 | 'contact_sub_type' => |
| 84 | array( |
| 85 | 'title' => ts('Contact SubType'), |
| 86 | ), |
| 87 | ), |
| 88 | 'filters' => |
| 89 | array( |
| 90 | 'sort_name' => |
| 91 | array('title' => ts('Donor Name'), |
| 92 | 'operator' => 'like', |
| 93 | ), |
| 94 | 'id' => |
| 95 | array('title' => ts('Contact ID'), |
| 96 | 'no_display' => TRUE, |
| 97 | 'type' => CRM_Utils_Type::T_INT, |
| 98 | ), |
| 99 | ), |
| 100 | 'order_bys' => |
| 101 | array( |
| 102 | 'sort_name' => array( |
| 103 | 'title' => ts('Last Name, First Name'), |
| 104 | 'default' => '1', |
| 105 | 'default_weight' => '0', |
| 106 | 'default_order' => 'ASC' |
| 107 | ), |
| 108 | ), |
| 109 | 'grouping' => 'contact-fields', |
| 110 | ), |
| 111 | 'civicrm_email' => |
| 112 | array( |
| 113 | 'dao' => 'CRM_Core_DAO_Email', |
| 114 | 'fields' => |
| 115 | array( |
| 116 | 'email' => |
| 117 | array('title' => ts('Donor Email'), |
| 118 | 'default' => TRUE, |
| 119 | ), |
| 120 | ), |
| 121 | 'grouping' => 'contact-fields', |
| 122 | ), |
| 123 | 'civicrm_phone' => |
| 124 | array( |
| 125 | 'dao' => 'CRM_Core_DAO_Phone', |
| 126 | 'fields' => |
| 127 | array( |
| 128 | 'phone' => |
| 129 | array('title' => ts('Donor Phone'), |
| 130 | 'default' => TRUE, |
| 131 | 'no_repeat' => TRUE, |
| 132 | ), |
| 133 | ), |
| 134 | 'grouping' => 'contact-fields', |
| 135 | ), |
| 136 | 'civicrm_contact_honor' => |
| 137 | array( |
| 138 | 'dao' => 'CRM_Contact_DAO_Contact', |
| 139 | 'fields' => |
| 140 | array( |
| 141 | 'sort_name_honor' => |
| 142 | array('title' => ts('Honoree Name'), |
| 143 | 'name' => 'sort_name', |
| 144 | 'alias' => 'contacthonor', |
| 145 | 'default' => FALSE, |
| 146 | ), |
| 147 | 'id_honor' => |
| 148 | array( |
| 149 | 'no_display' => TRUE, |
| 150 | 'title' => ts('Honoree ID'), |
| 151 | 'name' => 'id', |
| 152 | 'alias' => 'contacthonor', |
| 153 | 'required' => TRUE, |
| 154 | ), |
| 155 | ), |
| 156 | ), |
| 157 | 'civicrm_email_honor' => |
| 158 | array( |
| 159 | 'dao' => 'CRM_Core_DAO_Email', |
| 160 | 'fields' => |
| 161 | array( |
| 162 | 'email_honor' => |
| 163 | array('title' => ts('Honoree Email'), |
| 164 | 'name' => 'email', |
| 165 | 'alias' => 'emailhonor', |
| 166 | 'default' => FALSE, |
| 167 | ), |
| 168 | ), |
| 169 | 'grouping' => 'contact-fields', |
| 170 | ), |
| 171 | 'civicrm_contribution' => |
| 172 | array( |
| 173 | 'dao' => 'CRM_Contribute_DAO_Contribution', |
| 174 | 'fields' => |
| 175 | array( |
| 176 | 'contribution_id' => array( |
| 177 | 'name' => 'id', |
| 178 | 'no_display' => TRUE, |
| 179 | 'required' => TRUE, |
| 180 | ), |
| 181 | 'list_contri_id' => array( |
| 182 | 'name' => 'id', |
| 183 | 'title' => ts('Contribution ID'), |
| 184 | ), |
| 185 | 'contribution_or_soft' => |
| 186 | array('title' => ts('Contribution OR Soft Credit?'), |
| 187 | 'dbAlias' => "'Contribution'" |
| 188 | ), |
| 189 | 'soft_credits' => |
| 190 | array('title' => ts('Soft Credits'), |
| 191 | 'dbAlias' => "NULL" |
| 192 | ), |
| 193 | 'soft_credit_for' => |
| 194 | array('title' => ts('Soft Credit For'), |
| 195 | 'dbAlias' => "NULL" |
| 196 | ), |
| 197 | 'financial_type_id' => array('title' => ts('Financial Type'), |
| 198 | 'default' => TRUE, |
| 199 | ), |
| 200 | 'contribution_status_id' => array('title' => ts('Contribution Status'), |
| 201 | ), |
| 202 | 'contribution_page_id' => array('title' => ts('Contribution Page'), |
| 203 | ), |
| 204 | 'source' => array('title' => ts('Source'), |
| 205 | ), |
| 206 | 'payment_instrument_id' => array('title' => ts('Payment Type'), |
| 207 | ), |
| 208 | 'check_number' => array('title' => ts('Check Number'), |
| 209 | ), |
| 210 | 'currency' => |
| 211 | array('required' => TRUE, |
| 212 | 'no_display' => TRUE, |
| 213 | ), |
| 214 | 'trxn_id' => NULL, |
| 215 | 'receive_date' => array('default' => TRUE), |
| 216 | 'receipt_date' => NULL, |
| 217 | 'honor_type_id' => array('title' => ts('Honor Type'), |
| 218 | 'default' => FALSE, |
| 219 | ), |
| 220 | 'fee_amount' => NULL, |
| 221 | 'net_amount' => NULL, |
| 222 | 'total_amount' => array('title' => ts('Amount'), |
| 223 | 'required' => TRUE, |
| 224 | 'statistics' => |
| 225 | array('sum' => ts('Amount')), |
| 226 | ), |
| 227 | ), |
| 228 | 'filters' => |
| 229 | array( |
| 230 | 'contribution_or_soft' => |
| 231 | array('title' => ts('Contribution OR Soft Credit?'), |
| 232 | 'clause' => "(1)", |
| 233 | 'operatorType' => CRM_Report_Form::OP_SELECT, |
| 234 | 'type' => CRM_Utils_Type::T_STRING, |
| 235 | 'options' => array( |
| 236 | 'both' => ts('Both'), |
| 237 | 'contributions_only' => ts('Contributions Only'), |
| 238 | 'soft_credits_only' => ts('Soft Credits Only'), |
| 239 | ), |
| 240 | ), |
| 241 | 'receive_date' => |
| 242 | array('operatorType' => CRM_Report_Form::OP_DATE), |
| 243 | 'currency' => |
| 244 | array('title' => 'Currency', |
| 245 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
| 246 | 'options' => CRM_Core_OptionGroup::values('currencies_enabled'), |
| 247 | 'default' => NULL, |
| 248 | 'type' => CRM_Utils_Type::T_STRING, |
| 249 | ), |
| 250 | 'financial_type_id' => |
| 251 | array('title' => ts('Financial Type'), |
| 252 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
| 253 | 'options' => CRM_Contribute_PseudoConstant::financialType(), |
| 254 | 'type' => CRM_Utils_Type::T_INT, |
| 255 | ), |
| 256 | 'contribution_page_id' => |
| 257 | array('title' => ts('Contribution Page'), |
| 258 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
| 259 | 'options' => CRM_Contribute_PseudoConstant::contributionPage(), |
| 260 | 'type' => CRM_Utils_Type::T_INT, |
| 261 | ), |
| 262 | 'payment_instrument_id' => |
| 263 | array('title' => ts('Payment Type'), |
| 264 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
| 265 | 'options' => CRM_Contribute_PseudoConstant::paymentInstrument(), |
| 266 | 'type' => CRM_Utils_Type::T_INT, |
| 267 | ), |
| 268 | 'contribution_status_id' => |
| 269 | array('title' => ts('Contribution Status'), |
| 270 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
| 271 | 'options' => CRM_Contribute_PseudoConstant::contributionStatus(), |
| 272 | 'default' => array(1), |
| 273 | 'type' => CRM_Utils_Type::T_INT, |
| 274 | ), |
| 275 | 'total_amount' => |
| 276 | array('title' => ts('Contribution Amount')), |
| 277 | ), |
| 278 | 'order_bys' => array( |
| 279 | 'financial_type_id' => array('title' => ts('Financial Type')), |
| 280 | 'contribution_status_id' => array('title' => ts('Contribution Status')), |
| 281 | 'payment_instrument_id' => array('title' => ts('Payment Instrument')), |
| 282 | 'receive_date' => array('title' => ts('Receive Date')), |
| 283 | ), |
| 284 | 'grouping' => 'contri-fields', |
| 285 | ), |
| 286 | 'civicrm_group' => |
| 287 | array( |
| 288 | 'dao' => 'CRM_Contact_DAO_GroupContact', |
| 289 | 'alias' => 'cgroup', |
| 290 | 'filters' => |
| 291 | array( |
| 292 | 'gid' => |
| 293 | array( |
| 294 | 'name' => 'group_id', |
| 295 | 'title' => ts('Group'), |
| 296 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
| 297 | 'group' => TRUE, |
| 298 | 'options' => CRM_Core_PseudoConstant::group(), |
| 299 | 'type' => CRM_Utils_Type::T_INT, |
| 300 | ), |
| 301 | ), |
| 302 | ), |
| 303 | 'civicrm_contribution_ordinality' => |
| 304 | array( |
| 305 | 'dao' => 'CRM_Contribute_DAO_Contribution', |
| 306 | 'alias' => 'cordinality', |
| 307 | 'filters' => |
| 308 | array( |
| 309 | 'ordinality' => |
| 310 | array('title' => ts('Contribution Ordinality'), |
| 311 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
| 312 | 'options' => array( |
| 313 | 0 => 'First by Contributor', |
| 314 | 1 => 'Second or Later by Contributor', |
| 315 | ), |
| 316 | 'type' => CRM_Utils_Type::T_INT, |
| 317 | ), |
| 318 | ), |
| 319 | ), |
| 320 | 'civicrm_note' => |
| 321 | array( |
| 322 | 'dao' => 'CRM_Core_DAO_Note', |
| 323 | 'fields' => |
| 324 | array( |
| 325 | 'contribution_note' => |
| 326 | array( |
| 327 | 'name' => 'note', |
| 328 | 'title' => ts('Contribution Note'), |
| 329 | ), |
| 330 | ), |
| 331 | 'filters' => |
| 332 | array( |
| 333 | 'note' => |
| 334 | array( |
| 335 | 'name' => 'note', |
| 336 | 'title' => ts('Contribution Note'), |
| 337 | 'operator' => 'like', |
| 338 | 'type' => CRM_Utils_Type::T_STRING, |
| 339 | ), |
| 340 | ), |
| 341 | ), |
| 342 | ) + $this->addAddressFields(FALSE); |
| 343 | |
| 344 | $this->_tagFilter = TRUE; |
| 345 | |
| 346 | // Don't show Batch display column and filter unless batches are being used |
| 347 | $this->_allBatches = CRM_Batch_BAO_Batch::getBatches(); |
| 348 | if (!empty($this->_allBatches)) { |
| 349 | $this->_columns['civicrm_batch']['dao'] = 'CRM_Batch_DAO_Batch'; |
| 350 | $this->_columns['civicrm_batch']['fields']['batch_id'] = array( |
| 351 | 'name' => 'id', |
| 352 | 'title' => ts('Batch Name'), |
| 353 | ); |
| 354 | $this->_columns['civicrm_batch']['filters']['bid'] = array( |
| 355 | 'name' => 'id', |
| 356 | 'title' => ts('Batch Name'), |
| 357 | 'type' => CRM_Utils_Type::T_INT, |
| 358 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
| 359 | 'options' => $this->_allBatches, |
| 360 | ); |
| 361 | $this->_columns['civicrm_entity_batch']['dao'] = 'CRM_Batch_DAO_EntityBatch'; |
| 362 | $this->_columns['civicrm_entity_batch']['fields']['entity_batch_id'] = array( |
| 363 | 'name' => 'batch_id', |
| 364 | 'default' => TRUE, |
| 365 | 'no_display' => TRUE, |
| 366 | ); |
| 367 | } |
| 368 | |
| 369 | // If we have active campaigns add those elements to both the fields and filters |
| 370 | if ($campaignEnabled && !empty($this->activeCampaigns)) { |
| 371 | $this->_columns['civicrm_contribution']['fields']['campaign_id'] = array( |
| 372 | 'title' => ts('Campaign'), |
| 373 | 'default' => 'false', |
| 374 | ); |
| 375 | $this->_columns['civicrm_contribution']['filters']['campaign_id'] = array('title' => ts('Campaign'), |
| 376 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
| 377 | 'options' => $this->activeCampaigns, |
| 378 | ); |
| 379 | $this->_columns['civicrm_contribution']['order_bys']['campaign_id'] = array('title' => ts('Campaign')); |
| 380 | } |
| 381 | |
| 382 | $this->_currencyColumn = 'civicrm_contribution_currency'; |
| 383 | parent::__construct(); |
| 384 | } |
| 385 | |
| 386 | function preProcess() { |
| 387 | parent::preProcess(); |
| 388 | } |
| 389 | |
| 390 | function select() { |
| 391 | $this->_columnHeaders = array(); |
| 392 | foreach ($this->_columns as $tableName => $table) { |
| 393 | if (array_key_exists('fields', $table)) { |
| 394 | foreach ($table['fields'] as $fieldName => $field) { |
| 395 | if (CRM_Utils_Array::value('required', $field) || |
| 396 | CRM_Utils_Array::value($fieldName, $this->_params['fields']) |
| 397 | ) { |
| 398 | if ($tableName == 'civicrm_email_honor') { |
| 399 | $this->_emailFieldHonor = TRUE; |
| 400 | } |
| 401 | if ($tableName == 'civicrm_contact_honor') { |
| 402 | $this->_nameFieldHonor = TRUE; |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | parent::select(); |
| 410 | } |
| 411 | |
| 412 | function orderBy() { |
| 413 | parent::orderBy(); |
| 414 | |
| 415 | // please note this will just add the order-by columns to select query, and not display in column-headers. |
| 416 | // This is a solution to not throw fatal errors when there is a column in order-by, not present in select/display columns. |
| 417 | foreach ($this->_orderByFields as $orderBy) { |
| 418 | if (!array_key_exists($orderBy['name'], $this->_params['fields']) |
| 419 | && !CRM_Utils_Array::value('section', $orderBy)) { |
| 420 | $this->_select .= ", {$orderBy['dbAlias']} as {$orderBy['tplField']}"; |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | function from($softcredit = FALSE) { |
| 426 | $this->_from = " |
| 427 | FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom} |
| 428 | INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']} |
| 429 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id AND {$this->_aliases['civicrm_contribution']}.is_test = 0"; |
| 430 | if (CRM_Utils_Array::value('contribution_or_soft_value', $this->_params) == 'soft_credits_only') { |
| 431 | $this->_from .= " |
| 432 | INNER JOIN civicrm_contribution_soft contribution_soft_civireport |
| 433 | ON contribution_soft_civireport.contribution_id = {$this->_aliases['civicrm_contribution']}.id"; |
| 434 | } |
| 435 | |
| 436 | if ($softcredit) { |
| 437 | $this->_from = " |
| 438 | FROM civireport_contribution_detail_temp1 temp1_civireport |
| 439 | INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']} |
| 440 | ON temp1_civireport.civicrm_contribution_contribution_id = {$this->_aliases['civicrm_contribution']}.id |
| 441 | INNER JOIN civicrm_contribution_soft contribution_soft_civireport |
| 442 | ON contribution_soft_civireport.contribution_id = {$this->_aliases['civicrm_contribution']}.id |
| 443 | INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom} |
| 444 | ON {$this->_aliases['civicrm_contact']}.id = contribution_soft_civireport.contact_id"; |
| 445 | } |
| 446 | |
| 447 | if (!empty($this->_params['ordinality_value'])) { |
| 448 | $this->_from .= " |
| 449 | INNER JOIN (SELECT c.id, IF(COUNT(oc.id) = 0, 0, 1) AS ordinality FROM civicrm_contribution c LEFT JOIN civicrm_contribution oc ON c.contact_id = oc.contact_id AND oc.receive_date < c.receive_date GROUP BY c.id) {$this->_aliases['civicrm_contribution_ordinality']} |
| 450 | ON {$this->_aliases['civicrm_contribution_ordinality']}.id = {$this->_aliases['civicrm_contribution']}.id"; |
| 451 | } |
| 452 | |
| 453 | $this->addPhoneFromClause(); |
| 454 | |
| 455 | if ($this->_addressField OR (!empty($this->_params['state_province_id_value']) OR !empty($this->_params['country_id_value']))) { |
| 456 | $this->_from .= " |
| 457 | LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']} |
| 458 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND |
| 459 | {$this->_aliases['civicrm_address']}.is_primary = 1\n"; |
| 460 | } |
| 461 | |
| 462 | if ($this->_emailField) { |
| 463 | $this->_from .= " |
| 464 | LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']} |
| 465 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND |
| 466 | {$this->_aliases['civicrm_email']}.is_primary = 1\n"; |
| 467 | } |
| 468 | |
| 469 | // include Honor name field |
| 470 | if ($this->_nameFieldHonor) { |
| 471 | $this->_from .= " |
| 472 | LEFT JOIN civicrm_contact contacthonor |
| 473 | ON contacthonor.id = {$this->_aliases['civicrm_contribution']}.honor_contact_id"; |
| 474 | } |
| 475 | // include Honor email field |
| 476 | if ($this->_emailFieldHonor) { |
| 477 | $this->_from .= " |
| 478 | LEFT JOIN civicrm_email emailhonor |
| 479 | ON emailhonor.contact_id = {$this->_aliases['civicrm_contribution']}.honor_contact_id |
| 480 | AND emailhonor.is_primary = 1\n"; |
| 481 | } |
| 482 | // include contribution note |
| 483 | if (CRM_Utils_Array::value('contribution_note', $this->_params['fields']) || |
| 484 | CRM_Utils_Array::value('note_value', $this->_params)) { |
| 485 | $this->_from.= " |
| 486 | LEFT JOIN civicrm_note {$this->_aliases['civicrm_note']} |
| 487 | ON ( {$this->_aliases['civicrm_note']}.entity_table = 'civicrm_contribution' AND |
| 488 | {$this->_aliases['civicrm_contribution']}.id = {$this->_aliases['civicrm_note']}.entity_id )"; |
| 489 | } |
| 490 | //for contribution batches |
| 491 | if ($this->_allBatches && |
| 492 | (CRM_Utils_Array::value('batch_id', $this->_params['fields']) || !empty($this->_params['bid_value']))) { |
| 493 | $this->_from .= " |
| 494 | LEFT JOIN civicrm_entity_financial_trxn tx ON (tx.entity_id = {$this->_aliases['civicrm_contribution']}.id AND |
| 495 | tx.entity_table = 'civicrm_contribution') |
| 496 | LEFT JOIN civicrm_entity_batch {$this->_aliases['civicrm_entity_batch']} |
| 497 | ON ({$this->_aliases['civicrm_entity_batch']}.entity_id = tx.financial_trxn_id AND |
| 498 | {$this->_aliases['civicrm_entity_batch']}.entity_table = 'civicrm_financial_trxn') |
| 499 | LEFT JOIN civicrm_batch {$this->_aliases['civicrm_batch']} |
| 500 | ON {$this->_aliases['civicrm_batch']}.id = {$this->_aliases['civicrm_entity_batch']}.batch_id"; |
| 501 | } |
| 502 | |
| 503 | } |
| 504 | |
| 505 | function groupBy() { |
| 506 | $this->_groupBy = " GROUP BY {$this->_aliases['civicrm_contact']}.id, {$this->_aliases['civicrm_contribution']}.id "; |
| 507 | } |
| 508 | |
| 509 | function statistics(&$rows) { |
| 510 | $statistics = parent::statistics($rows); |
| 511 | |
| 512 | $totalAmount = $average = array(); |
| 513 | $count = 0; |
| 514 | $select = " |
| 515 | SELECT COUNT({$this->_aliases['civicrm_contribution']}.total_amount ) as count, |
| 516 | SUM( {$this->_aliases['civicrm_contribution']}.total_amount ) as amount, |
| 517 | ROUND(AVG({$this->_aliases['civicrm_contribution']}.total_amount), 2) as avg, |
| 518 | {$this->_aliases['civicrm_contribution']}.currency as currency |
| 519 | "; |
| 520 | |
| 521 | $group = "\nGROUP BY {$this->_aliases['civicrm_contribution']}.currency"; |
| 522 | $sql = "{$select} {$this->_from} {$this->_where} {$group}"; |
| 523 | $dao = CRM_Core_DAO::executeQuery($sql); |
| 524 | |
| 525 | while ($dao->fetch()) { |
| 526 | $totalAmount[] = CRM_Utils_Money::format($dao->amount, $dao->currency)." (".$dao->count.")"; |
| 527 | $average[] = CRM_Utils_Money::format($dao->avg, $dao->currency); |
| 528 | $count += $dao->count; |
| 529 | } |
| 530 | $statistics['counts']['amount'] = array( |
| 531 | 'title' => ts('Total Amount (Donations)'), |
| 532 | 'value' => implode(', ', $totalAmount), |
| 533 | 'type' => CRM_Utils_Type::T_STRING, |
| 534 | ); |
| 535 | $statistics['counts']['count'] = array( |
| 536 | 'title' => ts('Total Donations'), |
| 537 | 'value' => $count, |
| 538 | ); |
| 539 | $statistics['counts']['avg'] = array( |
| 540 | 'title' => ts('Average'), |
| 541 | 'value' => implode(', ', $average), |
| 542 | 'type' => CRM_Utils_Type::T_STRING, |
| 543 | ); |
| 544 | |
| 545 | // Stats for soft credits |
| 546 | if ($this->_softFrom && CRM_Utils_Array::value('contribution_or_soft_value', $this->_params) != 'contributions_only') { |
| 547 | $totalAmount = $average = array(); |
| 548 | $count = 0; |
| 549 | $select = " |
| 550 | SELECT COUNT(contribution_soft_civireport.amount ) as count, |
| 551 | SUM(contribution_soft_civireport.amount ) as amount, |
| 552 | ROUND(AVG(contribution_soft_civireport.amount), 2) as avg, |
| 553 | {$this->_aliases['civicrm_contribution']}.currency as currency"; |
| 554 | $sql = " |
| 555 | {$select} |
| 556 | {$this->_softFrom} |
| 557 | GROUP BY {$this->_aliases['civicrm_contribution']}.currency"; |
| 558 | $dao = CRM_Core_DAO::executeQuery($sql); |
| 559 | while ($dao->fetch()) { |
| 560 | $totalAmount[] = CRM_Utils_Money::format($dao->amount, $dao->currency)." (".$dao->count.")"; |
| 561 | $average[] = CRM_Utils_Money::format($dao->avg, $dao->currency); |
| 562 | $count += $dao->count; |
| 563 | } |
| 564 | $statistics['counts']['softamount'] = array( |
| 565 | 'title' => ts('Total Amount (Soft Credits)'), |
| 566 | 'value' => implode(', ', $totalAmount), |
| 567 | 'type' => CRM_Utils_Type::T_STRING, |
| 568 | ); |
| 569 | $statistics['counts']['softcount'] = array( |
| 570 | 'title' => ts('Total Soft Credits'), |
| 571 | 'value' => $count, |
| 572 | ); |
| 573 | $statistics['counts']['softavg'] = array( |
| 574 | 'title' => ts('Average (Soft Credits)'), |
| 575 | 'value' => implode(', ', $average), |
| 576 | 'type' => CRM_Utils_Type::T_STRING, |
| 577 | ); |
| 578 | } |
| 579 | |
| 580 | return $statistics; |
| 581 | } |
| 582 | |
| 583 | function postProcess() { |
| 584 | // get the acl clauses built before we assemble the query |
| 585 | $this->buildACLClause($this->_aliases['civicrm_contact']); |
| 586 | |
| 587 | $this->beginPostProcess(); |
| 588 | |
| 589 | // 1. use main contribution query to build temp table 1 |
| 590 | $sql = $this->buildQuery(); |
| 591 | $tempQuery = 'CREATE TEMPORARY TABLE civireport_contribution_detail_temp1 AS ' . $sql; |
| 592 | CRM_Core_DAO::executeQuery($tempQuery); |
| 593 | $this->setPager(); |
| 594 | |
| 595 | // 2. customize main contribution query for soft credit, and build temp table 2 with soft credit contributions only |
| 596 | $this->from(TRUE); |
| 597 | // also include custom group from if included |
| 598 | // since this might be included in select |
| 599 | $this->customDataFrom(); |
| 600 | |
| 601 | $select = str_ireplace('contribution_civireport.total_amount', 'contribution_soft_civireport.amount', $this->_select); |
| 602 | $select = str_ireplace("'Contribution' as", "'Soft Credit' as", $select); |
| 603 | // we inner join with temp1 to restrict soft contributions to those in temp1 table |
| 604 | $sql = "{$select} {$this->_from} {$this->_groupBy}"; |
| 605 | $tempQuery = 'CREATE TEMPORARY TABLE civireport_contribution_detail_temp2 AS ' . $sql; |
| 606 | CRM_Core_DAO::executeQuery($tempQuery); |
| 607 | if (CRM_Utils_Array::value('contribution_or_soft_value', $this->_params) == 'soft_credits_only') { |
| 608 | // revise pager : prev, next based on soft-credits only |
| 609 | $this->setPager(); |
| 610 | } |
| 611 | |
| 612 | // copy _from for later use of stats calculation for soft credits, and reset $this->_from to main query |
| 613 | $this->_softFrom = $this->_from; |
| 614 | |
| 615 | // simple reset of ->_from |
| 616 | $this->from(); |
| 617 | |
| 618 | // also include custom group from if included |
| 619 | // since this might be included in select |
| 620 | $this->customDataFrom(); |
| 621 | |
| 622 | // 3. Decide where to populate temp3 table from |
| 623 | if (CRM_Utils_Array::value('contribution_or_soft_value', $this->_params) == 'contributions_only') { |
| 624 | $tempQuery = "(SELECT * FROM civireport_contribution_detail_temp1)"; |
| 625 | } else if (CRM_Utils_Array::value('contribution_or_soft_value', $this->_params) == 'soft_credits_only') { |
| 626 | $tempQuery = "(SELECT * FROM civireport_contribution_detail_temp2)"; |
| 627 | } else { |
| 628 | $tempQuery = " |
| 629 | (SELECT * FROM civireport_contribution_detail_temp1) |
| 630 | UNION ALL |
| 631 | (SELECT * FROM civireport_contribution_detail_temp2)"; |
| 632 | } |
| 633 | |
| 634 | // 4. build temp table 3 |
| 635 | $sql = "CREATE TEMPORARY TABLE civireport_contribution_detail_temp3 AS {$tempQuery}"; |
| 636 | CRM_Core_DAO::executeQuery($sql); |
| 637 | |
| 638 | // 5. Re-construct order-by to make sense for final query on temp3 table |
| 639 | $orderBy = ''; |
| 640 | if (!empty($this->_orderByArray)) { |
| 641 | $aliases = array_flip($this->_aliases); |
| 642 | $orderClause = array(); |
| 643 | foreach ($this->_orderByArray as $clause) { |
| 644 | list($alias, $rest) = explode('.', $clause); |
| 645 | $orderClause[] = $aliases[$alias] . "_" . $rest; |
| 646 | } |
| 647 | $orderBy = (!empty($orderClause)) ? "ORDER BY " . implode(', ', $orderClause) : ''; |
| 648 | } |
| 649 | |
| 650 | // 6. show result set from temp table 3 |
| 651 | $rows = array(); |
| 652 | $sql = "SELECT * FROM civireport_contribution_detail_temp3 {$orderBy}"; |
| 653 | $this->buildRows($sql, $rows); |
| 654 | |
| 655 | // format result set. |
| 656 | $this->formatDisplay($rows, FALSE); |
| 657 | |
| 658 | // assign variables to templates |
| 659 | $this->doTemplateAssignment($rows); |
| 660 | |
| 661 | // do print / pdf / instance stuff if needed |
| 662 | $this->endPostProcess($rows); |
| 663 | } |
| 664 | |
| 665 | function alterDisplay(&$rows) { |
| 666 | // custom code to alter rows |
| 667 | $checkList = array(); |
| 668 | $entryFound = FALSE; |
| 669 | $display_flag = $prev_cid = $cid = 0; |
| 670 | $contributionTypes = CRM_Contribute_PseudoConstant::financialType(); |
| 671 | $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(); |
| 672 | $paymentInstruments = CRM_Contribute_PseudoConstant::paymentInstrument(); |
| 673 | $contributionPages = CRM_Contribute_PseudoConstant::contributionPage(); |
| 674 | $honorTypes = CRM_Core_OptionGroup::values('honor_type', FALSE, FALSE, FALSE, NULL, 'label'); |
| 675 | |
| 676 | |
| 677 | foreach ($rows as $rowNum => $row) { |
| 678 | if (!empty($this->_noRepeats) && $this->_outputMode != 'csv') { |
| 679 | // don't repeat contact details if its same as the previous row |
| 680 | if (array_key_exists('civicrm_contact_id', $row)) { |
| 681 | if ($cid = $row['civicrm_contact_id']) { |
| 682 | if ($rowNum == 0) { |
| 683 | $prev_cid = $cid; |
| 684 | } |
| 685 | else { |
| 686 | if ($prev_cid == $cid) { |
| 687 | $display_flag = 1; |
| 688 | $prev_cid = $cid; |
| 689 | } |
| 690 | else { |
| 691 | $display_flag = 0; |
| 692 | $prev_cid = $cid; |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | if ($display_flag) { |
| 697 | foreach ($row as $colName => $colVal) { |
| 698 | // Hide repeats in no-repeat columns, but not if the field's a section header |
| 699 | if (in_array($colName, $this->_noRepeats) && !array_key_exists($colName, $this->_sections)) { |
| 700 | unset($rows[$rowNum][$colName]); |
| 701 | } |
| 702 | } |
| 703 | } |
| 704 | $entryFound = TRUE; |
| 705 | } |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | |
| 710 | |
| 711 | // convert donor sort name to link |
| 712 | if (array_key_exists('civicrm_contact_sort_name', $row) && |
| 713 | CRM_Utils_Array::value('civicrm_contact_sort_name', $rows[$rowNum]) && |
| 714 | array_key_exists('civicrm_contact_id', $row) |
| 715 | ) { |
| 716 | $url = CRM_Utils_System::url("civicrm/contact/view", |
| 717 | 'reset=1&cid=' . $row['civicrm_contact_id'], |
| 718 | $this->_absoluteUrl |
| 719 | ); |
| 720 | $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url; |
| 721 | $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact Summary for this Contact."); |
| 722 | } |
| 723 | |
| 724 | // convert honoree sort name to link |
| 725 | if (array_key_exists('civicrm_contact_honor_sort_name_honor', $row) && |
| 726 | CRM_Utils_Array::value('civicrm_contact_honor_sort_name_honor', $rows[$rowNum]) && |
| 727 | array_key_exists('civicrm_contact_honor_id_honor', $row) |
| 728 | ) { |
| 729 | |
| 730 | $url = CRM_Utils_System::url("civicrm/contact/view", |
| 731 | 'reset=1&cid=' . $row['civicrm_contact_honor_id_honor'], |
| 732 | $this->_absoluteUrl |
| 733 | ); |
| 734 | $rows[$rowNum]['civicrm_contact_honor_sort_name_honor_link'] = $url; |
| 735 | $rows[$rowNum]['civicrm_contact_honor_sort_name_honor_hover'] = ts("View Contact Summary for Honoree."); |
| 736 | } |
| 737 | |
| 738 | if ($value = CRM_Utils_Array::value('civicrm_contribution_financial_type_id', $row)) { |
| 739 | $rows[$rowNum]['civicrm_contribution_financial_type_id'] = $contributionTypes[$value]; |
| 740 | $entryFound = TRUE; |
| 741 | } |
| 742 | if ($value = CRM_Utils_Array::value('civicrm_contribution_contribution_status_id', $row)) { |
| 743 | $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = $contributionStatus[$value]; |
| 744 | $entryFound = TRUE; |
| 745 | } |
| 746 | if ($value = CRM_Utils_Array::value('civicrm_contribution_contribution_page_id', $row)) { |
| 747 | $rows[$rowNum]['civicrm_contribution_contribution_page_id'] = $contributionPages[$value]; |
| 748 | $entryFound = TRUE; |
| 749 | } |
| 750 | if ($value = CRM_Utils_Array::value('civicrm_contribution_payment_instrument_id', $row)) { |
| 751 | $rows[$rowNum]['civicrm_contribution_payment_instrument_id'] = $paymentInstruments[$value]; |
| 752 | $entryFound = TRUE; |
| 753 | } |
| 754 | if ($value = CRM_Utils_Array::value('civicrm_contribution_honor_type_id', $row)) { |
| 755 | $rows[$rowNum]['civicrm_contribution_honor_type_id'] = $honorTypes[$value]; |
| 756 | $entryFound = TRUE; |
| 757 | } |
| 758 | if (array_key_exists('civicrm_batch_batch_id', $row)) { |
| 759 | if ($value = $row['civicrm_batch_batch_id']) { |
| 760 | $rows[$rowNum]['civicrm_batch_batch_id'] = CRM_Core_DAO::getFieldValue('CRM_Batch_DAO_Batch', $value, 'title'); |
| 761 | } |
| 762 | $entryFound = TRUE; |
| 763 | } |
| 764 | |
| 765 | // Contribution amount links to viewing contribution |
| 766 | if (($value = CRM_Utils_Array::value('civicrm_contribution_total_amount_sum', $row)) && |
| 767 | CRM_Core_Permission::check('access CiviContribute') |
| 768 | ) { |
| 769 | $url = CRM_Utils_System::url("civicrm/contact/view/contribution", |
| 770 | "reset=1&id=" . $row['civicrm_contribution_contribution_id'] . "&cid=" . $row['civicrm_contact_id'] . "&action=view&context=contribution&selectedChild=contribute", |
| 771 | $this->_absoluteUrl |
| 772 | ); |
| 773 | $rows[$rowNum]['civicrm_contribution_total_amount_sum_link'] = $url; |
| 774 | $rows[$rowNum]['civicrm_contribution_total_amount_sum_hover'] = ts("View Details of this Contribution."); |
| 775 | $entryFound = TRUE; |
| 776 | } |
| 777 | |
| 778 | // convert campaign_id to campaign title |
| 779 | if (array_key_exists('civicrm_contribution_campaign_id', $row)) { |
| 780 | if ($value = $row['civicrm_contribution_campaign_id']) { |
| 781 | $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns[$value]; |
| 782 | $entryFound = TRUE; |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | // soft credits |
| 787 | if (array_key_exists('civicrm_contribution_soft_credits', $row) && |
| 788 | 'Contribution' == CRM_Utils_Array::value('civicrm_contribution_contribution_or_soft', $rows[$rowNum]) && |
| 789 | array_key_exists('civicrm_contribution_contribution_id', $row) |
| 790 | ) { |
| 791 | $query = " |
| 792 | SELECT civicrm_contact_id, civicrm_contact_sort_name, civicrm_contribution_total_amount_sum, civicrm_contribution_currency |
| 793 | FROM civireport_contribution_detail_temp2 |
| 794 | WHERE civicrm_contribution_contribution_id={$row['civicrm_contribution_contribution_id']}"; |
| 795 | $dao = CRM_Core_DAO::executeQuery($query); |
| 796 | $string = ''; |
| 797 | $separator = ($this->_outputMode !== 'csv') ? "<br/>" : ' '; |
| 798 | while ($dao->fetch()) { |
| 799 | $url = CRM_Utils_System::url("civicrm/contact/view", 'reset=1&cid=' . $dao->civicrm_contact_id); |
| 800 | $string = $string . ($string ? $separator : '') . "<a href='{$url}'>{$dao->civicrm_contact_sort_name}</a> " . |
| 801 | CRM_Utils_Money::format($dao->civicrm_contribution_total_amount_sum, $dao->civicrm_contribution_currency); |
| 802 | } |
| 803 | $rows[$rowNum]['civicrm_contribution_soft_credits'] = $string; |
| 804 | } |
| 805 | |
| 806 | if (array_key_exists('civicrm_contribution_soft_credit_for', $row) && |
| 807 | 'Soft Credit' == CRM_Utils_Array::value('civicrm_contribution_contribution_or_soft', $rows[$rowNum]) && |
| 808 | array_key_exists('civicrm_contribution_contribution_id', $row) |
| 809 | ) { |
| 810 | $query = " |
| 811 | SELECT civicrm_contact_id, civicrm_contact_sort_name |
| 812 | FROM civireport_contribution_detail_temp1 |
| 813 | WHERE civicrm_contribution_contribution_id={$row['civicrm_contribution_contribution_id']}"; |
| 814 | $dao = CRM_Core_DAO::executeQuery($query); |
| 815 | $string = ''; |
| 816 | while ($dao->fetch()) { |
| 817 | $url = CRM_Utils_System::url("civicrm/contact/view", 'reset=1&cid=' . $dao->civicrm_contact_id); |
| 818 | $string = $string . "\n<a href='{$url}'>{$dao->civicrm_contact_sort_name}</a>"; |
| 819 | } |
| 820 | $rows[$rowNum]['civicrm_contribution_soft_credit_for'] = $string; |
| 821 | } |
| 822 | |
| 823 | $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'contribute/detail', 'List all contribution(s) for this ') ? TRUE : $entryFound; |
| 824 | |
| 825 | // skip looking further in rows, if first row itself doesn't |
| 826 | // have the column we need |
| 827 | if (!$entryFound) { |
| 828 | break; |
| 829 | } |
| 830 | $lastKey = $rowNum; |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | function sectionTotals( ) { |
| 835 | |
| 836 | // Reports using order_bys with sections must populate $this->_selectAliases in select() method. |
| 837 | if (empty($this->_selectAliases)) { |
| 838 | return; |
| 839 | } |
| 840 | |
| 841 | if (!empty($this->_sections)) { |
| 842 | // build the query with no LIMIT clause |
| 843 | $select = str_ireplace( 'SELECT SQL_CALC_FOUND_ROWS ', 'SELECT ', $this->_select ); |
| 844 | $sql = "{$select} {$this->_from} {$this->_where} {$this->_groupBy} {$this->_having} {$this->_orderBy}"; |
| 845 | |
| 846 | // pull section aliases out of $this->_sections |
| 847 | $sectionAliases = array_keys($this->_sections); |
| 848 | |
| 849 | $ifnulls = array(); |
| 850 | foreach (array_merge($sectionAliases, $this->_selectAliases) as $alias) { |
| 851 | $ifnulls[] = "ifnull($alias, '') as $alias"; |
| 852 | } |
| 853 | |
| 854 | /* Group (un-limited) report by all aliases and get counts. This might |
| 855 | * be done more efficiently when the contents of $sql are known, ie. by |
| 856 | * overriding this method in the report class. |
| 857 | */ |
| 858 | |
| 859 | $addtotals = ''; |
| 860 | |
| 861 | if (array_search("civicrm_contribution_total_amount_sum", $this->_selectAliases) !== FALSE) { |
| 862 | $addtotals = ", sum(civicrm_contribution_total_amount_sum) as sumcontribs"; |
| 863 | $showsumcontribs = TRUE; |
| 864 | } |
| 865 | |
| 866 | $query = "select " |
| 867 | . implode(", ", $ifnulls) |
| 868 | ."$addtotals, count(*) as ct from civireport_contribution_detail_temp3 group by ". implode(", ", $sectionAliases); |
| 869 | // initialize array of total counts |
| 870 | $sumcontribs = $totals = array(); |
| 871 | $dao = CRM_Core_DAO::executeQuery($query); |
| 872 | while ($dao->fetch()) { |
| 873 | |
| 874 | // let $this->_alterDisplay translate any integer ids to human-readable values. |
| 875 | $rows[0] = $dao->toArray(); |
| 876 | $this->alterDisplay($rows); |
| 877 | $row = $rows[0]; |
| 878 | |
| 879 | // add totals for all permutations of section values |
| 880 | $values = array(); |
| 881 | $i = 1; |
| 882 | $aliasCount = count($sectionAliases); |
| 883 | foreach ($sectionAliases as $alias) { |
| 884 | $values[] = $row[$alias]; |
| 885 | $key = implode(CRM_Core_DAO::VALUE_SEPARATOR, $values); |
| 886 | if ($i == $aliasCount) { |
| 887 | // the last alias is the lowest-level section header; use count as-is |
| 888 | $totals[$key] = $dao->ct; |
| 889 | if ($showsumcontribs) { $sumcontribs[$key] = $dao->sumcontribs; } |
| 890 | } |
| 891 | else { |
| 892 | // other aliases are higher level; roll count into their total |
| 893 | $totals[$key] = (array_key_exists($key, $totals)) ? $totals[$key] + $dao->ct : $dao->ct; |
| 894 | if ($showsumcontribs) { |
| 895 | $sumcontribs[$key] = array_key_exists($key, $sumcontribs) ? $sumcontribs[$key] + $dao->sumcontribs : $dao->sumcontribs; |
| 896 | } |
| 897 | } |
| 898 | } |
| 899 | } |
| 900 | if ($showsumcontribs) { |
| 901 | $totalandsum = array(); |
| 902 | // ts exception to avoid having ts("%1 %2: %3") |
| 903 | $title = '%1 contributions / soft-credits: %2'; |
| 904 | |
| 905 | if (CRM_Utils_Array::value('contribution_or_soft_value', $this->_params) == 'contributions_only') { |
| 906 | $title = '%1 contributions: %2'; |
| 907 | } else if (CRM_Utils_Array::value('contribution_or_soft_value', $this->_params) == 'soft_credits_only') { |
| 908 | $title = '%1 soft-credits: %2'; |
| 909 | } |
| 910 | foreach ($totals as $key => $total) { |
| 911 | $totalandsum[$key] = ts($title, array( |
| 912 | 1 => $total, |
| 913 | 2 => CRM_Utils_Money::format($sumcontribs[$key]) |
| 914 | )); |
| 915 | } |
| 916 | $this->assign('sectionTotals', $totalandsum); |
| 917 | } |
| 918 | else { |
| 919 | $this->assign('sectionTotals', $totals); |
| 920 | } |
| 921 | } |
| 922 | } |
| 923 | } |
| 924 | |