a few comment fixes
[civicrm-core.git] / CRM / Report / Form / Contribute / Bookkeeping.php
... / ...
CommitLineData
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26 */
27
28/**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35class 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(
43 'Membership',
44 );
45
46 /**
47 */
48 /**
49 */
50 public function __construct() {
51 $this->_columns = array(
52 'civicrm_contact' => array(
53 'dao' => 'CRM_Contact_DAO_Contact',
54 'fields' => array(
55 'sort_name' => array(
56 'title' => ts('Contact Name'),
57 'required' => TRUE,
58 'no_repeat' => TRUE,
59 ),
60 'id' => array(
61 'no_display' => TRUE,
62 'required' => TRUE,
63 ),
64 'contact_type' => array(
65 'title' => ts('Contact Type'),
66 ),
67 'contact_sub_type' => array(
68 'title' => ts('Contact Subtype'),
69 ),
70 ),
71 'filters' => array(
72 'sort_name' => array(
73 'title' => ts('Contact Name'),
74 'operator' => 'like',
75 ),
76 'id' => array(
77 'title' => ts('Contact ID'),
78 'no_display' => TRUE,
79 ),
80 ),
81 'order_bys' => array(
82 'sort_name' => array(
83 'title' => ts('Last Name, First Name'),
84 ),
85 ),
86 'grouping' => 'contact-fields',
87 ),
88 'civicrm_membership' => array(
89 'dao' => 'CRM_Member_DAO_Membership',
90 'fields' => array(
91 'id' => array(
92 'title' => ts('Membership #'),
93 'no_display' => TRUE,
94 'required' => TRUE,
95 ),
96 ),
97 ),
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'),
103 'name' => 'accounting_code',
104 'alias' => 'financial_account_civireport_debit',
105 'default' => TRUE,
106 ),
107 'credit_accounting_code' => array(
108 'title' => ts('Financial Account Code - Credit'),
109 'name' => 'accounting_code',
110 'alias' => 'financial_account_civireport_credit',
111 'default' => TRUE,
112 ),
113 'debit_name' => array(
114 'title' => ts('Financial Account Name - Debit'),
115 'name' => 'name',
116 'alias' => 'financial_account_civireport_debit',
117 'default' => TRUE,
118 ),
119 'credit_name' => array(
120 'title' => ts('Financial Account Name - Credit'),
121 'name' => 'name',
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'),
131 'name' => 'accounting_code',
132 'alias' => 'financial_account_civireport_debit',
133 ),
134 'credit_accounting_code' => array(
135 'title' => ts('Financial Account Code - Credit'),
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(),
143 'name' => 'id',
144 'alias' => 'financial_account_civireport_debit',
145 ),
146 'credit_name' => array(
147 'title' => ts('Financial Account Name - Credit'),
148 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
149 'options' => CRM_Contribute_PseudoConstant::financialAccount(),
150 ),
151 ),
152 ),
153 'civicrm_line_item' => array(
154 'dao' => 'CRM_Price_DAO_LineItem',
155 'fields' => array(
156 'financial_type_id' => array(
157 'title' => ts('Financial Type'),
158 'default' => TRUE,
159 ),
160 ),
161 'filters' => array(
162 'financial_type_id' => array(
163 'title' => ts('Financial Type'),
164 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
165 'options' => CRM_Contribute_PseudoConstant::financialType(),
166 ),
167 ),
168 'order_bys' => array(
169 'financial_type_id' => array('title' => ts('Financial Type')),
170 ),
171 ),
172 'civicrm_contribution' => array(
173 'dao' => 'CRM_Contribute_DAO_Contribution',
174 'fields' => array(
175 'receive_date' => array(
176 'default' => TRUE,
177 ),
178 'invoice_id' => array(
179 'title' => ts('Invoice ID'),
180 'default' => TRUE,
181 ),
182 'contribution_status_id' => array(
183 'title' => ts('Contribution Status'),
184 'default' => TRUE,
185 ),
186 'id' => array(
187 'title' => ts('Contribution #'),
188 'default' => TRUE,
189 ),
190 ),
191 'filters' => array(
192 'receive_date' => array('operatorType' => CRM_Report_Form::OP_DATE),
193 'contribution_status_id' => array(
194 'title' => ts('Contribution Status'),
195 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
196 'options' => CRM_Contribute_PseudoConstant::contributionStatus(),
197 'default' => array(1),
198 ),
199 ),
200 'order_bys' => array(
201 'contribution_id' => array('title' => ts('Contribution #')),
202 'contribution_status_id' => array('title' => ts('Contribution Status')),
203 ),
204 'grouping' => 'contri-fields',
205 ),
206 'civicrm_financial_trxn' => array(
207 'dao' => 'CRM_Financial_DAO_FinancialTrxn',
208 'fields' => array(
209 'check_number' => array(
210 'title' => ts('Cheque #'),
211 'default' => TRUE,
212 ),
213 'payment_instrument_id' => array(
214 'title' => ts('Payment Instrument'),
215 'default' => TRUE,
216 ),
217 'currency' => array(
218 'required' => TRUE,
219 'no_display' => TRUE,
220 ),
221 'trxn_date' => array(
222 'title' => ts('Transaction Date'),
223 'default' => TRUE,
224 'type' => CRM_Utils_Type::T_DATE,
225 ),
226 'trxn_id' => array(
227 'title' => ts('Trans #'),
228 'default' => TRUE,
229 ),
230 ),
231 'filters' => array(
232 'payment_instrument_id' => array(
233 'title' => ts('Payment Instrument'),
234 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
235 'options' => CRM_Contribute_PseudoConstant::paymentInstrument(),
236 ),
237 'currency' => array(
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,
243 ),
244 'trxn_date' => array(
245 'title' => ts('Transaction Date'),
246 'operatorType' => CRM_Report_Form::OP_DATE,
247 'type' => CRM_Utils_Type::T_DATE,
248 ),
249 ),
250 'order_bys' => array(
251 'payment_instrument_id' => array('title' => ts('Payment Instrument')),
252 ),
253 ),
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,
260 'type' => CRM_Utils_Type::T_STRING,
261 ),
262 ),
263 'filters' => array(
264 'amount' => array('title' => ts('Amount')),
265 ),
266 ),
267 );
268
269 $this->_groupFilter = TRUE;
270 $this->_tagFilter = TRUE;
271 parent::__construct();
272 }
273
274 public function preProcess() {
275 parent::preProcess();
276 }
277
278 public function select() {
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) {
285 if (!empty($field['required']) ||
286 !empty($this->_params['fields'][$fieldName])
287 ) {
288 switch ($fieldName) {
289 case 'credit_accounting_code':
290 $select[] = " CASE
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 ";
295 break;
296
297 case 'amount':
298 $select[] = " CASE
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 ";
303 break;
304
305 case 'credit_name':
306 $select[] = " CASE
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 ";
311 break;
312
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 public 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 public 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 public 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 elseif ($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 public 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 public 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 * 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.
478 */
479 public function alterDisplay(&$rows) {
480 $contributionTypes = CRM_Contribute_PseudoConstant::financialType();
481 $paymentInstruments = CRM_Contribute_PseudoConstant::paymentInstrument();
482 $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus();
483 foreach ($rows as $rowNum => $row) {
484 // convert display name to links
485 if (array_key_exists('civicrm_contact_sort_name', $row) &&
486 !empty($rows[$rowNum]['civicrm_contact_sort_name']) &&
487 array_key_exists('civicrm_contact_id', $row)
488 ) {
489 $url = CRM_Utils_System::url('civicrm/contact/view',
490 'reset=1&cid=' . $row['civicrm_contact_id'],
491 $this->_absoluteUrl
492 );
493 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
494 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts('View Contact Summary for this Contact.');
495 }
496
497 // handle contribution status id
498 if ($value = CRM_Utils_Array::value('civicrm_contribution_contribution_status_id', $row)) {
499 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = $contributionStatus[$value];
500 }
501
502 // handle payment instrument id
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];
505 }
506
507 // handle financial type id
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];
510 }
511 if ($value = CRM_Utils_Array::value('civicrm_entity_financial_trxn_amount', $row)) {
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']);
513 }
514 }
515 }
516
517}