Merge pull request #21514 from mattwire/1624_2319_casedashboard
[civicrm-core.git] / CRM / Report / Form / Contribute / Bookkeeping.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17class CRM_Report_Form_Contribute_Bookkeeping extends CRM_Report_Form {
6a488035
TO
18
19 protected $_summary = NULL;
20
be2fb01f 21 protected $_customGroupExtends = [
70bea8e2 22 'Contact',
23 'Individual',
24 'Contribution',
21dfd5f5 25 'Membership',
be2fb01f 26 ];
2f4c2f5d 27
74cf4551 28 /**
1728e9a0 29 * This report has not been optimised for group filtering.
30 *
31 * The functionality for group filtering has been improved but not
32 * all reports have been adjusted to take care of it. This report has not
33 * and will run an inefficient query until fixed.
34 *
1728e9a0 35 * @var bool
0e480632 36 * @see https://issues.civicrm.org/jira/browse/CRM-19170
1728e9a0 37 */
38 protected $groupFilterNotOptimised = TRUE;
39
40 /**
41 * Class constructor.
74cf4551 42 */
00be9182 43 public function __construct() {
70bea8e2 44 $this->_autoIncludeIndexedFieldsAsOrderBys = 1;
a921a7d4
AF
45 $this->_columns = array_merge(
46 $this->getColumns('Contact', [
47 'order_bys_defaults' => ['sort_name' => 'ASC '],
48 'fields_defaults' => ['sort_name'],
49 'fields_excluded' => ['id'],
50 'fields_required' => ['id'],
51 'filters_defaults' => ['is_deleted' => 0],
52 'no_field_disambiguation' => TRUE,
53 ]),
54 [
55 'civicrm_membership' => [
56 'dao' => 'CRM_Member_DAO_Membership',
57 'fields' => [
58 'id' => [
59 'title' => ts('Membership #'),
60 'no_display' => TRUE,
61 'required' => TRUE,
62 ],
be2fb01f
CW
63 ],
64 ],
a921a7d4
AF
65 'civicrm_financial_account' => [
66 'dao' => 'CRM_Financial_DAO_FinancialAccount',
67 'fields' => [
68 'debit_accounting_code' => [
69 'title' => ts('Financial Account Code - Debit'),
70 'name' => 'accounting_code',
71 'alias' => 'financial_account_civireport_debit',
72 'default' => TRUE,
73 ],
74 'debit_contact_id' => [
75 'title' => ts('Financial Account Owner - Debit'),
76 'name' => 'organization_name',
77 'alias' => 'debit_contact',
78 ],
79 'credit_accounting_code' => [
80 'title' => ts('Financial Account Code - Credit'),
81 'name' => 'accounting_code',
82 'alias' => 'financial_account_civireport_credit',
83 'default' => TRUE,
84 ],
85 'credit_contact_id' => [
86 'title' => ts('Financial Account Owner - Credit'),
87 'name' => 'organization_name',
88 'alias' => 'credit_contact',
89 ],
90 'debit_name' => [
91 'title' => ts('Financial Account Name - Debit'),
92 'name' => 'name',
93 'alias' => 'financial_account_civireport_debit',
94 'default' => TRUE,
95 ],
96 'credit_name' => [
97 'title' => ts('Financial Account Name - Credit'),
98 'name' => 'name',
99 'alias' => 'financial_account_civireport_credit',
100 'default' => TRUE,
101 ],
102 ],
103 'filters' => [
104 'debit_accounting_code' => [
105 'title' => ts('Financial Account Code - Debit'),
106 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
107 'options' => CRM_Contribute_PseudoConstant::financialAccount(NULL, NULL, 'accounting_code', 'accounting_code'),
108 'name' => 'accounting_code',
109 'alias' => 'financial_account_civireport_debit',
110 ],
111 'debit_contact_id' => [
112 'title' => ts('Financial Account Owner - Debit'),
113 'operatorType' => CRM_Report_Form::OP_SELECT,
114 'type' => CRM_Utils_Type::T_INT,
fbaf3c7d 115 'options' => ['' => ts('- Select Organization -')] + CRM_Financial_BAO_FinancialAccount::getOrganizationNames(FALSE),
a921a7d4
AF
116 'name' => 'contact_id',
117 'alias' => 'financial_account_civireport_debit',
118 ],
119 'credit_accounting_code' => [
120 'title' => ts('Financial Account Code - Credit'),
121 'type' => CRM_Utils_Type::T_INT,
122 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
123 'options' => CRM_Contribute_PseudoConstant::financialAccount(NULL, NULL, 'accounting_code', 'accounting_code'),
124 'name' => 'accounting_code',
125 'alias' => 'financial_account_civireport_credit',
126 ],
127 'credit_contact_id' => [
128 'title' => ts('Financial Account Owner - Credit'),
129 'operatorType' => CRM_Report_Form::OP_SELECT,
130 'type' => CRM_Utils_Type::T_INT,
fbaf3c7d 131 'options' => ['' => ts('- Select Organization -')] + CRM_Financial_BAO_FinancialAccount::getOrganizationNames(FALSE),
a921a7d4
AF
132 'name' => 'contact_id',
133 'alias' => 'financial_account_civireport_credit',
134 ],
135 'debit_name' => [
136 'title' => ts('Financial Account Name - Debit'),
137 'type' => CRM_Utils_Type::T_STRING,
138 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
139 'options' => CRM_Contribute_PseudoConstant::financialAccount(),
140 'name' => 'id',
141 'alias' => 'financial_account_civireport_debit',
142 ],
143 'credit_name' => [
144 'title' => ts('Financial Account Name - Credit'),
145 'type' => CRM_Utils_Type::T_STRING,
146 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
147 'options' => CRM_Contribute_PseudoConstant::financialAccount(),
148 'name' => 'id',
149 'alias' => 'financial_account_civireport_credit',
150 ],
be2fb01f
CW
151 ],
152 ],
a921a7d4
AF
153 'civicrm_line_item' => [
154 'dao' => 'CRM_Price_DAO_LineItem',
155 'fields' => [
156 'financial_type_id' => [
157 'title' => ts('Financial Type'),
158 'default' => TRUE,
159 ],
160 ],
161 'filters' => [
162 'financial_type_id' => [
163 'title' => ts('Financial Type'),
164 'type' => CRM_Utils_Type::T_INT,
165 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
166 'options' => CRM_Contribute_BAO_Contribution::buildOptions('financial_type_id', 'search'),
167 ],
168 ],
169 'order_bys' => [
170 'financial_type_id' => ['title' => ts('Financial Type')],
be2fb01f
CW
171 ],
172 ],
a921a7d4
AF
173 'civicrm_batch' => [
174 'dao' => 'CRM_Batch_DAO_Batch',
175 'fields' => [
176 'title' => [
177 'title' => ts('Batch Title'),
178 'alias' => 'batch',
179 'default' => FALSE,
180 ],
181 'name' => [
182 'title' => ts('Batch Name'),
183 'alias' => 'batch',
184 'default' => TRUE,
185 ],
be2fb01f
CW
186 ],
187 ],
a921a7d4
AF
188 'civicrm_contribution' => [
189 'dao' => 'CRM_Contribute_DAO_Contribution',
190 'fields' => [
191 'receive_date' => [
192 'default' => TRUE,
193 ],
194 'invoice_id' => [
195 'title' => ts('Invoice Reference'),
196 'default' => TRUE,
197 ],
198 'invoice_number' => [
199 'title' => ts('Invoice Number'),
200 ],
201 'contribution_status_id' => [
202 'title' => ts('Contribution Status'),
203 'default' => TRUE,
204 ],
205 'contribution_source' => [
206 'title' => ts('Source'),
207 'name' => 'source',
208 ],
209 'id' => [
210 'title' => ts('Contribution ID'),
211 'default' => TRUE,
212 ],
213 ],
214 'filters' => [
215 'contribution_id' => [
216 'title' => ts('Contribution ID'),
217 'name' => 'id',
218 'operatorType' => CRM_Report_Form::OP_INT,
219 'type' => CRM_Utils_Type::T_INT,
220 ],
221 'receive_date' => ['operatorType' => CRM_Report_Form::OP_DATETIME],
222 'receipt_date' => ['operatorType' => CRM_Report_Form::OP_DATETIME],
223 'contribution_source' => [
224 'title' => ts('Source'),
225 'name' => 'source',
226 'type' => CRM_Utils_Type::T_STRING,
227 ],
228 'contribution_status_id' => [
229 'title' => ts('Contribution Status'),
230 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
231 'options' => CRM_Contribute_BAO_Contribution::buildOptions('contribution_status_id', 'search'),
232 'default' => [1],
233 ],
234 ],
235 'order_bys' => [
236 'contribution_id' => ['title' => ts('Contribution #')],
237 'contribution_status_id' => ['title' => ts('Contribution Status')],
238 'receive_date' => ['title' => ts('Date Received')],
239 ],
240 'grouping' => 'contri-fields',
be2fb01f 241 ],
a921a7d4
AF
242 'civicrm_financial_trxn' => [
243 'dao' => 'CRM_Financial_DAO_FinancialTrxn',
244 'fields' => [
245 'check_number' => [
246 'title' => ts('Cheque #'),
247 'default' => TRUE,
248 ],
249 'payment_instrument_id' => [
250 'title' => ts('Payment Method'),
251 'default' => TRUE,
252 ],
253 'currency' => [
254 'required' => TRUE,
255 'no_display' => TRUE,
256 ],
257 'trxn_date' => [
258 'title' => ts('Transaction Date'),
259 'default' => TRUE,
260 'type' => CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME,
261 ],
262 'trxn_id' => [
263 'title' => ts('Trans #'),
264 'default' => TRUE,
265 ],
266 'card_type_id' => [
267 'title' => ts('Credit Card Type'),
268 ],
269 ],
270 'filters' => [
271 'payment_instrument_id' => [
272 'title' => ts('Payment Method'),
273 'type' => CRM_Utils_Type::T_INT,
274 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
275 'options' => CRM_Contribute_PseudoConstant::paymentInstrument(),
276 ],
277 'currency' => [
278 'title' => ts('Currency'),
279 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
280 'options' => CRM_Core_OptionGroup::values('currencies_enabled'),
281 'default' => NULL,
282 'type' => CRM_Utils_Type::T_STRING,
283 ],
284 'trxn_date' => [
285 'title' => ts('Transaction Date'),
286 'operatorType' => CRM_Report_Form::OP_DATETIME,
287 'type' => CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME,
288 ],
289 'status_id' => [
290 'title' => ts('Financial Transaction Status'),
291 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
292 'options' => CRM_Contribute_BAO_Contribution::buildOptions('contribution_status_id', 'search'),
293 'default' => [1],
294 ],
295 'card_type_id' => [
296 'title' => ts('Credit Card Type'),
297 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
298 'options' => CRM_Financial_DAO_FinancialTrxn::buildOptions('card_type_id'),
299 'default' => NULL,
300 'type' => CRM_Utils_Type::T_STRING,
301 ],
302 ],
303 'order_bys' => [
304 'payment_instrument_id' => ['title' => ts('Payment Method')],
305 'trxn_date' => ['title' => ts('Transaction Date')],
be2fb01f
CW
306 ],
307 ],
a921a7d4
AF
308 'civicrm_entity_financial_trxn' => [
309 'dao' => 'CRM_Financial_DAO_EntityFinancialTrxn',
310 'fields' => [
311 'amount' => [
312 'title' => ts('Amount'),
313 'default' => TRUE,
314 'type' => CRM_Utils_Type::T_STRING,
315 ],
316 ],
317 'filters' => [
318 'amount' => ['title' => ts('Amount')],
be2fb01f
CW
319 ],
320 ],
a921a7d4
AF
321 ]
322 );
444e4ce8 323
16e2e80c 324 $this->_groupFilter = TRUE;
444e4ce8 325 $this->_tagFilter = TRUE;
6a488035
TO
326 parent::__construct();
327 }
328
00be9182 329 public function preProcess() {
6a488035
TO
330 parent::preProcess();
331 }
332
00be9182 333 public function select() {
be2fb01f 334 $select = [];
6a488035 335
be2fb01f 336 $this->_columnHeaders = [];
6a488035
TO
337 foreach ($this->_columns as $tableName => $table) {
338 if (array_key_exists('fields', $table)) {
339 foreach ($table['fields'] as $fieldName => $field) {
9d72cede
EM
340 if (!empty($field['required']) ||
341 !empty($this->_params['fields'][$fieldName])
342 ) {
086ca649 343 switch ($fieldName) {
84178120 344 case 'credit_accounting_code':
9cf70da1 345 case 'credit_name':
9d72cede 346 $select[] = " CASE
086ca649 347 WHEN {$this->_aliases['civicrm_financial_trxn']}.from_financial_account_id IS NOT NULL
9cf70da1
PN
348 THEN {$this->_aliases['civicrm_financial_account']}_credit_1.{$field['name']}
349 ELSE {$this->_aliases['civicrm_financial_account']}_credit_2.{$field['name']}
350 END AS civicrm_financial_account_{$fieldName} ";
9d72cede 351 break;
84178120
TO
352
353 case 'amount':
9d72cede 354 $select[] = " CASE
086ca649
PN
355 WHEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_id IS NOT NULL
356 THEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.amount
357 ELSE {$this->_aliases['civicrm_entity_financial_trxn']}.amount
358 END AS civicrm_entity_financial_trxn_amount ";
9d72cede 359 break;
84178120 360
9cf70da1 361 case 'credit_contact_id':
9d72cede 362 $select[] = " CASE
086ca649 363 WHEN {$this->_aliases['civicrm_financial_trxn']}.from_financial_account_id IS NOT NULL
9cf70da1
PN
364 THEN credit_contact_1.{$field['name']}
365 ELSE credit_contact_2.{$field['name']}
366 END AS civicrm_financial_account_{$fieldName} ";
9d72cede 367 break;
84178120
TO
368
369 default:
9d72cede
EM
370 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
371 break;
6a488035
TO
372 }
373 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title'];
9c1bc317 374 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = $field['type'] ?? NULL;
6a488035
TO
375 }
376 }
377 }
378 }
d1641c51 379 $this->_selectClauses = $select;
6a488035 380
652a526e 381 $this->_select = 'SELECT ' . implode(', ', $select) . ' ';
6a488035
TO
382 }
383
00be9182 384 public function from() {
6a488035
TO
385 $this->_from = NULL;
386
387 $this->_from = "FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
388 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
2f4c2f5d 389 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id AND
9f108b4d
JJ
390 {$this->_aliases['civicrm_contribution']}.is_test = 0 AND
391 {$this->_aliases['civicrm_contribution']}.is_template = 0
6a488035 392 LEFT JOIN civicrm_membership_payment payment
086ca649 393 ON ( {$this->_aliases['civicrm_contribution']}.id = payment.contribution_id )
6a488035 394 LEFT JOIN civicrm_membership {$this->_aliases['civicrm_membership']}
2f4c2f5d 395 ON payment.membership_id = {$this->_aliases['civicrm_membership']}.id
6a488035 396 LEFT JOIN civicrm_entity_financial_trxn {$this->_aliases['civicrm_entity_financial_trxn']}
2f4c2f5d 397 ON ({$this->_aliases['civicrm_contribution']}.id = {$this->_aliases['civicrm_entity_financial_trxn']}.entity_id AND
6a488035 398 {$this->_aliases['civicrm_entity_financial_trxn']}.entity_table = 'civicrm_contribution')
4d9dd529
PN
399 LEFT JOIN civicrm_financial_trxn {$this->_aliases['civicrm_financial_trxn']}
400 ON {$this->_aliases['civicrm_financial_trxn']}.id = {$this->_aliases['civicrm_entity_financial_trxn']}.financial_trxn_id
6a488035 401 LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_debit
4d9dd529 402 ON {$this->_aliases['civicrm_financial_trxn']}.to_financial_account_id = {$this->_aliases['civicrm_financial_account']}_debit.id
9cf70da1 403 LEFT JOIN civicrm_contact debit_contact ON {$this->_aliases['civicrm_financial_account']}_debit.contact_id = debit_contact.id
6a488035 404 LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_credit_1
4d9dd529 405 ON {$this->_aliases['civicrm_financial_trxn']}.from_financial_account_id = {$this->_aliases['civicrm_financial_account']}_credit_1.id
9cf70da1 406 LEFT JOIN civicrm_contact credit_contact_1 ON {$this->_aliases['civicrm_financial_account']}_credit_1.contact_id = credit_contact_1.id
6a488035 407 LEFT JOIN civicrm_entity_financial_trxn {$this->_aliases['civicrm_entity_financial_trxn']}_item
2f4c2f5d 408 ON ({$this->_aliases['civicrm_financial_trxn']}.id = {$this->_aliases['civicrm_entity_financial_trxn']}_item.financial_trxn_id AND
6a488035 409 {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_table = 'civicrm_financial_item')
4d9dd529 410 LEFT JOIN civicrm_financial_item fitem
6a488035 411 ON fitem.id = {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_id
4d9dd529 412 LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_credit_2
6a488035 413 ON fitem.financial_account_id = {$this->_aliases['civicrm_financial_account']}_credit_2.id
9cf70da1 414 LEFT JOIN civicrm_contact credit_contact_2 ON {$this->_aliases['civicrm_financial_account']}_credit_2.contact_id = credit_contact_2.id
4d9dd529 415 LEFT JOIN civicrm_line_item {$this->_aliases['civicrm_line_item']}
9cf70da1
PN
416 ON fitem.entity_id = {$this->_aliases['civicrm_line_item']}.id AND fitem.entity_table = 'civicrm_line_item'
417 ";
418
d007cefe
L
419 if ($this->isTableSelected('civicrm_batch')) {
420 $this->_from .= "LEFT JOIN civicrm_entity_batch ent_batch
d1641c51 421 ON {$this->_aliases['civicrm_financial_trxn']}.id = ent_batch.entity_id AND ent_batch.entity_table = 'civicrm_financial_trxn'
d007cefe
L
422 LEFT JOIN civicrm_batch batch
423 ON ent_batch.batch_id = batch.id";
424 }
6a488035
TO
425 }
426
00be9182 427 public function orderBy() {
444e4ce8
JL
428 parent::orderBy();
429
430 // please note this will just add the order-by columns to select query, and not display in column-headers.
431 // This is a solution to not throw fatal errors when there is a column in order-by, not present in select/display columns.
432 foreach ($this->_orderByFields as $orderBy) {
9d72cede
EM
433 if (!array_key_exists($orderBy['name'], $this->_params['fields']) &&
434 empty($orderBy['section'])
435 ) {
444e4ce8
JL
436 $this->_select .= ", {$orderBy['dbAlias']} as {$orderBy['tplField']}";
437 }
438 }
6a488035
TO
439 }
440
2aaedeaa
SP
441 /**
442 * overriding to modify dbAlias for few fields.
443 *
444 * @param array $field Field specifications
445 * @param string $op Query operator (not an exact match to sql)
446 * @param mixed $value
447 * @param float $min
448 * @param float $max
449 *
450 * @return null|string
451 */
452 public function whereClause(&$field, $op, $value, $min, $max) {
453 if ($field['alias'] == 'financial_account_civireport_credit' &&
454 in_array($field['name'], ['accounting_code', 'id', 'contact_id'])
455 ) {
456 $field['dbAlias'] = "CASE
086ca649 457 WHEN financial_trxn_civireport.from_financial_account_id IS NOT NULL
9cf70da1
PN
458 THEN financial_account_civireport_credit_1.{$field['name']}
459 ELSE financial_account_civireport_credit_2.{$field['name']}
086ca649 460 END";
086ca649 461 }
2aaedeaa
SP
462
463 $clause = parent::whereClause($field, $op, $value, $min, $max);
464
465 return $clause;
086ca649
PN
466 }
467
00be9182 468 public function postProcess() {
6a488035
TO
469 // get the acl clauses built before we assemble the query
470 $this->buildACLClause($this->_aliases['civicrm_contact']);
471 parent::postProcess();
472 }
473
1f557688 474 public function groupBy() {
be2fb01f 475 $groupBy = [
d1641c51 476 "{$this->_aliases['civicrm_entity_financial_trxn']}.id",
dc852c7b 477 "{$this->_aliases['civicrm_line_item']}.id",
be2fb01f 478 ];
b708c08d 479 $this->_groupBy = CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, $groupBy);
1f557688
SB
480 }
481
74cf4551 482 /**
71d8f758 483 * @param array $rows
74cf4551
EM
484 *
485 * @return array
486 */
00be9182 487 public function statistics(&$rows) {
6a488035 488 $statistics = parent::statistics($rows);
dc852c7b 489 $financialSelect = "CASE WHEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_id IS NOT NULL
490 THEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.amount
491 ELSE {$this->_aliases['civicrm_entity_financial_trxn']}.amount
492 END as amount";
d1641c51 493
be2fb01f 494 $this->_selectClauses = [
d1641c51 495 "{$this->_aliases['civicrm_contribution']}.id",
496 "{$this->_aliases['civicrm_entity_financial_trxn']}.id as trxnID",
497 "{$this->_aliases['civicrm_contribution']}.currency",
dc852c7b 498 $financialSelect,
be2fb01f 499 ];
d1641c51 500 $select = "SELECT " . implode(', ', $this->_selectClauses);
501
502 $this->groupBy();
652a526e 503
a2472247 504 $tempTableName = $this->createTemporaryTable('tempTable', "
505 {$select} {$this->_from} {$this->_where} {$this->_groupBy} ");
6a488035 506
1f557688
SB
507 $sql = "SELECT COUNT(trxnID) as count, SUM(amount) as amount, currency
508 FROM {$tempTableName}
509 GROUP BY currency";
6a488035 510 $dao = CRM_Core_DAO::executeQuery($sql);
be2fb01f 511 $amount = $avg = [];
652a526e 512 while ($dao->fetch()) {
513 $amount[] = CRM_Utils_Money::format($dao->amount, $dao->currency);
9d72cede 514 $avg[] = CRM_Utils_Money::format(round(($dao->amount /
353ffa53 515 $dao->count), 2), $dao->currency);
6a488035
TO
516 }
517
be2fb01f 518 $statistics['counts']['amount'] = [
9d72cede 519 'value' => implode(', ', $amount),
fd6a6828 520 'title' => ts('Total Amount'),
9d72cede 521 'type' => CRM_Utils_Type::T_STRING,
be2fb01f
CW
522 ];
523 $statistics['counts']['avg'] = [
652a526e 524 'value' => implode(', ', $avg),
fd6a6828 525 'title' => ts('Average'),
652a526e 526 'type' => CRM_Utils_Type::T_STRING,
be2fb01f 527 ];
6a488035
TO
528 return $statistics;
529 }
530
74cf4551 531 /**
ced9bfed
EM
532 * Alter display of rows.
533 *
534 * Iterate through the rows retrieved via SQL and make changes for display purposes,
535 * such as rendering contacts as links.
536 *
537 * @param array $rows
538 * Rows generated by SQL, with an array for each row.
74cf4551 539 */
00be9182 540 public function alterDisplay(&$rows) {
6a488035
TO
541 $contributionTypes = CRM_Contribute_PseudoConstant::financialType();
542 $paymentInstruments = CRM_Contribute_PseudoConstant::paymentInstrument();
c3b82060 543 $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'label');
d72b084a 544 $creditCardTypes = CRM_Financial_DAO_FinancialTrxn::buildOptions('card_type_id');
6a488035 545 foreach ($rows as $rowNum => $row) {
d4d10c58 546 $entryFound = FALSE;
6a488035 547 // convert display name to links
9d72cede
EM
548 if (array_key_exists('civicrm_contact_sort_name', $row) &&
549 !empty($rows[$rowNum]['civicrm_contact_sort_name']) &&
6a488035
TO
550 array_key_exists('civicrm_contact_id', $row)
551 ) {
652a526e 552 $url = CRM_Utils_System::url('civicrm/contact/view',
6a488035
TO
553 'reset=1&cid=' . $row['civicrm_contact_id'],
554 $this->_absoluteUrl
555 );
556 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
652a526e 557 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts('View Contact Summary for this Contact.');
6a488035
TO
558 }
559
560 // handle contribution status id
4d9dd529
PN
561 if ($value = CRM_Utils_Array::value('civicrm_contribution_contribution_status_id', $row)) {
562 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = $contributionStatus[$value];
6a488035
TO
563 }
564
565 // handle payment instrument id
4d9dd529
PN
566 if ($value = CRM_Utils_Array::value('civicrm_financial_trxn_payment_instrument_id', $row)) {
567 $rows[$rowNum]['civicrm_financial_trxn_payment_instrument_id'] = $paymentInstruments[$value];
6a488035 568 }
2f4c2f5d 569
4d9dd529 570 // handle financial type id
6a488035
TO
571 if ($value = CRM_Utils_Array::value('civicrm_line_item_financial_type_id', $row)) {
572 $rows[$rowNum]['civicrm_line_item_financial_type_id'] = $contributionTypes[$value];
6a488035 573 }
652a526e 574 if ($value = CRM_Utils_Array::value('civicrm_entity_financial_trxn_amount', $row)) {
9d72cede 575 $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 576 }
70bea8e2 577
5e0343e8 578 if (!empty($row['civicrm_financial_trxn_card_type_id'])) {
9c1bc317 579 $rows[$rowNum]['civicrm_financial_trxn_card_type_id'] = $creditCardTypes[$row['civicrm_financial_trxn_card_type_id']] ?? NULL;
106bf8fd
E
580 $entryFound = TRUE;
581 }
582
e74fb1c1 583 $entryFound = $this->alterDisplayContactFields($row, $rows, $rowNum, NULL, NULL) ? TRUE : $entryFound;
70bea8e2 584
6a488035
TO
585 }
586 }
96025800 587
6a488035 588}