4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.4 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2013
36 class CRM_Report_Form_Contribute_Bookkeeping
extends CRM_Report_Form
{
37 protected $_addressField = FALSE;
39 protected $_emailField = FALSE;
41 protected $_summary = NULL;
43 protected $_customGroupExtends = array(
46 function __construct() {
47 $this->_columns
= array(
50 'dao' => 'CRM_Contact_DAO_Contact',
54 array('title' => ts('Contact Name'),
65 'title' => ts('Contact Type'),
69 'title' => ts('Contact SubType'),
75 array('title' => ts('Contact Name'),
79 array('title' => ts('Contact ID'),
86 'title' => ts('Last Name, First Name'),
89 'grouping' => 'contact-fields',
91 'civicrm_membership' =>
93 'dao' => 'CRM_Member_DAO_Membership',
97 array('title' => ts('Membership #'),
103 'civicrm_financial_account' => array(
104 'dao' => 'CRM_Financial_DAO_FinancialAccount',
106 'debit_accounting_code' => array(
107 'title' => ts('Financial Account Code - Debit'),
108 'name' => 'accounting_code',
109 'alias' => 'financial_account_civireport_debit',
112 'credit_accounting_code' => array(
113 'title' => ts('Financial Account Code - Credit'),
114 'name' => 'accounting_code',
115 'alias' => 'financial_account_civireport_credit',
118 'debit_name' => array(
119 'title' => ts('Financial Account Name - Debit'),
121 'alias' => 'financial_account_civireport_debit',
124 'credit_name' => array(
125 'title' => ts('Financial Account Name - Credit'),
127 'alias' => 'financial_account_civireport_credit',
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',
139 'credit_accounting_code' => array(
140 'title' => ts('Financial Account Code - Credit'),
141 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
142 'options' => CRM_Contribute_PseudoConstant
::financialAccount(NULL, NULL, 'accounting_code', 'accounting_code'),
144 'debit_name' => array(
145 'title' => ts('Financial Account Name - Debit'),
146 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
147 'options' => CRM_Contribute_PseudoConstant
::financialAccount(),
149 'alias' => 'financial_account_civireport_debit',
151 'credit_name' => array(
152 'title' => ts('Financial Account Name - Credit'),
153 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
154 'options' => CRM_Contribute_PseudoConstant
::financialAccount(),
158 'civicrm_line_item' => array(
159 'dao' => 'CRM_Price_DAO_LineItem',
161 'financial_type_id' => array('title' => ts('Financial Type'),
166 'financial_type_id' => array(
167 'title' => ts('Financial Type'),
168 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
169 'options' => CRM_Contribute_PseudoConstant
::financialType(),
172 'order_bys' => array(
173 'financial_type_id' => array('title' => ts('Financial Type')),
176 'civicrm_contribution' =>
178 'dao' => 'CRM_Contribute_DAO_Contribution',
181 'receive_date' => array(
184 'invoice_id' => array(
185 'title' => ts('Invoice ID'),
188 'contribution_status_id' => array('title' => ts('Contribution Status'),
191 'id' => array('title' => ts('Contribution #'),
198 array('operatorType' => CRM_Report_Form
::OP_DATE
),
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),
206 'order_bys' => array(
207 'contribution_id' => array('title' => ts('Contribution #')),
208 'contribution_status_id' => array('title' => ts('Contribution Status')),
210 'grouping' => 'contri-fields',
212 'civicrm_financial_trxn' => array(
213 'dao' => 'CRM_Financial_DAO_FinancialTrxn',
215 'check_number' => array(
216 'title' => ts('Cheque #'),
219 'payment_instrument_id' => array('title' => ts('Payment Instrument'),
224 'no_display' => TRUE,
226 'trxn_date' => array(
227 'title' => ts('Transaction Date'),
229 'type' => CRM_Utils_Type
::T_DATE
,
232 'title' => ts('Trans #'),
238 'payment_instrument_id' => array(
239 'title' => ts('Payment Instrument'),
240 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
241 'options' => CRM_Contribute_PseudoConstant
::paymentInstrument(),
244 'title' => 'Currency',
245 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
246 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
248 'type' => CRM_Utils_Type
::T_STRING
,
250 'trxn_date' => array(
251 'title' => ts('Transaction Date'),
252 'operatorType' => CRM_Report_Form
::OP_DATE
,
253 'type' => CRM_Utils_Type
::T_DATE
,
256 'order_bys' => array(
257 'payment_instrument_id' => array('title' => ts('Payment Instrument')),
260 'civicrm_entity_financial_trxn' => array(
261 'dao' => 'CRM_Financial_DAO_EntityFinancialTrxn',
264 'title' => ts('Amount'),
266 'type' => CRM_Utils_Type
::T_STRING
,
272 array('title' => ts('Amount')),
277 'dao' => 'CRM_Contact_DAO_Group',
283 'name' => 'group_id',
284 'title' => ts('Group'),
285 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
287 'options' => CRM_Core_PseudoConstant
::group(),
293 $this->_tagFilter
= TRUE;
294 parent
::__construct();
297 function preProcess() {
298 parent
::preProcess();
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'])
311 switch ($fieldName) {
312 case 'credit_accounting_code' :
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 ";
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 ";
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 ";
334 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
337 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
338 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
344 $this->_select
= 'SELECT ' . implode(', ', $select) . ' ';
350 $this->_from
= "FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
351 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
352 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id AND
353 {$this->_aliases['civicrm_contribution']}.is_test = 0
354 LEFT JOIN civicrm_membership_payment payment
355 ON ( {$this->_aliases['civicrm_contribution']}.id = payment.contribution_id )
356 LEFT JOIN civicrm_membership {$this->_aliases['civicrm_membership']}
357 ON payment.membership_id = {$this->_aliases['civicrm_membership']}.id
358 LEFT JOIN civicrm_entity_financial_trxn {$this->_aliases['civicrm_entity_financial_trxn']}
359 ON ({$this->_aliases['civicrm_contribution']}.id = {$this->_aliases['civicrm_entity_financial_trxn']}.entity_id AND
360 {$this->_aliases['civicrm_entity_financial_trxn']}.entity_table = 'civicrm_contribution')
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
363 LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_debit
364 ON {$this->_aliases['civicrm_financial_trxn']}.to_financial_account_id = {$this->_aliases['civicrm_financial_account']}_debit.id
365 LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_credit_1
366 ON {$this->_aliases['civicrm_financial_trxn']}.from_financial_account_id = {$this->_aliases['civicrm_financial_account']}_credit_1.id
367 LEFT JOIN civicrm_entity_financial_trxn {$this->_aliases['civicrm_entity_financial_trxn']}_item
368 ON ({$this->_aliases['civicrm_financial_trxn']}.id = {$this->_aliases['civicrm_entity_financial_trxn']}_item.financial_trxn_id AND
369 {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_table = 'civicrm_financial_item')
370 LEFT JOIN civicrm_financial_item fitem
371 ON fitem.id = {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_id
372 LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_credit_2
373 ON fitem.financial_account_id = {$this->_aliases['civicrm_financial_account']}_credit_2.id
374 LEFT JOIN civicrm_line_item {$this->_aliases['civicrm_line_item']}
375 ON fitem.entity_id = {$this->_aliases['civicrm_line_item']}.id AND fitem.entity_table = 'civicrm_line_item' ";
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']}";
392 foreach ($this->_columns
as $tableName => $table) {
393 if (array_key_exists('filters', $table)) {
394 foreach ($table['filters'] as $fieldName => $field) {
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
400 ELSE financial_account_civireport_credit_2.accounting_code
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
407 ELSE financial_account_civireport_credit_2.id
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
);
415 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
418 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
420 $clause = $this->whereClause($field,
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
)
428 if (!empty($clause)) {
429 $clauses[] = $clause;
434 if (empty($clauses)) {
435 $this->_where
= 'WHERE ( 1 )';
438 $this->_where
= 'WHERE ' . implode(' AND ', $clauses);
442 function postProcess() {
443 // get the acl clauses built before we assemble the query
444 $this->buildACLClause($this->_aliases
['civicrm_contact']);
445 parent
::postProcess();
448 function statistics(&$rows) {
449 $statistics = parent
::statistics($rows);
451 $select = " SELECT COUNT({$this->_aliases['civicrm_financial_trxn']}.id ) as count,
452 {$this->_aliases['civicrm_contribution']}.currency,
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
460 $sql = "{$select} {$this->_from} {$this->_where}
461 GROUP BY {$this->_aliases['civicrm_contribution']}.currency
464 $dao = CRM_Core_DAO
::executeQuery($sql);
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
);
470 $statistics['counts']['amount'] = array(
471 'value' => implode(', ', $amount),
472 'title' => 'Total Amount',
473 'type' => CRM_Utils_Type
::T_STRING
,
475 $statistics['counts']['avg'] = array(
476 'value' => implode(', ', $avg),
477 'title' => 'Average',
478 'type' => CRM_Utils_Type
::T_STRING
,
483 function alterDisplay(&$rows) {
484 $contributionTypes = CRM_Contribute_PseudoConstant
::financialType();
485 $paymentInstruments = CRM_Contribute_PseudoConstant
::paymentInstrument();
486 $contributionStatus = CRM_Contribute_PseudoConstant
::contributionStatus();
487 foreach ($rows as $rowNum => $row) {
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)
493 $url = CRM_Utils_System
::url('civicrm/contact/view',
494 'reset=1&cid=' . $row['civicrm_contact_id'],
497 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
498 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts('View Contact Summary for this Contact.');
501 // handle contribution status id
502 if ($value = CRM_Utils_Array
::value('civicrm_contribution_contribution_status_id', $row)) {
503 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = $contributionStatus[$value];
506 // handle payment instrument id
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];
511 // handle financial type id
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];
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']);