3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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-2016
33 class CRM_Report_Form_Contribute_Bookkeeping
extends CRM_Report_Form
{
34 protected $_addressField = FALSE;
36 protected $_emailField = FALSE;
38 protected $_summary = NULL;
40 protected $_customGroupExtends = array(
48 * This report has not been optimised for group filtering.
50 * The functionality for group filtering has been improved but not
51 * all reports have been adjusted to take care of it. This report has not
52 * and will run an inefficient query until fixed.
58 protected $groupFilterNotOptimised = TRUE;
63 public function __construct() {
64 $this->_autoIncludeIndexedFieldsAsOrderBys
= 1;
65 $this->_columns
= array(
66 'civicrm_contact' => array(
67 'dao' => 'CRM_Contact_DAO_Contact',
70 'title' => ts('Contact Name'),
74 'first_name' => array(
75 'title' => ts('First Name'),
77 'middle_name' => array(
78 'title' => ts('Middle Name'),
81 'title' => ts('Last Name'),
88 'title' => ts('Gender'),
90 'birth_date' => array(
91 'title' => ts('Birth Date'),
95 'dbAlias' => 'TIMESTAMPDIFF(YEAR, contact_civireport.birth_date, CURDATE())',
97 'contact_type' => array(
98 'title' => ts('Contact Type'),
100 'contact_sub_type' => array(
101 'title' => ts('Contact Subtype'),
104 'grouping' => 'contact-fields',
105 'order_bys' => array(
106 'sort_name' => array(
107 'title' => ts('Last Name, First Name'),
109 'default_weight' => '0',
110 'default_order' => 'ASC',
112 'first_name' => array(
113 'name' => 'first_name',
114 'title' => ts('First Name'),
116 'gender_id' => array(
117 'name' => 'gender_id',
118 'title' => ts('Gender'),
120 'birth_date' => array(
121 'name' => 'birth_date',
122 'title' => ts('Birth Date'),
124 'contact_type' => array(
125 'title' => ts('Contact Type'),
127 'contact_sub_type' => array(
128 'title' => ts('Contact Subtype'),
132 'sort_name' => array(
133 'title' => ts('Contact Name'),
134 'operator' => 'like',
137 'title' => ts('Contact ID'),
138 'no_display' => TRUE,
140 'gender_id' => array(
141 'title' => ts('Gender'),
142 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
143 'options' => CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'gender_id'),
145 'birth_date' => array(
146 'title' => ts('Birth Date'),
147 'operatorType' => CRM_Report_Form
::OP_DATE
,
149 'contact_type' => array(
150 'title' => ts('Contact Type'),
152 'contact_sub_type' => array(
153 'title' => ts('Contact Subtype'),
157 'civicrm_membership' => array(
158 'dao' => 'CRM_Member_DAO_Membership',
161 'title' => ts('Membership #'),
162 'no_display' => TRUE,
167 'civicrm_financial_account' => array(
168 'dao' => 'CRM_Financial_DAO_FinancialAccount',
170 'debit_accounting_code' => array(
171 'title' => ts('Financial Account Code - Debit'),
172 'name' => 'accounting_code',
173 'alias' => 'financial_account_civireport_debit',
176 'credit_accounting_code' => array(
177 'title' => ts('Financial Account Code - Credit'),
178 'name' => 'accounting_code',
179 'alias' => 'financial_account_civireport_credit',
182 'debit_name' => array(
183 'title' => ts('Financial Account Name - Debit'),
185 'alias' => 'financial_account_civireport_debit',
188 'credit_name' => array(
189 'title' => ts('Financial Account Name - Credit'),
191 'alias' => 'financial_account_civireport_credit',
196 'debit_accounting_code' => array(
197 'title' => ts('Financial Account Code - Debit'),
198 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
199 'options' => CRM_Contribute_PseudoConstant
::financialAccount(NULL, NULL, 'accounting_code', 'accounting_code'),
200 'name' => 'accounting_code',
201 'alias' => 'financial_account_civireport_debit',
203 'credit_accounting_code' => array(
204 'title' => ts('Financial Account Code - Credit'),
205 'type' => CRM_Utils_Type
::T_INT
,
206 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
207 'options' => CRM_Contribute_PseudoConstant
::financialAccount(NULL, NULL, 'accounting_code', 'accounting_code'),
209 'debit_name' => array(
210 'title' => ts('Financial Account Name - Debit'),
211 'type' => CRM_Utils_Type
::T_STRING
,
212 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
213 'options' => CRM_Contribute_PseudoConstant
::financialAccount(),
215 'alias' => 'financial_account_civireport_debit',
217 'credit_name' => array(
218 'title' => ts('Financial Account Name - Credit'),
219 'type' => CRM_Utils_Type
::T_STRING
,
220 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
221 'options' => CRM_Contribute_PseudoConstant
::financialAccount(),
225 'civicrm_line_item' => array(
226 'dao' => 'CRM_Price_DAO_LineItem',
228 'financial_type_id' => array(
229 'title' => ts('Financial Type'),
234 'financial_type_id' => array(
235 'title' => ts('Financial Type'),
236 'type' => CRM_Utils_Type
::T_INT
,
237 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
238 'options' => CRM_Financial_BAO_FinancialType
::getAvailableFinancialTypes(),
241 'order_bys' => array(
242 'financial_type_id' => array('title' => ts('Financial Type')),
245 'civicrm_batch' => array(
246 'dao' => 'CRM_Batch_DAO_Batch',
249 'title' => ts('Batch Title'),
254 'title' => ts('Batch Name'),
260 'civicrm_contribution' => array(
261 'dao' => 'CRM_Contribute_DAO_Contribution',
263 'receive_date' => array(
266 'invoice_id' => array(
267 'title' => ts('Invoice ID'),
270 'contribution_status_id' => array(
271 'title' => ts('Contribution Status'),
275 'title' => ts('Contribution #'),
280 'receive_date' => array('operatorType' => CRM_Report_Form
::OP_DATE
),
281 'contribution_status_id' => array(
282 'title' => ts('Contribution Status'),
283 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
284 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
285 'default' => array(1),
288 'order_bys' => array(
289 'contribution_id' => array('title' => ts('Contribution #')),
290 'contribution_status_id' => array('title' => ts('Contribution Status')),
292 'grouping' => 'contri-fields',
294 'civicrm_financial_trxn' => array(
295 'dao' => 'CRM_Financial_DAO_FinancialTrxn',
297 'check_number' => array(
298 'title' => ts('Cheque #'),
301 'payment_instrument_id' => array(
302 'title' => ts('Payment Method'),
307 'no_display' => TRUE,
309 'trxn_date' => array(
310 'title' => ts('Transaction Date'),
312 'type' => CRM_Utils_Type
::T_DATE
,
315 'title' => ts('Trans #'),
320 'payment_instrument_id' => array(
321 'title' => ts('Payment Method'),
322 'type' => CRM_Utils_Type
::T_INT
,
323 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
324 'options' => CRM_Contribute_PseudoConstant
::paymentInstrument(),
327 'title' => ts('Currency'),
328 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
329 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
331 'type' => CRM_Utils_Type
::T_STRING
,
333 'trxn_date' => array(
334 'title' => ts('Transaction Date'),
335 'operatorType' => CRM_Report_Form
::OP_DATE
,
336 'type' => CRM_Utils_Type
::T_DATE
,
339 'order_bys' => array(
340 'payment_instrument_id' => array('title' => ts('Payment Method')),
343 'civicrm_entity_financial_trxn' => array(
344 'dao' => 'CRM_Financial_DAO_EntityFinancialTrxn',
347 'title' => ts('Amount'),
349 'type' => CRM_Utils_Type
::T_STRING
,
353 'amount' => array('title' => ts('Amount')),
358 $this->_groupFilter
= TRUE;
359 $this->_tagFilter
= TRUE;
360 parent
::__construct();
363 public function preProcess() {
364 parent
::preProcess();
367 public function select() {
370 $this->_columnHeaders
= array();
371 foreach ($this->_columns
as $tableName => $table) {
372 if (array_key_exists('fields', $table)) {
373 foreach ($table['fields'] as $fieldName => $field) {
374 if (!empty($field['required']) ||
375 !empty($this->_params
['fields'][$fieldName])
377 switch ($fieldName) {
378 case 'credit_accounting_code':
380 WHEN {$this->_aliases['civicrm_financial_trxn']}.from_financial_account_id IS NOT NULL
381 THEN {$this->_aliases['civicrm_financial_account']}_credit_1.accounting_code
382 ELSE {$this->_aliases['civicrm_financial_account']}_credit_2.accounting_code
383 END AS civicrm_financial_account_credit_accounting_code ";
388 WHEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_id IS NOT NULL
389 THEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.amount
390 ELSE {$this->_aliases['civicrm_entity_financial_trxn']}.amount
391 END AS civicrm_entity_financial_trxn_amount ";
396 WHEN {$this->_aliases['civicrm_financial_trxn']}.from_financial_account_id IS NOT NULL
397 THEN {$this->_aliases['civicrm_financial_account']}_credit_1.name
398 ELSE {$this->_aliases['civicrm_financial_account']}_credit_2.name
399 END AS civicrm_financial_account_credit_name ";
403 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
406 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
407 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
412 $this->_selectClauses
= $select;
414 $this->_select
= 'SELECT ' . implode(', ', $select) . ' ';
417 public function from() {
420 $this->_from
= "FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
421 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
422 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id AND
423 {$this->_aliases['civicrm_contribution']}.is_test = 0
424 LEFT JOIN civicrm_membership_payment payment
425 ON ( {$this->_aliases['civicrm_contribution']}.id = payment.contribution_id )
426 LEFT JOIN civicrm_membership {$this->_aliases['civicrm_membership']}
427 ON payment.membership_id = {$this->_aliases['civicrm_membership']}.id
428 LEFT JOIN civicrm_entity_financial_trxn {$this->_aliases['civicrm_entity_financial_trxn']}
429 ON ({$this->_aliases['civicrm_contribution']}.id = {$this->_aliases['civicrm_entity_financial_trxn']}.entity_id AND
430 {$this->_aliases['civicrm_entity_financial_trxn']}.entity_table = 'civicrm_contribution')
431 LEFT JOIN civicrm_financial_trxn {$this->_aliases['civicrm_financial_trxn']}
432 ON {$this->_aliases['civicrm_financial_trxn']}.id = {$this->_aliases['civicrm_entity_financial_trxn']}.financial_trxn_id
433 LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_debit
434 ON {$this->_aliases['civicrm_financial_trxn']}.to_financial_account_id = {$this->_aliases['civicrm_financial_account']}_debit.id
435 LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_credit_1
436 ON {$this->_aliases['civicrm_financial_trxn']}.from_financial_account_id = {$this->_aliases['civicrm_financial_account']}_credit_1.id
437 LEFT JOIN civicrm_entity_financial_trxn {$this->_aliases['civicrm_entity_financial_trxn']}_item
438 ON ({$this->_aliases['civicrm_financial_trxn']}.id = {$this->_aliases['civicrm_entity_financial_trxn']}_item.financial_trxn_id AND
439 {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_table = 'civicrm_financial_item')
440 LEFT JOIN civicrm_financial_item fitem
441 ON fitem.id = {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_id
442 LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_credit_2
443 ON fitem.financial_account_id = {$this->_aliases['civicrm_financial_account']}_credit_2.id
444 LEFT JOIN civicrm_line_item {$this->_aliases['civicrm_line_item']}
445 ON fitem.entity_id = {$this->_aliases['civicrm_line_item']}.id AND fitem.entity_table = 'civicrm_line_item' ";
446 if ($this->isTableSelected('civicrm_batch')) {
447 $this->_from
.= "LEFT JOIN civicrm_entity_batch ent_batch
448 ON {$this->_aliases['civicrm_financial_trxn']}.id = ent_batch.entity_id AND ent_batch.entity_table = 'civicrm_financial_trxn'
449 LEFT JOIN civicrm_batch batch
450 ON ent_batch.batch_id = batch.id";
453 $this->getPermissionedFTQuery($this, "civicrm_line_item_1");
456 public function orderBy() {
459 // please note this will just add the order-by columns to select query, and not display in column-headers.
460 // This is a solution to not throw fatal errors when there is a column in order-by, not present in select/display columns.
461 foreach ($this->_orderByFields
as $orderBy) {
462 if (!array_key_exists($orderBy['name'], $this->_params
['fields']) &&
463 empty($orderBy['section'])
465 $this->_select
.= ", {$orderBy['dbAlias']} as {$orderBy['tplField']}";
470 public function where() {
471 foreach ($this->_columns
as $tableName => $table) {
472 if (array_key_exists('filters', $table)) {
473 foreach ($table['filters'] as $fieldName => $field) {
475 if ($fieldName == 'credit_accounting_code') {
476 $field['dbAlias'] = "CASE
477 WHEN financial_trxn_civireport.from_financial_account_id IS NOT NULL
478 THEN financial_account_civireport_credit_1.accounting_code
479 ELSE financial_account_civireport_credit_2.accounting_code
482 elseif ($fieldName == 'credit_name') {
483 $field['dbAlias'] = "CASE
484 WHEN financial_trxn_civireport.from_financial_account_id IS NOT NULL
485 THEN financial_account_civireport_credit_1.id
486 ELSE financial_account_civireport_credit_2.id
489 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
490 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
491 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
492 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
494 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
497 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
499 $clause = $this->whereClause($field,
501 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
502 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
503 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
507 if (!empty($clause)) {
508 $clauses[] = $clause;
513 if (empty($clauses)) {
514 $this->_where
= 'WHERE ( 1 )';
517 $this->_where
= 'WHERE ' . implode(' AND ', $clauses);
521 public function postProcess() {
522 // get the acl clauses built before we assemble the query
523 $this->buildACLClause($this->_aliases
['civicrm_contact']);
524 parent
::postProcess();
527 public function groupBy() {
529 "{$this->_aliases['civicrm_entity_financial_trxn']}.id",
530 "{$this->_aliases['civicrm_line_item']}.id",
532 $this->_groupBy
= CRM_Contact_BAO_Query
::getGroupByFromSelectColumns($this->_selectClauses
, $groupBy);
540 public function statistics(&$rows) {
541 $statistics = parent
::statistics($rows);
542 $tempTableName = CRM_Core_DAO
::createTempTableName('civicrm_contribution');
543 $financialSelect = "CASE WHEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_id IS NOT NULL
544 THEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.amount
545 ELSE {$this->_aliases['civicrm_entity_financial_trxn']}.amount
548 $this->_selectClauses
= array(
549 "{$this->_aliases['civicrm_contribution']}.id",
550 "{$this->_aliases['civicrm_entity_financial_trxn']}.id as trxnID",
551 "{$this->_aliases['civicrm_contribution']}.currency",
554 $select = "SELECT " . implode(', ', $this->_selectClauses
);
558 $tempQuery = "CREATE TEMPORARY TABLE {$tempTableName} CHARACTER SET utf8 COLLATE utf8_unicode_ci AS
559 {$select} {$this->_from} {$this->_where} {$this->_groupBy} ";
560 CRM_Core_DAO
::executeQuery($tempQuery);
562 $sql = "SELECT COUNT(trxnID) as count, SUM(amount) as amount, currency
563 FROM {$tempTableName}
565 $dao = CRM_Core_DAO
::executeQuery($sql);
566 $amount = $avg = array();
567 while ($dao->fetch()) {
568 $amount[] = CRM_Utils_Money
::format($dao->amount
, $dao->currency
);
569 $avg[] = CRM_Utils_Money
::format(round(($dao->amount
/
570 $dao->count
), 2), $dao->currency
);
573 $statistics['counts']['amount'] = array(
574 'value' => implode(', ', $amount),
575 'title' => ts('Total Amount'),
576 'type' => CRM_Utils_Type
::T_STRING
,
578 $statistics['counts']['avg'] = array(
579 'value' => implode(', ', $avg),
580 'title' => ts('Average'),
581 'type' => CRM_Utils_Type
::T_STRING
,
587 * Alter display of rows.
589 * Iterate through the rows retrieved via SQL and make changes for display purposes,
590 * such as rendering contacts as links.
593 * Rows generated by SQL, with an array for each row.
595 public function alterDisplay(&$rows) {
596 $contributionTypes = CRM_Contribute_PseudoConstant
::financialType();
597 $paymentInstruments = CRM_Contribute_PseudoConstant
::paymentInstrument();
598 $contributionStatus = CRM_Contribute_PseudoConstant
::contributionStatus();
599 foreach ($rows as $rowNum => $row) {
600 // convert display name to links
601 if (array_key_exists('civicrm_contact_sort_name', $row) &&
602 !empty($rows[$rowNum]['civicrm_contact_sort_name']) &&
603 array_key_exists('civicrm_contact_id', $row)
605 $url = CRM_Utils_System
::url('civicrm/contact/view',
606 'reset=1&cid=' . $row['civicrm_contact_id'],
609 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
610 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts('View Contact Summary for this Contact.');
613 // handle contribution status id
614 if ($value = CRM_Utils_Array
::value('civicrm_contribution_contribution_status_id', $row)) {
615 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = $contributionStatus[$value];
618 // handle payment instrument id
619 if ($value = CRM_Utils_Array
::value('civicrm_financial_trxn_payment_instrument_id', $row)) {
620 $rows[$rowNum]['civicrm_financial_trxn_payment_instrument_id'] = $paymentInstruments[$value];
623 // handle financial type id
624 if ($value = CRM_Utils_Array
::value('civicrm_line_item_financial_type_id', $row)) {
625 $rows[$rowNum]['civicrm_line_item_financial_type_id'] = $contributionTypes[$value];
627 if ($value = CRM_Utils_Array
::value('civicrm_entity_financial_trxn_amount', $row)) {
628 $rows[$rowNum]['civicrm_entity_financial_trxn_amount'] = CRM_Utils_Money
::format($rows[$rowNum]['civicrm_entity_financial_trxn_amount'], $rows[$rowNum]['civicrm_financial_trxn_currency']);
632 if (array_key_exists('civicrm_contact_gender_id', $row)) {
633 if ($value = $row['civicrm_contact_gender_id']) {
634 $gender = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'gender_id');
635 $rows[$rowNum]['civicrm_contact_gender_id'] = $gender[$value];
640 // display birthday in the configured custom format
641 if (array_key_exists('civicrm_contact_birth_date', $row)) {
642 $birthDate = $row['civicrm_contact_birth_date'];
644 $rows[$rowNum]['civicrm_contact_birth_date'] = CRM_Utils_Date
::customFormat($birthDate, '%Y%m%d');