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'),
204 'no_display' => TRUE,
206 'trxn_date' => array(
207 'title' => ts('Transaction Date'),
209 'type' => CRM_Utils_Type
::T_DATE
,
212 'title' => ts('Trans #'),
218 'payment_instrument_id' => array(
219 'title' => ts('Payment Instrument'),
220 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
221 'options' => CRM_Contribute_PseudoConstant
::paymentInstrument(),
224 'title' => 'Currency',
225 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
226 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
228 'type' => CRM_Utils_Type
::T_STRING
,
230 'trxn_date' => array(
231 'title' => ts('Transaction Date'),
232 'operatorType' => CRM_Report_Form
::OP_DATE
,
233 'type' => CRM_Utils_Type
::T_DATE
,
237 'civicrm_entity_financial_trxn' => array(
238 'dao' => 'CRM_Financial_DAO_EntityFinancialTrxn',
241 'title' => ts('Amount'),
243 'type' => CRM_Utils_Type
::T_STRING
,
249 array('title' => ts('Amount')),
253 parent
::__construct();
256 function preProcess() {
257 parent
::preProcess();
263 $this->_columnHeaders
= array();
264 foreach ($this->_columns
as $tableName => $table) {
265 if (array_key_exists('fields', $table)) {
266 foreach ($table['fields'] as $fieldName => $field) {
267 if (CRM_Utils_Array
::value('required', $field) ||
268 CRM_Utils_Array
::value($fieldName, $this->_params
['fields'])
270 switch ($fieldName) {
271 case 'credit_accounting_code' :
273 WHEN {$this->_aliases['civicrm_financial_trxn']}.from_financial_account_id IS NOT NULL
274 THEN {$this->_aliases['civicrm_financial_account']}_credit_1.accounting_code
275 ELSE {$this->_aliases['civicrm_financial_account']}_credit_2.accounting_code
276 END AS civicrm_financial_account_credit_accounting_code ";
280 WHEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_id IS NOT NULL
281 THEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.amount
282 ELSE {$this->_aliases['civicrm_entity_financial_trxn']}.amount
283 END AS civicrm_entity_financial_trxn_amount ";
287 WHEN {$this->_aliases['civicrm_financial_trxn']}.from_financial_account_id IS NOT NULL
288 THEN {$this->_aliases['civicrm_financial_account']}_credit_1.name
289 ELSE {$this->_aliases['civicrm_financial_account']}_credit_2.name
290 END AS civicrm_financial_account_credit_name ";
293 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
296 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
297 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
303 $this->_select
= 'SELECT ' . implode(', ', $select) . ' ';
309 $this->_from
= "FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
310 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
311 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id AND
312 {$this->_aliases['civicrm_contribution']}.is_test = 0
313 LEFT JOIN civicrm_membership_payment payment
314 ON ( {$this->_aliases['civicrm_contribution']}.id = payment.contribution_id )
315 LEFT JOIN civicrm_membership {$this->_aliases['civicrm_membership']}
316 ON payment.membership_id = {$this->_aliases['civicrm_membership']}.id
317 LEFT JOIN civicrm_entity_financial_trxn {$this->_aliases['civicrm_entity_financial_trxn']}
318 ON ({$this->_aliases['civicrm_contribution']}.id = {$this->_aliases['civicrm_entity_financial_trxn']}.entity_id AND
319 {$this->_aliases['civicrm_entity_financial_trxn']}.entity_table = 'civicrm_contribution')
320 LEFT JOIN civicrm_financial_trxn {$this->_aliases['civicrm_financial_trxn']}
321 ON {$this->_aliases['civicrm_financial_trxn']}.id = {$this->_aliases['civicrm_entity_financial_trxn']}.financial_trxn_id
322 LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_debit
323 ON {$this->_aliases['civicrm_financial_trxn']}.to_financial_account_id = {$this->_aliases['civicrm_financial_account']}_debit.id
324 LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_credit_1
325 ON {$this->_aliases['civicrm_financial_trxn']}.from_financial_account_id = {$this->_aliases['civicrm_financial_account']}_credit_1.id
326 LEFT JOIN civicrm_entity_financial_trxn {$this->_aliases['civicrm_entity_financial_trxn']}_item
327 ON ({$this->_aliases['civicrm_financial_trxn']}.id = {$this->_aliases['civicrm_entity_financial_trxn']}_item.financial_trxn_id AND
328 {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_table = 'civicrm_financial_item')
329 LEFT JOIN civicrm_financial_item fitem
330 ON fitem.id = {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_id
331 LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_credit_2
332 ON fitem.financial_account_id = {$this->_aliases['civicrm_financial_account']}_credit_2.id
333 LEFT JOIN civicrm_line_item {$this->_aliases['civicrm_line_item']}
334 ON fitem.entity_id = {$this->_aliases['civicrm_line_item']}.id AND fitem.entity_table = 'civicrm_line_item' ";
338 $this->_orderBy
= " ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_contribution']}.id, {$this->_aliases['civicrm_entity_financial_trxn']}.id ";
342 foreach ($this->_columns
as $tableName => $table) {
343 if (array_key_exists('filters', $table)) {
344 foreach ($table['filters'] as $fieldName => $field) {
346 if ($fieldName == 'credit_accounting_code') {
347 $field['dbAlias'] = "CASE
348 WHEN financial_trxn_civireport.from_financial_account_id IS NOT NULL
349 THEN financial_account_civireport_credit_1.accounting_code
350 ELSE financial_account_civireport_credit_2.accounting_code
353 else if ($fieldName == 'credit_name') {
354 $field['dbAlias'] = "CASE
355 WHEN financial_trxn_civireport.from_financial_account_id IS NOT NULL
356 THEN financial_account_civireport_credit_1.id
357 ELSE financial_account_civireport_credit_2.id
360 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
361 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
362 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
363 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
365 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
368 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
370 $clause = $this->whereClause($field,
372 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
373 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
374 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
378 if (!empty($clause)) {
379 $clauses[] = $clause;
384 if (empty($clauses)) {
385 $this->_where
= 'WHERE ( 1 )';
388 $this->_where
= 'WHERE ' . implode(' AND ', $clauses);
392 function postProcess() {
393 // get the acl clauses built before we assemble the query
394 $this->buildACLClause($this->_aliases
['civicrm_contact']);
395 parent
::postProcess();
398 function statistics(&$rows) {
399 $statistics = parent
::statistics($rows);
401 $select = " SELECT COUNT({$this->_aliases['civicrm_financial_trxn']}.id ) as count,
402 {$this->_aliases['civicrm_contribution']}.currency,
404 WHEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_id IS NOT NULL
405 THEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.amount
406 ELSE {$this->_aliases['civicrm_entity_financial_trxn']}.amount
410 $sql = "{$select} {$this->_from} {$this->_where}
411 GROUP BY {$this->_aliases['civicrm_contribution']}.currency
414 $dao = CRM_Core_DAO
::executeQuery($sql);
415 while ($dao->fetch()) {
416 $amount[] = CRM_Utils_Money
::format($dao->amount
, $dao->currency
);
417 $avg[] = CRM_Utils_Money
::format(round(($dao->amount
/ $dao->count
), 2), $dao->currency
);
420 $statistics['counts']['amount'] = array(
421 'value' => implode(', ', $amount),
422 'title' => 'Total Amount',
423 'type' => CRM_Utils_Type
::T_STRING
,
425 $statistics['counts']['avg'] = array(
426 'value' => implode(', ', $avg),
427 'title' => 'Average',
428 'type' => CRM_Utils_Type
::T_STRING
,
433 function alterDisplay(&$rows) {
434 $contributionTypes = CRM_Contribute_PseudoConstant
::financialType();
435 $paymentInstruments = CRM_Contribute_PseudoConstant
::paymentInstrument();
436 $contributionStatus = CRM_Contribute_PseudoConstant
::contributionStatus();
437 foreach ($rows as $rowNum => $row) {
438 // convert display name to links
439 if (array_key_exists('civicrm_contact_sort_name', $row) &&
440 CRM_Utils_Array
::value('civicrm_contact_sort_name', $rows[$rowNum]) &&
441 array_key_exists('civicrm_contact_id', $row)
443 $url = CRM_Utils_System
::url('civicrm/contact/view',
444 'reset=1&cid=' . $row['civicrm_contact_id'],
447 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
448 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts('View Contact Summary for this Contact.');
451 // handle contribution status id
452 if ($value = CRM_Utils_Array
::value('civicrm_contribution_contribution_status_id', $row)) {
453 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = $contributionStatus[$value];
456 // handle payment instrument id
457 if ($value = CRM_Utils_Array
::value('civicrm_financial_trxn_payment_instrument_id', $row)) {
458 $rows[$rowNum]['civicrm_financial_trxn_payment_instrument_id'] = $paymentInstruments[$value];
461 // handle financial type id
462 if ($value = CRM_Utils_Array
::value('civicrm_line_item_financial_type_id', $row)) {
463 $rows[$rowNum]['civicrm_line_item_financial_type_id'] = $contributionTypes[$value];
465 if ($value = CRM_Utils_Array
::value('civicrm_entity_financial_trxn_amount', $row)) {
466 $rows[$rowNum]['civicrm_entity_financial_trxn_amount'] = CRM_Utils_Money
::format($rows[$rowNum]['civicrm_entity_financial_trxn_amount'],$rows[$rowNum]['civicrm_financial_trxn_currency']);