phpcs - Fix error, "CONST keyword must be lowercase; expected const but found CONST"
[civicrm-core.git] / CRM / Report / Form / Contribute / Bookkeeping.php
... / ...
CommitLineData
1<?php
2
3/*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.6 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
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
32 * @copyright CiviCRM LLC (c) 2004-2014
33 * $Id$
34 *
35 */
36class 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(
44 'Membership'
45 );
46
47 /**
48 *
49 */
50 /**
51 *
52 */
53 function __construct() {
54 $this->_columns = array(
55 'civicrm_contact' => array(
56 'dao' => 'CRM_Contact_DAO_Contact',
57 'fields' => array(
58 'sort_name' => array(
59 'title' => ts('Contact Name'),
60 'required' => TRUE,
61 'no_repeat' => TRUE,
62 ),
63 'id' => array(
64 'no_display' => TRUE,
65 'required' => TRUE,
66 ),
67 'contact_type' => array(
68 'title' => ts('Contact Type'),
69 ),
70 'contact_sub_type' => array(
71 'title' => ts('Contact Subtype'),
72 ),
73 ),
74 'filters' => array(
75 'sort_name' => array(
76 'title' => ts('Contact Name'),
77 'operator' => 'like',
78 ),
79 'id' => array(
80 'title' => ts('Contact ID'),
81 'no_display' => TRUE,
82 ),
83 ),
84 'order_bys' => array(
85 'sort_name' => array(
86 'title' => ts('Last Name, First Name'),
87 ),
88 ),
89 'grouping' => 'contact-fields',
90 ),
91 'civicrm_membership' => array(
92 'dao' => 'CRM_Member_DAO_Membership',
93 'fields' => array(
94 'id' => array(
95 'title' => ts('Membership #'),
96 'no_display' => TRUE,
97 'required' => TRUE,
98 ),
99 ),
100 ),
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'),
106 'name' => 'accounting_code',
107 'alias' => 'financial_account_civireport_debit',
108 'default' => TRUE,
109 ),
110 'credit_accounting_code' => array(
111 'title' => ts('Financial Account Code - Credit'),
112 'name' => 'accounting_code',
113 'alias' => 'financial_account_civireport_credit',
114 'default' => TRUE,
115 ),
116 'debit_name' => array(
117 'title' => ts('Financial Account Name - Debit'),
118 'name' => 'name',
119 'alias' => 'financial_account_civireport_debit',
120 'default' => TRUE,
121 ),
122 'credit_name' => array(
123 'title' => ts('Financial Account Name - Credit'),
124 'name' => 'name',
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'),
134 'name' => 'accounting_code',
135 'alias' => 'financial_account_civireport_debit',
136 ),
137 'credit_accounting_code' => array(
138 'title' => ts('Financial Account Code - Credit'),
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(),
146 'name' => 'id',
147 'alias' => 'financial_account_civireport_debit',
148 ),
149 'credit_name' => array(
150 'title' => ts('Financial Account Name - Credit'),
151 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
152 'options' => CRM_Contribute_PseudoConstant::financialAccount(),
153 ),
154 ),
155 ),
156 'civicrm_line_item' => array(
157 'dao' => 'CRM_Price_DAO_LineItem',
158 'fields' => array(
159 'financial_type_id' => array(
160 'title' => ts('Financial Type'),
161 'default' => TRUE,
162 ),
163 ),
164 'filters' => array(
165 'financial_type_id' => array(
166 'title' => ts('Financial Type'),
167 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
168 'options' => CRM_Contribute_PseudoConstant::financialType(),
169 ),
170 ),
171 'order_bys' => array(
172 'financial_type_id' => array('title' => ts('Financial Type')),
173 ),
174 ),
175 'civicrm_contribution' => array(
176 'dao' => 'CRM_Contribute_DAO_Contribution',
177 'fields' => array(
178 'receive_date' => array(
179 'default' => TRUE
180 ),
181 'invoice_id' => array(
182 'title' => ts('Invoice ID'),
183 'default' => TRUE,
184 ),
185 'contribution_status_id' => array(
186 'title' => ts('Contribution Status'),
187 'default' => TRUE,
188 ),
189 'id' => array(
190 'title' => ts('Contribution #'),
191 'default' => TRUE,
192 ),
193 ),
194 'filters' => array(
195 'receive_date' => array('operatorType' => CRM_Report_Form::OP_DATE),
196 'contribution_status_id' => array(
197 'title' => ts('Contribution Status'),
198 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
199 'options' => CRM_Contribute_PseudoConstant::contributionStatus(),
200 'default' => array(1),
201 ),
202 ),
203 'order_bys' => array(
204 'contribution_id' => array('title' => ts('Contribution #')),
205 'contribution_status_id' => array('title' => ts('Contribution Status')),
206 ),
207 'grouping' => 'contri-fields',
208 ),
209 'civicrm_financial_trxn' => array(
210 'dao' => 'CRM_Financial_DAO_FinancialTrxn',
211 'fields' => array(
212 'check_number' => array(
213 'title' => ts('Cheque #'),
214 'default' => TRUE,
215 ),
216 'payment_instrument_id' => array(
217 'title' => ts('Payment Instrument'),
218 'default' => TRUE,
219 ),
220 'currency' => array(
221 'required' => TRUE,
222 'no_display' => TRUE,
223 ),
224 'trxn_date' => array(
225 'title' => ts('Transaction Date'),
226 'default' => TRUE,
227 'type' => CRM_Utils_Type::T_DATE,
228 ),
229 'trxn_id' => array(
230 'title' => ts('Trans #'),
231 'default' => TRUE,
232 ),
233 ),
234 'filters' => array(
235 'payment_instrument_id' => array(
236 'title' => ts('Payment Instrument'),
237 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
238 'options' => CRM_Contribute_PseudoConstant::paymentInstrument(),
239 ),
240 'currency' => array(
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,
246 ),
247 'trxn_date' => array(
248 'title' => ts('Transaction Date'),
249 'operatorType' => CRM_Report_Form::OP_DATE,
250 'type' => CRM_Utils_Type::T_DATE,
251 ),
252 ),
253 'order_bys' => array(
254 'payment_instrument_id' => array('title' => ts('Payment Instrument')),
255 ),
256 ),
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,
263 'type' => CRM_Utils_Type::T_STRING,
264 ),
265 ),
266 'filters' => array(
267 'amount' => array('title' => ts('Amount')),
268 ),
269 ),
270 );
271
272 $this->_groupFilter = TRUE;
273 $this->_tagFilter = TRUE;
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) {
288 if (!empty($field['required']) ||
289 !empty($this->_params['fields'][$fieldName])
290 ) {
291 switch ($fieldName) {
292 case 'credit_accounting_code' :
293 $select[] = " CASE
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 ";
298 break;
299 case 'amount' :
300 $select[] = " CASE
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 ";
305 break;
306 case 'credit_name' :
307 $select[] = " CASE
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 ";
312 break;
313 default :
314 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
315 break;
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
324 $this->_select = 'SELECT ' . implode(', ', $select) . ' ';
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']}
332 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id AND
333 {$this->_aliases['civicrm_contribution']}.is_test = 0
334 LEFT JOIN civicrm_membership_payment payment
335 ON ( {$this->_aliases['civicrm_contribution']}.id = payment.contribution_id )
336 LEFT JOIN civicrm_membership {$this->_aliases['civicrm_membership']}
337 ON payment.membership_id = {$this->_aliases['civicrm_membership']}.id
338 LEFT JOIN civicrm_entity_financial_trxn {$this->_aliases['civicrm_entity_financial_trxn']}
339 ON ({$this->_aliases['civicrm_contribution']}.id = {$this->_aliases['civicrm_entity_financial_trxn']}.entity_id AND
340 {$this->_aliases['civicrm_entity_financial_trxn']}.entity_table = 'civicrm_contribution')
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
343 LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_debit
344 ON {$this->_aliases['civicrm_financial_trxn']}.to_financial_account_id = {$this->_aliases['civicrm_financial_account']}_debit.id
345 LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_credit_1
346 ON {$this->_aliases['civicrm_financial_trxn']}.from_financial_account_id = {$this->_aliases['civicrm_financial_account']}_credit_1.id
347 LEFT JOIN civicrm_entity_financial_trxn {$this->_aliases['civicrm_entity_financial_trxn']}_item
348 ON ({$this->_aliases['civicrm_financial_trxn']}.id = {$this->_aliases['civicrm_entity_financial_trxn']}_item.financial_trxn_id AND
349 {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_table = 'civicrm_financial_item')
350 LEFT JOIN civicrm_financial_item fitem
351 ON fitem.id = {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_id
352 LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_credit_2
353 ON fitem.financial_account_id = {$this->_aliases['civicrm_financial_account']}_credit_2.id
354 LEFT JOIN civicrm_line_item {$this->_aliases['civicrm_line_item']}
355 ON fitem.entity_id = {$this->_aliases['civicrm_line_item']}.id AND fitem.entity_table = 'civicrm_line_item' ";
356 }
357
358 function orderBy() {
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) {
364 if (!array_key_exists($orderBy['name'], $this->_params['fields']) &&
365 empty($orderBy['section'])
366 ) {
367 $this->_select .= ", {$orderBy['dbAlias']} as {$orderBy['tplField']}";
368 }
369 }
370 }
371
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
381 ELSE financial_account_civireport_credit_2.accounting_code
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
388 ELSE financial_account_civireport_credit_2.id
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);
393 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
394 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
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)) {
416 $this->_where = 'WHERE ( 1 )';
417 }
418 else {
419 $this->_where = 'WHERE ' . implode(' AND ', $clauses);
420 }
421 }
422
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
429 /**
430 * @param $rows
431 *
432 * @return array
433 */
434 function statistics(&$rows) {
435 $statistics = parent::statistics($rows);
436
437 $select = " SELECT COUNT({$this->_aliases['civicrm_financial_trxn']}.id ) as count,
438 {$this->_aliases['civicrm_contribution']}.currency,
439 SUM(CASE
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
443 END) as amount
444";
445
446 $sql = "{$select} {$this->_from} {$this->_where}
447 GROUP BY {$this->_aliases['civicrm_contribution']}.currency
448";
449
450 $dao = CRM_Core_DAO::executeQuery($sql);
451 while ($dao->fetch()) {
452 $amount[] = CRM_Utils_Money::format($dao->amount, $dao->currency);
453 $avg[] = CRM_Utils_Money::format(round(($dao->amount /
454 $dao->count), 2), $dao->currency);
455 }
456
457 $statistics['counts']['amount'] = array(
458 'value' => implode(', ', $amount),
459 'title' => 'Total Amount',
460 'type' => CRM_Utils_Type::T_STRING,
461 );
462 $statistics['counts']['avg'] = array(
463 'value' => implode(', ', $avg),
464 'title' => 'Average',
465 'type' => CRM_Utils_Type::T_STRING,
466 );
467 return $statistics;
468 }
469
470 /**
471 * @param $rows
472 */
473 function alterDisplay(&$rows) {
474 $contributionTypes = CRM_Contribute_PseudoConstant::financialType();
475 $paymentInstruments = CRM_Contribute_PseudoConstant::paymentInstrument();
476 $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus();
477 foreach ($rows as $rowNum => $row) {
478 // convert display name to links
479 if (array_key_exists('civicrm_contact_sort_name', $row) &&
480 !empty($rows[$rowNum]['civicrm_contact_sort_name']) &&
481 array_key_exists('civicrm_contact_id', $row)
482 ) {
483 $url = CRM_Utils_System::url('civicrm/contact/view',
484 'reset=1&cid=' . $row['civicrm_contact_id'],
485 $this->_absoluteUrl
486 );
487 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
488 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts('View Contact Summary for this Contact.');
489 }
490
491 // handle contribution status id
492 if ($value = CRM_Utils_Array::value('civicrm_contribution_contribution_status_id', $row)) {
493 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = $contributionStatus[$value];
494 }
495
496 // handle payment instrument id
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];
499 }
500
501 // handle financial type id
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];
504 }
505 if ($value = CRM_Utils_Array::value('civicrm_entity_financial_trxn_amount', $row)) {
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']);
507 }
508 }
509 }
510}
511