3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
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_Financial_BAO_FinancialType
::getAvailableFinancialTypes(),
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 Method'),
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 Method'),
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 Method')),
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' ";
413 $this->getPermissionedFTQuery($this, "civicrm_line_item_1");
416 public function orderBy() {
419 // please note this will just add the order-by columns to select query, and not display in column-headers.
420 // This is a solution to not throw fatal errors when there is a column in order-by, not present in select/display columns.
421 foreach ($this->_orderByFields
as $orderBy) {
422 if (!array_key_exists($orderBy['name'], $this->_params
['fields']) &&
423 empty($orderBy['section'])
425 $this->_select
.= ", {$orderBy['dbAlias']} as {$orderBy['tplField']}";
430 public function where() {
431 foreach ($this->_columns
as $tableName => $table) {
432 if (array_key_exists('filters', $table)) {
433 foreach ($table['filters'] as $fieldName => $field) {
435 if ($fieldName == 'credit_accounting_code') {
436 $field['dbAlias'] = "CASE
437 WHEN financial_trxn_civireport.from_financial_account_id IS NOT NULL
438 THEN financial_account_civireport_credit_1.accounting_code
439 ELSE financial_account_civireport_credit_2.accounting_code
442 elseif ($fieldName == 'credit_name') {
443 $field['dbAlias'] = "CASE
444 WHEN financial_trxn_civireport.from_financial_account_id IS NOT NULL
445 THEN financial_account_civireport_credit_1.id
446 ELSE financial_account_civireport_credit_2.id
449 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
450 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
451 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
452 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
454 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
457 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
459 $clause = $this->whereClause($field,
461 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
462 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
463 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
467 if (!empty($clause)) {
468 $clauses[] = $clause;
473 if (empty($clauses)) {
474 $this->_where
= 'WHERE ( 1 )';
477 $this->_where
= 'WHERE ' . implode(' AND ', $clauses);
481 public function postProcess() {
482 // get the acl clauses built before we assemble the query
483 $this->buildACLClause($this->_aliases
['civicrm_contact']);
484 parent
::postProcess();
487 public function groupBy() {
488 $this->_groupBy
= " GROUP BY {$this->_aliases['civicrm_entity_financial_trxn']}.id, {$this->_aliases['civicrm_line_item']}.id ";
496 public function statistics(&$rows) {
497 $statistics = parent
::statistics($rows);
498 $tempTableName = CRM_Core_DAO
::createTempTableName('civicrm_contribution');
499 $select = "SELECT {$this->_aliases['civicrm_contribution']}.id, {$this->_aliases['civicrm_entity_financial_trxn']}.id as trxnID, {$this->_aliases['civicrm_contribution']}.currency,
501 WHEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_id IS NOT NULL
502 THEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.amount
503 ELSE {$this->_aliases['civicrm_entity_financial_trxn']}.amount
507 $tempQuery = "CREATE TEMPORARY TABLE {$tempTableName} CHARACTER SET utf8 COLLATE utf8_unicode_ci AS
508 {$select} {$this->_from} {$this->_where} {$this->_groupBy} ";
509 CRM_Core_DAO
::executeQuery($tempQuery);
511 $sql = "SELECT COUNT(trxnID) as count, SUM(amount) as amount, currency
512 FROM {$tempTableName}
514 $dao = CRM_Core_DAO
::executeQuery($sql);
515 $amount = $avg = array();
516 while ($dao->fetch()) {
517 $amount[] = CRM_Utils_Money
::format($dao->amount
, $dao->currency
);
518 $avg[] = CRM_Utils_Money
::format(round(($dao->amount
/
519 $dao->count
), 2), $dao->currency
);
522 $statistics['counts']['amount'] = array(
523 'value' => implode(', ', $amount),
524 'title' => 'Total Amount',
525 'type' => CRM_Utils_Type
::T_STRING
,
527 $statistics['counts']['avg'] = array(
528 'value' => implode(', ', $avg),
529 'title' => 'Average',
530 'type' => CRM_Utils_Type
::T_STRING
,
536 * Alter display of rows.
538 * Iterate through the rows retrieved via SQL and make changes for display purposes,
539 * such as rendering contacts as links.
542 * Rows generated by SQL, with an array for each row.
544 public function alterDisplay(&$rows) {
545 $contributionTypes = CRM_Contribute_PseudoConstant
::financialType();
546 $paymentInstruments = CRM_Contribute_PseudoConstant
::paymentInstrument();
547 $contributionStatus = CRM_Contribute_PseudoConstant
::contributionStatus();
548 foreach ($rows as $rowNum => $row) {
549 // convert display name to links
550 if (array_key_exists('civicrm_contact_sort_name', $row) &&
551 !empty($rows[$rowNum]['civicrm_contact_sort_name']) &&
552 array_key_exists('civicrm_contact_id', $row)
554 $url = CRM_Utils_System
::url('civicrm/contact/view',
555 'reset=1&cid=' . $row['civicrm_contact_id'],
558 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
559 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts('View Contact Summary for this Contact.');
562 // handle contribution status id
563 if ($value = CRM_Utils_Array
::value('civicrm_contribution_contribution_status_id', $row)) {
564 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = $contributionStatus[$value];
567 // handle payment instrument id
568 if ($value = CRM_Utils_Array
::value('civicrm_financial_trxn_payment_instrument_id', $row)) {
569 $rows[$rowNum]['civicrm_financial_trxn_payment_instrument_id'] = $paymentInstruments[$value];
572 // handle financial type id
573 if ($value = CRM_Utils_Array
::value('civicrm_line_item_financial_type_id', $row)) {
574 $rows[$rowNum]['civicrm_line_item_financial_type_id'] = $contributionTypes[$value];
576 if ($value = CRM_Utils_Array
::value('civicrm_entity_financial_trxn_amount', $row)) {
577 $rows[$rowNum]['civicrm_entity_financial_trxn_amount'] = CRM_Utils_Money
::format($rows[$rowNum]['civicrm_entity_financial_trxn_amount'], $rows[$rowNum]['civicrm_financial_trxn_currency']);
581 if (array_key_exists('civicrm_contact_gender_id', $row)) {
582 if ($value = $row['civicrm_contact_gender_id']) {
583 $gender = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'gender_id');
584 $rows[$rowNum]['civicrm_contact_gender_id'] = $gender[$value];
589 // display birthday in the configured custom format
590 if (array_key_exists('civicrm_contact_birth_date', $row)) {
591 $birthDate = $row['civicrm_contact_birth_date'];
593 $rows[$rowNum]['civicrm_contact_birth_date'] = CRM_Utils_Date
::customFormat($birthDate, '%Y%m%d');