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(
53 function __construct() {
54 $this->_columns
= array(
55 'civicrm_contact' => array(
56 'dao' => 'CRM_Contact_DAO_Contact',
59 'title' => ts('Contact Name'),
67 'contact_type' => array(
68 'title' => ts('Contact Type'),
70 'contact_sub_type' => array(
71 'title' => ts('Contact Subtype'),
76 'title' => ts('Contact Name'),
80 'title' => ts('Contact ID'),
86 'title' => ts('Last Name, First Name'),
89 'grouping' => 'contact-fields',
91 'civicrm_membership' => array(
92 'dao' => 'CRM_Member_DAO_Membership',
95 'title' => ts('Membership #'),
101 'civicrm_financial_account' => array(
102 'dao' => 'CRM_Financial_DAO_FinancialAccount',
104 'debit_accounting_code' => array(
105 'title' => ts('Financial Account Code - Debit'),
106 'name' => 'accounting_code',
107 'alias' => 'financial_account_civireport_debit',
110 'credit_accounting_code' => array(
111 'title' => ts('Financial Account Code - Credit'),
112 'name' => 'accounting_code',
113 'alias' => 'financial_account_civireport_credit',
116 'debit_name' => array(
117 'title' => ts('Financial Account Name - Debit'),
119 'alias' => 'financial_account_civireport_debit',
122 'credit_name' => array(
123 'title' => ts('Financial Account Name - Credit'),
125 'alias' => 'financial_account_civireport_credit',
130 'debit_accounting_code' => array(
131 'title' => ts('Financial Account Code - Debit'),
132 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
133 'options' => CRM_Contribute_PseudoConstant
::financialAccount(NULL, NULL, 'accounting_code', 'accounting_code'),
134 'name' => 'accounting_code',
135 'alias' => 'financial_account_civireport_debit',
137 'credit_accounting_code' => array(
138 'title' => ts('Financial Account Code - Credit'),
139 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
140 'options' => CRM_Contribute_PseudoConstant
::financialAccount(NULL, NULL, 'accounting_code', 'accounting_code'),
142 'debit_name' => array(
143 'title' => ts('Financial Account Name - Debit'),
144 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
145 'options' => CRM_Contribute_PseudoConstant
::financialAccount(),
147 'alias' => 'financial_account_civireport_debit',
149 'credit_name' => array(
150 'title' => ts('Financial Account Name - Credit'),
151 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
152 'options' => CRM_Contribute_PseudoConstant
::financialAccount(),
156 'civicrm_line_item' => array(
157 'dao' => 'CRM_Price_DAO_LineItem',
159 'financial_type_id' => array(
160 'title' => ts('Financial Type'),
165 'financial_type_id' => array(
166 'title' => ts('Financial Type'),
167 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
168 'options' => CRM_Contribute_PseudoConstant
::financialType(),
171 'order_bys' => array(
172 'financial_type_id' => array('title' => ts('Financial Type')),
175 'civicrm_contribution' => array(
176 'dao' => 'CRM_Contribute_DAO_Contribution',
178 'receive_date' => array(
181 'invoice_id' => array(
182 'title' => ts('Invoice ID'),
185 'contribution_status_id' => array(
186 'title' => ts('Contribution Status'),
190 'title' => ts('Contribution #'),
195 'receive_date' => array('operatorType' => CRM_Report_Form
::OP_DATE
),
196 'contribution_status_id' => array(
197 'title' => ts('Contribution Status'),
198 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
199 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
200 'default' => array(1),
203 'order_bys' => array(
204 'contribution_id' => array('title' => ts('Contribution #')),
205 'contribution_status_id' => array('title' => ts('Contribution Status')),
207 'grouping' => 'contri-fields',
209 'civicrm_financial_trxn' => array(
210 'dao' => 'CRM_Financial_DAO_FinancialTrxn',
212 'check_number' => array(
213 'title' => ts('Cheque #'),
216 'payment_instrument_id' => array(
217 'title' => ts('Payment Instrument'),
222 'no_display' => TRUE,
224 'trxn_date' => array(
225 'title' => ts('Transaction Date'),
227 'type' => CRM_Utils_Type
::T_DATE
,
230 'title' => ts('Trans #'),
235 'payment_instrument_id' => array(
236 'title' => ts('Payment Instrument'),
237 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
238 'options' => CRM_Contribute_PseudoConstant
::paymentInstrument(),
241 'title' => 'Currency',
242 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
243 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
245 'type' => CRM_Utils_Type
::T_STRING
,
247 'trxn_date' => array(
248 'title' => ts('Transaction Date'),
249 'operatorType' => CRM_Report_Form
::OP_DATE
,
250 'type' => CRM_Utils_Type
::T_DATE
,
253 'order_bys' => array(
254 'payment_instrument_id' => array('title' => ts('Payment Instrument')),
257 'civicrm_entity_financial_trxn' => array(
258 'dao' => 'CRM_Financial_DAO_EntityFinancialTrxn',
261 'title' => ts('Amount'),
263 'type' => CRM_Utils_Type
::T_STRING
,
267 'amount' => array('title' => ts('Amount')),
272 $this->_groupFilter
= TRUE;
273 $this->_tagFilter
= TRUE;
274 parent
::__construct();
277 function preProcess() {
278 parent
::preProcess();
284 $this->_columnHeaders
= array();
285 foreach ($this->_columns
as $tableName => $table) {
286 if (array_key_exists('fields', $table)) {
287 foreach ($table['fields'] as $fieldName => $field) {
288 if (!empty($field['required']) ||
289 !empty($this->_params
['fields'][$fieldName])
291 switch ($fieldName) {
292 case 'credit_accounting_code' :
294 WHEN {$this->_aliases['civicrm_financial_trxn']}.from_financial_account_id IS NOT NULL
295 THEN {$this->_aliases['civicrm_financial_account']}_credit_1.accounting_code
296 ELSE {$this->_aliases['civicrm_financial_account']}_credit_2.accounting_code
297 END AS civicrm_financial_account_credit_accounting_code ";
301 WHEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_id IS NOT NULL
302 THEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.amount
303 ELSE {$this->_aliases['civicrm_entity_financial_trxn']}.amount
304 END AS civicrm_entity_financial_trxn_amount ";
308 WHEN {$this->_aliases['civicrm_financial_trxn']}.from_financial_account_id IS NOT NULL
309 THEN {$this->_aliases['civicrm_financial_account']}_credit_1.name
310 ELSE {$this->_aliases['civicrm_financial_account']}_credit_2.name
311 END AS civicrm_financial_account_credit_name ";
314 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
317 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
318 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
324 $this->_select
= 'SELECT ' . implode(', ', $select) . ' ';
330 $this->_from
= "FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
331 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
332 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id AND
333 {$this->_aliases['civicrm_contribution']}.is_test = 0
334 LEFT JOIN civicrm_membership_payment payment
335 ON ( {$this->_aliases['civicrm_contribution']}.id = payment.contribution_id )
336 LEFT JOIN civicrm_membership {$this->_aliases['civicrm_membership']}
337 ON payment.membership_id = {$this->_aliases['civicrm_membership']}.id
338 LEFT JOIN civicrm_entity_financial_trxn {$this->_aliases['civicrm_entity_financial_trxn']}
339 ON ({$this->_aliases['civicrm_contribution']}.id = {$this->_aliases['civicrm_entity_financial_trxn']}.entity_id AND
340 {$this->_aliases['civicrm_entity_financial_trxn']}.entity_table = 'civicrm_contribution')
341 LEFT JOIN civicrm_financial_trxn {$this->_aliases['civicrm_financial_trxn']}
342 ON {$this->_aliases['civicrm_financial_trxn']}.id = {$this->_aliases['civicrm_entity_financial_trxn']}.financial_trxn_id
343 LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_debit
344 ON {$this->_aliases['civicrm_financial_trxn']}.to_financial_account_id = {$this->_aliases['civicrm_financial_account']}_debit.id
345 LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_credit_1
346 ON {$this->_aliases['civicrm_financial_trxn']}.from_financial_account_id = {$this->_aliases['civicrm_financial_account']}_credit_1.id
347 LEFT JOIN civicrm_entity_financial_trxn {$this->_aliases['civicrm_entity_financial_trxn']}_item
348 ON ({$this->_aliases['civicrm_financial_trxn']}.id = {$this->_aliases['civicrm_entity_financial_trxn']}_item.financial_trxn_id AND
349 {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_table = 'civicrm_financial_item')
350 LEFT JOIN civicrm_financial_item fitem
351 ON fitem.id = {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_id
352 LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_credit_2
353 ON fitem.financial_account_id = {$this->_aliases['civicrm_financial_account']}_credit_2.id
354 LEFT JOIN civicrm_line_item {$this->_aliases['civicrm_line_item']}
355 ON fitem.entity_id = {$this->_aliases['civicrm_line_item']}.id AND fitem.entity_table = 'civicrm_line_item' ";
361 // please note this will just add the order-by columns to select query, and not display in column-headers.
362 // This is a solution to not throw fatal errors when there is a column in order-by, not present in select/display columns.
363 foreach ($this->_orderByFields
as $orderBy) {
364 if (!array_key_exists($orderBy['name'], $this->_params
['fields']) &&
365 empty($orderBy['section'])
367 $this->_select
.= ", {$orderBy['dbAlias']} as {$orderBy['tplField']}";
373 foreach ($this->_columns
as $tableName => $table) {
374 if (array_key_exists('filters', $table)) {
375 foreach ($table['filters'] as $fieldName => $field) {
377 if ($fieldName == 'credit_accounting_code') {
378 $field['dbAlias'] = "CASE
379 WHEN financial_trxn_civireport.from_financial_account_id IS NOT NULL
380 THEN financial_account_civireport_credit_1.accounting_code
381 ELSE financial_account_civireport_credit_2.accounting_code
384 else if ($fieldName == 'credit_name') {
385 $field['dbAlias'] = "CASE
386 WHEN financial_trxn_civireport.from_financial_account_id IS NOT NULL
387 THEN financial_account_civireport_credit_1.id
388 ELSE financial_account_civireport_credit_2.id
391 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
392 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
393 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
394 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
396 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
399 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
401 $clause = $this->whereClause($field,
403 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
404 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
405 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
409 if (!empty($clause)) {
410 $clauses[] = $clause;
415 if (empty($clauses)) {
416 $this->_where
= 'WHERE ( 1 )';
419 $this->_where
= 'WHERE ' . implode(' AND ', $clauses);
423 function postProcess() {
424 // get the acl clauses built before we assemble the query
425 $this->buildACLClause($this->_aliases
['civicrm_contact']);
426 parent
::postProcess();
434 function statistics(&$rows) {
435 $statistics = parent
::statistics($rows);
437 $select = " SELECT COUNT({$this->_aliases['civicrm_financial_trxn']}.id ) as count,
438 {$this->_aliases['civicrm_contribution']}.currency,
440 WHEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_id IS NOT NULL
441 THEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.amount
442 ELSE {$this->_aliases['civicrm_entity_financial_trxn']}.amount
446 $sql = "{$select} {$this->_from} {$this->_where}
447 GROUP BY {$this->_aliases['civicrm_contribution']}.currency
450 $dao = CRM_Core_DAO
::executeQuery($sql);
451 while ($dao->fetch()) {
452 $amount[] = CRM_Utils_Money
::format($dao->amount
, $dao->currency
);
453 $avg[] = CRM_Utils_Money
::format(round(($dao->amount
/
454 $dao->count
), 2), $dao->currency
);
457 $statistics['counts']['amount'] = array(
458 'value' => implode(', ', $amount),
459 'title' => 'Total Amount',
460 'type' => CRM_Utils_Type
::T_STRING
,
462 $statistics['counts']['avg'] = array(
463 'value' => implode(', ', $avg),
464 'title' => 'Average',
465 'type' => CRM_Utils_Type
::T_STRING
,
473 function alterDisplay(&$rows) {
474 $contributionTypes = CRM_Contribute_PseudoConstant
::financialType();
475 $paymentInstruments = CRM_Contribute_PseudoConstant
::paymentInstrument();
476 $contributionStatus = CRM_Contribute_PseudoConstant
::contributionStatus();
477 foreach ($rows as $rowNum => $row) {
478 // convert display name to links
479 if (array_key_exists('civicrm_contact_sort_name', $row) &&
480 !empty($rows[$rowNum]['civicrm_contact_sort_name']) &&
481 array_key_exists('civicrm_contact_id', $row)
483 $url = CRM_Utils_System
::url('civicrm/contact/view',
484 'reset=1&cid=' . $row['civicrm_contact_id'],
487 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
488 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts('View Contact Summary for this Contact.');
491 // handle contribution status id
492 if ($value = CRM_Utils_Array
::value('civicrm_contribution_contribution_status_id', $row)) {
493 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = $contributionStatus[$value];
496 // handle payment instrument id
497 if ($value = CRM_Utils_Array
::value('civicrm_financial_trxn_payment_instrument_id', $row)) {
498 $rows[$rowNum]['civicrm_financial_trxn_payment_instrument_id'] = $paymentInstruments[$value];
501 // handle financial type id
502 if ($value = CRM_Utils_Array
::value('civicrm_line_item_financial_type_id', $row)) {
503 $rows[$rowNum]['civicrm_line_item_financial_type_id'] = $contributionTypes[$value];
505 if ($value = CRM_Utils_Array
::value('civicrm_entity_financial_trxn_amount', $row)) {
506 $rows[$rowNum]['civicrm_entity_financial_trxn_amount'] = CRM_Utils_Money
::format($rows[$rowNum]['civicrm_entity_financial_trxn_amount'], $rows[$rowNum]['civicrm_financial_trxn_currency']);