Merge pull request #1504 from davecivicrm/CRM-13282
[civicrm-core.git] / CRM / Report / Form / Contribute / Bookkeeping.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.4 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
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. |
14 | |
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. |
19 | |
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 +--------------------------------------------------------------------+
27 */
28
29 /**
30 *
31 * @package CRM
32 * @copyright CiviCRM LLC (c) 2004-2013
33 * $Id$
34 *
35 */
36 class CRM_Report_Form_Contribute_Bookkeeping extends CRM_Report_Form {
37 protected $_addressField = FALSE;
38
39 protected $_emailField = FALSE;
40
41 protected $_summary = NULL;
42
43 protected $_customGroupExtends = array(
44 'Membership');
45
46 function __construct() {
47 $this->_columns = array(
48 'civicrm_contact' =>
49 array(
50 'dao' => 'CRM_Contact_DAO_Contact',
51 'fields' =>
52 array(
53 'sort_name' =>
54 array('title' => ts('Contact Name'),
55 'required' => TRUE,
56 'no_repeat' => TRUE,
57 ),
58 'id' =>
59 array(
60 'no_display' => TRUE,
61 'required' => TRUE,
62 ),
63 'contact_type' =>
64 array(
65 'title' => ts('Contact Type'),
66 ),
67 'contact_sub_type' =>
68 array(
69 'title' => ts('Contact SubType'),
70 ),
71 ),
72 'filters' =>
73 array(
74 'sort_name' =>
75 array('title' => ts('Contact Name'),
76 'operator' => 'like',
77 ),
78 'id' =>
79 array('title' => ts('Contact ID'),
80 'no_display' => TRUE,
81 ),
82 ),
83 'grouping' => 'contact-fields',
84 ),
85 'civicrm_membership' =>
86 array(
87 'dao' => 'CRM_Member_DAO_Membership',
88 'fields' =>
89 array(
90 'id' =>
91 array('title' => ts('Membership #'),
92 'no_display' => TRUE,
93 'required' => TRUE,
94 ),
95 ),
96 ),
97 'civicrm_financial_account' => array(
98 'dao' => 'CRM_Financial_DAO_FinancialAccount',
99 'fields' => array(
100 'debit_accounting_code' => array(
101 'title' => ts('Financial Account Code - Debit'),
102 'name' => 'accounting_code',
103 'alias' => 'financial_account_civireport_debit',
104 'default' => TRUE,
105 ),
106 'credit_accounting_code' => array(
107 'title' => ts('Financial Account Code - Credit'),
108 'name' => 'accounting_code',
109 'alias' => 'financial_account_civireport_credit',
110 'default' => TRUE,
111 ),
112 'debit_name' => array(
113 'title' => ts('Financial Account Name - Debit'),
114 'name' => 'name',
115 'alias' => 'financial_account_civireport_debit',
116 'default' => TRUE,
117 ),
118 'credit_name' => array(
119 'title' => ts('Financial Account Name - Credit'),
120 'name' => 'name',
121 'alias' => 'financial_account_civireport_credit',
122 'default' => TRUE,
123 ),
124 ),
125 'filters' => array(
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',
132 ),
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'),
137 ),
138 'debit_name' => array(
139 'title' => ts('Financial Account Name - Debit'),
140 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
141 'options' => CRM_Contribute_PseudoConstant::financialAccount(),
142 'name' => 'id',
143 'alias' => 'financial_account_civireport_debit',
144 ),
145 'credit_name' => array(
146 'title' => ts('Financial Account Name - Credit'),
147 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
148 'options' => CRM_Contribute_PseudoConstant::financialAccount(),
149 ),
150 ),
151 ),
152 'civicrm_line_item' => array(
153 'dao' => 'CRM_Price_DAO_LineItem',
154 'fields' => array(
155 'financial_type_id' => array('title' => ts('Financial Type'),
156 'default' => TRUE,
157 ),
158 ),
159 'filters' => array(
160 'financial_type_id' => array(
161 'title' => ts('Financial Type'),
162 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
163 'options' => CRM_Contribute_PseudoConstant::financialType(),
164 ),
165 ),
166 ),
167 'civicrm_contribution' =>
168 array(
169 'dao' => 'CRM_Contribute_DAO_Contribution',
170 'fields' =>
171 array(
172 'receive_date' => array(
173 'default' => TRUE
174 ),
175 'invoice_id' => array(
176 'title' => ts('Invoice ID'),
177 'default' => TRUE,
178 ),
179 'contribution_status_id' => array('title' => ts('Contribution Status'),
180 'default' => TRUE,
181 ),
182 'id' => array('title' => ts('Contribution #'),
183 'default' => TRUE,
184 ),
185 ),
186 'filters' =>
187 array(
188 'receive_date' =>
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),
195 ),
196 ),
197 'grouping' => 'contri-fields',
198 ),
199 'civicrm_financial_trxn' => array(
200 'dao' => 'CRM_Financial_DAO_FinancialTrxn',
201 'fields' => array(
202 'check_number' => array(
203 'title' => ts('Cheque #'),
204 'default' => TRUE,
205 ),
206 'payment_instrument_id' => array('title' => ts('Payment Instrument'),
207 'default' => TRUE,
208 ),
209 'currency' => array(
210 'required' => TRUE,
211 'no_display' => TRUE,
212 ),
213 'trxn_date' => array(
214 'title' => ts('Transaction Date'),
215 'default' => TRUE,
216 'type' => CRM_Utils_Type::T_DATE,
217 ),
218 'trxn_id' => array(
219 'title' => ts('Trans #'),
220 'default' => TRUE,
221 ),
222 ),
223 'filters' =>
224 array(
225 'payment_instrument_id' => array(
226 'title' => ts('Payment Instrument'),
227 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
228 'options' => CRM_Contribute_PseudoConstant::paymentInstrument(),
229 ),
230 'currency' => array(
231 'title' => 'Currency',
232 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
233 'options' => CRM_Core_OptionGroup::values('currencies_enabled'),
234 'default' => NULL,
235 'type' => CRM_Utils_Type::T_STRING,
236 ),
237 'trxn_date' => array(
238 'title' => ts('Transaction Date'),
239 'operatorType' => CRM_Report_Form::OP_DATE,
240 'type' => CRM_Utils_Type::T_DATE,
241 ),
242 ),
243 ),
244 'civicrm_entity_financial_trxn' => array(
245 'dao' => 'CRM_Financial_DAO_EntityFinancialTrxn',
246 'fields' => array(
247 'amount' => array(
248 'title' => ts('Amount'),
249 'default' => TRUE,
250 'type' => CRM_Utils_Type::T_STRING,
251 ),
252 ),
253 'filters' =>
254 array(
255 'amount' =>
256 array('title' => ts('Amount')),
257 ),
258 ),
259 );
260 parent::__construct();
261 }
262
263 function preProcess() {
264 parent::preProcess();
265 }
266
267 function select() {
268 $select = array();
269
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'])
276 ) {
277 switch ($fieldName) {
278 case 'credit_accounting_code' :
279 $select[] = " CASE
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 ";
284 break;
285 case 'amount' :
286 $select[] = " CASE
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 ";
291 break;
292 case 'credit_name' :
293 $select[] = " CASE
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 ";
298 break;
299 default :
300 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
301 break;
302 }
303 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title'];
304 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
305 }
306 }
307 }
308 }
309
310 $this->_select = 'SELECT ' . implode(', ', $select) . ' ';
311 }
312
313 function from() {
314 $this->_from = NULL;
315
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' ";
342 }
343
344 function orderBy() {
345 $this->_orderBy = " ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_contribution']}.id, {$this->_aliases['civicrm_entity_financial_trxn']}.id ";
346 }
347
348 function where() {
349 foreach ($this->_columns as $tableName => $table) {
350 if (array_key_exists('filters', $table)) {
351 foreach ($table['filters'] as $fieldName => $field) {
352 $clause = NULL;
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
358 END";
359 }
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
365 END";
366 }
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);
371
372 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
373 }
374 else {
375 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
376 if ($op) {
377 $clause = $this->whereClause($field,
378 $op,
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)
382 );
383 }
384 }
385 if (!empty($clause)) {
386 $clauses[] = $clause;
387 }
388 }
389 }
390 }
391 if (empty($clauses)) {
392 $this->_where = 'WHERE ( 1 )';
393 }
394 else {
395 $this->_where = 'WHERE ' . implode(' AND ', $clauses);
396 }
397 }
398
399 function postProcess() {
400 // get the acl clauses built before we assemble the query
401 $this->buildACLClause($this->_aliases['civicrm_contact']);
402 parent::postProcess();
403 }
404
405 function statistics(&$rows) {
406 $statistics = parent::statistics($rows);
407
408 $select = " SELECT COUNT({$this->_aliases['civicrm_financial_trxn']}.id ) as count,
409 {$this->_aliases['civicrm_contribution']}.currency,
410 SUM(CASE
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
414 END) as amount
415 ";
416
417 $sql = "{$select} {$this->_from} {$this->_where}
418 GROUP BY {$this->_aliases['civicrm_contribution']}.currency
419 ";
420
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);
425 }
426
427 $statistics['counts']['amount'] = array(
428 'value' => implode(', ', $amount),
429 'title' => 'Total Amount',
430 'type' => CRM_Utils_Type::T_STRING,
431 );
432 $statistics['counts']['avg'] = array(
433 'value' => implode(', ', $avg),
434 'title' => 'Average',
435 'type' => CRM_Utils_Type::T_STRING,
436 );
437 return $statistics;
438 }
439
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)
449 ) {
450 $url = CRM_Utils_System::url('civicrm/contact/view',
451 'reset=1&cid=' . $row['civicrm_contact_id'],
452 $this->_absoluteUrl
453 );
454 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
455 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts('View Contact Summary for this Contact.');
456 }
457
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];
461 }
462
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];
466 }
467
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];
471 }
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']);
474 }
475 }
476 }
477 }
478