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