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_contribution' =>
92 'dao' => 'CRM_Contribute_DAO_Contribution',
95 'receive_date' => array('default' => TRUE),
96 'trxn_id' => array('title' => ts('Trans #'),
99 'invoice_id' => array('title' => ts('Invoice ID'),
102 'contribution_status_id' => array('title' => ts('Status'),
105 'id' => array('title' => ts('Contribution #'),
112 array('operatorType' => CRM_Report_Form
::OP_DATE
),
113 'contribution_status_id' =>
114 array('title' => ts('Contribution Status'),
115 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
116 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
117 'default' => array(1),
120 'grouping' => 'contri-fields',
122 'civicrm_line_item' => array(
123 'dao' => 'CRM_Price_DAO_LineItem',
125 'financial_type_id' => array('title' => ts('Financial Type'),
130 'financial_type_id' => array(
131 'title' => ts('Financial Type'),
132 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
133 'options' => CRM_Contribute_PseudoConstant
::financialType(),
137 'civicrm_financial_trxn' => array(
138 'dao' => 'CRM_Financial_DAO_FinancialTrxn',
140 'check_number' => array(
141 'title' => ts('Cheque #'),
144 'payment_instrument_id' => array('title' => ts('Payment Instrument'),
150 'payment_instrument_id' => array(
151 'title' => ts('Paid By'),
152 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
153 'options' => CRM_Contribute_PseudoConstant
::paymentInstrument(),
157 'civicrm_entity_financial_trxn' => array(
158 'dao' => 'CRM_Financial_DAO_EntityFinancialTrxn',
161 'title' => ts('Amount'),
168 array('title' => ts('Amount')),
171 'civicrm_financial_account' => array(
172 'dao' => 'CRM_Financial_DAO_FinancialAccount',
174 'debit_accounting_code' => array(
175 'title' => ts('Financial Account Code- Debit'),
176 'name' => 'accounting_code',
177 'alias' => 'financial_account_civireport_debit',
180 'credit_accounting_code' => array(
181 'title' => ts('Financial Account Code- Credit'),
182 'name' => 'accounting_code',
183 'alias' => 'financial_account_civireport_credit',
189 parent
::__construct();
192 function preProcess() {
193 parent
::preProcess();
199 $this->_columnHeaders
= array();
200 foreach ($this->_columns
as $tableName => $table) {
201 if (array_key_exists('fields', $table)) {
202 foreach ($table['fields'] as $fieldName => $field) {
203 if (CRM_Utils_Array
::value('required', $field) ||
204 CRM_Utils_Array
::value($fieldName, $this->_params
['fields'])
206 if ($fieldName != 'credit_accounting_code' && $fieldName != 'amount') {
207 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
209 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
210 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
216 $this->_select
= "SELECT " . implode(', ', $select) . " ";
217 $this->_select
.= ", CASE
218 WHEN {$this->_aliases['civicrm_financial_trxn']}.from_financial_account_id IS NOT NULL
219 THEN {$this->_aliases['civicrm_financial_account']}_credit_1.accounting_code
220 ELSE {$this->_aliases['civicrm_financial_account']}_credit_2.accounting_code
221 END AS civicrm_financial_account_credit_accounting_code, CASE
222 WHEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_id IS NOT NULL
223 THEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.amount
224 ELSE {$this->_aliases['civicrm_entity_financial_trxn']}.amount
225 END AS civicrm_entity_financial_trxn_amount ";
231 $this->_from
= "FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
232 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
233 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id AND
234 {$this->_aliases['civicrm_contribution']}.is_test = 0
235 LEFT JOIN civicrm_membership_payment payment
236 ON ( {$this->_aliases['civicrm_contribution']}.id = payment.contribution_id )
237 LEFT JOIN civicrm_membership {$this->_aliases['civicrm_membership']}
238 ON payment.membership_id = {$this->_aliases['civicrm_membership']}.id
239 LEFT JOIN civicrm_entity_financial_trxn {$this->_aliases['civicrm_entity_financial_trxn']}
240 ON ({$this->_aliases['civicrm_contribution']}.id = {$this->_aliases['civicrm_entity_financial_trxn']}.entity_id AND
241 {$this->_aliases['civicrm_entity_financial_trxn']}.entity_table = 'civicrm_contribution')
242 LEFT JOIN civicrm_financial_trxn {$this->_aliases['civicrm_financial_trxn']}
243 ON {$this->_aliases['civicrm_financial_trxn']}.id = {$this->_aliases['civicrm_entity_financial_trxn']}.financial_trxn_id
244 LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_debit
245 ON {$this->_aliases['civicrm_financial_trxn']}.to_financial_account_id = {$this->_aliases['civicrm_financial_account']}_debit.id
246 LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_credit_1
247 ON {$this->_aliases['civicrm_financial_trxn']}.from_financial_account_id = {$this->_aliases['civicrm_financial_account']}_credit_1.id
248 LEFT JOIN civicrm_entity_financial_trxn {$this->_aliases['civicrm_entity_financial_trxn']}_item
249 ON ({$this->_aliases['civicrm_financial_trxn']}.id = {$this->_aliases['civicrm_entity_financial_trxn']}_item.financial_trxn_id AND
250 {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_table = 'civicrm_financial_item')
251 LEFT JOIN civicrm_financial_item fitem
252 ON fitem.id = {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_id
253 LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_credit_2
254 ON fitem.financial_account_id = {$this->_aliases['civicrm_financial_account']}_credit_2.id
255 LEFT JOIN civicrm_line_item {$this->_aliases['civicrm_line_item']}
256 ON fitem.entity_id = {$this->_aliases['civicrm_line_item']}.id AND fitem.entity_table = 'civicrm_line_item' ";
260 $this->_orderBy
= " ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_contribution']}.id, {$this->_aliases['civicrm_entity_financial_trxn']}.id ";
263 function postProcess() {
264 // get the acl clauses built before we assemble the query
265 $this->buildACLClause($this->_aliases
['civicrm_contact']);
266 parent
::postProcess();
269 function statistics(&$rows) {
270 $statistics = parent
::statistics($rows);
272 $select = " SELECT COUNT({$this->_aliases['civicrm_financial_trxn']}.id ) as count,
274 WHEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_id IS NOT NULL
275 THEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.amount
276 ELSE {$this->_aliases['civicrm_entity_financial_trxn']}.amount
279 $sql = "{$select} {$this->_from} {$this->_where}";
280 $dao = CRM_Core_DAO
::executeQuery($sql);
283 $statistics['counts']['amount'] = array(
284 'value' => $dao->amount
,
285 'title' => 'Total Amount',
286 'type' => CRM_Utils_Type
::T_MONEY
,
288 $statistics['counts']['avg'] = array(
289 'value' => round(($dao->amount
/ $dao->count
), 2),
290 'title' => 'Average',
291 'type' => CRM_Utils_Type
::T_MONEY
,
298 function alterDisplay(&$rows) {
299 $contributionTypes = CRM_Contribute_PseudoConstant
::financialType();
300 $paymentInstruments = CRM_Contribute_PseudoConstant
::paymentInstrument();
301 $contributionStatus = CRM_Contribute_PseudoConstant
::contributionStatus();
302 foreach ($rows as $rowNum => $row) {
303 // convert display name to links
304 if (array_key_exists('civicrm_contact_sort_name', $row) &&
305 CRM_Utils_Array
::value('civicrm_contact_sort_name', $rows[$rowNum]) &&
306 array_key_exists('civicrm_contact_id', $row)
308 $url = CRM_Utils_System
::url("civicrm/contact/view",
309 'reset=1&cid=' . $row['civicrm_contact_id'],
312 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
313 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact Summary for this Contact.");
316 // handle contribution status id
317 if ($value = CRM_Utils_Array
::value('civicrm_contribution_contribution_status_id', $row)) {
318 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = $contributionStatus[$value];
321 // handle payment instrument id
322 if ($value = CRM_Utils_Array
::value('civicrm_financial_trxn_payment_instrument_id', $row)) {
323 $rows[$rowNum]['civicrm_financial_trxn_payment_instrument_id'] = $paymentInstruments[$value];
326 // handle financial type id
327 if ($value = CRM_Utils_Array
::value('civicrm_line_item_financial_type_id', $row)) {
328 $rows[$rowNum]['civicrm_line_item_financial_type_id'] = $contributionTypes[$value];