Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Report_Form_Contribute_Bookkeeping extends CRM_Report_Form { | |
36 | protected $_addressField = FALSE; | |
37 | ||
38 | protected $_emailField = FALSE; | |
39 | ||
40 | protected $_summary = NULL; | |
41 | ||
42 | protected $_customGroupExtends = array( | |
21dfd5f5 | 43 | 'Membership', |
9d72cede | 44 | ); |
2f4c2f5d | 45 | |
74cf4551 | 46 | /** |
74cf4551 EM |
47 | */ |
48 | /** | |
74cf4551 | 49 | */ |
00be9182 | 50 | public function __construct() { |
6a488035 | 51 | $this->_columns = array( |
9d72cede | 52 | 'civicrm_contact' => array( |
6a488035 | 53 | 'dao' => 'CRM_Contact_DAO_Contact', |
9d72cede EM |
54 | 'fields' => array( |
55 | 'sort_name' => array( | |
56 | 'title' => ts('Contact Name'), | |
6a488035 TO |
57 | 'required' => TRUE, |
58 | 'no_repeat' => TRUE, | |
59 | ), | |
9d72cede | 60 | 'id' => array( |
6a488035 TO |
61 | 'no_display' => TRUE, |
62 | 'required' => TRUE, | |
63 | ), | |
9d72cede | 64 | 'contact_type' => array( |
30f85891 RN |
65 | 'title' => ts('Contact Type'), |
66 | ), | |
9d72cede | 67 | 'contact_sub_type' => array( |
b8f96eb8 | 68 | 'title' => ts('Contact Subtype'), |
30f85891 | 69 | ), |
6a488035 | 70 | ), |
9d72cede EM |
71 | 'filters' => array( |
72 | 'sort_name' => array( | |
73 | 'title' => ts('Contact Name'), | |
6a488035 TO |
74 | 'operator' => 'like', |
75 | ), | |
9d72cede EM |
76 | 'id' => array( |
77 | 'title' => ts('Contact ID'), | |
6a488035 TO |
78 | 'no_display' => TRUE, |
79 | ), | |
80 | ), | |
9d72cede | 81 | 'order_bys' => array( |
444e4ce8 JL |
82 | 'sort_name' => array( |
83 | 'title' => ts('Last Name, First Name'), | |
84 | ), | |
85 | ), | |
6a488035 TO |
86 | 'grouping' => 'contact-fields', |
87 | ), | |
9d72cede | 88 | 'civicrm_membership' => array( |
6a488035 | 89 | 'dao' => 'CRM_Member_DAO_Membership', |
9d72cede EM |
90 | 'fields' => array( |
91 | 'id' => array( | |
92 | 'title' => ts('Membership #'), | |
6a488035 TO |
93 | 'no_display' => TRUE, |
94 | 'required' => TRUE, | |
95 | ), | |
96 | ), | |
97 | ), | |
086ca649 PN |
98 | 'civicrm_financial_account' => array( |
99 | 'dao' => 'CRM_Financial_DAO_FinancialAccount', | |
100 | 'fields' => array( | |
101 | 'debit_accounting_code' => array( | |
102 | 'title' => ts('Financial Account Code - Debit'), | |
9d72cede | 103 | 'name' => 'accounting_code', |
086ca649 PN |
104 | 'alias' => 'financial_account_civireport_debit', |
105 | 'default' => TRUE, | |
086ca649 PN |
106 | ), |
107 | 'credit_accounting_code' => array( | |
108 | 'title' => ts('Financial Account Code - Credit'), | |
9d72cede | 109 | 'name' => 'accounting_code', |
086ca649 PN |
110 | 'alias' => 'financial_account_civireport_credit', |
111 | 'default' => TRUE, | |
086ca649 PN |
112 | ), |
113 | 'debit_name' => array( | |
114 | 'title' => ts('Financial Account Name - Debit'), | |
9d72cede | 115 | 'name' => 'name', |
086ca649 PN |
116 | 'alias' => 'financial_account_civireport_debit', |
117 | 'default' => TRUE, | |
118 | ), | |
119 | 'credit_name' => array( | |
120 | 'title' => ts('Financial Account Name - Credit'), | |
9d72cede | 121 | 'name' => 'name', |
086ca649 PN |
122 | 'alias' => 'financial_account_civireport_credit', |
123 | 'default' => TRUE, | |
124 | ), | |
125 | ), | |
126 | 'filters' => array( | |
127 | 'debit_accounting_code' => array( | |
128 | 'title' => ts('Financial Account Code - Debit'), | |
129 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
130 | 'options' => CRM_Contribute_PseudoConstant::financialAccount(NULL, NULL, 'accounting_code', 'accounting_code'), | |
9d72cede | 131 | 'name' => 'accounting_code', |
086ca649 PN |
132 | 'alias' => 'financial_account_civireport_debit', |
133 | ), | |
134 | 'credit_accounting_code' => array( | |
2f4c2f5d | 135 | 'title' => ts('Financial Account Code - Credit'), |
086ca649 PN |
136 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
137 | 'options' => CRM_Contribute_PseudoConstant::financialAccount(NULL, NULL, 'accounting_code', 'accounting_code'), | |
138 | ), | |
139 | 'debit_name' => array( | |
140 | 'title' => ts('Financial Account Name - Debit'), | |
141 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
142 | 'options' => CRM_Contribute_PseudoConstant::financialAccount(), | |
9d72cede | 143 | 'name' => 'id', |
086ca649 PN |
144 | 'alias' => 'financial_account_civireport_debit', |
145 | ), | |
146 | 'credit_name' => array( | |
2f4c2f5d | 147 | 'title' => ts('Financial Account Name - Credit'), |
086ca649 PN |
148 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
149 | 'options' => CRM_Contribute_PseudoConstant::financialAccount(), | |
150 | ), | |
151 | ), | |
2f4c2f5d | 152 | ), |
086ca649 PN |
153 | 'civicrm_line_item' => array( |
154 | 'dao' => 'CRM_Price_DAO_LineItem', | |
155 | 'fields' => array( | |
9d72cede EM |
156 | 'financial_type_id' => array( |
157 | 'title' => ts('Financial Type'), | |
086ca649 PN |
158 | 'default' => TRUE, |
159 | ), | |
160 | ), | |
161 | 'filters' => array( | |
2f4c2f5d | 162 | 'financial_type_id' => array( |
163 | 'title' => ts('Financial Type'), | |
086ca649 PN |
164 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
165 | 'options' => CRM_Contribute_PseudoConstant::financialType(), | |
166 | ), | |
167 | ), | |
444e4ce8 JL |
168 | 'order_bys' => array( |
169 | 'financial_type_id' => array('title' => ts('Financial Type')), | |
170 | ), | |
2f4c2f5d | 171 | ), |
9d72cede | 172 | 'civicrm_contribution' => array( |
6a488035 | 173 | 'dao' => 'CRM_Contribute_DAO_Contribution', |
9d72cede | 174 | 'fields' => array( |
6e3c3a7d | 175 | 'receive_date' => array( |
21dfd5f5 | 176 | 'default' => TRUE, |
6a488035 | 177 | ), |
6e3c3a7d PN |
178 | 'invoice_id' => array( |
179 | 'title' => ts('Invoice ID'), | |
6a488035 TO |
180 | 'default' => TRUE, |
181 | ), | |
9d72cede EM |
182 | 'contribution_status_id' => array( |
183 | 'title' => ts('Contribution Status'), | |
6a488035 TO |
184 | 'default' => TRUE, |
185 | ), | |
9d72cede EM |
186 | 'id' => array( |
187 | 'title' => ts('Contribution #'), | |
6a488035 TO |
188 | 'default' => TRUE, |
189 | ), | |
190 | ), | |
9d72cede EM |
191 | 'filters' => array( |
192 | 'receive_date' => array('operatorType' => CRM_Report_Form::OP_DATE), | |
193 | 'contribution_status_id' => array( | |
194 | 'title' => ts('Contribution Status'), | |
6a488035 TO |
195 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
196 | 'options' => CRM_Contribute_PseudoConstant::contributionStatus(), | |
197 | 'default' => array(1), | |
198 | ), | |
6a488035 | 199 | ), |
444e4ce8 JL |
200 | 'order_bys' => array( |
201 | 'contribution_id' => array('title' => ts('Contribution #')), | |
202 | 'contribution_status_id' => array('title' => ts('Contribution Status')), | |
203 | ), | |
6a488035 | 204 | 'grouping' => 'contri-fields', |
086ca649 | 205 | ), |
4d9dd529 PN |
206 | 'civicrm_financial_trxn' => array( |
207 | 'dao' => 'CRM_Financial_DAO_FinancialTrxn', | |
208 | 'fields' => array( | |
209 | 'check_number' => array( | |
210 | 'title' => ts('Cheque #'), | |
6a488035 TO |
211 | 'default' => TRUE, |
212 | ), | |
9d72cede EM |
213 | 'payment_instrument_id' => array( |
214 | 'title' => ts('Payment Instrument'), | |
4d9dd529 PN |
215 | 'default' => TRUE, |
216 | ), | |
652a526e | 217 | 'currency' => array( |
9d72cede EM |
218 | 'required' => TRUE, |
219 | 'no_display' => TRUE, | |
2f4c2f5d | 220 | ), |
6e3c3a7d PN |
221 | 'trxn_date' => array( |
222 | 'title' => ts('Transaction Date'), | |
086ca649 PN |
223 | 'default' => TRUE, |
224 | 'type' => CRM_Utils_Type::T_DATE, | |
6e3c3a7d PN |
225 | ), |
226 | 'trxn_id' => array( | |
227 | 'title' => ts('Trans #'), | |
228 | 'default' => TRUE, | |
229 | ), | |
4d9dd529 | 230 | ), |
9d72cede | 231 | 'filters' => array( |
4d9dd529 | 232 | 'payment_instrument_id' => array( |
6e3c3a7d | 233 | 'title' => ts('Payment Instrument'), |
4d9dd529 PN |
234 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
235 | 'options' => CRM_Contribute_PseudoConstant::paymentInstrument(), | |
236 | ), | |
652a526e | 237 | 'currency' => array( |
9d72cede EM |
238 | 'title' => 'Currency', |
239 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
240 | 'options' => CRM_Core_OptionGroup::values('currencies_enabled'), | |
241 | 'default' => NULL, | |
242 | 'type' => CRM_Utils_Type::T_STRING, | |
652a526e | 243 | ), |
2f4c2f5d | 244 | 'trxn_date' => array( |
6e3c3a7d PN |
245 | 'title' => ts('Transaction Date'), |
246 | 'operatorType' => CRM_Report_Form::OP_DATE, | |
247 | 'type' => CRM_Utils_Type::T_DATE, | |
248 | ), | |
4d9dd529 | 249 | ), |
444e4ce8 JL |
250 | 'order_bys' => array( |
251 | 'payment_instrument_id' => array('title' => ts('Payment Instrument')), | |
252 | ), | |
2f4c2f5d | 253 | ), |
6a488035 TO |
254 | 'civicrm_entity_financial_trxn' => array( |
255 | 'dao' => 'CRM_Financial_DAO_EntityFinancialTrxn', | |
256 | 'fields' => array( | |
257 | 'amount' => array( | |
258 | 'title' => ts('Amount'), | |
259 | 'default' => TRUE, | |
652a526e | 260 | 'type' => CRM_Utils_Type::T_STRING, |
6a488035 TO |
261 | ), |
262 | ), | |
9d72cede EM |
263 | 'filters' => array( |
264 | 'amount' => array('title' => ts('Amount')), | |
4d9dd529 | 265 | ), |
2f4c2f5d | 266 | ), |
6a488035 | 267 | ); |
444e4ce8 | 268 | |
16e2e80c | 269 | $this->_groupFilter = TRUE; |
444e4ce8 | 270 | $this->_tagFilter = TRUE; |
6a488035 TO |
271 | parent::__construct(); |
272 | } | |
273 | ||
00be9182 | 274 | public function preProcess() { |
6a488035 TO |
275 | parent::preProcess(); |
276 | } | |
277 | ||
00be9182 | 278 | public function select() { |
6a488035 TO |
279 | $select = array(); |
280 | ||
281 | $this->_columnHeaders = array(); | |
282 | foreach ($this->_columns as $tableName => $table) { | |
283 | if (array_key_exists('fields', $table)) { | |
284 | foreach ($table['fields'] as $fieldName => $field) { | |
9d72cede EM |
285 | if (!empty($field['required']) || |
286 | !empty($this->_params['fields'][$fieldName]) | |
287 | ) { | |
086ca649 | 288 | switch ($fieldName) { |
84178120 | 289 | case 'credit_accounting_code': |
9d72cede | 290 | $select[] = " CASE |
086ca649 PN |
291 | WHEN {$this->_aliases['civicrm_financial_trxn']}.from_financial_account_id IS NOT NULL |
292 | THEN {$this->_aliases['civicrm_financial_account']}_credit_1.accounting_code | |
293 | ELSE {$this->_aliases['civicrm_financial_account']}_credit_2.accounting_code | |
294 | END AS civicrm_financial_account_credit_accounting_code "; | |
9d72cede | 295 | break; |
84178120 TO |
296 | |
297 | case 'amount': | |
9d72cede | 298 | $select[] = " CASE |
086ca649 PN |
299 | WHEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_id IS NOT NULL |
300 | THEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.amount | |
301 | ELSE {$this->_aliases['civicrm_entity_financial_trxn']}.amount | |
302 | END AS civicrm_entity_financial_trxn_amount "; | |
9d72cede | 303 | break; |
84178120 TO |
304 | |
305 | case 'credit_name': | |
9d72cede | 306 | $select[] = " CASE |
086ca649 PN |
307 | WHEN {$this->_aliases['civicrm_financial_trxn']}.from_financial_account_id IS NOT NULL |
308 | THEN {$this->_aliases['civicrm_financial_account']}_credit_1.name | |
309 | ELSE {$this->_aliases['civicrm_financial_account']}_credit_2.name | |
310 | END AS civicrm_financial_account_credit_name "; | |
9d72cede | 311 | break; |
84178120 TO |
312 | |
313 | default: | |
9d72cede EM |
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 | ||
00be9182 | 327 | public function from() { |
6a488035 TO |
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 | ||
00be9182 | 358 | public 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 | ||
00be9182 | 372 | public function where() { |
086ca649 PN |
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 | } | |
4c9b6178 | 384 | elseif ($fieldName == 'credit_name') { |
086ca649 PN |
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 | ||
00be9182 | 423 | public function postProcess() { |
6a488035 TO |
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 | */ | |
00be9182 | 434 | public function statistics(&$rows) { |
6a488035 TO |
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 | 453 | $avg[] = CRM_Utils_Money::format(round(($dao->amount / |
353ffa53 | 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 | 470 | /** |
ced9bfed EM |
471 | * Alter display of rows. |
472 | * | |
473 | * Iterate through the rows retrieved via SQL and make changes for display purposes, | |
474 | * such as rendering contacts as links. | |
475 | * | |
476 | * @param array $rows | |
477 | * Rows generated by SQL, with an array for each row. | |
74cf4551 | 478 | */ |
00be9182 | 479 | public function alterDisplay(&$rows) { |
6a488035 TO |
480 | $contributionTypes = CRM_Contribute_PseudoConstant::financialType(); |
481 | $paymentInstruments = CRM_Contribute_PseudoConstant::paymentInstrument(); | |
4d9dd529 | 482 | $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(); |
6a488035 | 483 | foreach ($rows as $rowNum => $row) { |
6a488035 | 484 | // convert display name to links |
9d72cede EM |
485 | if (array_key_exists('civicrm_contact_sort_name', $row) && |
486 | !empty($rows[$rowNum]['civicrm_contact_sort_name']) && | |
6a488035 TO |
487 | array_key_exists('civicrm_contact_id', $row) |
488 | ) { | |
652a526e | 489 | $url = CRM_Utils_System::url('civicrm/contact/view', |
6a488035 TO |
490 | 'reset=1&cid=' . $row['civicrm_contact_id'], |
491 | $this->_absoluteUrl | |
492 | ); | |
493 | $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url; | |
652a526e | 494 | $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts('View Contact Summary for this Contact.'); |
6a488035 TO |
495 | } |
496 | ||
497 | // handle contribution status id | |
4d9dd529 PN |
498 | if ($value = CRM_Utils_Array::value('civicrm_contribution_contribution_status_id', $row)) { |
499 | $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = $contributionStatus[$value]; | |
6a488035 TO |
500 | } |
501 | ||
502 | // handle payment instrument id | |
4d9dd529 PN |
503 | if ($value = CRM_Utils_Array::value('civicrm_financial_trxn_payment_instrument_id', $row)) { |
504 | $rows[$rowNum]['civicrm_financial_trxn_payment_instrument_id'] = $paymentInstruments[$value]; | |
6a488035 | 505 | } |
2f4c2f5d | 506 | |
4d9dd529 | 507 | // handle financial type id |
6a488035 TO |
508 | if ($value = CRM_Utils_Array::value('civicrm_line_item_financial_type_id', $row)) { |
509 | $rows[$rowNum]['civicrm_line_item_financial_type_id'] = $contributionTypes[$value]; | |
6a488035 | 510 | } |
652a526e | 511 | if ($value = CRM_Utils_Array::value('civicrm_entity_financial_trxn_amount', $row)) { |
9d72cede | 512 | $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 | 513 | } |
6a488035 TO |
514 | } |
515 | } | |
96025800 | 516 | |
6a488035 | 517 | } |