4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.4 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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(
46 function __construct() {
47 $this->_columns
= array(
50 'dao' => 'CRM_Contact_DAO_Contact',
54 array('title' => ts('Contact Name'),
65 'title' => ts('Contact Type'),
69 'title' => ts('Contact SubType'),
75 array('title' => ts('Contact Name'),
79 array('title' => ts('Contact ID'),
83 'grouping' => 'contact-fields',
85 'civicrm_membership' =>
87 'dao' => 'CRM_Member_DAO_Membership',
91 array('title' => ts('Membership #'),
97 'civicrm_financial_account' => array(
98 'dao' => 'CRM_Financial_DAO_FinancialAccount',
100 'debit_accounting_code' => array(
101 'title' => ts('Financial Account Code - Debit'),
102 'name' => 'accounting_code',
103 'alias' => 'financial_account_civireport_debit',
106 'credit_accounting_code' => array(
107 'title' => ts('Financial Account Code - Credit'),
108 'name' => 'accounting_code',
109 'alias' => 'financial_account_civireport_credit',
112 'debit_name' => array(
113 'title' => ts('Financial Account Name - Debit'),
115 'alias' => 'financial_account_civireport_debit',
118 'credit_name' => array(
119 'title' => ts('Financial Account Name - Credit'),
121 'alias' => 'financial_account_civireport_credit',
126 'debit_accounting_code' => array(
127 'title' => ts('Financial Account Code - Debit'),
128 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
129 'options' => CRM_Contribute_PseudoConstant
::financialAccount(NULL, NULL, 'accounting_code', 'accounting_code'),
130 'name' => 'accounting_code',
131 'alias' => 'financial_account_civireport_debit',
133 'credit_accounting_code' => array(
134 'title' => ts('Financial Account Code - Credit'),
135 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
136 'options' => CRM_Contribute_PseudoConstant
::financialAccount(NULL, NULL, 'accounting_code', 'accounting_code'),
138 'debit_name' => array(
139 'title' => ts('Financial Account Name - Debit'),
140 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
141 'options' => CRM_Contribute_PseudoConstant
::financialAccount(),
143 'alias' => 'financial_account_civireport_debit',
145 'credit_name' => array(
146 'title' => ts('Financial Account Name - Credit'),
147 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
148 'options' => CRM_Contribute_PseudoConstant
::financialAccount(),
152 'civicrm_line_item' => array(
153 'dao' => 'CRM_Price_DAO_LineItem',
155 'financial_type_id' => array('title' => ts('Financial Type'),
160 'financial_type_id' => array(
161 'title' => ts('Financial Type'),
162 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
163 'options' => CRM_Contribute_PseudoConstant
::financialType(),
167 'civicrm_contribution' =>
169 'dao' => 'CRM_Contribute_DAO_Contribution',
172 'receive_date' => array(
175 'invoice_id' => array(
176 'title' => ts('Invoice ID'),
179 'contribution_status_id' => array('title' => ts('Contribution Status'),
182 'id' => array('title' => ts('Contribution #'),
189 array('operatorType' => CRM_Report_Form
::OP_DATE
),
190 'contribution_status_id' =>
191 array('title' => ts('Contribution Status'),
192 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
193 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
194 'default' => array(1),
197 'grouping' => 'contri-fields',
199 'civicrm_financial_trxn' => array(
200 'dao' => 'CRM_Financial_DAO_FinancialTrxn',
202 'check_number' => array(
203 'title' => ts('Cheque #'),
206 'payment_instrument_id' => array('title' => ts('Payment Instrument'),
211 'no_display' => TRUE,
213 'trxn_date' => array(
214 'title' => ts('Transaction Date'),
216 'type' => CRM_Utils_Type
::T_DATE
,
219 'title' => ts('Trans #'),
225 'payment_instrument_id' => array(
226 'title' => ts('Payment Instrument'),
227 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
228 'options' => CRM_Contribute_PseudoConstant
::paymentInstrument(),
231 'title' => 'Currency',
232 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
233 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
235 'type' => CRM_Utils_Type
::T_STRING
,
237 'trxn_date' => array(
238 'title' => ts('Transaction Date'),
239 'operatorType' => CRM_Report_Form
::OP_DATE
,
240 'type' => CRM_Utils_Type
::T_DATE
,
244 'civicrm_entity_financial_trxn' => array(
245 'dao' => 'CRM_Financial_DAO_EntityFinancialTrxn',
248 'title' => ts('Amount'),
250 'type' => CRM_Utils_Type
::T_STRING
,
256 array('title' => ts('Amount')),
260 parent
::__construct();
263 function preProcess() {
264 parent
::preProcess();
270 $this->_columnHeaders
= array();
271 foreach ($this->_columns
as $tableName => $table) {
272 if (array_key_exists('fields', $table)) {
273 foreach ($table['fields'] as $fieldName => $field) {
274 if (CRM_Utils_Array
::value('required', $field) ||
275 CRM_Utils_Array
::value($fieldName, $this->_params
['fields'])
277 switch ($fieldName) {
278 case 'credit_accounting_code' :
280 WHEN {$this->_aliases['civicrm_financial_trxn']}.from_financial_account_id IS NOT NULL
281 THEN {$this->_aliases['civicrm_financial_account']}_credit_1.accounting_code
282 ELSE {$this->_aliases['civicrm_financial_account']}_credit_2.accounting_code
283 END AS civicrm_financial_account_credit_accounting_code ";
287 WHEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_id IS NOT NULL
288 THEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.amount
289 ELSE {$this->_aliases['civicrm_entity_financial_trxn']}.amount
290 END AS civicrm_entity_financial_trxn_amount ";
294 WHEN {$this->_aliases['civicrm_financial_trxn']}.from_financial_account_id IS NOT NULL
295 THEN {$this->_aliases['civicrm_financial_account']}_credit_1.name
296 ELSE {$this->_aliases['civicrm_financial_account']}_credit_2.name
297 END AS civicrm_financial_account_credit_name ";
300 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
303 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
304 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
310 $this->_select
= 'SELECT ' . implode(', ', $select) . ' ';
316 $this->_from
= "FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
317 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
318 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id AND
319 {$this->_aliases['civicrm_contribution']}.is_test = 0
320 LEFT JOIN civicrm_membership_payment payment
321 ON ( {$this->_aliases['civicrm_contribution']}.id = payment.contribution_id )
322 LEFT JOIN civicrm_membership {$this->_aliases['civicrm_membership']}
323 ON payment.membership_id = {$this->_aliases['civicrm_membership']}.id
324 LEFT JOIN civicrm_entity_financial_trxn {$this->_aliases['civicrm_entity_financial_trxn']}
325 ON ({$this->_aliases['civicrm_contribution']}.id = {$this->_aliases['civicrm_entity_financial_trxn']}.entity_id AND
326 {$this->_aliases['civicrm_entity_financial_trxn']}.entity_table = 'civicrm_contribution')
327 LEFT JOIN civicrm_financial_trxn {$this->_aliases['civicrm_financial_trxn']}
328 ON {$this->_aliases['civicrm_financial_trxn']}.id = {$this->_aliases['civicrm_entity_financial_trxn']}.financial_trxn_id
329 LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_debit
330 ON {$this->_aliases['civicrm_financial_trxn']}.to_financial_account_id = {$this->_aliases['civicrm_financial_account']}_debit.id
331 LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_credit_1
332 ON {$this->_aliases['civicrm_financial_trxn']}.from_financial_account_id = {$this->_aliases['civicrm_financial_account']}_credit_1.id
333 LEFT JOIN civicrm_entity_financial_trxn {$this->_aliases['civicrm_entity_financial_trxn']}_item
334 ON ({$this->_aliases['civicrm_financial_trxn']}.id = {$this->_aliases['civicrm_entity_financial_trxn']}_item.financial_trxn_id AND
335 {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_table = 'civicrm_financial_item')
336 LEFT JOIN civicrm_financial_item fitem
337 ON fitem.id = {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_id
338 LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_credit_2
339 ON fitem.financial_account_id = {$this->_aliases['civicrm_financial_account']}_credit_2.id
340 LEFT JOIN civicrm_line_item {$this->_aliases['civicrm_line_item']}
341 ON fitem.entity_id = {$this->_aliases['civicrm_line_item']}.id AND fitem.entity_table = 'civicrm_line_item' ";
345 $this->_orderBy
= " ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_contribution']}.id, {$this->_aliases['civicrm_entity_financial_trxn']}.id ";
349 foreach ($this->_columns
as $tableName => $table) {
350 if (array_key_exists('filters', $table)) {
351 foreach ($table['filters'] as $fieldName => $field) {
353 if ($fieldName == 'credit_accounting_code') {
354 $field['dbAlias'] = "CASE
355 WHEN financial_trxn_civireport.from_financial_account_id IS NOT NULL
356 THEN financial_account_civireport_credit_1.accounting_code
357 ELSE financial_account_civireport_credit_2.accounting_code
360 else if ($fieldName == 'credit_name') {
361 $field['dbAlias'] = "CASE
362 WHEN financial_trxn_civireport.from_financial_account_id IS NOT NULL
363 THEN financial_account_civireport_credit_1.id
364 ELSE financial_account_civireport_credit_2.id
367 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
368 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
369 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
370 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
372 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
375 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
377 $clause = $this->whereClause($field,
379 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
380 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
381 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
385 if (!empty($clause)) {
386 $clauses[] = $clause;
391 if (empty($clauses)) {
392 $this->_where
= 'WHERE ( 1 )';
395 $this->_where
= 'WHERE ' . implode(' AND ', $clauses);
399 function postProcess() {
400 // get the acl clauses built before we assemble the query
401 $this->buildACLClause($this->_aliases
['civicrm_contact']);
402 parent
::postProcess();
405 function statistics(&$rows) {
406 $statistics = parent
::statistics($rows);
408 $select = " SELECT COUNT({$this->_aliases['civicrm_financial_trxn']}.id ) as count,
409 {$this->_aliases['civicrm_contribution']}.currency,
411 WHEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_id IS NOT NULL
412 THEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.amount
413 ELSE {$this->_aliases['civicrm_entity_financial_trxn']}.amount
417 $sql = "{$select} {$this->_from} {$this->_where}
418 GROUP BY {$this->_aliases['civicrm_contribution']}.currency
421 $dao = CRM_Core_DAO
::executeQuery($sql);
422 while ($dao->fetch()) {
423 $amount[] = CRM_Utils_Money
::format($dao->amount
, $dao->currency
);
424 $avg[] = CRM_Utils_Money
::format(round(($dao->amount
/ $dao->count
), 2), $dao->currency
);
427 $statistics['counts']['amount'] = array(
428 'value' => implode(', ', $amount),
429 'title' => 'Total Amount',
430 'type' => CRM_Utils_Type
::T_STRING
,
432 $statistics['counts']['avg'] = array(
433 'value' => implode(', ', $avg),
434 'title' => 'Average',
435 'type' => CRM_Utils_Type
::T_STRING
,
440 function alterDisplay(&$rows) {
441 $contributionTypes = CRM_Contribute_PseudoConstant
::financialType();
442 $paymentInstruments = CRM_Contribute_PseudoConstant
::paymentInstrument();
443 $contributionStatus = CRM_Contribute_PseudoConstant
::contributionStatus();
444 foreach ($rows as $rowNum => $row) {
445 // convert display name to links
446 if (array_key_exists('civicrm_contact_sort_name', $row) &&
447 CRM_Utils_Array
::value('civicrm_contact_sort_name', $rows[$rowNum]) &&
448 array_key_exists('civicrm_contact_id', $row)
450 $url = CRM_Utils_System
::url('civicrm/contact/view',
451 'reset=1&cid=' . $row['civicrm_contact_id'],
454 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
455 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts('View Contact Summary for this Contact.');
458 // handle contribution status id
459 if ($value = CRM_Utils_Array
::value('civicrm_contribution_contribution_status_id', $row)) {
460 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = $contributionStatus[$value];
463 // handle payment instrument id
464 if ($value = CRM_Utils_Array
::value('civicrm_financial_trxn_payment_instrument_id', $row)) {
465 $rows[$rowNum]['civicrm_financial_trxn_payment_instrument_id'] = $paymentInstruments[$value];
468 // handle financial type id
469 if ($value = CRM_Utils_Array
::value('civicrm_line_item_financial_type_id', $row)) {
470 $rows[$rowNum]['civicrm_line_item_financial_type_id'] = $contributionTypes[$value];
472 if ($value = CRM_Utils_Array
::value('civicrm_entity_financial_trxn_amount', $row)) {
473 $rows[$rowNum]['civicrm_entity_financial_trxn_amount'] = CRM_Utils_Money
::format($rows[$rowNum]['civicrm_entity_financial_trxn_amount'],$rows[$rowNum]['civicrm_financial_trxn_currency']);