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