4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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(
52 function __construct() {
53 $this->_columns
= array(
56 'dao' => 'CRM_Contact_DAO_Contact',
60 array('title' => ts('Contact Name'),
71 'title' => ts('Contact Type'),
75 'title' => ts('Contact SubType'),
81 array('title' => ts('Contact Name'),
85 array('title' => ts('Contact ID'),
92 'title' => ts('Last Name, First Name'),
95 'grouping' => 'contact-fields',
97 'civicrm_membership' =>
99 'dao' => 'CRM_Member_DAO_Membership',
103 array('title' => ts('Membership #'),
104 'no_display' => TRUE,
109 'civicrm_financial_account' => array(
110 'dao' => 'CRM_Financial_DAO_FinancialAccount',
112 'debit_accounting_code' => array(
113 'title' => ts('Financial Account Code - Debit'),
114 'name' => 'accounting_code',
115 'alias' => 'financial_account_civireport_debit',
118 'credit_accounting_code' => array(
119 'title' => ts('Financial Account Code - Credit'),
120 'name' => 'accounting_code',
121 'alias' => 'financial_account_civireport_credit',
124 'debit_name' => array(
125 'title' => ts('Financial Account Name - Debit'),
127 'alias' => 'financial_account_civireport_debit',
130 'credit_name' => array(
131 'title' => ts('Financial Account Name - Credit'),
133 'alias' => 'financial_account_civireport_credit',
138 'debit_accounting_code' => array(
139 'title' => ts('Financial Account Code - Debit'),
140 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
141 'options' => CRM_Contribute_PseudoConstant
::financialAccount(NULL, NULL, 'accounting_code', 'accounting_code'),
142 'name' => 'accounting_code',
143 'alias' => 'financial_account_civireport_debit',
145 'credit_accounting_code' => array(
146 'title' => ts('Financial Account Code - Credit'),
147 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
148 'options' => CRM_Contribute_PseudoConstant
::financialAccount(NULL, NULL, 'accounting_code', 'accounting_code'),
150 'debit_name' => array(
151 'title' => ts('Financial Account Name - Debit'),
152 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
153 'options' => CRM_Contribute_PseudoConstant
::financialAccount(),
155 'alias' => 'financial_account_civireport_debit',
157 'credit_name' => array(
158 'title' => ts('Financial Account Name - Credit'),
159 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
160 'options' => CRM_Contribute_PseudoConstant
::financialAccount(),
164 'civicrm_line_item' => array(
165 'dao' => 'CRM_Price_DAO_LineItem',
167 'financial_type_id' => array('title' => ts('Financial Type'),
172 'financial_type_id' => array(
173 'title' => ts('Financial Type'),
174 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
175 'options' => CRM_Contribute_PseudoConstant
::financialType(),
178 'order_bys' => array(
179 'financial_type_id' => array('title' => ts('Financial Type')),
182 'civicrm_contribution' =>
184 'dao' => 'CRM_Contribute_DAO_Contribution',
187 'receive_date' => array(
190 'invoice_id' => array(
191 'title' => ts('Invoice ID'),
194 'contribution_status_id' => array('title' => ts('Contribution Status'),
197 'id' => array('title' => ts('Contribution #'),
204 array('operatorType' => CRM_Report_Form
::OP_DATE
),
205 'contribution_status_id' =>
206 array('title' => ts('Contribution Status'),
207 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
208 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
209 'default' => array(1),
212 'order_bys' => array(
213 'contribution_id' => array('title' => ts('Contribution #')),
214 'contribution_status_id' => array('title' => ts('Contribution Status')),
216 'grouping' => 'contri-fields',
218 'civicrm_financial_trxn' => array(
219 'dao' => 'CRM_Financial_DAO_FinancialTrxn',
221 'check_number' => array(
222 'title' => ts('Cheque #'),
225 'payment_instrument_id' => array('title' => ts('Payment Instrument'),
230 'no_display' => TRUE,
232 'trxn_date' => array(
233 'title' => ts('Transaction Date'),
235 'type' => CRM_Utils_Type
::T_DATE
,
238 'title' => ts('Trans #'),
244 'payment_instrument_id' => array(
245 'title' => ts('Payment Instrument'),
246 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
247 'options' => CRM_Contribute_PseudoConstant
::paymentInstrument(),
250 'title' => 'Currency',
251 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
252 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
254 'type' => CRM_Utils_Type
::T_STRING
,
256 'trxn_date' => array(
257 'title' => ts('Transaction Date'),
258 'operatorType' => CRM_Report_Form
::OP_DATE
,
259 'type' => CRM_Utils_Type
::T_DATE
,
262 'order_bys' => array(
263 'payment_instrument_id' => array('title' => ts('Payment Instrument')),
266 'civicrm_entity_financial_trxn' => array(
267 'dao' => 'CRM_Financial_DAO_EntityFinancialTrxn',
270 'title' => ts('Amount'),
272 'type' => CRM_Utils_Type
::T_STRING
,
278 array('title' => ts('Amount')),
283 $this->_groupFilter
= TRUE;
284 $this->_tagFilter
= TRUE;
285 parent
::__construct();
288 function preProcess() {
289 parent
::preProcess();
295 $this->_columnHeaders
= array();
296 foreach ($this->_columns
as $tableName => $table) {
297 if (array_key_exists('fields', $table)) {
298 foreach ($table['fields'] as $fieldName => $field) {
299 if (!empty($field['required']) ||
!empty($this->_params
['fields'][$fieldName])) {
300 switch ($fieldName) {
301 case 'credit_accounting_code' :
303 WHEN {$this->_aliases['civicrm_financial_trxn']}.from_financial_account_id IS NOT NULL
304 THEN {$this->_aliases['civicrm_financial_account']}_credit_1.accounting_code
305 ELSE {$this->_aliases['civicrm_financial_account']}_credit_2.accounting_code
306 END AS civicrm_financial_account_credit_accounting_code ";
310 WHEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_id IS NOT NULL
311 THEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.amount
312 ELSE {$this->_aliases['civicrm_entity_financial_trxn']}.amount
313 END AS civicrm_entity_financial_trxn_amount ";
317 WHEN {$this->_aliases['civicrm_financial_trxn']}.from_financial_account_id IS NOT NULL
318 THEN {$this->_aliases['civicrm_financial_account']}_credit_1.name
319 ELSE {$this->_aliases['civicrm_financial_account']}_credit_2.name
320 END AS civicrm_financial_account_credit_name ";
323 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
326 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
327 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
333 $this->_select
= 'SELECT ' . implode(', ', $select) . ' ';
339 $this->_from
= "FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
340 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
341 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id AND
342 {$this->_aliases['civicrm_contribution']}.is_test = 0
343 LEFT JOIN civicrm_membership_payment payment
344 ON ( {$this->_aliases['civicrm_contribution']}.id = payment.contribution_id )
345 LEFT JOIN civicrm_membership {$this->_aliases['civicrm_membership']}
346 ON payment.membership_id = {$this->_aliases['civicrm_membership']}.id
347 LEFT JOIN civicrm_entity_financial_trxn {$this->_aliases['civicrm_entity_financial_trxn']}
348 ON ({$this->_aliases['civicrm_contribution']}.id = {$this->_aliases['civicrm_entity_financial_trxn']}.entity_id AND
349 {$this->_aliases['civicrm_entity_financial_trxn']}.entity_table = 'civicrm_contribution')
350 LEFT JOIN civicrm_financial_trxn {$this->_aliases['civicrm_financial_trxn']}
351 ON {$this->_aliases['civicrm_financial_trxn']}.id = {$this->_aliases['civicrm_entity_financial_trxn']}.financial_trxn_id
352 LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_debit
353 ON {$this->_aliases['civicrm_financial_trxn']}.to_financial_account_id = {$this->_aliases['civicrm_financial_account']}_debit.id
354 LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_credit_1
355 ON {$this->_aliases['civicrm_financial_trxn']}.from_financial_account_id = {$this->_aliases['civicrm_financial_account']}_credit_1.id
356 LEFT JOIN civicrm_entity_financial_trxn {$this->_aliases['civicrm_entity_financial_trxn']}_item
357 ON ({$this->_aliases['civicrm_financial_trxn']}.id = {$this->_aliases['civicrm_entity_financial_trxn']}_item.financial_trxn_id AND
358 {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_table = 'civicrm_financial_item')
359 LEFT JOIN civicrm_financial_item fitem
360 ON fitem.id = {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_id
361 LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_credit_2
362 ON fitem.financial_account_id = {$this->_aliases['civicrm_financial_account']}_credit_2.id
363 LEFT JOIN civicrm_line_item {$this->_aliases['civicrm_line_item']}
364 ON fitem.entity_id = {$this->_aliases['civicrm_line_item']}.id AND fitem.entity_table = 'civicrm_line_item' ";
370 // please note this will just add the order-by columns to select query, and not display in column-headers.
371 // This is a solution to not throw fatal errors when there is a column in order-by, not present in select/display columns.
372 foreach ($this->_orderByFields
as $orderBy) {
373 if (!array_key_exists($orderBy['name'], $this->_params
['fields']) && empty($orderBy['section'])) {
374 $this->_select
.= ", {$orderBy['dbAlias']} as {$orderBy['tplField']}";
380 foreach ($this->_columns
as $tableName => $table) {
381 if (array_key_exists('filters', $table)) {
382 foreach ($table['filters'] as $fieldName => $field) {
384 if ($fieldName == 'credit_accounting_code') {
385 $field['dbAlias'] = "CASE
386 WHEN financial_trxn_civireport.from_financial_account_id IS NOT NULL
387 THEN financial_account_civireport_credit_1.accounting_code
388 ELSE financial_account_civireport_credit_2.accounting_code
391 else if ($fieldName == 'credit_name') {
392 $field['dbAlias'] = "CASE
393 WHEN financial_trxn_civireport.from_financial_account_id IS NOT NULL
394 THEN financial_account_civireport_credit_1.id
395 ELSE financial_account_civireport_credit_2.id
398 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
399 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
400 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
401 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
403 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
406 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
408 $clause = $this->whereClause($field,
410 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
411 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
412 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
416 if (!empty($clause)) {
417 $clauses[] = $clause;
422 if (empty($clauses)) {
423 $this->_where
= 'WHERE ( 1 )';
426 $this->_where
= 'WHERE ' . implode(' AND ', $clauses);
430 function postProcess() {
431 // get the acl clauses built before we assemble the query
432 $this->buildACLClause($this->_aliases
['civicrm_contact']);
433 parent
::postProcess();
441 function statistics(&$rows) {
442 $statistics = parent
::statistics($rows);
444 $select = " SELECT COUNT({$this->_aliases['civicrm_financial_trxn']}.id ) as count,
445 {$this->_aliases['civicrm_contribution']}.currency,
447 WHEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_id IS NOT NULL
448 THEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.amount
449 ELSE {$this->_aliases['civicrm_entity_financial_trxn']}.amount
453 $sql = "{$select} {$this->_from} {$this->_where}
454 GROUP BY {$this->_aliases['civicrm_contribution']}.currency
457 $dao = CRM_Core_DAO
::executeQuery($sql);
458 while ($dao->fetch()) {
459 $amount[] = CRM_Utils_Money
::format($dao->amount
, $dao->currency
);
460 $avg[] = CRM_Utils_Money
::format(round(($dao->amount
/ $dao->count
), 2), $dao->currency
);
463 $statistics['counts']['amount'] = array(
464 'value' => implode(', ', $amount),
465 'title' => 'Total Amount',
466 'type' => CRM_Utils_Type
::T_STRING
,
468 $statistics['counts']['avg'] = array(
469 'value' => implode(', ', $avg),
470 'title' => 'Average',
471 'type' => CRM_Utils_Type
::T_STRING
,
479 function alterDisplay(&$rows) {
480 $contributionTypes = CRM_Contribute_PseudoConstant
::financialType();
481 $paymentInstruments = CRM_Contribute_PseudoConstant
::paymentInstrument();
482 $contributionStatus = CRM_Contribute_PseudoConstant
::contributionStatus();
483 foreach ($rows as $rowNum => $row) {
484 // convert display name to links
485 if (array_key_exists('civicrm_contact_sort_name', $row) && !empty($rows[$rowNum]['civicrm_contact_sort_name']) &&
486 array_key_exists('civicrm_contact_id', $row)
488 $url = CRM_Utils_System
::url('civicrm/contact/view',
489 'reset=1&cid=' . $row['civicrm_contact_id'],
492 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
493 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts('View Contact Summary for this Contact.');
496 // handle contribution status id
497 if ($value = CRM_Utils_Array
::value('civicrm_contribution_contribution_status_id', $row)) {
498 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = $contributionStatus[$value];
501 // handle payment instrument id
502 if ($value = CRM_Utils_Array
::value('civicrm_financial_trxn_payment_instrument_id', $row)) {
503 $rows[$rowNum]['civicrm_financial_trxn_payment_instrument_id'] = $paymentInstruments[$value];
506 // handle financial type id
507 if ($value = CRM_Utils_Array
::value('civicrm_line_item_financial_type_id', $row)) {
508 $rows[$rowNum]['civicrm_line_item_financial_type_id'] = $contributionTypes[$value];
510 if ($value = CRM_Utils_Array
::value('civicrm_entity_financial_trxn_amount', $row)) {
511 $rows[$rowNum]['civicrm_entity_financial_trxn_amount'] = CRM_Utils_Money
::format($rows[$rowNum]['civicrm_entity_financial_trxn_amount'],$rows[$rowNum]['civicrm_financial_trxn_currency']);