Merge pull request #9602 from eileenmcnaughton/triggers
[civicrm-core.git] / CRM / Report / Form / Contribute / Bookkeeping.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
0f03f337 6 | Copyright CiviCRM LLC (c) 2004-2017 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
0f03f337 31 * @copyright CiviCRM LLC (c) 2004-2017
6a488035
TO
32 */
33class CRM_Report_Form_Contribute_Bookkeeping extends CRM_Report_Form {
34 protected $_addressField = FALSE;
35
36 protected $_emailField = FALSE;
37
38 protected $_summary = NULL;
39
40 protected $_customGroupExtends = array(
70bea8e2 41 'Contact',
42 'Individual',
43 'Contribution',
21dfd5f5 44 'Membership',
9d72cede 45 );
2f4c2f5d 46
74cf4551 47 /**
1728e9a0 48 * This report has not been optimised for group filtering.
49 *
50 * The functionality for group filtering has been improved but not
51 * all reports have been adjusted to take care of it. This report has not
52 * and will run an inefficient query until fixed.
53 *
54 * CRM-19170
55 *
56 * @var bool
57 */
58 protected $groupFilterNotOptimised = TRUE;
59
60 /**
61 * Class constructor.
74cf4551 62 */
00be9182 63 public function __construct() {
70bea8e2 64 $this->_autoIncludeIndexedFieldsAsOrderBys = 1;
6a488035 65 $this->_columns = array(
9d72cede 66 'civicrm_contact' => array(
6a488035 67 'dao' => 'CRM_Contact_DAO_Contact',
9d72cede
EM
68 'fields' => array(
69 'sort_name' => array(
70 'title' => ts('Contact Name'),
6a488035
TO
71 'required' => TRUE,
72 'no_repeat' => TRUE,
73 ),
70bea8e2 74 'first_name' => array(
75 'title' => ts('First Name'),
76 ),
77 'middle_name' => array(
78 'title' => ts('Middle Name'),
79 ),
80 'last_name' => array(
81 'title' => ts('Last Name'),
82 ),
9d72cede 83 'id' => array(
6a488035
TO
84 'no_display' => TRUE,
85 'required' => TRUE,
86 ),
70bea8e2 87 'gender_id' => array(
88 'title' => ts('Gender'),
89 ),
90 'birth_date' => array(
91 'title' => ts('Birth Date'),
92 ),
93 'age' => array(
94 'title' => ts('Age'),
95 'dbAlias' => 'TIMESTAMPDIFF(YEAR, contact_civireport.birth_date, CURDATE())',
96 ),
97 'contact_type' => array(
98 'title' => ts('Contact Type'),
99 ),
100 'contact_sub_type' => array(
101 'title' => ts('Contact Subtype'),
102 ),
103 ),
104 'grouping' => 'contact-fields',
105 'order_bys' => array(
106 'sort_name' => array(
107 'title' => ts('Last Name, First Name'),
108 'default' => '1',
109 'default_weight' => '0',
110 'default_order' => 'ASC',
111 ),
112 'first_name' => array(
113 'name' => 'first_name',
114 'title' => ts('First Name'),
115 ),
116 'gender_id' => array(
117 'name' => 'gender_id',
118 'title' => ts('Gender'),
119 ),
120 'birth_date' => array(
121 'name' => 'birth_date',
122 'title' => ts('Birth Date'),
123 ),
9d72cede 124 'contact_type' => array(
30f85891
RN
125 'title' => ts('Contact Type'),
126 ),
9d72cede 127 'contact_sub_type' => array(
b8f96eb8 128 'title' => ts('Contact Subtype'),
30f85891 129 ),
6a488035 130 ),
9d72cede
EM
131 'filters' => array(
132 'sort_name' => array(
133 'title' => ts('Contact Name'),
6a488035
TO
134 'operator' => 'like',
135 ),
9d72cede
EM
136 'id' => array(
137 'title' => ts('Contact ID'),
6a488035
TO
138 'no_display' => TRUE,
139 ),
70bea8e2 140 'gender_id' => array(
141 'title' => ts('Gender'),
142 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
143 'options' => CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'),
144 ),
145 'birth_date' => array(
146 'title' => ts('Birth Date'),
147 'operatorType' => CRM_Report_Form::OP_DATE,
148 ),
149 'contact_type' => array(
150 'title' => ts('Contact Type'),
151 ),
152 'contact_sub_type' => array(
153 'title' => ts('Contact Subtype'),
444e4ce8
JL
154 ),
155 ),
6a488035 156 ),
9d72cede 157 'civicrm_membership' => array(
6a488035 158 'dao' => 'CRM_Member_DAO_Membership',
9d72cede
EM
159 'fields' => array(
160 'id' => array(
161 'title' => ts('Membership #'),
6a488035
TO
162 'no_display' => TRUE,
163 'required' => TRUE,
164 ),
165 ),
166 ),
086ca649
PN
167 'civicrm_financial_account' => array(
168 'dao' => 'CRM_Financial_DAO_FinancialAccount',
169 'fields' => array(
170 'debit_accounting_code' => array(
171 'title' => ts('Financial Account Code - Debit'),
9d72cede 172 'name' => 'accounting_code',
086ca649
PN
173 'alias' => 'financial_account_civireport_debit',
174 'default' => TRUE,
086ca649
PN
175 ),
176 'credit_accounting_code' => array(
177 'title' => ts('Financial Account Code - Credit'),
9d72cede 178 'name' => 'accounting_code',
086ca649
PN
179 'alias' => 'financial_account_civireport_credit',
180 'default' => TRUE,
086ca649
PN
181 ),
182 'debit_name' => array(
183 'title' => ts('Financial Account Name - Debit'),
9d72cede 184 'name' => 'name',
086ca649
PN
185 'alias' => 'financial_account_civireport_debit',
186 'default' => TRUE,
187 ),
188 'credit_name' => array(
189 'title' => ts('Financial Account Name - Credit'),
9d72cede 190 'name' => 'name',
086ca649
PN
191 'alias' => 'financial_account_civireport_credit',
192 'default' => TRUE,
193 ),
194 ),
195 'filters' => array(
196 'debit_accounting_code' => array(
197 'title' => ts('Financial Account Code - Debit'),
198 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
199 'options' => CRM_Contribute_PseudoConstant::financialAccount(NULL, NULL, 'accounting_code', 'accounting_code'),
9d72cede 200 'name' => 'accounting_code',
086ca649
PN
201 'alias' => 'financial_account_civireport_debit',
202 ),
203 'credit_accounting_code' => array(
2f4c2f5d 204 'title' => ts('Financial Account Code - Credit'),
8ee006e7 205 'type' => CRM_Utils_Type::T_INT,
086ca649
PN
206 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
207 'options' => CRM_Contribute_PseudoConstant::financialAccount(NULL, NULL, 'accounting_code', 'accounting_code'),
208 ),
209 'debit_name' => array(
210 'title' => ts('Financial Account Name - Debit'),
8ee006e7 211 'type' => CRM_Utils_Type::T_STRING,
086ca649
PN
212 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
213 'options' => CRM_Contribute_PseudoConstant::financialAccount(),
9d72cede 214 'name' => 'id',
086ca649
PN
215 'alias' => 'financial_account_civireport_debit',
216 ),
217 'credit_name' => array(
2f4c2f5d 218 'title' => ts('Financial Account Name - Credit'),
8ee006e7 219 'type' => CRM_Utils_Type::T_STRING,
086ca649
PN
220 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
221 'options' => CRM_Contribute_PseudoConstant::financialAccount(),
222 ),
223 ),
2f4c2f5d 224 ),
086ca649
PN
225 'civicrm_line_item' => array(
226 'dao' => 'CRM_Price_DAO_LineItem',
227 'fields' => array(
9d72cede
EM
228 'financial_type_id' => array(
229 'title' => ts('Financial Type'),
086ca649
PN
230 'default' => TRUE,
231 ),
232 ),
233 'filters' => array(
2f4c2f5d 234 'financial_type_id' => array(
235 'title' => ts('Financial Type'),
8ee006e7 236 'type' => CRM_Utils_Type::T_INT,
086ca649 237 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
ace6a865 238 'options' => CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes(),
086ca649
PN
239 ),
240 ),
444e4ce8
JL
241 'order_bys' => array(
242 'financial_type_id' => array('title' => ts('Financial Type')),
243 ),
2f4c2f5d 244 ),
d007cefe
L
245 'civicrm_batch' => array(
246 'dao' => 'CRM_Batch_DAO_Batch',
247 'fields' => array(
248 'title' => array(
249 'title' => ts('Batch Title'),
250 'alias' => 'batch',
251 'default' => FALSE,
252 ),
253 'name' => array(
254 'title' => ts('Batch Name'),
255 'alias' => 'batch',
256 'default' => TRUE,
257 ),
258 ),
259 ),
9d72cede 260 'civicrm_contribution' => array(
6a488035 261 'dao' => 'CRM_Contribute_DAO_Contribution',
9d72cede 262 'fields' => array(
6e3c3a7d 263 'receive_date' => array(
21dfd5f5 264 'default' => TRUE,
6a488035 265 ),
6e3c3a7d
PN
266 'invoice_id' => array(
267 'title' => ts('Invoice ID'),
6a488035
TO
268 'default' => TRUE,
269 ),
9d72cede
EM
270 'contribution_status_id' => array(
271 'title' => ts('Contribution Status'),
6a488035
TO
272 'default' => TRUE,
273 ),
9d72cede
EM
274 'id' => array(
275 'title' => ts('Contribution #'),
6a488035
TO
276 'default' => TRUE,
277 ),
278 ),
9d72cede
EM
279 'filters' => array(
280 'receive_date' => array('operatorType' => CRM_Report_Form::OP_DATE),
281 'contribution_status_id' => array(
282 'title' => ts('Contribution Status'),
6a488035
TO
283 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
284 'options' => CRM_Contribute_PseudoConstant::contributionStatus(),
285 'default' => array(1),
286 ),
6a488035 287 ),
444e4ce8
JL
288 'order_bys' => array(
289 'contribution_id' => array('title' => ts('Contribution #')),
290 'contribution_status_id' => array('title' => ts('Contribution Status')),
291 ),
6a488035 292 'grouping' => 'contri-fields',
086ca649 293 ),
4d9dd529
PN
294 'civicrm_financial_trxn' => array(
295 'dao' => 'CRM_Financial_DAO_FinancialTrxn',
296 'fields' => array(
297 'check_number' => array(
298 'title' => ts('Cheque #'),
6a488035
TO
299 'default' => TRUE,
300 ),
9d72cede 301 'payment_instrument_id' => array(
536f0e02 302 'title' => ts('Payment Method'),
4d9dd529
PN
303 'default' => TRUE,
304 ),
652a526e 305 'currency' => array(
9d72cede
EM
306 'required' => TRUE,
307 'no_display' => TRUE,
2f4c2f5d 308 ),
6e3c3a7d
PN
309 'trxn_date' => array(
310 'title' => ts('Transaction Date'),
086ca649
PN
311 'default' => TRUE,
312 'type' => CRM_Utils_Type::T_DATE,
6e3c3a7d
PN
313 ),
314 'trxn_id' => array(
315 'title' => ts('Trans #'),
316 'default' => TRUE,
317 ),
4d9dd529 318 ),
9d72cede 319 'filters' => array(
4d9dd529 320 'payment_instrument_id' => array(
536f0e02 321 'title' => ts('Payment Method'),
8ee006e7 322 'type' => CRM_Utils_Type::T_INT,
4d9dd529
PN
323 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
324 'options' => CRM_Contribute_PseudoConstant::paymentInstrument(),
325 ),
652a526e 326 'currency' => array(
fd6a6828 327 'title' => ts('Currency'),
9d72cede
EM
328 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
329 'options' => CRM_Core_OptionGroup::values('currencies_enabled'),
330 'default' => NULL,
331 'type' => CRM_Utils_Type::T_STRING,
652a526e 332 ),
2f4c2f5d 333 'trxn_date' => array(
6e3c3a7d
PN
334 'title' => ts('Transaction Date'),
335 'operatorType' => CRM_Report_Form::OP_DATE,
336 'type' => CRM_Utils_Type::T_DATE,
337 ),
1552ab09 338 'status_id' => array(
339 'title' => ts('Financial Transaction Status'),
340 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
341 'options' => CRM_Contribute_PseudoConstant::contributionStatus(),
342 'default' => array(1),
343 ),
4d9dd529 344 ),
444e4ce8 345 'order_bys' => array(
536f0e02 346 'payment_instrument_id' => array('title' => ts('Payment Method')),
444e4ce8 347 ),
2f4c2f5d 348 ),
6a488035
TO
349 'civicrm_entity_financial_trxn' => array(
350 'dao' => 'CRM_Financial_DAO_EntityFinancialTrxn',
351 'fields' => array(
352 'amount' => array(
353 'title' => ts('Amount'),
354 'default' => TRUE,
652a526e 355 'type' => CRM_Utils_Type::T_STRING,
6a488035
TO
356 ),
357 ),
9d72cede
EM
358 'filters' => array(
359 'amount' => array('title' => ts('Amount')),
4d9dd529 360 ),
2f4c2f5d 361 ),
6a488035 362 );
444e4ce8 363
16e2e80c 364 $this->_groupFilter = TRUE;
444e4ce8 365 $this->_tagFilter = TRUE;
6a488035
TO
366 parent::__construct();
367 }
368
00be9182 369 public function preProcess() {
6a488035
TO
370 parent::preProcess();
371 }
372
00be9182 373 public function select() {
6a488035
TO
374 $select = array();
375
376 $this->_columnHeaders = array();
377 foreach ($this->_columns as $tableName => $table) {
378 if (array_key_exists('fields', $table)) {
379 foreach ($table['fields'] as $fieldName => $field) {
9d72cede
EM
380 if (!empty($field['required']) ||
381 !empty($this->_params['fields'][$fieldName])
382 ) {
086ca649 383 switch ($fieldName) {
84178120 384 case 'credit_accounting_code':
9d72cede 385 $select[] = " CASE
086ca649
PN
386 WHEN {$this->_aliases['civicrm_financial_trxn']}.from_financial_account_id IS NOT NULL
387 THEN {$this->_aliases['civicrm_financial_account']}_credit_1.accounting_code
388 ELSE {$this->_aliases['civicrm_financial_account']}_credit_2.accounting_code
389 END AS civicrm_financial_account_credit_accounting_code ";
9d72cede 390 break;
84178120
TO
391
392 case 'amount':
9d72cede 393 $select[] = " CASE
086ca649
PN
394 WHEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_id IS NOT NULL
395 THEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.amount
396 ELSE {$this->_aliases['civicrm_entity_financial_trxn']}.amount
397 END AS civicrm_entity_financial_trxn_amount ";
9d72cede 398 break;
84178120
TO
399
400 case 'credit_name':
9d72cede 401 $select[] = " CASE
086ca649
PN
402 WHEN {$this->_aliases['civicrm_financial_trxn']}.from_financial_account_id IS NOT NULL
403 THEN {$this->_aliases['civicrm_financial_account']}_credit_1.name
404 ELSE {$this->_aliases['civicrm_financial_account']}_credit_2.name
405 END AS civicrm_financial_account_credit_name ";
9d72cede 406 break;
84178120
TO
407
408 default:
9d72cede
EM
409 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
410 break;
6a488035
TO
411 }
412 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title'];
413 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
414 }
415 }
416 }
417 }
d1641c51 418 $this->_selectClauses = $select;
6a488035 419
652a526e 420 $this->_select = 'SELECT ' . implode(', ', $select) . ' ';
6a488035
TO
421 }
422
00be9182 423 public function from() {
6a488035
TO
424 $this->_from = NULL;
425
426 $this->_from = "FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
427 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
2f4c2f5d 428 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id AND
6a488035
TO
429 {$this->_aliases['civicrm_contribution']}.is_test = 0
430 LEFT JOIN civicrm_membership_payment payment
086ca649 431 ON ( {$this->_aliases['civicrm_contribution']}.id = payment.contribution_id )
6a488035 432 LEFT JOIN civicrm_membership {$this->_aliases['civicrm_membership']}
2f4c2f5d 433 ON payment.membership_id = {$this->_aliases['civicrm_membership']}.id
6a488035 434 LEFT JOIN civicrm_entity_financial_trxn {$this->_aliases['civicrm_entity_financial_trxn']}
2f4c2f5d 435 ON ({$this->_aliases['civicrm_contribution']}.id = {$this->_aliases['civicrm_entity_financial_trxn']}.entity_id AND
6a488035 436 {$this->_aliases['civicrm_entity_financial_trxn']}.entity_table = 'civicrm_contribution')
4d9dd529
PN
437 LEFT JOIN civicrm_financial_trxn {$this->_aliases['civicrm_financial_trxn']}
438 ON {$this->_aliases['civicrm_financial_trxn']}.id = {$this->_aliases['civicrm_entity_financial_trxn']}.financial_trxn_id
6a488035 439 LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_debit
4d9dd529 440 ON {$this->_aliases['civicrm_financial_trxn']}.to_financial_account_id = {$this->_aliases['civicrm_financial_account']}_debit.id
6a488035 441 LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_credit_1
4d9dd529 442 ON {$this->_aliases['civicrm_financial_trxn']}.from_financial_account_id = {$this->_aliases['civicrm_financial_account']}_credit_1.id
6a488035 443 LEFT JOIN civicrm_entity_financial_trxn {$this->_aliases['civicrm_entity_financial_trxn']}_item
2f4c2f5d 444 ON ({$this->_aliases['civicrm_financial_trxn']}.id = {$this->_aliases['civicrm_entity_financial_trxn']}_item.financial_trxn_id AND
6a488035 445 {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_table = 'civicrm_financial_item')
4d9dd529 446 LEFT JOIN civicrm_financial_item fitem
6a488035 447 ON fitem.id = {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_id
4d9dd529 448 LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_credit_2
6a488035 449 ON fitem.financial_account_id = {$this->_aliases['civicrm_financial_account']}_credit_2.id
4d9dd529 450 LEFT JOIN civicrm_line_item {$this->_aliases['civicrm_line_item']}
6a488035 451 ON fitem.entity_id = {$this->_aliases['civicrm_line_item']}.id AND fitem.entity_table = 'civicrm_line_item' ";
d007cefe
L
452 if ($this->isTableSelected('civicrm_batch')) {
453 $this->_from .= "LEFT JOIN civicrm_entity_batch ent_batch
d1641c51 454 ON {$this->_aliases['civicrm_financial_trxn']}.id = ent_batch.entity_id AND ent_batch.entity_table = 'civicrm_financial_trxn'
d007cefe
L
455 LEFT JOIN civicrm_batch batch
456 ON ent_batch.batch_id = batch.id";
457 }
458
fe32835e 459 $this->getPermissionedFTQuery($this, "civicrm_line_item_1");
6a488035
TO
460 }
461
00be9182 462 public function orderBy() {
444e4ce8
JL
463 parent::orderBy();
464
465 // please note this will just add the order-by columns to select query, and not display in column-headers.
466 // This is a solution to not throw fatal errors when there is a column in order-by, not present in select/display columns.
467 foreach ($this->_orderByFields as $orderBy) {
9d72cede
EM
468 if (!array_key_exists($orderBy['name'], $this->_params['fields']) &&
469 empty($orderBy['section'])
470 ) {
444e4ce8
JL
471 $this->_select .= ", {$orderBy['dbAlias']} as {$orderBy['tplField']}";
472 }
473 }
6a488035
TO
474 }
475
00be9182 476 public function where() {
086ca649
PN
477 foreach ($this->_columns as $tableName => $table) {
478 if (array_key_exists('filters', $table)) {
479 foreach ($table['filters'] as $fieldName => $field) {
480 $clause = NULL;
481 if ($fieldName == 'credit_accounting_code') {
482 $field['dbAlias'] = "CASE
483 WHEN financial_trxn_civireport.from_financial_account_id IS NOT NULL
484 THEN financial_account_civireport_credit_1.accounting_code
2f4c2f5d 485 ELSE financial_account_civireport_credit_2.accounting_code
086ca649
PN
486 END";
487 }
4c9b6178 488 elseif ($fieldName == 'credit_name') {
086ca649
PN
489 $field['dbAlias'] = "CASE
490 WHEN financial_trxn_civireport.from_financial_account_id IS NOT NULL
491 THEN financial_account_civireport_credit_1.id
2f4c2f5d 492 ELSE financial_account_civireport_credit_2.id
086ca649
PN
493 END";
494 }
495 if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) {
496 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
9d72cede
EM
497 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
498 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
086ca649
PN
499
500 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
501 }
502 else {
503 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
504 if ($op) {
505 $clause = $this->whereClause($field,
506 $op,
507 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
508 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
509 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
510 );
511 }
512 }
513 if (!empty($clause)) {
514 $clauses[] = $clause;
515 }
516 }
517 }
518 }
519 if (empty($clauses)) {
652a526e 520 $this->_where = 'WHERE ( 1 )';
086ca649
PN
521 }
522 else {
652a526e 523 $this->_where = 'WHERE ' . implode(' AND ', $clauses);
086ca649
PN
524 }
525 }
526
00be9182 527 public function postProcess() {
6a488035
TO
528 // get the acl clauses built before we assemble the query
529 $this->buildACLClause($this->_aliases['civicrm_contact']);
530 parent::postProcess();
531 }
532
1f557688 533 public function groupBy() {
d1641c51 534 $groupBy = array(
535 "{$this->_aliases['civicrm_entity_financial_trxn']}.id",
dc852c7b 536 "{$this->_aliases['civicrm_line_item']}.id",
d1641c51 537 );
b708c08d 538 $this->_groupBy = CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, $groupBy);
1f557688
SB
539 }
540
74cf4551
EM
541 /**
542 * @param $rows
543 *
544 * @return array
545 */
00be9182 546 public function statistics(&$rows) {
6a488035 547 $statistics = parent::statistics($rows);
1f557688 548 $tempTableName = CRM_Core_DAO::createTempTableName('civicrm_contribution');
dc852c7b 549 $financialSelect = "CASE WHEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_id IS NOT NULL
550 THEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.amount
551 ELSE {$this->_aliases['civicrm_entity_financial_trxn']}.amount
552 END as amount";
d1641c51 553
554 $this->_selectClauses = array(
555 "{$this->_aliases['civicrm_contribution']}.id",
556 "{$this->_aliases['civicrm_entity_financial_trxn']}.id as trxnID",
557 "{$this->_aliases['civicrm_contribution']}.currency",
dc852c7b 558 $financialSelect,
d1641c51 559 );
560 $select = "SELECT " . implode(', ', $this->_selectClauses);
561
562 $this->groupBy();
652a526e 563
a2e4e741 564 $tempQuery = "CREATE TEMPORARY TABLE {$tempTableName} {$this->_databaseAttributes} AS
1f557688
SB
565 {$select} {$this->_from} {$this->_where} {$this->_groupBy} ";
566 CRM_Core_DAO::executeQuery($tempQuery);
6a488035 567
1f557688
SB
568 $sql = "SELECT COUNT(trxnID) as count, SUM(amount) as amount, currency
569 FROM {$tempTableName}
570 GROUP BY currency";
6a488035 571 $dao = CRM_Core_DAO::executeQuery($sql);
6e68fe2a 572 $amount = $avg = array();
652a526e 573 while ($dao->fetch()) {
574 $amount[] = CRM_Utils_Money::format($dao->amount, $dao->currency);
9d72cede 575 $avg[] = CRM_Utils_Money::format(round(($dao->amount /
353ffa53 576 $dao->count), 2), $dao->currency);
6a488035
TO
577 }
578
652a526e 579 $statistics['counts']['amount'] = array(
9d72cede 580 'value' => implode(', ', $amount),
fd6a6828 581 'title' => ts('Total Amount'),
9d72cede 582 'type' => CRM_Utils_Type::T_STRING,
652a526e 583 );
584 $statistics['counts']['avg'] = array(
585 'value' => implode(', ', $avg),
fd6a6828 586 'title' => ts('Average'),
652a526e 587 'type' => CRM_Utils_Type::T_STRING,
588 );
6a488035
TO
589 return $statistics;
590 }
591
74cf4551 592 /**
ced9bfed
EM
593 * Alter display of rows.
594 *
595 * Iterate through the rows retrieved via SQL and make changes for display purposes,
596 * such as rendering contacts as links.
597 *
598 * @param array $rows
599 * Rows generated by SQL, with an array for each row.
74cf4551 600 */
00be9182 601 public function alterDisplay(&$rows) {
6a488035
TO
602 $contributionTypes = CRM_Contribute_PseudoConstant::financialType();
603 $paymentInstruments = CRM_Contribute_PseudoConstant::paymentInstrument();
4d9dd529 604 $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus();
6a488035 605 foreach ($rows as $rowNum => $row) {
6a488035 606 // convert display name to links
9d72cede
EM
607 if (array_key_exists('civicrm_contact_sort_name', $row) &&
608 !empty($rows[$rowNum]['civicrm_contact_sort_name']) &&
6a488035
TO
609 array_key_exists('civicrm_contact_id', $row)
610 ) {
652a526e 611 $url = CRM_Utils_System::url('civicrm/contact/view',
6a488035
TO
612 'reset=1&cid=' . $row['civicrm_contact_id'],
613 $this->_absoluteUrl
614 );
615 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
652a526e 616 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts('View Contact Summary for this Contact.');
6a488035
TO
617 }
618
619 // handle contribution status id
4d9dd529
PN
620 if ($value = CRM_Utils_Array::value('civicrm_contribution_contribution_status_id', $row)) {
621 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = $contributionStatus[$value];
6a488035
TO
622 }
623
624 // handle payment instrument id
4d9dd529
PN
625 if ($value = CRM_Utils_Array::value('civicrm_financial_trxn_payment_instrument_id', $row)) {
626 $rows[$rowNum]['civicrm_financial_trxn_payment_instrument_id'] = $paymentInstruments[$value];
6a488035 627 }
2f4c2f5d 628
4d9dd529 629 // handle financial type id
6a488035
TO
630 if ($value = CRM_Utils_Array::value('civicrm_line_item_financial_type_id', $row)) {
631 $rows[$rowNum]['civicrm_line_item_financial_type_id'] = $contributionTypes[$value];
6a488035 632 }
652a526e 633 if ($value = CRM_Utils_Array::value('civicrm_entity_financial_trxn_amount', $row)) {
9d72cede 634 $rows[$rowNum]['civicrm_entity_financial_trxn_amount'] = CRM_Utils_Money::format($rows[$rowNum]['civicrm_entity_financial_trxn_amount'], $rows[$rowNum]['civicrm_financial_trxn_currency']);
652a526e 635 }
70bea8e2 636
637 //handle gender
638 if (array_key_exists('civicrm_contact_gender_id', $row)) {
639 if ($value = $row['civicrm_contact_gender_id']) {
640 $gender = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id');
641 $rows[$rowNum]['civicrm_contact_gender_id'] = $gender[$value];
642 }
643 $entryFound = TRUE;
644 }
645
646 // display birthday in the configured custom format
647 if (array_key_exists('civicrm_contact_birth_date', $row)) {
648 $birthDate = $row['civicrm_contact_birth_date'];
649 if ($birthDate) {
650 $rows[$rowNum]['civicrm_contact_birth_date'] = CRM_Utils_Date::customFormat($birthDate, '%Y%m%d');
651 }
652 $entryFound = TRUE;
653 }
654
6a488035
TO
655 }
656 }
96025800 657
6a488035 658}