Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | // $Id$ | |
3 | ||
4 | /* | |
5 | +--------------------------------------------------------------------+ | |
6 | | CiviCRM version 4.3 | | |
7 | +--------------------------------------------------------------------+ | |
8 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
9 | +--------------------------------------------------------------------+ | |
10 | | This file is a part of CiviCRM. | | |
11 | | | | |
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. | | |
15 | | | | |
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. | | |
20 | | | | |
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 | +--------------------------------------------------------------------+ | |
28 | */ | |
29 | ||
30 | /** | |
31 | * | |
32 | * @package CRM | |
33 | * @copyright CiviCRM LLC (c) 2004-2013 | |
34 | * $Id$ | |
35 | * | |
36 | */ | |
37 | class CRM_Report_Form_Contribute_Bookkeeping extends CRM_Report_Form { | |
38 | protected $_addressField = FALSE; | |
39 | ||
40 | protected $_emailField = FALSE; | |
41 | ||
42 | protected $_summary = NULL; | |
43 | ||
44 | protected $_customGroupExtends = array( | |
45 | 'Membership'); function __construct() { | |
46 | $this->_columns = array( | |
47 | 'civicrm_contact' => | |
48 | array( | |
49 | 'dao' => 'CRM_Contact_DAO_Contact', | |
50 | 'fields' => | |
51 | array( | |
52 | 'sort_name' => | |
53 | array('title' => ts('Contact Name'), | |
54 | 'required' => TRUE, | |
55 | 'no_repeat' => TRUE, | |
56 | ), | |
57 | 'id' => | |
58 | array( | |
59 | 'no_display' => TRUE, | |
60 | 'required' => TRUE, | |
61 | ), | |
62 | ), | |
63 | 'filters' => | |
64 | array( | |
65 | 'sort_name' => | |
66 | array('title' => ts('Contact Name'), | |
67 | 'operator' => 'like', | |
68 | ), | |
69 | 'id' => | |
70 | array('title' => ts('Contact ID'), | |
71 | 'no_display' => TRUE, | |
72 | ), | |
73 | ), | |
74 | 'grouping' => 'contact-fields', | |
75 | ), | |
76 | 'civicrm_membership' => | |
77 | array( | |
78 | 'dao' => 'CRM_Member_DAO_Membership', | |
79 | 'fields' => | |
80 | array( | |
81 | 'id' => | |
82 | array('title' => ts('Membership #'), | |
83 | 'no_display' => TRUE, | |
84 | 'required' => TRUE, | |
85 | ), | |
86 | ), | |
87 | ), | |
88 | 'civicrm_contribution' => | |
89 | array( | |
90 | 'dao' => 'CRM_Contribute_DAO_Contribution', | |
91 | 'fields' => | |
92 | array( | |
93 | 'receive_date' => array('default' => TRUE), | |
94 | 'trxn_id' => array('title' => ts('Trans #'), | |
95 | 'default' => TRUE, | |
96 | ), | |
97 | 'invoice_id' => array('title' => ts('Invoice ID'), | |
98 | 'default' => TRUE, | |
99 | ), | |
100 | 'check_number' => array('title' => ts('Cheque #'), | |
101 | 'default' => TRUE, | |
102 | ), | |
103 | 'payment_instrument_id' => array('title' => ts('Payment Instrument'), | |
104 | 'default' => TRUE, | |
105 | ), | |
106 | 'contribution_status_id' => array('title' => ts('Status'), | |
107 | 'default' => TRUE, | |
108 | ), | |
109 | 'id' => array('title' => ts('Contribution #'), | |
110 | 'default' => TRUE, | |
111 | ), | |
112 | ), | |
113 | 'filters' => | |
114 | array( | |
115 | 'receive_date' => | |
116 | array('operatorType' => CRM_Report_Form::OP_DATE), | |
117 | 'payment_instrument_id' => | |
118 | array('title' => ts('Paid By'), | |
119 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
120 | 'options' => CRM_Contribute_PseudoConstant::paymentInstrument(), | |
121 | ), | |
122 | 'contribution_status_id' => | |
123 | array('title' => ts('Contribution Status'), | |
124 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
125 | 'options' => CRM_Contribute_PseudoConstant::contributionStatus(), | |
126 | 'default' => array(1), | |
127 | ), | |
128 | 'total_amount' => | |
129 | array('title' => ts('Contribution Amount')), | |
130 | ), | |
131 | 'grouping' => 'contri-fields', | |
132 | ), | |
133 | 'civicrm_financial_account' => array( | |
134 | 'dao' => 'CRM_Financial_DAO_FinancialAccount', | |
135 | 'fields' => array( | |
136 | 'debit_accounting_code' => array( | |
137 | 'title' => ts('Financial Account Code- Debit'), | |
138 | 'name' => 'accounting_code', | |
139 | 'alias' => 'financial_account_civireport_debit', | |
140 | 'default' => TRUE, | |
141 | ), | |
142 | 'credit_accounting_code' => array( | |
143 | 'title' => ts('Financial Account Code- Credit'), | |
144 | 'name' => 'accounting_code', | |
145 | 'alias' => 'financial_account_civireport_credit', | |
146 | 'default' => TRUE, | |
147 | ), | |
148 | ) | |
149 | ), | |
150 | 'civicrm_entity_financial_trxn' => array( | |
151 | 'dao' => 'CRM_Financial_DAO_EntityFinancialTrxn', | |
152 | 'fields' => array( | |
153 | 'amount' => array( | |
154 | 'title' => ts('Amount'), | |
155 | 'default' => TRUE, | |
156 | ), | |
157 | ), | |
158 | ), | |
159 | 'civicrm_line_item' => array( | |
160 | 'dao' => 'CRM_Price_DAO_LineItem', | |
161 | 'fields' => array( | |
162 | 'financial_type_id' => array('title' => ts('Financial Type'), | |
163 | 'default' => TRUE, | |
164 | ), | |
165 | ), | |
166 | 'filters' => array( | |
167 | 'financial_type_id' => array( | |
168 | 'title' => ts('Financial Type'), | |
169 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
170 | 'options' => CRM_Contribute_PseudoConstant::financialType(), | |
171 | ), | |
172 | ), | |
173 | ), | |
174 | ); | |
175 | parent::__construct(); | |
176 | } | |
177 | ||
178 | function preProcess() { | |
179 | parent::preProcess(); | |
180 | } | |
181 | ||
182 | function select() { | |
183 | $select = array(); | |
184 | ||
185 | $this->_columnHeaders = array(); | |
186 | foreach ($this->_columns as $tableName => $table) { | |
187 | if (array_key_exists('fields', $table)) { | |
188 | foreach ($table['fields'] as $fieldName => $field) { | |
189 | if (CRM_Utils_Array::value('required', $field) || | |
190 | CRM_Utils_Array::value($fieldName, $this->_params['fields']) | |
191 | ) { | |
192 | if ($fieldName != 'credit_accounting_code') { | |
193 | $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}"; | |
194 | } | |
195 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title']; | |
196 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field); | |
197 | } | |
198 | } | |
199 | } | |
200 | } | |
201 | ||
202 | $this->_select = "SELECT " . implode(', ', $select) . " "; | |
203 | $this->_select .= ", CASE | |
204 | WHEN trxn.from_financial_account_id IS NOT NULL | |
205 | THEN {$this->_aliases['civicrm_financial_account']}_credit_1.accounting_code | |
206 | ELSE {$this->_aliases['civicrm_financial_account']}_credit_2.accounting_code | |
207 | END AS civicrm_financial_account_credit_accounting_code "; | |
208 | } | |
209 | ||
210 | function from() { | |
211 | $this->_from = NULL; | |
212 | ||
213 | $this->_from = "FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom} | |
214 | INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']} | |
215 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id AND | |
216 | {$this->_aliases['civicrm_contribution']}.is_test = 0 | |
217 | LEFT JOIN civicrm_membership_payment payment | |
218 | ON ( {$this->_aliases['civicrm_contribution']}.id = payment.contribution_id ) | |
219 | LEFT JOIN civicrm_membership {$this->_aliases['civicrm_membership']} | |
220 | ON payment.membership_id = {$this->_aliases['civicrm_membership']}.id | |
221 | LEFT JOIN civicrm_entity_financial_trxn {$this->_aliases['civicrm_entity_financial_trxn']} | |
222 | ON ({$this->_aliases['civicrm_contribution']}.id = {$this->_aliases['civicrm_entity_financial_trxn']}.entity_id AND | |
223 | {$this->_aliases['civicrm_entity_financial_trxn']}.entity_table = 'civicrm_contribution') | |
224 | LEFT JOIN civicrm_financial_trxn trxn | |
225 | ON trxn.id = {$this->_aliases['civicrm_entity_financial_trxn']}.financial_trxn_id | |
226 | LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_debit | |
227 | ON trxn.to_financial_account_id = {$this->_aliases['civicrm_financial_account']}_debit.id | |
228 | LEFT JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_credit_1 | |
229 | ON trxn.from_financial_account_id = {$this->_aliases['civicrm_financial_account']}_credit_1.id | |
230 | LEFT JOIN civicrm_entity_financial_trxn {$this->_aliases['civicrm_entity_financial_trxn']}_item | |
231 | ON (trxn.id = {$this->_aliases['civicrm_entity_financial_trxn']}_item.financial_trxn_id AND | |
232 | {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_table = 'civicrm_financial_item') | |
233 | INNER JOIN civicrm_financial_item fitem | |
234 | ON fitem.id = {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_id | |
235 | INNER JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}_credit_2 | |
236 | ON fitem.financial_account_id = {$this->_aliases['civicrm_financial_account']}_credit_2.id | |
237 | INNER JOIN civicrm_line_item {$this->_aliases['civicrm_line_item']} | |
238 | ON fitem.entity_id = {$this->_aliases['civicrm_line_item']}.id AND fitem.entity_table = 'civicrm_line_item' "; | |
239 | } | |
240 | ||
241 | function orderBy() { | |
242 | $this->_orderBy = " ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_contribution']}.id, {$this->_aliases['civicrm_entity_financial_trxn']}.id "; | |
243 | } | |
244 | ||
245 | function postProcess() { | |
246 | // get the acl clauses built before we assemble the query | |
247 | $this->buildACLClause($this->_aliases['civicrm_contact']); | |
248 | parent::postProcess(); | |
249 | } | |
250 | ||
251 | function statistics(&$rows) { | |
252 | $statistics = parent::statistics($rows); | |
253 | ||
254 | $select = " | |
255 | SELECT COUNT({$this->_aliases['civicrm_entity_financial_trxn']}.amount ) as count, | |
256 | SUM( {$this->_aliases['civicrm_entity_financial_trxn']}.amount ) as amount, | |
257 | ROUND(AVG({$this->_aliases['civicrm_entity_financial_trxn']}.amount), 2) as avg | |
258 | "; | |
259 | ||
260 | $this->_statWhere = " WHERE {$this->_aliases['civicrm_entity_financial_trxn']}.entity_table = 'civicrm_financial_item'"; | |
261 | $sql = "{$select} {$this->_from} {$this->_statWhere}"; | |
262 | $dao = CRM_Core_DAO::executeQuery($sql); | |
263 | ||
264 | if ($dao->fetch()) { | |
265 | $statistics['counts']['amount'] = array( | |
266 | 'value' => $dao->amount, | |
267 | 'title' => 'Total Amount', | |
268 | 'type' => CRM_Utils_Type::T_MONEY, | |
269 | ); | |
270 | $statistics['counts']['avg'] = array( | |
271 | 'value' => $dao->avg, | |
272 | 'title' => 'Average', | |
273 | 'type' => CRM_Utils_Type::T_MONEY, | |
274 | ); | |
275 | } | |
276 | ||
277 | return $statistics; | |
278 | } | |
279 | ||
280 | function alterDisplay(&$rows) { | |
281 | // custom code to alter rows | |
282 | $checkList = array(); | |
283 | $entryFound = FALSE; | |
284 | $display_flag = $prev_cid = $cid = 0; | |
285 | $contributionTypes = CRM_Contribute_PseudoConstant::financialType(); | |
286 | $paymentInstruments = CRM_Contribute_PseudoConstant::paymentInstrument(); | |
287 | ||
288 | foreach ($rows as $rowNum => $row) { | |
289 | ||
290 | // convert display name to links | |
291 | if (array_key_exists('civicrm_contact_sort_name', $row) && | |
292 | CRM_Utils_Array::value('civicrm_contact_sort_name', $rows[$rowNum]) && | |
293 | array_key_exists('civicrm_contact_id', $row) | |
294 | ) { | |
295 | $url = CRM_Utils_System::url("civicrm/contact/view", | |
296 | 'reset=1&cid=' . $row['civicrm_contact_id'], | |
297 | $this->_absoluteUrl | |
298 | ); | |
299 | $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url; | |
300 | $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact Summary for this Contact."); | |
301 | } | |
302 | ||
303 | // handle contribution status id | |
304 | if (array_key_exists('civicrm_contribution_contribution_status_id', $row)) { | |
305 | if ($value = $row['civicrm_contribution_contribution_status_id']) { | |
306 | $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = CRM_Contribute_PseudoConstant::contributionStatus($value); | |
307 | } | |
308 | $entryFound = TRUE; | |
309 | } | |
310 | ||
311 | // handle payment instrument id | |
312 | if (array_key_exists('civicrm_contribution_payment_instrument_id', $row)) { | |
313 | if ($value = $row['civicrm_contribution_payment_instrument_id']) { | |
314 | $rows[$rowNum]['civicrm_contribution_payment_instrument_id'] = $paymentInstruments[$value]; | |
315 | } | |
316 | $entryFound = TRUE; | |
317 | } | |
318 | ||
319 | if ($value = CRM_Utils_Array::value('civicrm_line_item_financial_type_id', $row)) { | |
320 | $rows[$rowNum]['civicrm_line_item_financial_type_id'] = $contributionTypes[$value]; | |
321 | $entryFound = TRUE; | |
322 | } | |
323 | ||
324 | // skip looking further in rows, if first row itself doesn't | |
325 | // have the column we need | |
326 | if (!$entryFound) { | |
327 | break; | |
328 | } | |
329 | $lastKey = $rowNum; | |
330 | } | |
331 | } | |
332 | } | |
333 |