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