Commit | Line | Data |
---|---|---|
3c6d6452 PN |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.7 | | |
5 | +--------------------------------------------------------------------+ | |
0f03f337 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
3c6d6452 PN |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
0f03f337 | 31 | * @copyright CiviCRM LLC (c) 2004-2017 |
3c6d6452 PN |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Report_Form_Contribute_DeferredRevenue extends CRM_Report_Form { | |
36 | ||
37 | /** | |
38 | * Holds Deferred Financial Account | |
39 | */ | |
40 | protected $_deferredFinancialAccount = array(); | |
41 | ||
42 | /** | |
43 | */ | |
44 | public function __construct() { | |
a3bba030 | 45 | $this->_exposeContactID = FALSE; |
3c6d6452 PN |
46 | $this->_deferredFinancialAccount = CRM_Financial_BAO_FinancialAccount::getAllDeferredFinancialAccount(); |
47 | $this->_columns = array( | |
48 | 'civicrm_financial_account' => array( | |
49 | 'dao' => 'CRM_Financial_DAO_FinancialAccount', | |
a3bba030 PN |
50 | 'fields' => array( |
51 | 'name' => array( | |
52 | 'title' => ts('Deferred Account'), | |
53 | 'required' => TRUE, | |
54 | 'no_display' => TRUE, | |
55 | ), | |
56 | 'id' => array( | |
57 | 'title' => ts('Deferred Account ID'), | |
58 | 'required' => TRUE, | |
59 | 'no_display' => TRUE, | |
60 | ), | |
61 | 'accounting_code' => array( | |
62 | 'title' => ts('Deferred Accounting Code'), | |
63 | 'required' => TRUE, | |
64 | 'no_display' => TRUE, | |
65 | ), | |
66 | ), | |
3c6d6452 PN |
67 | 'filters' => array( |
68 | 'id' => array( | |
69 | 'title' => ts('Deferred Financial Account'), | |
70 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
71 | 'options' => $this->_deferredFinancialAccount, | |
72 | 'type' => CRM_Utils_Type::T_INT, | |
73 | ), | |
74 | ), | |
75 | ), | |
a3bba030 PN |
76 | 'civicrm_financial_account_1' => array( |
77 | 'dao' => 'CRM_Financial_DAO_FinancialAccount', | |
78 | 'fields' => array( | |
79 | 'name' => array( | |
80 | 'title' => ts('Revenue Account'), | |
81 | 'required' => TRUE, | |
82 | 'no_display' => TRUE, | |
83 | ), | |
84 | 'id' => array( | |
85 | 'title' => ts('Revenue Account ID'), | |
86 | 'required' => TRUE, | |
87 | 'no_display' => TRUE, | |
88 | ), | |
89 | 'accounting_code' => array( | |
90 | 'title' => ts('Revenue Accounting code'), | |
91 | 'no_display' => TRUE, | |
92 | 'required' => TRUE, | |
93 | ), | |
94 | ), | |
95 | ), | |
96 | 'civicrm_financial_item' => array( | |
97 | 'dao' => 'CRM_Financial_DAO_FinancialItem', | |
98 | 'fields' => array( | |
99 | 'status_id' => array( | |
100 | 'title' => ts('Status'), | |
101 | 'required' => TRUE, | |
102 | 'no_display' => TRUE, | |
103 | ), | |
104 | 'id' => array( | |
468477f3 | 105 | 'title' => ts('Financial Item ID'), |
a3bba030 PN |
106 | 'required' => TRUE, |
107 | 'no_display' => TRUE, | |
108 | ), | |
109 | 'description' => array( | |
110 | 'title' => ts('Description'), | |
111 | 'required' => TRUE, | |
112 | 'no_display' => TRUE, | |
113 | ), | |
114 | ), | |
115 | ), | |
116 | 'civicrm_financial_trxn_1' => array( | |
117 | 'dao' => 'CRM_Financial_DAO_FinancialTrxn', | |
118 | 'fields' => array( | |
119 | 'total_amount' => array( | |
120 | 'title' => ts('Deferred Transaction Amount'), | |
121 | 'required' => TRUE, | |
122 | 'no_display' => TRUE, | |
123 | 'dbAlias' => 'GROUP_CONCAT(financial_trxn_1_civireport.total_amount)', | |
124 | ), | |
125 | 'trxn_date' => array( | |
126 | 'title' => ts('Deferred Transaction Date'), | |
127 | 'required' => TRUE, | |
128 | 'no_display' => TRUE, | |
129 | 'dbAlias' => 'GROUP_CONCAT(financial_trxn_1_civireport.trxn_date)', | |
130 | ), | |
131 | ), | |
132 | ), | |
133 | 'civicrm_contact' => array( | |
134 | 'dao' => 'CRM_Contact_DAO_Contact', | |
135 | 'fields' => array( | |
136 | 'display_name' => array( | |
137 | 'title' => ts('Display_name'), | |
138 | 'required' => TRUE, | |
139 | 'no_display' => TRUE, | |
140 | ), | |
141 | ), | |
142 | ), | |
143 | 'civicrm_membership' => array( | |
144 | 'dao' => 'CRM_Member_DAO_Membership', | |
145 | 'fields' => array( | |
146 | 'start_date' => array( | |
147 | 'title' => ts('Start Date'), | |
148 | 'required' => TRUE, | |
149 | 'no_display' => TRUE, | |
150 | 'dbAlias' => 'IFNULL(membership_civireport.start_date, event_civireport.start_date)', | |
151 | ), | |
152 | 'end_date' => array( | |
153 | 'title' => ts('End Date'), | |
154 | 'required' => TRUE, | |
155 | 'no_display' => TRUE, | |
156 | 'dbdbAlias' => 'IFNULL(membership_civireport.end_date, event_civireport.end_date)', | |
157 | ), | |
158 | ), | |
159 | ), | |
160 | 'civicrm_event' => array( | |
161 | 'dao' => 'CRM_Event_DAO_Event', | |
162 | ), | |
163 | 'civicrm_participant' => array( | |
164 | 'dao' => 'CRM_Event_DAO_Participant', | |
165 | ), | |
5f2d82f6 PN |
166 | 'civicrm_batch' => array( |
167 | 'dao' => 'CRM_Batch_DAO_EntityBatch', | |
168 | 'grouping' => 'contri-fields', | |
169 | 'filters' => array( | |
170 | 'batch_id' => array( | |
171 | 'title' => ts('Batch Title'), | |
172 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
173 | 'options' => CRM_Batch_BAO_Batch::getBatches(), | |
174 | 'type' => CRM_Utils_Type::T_INT, | |
175 | ), | |
176 | ), | |
177 | ), | |
ca534f2b PN |
178 | 'civicrm_contribution' => array( |
179 | 'dao' => 'CRM_Contribute_DAO_Contribution', | |
a3bba030 PN |
180 | 'fields' => array( |
181 | 'id' => array( | |
182 | 'title' => ts('Contribution ID'), | |
183 | 'required' => TRUE, | |
184 | 'no_display' => TRUE, | |
185 | ), | |
186 | 'contact_id' => array( | |
187 | 'title' => ts('Contact ID'), | |
188 | 'required' => TRUE, | |
189 | 'no_display' => TRUE, | |
190 | ), | |
191 | 'source' => array( | |
192 | 'title' => ts('Source'), | |
193 | 'required' => TRUE, | |
194 | 'no_display' => TRUE, | |
195 | ), | |
196 | ), | |
ca534f2b PN |
197 | 'filters' => array( |
198 | 'receive_date' => array( | |
199 | 'title' => ts('Receive Date'), | |
200 | 'operatorType' => CRM_Report_Form::OP_DATE, | |
201 | 'type' => CRM_Utils_Type::T_DATE, | |
202 | ), | |
203 | 'cancel_date' => array( | |
204 | 'title' => ts('Cancel Date'), | |
205 | 'operatorType' => CRM_Report_Form::OP_DATE, | |
206 | 'type' => CRM_Utils_Type::T_DATE, | |
207 | ), | |
208 | 'revenue_recognition_date' => array( | |
209 | 'title' => ts('Revenue Recognition Date'), | |
210 | 'operatorType' => CRM_Report_Form::OP_DATE, | |
211 | 'type' => CRM_Utils_Type::T_DATE, | |
212 | ), | |
213 | ), | |
214 | ), | |
215 | 'civicrm_financial_trxn' => array( | |
216 | 'dao' => 'CRM_Financial_DAO_FinancialTrxn', | |
a3bba030 PN |
217 | 'fields' => array( |
218 | 'status_id' => array( | |
219 | 'title' => ts('Transaction Status'), | |
220 | 'required' => TRUE, | |
221 | 'no_display' => TRUE, | |
222 | ), | |
223 | 'trxn_date' => array( | |
224 | 'title' => ts('Transaction Date'), | |
225 | 'required' => TRUE, | |
226 | 'no_display' => TRUE, | |
227 | ), | |
228 | 'total_amount' => array( | |
229 | 'title' => ts('Transaction Amount'), | |
230 | 'required' => TRUE, | |
231 | 'no_display' => TRUE, | |
232 | ), | |
233 | ), | |
ca534f2b PN |
234 | 'filters' => array( |
235 | 'trxn_date' => array( | |
236 | 'title' => ts('Transaction Date'), | |
237 | 'operatorType' => CRM_Report_Form::OP_DATE, | |
238 | 'type' => CRM_Utils_Type::T_DATE, | |
239 | ), | |
240 | ), | |
241 | ), | |
3c6d6452 PN |
242 | ); |
243 | parent::__construct(); | |
244 | } | |
245 | ||
f5aedad3 PN |
246 | /** |
247 | * Pre process function. | |
248 | * | |
249 | * Called prior to build form. | |
250 | */ | |
3c6d6452 PN |
251 | public function preProcess() { |
252 | parent::preProcess(); | |
253 | } | |
254 | ||
f5aedad3 PN |
255 | /** |
256 | * Build from clause. | |
257 | */ | |
3c6d6452 PN |
258 | public function from() { |
259 | $deferredRelationship = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Deferred Revenue Account is' ")); | |
260 | $revenueRelationship = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Income Account is' ")); | |
a3bba030 | 261 | $this->_from = " FROM civicrm_financial_item {$this->_aliases['civicrm_financial_item']} |
3c6d6452 | 262 | INNER JOIN civicrm_entity_financial_account entity_financial_account_deferred |
a3bba030 | 263 | ON {$this->_aliases['civicrm_financial_item']}.financial_account_id = entity_financial_account_deferred.financial_account_id AND entity_financial_account_deferred.entity_table = 'civicrm_financial_type' |
3c6d6452 | 264 | AND entity_financial_account_deferred.account_relationship = {$deferredRelationship} |
a3bba030 PN |
265 | INNER JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']} |
266 | ON entity_financial_account_deferred.financial_account_id = {$this->_aliases['civicrm_financial_account']}.id | |
3c6d6452 PN |
267 | INNER JOIN civicrm_entity_financial_account entity_financial_account_revenue |
268 | ON entity_financial_account_deferred.entity_id = entity_financial_account_revenue.entity_id | |
269 | AND entity_financial_account_deferred.entity_table= entity_financial_account_revenue.entity_table | |
a3bba030 PN |
270 | INNER JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account_1']} |
271 | ON entity_financial_account_revenue.financial_account_id = {$this->_aliases['civicrm_financial_account_1']}.id | |
3c6d6452 PN |
272 | AND {$revenueRelationship} = entity_financial_account_revenue.account_relationship |
273 | INNER JOIN civicrm_entity_financial_trxn entity_financial_trxn_item | |
a3bba030 PN |
274 | ON entity_financial_trxn_item.entity_id = {$this->_aliases['civicrm_financial_item']}.id AND entity_financial_trxn_item.entity_table = 'civicrm_financial_item' |
275 | INNER JOIN civicrm_financial_trxn {$this->_aliases['civicrm_financial_trxn_1']} | |
276 | ON {$this->_aliases['civicrm_financial_trxn_1']}.from_financial_account_id = {$this->_aliases['civicrm_financial_account']}.id AND {$this->_aliases['civicrm_financial_trxn_1']}.id = entity_financial_trxn_item.financial_trxn_id | |
3c6d6452 | 277 | INNER JOIN civicrm_entity_financial_trxn financial_trxn_contribution |
a3bba030 PN |
278 | ON financial_trxn_contribution.financial_trxn_id = {$this->_aliases['civicrm_financial_trxn_1']}.id AND financial_trxn_contribution.entity_table = 'civicrm_contribution' |
279 | INNER JOIN civicrm_entity_financial_trxn entity_financial_trxn_contribution ON entity_financial_trxn_contribution.entity_id = {$this->_aliases['civicrm_financial_item']}.id and entity_financial_trxn_contribution.entity_table = 'civicrm_financial_item' | |
280 | INNER JOIN civicrm_financial_trxn {$this->_aliases['civicrm_financial_trxn']} ON {$this->_aliases['civicrm_financial_trxn']}.id = entity_financial_trxn_contribution.financial_trxn_id AND ({$this->_aliases['civicrm_financial_trxn']}.from_financial_account_id NOT IN (" . implode(',', array_keys($this->_deferredFinancialAccount)) . ") OR {$this->_aliases['civicrm_financial_trxn']}.from_financial_account_id IS NULL) | |
281 | INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']} | |
282 | ON {$this->_aliases['civicrm_contribution']}.id = financial_trxn_contribution.entity_id | |
283 | INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact']} | |
284 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id | |
3c6d6452 | 285 | INNER JOIN civicrm_line_item line_item |
a3bba030 PN |
286 | ON line_item.contribution_id = {$this->_aliases['civicrm_contribution']}.id |
287 | LEFT JOIN civicrm_membership {$this->_aliases['civicrm_membership']} | |
3c6d6452 PN |
288 | ON CASE |
289 | WHEN line_item.entity_table = 'civicrm_membership' | |
a3bba030 PN |
290 | THEN line_item.entity_id = {$this->_aliases['civicrm_membership']}.id |
291 | ELSE {$this->_aliases['civicrm_membership']}.id = 0 | |
3c6d6452 | 292 | END |
a3bba030 | 293 | LEFT JOIN civicrm_participant {$this->_aliases['civicrm_participant']} |
3c6d6452 PN |
294 | ON CASE |
295 | WHEN line_item.entity_table = 'civicrm_participant' | |
a3bba030 PN |
296 | THEN line_item.entity_id = {$this->_aliases['civicrm_participant']}.id |
297 | ELSE {$this->_aliases['civicrm_participant']}.id = 0 | |
3c6d6452 | 298 | END |
a3bba030 | 299 | LEFT JOIN civicrm_event {$this->_aliases['civicrm_event']} ON {$this->_aliases['civicrm_participant']}.event_id = {$this->_aliases['civicrm_event']}.id |
3c6d6452 | 300 | "; |
5f2d82f6 | 301 | |
246a29bd | 302 | if (!empty($this->_params['batch_id_value'])) { |
5f2d82f6 PN |
303 | $this->_from .= " |
304 | LEFT JOIN civicrm_entity_batch {$this->_aliases['civicrm_batch']} | |
305 | ON {$this->_aliases['civicrm_batch']}.entity_id = {$this->_aliases['civicrm_financial_trxn_1']}.id AND | |
306 | {$this->_aliases['civicrm_batch']}.entity_table = 'civicrm_financial_trxn'\n"; | |
307 | } | |
3c6d6452 PN |
308 | } |
309 | ||
f5aedad3 PN |
310 | /** |
311 | * Post process function. | |
312 | */ | |
a3bba030 | 313 | public function postProcess() { |
86f01e6c | 314 | $this->_noFields = TRUE; |
f5aedad3 PN |
315 | parent::postProcess(); |
316 | } | |
a3bba030 | 317 | |
f5aedad3 PN |
318 | /** |
319 | * Set limit. | |
320 | * | |
321 | * @param int $rowCount | |
f5aedad3 PN |
322 | */ |
323 | public function limit($rowCount = self::ROW_COUNT_LIMIT) { | |
324 | $this->_limit = NULL; | |
3c6d6452 PN |
325 | } |
326 | ||
86f01e6c PN |
327 | /** |
328 | * Build where clause. | |
329 | */ | |
330 | public function where() { | |
331 | parent::where(); | |
332 | $startDate = date('Y-m-01'); | |
333 | $endDate = date('Y-m-t', strtotime(date('ymd') . '+11 month')); | |
334 | $this->_where .= " AND {$this->_aliases['civicrm_financial_trxn_1']}.trxn_date BETWEEN '{$startDate}' AND '{$endDate}'"; | |
335 | } | |
336 | ||
f5aedad3 PN |
337 | /** |
338 | * Build group by clause. | |
339 | */ | |
3c6d6452 | 340 | public function groupBy() { |
a3bba030 | 341 | $this->_groupBy = "GROUP BY {$this->_aliases['civicrm_financial_account']}.id, {$this->_aliases['civicrm_financial_account_1']}.id, {$this->_aliases['civicrm_financial_item']}.id"; |
3c6d6452 PN |
342 | } |
343 | ||
344 | /** | |
345 | * Build output rows. | |
346 | * | |
347 | * @param string $sql | |
348 | * @param array $rows | |
349 | */ | |
350 | public function buildRows($sql, &$rows) { | |
351 | $dao = CRM_Core_DAO::executeQuery($sql); | |
352 | if (!is_array($rows)) { | |
353 | $rows = array(); | |
354 | } | |
355 | $statuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
356 | $dateColumn = array(); | |
357 | $columns = array( | |
358 | 'Transaction' => 1, | |
359 | 'Date of Transaction' => 1, | |
360 | 'Amount' => 1, | |
361 | 'Contribution ID' => 1, | |
362 | 'Item' => 1, | |
363 | 'Contact ID' => 1, | |
364 | 'Contact Name' => 1, | |
365 | 'Source' => 1, | |
366 | 'Start Date' => 1, | |
367 | 'End Date' => 1, | |
368 | ); | |
369 | $dateFormat = Civi::settings()->get('dateformatFinancialBatch'); | |
86f01e6c PN |
370 | for ($i = 0; $i < 12; $i++) { |
371 | //$columns[date('M, Y', strtotime("+1 month", date('Y-m-d')))] = 1; | |
372 | $columns[date('M, Y', strtotime(date('Y-m-d') . "+{$i} month"))] = 1; | |
373 | } | |
3c6d6452 | 374 | while ($dao->fetch()) { |
f422493a | 375 | $arraykey = $dao->civicrm_financial_account_id . '_' . $dao->civicrm_financial_account_1_id; |
3c6d6452 | 376 | if (empty($rows[$arraykey])) { |
f422493a | 377 | $rows[$arraykey]['label'] = "Deferred Revenue Account: {$dao->civicrm_financial_account_name} ({$dao->civicrm_financial_account_accounting_code}), Revenue Account: {$dao->civicrm_financial_account_1_name} {$dao->civicrm_financial_account_1_accounting_code}"; |
3c6d6452 | 378 | } |
f422493a PN |
379 | $rows[$arraykey]['rows'][$dao->civicrm_financial_item_id] = array( |
380 | 'Transaction' => $statuses[$dao->civicrm_financial_trxn_status_id], | |
381 | 'Date of Transaction' => CRM_Utils_Date::customFormat($dao->civicrm_financial_trxn_trxn_date, $dateFormat), | |
382 | 'Amount' => CRM_Utils_Money::format($dao->civicrm_financial_trxn_total_amount), | |
383 | 'Contribution ID' => $dao->civicrm_contribution_id, | |
384 | 'Item' => $dao->civicrm_financial_item_description, | |
385 | 'Contact ID' => $dao->civicrm_contribution_contact_id, | |
386 | 'Contact Name' => $dao->civicrm_contact_display_name, | |
387 | 'Source' => $dao->civicrm_contribution_source, | |
388 | 'Start Date' => CRM_Utils_Date::customFormat($dao->civicrm_membership_start_date, $dateFormat), | |
389 | 'End Date' => CRM_Utils_Date::customFormat($dao->civicrm_membership_end_date, $dateFormat), | |
3c6d6452 | 390 | ); |
f422493a PN |
391 | $trxnDate = explode(',', $dao->civicrm_financial_trxn_1_trxn_date); |
392 | $trxnAmount = explode(',', $dao->civicrm_financial_trxn_1_total_amount); | |
3c6d6452 PN |
393 | foreach ($trxnDate as $key => $date) { |
394 | $keyDate = date('M, Y', strtotime($date)); | |
86f01e6c PN |
395 | if (!array_key_exists($keyDate, $columns)) { |
396 | continue; | |
397 | } | |
f422493a | 398 | $rows[$arraykey]['rows'][$dao->civicrm_financial_item_id][$keyDate] = CRM_Utils_Money::format($trxnAmount[$key]); |
3c6d6452 PN |
399 | } |
400 | } | |
3c6d6452 PN |
401 | $this->_columnHeaders = $columns; |
402 | } | |
3c6d6452 PN |
403 | |
404 | } |