5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.3 |
7 +--------------------------------------------------------------------+
8 | Copyright CiviCRM LLC (c) 2004-2013 |
9 +--------------------------------------------------------------------+
10 | This file is a part of CiviCRM. |
12 | CiviCRM is free software; you can copy, modify, and distribute it |
13 | under the terms of the GNU Affero General Public License |
14 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
16 | CiviCRM is distributed in the hope that it will be useful, but |
17 | WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
19 | See the GNU Affero General Public License for more details. |
21 | You should have received a copy of the GNU Affero General Public |
22 | License and the CiviCRM Licensing Exception along |
23 | with this program; if not, contact CiviCRM LLC |
24 | at info[AT]civicrm[DOT]org. If you have questions about the |
25 | GNU Affero General Public License or the licensing of CiviCRM, |
26 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
27 +--------------------------------------------------------------------+
33 * @copyright CiviCRM LLC (c) 2004-2013
37 class CRM_Report_Form_Contribute_Bookkeeping
extends CRM_Report_Form
{
38 protected $_addressField = FALSE;
40 protected $_emailField = FALSE;
42 protected $_summary = NULL;
44 protected $_customGroupExtends = array(
47 function __construct() {
48 $this->_columns
= array(
51 'dao' => 'CRM_Contact_DAO_Contact',
55 array('title' => ts('Contact Name'),
68 array('title' => ts('Contact Name'),
72 array('title' => ts('Contact ID'),
76 'grouping' => 'contact-fields',
78 'civicrm_membership' =>
80 'dao' => 'CRM_Member_DAO_Membership',
84 array('title' => ts('Membership #'),
90 'civicrm_financial_account' => array(
91 'dao' => 'CRM_Financial_DAO_FinancialAccount',
93 'debit_accounting_code' => array(
94 'title' => ts('Financial Account Code - Debit'),
95 'name' => 'accounting_code',
96 'alias' => 'financial_account_civireport_debit',
99 'credit_accounting_code' => array(
100 'title' => ts('Financial Account Code - Credit'),
101 'name' => 'accounting_code',
102 'alias' => 'financial_account_civireport_credit',
105 'debit_name' => array(
106 'title' => ts('Financial Account Name - Debit'),
108 'alias' => 'financial_account_civireport_debit',
111 'credit_name' => array(
112 'title' => ts('Financial Account Name - Credit'),
114 'alias' => 'financial_account_civireport_credit',
119 'debit_accounting_code' => array(
120 'title' => ts('Financial Account Code - Debit'),
121 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
122 'options' => CRM_Contribute_PseudoConstant
::financialAccount(NULL, NULL, 'accounting_code', 'accounting_code'),
123 'name' => 'accounting_code',
124 'alias' => 'financial_account_civireport_debit',
126 'credit_accounting_code' => array(
127 'title' => ts('Financial Account Code - Credit'),
128 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
129 'options' => CRM_Contribute_PseudoConstant
::financialAccount(NULL, NULL, 'accounting_code', 'accounting_code'),
131 'debit_name' => array(
132 'title' => ts('Financial Account Name - Debit'),
133 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
134 'options' => CRM_Contribute_PseudoConstant
::financialAccount(),
136 'alias' => 'financial_account_civireport_debit',
138 'credit_name' => array(
139 'title' => ts('Financial Account Name - Credit'),
140 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
141 'options' => CRM_Contribute_PseudoConstant
::financialAccount(),
145 'civicrm_line_item' => array(
146 'dao' => 'CRM_Price_DAO_LineItem',
148 'financial_type_id' => array('title' => ts('Financial Type'),
153 'financial_type_id' => array(
154 'title' => ts('Financial Type'),
155 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
156 'options' => CRM_Contribute_PseudoConstant
::financialType(),
160 'civicrm_contribution' =>
162 'dao' => 'CRM_Contribute_DAO_Contribution',
165 'receive_date' => array(
168 'invoice_id' => array(
169 'title' => ts('Invoice ID'),
172 'contribution_status_id' => array('title' => ts('Contribution Status'),
175 'id' => array('title' => ts('Contribution #'),
182 array('operatorType' => CRM_Report_Form
::OP_DATE
),
183 'contribution_status_id' =>
184 array('title' => ts('Contribution Status'),
185 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
186 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
187 'default' => array(1),
190 'grouping' => 'contri-fields',
192 'civicrm_financial_trxn' => array(
193 'dao' => 'CRM_Financial_DAO_FinancialTrxn',
195 'check_number' => array(
196 'title' => ts('Cheque #'),
199 'payment_instrument_id' => array('title' => ts('Payment Instrument'),
202 'trxn_date' => array(
203 'title' => ts('Transaction Date'),
205 'type' => CRM_Utils_Type
::T_DATE
,
208 'title' => ts('Trans #'),
214 'payment_instrument_id' => array(
215 'title' => ts('Payment Instrument'),
216 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
217 'options' => CRM_Contribute_PseudoConstant
::paymentInstrument(),
219 'trxn_date' => array(
220 'title' => ts('Transaction Date'),
221 'operatorType' => CRM_Report_Form
::OP_DATE
,
222 'type' => CRM_Utils_Type
::T_DATE
,
226 'civicrm_entity_financial_trxn' => array(
227 'dao' => 'CRM_Financial_DAO_EntityFinancialTrxn',
230 'title' => ts('Amount'),
237 array('title' => ts('Amount')),
241 parent
::__construct();
244 function preProcess() {
245 parent
::preProcess();
251 $this->_columnHeaders
= array();
252 foreach ($this->_columns
as $tableName => $table) {
253 if (array_key_exists('fields', $table)) {
254 foreach ($table['fields'] as $fieldName => $field) {
255 if (CRM_Utils_Array
::value('required', $field) ||
256 CRM_Utils_Array
::value($fieldName, $this->_params
['fields'])
258 switch ($fieldName) {
259 case 'credit_accounting_code' :
261 WHEN {$this->_aliases['civicrm_financial_trxn']}.from_financial_account_id IS NOT NULL
262 THEN {$this->_aliases['civicrm_financial_account']}_credit_1.accounting_code
263 ELSE {$this->_aliases['civicrm_financial_account']}_credit_2.accounting_code
264 END AS civicrm_financial_account_credit_accounting_code ";
268 WHEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_id IS NOT NULL
269 THEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.amount
270 ELSE {$this->_aliases['civicrm_entity_financial_trxn']}.amount
271 END AS civicrm_entity_financial_trxn_amount ";
275 WHEN {$this->_aliases['civicrm_financial_trxn']}.from_financial_account_id IS NOT NULL
276 THEN {$this->_aliases['civicrm_financial_account']}_credit_1.name
277 ELSE {$this->_aliases['civicrm_financial_account']}_credit_2.name
278 END AS civicrm_financial_account_credit_name ";
281 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
284 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
285 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
291 $this->_select
= "SELECT " . implode(', ', $select) . " ";
297 $this->_from
= "FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
298 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
299 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id AND
300 {$this->_aliases['civicrm_contribution']}.is_test = 0
301 LEFT JOIN civicrm_membership_payment payment
302 ON ( {$this->_aliases['civicrm_contribution']}.id = payment.contribution_id )
303 LEFT JOIN civicrm_membership {$this->_aliases['civicrm_membership']}
304 ON payment.membership_id = {$this->_aliases['civicrm_membership']}.id
305 LEFT JOIN civicrm_entity_financial_trxn {$this->_aliases['civicrm_entity_financial_trxn']}
306 ON ({$this->_aliases['civicrm_contribution']}.id = {$this->_aliases['civicrm_entity_financial_trxn']}.entity_id AND
307 {$this->_aliases['civicrm_entity_financial_trxn']}.entity_table = 'civicrm_contribution')
308 LEFT JOIN civicrm_financial_trxn {$this->_aliases['civicrm_financial_trxn']}
309 ON {$this->_aliases['civicrm_financial_trxn']}.id = {$this->_aliases['civicrm_entity_financial_trxn']}.financial_trxn_id
310 LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_debit
311 ON {$this->_aliases['civicrm_financial_trxn']}.to_financial_account_id = {$this->_aliases['civicrm_financial_account']}_debit.id
312 LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_credit_1
313 ON {$this->_aliases['civicrm_financial_trxn']}.from_financial_account_id = {$this->_aliases['civicrm_financial_account']}_credit_1.id
314 LEFT JOIN civicrm_entity_financial_trxn {$this->_aliases['civicrm_entity_financial_trxn']}_item
315 ON ({$this->_aliases['civicrm_financial_trxn']}.id = {$this->_aliases['civicrm_entity_financial_trxn']}_item.financial_trxn_id AND
316 {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_table = 'civicrm_financial_item')
317 LEFT JOIN civicrm_financial_item fitem
318 ON fitem.id = {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_id
319 LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_credit_2
320 ON fitem.financial_account_id = {$this->_aliases['civicrm_financial_account']}_credit_2.id
321 LEFT JOIN civicrm_line_item {$this->_aliases['civicrm_line_item']}
322 ON fitem.entity_id = {$this->_aliases['civicrm_line_item']}.id AND fitem.entity_table = 'civicrm_line_item' ";
326 $this->_orderBy
= " ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_contribution']}.id, {$this->_aliases['civicrm_entity_financial_trxn']}.id ";
330 foreach ($this->_columns
as $tableName => $table) {
331 if (array_key_exists('filters', $table)) {
332 foreach ($table['filters'] as $fieldName => $field) {
334 if ($fieldName == 'credit_accounting_code') {
335 $field['dbAlias'] = "CASE
336 WHEN financial_trxn_civireport.from_financial_account_id IS NOT NULL
337 THEN financial_account_civireport_credit_1.accounting_code
338 ELSE financial_account_civireport_credit_2.accounting_code
341 else if ($fieldName == 'credit_name') {
342 $field['dbAlias'] = "CASE
343 WHEN financial_trxn_civireport.from_financial_account_id IS NOT NULL
344 THEN financial_account_civireport_credit_1.id
345 ELSE financial_account_civireport_credit_2.id
348 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
349 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
350 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
351 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
353 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
356 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
358 $clause = $this->whereClause($field,
360 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
361 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
362 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
366 if (!empty($clause)) {
367 $clauses[] = $clause;
372 if (empty($clauses)) {
373 $this->_where
= "WHERE ( 1 )";
376 $this->_where
= "WHERE " . implode(' AND ', $clauses);
380 function postProcess() {
381 // get the acl clauses built before we assemble the query
382 $this->buildACLClause($this->_aliases
['civicrm_contact']);
383 parent
::postProcess();
386 function statistics(&$rows) {
387 $statistics = parent
::statistics($rows);
389 $select = " SELECT COUNT({$this->_aliases['civicrm_financial_trxn']}.id ) as count,
391 WHEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_id IS NOT NULL
392 THEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.amount
393 ELSE {$this->_aliases['civicrm_entity_financial_trxn']}.amount
396 $sql = "{$select} {$this->_from} {$this->_where}";
397 $dao = CRM_Core_DAO
::executeQuery($sql);
400 $statistics['counts']['amount'] = array(
401 'value' => $dao->amount
,
402 'title' => 'Total Amount',
403 'type' => CRM_Utils_Type
::T_MONEY
,
405 $statistics['counts']['avg'] = array(
406 'value' => round(($dao->amount
/ $dao->count
), 2),
407 'title' => 'Average',
408 'type' => CRM_Utils_Type
::T_MONEY
,
415 function alterDisplay(&$rows) {
416 $contributionTypes = CRM_Contribute_PseudoConstant
::financialType();
417 $paymentInstruments = CRM_Contribute_PseudoConstant
::paymentInstrument();
418 $contributionStatus = CRM_Contribute_PseudoConstant
::contributionStatus();
419 foreach ($rows as $rowNum => $row) {
420 // convert display name to links
421 if (array_key_exists('civicrm_contact_sort_name', $row) &&
422 CRM_Utils_Array
::value('civicrm_contact_sort_name', $rows[$rowNum]) &&
423 array_key_exists('civicrm_contact_id', $row)
425 $url = CRM_Utils_System
::url("civicrm/contact/view",
426 'reset=1&cid=' . $row['civicrm_contact_id'],
429 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
430 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact Summary for this Contact.");
433 // handle contribution status id
434 if ($value = CRM_Utils_Array
::value('civicrm_contribution_contribution_status_id', $row)) {
435 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = $contributionStatus[$value];
438 // handle payment instrument id
439 if ($value = CRM_Utils_Array
::value('civicrm_financial_trxn_payment_instrument_id', $row)) {
440 $rows[$rowNum]['civicrm_financial_trxn_payment_instrument_id'] = $paymentInstruments[$value];
443 // handle financial type id
444 if ($value = CRM_Utils_Array
::value('civicrm_line_item_financial_type_id', $row)) {
445 $rows[$rowNum]['civicrm_line_item_financial_type_id'] = $contributionTypes[$value];