3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2015
35 class CRM_Report_Form_Contribute_Bookkeeping
extends CRM_Report_Form
{
36 protected $_addressField = FALSE;
38 protected $_emailField = FALSE;
40 protected $_summary = NULL;
42 protected $_customGroupExtends = array(
51 public function __construct() {
52 $this->_autoIncludeIndexedFieldsAsOrderBys
= 1;
53 $this->_columns
= array(
54 'civicrm_contact' => array(
55 'dao' => 'CRM_Contact_DAO_Contact',
58 'title' => ts('Contact Name'),
62 'first_name' => array(
63 'title' => ts('First Name'),
65 'middle_name' => array(
66 'title' => ts('Middle Name'),
69 'title' => ts('Last Name'),
76 'title' => ts('Gender'),
78 'birth_date' => array(
79 'title' => ts('Birth Date'),
83 'dbAlias' => 'TIMESTAMPDIFF(YEAR, contact_civireport.birth_date, CURDATE())',
85 'contact_type' => array(
86 'title' => ts('Contact Type'),
88 'contact_sub_type' => array(
89 'title' => ts('Contact Subtype'),
92 'grouping' => 'contact-fields',
95 'title' => ts('Last Name, First Name'),
97 'default_weight' => '0',
98 'default_order' => 'ASC',
100 'first_name' => array(
101 'name' => 'first_name',
102 'title' => ts('First Name'),
104 'gender_id' => array(
105 'name' => 'gender_id',
106 'title' => ts('Gender'),
108 'birth_date' => array(
109 'name' => 'birth_date',
110 'title' => ts('Birth Date'),
112 'contact_type' => array(
113 'title' => ts('Contact Type'),
115 'contact_sub_type' => array(
116 'title' => ts('Contact Subtype'),
120 'sort_name' => array(
121 'title' => ts('Contact Name'),
122 'operator' => 'like',
125 'title' => ts('Contact ID'),
126 'no_display' => TRUE,
128 'gender_id' => array(
129 'title' => ts('Gender'),
130 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
131 'options' => CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'gender_id'),
133 'birth_date' => array(
134 'title' => ts('Birth Date'),
135 'operatorType' => CRM_Report_Form
::OP_DATE
,
137 'contact_type' => array(
138 'title' => ts('Contact Type'),
140 'contact_sub_type' => array(
141 'title' => ts('Contact Subtype'),
145 'civicrm_membership' => array(
146 'dao' => 'CRM_Member_DAO_Membership',
149 'title' => ts('Membership #'),
150 'no_display' => TRUE,
155 'civicrm_financial_account' => array(
156 'dao' => 'CRM_Financial_DAO_FinancialAccount',
158 'debit_accounting_code' => array(
159 'title' => ts('Financial Account Code - Debit'),
160 'name' => 'accounting_code',
161 'alias' => 'financial_account_civireport_debit',
164 'credit_accounting_code' => array(
165 'title' => ts('Financial Account Code - Credit'),
166 'name' => 'accounting_code',
167 'alias' => 'financial_account_civireport_credit',
170 'debit_name' => array(
171 'title' => ts('Financial Account Name - Debit'),
173 'alias' => 'financial_account_civireport_debit',
176 'credit_name' => array(
177 'title' => ts('Financial Account Name - Credit'),
179 'alias' => 'financial_account_civireport_credit',
184 'debit_accounting_code' => array(
185 'title' => ts('Financial Account Code - Debit'),
186 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
187 'options' => CRM_Contribute_PseudoConstant
::financialAccount(NULL, NULL, 'accounting_code', 'accounting_code'),
188 'name' => 'accounting_code',
189 'alias' => 'financial_account_civireport_debit',
191 'credit_accounting_code' => array(
192 'title' => ts('Financial Account Code - Credit'),
193 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
194 'options' => CRM_Contribute_PseudoConstant
::financialAccount(NULL, NULL, 'accounting_code', 'accounting_code'),
196 'debit_name' => array(
197 'title' => ts('Financial Account Name - Debit'),
198 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
199 'options' => CRM_Contribute_PseudoConstant
::financialAccount(),
201 'alias' => 'financial_account_civireport_debit',
203 'credit_name' => array(
204 'title' => ts('Financial Account Name - Credit'),
205 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
206 'options' => CRM_Contribute_PseudoConstant
::financialAccount(),
210 'civicrm_line_item' => array(
211 'dao' => 'CRM_Price_DAO_LineItem',
213 'financial_type_id' => array(
214 'title' => ts('Financial Type'),
219 'financial_type_id' => array(
220 'title' => ts('Financial Type'),
221 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
222 'options' => CRM_Contribute_PseudoConstant
::financialType(),
225 'order_bys' => array(
226 'financial_type_id' => array('title' => ts('Financial Type')),
229 'civicrm_contribution' => array(
230 'dao' => 'CRM_Contribute_DAO_Contribution',
232 'receive_date' => array(
235 'invoice_id' => array(
236 'title' => ts('Invoice ID'),
239 'contribution_status_id' => array(
240 'title' => ts('Contribution Status'),
244 'title' => ts('Contribution #'),
249 'receive_date' => array('operatorType' => CRM_Report_Form
::OP_DATE
),
250 'contribution_status_id' => array(
251 'title' => ts('Contribution Status'),
252 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
253 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
254 'default' => array(1),
257 'order_bys' => array(
258 'contribution_id' => array('title' => ts('Contribution #')),
259 'contribution_status_id' => array('title' => ts('Contribution Status')),
261 'grouping' => 'contri-fields',
263 'civicrm_financial_trxn' => array(
264 'dao' => 'CRM_Financial_DAO_FinancialTrxn',
266 'check_number' => array(
267 'title' => ts('Cheque #'),
270 'payment_instrument_id' => array(
271 'title' => ts('Payment Instrument'),
276 'no_display' => TRUE,
278 'trxn_date' => array(
279 'title' => ts('Transaction Date'),
281 'type' => CRM_Utils_Type
::T_DATE
,
284 'title' => ts('Trans #'),
289 'payment_instrument_id' => array(
290 'title' => ts('Payment Instrument'),
291 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
292 'options' => CRM_Contribute_PseudoConstant
::paymentInstrument(),
295 'title' => 'Currency',
296 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
297 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
299 'type' => CRM_Utils_Type
::T_STRING
,
301 'trxn_date' => array(
302 'title' => ts('Transaction Date'),
303 'operatorType' => CRM_Report_Form
::OP_DATE
,
304 'type' => CRM_Utils_Type
::T_DATE
,
307 'order_bys' => array(
308 'payment_instrument_id' => array('title' => ts('Payment Instrument')),
311 'civicrm_entity_financial_trxn' => array(
312 'dao' => 'CRM_Financial_DAO_EntityFinancialTrxn',
315 'title' => ts('Amount'),
317 'type' => CRM_Utils_Type
::T_STRING
,
321 'amount' => array('title' => ts('Amount')),
326 $this->_groupFilter
= TRUE;
327 $this->_tagFilter
= TRUE;
328 parent
::__construct();
331 public function preProcess() {
332 parent
::preProcess();
335 public function select() {
338 $this->_columnHeaders
= array();
339 foreach ($this->_columns
as $tableName => $table) {
340 if (array_key_exists('fields', $table)) {
341 foreach ($table['fields'] as $fieldName => $field) {
342 if (!empty($field['required']) ||
343 !empty($this->_params
['fields'][$fieldName])
345 switch ($fieldName) {
346 case 'credit_accounting_code':
348 WHEN {$this->_aliases['civicrm_financial_trxn']}.from_financial_account_id IS NOT NULL
349 THEN {$this->_aliases['civicrm_financial_account']}_credit_1.accounting_code
350 ELSE {$this->_aliases['civicrm_financial_account']}_credit_2.accounting_code
351 END AS civicrm_financial_account_credit_accounting_code ";
356 WHEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_id IS NOT NULL
357 THEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.amount
358 ELSE {$this->_aliases['civicrm_entity_financial_trxn']}.amount
359 END AS civicrm_entity_financial_trxn_amount ";
364 WHEN {$this->_aliases['civicrm_financial_trxn']}.from_financial_account_id IS NOT NULL
365 THEN {$this->_aliases['civicrm_financial_account']}_credit_1.name
366 ELSE {$this->_aliases['civicrm_financial_account']}_credit_2.name
367 END AS civicrm_financial_account_credit_name ";
371 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
374 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
375 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
381 $this->_select
= 'SELECT ' . implode(', ', $select) . ' ';
384 public function from() {
387 $this->_from
= "FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
388 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
389 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id AND
390 {$this->_aliases['civicrm_contribution']}.is_test = 0
391 LEFT JOIN civicrm_membership_payment payment
392 ON ( {$this->_aliases['civicrm_contribution']}.id = payment.contribution_id )
393 LEFT JOIN civicrm_membership {$this->_aliases['civicrm_membership']}
394 ON payment.membership_id = {$this->_aliases['civicrm_membership']}.id
395 LEFT JOIN civicrm_entity_financial_trxn {$this->_aliases['civicrm_entity_financial_trxn']}
396 ON ({$this->_aliases['civicrm_contribution']}.id = {$this->_aliases['civicrm_entity_financial_trxn']}.entity_id AND
397 {$this->_aliases['civicrm_entity_financial_trxn']}.entity_table = 'civicrm_contribution')
398 LEFT JOIN civicrm_financial_trxn {$this->_aliases['civicrm_financial_trxn']}
399 ON {$this->_aliases['civicrm_financial_trxn']}.id = {$this->_aliases['civicrm_entity_financial_trxn']}.financial_trxn_id
400 LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_debit
401 ON {$this->_aliases['civicrm_financial_trxn']}.to_financial_account_id = {$this->_aliases['civicrm_financial_account']}_debit.id
402 LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_credit_1
403 ON {$this->_aliases['civicrm_financial_trxn']}.from_financial_account_id = {$this->_aliases['civicrm_financial_account']}_credit_1.id
404 LEFT JOIN civicrm_entity_financial_trxn {$this->_aliases['civicrm_entity_financial_trxn']}_item
405 ON ({$this->_aliases['civicrm_financial_trxn']}.id = {$this->_aliases['civicrm_entity_financial_trxn']}_item.financial_trxn_id AND
406 {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_table = 'civicrm_financial_item')
407 LEFT JOIN civicrm_financial_item fitem
408 ON fitem.id = {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_id
409 LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_credit_2
410 ON fitem.financial_account_id = {$this->_aliases['civicrm_financial_account']}_credit_2.id
411 LEFT JOIN civicrm_line_item {$this->_aliases['civicrm_line_item']}
412 ON fitem.entity_id = {$this->_aliases['civicrm_line_item']}.id AND fitem.entity_table = 'civicrm_line_item' ";
415 public function orderBy() {
418 // please note this will just add the order-by columns to select query, and not display in column-headers.
419 // This is a solution to not throw fatal errors when there is a column in order-by, not present in select/display columns.
420 foreach ($this->_orderByFields
as $orderBy) {
421 if (!array_key_exists($orderBy['name'], $this->_params
['fields']) &&
422 empty($orderBy['section'])
424 $this->_select
.= ", {$orderBy['dbAlias']} as {$orderBy['tplField']}";
429 public function where() {
430 foreach ($this->_columns
as $tableName => $table) {
431 if (array_key_exists('filters', $table)) {
432 foreach ($table['filters'] as $fieldName => $field) {
434 if ($fieldName == 'credit_accounting_code') {
435 $field['dbAlias'] = "CASE
436 WHEN financial_trxn_civireport.from_financial_account_id IS NOT NULL
437 THEN financial_account_civireport_credit_1.accounting_code
438 ELSE financial_account_civireport_credit_2.accounting_code
441 elseif ($fieldName == 'credit_name') {
442 $field['dbAlias'] = "CASE
443 WHEN financial_trxn_civireport.from_financial_account_id IS NOT NULL
444 THEN financial_account_civireport_credit_1.id
445 ELSE financial_account_civireport_credit_2.id
448 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
449 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
450 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
451 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
453 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
456 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
458 $clause = $this->whereClause($field,
460 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
461 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
462 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
466 if (!empty($clause)) {
467 $clauses[] = $clause;
472 if (empty($clauses)) {
473 $this->_where
= 'WHERE ( 1 )';
476 $this->_where
= 'WHERE ' . implode(' AND ', $clauses);
480 public function postProcess() {
481 // get the acl clauses built before we assemble the query
482 $this->buildACLClause($this->_aliases
['civicrm_contact']);
483 parent
::postProcess();
486 public function groupBy() {
487 $this->_groupBy
= " GROUP BY {$this->_aliases['civicrm_contribution']}.id, {$this->_aliases['civicrm_line_item']}.id ";
495 public function statistics(&$rows) {
496 $statistics = parent
::statistics($rows);
497 $tempTableName = CRM_Core_DAO
::createTempTableName('civicrm_contribution');
498 $select = "SELECT {$this->_aliases['civicrm_contribution']}.id, {$this->_aliases['civicrm_entity_financial_trxn']}.id as trxnID, {$this->_aliases['civicrm_contribution']}.currency,
500 WHEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_id IS NOT NULL
501 THEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.amount
502 ELSE {$this->_aliases['civicrm_entity_financial_trxn']}.amount
506 $tempQuery = "CREATE TEMPORARY TABLE {$tempTableName} CHARACTER SET utf8 COLLATE utf8_unicode_ci AS
507 {$select} {$this->_from} {$this->_where} {$this->_groupBy} ";
508 CRM_Core_DAO
::executeQuery($tempQuery);
510 $sql = "SELECT COUNT(trxnID) as count, SUM(amount) as amount, currency
511 FROM {$tempTableName}
513 $dao = CRM_Core_DAO
::executeQuery($sql);
514 $amount = $avg = array();
515 while ($dao->fetch()) {
516 $amount[] = CRM_Utils_Money
::format($dao->amount
, $dao->currency
);
517 $avg[] = CRM_Utils_Money
::format(round(($dao->amount
/
518 $dao->count
), 2), $dao->currency
);
521 $statistics['counts']['amount'] = array(
522 'value' => implode(', ', $amount),
523 'title' => 'Total Amount',
524 'type' => CRM_Utils_Type
::T_STRING
,
526 $statistics['counts']['avg'] = array(
527 'value' => implode(', ', $avg),
528 'title' => 'Average',
529 'type' => CRM_Utils_Type
::T_STRING
,
535 * Alter display of rows.
537 * Iterate through the rows retrieved via SQL and make changes for display purposes,
538 * such as rendering contacts as links.
541 * Rows generated by SQL, with an array for each row.
543 public function alterDisplay(&$rows) {
544 $contributionTypes = CRM_Contribute_PseudoConstant
::financialType();
545 $paymentInstruments = CRM_Contribute_PseudoConstant
::paymentInstrument();
546 $contributionStatus = CRM_Contribute_PseudoConstant
::contributionStatus();
547 foreach ($rows as $rowNum => $row) {
548 // convert display name to links
549 if (array_key_exists('civicrm_contact_sort_name', $row) &&
550 !empty($rows[$rowNum]['civicrm_contact_sort_name']) &&
551 array_key_exists('civicrm_contact_id', $row)
553 $url = CRM_Utils_System
::url('civicrm/contact/view',
554 'reset=1&cid=' . $row['civicrm_contact_id'],
557 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
558 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts('View Contact Summary for this Contact.');
561 // handle contribution status id
562 if ($value = CRM_Utils_Array
::value('civicrm_contribution_contribution_status_id', $row)) {
563 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = $contributionStatus[$value];
566 // handle payment instrument id
567 if ($value = CRM_Utils_Array
::value('civicrm_financial_trxn_payment_instrument_id', $row)) {
568 $rows[$rowNum]['civicrm_financial_trxn_payment_instrument_id'] = $paymentInstruments[$value];
571 // handle financial type id
572 if ($value = CRM_Utils_Array
::value('civicrm_line_item_financial_type_id', $row)) {
573 $rows[$rowNum]['civicrm_line_item_financial_type_id'] = $contributionTypes[$value];
575 if ($value = CRM_Utils_Array
::value('civicrm_entity_financial_trxn_amount', $row)) {
576 $rows[$rowNum]['civicrm_entity_financial_trxn_amount'] = CRM_Utils_Money
::format($rows[$rowNum]['civicrm_entity_financial_trxn_amount'], $rows[$rowNum]['civicrm_financial_trxn_currency']);
580 if (array_key_exists('civicrm_contact_gender_id', $row)) {
581 if ($value = $row['civicrm_contact_gender_id']) {
582 $gender = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'gender_id');
583 $rows[$rowNum]['civicrm_contact_gender_id'] = $gender[$value];
588 // display birthday in the configured custom format
589 if (array_key_exists('civicrm_contact_birth_date', $row)) {
590 $birthDate = $row['civicrm_contact_birth_date'];
592 $rows[$rowNum]['civicrm_contact_birth_date'] = CRM_Utils_Date
::customFormat($birthDate, '%Y%m%d');