Merge pull request #176 from yashodha/issue-9394
[civicrm-core.git] / CRM / Report / Form / Contribute / Bookkeeping.php
CommitLineData
6a488035
TO
1<?php
2// $Id$
3
4/*
5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.3 |
7 +--------------------------------------------------------------------+
8 | Copyright CiviCRM LLC (c) 2004-2013 |
9 +--------------------------------------------------------------------+
10 | This file is a part of CiviCRM. |
11 | |
12 | CiviCRM is free software; you can copy, modify, and distribute it |
13 | under the terms of the GNU Affero General Public License |
14 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | |
16 | CiviCRM is distributed in the hope that it will be useful, but |
17 | WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
19 | See the GNU Affero General Public License for more details. |
20 | |
21 | You should have received a copy of the GNU Affero General Public |
22 | License and the CiviCRM Licensing Exception along |
23 | with this program; if not, contact CiviCRM LLC |
24 | at info[AT]civicrm[DOT]org. If you have questions about the |
25 | GNU Affero General Public License or the licensing of CiviCRM, |
26 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
27 +--------------------------------------------------------------------+
28*/
29
30/**
31 *
32 * @package CRM
33 * @copyright CiviCRM LLC (c) 2004-2013
34 * $Id$
35 *
36 */
37class CRM_Report_Form_Contribute_Bookkeeping extends CRM_Report_Form {
38 protected $_addressField = FALSE;
39
40 protected $_emailField = FALSE;
41
42 protected $_summary = NULL;
43
44 protected $_customGroupExtends = array(
4d9dd529
PN
45 'Membership');
46
47 function __construct() {
6a488035
TO
48 $this->_columns = array(
49 'civicrm_contact' =>
50 array(
51 'dao' => 'CRM_Contact_DAO_Contact',
52 'fields' =>
53 array(
54 'sort_name' =>
55 array('title' => ts('Contact Name'),
56 'required' => TRUE,
57 'no_repeat' => TRUE,
58 ),
59 'id' =>
60 array(
61 'no_display' => TRUE,
62 'required' => TRUE,
63 ),
64 ),
65 'filters' =>
66 array(
67 'sort_name' =>
68 array('title' => ts('Contact Name'),
69 'operator' => 'like',
70 ),
71 'id' =>
72 array('title' => ts('Contact ID'),
73 'no_display' => TRUE,
74 ),
75 ),
76 'grouping' => 'contact-fields',
77 ),
78 'civicrm_membership' =>
79 array(
80 'dao' => 'CRM_Member_DAO_Membership',
81 'fields' =>
82 array(
83 'id' =>
84 array('title' => ts('Membership #'),
85 'no_display' => TRUE,
86 'required' => TRUE,
87 ),
88 ),
89 ),
086ca649
PN
90 'civicrm_financial_account' => array(
91 'dao' => 'CRM_Financial_DAO_FinancialAccount',
92 'fields' => array(
93 'debit_accounting_code' => array(
94 'title' => ts('Financial Account Code - Debit'),
95 'name' => 'accounting_code',
96 'alias' => 'financial_account_civireport_debit',
97 'default' => TRUE,
086ca649
PN
98 ),
99 'credit_accounting_code' => array(
100 'title' => ts('Financial Account Code - Credit'),
101 'name' => 'accounting_code',
102 'alias' => 'financial_account_civireport_credit',
103 'default' => TRUE,
086ca649
PN
104 ),
105 'debit_name' => array(
106 'title' => ts('Financial Account Name - Debit'),
107 'name' => 'name',
108 'alias' => 'financial_account_civireport_debit',
109 'default' => TRUE,
110 ),
111 'credit_name' => array(
112 'title' => ts('Financial Account Name - Credit'),
113 'name' => 'name',
114 'alias' => 'financial_account_civireport_credit',
115 'default' => TRUE,
116 ),
117 ),
118 'filters' => array(
119 'debit_accounting_code' => array(
120 'title' => ts('Financial Account Code - Debit'),
121 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
122 'options' => CRM_Contribute_PseudoConstant::financialAccount(NULL, NULL, 'accounting_code', 'accounting_code'),
123 'name' => 'accounting_code',
124 'alias' => 'financial_account_civireport_debit',
125 ),
126 'credit_accounting_code' => array(
127 'title' => ts('Financial Account Code - Credit'),
128 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
129 'options' => CRM_Contribute_PseudoConstant::financialAccount(NULL, NULL, 'accounting_code', 'accounting_code'),
130 ),
131 'debit_name' => array(
132 'title' => ts('Financial Account Name - Debit'),
133 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
134 'options' => CRM_Contribute_PseudoConstant::financialAccount(),
135 'name' => 'id',
136 'alias' => 'financial_account_civireport_debit',
137 ),
138 'credit_name' => array(
139 'title' => ts('Financial Account Name - Credit'),
140 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
141 'options' => CRM_Contribute_PseudoConstant::financialAccount(),
142 ),
143 ),
144 ),
145 'civicrm_line_item' => array(
146 'dao' => 'CRM_Price_DAO_LineItem',
147 'fields' => array(
148 'financial_type_id' => array('title' => ts('Financial Type'),
149 'default' => TRUE,
150 ),
151 ),
152 'filters' => array(
153 'financial_type_id' => array(
154 'title' => ts('Financial Type'),
155 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
156 'options' => CRM_Contribute_PseudoConstant::financialType(),
157 ),
158 ),
159 ),
6a488035
TO
160 'civicrm_contribution' =>
161 array(
162 'dao' => 'CRM_Contribute_DAO_Contribution',
163 'fields' =>
164 array(
6e3c3a7d
PN
165 'receive_date' => array(
166 'default' => TRUE
6a488035 167 ),
6e3c3a7d
PN
168 'invoice_id' => array(
169 'title' => ts('Invoice ID'),
6a488035
TO
170 'default' => TRUE,
171 ),
6e3c3a7d 172 'contribution_status_id' => array('title' => ts('Contribution Status'),
6a488035
TO
173 'default' => TRUE,
174 ),
175 'id' => array('title' => ts('Contribution #'),
176 'default' => TRUE,
177 ),
178 ),
179 'filters' =>
180 array(
181 'receive_date' =>
182 array('operatorType' => CRM_Report_Form::OP_DATE),
6a488035
TO
183 'contribution_status_id' =>
184 array('title' => ts('Contribution Status'),
185 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
186 'options' => CRM_Contribute_PseudoConstant::contributionStatus(),
187 'default' => array(1),
188 ),
6a488035
TO
189 ),
190 'grouping' => 'contri-fields',
086ca649 191 ),
4d9dd529
PN
192 'civicrm_financial_trxn' => array(
193 'dao' => 'CRM_Financial_DAO_FinancialTrxn',
194 'fields' => array(
195 'check_number' => array(
196 'title' => ts('Cheque #'),
6a488035
TO
197 'default' => TRUE,
198 ),
4d9dd529
PN
199 'payment_instrument_id' => array('title' => ts('Payment Instrument'),
200 'default' => TRUE,
201 ),
652a526e 202 'currency' => array(
203 'required' => TRUE,
204 'no_display' => TRUE,
205 ),
6e3c3a7d
PN
206 'trxn_date' => array(
207 'title' => ts('Transaction Date'),
086ca649
PN
208 'default' => TRUE,
209 'type' => CRM_Utils_Type::T_DATE,
6e3c3a7d
PN
210 ),
211 'trxn_id' => array(
212 'title' => ts('Trans #'),
213 'default' => TRUE,
214 ),
4d9dd529
PN
215 ),
216 'filters' =>
217 array(
218 'payment_instrument_id' => array(
6e3c3a7d 219 'title' => ts('Payment Instrument'),
4d9dd529
PN
220 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
221 'options' => CRM_Contribute_PseudoConstant::paymentInstrument(),
222 ),
652a526e 223 'currency' => array(
224 'title' => 'Currency',
225 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
226 'options' => CRM_Core_OptionGroup::values('currencies_enabled'),
227 'type' => CRM_Utils_Type::T_STRING,
228 ),
6e3c3a7d
PN
229 'trxn_date' => array(
230 'title' => ts('Transaction Date'),
231 'operatorType' => CRM_Report_Form::OP_DATE,
232 'type' => CRM_Utils_Type::T_DATE,
233 ),
4d9dd529
PN
234 ),
235 ),
6a488035
TO
236 'civicrm_entity_financial_trxn' => array(
237 'dao' => 'CRM_Financial_DAO_EntityFinancialTrxn',
238 'fields' => array(
239 'amount' => array(
240 'title' => ts('Amount'),
241 'default' => TRUE,
652a526e 242 'type' => CRM_Utils_Type::T_STRING,
6a488035
TO
243 ),
244 ),
4d9dd529
PN
245 'filters' =>
246 array(
247 'amount' =>
248 array('title' => ts('Amount')),
249 ),
4d9dd529 250 ),
6a488035
TO
251 );
252 parent::__construct();
253 }
254
255 function preProcess() {
256 parent::preProcess();
257 }
258
259 function select() {
260 $select = array();
261
262 $this->_columnHeaders = array();
263 foreach ($this->_columns as $tableName => $table) {
264 if (array_key_exists('fields', $table)) {
265 foreach ($table['fields'] as $fieldName => $field) {
266 if (CRM_Utils_Array::value('required', $field) ||
267 CRM_Utils_Array::value($fieldName, $this->_params['fields'])
268 ) {
086ca649
PN
269 switch ($fieldName) {
270 case 'credit_accounting_code' :
271 $select[] = " CASE
272 WHEN {$this->_aliases['civicrm_financial_trxn']}.from_financial_account_id IS NOT NULL
273 THEN {$this->_aliases['civicrm_financial_account']}_credit_1.accounting_code
274 ELSE {$this->_aliases['civicrm_financial_account']}_credit_2.accounting_code
275 END AS civicrm_financial_account_credit_accounting_code ";
276 break;
277 case 'amount' :
278 $select[] = " CASE
279 WHEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_id IS NOT NULL
280 THEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.amount
281 ELSE {$this->_aliases['civicrm_entity_financial_trxn']}.amount
282 END AS civicrm_entity_financial_trxn_amount ";
283 break;
284 case 'credit_name' :
285 $select[] = " CASE
286 WHEN {$this->_aliases['civicrm_financial_trxn']}.from_financial_account_id IS NOT NULL
287 THEN {$this->_aliases['civicrm_financial_account']}_credit_1.name
288 ELSE {$this->_aliases['civicrm_financial_account']}_credit_2.name
289 END AS civicrm_financial_account_credit_name ";
290 break;
291 default :
6a488035 292 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
086ca649 293 break;
6a488035
TO
294 }
295 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title'];
296 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
297 }
298 }
299 }
300 }
301
652a526e 302 $this->_select = 'SELECT ' . implode(', ', $select) . ' ';
6a488035
TO
303 }
304
305 function from() {
306 $this->_from = NULL;
307
308 $this->_from = "FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
309 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
086ca649 310 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id AND
6a488035
TO
311 {$this->_aliases['civicrm_contribution']}.is_test = 0
312 LEFT JOIN civicrm_membership_payment payment
086ca649 313 ON ( {$this->_aliases['civicrm_contribution']}.id = payment.contribution_id )
6a488035
TO
314 LEFT JOIN civicrm_membership {$this->_aliases['civicrm_membership']}
315 ON payment.membership_id = {$this->_aliases['civicrm_membership']}.id
316 LEFT JOIN civicrm_entity_financial_trxn {$this->_aliases['civicrm_entity_financial_trxn']}
317 ON ({$this->_aliases['civicrm_contribution']}.id = {$this->_aliases['civicrm_entity_financial_trxn']}.entity_id AND
318 {$this->_aliases['civicrm_entity_financial_trxn']}.entity_table = 'civicrm_contribution')
4d9dd529
PN
319 LEFT JOIN civicrm_financial_trxn {$this->_aliases['civicrm_financial_trxn']}
320 ON {$this->_aliases['civicrm_financial_trxn']}.id = {$this->_aliases['civicrm_entity_financial_trxn']}.financial_trxn_id
6a488035 321 LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_debit
4d9dd529 322 ON {$this->_aliases['civicrm_financial_trxn']}.to_financial_account_id = {$this->_aliases['civicrm_financial_account']}_debit.id
6a488035 323 LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_credit_1
4d9dd529 324 ON {$this->_aliases['civicrm_financial_trxn']}.from_financial_account_id = {$this->_aliases['civicrm_financial_account']}_credit_1.id
6a488035 325 LEFT JOIN civicrm_entity_financial_trxn {$this->_aliases['civicrm_entity_financial_trxn']}_item
4d9dd529 326 ON ({$this->_aliases['civicrm_financial_trxn']}.id = {$this->_aliases['civicrm_entity_financial_trxn']}_item.financial_trxn_id AND
6a488035 327 {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_table = 'civicrm_financial_item')
4d9dd529 328 LEFT JOIN civicrm_financial_item fitem
6a488035 329 ON fitem.id = {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_id
4d9dd529 330 LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_credit_2
6a488035 331 ON fitem.financial_account_id = {$this->_aliases['civicrm_financial_account']}_credit_2.id
4d9dd529 332 LEFT JOIN civicrm_line_item {$this->_aliases['civicrm_line_item']}
6a488035
TO
333 ON fitem.entity_id = {$this->_aliases['civicrm_line_item']}.id AND fitem.entity_table = 'civicrm_line_item' ";
334 }
335
336 function orderBy() {
337 $this->_orderBy = " ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_contribution']}.id, {$this->_aliases['civicrm_entity_financial_trxn']}.id ";
338 }
339
086ca649
PN
340 function where() {
341 foreach ($this->_columns as $tableName => $table) {
342 if (array_key_exists('filters', $table)) {
343 foreach ($table['filters'] as $fieldName => $field) {
344 $clause = NULL;
345 if ($fieldName == 'credit_accounting_code') {
346 $field['dbAlias'] = "CASE
347 WHEN financial_trxn_civireport.from_financial_account_id IS NOT NULL
348 THEN financial_account_civireport_credit_1.accounting_code
349 ELSE financial_account_civireport_credit_2.accounting_code
350 END";
351 }
352 else if ($fieldName == 'credit_name') {
353 $field['dbAlias'] = "CASE
354 WHEN financial_trxn_civireport.from_financial_account_id IS NOT NULL
355 THEN financial_account_civireport_credit_1.id
356 ELSE financial_account_civireport_credit_2.id
357 END";
358 }
359 if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) {
360 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
361 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
362 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
363
364 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
365 }
366 else {
367 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
368 if ($op) {
369 $clause = $this->whereClause($field,
370 $op,
371 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
372 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
373 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
374 );
375 }
376 }
377 if (!empty($clause)) {
378 $clauses[] = $clause;
379 }
380 }
381 }
382 }
383 if (empty($clauses)) {
652a526e 384 $this->_where = 'WHERE ( 1 )';
086ca649
PN
385 }
386 else {
652a526e 387 $this->_where = 'WHERE ' . implode(' AND ', $clauses);
086ca649
PN
388 }
389 }
390
6a488035
TO
391 function postProcess() {
392 // get the acl clauses built before we assemble the query
393 $this->buildACLClause($this->_aliases['civicrm_contact']);
394 parent::postProcess();
395 }
396
397 function statistics(&$rows) {
398 $statistics = parent::statistics($rows);
399
4d9dd529 400 $select = " SELECT COUNT({$this->_aliases['civicrm_financial_trxn']}.id ) as count,
652a526e 401 {$this->_aliases['civicrm_contribution']}.currency,
4d9dd529
PN
402 SUM(CASE
403 WHEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_id IS NOT NULL
404 THEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.amount
405 ELSE {$this->_aliases['civicrm_entity_financial_trxn']}.amount
652a526e 406 END) as amount
407";
408
409 $sql = "{$select} {$this->_from} {$this->_where}
410 GROUP BY {$this->_aliases['civicrm_contribution']}.currency
411";
6a488035 412
6a488035 413 $dao = CRM_Core_DAO::executeQuery($sql);
652a526e 414 while ($dao->fetch()) {
415 $amount[] = CRM_Utils_Money::format($dao->amount, $dao->currency);
416 $avg[] = CRM_Utils_Money::format(round(($dao->amount / $dao->count), 2), $dao->currency);
6a488035
TO
417 }
418
652a526e 419 $statistics['counts']['amount'] = array(
420 'value' => implode(', ', $amount),
421 'title' => 'Total Amount',
422 'type' => CRM_Utils_Type::T_STRING,
423 );
424 $statistics['counts']['avg'] = array(
425 'value' => implode(', ', $avg),
426 'title' => 'Average',
427 'type' => CRM_Utils_Type::T_STRING,
428 );
6a488035
TO
429 return $statistics;
430 }
431
432 function alterDisplay(&$rows) {
6a488035
TO
433 $contributionTypes = CRM_Contribute_PseudoConstant::financialType();
434 $paymentInstruments = CRM_Contribute_PseudoConstant::paymentInstrument();
4d9dd529 435 $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus();
6a488035 436 foreach ($rows as $rowNum => $row) {
6a488035
TO
437 // convert display name to links
438 if (array_key_exists('civicrm_contact_sort_name', $row) &&
439 CRM_Utils_Array::value('civicrm_contact_sort_name', $rows[$rowNum]) &&
440 array_key_exists('civicrm_contact_id', $row)
441 ) {
652a526e 442 $url = CRM_Utils_System::url('civicrm/contact/view',
6a488035
TO
443 'reset=1&cid=' . $row['civicrm_contact_id'],
444 $this->_absoluteUrl
445 );
446 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
652a526e 447 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts('View Contact Summary for this Contact.');
6a488035
TO
448 }
449
450 // handle contribution status id
4d9dd529
PN
451 if ($value = CRM_Utils_Array::value('civicrm_contribution_contribution_status_id', $row)) {
452 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = $contributionStatus[$value];
6a488035
TO
453 }
454
455 // handle payment instrument id
4d9dd529
PN
456 if ($value = CRM_Utils_Array::value('civicrm_financial_trxn_payment_instrument_id', $row)) {
457 $rows[$rowNum]['civicrm_financial_trxn_payment_instrument_id'] = $paymentInstruments[$value];
6a488035 458 }
4d9dd529
PN
459
460 // handle financial type id
6a488035
TO
461 if ($value = CRM_Utils_Array::value('civicrm_line_item_financial_type_id', $row)) {
462 $rows[$rowNum]['civicrm_line_item_financial_type_id'] = $contributionTypes[$value];
6a488035 463 }
652a526e 464 if ($value = CRM_Utils_Array::value('civicrm_entity_financial_trxn_amount', $row)) {
465 $rows[$rowNum]['civicrm_entity_financial_trxn_amount'] = CRM_Utils_Money::format($rows[$rowNum]['civicrm_entity_financial_trxn_amount'],$rows[$rowNum]['civicrm_financial_trxn_currency']);
466 }
6a488035
TO
467 }
468 }
469}
470