Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | |
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
06b69b18 | 5 | | CiviCRM version 4.5 | |
6a488035 | 6 | +--------------------------------------------------------------------+ |
06b69b18 | 7 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
8 | +--------------------------------------------------------------------+ |
9 | | This file is a part of CiviCRM. | | |
10 | | | | |
11 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
12 | | under the terms of the GNU Affero General Public License | | |
13 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
14 | | | | |
15 | | CiviCRM is distributed in the hope that it will be useful, but | | |
16 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
17 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
18 | | See the GNU Affero General Public License for more details. | | |
19 | | | | |
20 | | You should have received a copy of the GNU Affero General Public | | |
21 | | License and the CiviCRM Licensing Exception along | | |
22 | | with this program; if not, contact CiviCRM LLC | | |
23 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
24 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
25 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
26 | +--------------------------------------------------------------------+ | |
27 | */ | |
28 | ||
29 | /** | |
30 | * | |
31 | * @package CRM | |
06b69b18 | 32 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
33 | * $Id$ |
34 | * | |
35 | */ | |
36 | class CRM_Report_Form_Contribute_Bookkeeping extends CRM_Report_Form { | |
37 | protected $_addressField = FALSE; | |
38 | ||
39 | protected $_emailField = FALSE; | |
40 | ||
41 | protected $_summary = NULL; | |
42 | ||
43 | protected $_customGroupExtends = array( | |
9d72cede EM |
44 | 'Membership' |
45 | ); | |
2f4c2f5d | 46 | |
74cf4551 EM |
47 | /** |
48 | * | |
49 | */ | |
50 | /** | |
51 | * | |
52 | */ | |
4d9dd529 | 53 | function __construct() { |
6a488035 | 54 | $this->_columns = array( |
9d72cede | 55 | 'civicrm_contact' => array( |
6a488035 | 56 | 'dao' => 'CRM_Contact_DAO_Contact', |
9d72cede EM |
57 | 'fields' => array( |
58 | 'sort_name' => array( | |
59 | 'title' => ts('Contact Name'), | |
6a488035 TO |
60 | 'required' => TRUE, |
61 | 'no_repeat' => TRUE, | |
62 | ), | |
9d72cede | 63 | 'id' => array( |
6a488035 TO |
64 | 'no_display' => TRUE, |
65 | 'required' => TRUE, | |
66 | ), | |
9d72cede | 67 | 'contact_type' => array( |
30f85891 RN |
68 | 'title' => ts('Contact Type'), |
69 | ), | |
9d72cede | 70 | 'contact_sub_type' => array( |
b8f96eb8 | 71 | 'title' => ts('Contact Subtype'), |
30f85891 | 72 | ), |
6a488035 | 73 | ), |
9d72cede EM |
74 | 'filters' => array( |
75 | 'sort_name' => array( | |
76 | 'title' => ts('Contact Name'), | |
6a488035 TO |
77 | 'operator' => 'like', |
78 | ), | |
9d72cede EM |
79 | 'id' => array( |
80 | 'title' => ts('Contact ID'), | |
6a488035 TO |
81 | 'no_display' => TRUE, |
82 | ), | |
83 | ), | |
9d72cede | 84 | 'order_bys' => array( |
444e4ce8 JL |
85 | 'sort_name' => array( |
86 | 'title' => ts('Last Name, First Name'), | |
87 | ), | |
88 | ), | |
6a488035 TO |
89 | 'grouping' => 'contact-fields', |
90 | ), | |
9d72cede | 91 | 'civicrm_membership' => array( |
6a488035 | 92 | 'dao' => 'CRM_Member_DAO_Membership', |
9d72cede EM |
93 | 'fields' => array( |
94 | 'id' => array( | |
95 | 'title' => ts('Membership #'), | |
6a488035 TO |
96 | 'no_display' => TRUE, |
97 | 'required' => TRUE, | |
98 | ), | |
99 | ), | |
100 | ), | |
086ca649 PN |
101 | 'civicrm_financial_account' => array( |
102 | 'dao' => 'CRM_Financial_DAO_FinancialAccount', | |
103 | 'fields' => array( | |
104 | 'debit_accounting_code' => array( | |
105 | 'title' => ts('Financial Account Code - Debit'), | |
9d72cede | 106 | 'name' => 'accounting_code', |
086ca649 PN |
107 | 'alias' => 'financial_account_civireport_debit', |
108 | 'default' => TRUE, | |
086ca649 PN |
109 | ), |
110 | 'credit_accounting_code' => array( | |
111 | 'title' => ts('Financial Account Code - Credit'), | |
9d72cede | 112 | 'name' => 'accounting_code', |
086ca649 PN |
113 | 'alias' => 'financial_account_civireport_credit', |
114 | 'default' => TRUE, | |
086ca649 PN |
115 | ), |
116 | 'debit_name' => array( | |
117 | 'title' => ts('Financial Account Name - Debit'), | |
9d72cede | 118 | 'name' => 'name', |
086ca649 PN |
119 | 'alias' => 'financial_account_civireport_debit', |
120 | 'default' => TRUE, | |
121 | ), | |
122 | 'credit_name' => array( | |
123 | 'title' => ts('Financial Account Name - Credit'), | |
9d72cede | 124 | 'name' => 'name', |
086ca649 PN |
125 | 'alias' => 'financial_account_civireport_credit', |
126 | 'default' => TRUE, | |
127 | ), | |
128 | ), | |
129 | 'filters' => array( | |
130 | 'debit_accounting_code' => array( | |
131 | 'title' => ts('Financial Account Code - Debit'), | |
132 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
133 | 'options' => CRM_Contribute_PseudoConstant::financialAccount(NULL, NULL, 'accounting_code', 'accounting_code'), | |
9d72cede | 134 | 'name' => 'accounting_code', |
086ca649 PN |
135 | 'alias' => 'financial_account_civireport_debit', |
136 | ), | |
137 | 'credit_accounting_code' => array( | |
2f4c2f5d | 138 | 'title' => ts('Financial Account Code - Credit'), |
086ca649 PN |
139 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
140 | 'options' => CRM_Contribute_PseudoConstant::financialAccount(NULL, NULL, 'accounting_code', 'accounting_code'), | |
141 | ), | |
142 | 'debit_name' => array( | |
143 | 'title' => ts('Financial Account Name - Debit'), | |
144 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
145 | 'options' => CRM_Contribute_PseudoConstant::financialAccount(), | |
9d72cede | 146 | 'name' => 'id', |
086ca649 PN |
147 | 'alias' => 'financial_account_civireport_debit', |
148 | ), | |
149 | 'credit_name' => array( | |
2f4c2f5d | 150 | 'title' => ts('Financial Account Name - Credit'), |
086ca649 PN |
151 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
152 | 'options' => CRM_Contribute_PseudoConstant::financialAccount(), | |
153 | ), | |
154 | ), | |
2f4c2f5d | 155 | ), |
086ca649 PN |
156 | 'civicrm_line_item' => array( |
157 | 'dao' => 'CRM_Price_DAO_LineItem', | |
158 | 'fields' => array( | |
9d72cede EM |
159 | 'financial_type_id' => array( |
160 | 'title' => ts('Financial Type'), | |
086ca649 PN |
161 | 'default' => TRUE, |
162 | ), | |
163 | ), | |
164 | 'filters' => array( | |
2f4c2f5d | 165 | 'financial_type_id' => array( |
166 | 'title' => ts('Financial Type'), | |
086ca649 PN |
167 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
168 | 'options' => CRM_Contribute_PseudoConstant::financialType(), | |
169 | ), | |
170 | ), | |
444e4ce8 JL |
171 | 'order_bys' => array( |
172 | 'financial_type_id' => array('title' => ts('Financial Type')), | |
173 | ), | |
2f4c2f5d | 174 | ), |
9d72cede | 175 | 'civicrm_contribution' => array( |
6a488035 | 176 | 'dao' => 'CRM_Contribute_DAO_Contribution', |
9d72cede | 177 | 'fields' => array( |
6e3c3a7d PN |
178 | 'receive_date' => array( |
179 | 'default' => TRUE | |
6a488035 | 180 | ), |
6e3c3a7d PN |
181 | 'invoice_id' => array( |
182 | 'title' => ts('Invoice ID'), | |
6a488035 TO |
183 | 'default' => TRUE, |
184 | ), | |
9d72cede EM |
185 | 'contribution_status_id' => array( |
186 | 'title' => ts('Contribution Status'), | |
6a488035 TO |
187 | 'default' => TRUE, |
188 | ), | |
9d72cede EM |
189 | 'id' => array( |
190 | 'title' => ts('Contribution #'), | |
6a488035 TO |
191 | 'default' => TRUE, |
192 | ), | |
193 | ), | |
9d72cede EM |
194 | 'filters' => array( |
195 | 'receive_date' => array('operatorType' => CRM_Report_Form::OP_DATE), | |
196 | 'contribution_status_id' => array( | |
197 | 'title' => ts('Contribution Status'), | |
6a488035 TO |
198 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
199 | 'options' => CRM_Contribute_PseudoConstant::contributionStatus(), | |
200 | 'default' => array(1), | |
201 | ), | |
6a488035 | 202 | ), |
444e4ce8 JL |
203 | 'order_bys' => array( |
204 | 'contribution_id' => array('title' => ts('Contribution #')), | |
205 | 'contribution_status_id' => array('title' => ts('Contribution Status')), | |
206 | ), | |
6a488035 | 207 | 'grouping' => 'contri-fields', |
086ca649 | 208 | ), |
4d9dd529 PN |
209 | 'civicrm_financial_trxn' => array( |
210 | 'dao' => 'CRM_Financial_DAO_FinancialTrxn', | |
211 | 'fields' => array( | |
212 | 'check_number' => array( | |
213 | 'title' => ts('Cheque #'), | |
6a488035 TO |
214 | 'default' => TRUE, |
215 | ), | |
9d72cede EM |
216 | 'payment_instrument_id' => array( |
217 | 'title' => ts('Payment Instrument'), | |
4d9dd529 PN |
218 | 'default' => TRUE, |
219 | ), | |
652a526e | 220 | 'currency' => array( |
9d72cede EM |
221 | 'required' => TRUE, |
222 | 'no_display' => TRUE, | |
2f4c2f5d | 223 | ), |
6e3c3a7d PN |
224 | 'trxn_date' => array( |
225 | 'title' => ts('Transaction Date'), | |
086ca649 PN |
226 | 'default' => TRUE, |
227 | 'type' => CRM_Utils_Type::T_DATE, | |
6e3c3a7d PN |
228 | ), |
229 | 'trxn_id' => array( | |
230 | 'title' => ts('Trans #'), | |
231 | 'default' => TRUE, | |
232 | ), | |
4d9dd529 | 233 | ), |
9d72cede | 234 | 'filters' => array( |
4d9dd529 | 235 | 'payment_instrument_id' => array( |
6e3c3a7d | 236 | 'title' => ts('Payment Instrument'), |
4d9dd529 PN |
237 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
238 | 'options' => CRM_Contribute_PseudoConstant::paymentInstrument(), | |
239 | ), | |
652a526e | 240 | 'currency' => array( |
9d72cede EM |
241 | 'title' => 'Currency', |
242 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
243 | 'options' => CRM_Core_OptionGroup::values('currencies_enabled'), | |
244 | 'default' => NULL, | |
245 | 'type' => CRM_Utils_Type::T_STRING, | |
652a526e | 246 | ), |
2f4c2f5d | 247 | 'trxn_date' => array( |
6e3c3a7d PN |
248 | 'title' => ts('Transaction Date'), |
249 | 'operatorType' => CRM_Report_Form::OP_DATE, | |
250 | 'type' => CRM_Utils_Type::T_DATE, | |
251 | ), | |
4d9dd529 | 252 | ), |
444e4ce8 JL |
253 | 'order_bys' => array( |
254 | 'payment_instrument_id' => array('title' => ts('Payment Instrument')), | |
255 | ), | |
2f4c2f5d | 256 | ), |
6a488035 TO |
257 | 'civicrm_entity_financial_trxn' => array( |
258 | 'dao' => 'CRM_Financial_DAO_EntityFinancialTrxn', | |
259 | 'fields' => array( | |
260 | 'amount' => array( | |
261 | 'title' => ts('Amount'), | |
262 | 'default' => TRUE, | |
652a526e | 263 | 'type' => CRM_Utils_Type::T_STRING, |
6a488035 TO |
264 | ), |
265 | ), | |
9d72cede EM |
266 | 'filters' => array( |
267 | 'amount' => array('title' => ts('Amount')), | |
4d9dd529 | 268 | ), |
2f4c2f5d | 269 | ), |
6a488035 | 270 | ); |
444e4ce8 | 271 | |
16e2e80c | 272 | $this->_groupFilter = TRUE; |
444e4ce8 | 273 | $this->_tagFilter = TRUE; |
6a488035 TO |
274 | parent::__construct(); |
275 | } | |
276 | ||
277 | function preProcess() { | |
278 | parent::preProcess(); | |
279 | } | |
280 | ||
281 | function select() { | |
282 | $select = array(); | |
283 | ||
284 | $this->_columnHeaders = array(); | |
285 | foreach ($this->_columns as $tableName => $table) { | |
286 | if (array_key_exists('fields', $table)) { | |
287 | foreach ($table['fields'] as $fieldName => $field) { | |
9d72cede EM |
288 | if (!empty($field['required']) || |
289 | !empty($this->_params['fields'][$fieldName]) | |
290 | ) { | |
086ca649 | 291 | switch ($fieldName) { |
9d72cede EM |
292 | case 'credit_accounting_code' : |
293 | $select[] = " CASE | |
086ca649 PN |
294 | WHEN {$this->_aliases['civicrm_financial_trxn']}.from_financial_account_id IS NOT NULL |
295 | THEN {$this->_aliases['civicrm_financial_account']}_credit_1.accounting_code | |
296 | ELSE {$this->_aliases['civicrm_financial_account']}_credit_2.accounting_code | |
297 | END AS civicrm_financial_account_credit_accounting_code "; | |
9d72cede EM |
298 | break; |
299 | case 'amount' : | |
300 | $select[] = " CASE | |
086ca649 PN |
301 | WHEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_id IS NOT NULL |
302 | THEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.amount | |
303 | ELSE {$this->_aliases['civicrm_entity_financial_trxn']}.amount | |
304 | END AS civicrm_entity_financial_trxn_amount "; | |
9d72cede EM |
305 | break; |
306 | case 'credit_name' : | |
307 | $select[] = " CASE | |
086ca649 PN |
308 | WHEN {$this->_aliases['civicrm_financial_trxn']}.from_financial_account_id IS NOT NULL |
309 | THEN {$this->_aliases['civicrm_financial_account']}_credit_1.name | |
310 | ELSE {$this->_aliases['civicrm_financial_account']}_credit_2.name | |
311 | END AS civicrm_financial_account_credit_name "; | |
9d72cede EM |
312 | break; |
313 | default : | |
314 | $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}"; | |
315 | break; | |
6a488035 TO |
316 | } |
317 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title']; | |
318 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field); | |
319 | } | |
320 | } | |
321 | } | |
322 | } | |
323 | ||
652a526e | 324 | $this->_select = 'SELECT ' . implode(', ', $select) . ' '; |
6a488035 TO |
325 | } |
326 | ||
327 | function from() { | |
328 | $this->_from = NULL; | |
329 | ||
330 | $this->_from = "FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom} | |
331 | INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']} | |
2f4c2f5d | 332 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id AND |
6a488035 TO |
333 | {$this->_aliases['civicrm_contribution']}.is_test = 0 |
334 | LEFT JOIN civicrm_membership_payment payment | |
086ca649 | 335 | ON ( {$this->_aliases['civicrm_contribution']}.id = payment.contribution_id ) |
6a488035 | 336 | LEFT JOIN civicrm_membership {$this->_aliases['civicrm_membership']} |
2f4c2f5d | 337 | ON payment.membership_id = {$this->_aliases['civicrm_membership']}.id |
6a488035 | 338 | LEFT JOIN civicrm_entity_financial_trxn {$this->_aliases['civicrm_entity_financial_trxn']} |
2f4c2f5d | 339 | ON ({$this->_aliases['civicrm_contribution']}.id = {$this->_aliases['civicrm_entity_financial_trxn']}.entity_id AND |
6a488035 | 340 | {$this->_aliases['civicrm_entity_financial_trxn']}.entity_table = 'civicrm_contribution') |
4d9dd529 PN |
341 | LEFT JOIN civicrm_financial_trxn {$this->_aliases['civicrm_financial_trxn']} |
342 | ON {$this->_aliases['civicrm_financial_trxn']}.id = {$this->_aliases['civicrm_entity_financial_trxn']}.financial_trxn_id | |
6a488035 | 343 | LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_debit |
4d9dd529 | 344 | ON {$this->_aliases['civicrm_financial_trxn']}.to_financial_account_id = {$this->_aliases['civicrm_financial_account']}_debit.id |
6a488035 | 345 | LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_credit_1 |
4d9dd529 | 346 | ON {$this->_aliases['civicrm_financial_trxn']}.from_financial_account_id = {$this->_aliases['civicrm_financial_account']}_credit_1.id |
6a488035 | 347 | LEFT JOIN civicrm_entity_financial_trxn {$this->_aliases['civicrm_entity_financial_trxn']}_item |
2f4c2f5d | 348 | ON ({$this->_aliases['civicrm_financial_trxn']}.id = {$this->_aliases['civicrm_entity_financial_trxn']}_item.financial_trxn_id AND |
6a488035 | 349 | {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_table = 'civicrm_financial_item') |
4d9dd529 | 350 | LEFT JOIN civicrm_financial_item fitem |
6a488035 | 351 | ON fitem.id = {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_id |
4d9dd529 | 352 | LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_credit_2 |
6a488035 | 353 | ON fitem.financial_account_id = {$this->_aliases['civicrm_financial_account']}_credit_2.id |
4d9dd529 | 354 | LEFT JOIN civicrm_line_item {$this->_aliases['civicrm_line_item']} |
6a488035 TO |
355 | ON fitem.entity_id = {$this->_aliases['civicrm_line_item']}.id AND fitem.entity_table = 'civicrm_line_item' "; |
356 | } | |
357 | ||
358 | function orderBy() { | |
444e4ce8 JL |
359 | parent::orderBy(); |
360 | ||
361 | // please note this will just add the order-by columns to select query, and not display in column-headers. | |
362 | // This is a solution to not throw fatal errors when there is a column in order-by, not present in select/display columns. | |
363 | foreach ($this->_orderByFields as $orderBy) { | |
9d72cede EM |
364 | if (!array_key_exists($orderBy['name'], $this->_params['fields']) && |
365 | empty($orderBy['section']) | |
366 | ) { | |
444e4ce8 JL |
367 | $this->_select .= ", {$orderBy['dbAlias']} as {$orderBy['tplField']}"; |
368 | } | |
369 | } | |
6a488035 TO |
370 | } |
371 | ||
086ca649 PN |
372 | function where() { |
373 | foreach ($this->_columns as $tableName => $table) { | |
374 | if (array_key_exists('filters', $table)) { | |
375 | foreach ($table['filters'] as $fieldName => $field) { | |
376 | $clause = NULL; | |
377 | if ($fieldName == 'credit_accounting_code') { | |
378 | $field['dbAlias'] = "CASE | |
379 | WHEN financial_trxn_civireport.from_financial_account_id IS NOT NULL | |
380 | THEN financial_account_civireport_credit_1.accounting_code | |
2f4c2f5d | 381 | ELSE financial_account_civireport_credit_2.accounting_code |
086ca649 PN |
382 | END"; |
383 | } | |
384 | else if ($fieldName == 'credit_name') { | |
385 | $field['dbAlias'] = "CASE | |
386 | WHEN financial_trxn_civireport.from_financial_account_id IS NOT NULL | |
387 | THEN financial_account_civireport_credit_1.id | |
2f4c2f5d | 388 | ELSE financial_account_civireport_credit_2.id |
086ca649 PN |
389 | END"; |
390 | } | |
391 | if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) { | |
392 | $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params); | |
9d72cede EM |
393 | $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params); |
394 | $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params); | |
086ca649 PN |
395 | |
396 | $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']); | |
397 | } | |
398 | else { | |
399 | $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params); | |
400 | if ($op) { | |
401 | $clause = $this->whereClause($field, | |
402 | $op, | |
403 | CRM_Utils_Array::value("{$fieldName}_value", $this->_params), | |
404 | CRM_Utils_Array::value("{$fieldName}_min", $this->_params), | |
405 | CRM_Utils_Array::value("{$fieldName}_max", $this->_params) | |
406 | ); | |
407 | } | |
408 | } | |
409 | if (!empty($clause)) { | |
410 | $clauses[] = $clause; | |
411 | } | |
412 | } | |
413 | } | |
414 | } | |
415 | if (empty($clauses)) { | |
652a526e | 416 | $this->_where = 'WHERE ( 1 )'; |
086ca649 PN |
417 | } |
418 | else { | |
652a526e | 419 | $this->_where = 'WHERE ' . implode(' AND ', $clauses); |
086ca649 PN |
420 | } |
421 | } | |
422 | ||
6a488035 TO |
423 | function postProcess() { |
424 | // get the acl clauses built before we assemble the query | |
425 | $this->buildACLClause($this->_aliases['civicrm_contact']); | |
426 | parent::postProcess(); | |
427 | } | |
428 | ||
74cf4551 EM |
429 | /** |
430 | * @param $rows | |
431 | * | |
432 | * @return array | |
433 | */ | |
6a488035 TO |
434 | function statistics(&$rows) { |
435 | $statistics = parent::statistics($rows); | |
436 | ||
4d9dd529 | 437 | $select = " SELECT COUNT({$this->_aliases['civicrm_financial_trxn']}.id ) as count, |
652a526e | 438 | {$this->_aliases['civicrm_contribution']}.currency, |
2f4c2f5d | 439 | SUM(CASE |
4d9dd529 PN |
440 | WHEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_id IS NOT NULL |
441 | THEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.amount | |
442 | ELSE {$this->_aliases['civicrm_entity_financial_trxn']}.amount | |
2f4c2f5d | 443 | END) as amount |
652a526e | 444 | "; |
445 | ||
2f4c2f5d | 446 | $sql = "{$select} {$this->_from} {$this->_where} |
652a526e | 447 | GROUP BY {$this->_aliases['civicrm_contribution']}.currency |
448 | "; | |
6a488035 | 449 | |
6a488035 | 450 | $dao = CRM_Core_DAO::executeQuery($sql); |
652a526e | 451 | while ($dao->fetch()) { |
452 | $amount[] = CRM_Utils_Money::format($dao->amount, $dao->currency); | |
9d72cede EM |
453 | $avg[] = CRM_Utils_Money::format(round(($dao->amount / |
454 | $dao->count), 2), $dao->currency); | |
6a488035 TO |
455 | } |
456 | ||
652a526e | 457 | $statistics['counts']['amount'] = array( |
9d72cede EM |
458 | 'value' => implode(', ', $amount), |
459 | 'title' => 'Total Amount', | |
460 | 'type' => CRM_Utils_Type::T_STRING, | |
652a526e | 461 | ); |
462 | $statistics['counts']['avg'] = array( | |
463 | 'value' => implode(', ', $avg), | |
464 | 'title' => 'Average', | |
465 | 'type' => CRM_Utils_Type::T_STRING, | |
466 | ); | |
6a488035 TO |
467 | return $statistics; |
468 | } | |
469 | ||
74cf4551 EM |
470 | /** |
471 | * @param $rows | |
472 | */ | |
6a488035 | 473 | function alterDisplay(&$rows) { |
6a488035 TO |
474 | $contributionTypes = CRM_Contribute_PseudoConstant::financialType(); |
475 | $paymentInstruments = CRM_Contribute_PseudoConstant::paymentInstrument(); | |
4d9dd529 | 476 | $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(); |
6a488035 | 477 | foreach ($rows as $rowNum => $row) { |
6a488035 | 478 | // convert display name to links |
9d72cede EM |
479 | if (array_key_exists('civicrm_contact_sort_name', $row) && |
480 | !empty($rows[$rowNum]['civicrm_contact_sort_name']) && | |
6a488035 TO |
481 | array_key_exists('civicrm_contact_id', $row) |
482 | ) { | |
652a526e | 483 | $url = CRM_Utils_System::url('civicrm/contact/view', |
6a488035 TO |
484 | 'reset=1&cid=' . $row['civicrm_contact_id'], |
485 | $this->_absoluteUrl | |
486 | ); | |
487 | $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url; | |
652a526e | 488 | $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts('View Contact Summary for this Contact.'); |
6a488035 TO |
489 | } |
490 | ||
491 | // handle contribution status id | |
4d9dd529 PN |
492 | if ($value = CRM_Utils_Array::value('civicrm_contribution_contribution_status_id', $row)) { |
493 | $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = $contributionStatus[$value]; | |
6a488035 TO |
494 | } |
495 | ||
496 | // handle payment instrument id | |
4d9dd529 PN |
497 | if ($value = CRM_Utils_Array::value('civicrm_financial_trxn_payment_instrument_id', $row)) { |
498 | $rows[$rowNum]['civicrm_financial_trxn_payment_instrument_id'] = $paymentInstruments[$value]; | |
6a488035 | 499 | } |
2f4c2f5d | 500 | |
4d9dd529 | 501 | // handle financial type id |
6a488035 TO |
502 | if ($value = CRM_Utils_Array::value('civicrm_line_item_financial_type_id', $row)) { |
503 | $rows[$rowNum]['civicrm_line_item_financial_type_id'] = $contributionTypes[$value]; | |
6a488035 | 504 | } |
652a526e | 505 | if ($value = CRM_Utils_Array::value('civicrm_entity_financial_trxn_amount', $row)) { |
9d72cede | 506 | $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 | 507 | } |
6a488035 TO |
508 | } |
509 | } | |
510 | } | |
511 |