CRM-16189, more changes to the code
[civicrm-core.git] / CRM / Report / Form / Contribute / DeferredRevenue.php
CommitLineData
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 */
35class 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(
105 'title' => ts('Financial_item_id'),
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 ),
ca534f2b
PN
166 'civicrm_contribution' => array(
167 'dao' => 'CRM_Contribute_DAO_Contribution',
a3bba030
PN
168 'fields' => array(
169 'id' => array(
170 'title' => ts('Contribution ID'),
171 'required' => TRUE,
172 'no_display' => TRUE,
173 ),
174 'contact_id' => array(
175 'title' => ts('Contact ID'),
176 'required' => TRUE,
177 'no_display' => TRUE,
178 ),
179 'source' => array(
180 'title' => ts('Source'),
181 'required' => TRUE,
182 'no_display' => TRUE,
183 ),
184 ),
ca534f2b
PN
185 'filters' => array(
186 'receive_date' => array(
187 'title' => ts('Receive Date'),
188 'operatorType' => CRM_Report_Form::OP_DATE,
189 'type' => CRM_Utils_Type::T_DATE,
190 ),
191 'cancel_date' => array(
192 'title' => ts('Cancel Date'),
193 'operatorType' => CRM_Report_Form::OP_DATE,
194 'type' => CRM_Utils_Type::T_DATE,
195 ),
196 'revenue_recognition_date' => array(
197 'title' => ts('Revenue Recognition Date'),
198 'operatorType' => CRM_Report_Form::OP_DATE,
199 'type' => CRM_Utils_Type::T_DATE,
200 ),
201 ),
202 ),
203 'civicrm_financial_trxn' => array(
204 'dao' => 'CRM_Financial_DAO_FinancialTrxn',
a3bba030
PN
205 'fields' => array(
206 'status_id' => array(
207 'title' => ts('Transaction Status'),
208 'required' => TRUE,
209 'no_display' => TRUE,
210 ),
211 'trxn_date' => array(
212 'title' => ts('Transaction Date'),
213 'required' => TRUE,
214 'no_display' => TRUE,
215 ),
216 'total_amount' => array(
217 'title' => ts('Transaction Amount'),
218 'required' => TRUE,
219 'no_display' => TRUE,
220 ),
221 ),
ca534f2b
PN
222 'filters' => array(
223 'trxn_date' => array(
224 'title' => ts('Transaction Date'),
225 'operatorType' => CRM_Report_Form::OP_DATE,
226 'type' => CRM_Utils_Type::T_DATE,
227 ),
228 ),
229 ),
3c6d6452
PN
230 );
231 parent::__construct();
232 }
233
234 public function preProcess() {
235 parent::preProcess();
236 }
237
3c6d6452
PN
238 public function from() {
239 $deferredRelationship = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Deferred Revenue Account is' "));
240 $revenueRelationship = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Income Account is' "));
a3bba030 241 $this->_from = " FROM civicrm_financial_item {$this->_aliases['civicrm_financial_item']}
3c6d6452 242INNER JOIN civicrm_entity_financial_account entity_financial_account_deferred
a3bba030 243 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 244 AND entity_financial_account_deferred.account_relationship = {$deferredRelationship}
a3bba030
PN
245INNER JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account']}
246 ON entity_financial_account_deferred.financial_account_id = {$this->_aliases['civicrm_financial_account']}.id
3c6d6452
PN
247INNER JOIN civicrm_entity_financial_account entity_financial_account_revenue
248 ON entity_financial_account_deferred.entity_id = entity_financial_account_revenue.entity_id
249 AND entity_financial_account_deferred.entity_table= entity_financial_account_revenue.entity_table
a3bba030
PN
250INNER JOIN civicrm_financial_account {$this->_aliases['civicrm_financial_account_1']}
251 ON entity_financial_account_revenue.financial_account_id = {$this->_aliases['civicrm_financial_account_1']}.id
3c6d6452
PN
252 AND {$revenueRelationship} = entity_financial_account_revenue.account_relationship
253INNER JOIN civicrm_entity_financial_trxn entity_financial_trxn_item
a3bba030
PN
254 ON entity_financial_trxn_item.entity_id = {$this->_aliases['civicrm_financial_item']}.id AND entity_financial_trxn_item.entity_table = 'civicrm_financial_item'
255INNER JOIN civicrm_financial_trxn {$this->_aliases['civicrm_financial_trxn_1']}
256 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 257INNER JOIN civicrm_entity_financial_trxn financial_trxn_contribution
a3bba030
PN
258 ON financial_trxn_contribution.financial_trxn_id = {$this->_aliases['civicrm_financial_trxn_1']}.id AND financial_trxn_contribution.entity_table = 'civicrm_contribution'
259INNER 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'
260INNER 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)
261INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
262 ON {$this->_aliases['civicrm_contribution']}.id = financial_trxn_contribution.entity_id
263INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
264 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id
3c6d6452 265INNER JOIN civicrm_line_item line_item
a3bba030
PN
266 ON line_item.contribution_id = {$this->_aliases['civicrm_contribution']}.id
267LEFT JOIN civicrm_membership {$this->_aliases['civicrm_membership']}
3c6d6452
PN
268 ON CASE
269 WHEN line_item.entity_table = 'civicrm_membership'
a3bba030
PN
270 THEN line_item.entity_id = {$this->_aliases['civicrm_membership']}.id
271 ELSE {$this->_aliases['civicrm_membership']}.id = 0
3c6d6452 272 END
a3bba030 273LEFT JOIN civicrm_participant {$this->_aliases['civicrm_participant']}
3c6d6452
PN
274 ON CASE
275 WHEN line_item.entity_table = 'civicrm_participant'
a3bba030
PN
276 THEN line_item.entity_id = {$this->_aliases['civicrm_participant']}.id
277 ELSE {$this->_aliases['civicrm_participant']}.id = 0
3c6d6452 278 END
a3bba030 279LEFT JOIN civicrm_event {$this->_aliases['civicrm_event']} ON {$this->_aliases['civicrm_participant']}.event_id = {$this->_aliases['civicrm_event']}.id
3c6d6452
PN
280";
281 }
282
a3bba030 283 public function postProcess() {
86f01e6c 284 $this->_noFields = TRUE;
a3bba030
PN
285 // get ready with post process params
286 $this->beginPostProcess();
3c6d6452 287
a3bba030
PN
288 // build query
289 $sql = $this->buildQuery(FALSE);
3c6d6452 290
a3bba030
PN
291 // build array of result based on column headers. This method also allows
292 // modifying column headers before using it to build result set i.e $rows.
293 $rows = array();
294 $this->buildRows($sql, $rows);
3c6d6452 295
a3bba030
PN
296 // format result set.
297 $this->formatDisplay($rows);
298
299 // assign variables to templates
300 $this->doTemplateAssignment($rows);
301
302 // do print / pdf / instance stuff if needed
303 $this->endPostProcess($rows);
3c6d6452
PN
304 }
305
86f01e6c
PN
306 /**
307 * Build where clause.
308 */
309 public function where() {
310 parent::where();
311 $startDate = date('Y-m-01');
312 $endDate = date('Y-m-t', strtotime(date('ymd') . '+11 month'));
313 $this->_where .= " AND {$this->_aliases['civicrm_financial_trxn_1']}.trxn_date BETWEEN '{$startDate}' AND '{$endDate}'";
314 }
315
3c6d6452 316 public function groupBy() {
a3bba030 317 $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
318 }
319
320 /**
321 * Build output rows.
322 *
323 * @param string $sql
324 * @param array $rows
325 */
326 public function buildRows($sql, &$rows) {
327 $dao = CRM_Core_DAO::executeQuery($sql);
328 if (!is_array($rows)) {
329 $rows = array();
330 }
331 $statuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
332 $dateColumn = array();
333 $columns = array(
334 'Transaction' => 1,
335 'Date of Transaction' => 1,
336 'Amount' => 1,
337 'Contribution ID' => 1,
338 'Item' => 1,
339 'Contact ID' => 1,
340 'Contact Name' => 1,
341 'Source' => 1,
342 'Start Date' => 1,
343 'End Date' => 1,
344 );
345 $dateFormat = Civi::settings()->get('dateformatFinancialBatch');
86f01e6c
PN
346 for ($i = 0; $i < 12; $i++) {
347 //$columns[date('M, Y', strtotime("+1 month", date('Y-m-d')))] = 1;
348 $columns[date('M, Y', strtotime(date('Y-m-d') . "+{$i} month"))] = 1;
349 }
3c6d6452 350 while ($dao->fetch()) {
f422493a 351 $arraykey = $dao->civicrm_financial_account_id . '_' . $dao->civicrm_financial_account_1_id;
3c6d6452 352 if (empty($rows[$arraykey])) {
f422493a 353 $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 354 }
f422493a
PN
355 $rows[$arraykey]['rows'][$dao->civicrm_financial_item_id] = array(
356 'Transaction' => $statuses[$dao->civicrm_financial_trxn_status_id],
357 'Date of Transaction' => CRM_Utils_Date::customFormat($dao->civicrm_financial_trxn_trxn_date, $dateFormat),
358 'Amount' => CRM_Utils_Money::format($dao->civicrm_financial_trxn_total_amount),
359 'Contribution ID' => $dao->civicrm_contribution_id,
360 'Item' => $dao->civicrm_financial_item_description,
361 'Contact ID' => $dao->civicrm_contribution_contact_id,
362 'Contact Name' => $dao->civicrm_contact_display_name,
363 'Source' => $dao->civicrm_contribution_source,
364 'Start Date' => CRM_Utils_Date::customFormat($dao->civicrm_membership_start_date, $dateFormat),
365 'End Date' => CRM_Utils_Date::customFormat($dao->civicrm_membership_end_date, $dateFormat),
3c6d6452 366 );
f422493a
PN
367 $trxnDate = explode(',', $dao->civicrm_financial_trxn_1_trxn_date);
368 $trxnAmount = explode(',', $dao->civicrm_financial_trxn_1_total_amount);
3c6d6452
PN
369 foreach ($trxnDate as $key => $date) {
370 $keyDate = date('M, Y', strtotime($date));
86f01e6c
PN
371 if (!array_key_exists($keyDate, $columns)) {
372 continue;
373 }
f422493a 374 $rows[$arraykey]['rows'][$dao->civicrm_financial_item_id][$keyDate] = CRM_Utils_Money::format($trxnAmount[$key]);
3c6d6452
PN
375 }
376 }
3c6d6452
PN
377 $this->_columnHeaders = $columns;
378 }
379 /**
380 * @param $rows
381 *
382 */
a3bba030
PN
383 public function statistics(&$rows) {
384
385 }
3c6d6452
PN
386
387 /**
388 * Alter display of rows.
389 *
390 * Iterate through the rows retrieved via SQL and make changes for display purposes,
391 * such as rendering contacts as links.
392 *
393 * @param array $rows
394 * Rows generated by SQL, with an array for each row.
395 */
a3bba030
PN
396 public function alterDisplay(&$rows) {
397
398 }
3c6d6452
PN
399
400}