Merge pull request #67 from dpradeep/merge-20140827
[civicrm-core.git] / CRM / Report / Form / Contribute / Bookkeeping.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.5 |
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 */
36 class 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 function __construct() {
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 ),
69 'contact_type' =>
70 array(
71 'title' => ts('Contact Type'),
72 ),
73 'contact_sub_type' =>
74 array(
75 'title' => ts('Contact SubType'),
76 ),
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 ),
89 'order_bys' =>
90 array(
91 'sort_name' => array(
92 'title' => ts('Last Name, First Name'),
93 ),
94 ),
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 ),
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,
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,
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(
146 'title' => ts('Financial Account Code - Credit'),
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(
158 'title' => ts('Financial Account Name - Credit'),
159 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
160 'options' => CRM_Contribute_PseudoConstant::financialAccount(),
161 ),
162 ),
163 ),
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(
172 'financial_type_id' => array(
173 'title' => ts('Financial Type'),
174 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
175 'options' => CRM_Contribute_PseudoConstant::financialType(),
176 ),
177 ),
178 'order_bys' => array(
179 'financial_type_id' => array('title' => ts('Financial Type')),
180 ),
181 ),
182 'civicrm_contribution' =>
183 array(
184 'dao' => 'CRM_Contribute_DAO_Contribution',
185 'fields' =>
186 array(
187 'receive_date' => array(
188 'default' => TRUE
189 ),
190 'invoice_id' => array(
191 'title' => ts('Invoice ID'),
192 'default' => TRUE,
193 ),
194 'contribution_status_id' => array('title' => ts('Contribution Status'),
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),
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 ),
211 ),
212 'order_bys' => array(
213 'contribution_id' => array('title' => ts('Contribution #')),
214 'contribution_status_id' => array('title' => ts('Contribution Status')),
215 ),
216 'grouping' => 'contri-fields',
217 ),
218 'civicrm_financial_trxn' => array(
219 'dao' => 'CRM_Financial_DAO_FinancialTrxn',
220 'fields' => array(
221 'check_number' => array(
222 'title' => ts('Cheque #'),
223 'default' => TRUE,
224 ),
225 'payment_instrument_id' => array('title' => ts('Payment Instrument'),
226 'default' => TRUE,
227 ),
228 'currency' => array(
229 'required' => TRUE,
230 'no_display' => TRUE,
231 ),
232 'trxn_date' => array(
233 'title' => ts('Transaction Date'),
234 'default' => TRUE,
235 'type' => CRM_Utils_Type::T_DATE,
236 ),
237 'trxn_id' => array(
238 'title' => ts('Trans #'),
239 'default' => TRUE,
240 ),
241 ),
242 'filters' =>
243 array(
244 'payment_instrument_id' => array(
245 'title' => ts('Payment Instrument'),
246 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
247 'options' => CRM_Contribute_PseudoConstant::paymentInstrument(),
248 ),
249 'currency' => array(
250 'title' => 'Currency',
251 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
252 'options' => CRM_Core_OptionGroup::values('currencies_enabled'),
253 'default' => NULL,
254 'type' => CRM_Utils_Type::T_STRING,
255 ),
256 'trxn_date' => array(
257 'title' => ts('Transaction Date'),
258 'operatorType' => CRM_Report_Form::OP_DATE,
259 'type' => CRM_Utils_Type::T_DATE,
260 ),
261 ),
262 'order_bys' => array(
263 'payment_instrument_id' => array('title' => ts('Payment Instrument')),
264 ),
265 ),
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,
272 'type' => CRM_Utils_Type::T_STRING,
273 ),
274 ),
275 'filters' =>
276 array(
277 'amount' =>
278 array('title' => ts('Amount')),
279 ),
280 ),
281 );
282
283 $this->_groupFilter = TRUE;
284 $this->_tagFilter = TRUE;
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) {
299 if (!empty($field['required']) || !empty($this->_params['fields'][$fieldName])) {
300 switch ($fieldName) {
301 case 'credit_accounting_code' :
302 $select[] = " CASE
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;
308 case 'amount' :
309 $select[] = " CASE
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' :
316 $select[] = " CASE
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 :
323 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
324 break;
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
333 $this->_select = 'SELECT ' . implode(', ', $select) . ' ';
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']}
341 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id AND
342 {$this->_aliases['civicrm_contribution']}.is_test = 0
343 LEFT JOIN civicrm_membership_payment payment
344 ON ( {$this->_aliases['civicrm_contribution']}.id = payment.contribution_id )
345 LEFT JOIN civicrm_membership {$this->_aliases['civicrm_membership']}
346 ON payment.membership_id = {$this->_aliases['civicrm_membership']}.id
347 LEFT JOIN civicrm_entity_financial_trxn {$this->_aliases['civicrm_entity_financial_trxn']}
348 ON ({$this->_aliases['civicrm_contribution']}.id = {$this->_aliases['civicrm_entity_financial_trxn']}.entity_id AND
349 {$this->_aliases['civicrm_entity_financial_trxn']}.entity_table = 'civicrm_contribution')
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
352 LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_debit
353 ON {$this->_aliases['civicrm_financial_trxn']}.to_financial_account_id = {$this->_aliases['civicrm_financial_account']}_debit.id
354 LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_credit_1
355 ON {$this->_aliases['civicrm_financial_trxn']}.from_financial_account_id = {$this->_aliases['civicrm_financial_account']}_credit_1.id
356 LEFT JOIN civicrm_entity_financial_trxn {$this->_aliases['civicrm_entity_financial_trxn']}_item
357 ON ({$this->_aliases['civicrm_financial_trxn']}.id = {$this->_aliases['civicrm_entity_financial_trxn']}_item.financial_trxn_id AND
358 {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_table = 'civicrm_financial_item')
359 LEFT JOIN civicrm_financial_item fitem
360 ON fitem.id = {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_id
361 LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_credit_2
362 ON fitem.financial_account_id = {$this->_aliases['civicrm_financial_account']}_credit_2.id
363 LEFT JOIN civicrm_line_item {$this->_aliases['civicrm_line_item']}
364 ON fitem.entity_id = {$this->_aliases['civicrm_line_item']}.id AND fitem.entity_table = 'civicrm_line_item' ";
365 }
366
367 function orderBy() {
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) {
373 if (!array_key_exists($orderBy['name'], $this->_params['fields']) && empty($orderBy['section'])) {
374 $this->_select .= ", {$orderBy['dbAlias']} as {$orderBy['tplField']}";
375 }
376 }
377 }
378
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
388 ELSE financial_account_civireport_credit_2.accounting_code
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
395 ELSE financial_account_civireport_credit_2.id
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)) {
423 $this->_where = 'WHERE ( 1 )';
424 }
425 else {
426 $this->_where = 'WHERE ' . implode(' AND ', $clauses);
427 }
428 }
429
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
436 /**
437 * @param $rows
438 *
439 * @return array
440 */
441 function statistics(&$rows) {
442 $statistics = parent::statistics($rows);
443
444 $select = " SELECT COUNT({$this->_aliases['civicrm_financial_trxn']}.id ) as count,
445 {$this->_aliases['civicrm_contribution']}.currency,
446 SUM(CASE
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
450 END) as amount
451 ";
452
453 $sql = "{$select} {$this->_from} {$this->_where}
454 GROUP BY {$this->_aliases['civicrm_contribution']}.currency
455 ";
456
457 $dao = CRM_Core_DAO::executeQuery($sql);
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);
461 }
462
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 );
473 return $statistics;
474 }
475
476 /**
477 * @param $rows
478 */
479 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) && !empty($rows[$rowNum]['civicrm_contact_sort_name']) &&
486 array_key_exists('civicrm_contact_id', $row)
487 ) {
488 $url = CRM_Utils_System::url('civicrm/contact/view',
489 'reset=1&cid=' . $row['civicrm_contact_id'],
490 $this->_absoluteUrl
491 );
492 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
493 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts('View Contact Summary for this Contact.');
494 }
495
496 // handle contribution status id
497 if ($value = CRM_Utils_Array::value('civicrm_contribution_contribution_status_id', $row)) {
498 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = $contributionStatus[$value];
499 }
500
501 // handle payment instrument id
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];
504 }
505
506 // handle financial type id
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];
509 }
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 }
513 }
514 }
515 }
516