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_batch' => array(
230 'dao' => 'CRM_Batch_DAO_Batch',
233 'title' => ts('Batch Title'),
238 'title' => ts('Batch Name'),
244 'civicrm_contribution' => array(
245 'dao' => 'CRM_Contribute_DAO_Contribution',
247 'receive_date' => array(
250 'invoice_id' => array(
251 'title' => ts('Invoice ID'),
254 'contribution_status_id' => array(
255 'title' => ts('Contribution Status'),
259 'title' => ts('Contribution #'),
264 'receive_date' => array('operatorType' => CRM_Report_Form
::OP_DATE
),
265 'contribution_status_id' => array(
266 'title' => ts('Contribution Status'),
267 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
268 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
269 'default' => array(1),
272 'order_bys' => array(
273 'contribution_id' => array('title' => ts('Contribution #')),
274 'contribution_status_id' => array('title' => ts('Contribution Status')),
276 'grouping' => 'contri-fields',
278 'civicrm_financial_trxn' => array(
279 'dao' => 'CRM_Financial_DAO_FinancialTrxn',
281 'check_number' => array(
282 'title' => ts('Cheque #'),
285 'payment_instrument_id' => array(
286 'title' => ts('Payment Instrument'),
291 'no_display' => TRUE,
293 'trxn_date' => array(
294 'title' => ts('Transaction Date'),
296 'type' => CRM_Utils_Type
::T_DATE
,
299 'title' => ts('Trans #'),
304 'payment_instrument_id' => array(
305 'title' => ts('Payment Instrument'),
306 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
307 'options' => CRM_Contribute_PseudoConstant
::paymentInstrument(),
310 'title' => 'Currency',
311 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
312 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
314 'type' => CRM_Utils_Type
::T_STRING
,
316 'trxn_date' => array(
317 'title' => ts('Transaction Date'),
318 'operatorType' => CRM_Report_Form
::OP_DATE
,
319 'type' => CRM_Utils_Type
::T_DATE
,
322 'order_bys' => array(
323 'payment_instrument_id' => array('title' => ts('Payment Instrument')),
326 'civicrm_entity_financial_trxn' => array(
327 'dao' => 'CRM_Financial_DAO_EntityFinancialTrxn',
330 'title' => ts('Amount'),
332 'type' => CRM_Utils_Type
::T_STRING
,
336 'amount' => array('title' => ts('Amount')),
341 $this->_groupFilter
= TRUE;
342 $this->_tagFilter
= TRUE;
343 parent
::__construct();
346 public function preProcess() {
347 parent
::preProcess();
350 public function select() {
353 $this->_columnHeaders
= array();
354 foreach ($this->_columns
as $tableName => $table) {
355 if (array_key_exists('fields', $table)) {
356 foreach ($table['fields'] as $fieldName => $field) {
357 if (!empty($field['required']) ||
358 !empty($this->_params
['fields'][$fieldName])
360 switch ($fieldName) {
361 case 'credit_accounting_code':
363 WHEN {$this->_aliases['civicrm_financial_trxn']}.from_financial_account_id IS NOT NULL
364 THEN {$this->_aliases['civicrm_financial_account']}_credit_1.accounting_code
365 ELSE {$this->_aliases['civicrm_financial_account']}_credit_2.accounting_code
366 END AS civicrm_financial_account_credit_accounting_code ";
371 WHEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_id IS NOT NULL
372 THEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.amount
373 ELSE {$this->_aliases['civicrm_entity_financial_trxn']}.amount
374 END AS civicrm_entity_financial_trxn_amount ";
379 WHEN {$this->_aliases['civicrm_financial_trxn']}.from_financial_account_id IS NOT NULL
380 THEN {$this->_aliases['civicrm_financial_account']}_credit_1.name
381 ELSE {$this->_aliases['civicrm_financial_account']}_credit_2.name
382 END AS civicrm_financial_account_credit_name ";
386 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
389 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
390 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
396 $this->_select
= 'SELECT ' . implode(', ', $select) . ' ';
399 public function from() {
402 $this->_from
= "FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
403 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
404 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id AND
405 {$this->_aliases['civicrm_contribution']}.is_test = 0
406 LEFT JOIN civicrm_membership_payment payment
407 ON ( {$this->_aliases['civicrm_contribution']}.id = payment.contribution_id )
408 LEFT JOIN civicrm_membership {$this->_aliases['civicrm_membership']}
409 ON payment.membership_id = {$this->_aliases['civicrm_membership']}.id
410 LEFT JOIN civicrm_entity_financial_trxn {$this->_aliases['civicrm_entity_financial_trxn']}
411 ON ({$this->_aliases['civicrm_contribution']}.id = {$this->_aliases['civicrm_entity_financial_trxn']}.entity_id AND
412 {$this->_aliases['civicrm_entity_financial_trxn']}.entity_table = 'civicrm_contribution')
413 LEFT JOIN civicrm_financial_trxn {$this->_aliases['civicrm_financial_trxn']}
414 ON {$this->_aliases['civicrm_financial_trxn']}.id = {$this->_aliases['civicrm_entity_financial_trxn']}.financial_trxn_id
415 LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_debit
416 ON {$this->_aliases['civicrm_financial_trxn']}.to_financial_account_id = {$this->_aliases['civicrm_financial_account']}_debit.id
417 LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_credit_1
418 ON {$this->_aliases['civicrm_financial_trxn']}.from_financial_account_id = {$this->_aliases['civicrm_financial_account']}_credit_1.id
419 LEFT JOIN civicrm_entity_financial_trxn {$this->_aliases['civicrm_entity_financial_trxn']}_item
420 ON ({$this->_aliases['civicrm_financial_trxn']}.id = {$this->_aliases['civicrm_entity_financial_trxn']}_item.financial_trxn_id AND
421 {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_table = 'civicrm_financial_item')
422 LEFT JOIN civicrm_financial_item fitem
423 ON fitem.id = {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_id
424 LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_credit_2
425 ON fitem.financial_account_id = {$this->_aliases['civicrm_financial_account']}_credit_2.id
426 LEFT JOIN civicrm_line_item {$this->_aliases['civicrm_line_item']}
427 ON fitem.entity_id = {$this->_aliases['civicrm_line_item']}.id AND fitem.entity_table = 'civicrm_line_item' ";
428 if ($this->isTableSelected('civicrm_batch')) {
429 $this->_from
.= "LEFT JOIN civicrm_entity_batch ent_batch
430 ON {$this->_aliases['civicrm_financial_trxn']}.id = ent_batch.entity_id AND ent_batch.entity_table = 'civicrm_financial_trxn'
431 LEFT JOIN civicrm_batch batch
432 ON ent_batch.batch_id = batch.id";
436 public function orderBy() {
439 // please note this will just add the order-by columns to select query, and not display in column-headers.
440 // This is a solution to not throw fatal errors when there is a column in order-by, not present in select/display columns.
441 foreach ($this->_orderByFields
as $orderBy) {
442 if (!array_key_exists($orderBy['name'], $this->_params
['fields']) &&
443 empty($orderBy['section'])
445 $this->_select
.= ", {$orderBy['dbAlias']} as {$orderBy['tplField']}";
450 public function where() {
451 foreach ($this->_columns
as $tableName => $table) {
452 if (array_key_exists('filters', $table)) {
453 foreach ($table['filters'] as $fieldName => $field) {
455 if ($fieldName == 'credit_accounting_code') {
456 $field['dbAlias'] = "CASE
457 WHEN financial_trxn_civireport.from_financial_account_id IS NOT NULL
458 THEN financial_account_civireport_credit_1.accounting_code
459 ELSE financial_account_civireport_credit_2.accounting_code
462 elseif ($fieldName == 'credit_name') {
463 $field['dbAlias'] = "CASE
464 WHEN financial_trxn_civireport.from_financial_account_id IS NOT NULL
465 THEN financial_account_civireport_credit_1.id
466 ELSE financial_account_civireport_credit_2.id
469 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
470 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
471 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
472 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
474 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
477 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
479 $clause = $this->whereClause($field,
481 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
482 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
483 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
487 if (!empty($clause)) {
488 $clauses[] = $clause;
493 if (empty($clauses)) {
494 $this->_where
= 'WHERE ( 1 )';
497 $this->_where
= 'WHERE ' . implode(' AND ', $clauses);
501 public function postProcess() {
502 // get the acl clauses built before we assemble the query
503 $this->buildACLClause($this->_aliases
['civicrm_contact']);
504 parent
::postProcess();
507 public function groupBy() {
508 $this->_groupBy
= " GROUP BY {$this->_aliases['civicrm_entity_financial_trxn']}.id, {$this->_aliases['civicrm_line_item']}.id ";
516 public function statistics(&$rows) {
517 $statistics = parent
::statistics($rows);
518 $tempTableName = CRM_Core_DAO
::createTempTableName('civicrm_contribution');
519 $select = "SELECT {$this->_aliases['civicrm_contribution']}.id, {$this->_aliases['civicrm_entity_financial_trxn']}.id as trxnID, {$this->_aliases['civicrm_contribution']}.currency,
521 WHEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_id IS NOT NULL
522 THEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.amount
523 ELSE {$this->_aliases['civicrm_entity_financial_trxn']}.amount
527 $tempQuery = "CREATE TEMPORARY TABLE {$tempTableName} CHARACTER SET utf8 COLLATE utf8_unicode_ci AS
528 {$select} {$this->_from} {$this->_where} {$this->_groupBy} ";
529 CRM_Core_DAO
::executeQuery($tempQuery);
531 $sql = "SELECT COUNT(trxnID) as count, SUM(amount) as amount, currency
532 FROM {$tempTableName}
534 $dao = CRM_Core_DAO
::executeQuery($sql);
535 $amount = $avg = array();
536 while ($dao->fetch()) {
537 $amount[] = CRM_Utils_Money
::format($dao->amount
, $dao->currency
);
538 $avg[] = CRM_Utils_Money
::format(round(($dao->amount
/
539 $dao->count
), 2), $dao->currency
);
542 $statistics['counts']['amount'] = array(
543 'value' => implode(', ', $amount),
544 'title' => 'Total Amount',
545 'type' => CRM_Utils_Type
::T_STRING
,
547 $statistics['counts']['avg'] = array(
548 'value' => implode(', ', $avg),
549 'title' => 'Average',
550 'type' => CRM_Utils_Type
::T_STRING
,
556 * Alter display of rows.
558 * Iterate through the rows retrieved via SQL and make changes for display purposes,
559 * such as rendering contacts as links.
562 * Rows generated by SQL, with an array for each row.
564 public function alterDisplay(&$rows) {
565 $contributionTypes = CRM_Contribute_PseudoConstant
::financialType();
566 $paymentInstruments = CRM_Contribute_PseudoConstant
::paymentInstrument();
567 $contributionStatus = CRM_Contribute_PseudoConstant
::contributionStatus();
568 foreach ($rows as $rowNum => $row) {
569 // convert display name to links
570 if (array_key_exists('civicrm_contact_sort_name', $row) &&
571 !empty($rows[$rowNum]['civicrm_contact_sort_name']) &&
572 array_key_exists('civicrm_contact_id', $row)
574 $url = CRM_Utils_System
::url('civicrm/contact/view',
575 'reset=1&cid=' . $row['civicrm_contact_id'],
578 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
579 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts('View Contact Summary for this Contact.');
582 // handle contribution status id
583 if ($value = CRM_Utils_Array
::value('civicrm_contribution_contribution_status_id', $row)) {
584 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = $contributionStatus[$value];
587 // handle payment instrument id
588 if ($value = CRM_Utils_Array
::value('civicrm_financial_trxn_payment_instrument_id', $row)) {
589 $rows[$rowNum]['civicrm_financial_trxn_payment_instrument_id'] = $paymentInstruments[$value];
592 // handle financial type id
593 if ($value = CRM_Utils_Array
::value('civicrm_line_item_financial_type_id', $row)) {
594 $rows[$rowNum]['civicrm_line_item_financial_type_id'] = $contributionTypes[$value];
596 if ($value = CRM_Utils_Array
::value('civicrm_entity_financial_trxn_amount', $row)) {
597 $rows[$rowNum]['civicrm_entity_financial_trxn_amount'] = CRM_Utils_Money
::format($rows[$rowNum]['civicrm_entity_financial_trxn_amount'], $rows[$rowNum]['civicrm_financial_trxn_currency']);
601 if (array_key_exists('civicrm_contact_gender_id', $row)) {
602 if ($value = $row['civicrm_contact_gender_id']) {
603 $gender = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'gender_id');
604 $rows[$rowNum]['civicrm_contact_gender_id'] = $gender[$value];
609 // display birthday in the configured custom format
610 if (array_key_exists('civicrm_contact_birth_date', $row)) {
611 $birthDate = $row['civicrm_contact_birth_date'];
613 $rows[$rowNum]['civicrm_contact_birth_date'] = CRM_Utils_Date
::customFormat($birthDate, '%Y%m%d');