Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
32 | */ |
33 | class CRM_Report_Form_Member_Summary extends CRM_Report_Form { | |
34 | ||
35 | protected $_summary = NULL; | |
36 | protected $_interval = NULL; | |
37 | protected $_charts = array( | |
38 | '' => 'Tabular', | |
39 | 'barChart' => 'Bar Chart', | |
40 | 'pieChart' => 'Pie Chart', | |
41 | ); | |
42 | protected $_add2groupSupported = FALSE; | |
43 | ||
44 | protected $_customGroupExtends = array('Membership'); | |
f813f78e | 45 | protected $_customGroupGroupBy = FALSE; |
6a488035 TO |
46 | public $_drilldownReport = array('member/detail' => 'Link to Detail Report'); |
47 | ||
74cf4551 | 48 | /** |
1728e9a0 | 49 | * This report has not been optimised for group filtering. |
50 | * | |
51 | * The functionality for group filtering has been improved but not | |
52 | * all reports have been adjusted to take care of it. This report has not | |
53 | * and will run an inefficient query until fixed. | |
54 | * | |
55 | * CRM-19170 | |
56 | * | |
57 | * @var bool | |
74cf4551 | 58 | */ |
1728e9a0 | 59 | protected $groupFilterNotOptimised = TRUE; |
60 | ||
74cf4551 | 61 | /** |
73b448bf | 62 | * Class constructor. |
74cf4551 | 63 | */ |
00be9182 | 64 | public function __construct() { |
f813f78e | 65 | |
6a488035 | 66 | // UI for selecting columns to appear in the report list |
a7dbbc5d CD |
67 | // Array containing the columns, group_bys and filters build and provided to Form |
68 | ||
f813f78e | 69 | // Check if CiviCampaign is a) enabled and b) has active campaigns |
9d72cede | 70 | $config = CRM_Core_Config::singleton(); |
a7dbbc5d CD |
71 | $campaignEnabled = in_array("CiviCampaign", $config->enableComponents); |
72 | if ($campaignEnabled) { | |
73 | $getCampaigns = CRM_Campaign_BAO_Campaign::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE); | |
74 | $this->activeCampaigns = $getCampaigns['campaigns']; | |
75 | asort($this->activeCampaigns); | |
76 | } | |
6a488035 TO |
77 | |
78 | $this->_columns = array( | |
9d72cede | 79 | 'civicrm_membership' => array( |
6a488035 TO |
80 | 'dao' => 'CRM_Member_DAO_MembershipType', |
81 | 'grouping' => 'member-fields', | |
9d72cede EM |
82 | 'fields' => array( |
83 | 'membership_type_id' => array( | |
ccc29f8e | 84 | 'title' => ts('Membership Type'), |
6a488035 TO |
85 | 'required' => TRUE, |
86 | ), | |
87 | ), | |
9d72cede EM |
88 | 'filters' => array( |
89 | 'join_date' => array( | |
90 | 'title' => ts('Member Since'), | |
6a488035 TO |
91 | 'type' => CRM_Utils_Type::T_DATE, |
92 | 'operatorType' => CRM_Report_Form::OP_DATE, | |
93 | ), | |
9d72cede | 94 | 'membership_start_date' => array( |
6a488035 TO |
95 | 'name' => 'start_date', |
96 | 'title' => ts('Membership Start Date'), | |
97 | 'type' => CRM_Utils_Type::T_DATE, | |
98 | 'operatorType' => CRM_Report_Form::OP_DATE, | |
99 | ), | |
9d72cede | 100 | 'membership_end_date' => array( |
6a488035 TO |
101 | 'name' => 'end_date', |
102 | 'title' => ts('Membership End Date'), | |
103 | 'type' => CRM_Utils_Type::T_DATE, | |
104 | 'operatorType' => CRM_Report_Form::OP_DATE, | |
105 | ), | |
9d72cede EM |
106 | 'owner_membership_id' => array( |
107 | 'title' => ts('Membership Owner ID'), | |
8043337a | 108 | 'type' => CRM_Utils_Type::T_INT, |
5a9a44d9 DS |
109 | 'operatorType' => CRM_Report_Form::OP_INT, |
110 | ), | |
9d72cede EM |
111 | 'membership_type_id' => array( |
112 | 'title' => ts('Membership Type'), | |
8ee006e7 | 113 | 'type' => CRM_Utils_Type::T_INT, |
6a488035 TO |
114 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
115 | 'options' => CRM_Member_PseudoConstant::membershipType(), | |
116 | ), | |
9d72cede EM |
117 | 'status_id' => array( |
118 | 'title' => ts('Membership Status'), | |
8ee006e7 | 119 | 'type' => CRM_Utils_Type::T_INT, |
6a488035 TO |
120 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
121 | 'options' => CRM_Member_PseudoConstant::membershipStatus(NULL, NULL, 'label'), | |
122 | ), | |
123 | ), | |
9d72cede EM |
124 | 'group_bys' => array( |
125 | 'join_date' => array( | |
126 | 'title' => ts('Member Since'), | |
6a488035 TO |
127 | 'default' => TRUE, |
128 | 'frequency' => TRUE, | |
129 | 'chart' => TRUE, | |
130 | 'type' => 12, | |
131 | ), | |
9d72cede | 132 | 'membership_type_id' => array( |
ccc29f8e | 133 | 'title' => ts('Membership Type'), |
6a488035 TO |
134 | 'default' => TRUE, |
135 | 'chart' => TRUE, | |
136 | ), | |
137 | ), | |
138 | ), | |
9d72cede | 139 | 'civicrm_contact' => array( |
6a488035 | 140 | 'dao' => 'CRM_Contact_DAO_Contact', |
9d72cede EM |
141 | 'fields' => array( |
142 | 'contact_id' => array( | |
6a488035 TO |
143 | 'no_display' => TRUE, |
144 | ), | |
9d72cede | 145 | 'contact_type' => array( |
30f85891 RN |
146 | 'title' => ts('Contact Type'), |
147 | ), | |
9d72cede | 148 | 'contact_sub_type' => array( |
b8f96eb8 | 149 | 'title' => ts('Contact Subtype'), |
30f85891 | 150 | ), |
6a488035 TO |
151 | ), |
152 | ), | |
9d72cede | 153 | 'civicrm_contribution' => array( |
6a488035 | 154 | 'dao' => 'CRM_Contribute_DAO_Contribution', |
9d72cede EM |
155 | 'fields' => array( |
156 | 'currency' => array( | |
157 | 'required' => TRUE, | |
35d8497b | 158 | 'no_display' => TRUE, |
159 | ), | |
9d72cede EM |
160 | 'total_amount' => array( |
161 | 'title' => ts('Amount Statistics'), | |
6a488035 | 162 | 'required' => TRUE, |
9d72cede EM |
163 | 'statistics' => array( |
164 | 'sum' => ts('Total Payments Made'), | |
6a488035 TO |
165 | 'count' => ts('Contribution Count'), |
166 | 'avg' => ts('Average'), | |
167 | ), | |
168 | ), | |
169 | ), | |
9d72cede EM |
170 | 'filters' => array( |
171 | 'currency' => array( | |
ccc29f8e | 172 | 'title' => ts('Currency'), |
35d8497b | 173 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
174 | 'options' => CRM_Core_OptionGroup::values('currencies_enabled'), | |
d1b0931b | 175 | 'default' => NULL, |
35d8497b | 176 | 'type' => CRM_Utils_Type::T_STRING, |
177 | ), | |
9d72cede EM |
178 | 'contribution_status_id' => array( |
179 | 'title' => ts('Contribution Status'), | |
6a488035 TO |
180 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
181 | 'options' => CRM_Contribute_PseudoConstant::contributionStatus(), | |
182 | ), | |
183 | ), | |
184 | 'grouping' => 'member-fields', | |
185 | ), | |
186 | ); | |
187 | $this->_tagFilter = TRUE; | |
f813f78e | 188 | |
a7dbbc5d CD |
189 | // If we have a campaign, build out the relevant elements |
190 | if ($campaignEnabled && !empty($this->activeCampaigns)) { | |
191 | $this->_columns['civicrm_membership']['fields']['campaign_id'] = array( | |
ccc29f8e | 192 | 'title' => ts('Campaign'), |
a7dbbc5d CD |
193 | 'default' => 'false', |
194 | ); | |
9d72cede EM |
195 | $this->_columns['civicrm_membership']['filters']['campaign_id'] = array( |
196 | 'title' => ts('Campaign'), | |
a7dbbc5d CD |
197 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
198 | 'options' => $this->activeCampaigns, | |
942b1d01 | 199 | 'type' => CRM_Utils_Type::T_INT, |
a7dbbc5d | 200 | ); |
a7dbbc5d CD |
201 | $this->_columns['civicrm_membership']['group_bys']['campaign_id'] = array('title' => ts('Campaign')); |
202 | } | |
f813f78e | 203 | |
6a488035 | 204 | $this->_groupFilter = TRUE; |
35d8497b | 205 | $this->_currencyColumn = 'civicrm_contribution_currency'; |
6a488035 TO |
206 | parent::__construct(); |
207 | } | |
208 | ||
00be9182 | 209 | public function select() { |
6a488035 TO |
210 | $select = array(); |
211 | $groupBys = FALSE; | |
212 | $this->_columnHeaders = array(); | |
213 | $select[] = " COUNT( DISTINCT {$this->_aliases['civicrm_membership']}.id ) as civicrm_membership_member_count"; | |
214 | $select['joinDate'] = " {$this->_aliases['civicrm_membership']}.join_date as civicrm_membership_member_join_date"; | |
9d72cede EM |
215 | $this->_columnHeaders["civicrm_membership_member_join_date"] = array( |
216 | 'title' => ts('Member Since'), | |
6a488035 TO |
217 | 'type' => CRM_Utils_Type::T_DATE, |
218 | ); | |
219 | foreach ($this->_columns as $tableName => $table) { | |
220 | if (array_key_exists('group_bys', $table)) { | |
221 | foreach ($table['group_bys'] as $fieldName => $field) { | |
a7488080 | 222 | if (!empty($this->_params['group_bys'][$fieldName])) { |
6a488035 TO |
223 | |
224 | switch (CRM_Utils_Array::value($fieldName, $this->_params['group_bys_freq'])) { | |
225 | case 'YEARWEEK': | |
226 | $select[] = "DATE_SUB({$field['dbAlias']}, INTERVAL WEEKDAY({$field['dbAlias']}) DAY) AS {$tableName}_{$fieldName}_start"; | |
227 | ||
9d72cede EM |
228 | $select[] = "YEARWEEK({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal"; |
229 | $select[] = "WEEKOFYEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval"; | |
6a488035 TO |
230 | $field['title'] = 'Week'; |
231 | break; | |
232 | ||
233 | case 'YEAR': | |
9d72cede EM |
234 | $select[] = "MAKEDATE(YEAR({$field['dbAlias']}), 1) AS {$tableName}_{$fieldName}_start"; |
235 | $select[] = "YEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal"; | |
236 | $select[] = "YEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval"; | |
6a488035 TO |
237 | $field['title'] = 'Year'; |
238 | break; | |
239 | ||
240 | case 'MONTH': | |
9d72cede EM |
241 | $select[] = "DATE_SUB({$field['dbAlias']}, INTERVAL (DAYOFMONTH({$field['dbAlias']})-1) DAY) as {$tableName}_{$fieldName}_start"; |
242 | $select[] = "MONTH({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal"; | |
243 | $select[] = "MONTHNAME({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval"; | |
6a488035 TO |
244 | $field['title'] = 'Month'; |
245 | break; | |
246 | ||
247 | case 'QUARTER': | |
9d72cede EM |
248 | $select[] = "STR_TO_DATE(CONCAT( 3 * QUARTER( {$field['dbAlias']} ) -2 , '/', '1', '/', YEAR( {$field['dbAlias']} ) ), '%m/%d/%Y') AS {$tableName}_{$fieldName}_start"; |
249 | $select[] = "QUARTER({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal"; | |
250 | $select[] = "QUARTER({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval"; | |
6a488035 TO |
251 | $field['title'] = 'Quarter'; |
252 | break; | |
253 | } | |
a7488080 | 254 | if (!empty($this->_params['group_bys_freq'][$fieldName])) { |
6a488035 | 255 | $this->_interval = $field['title']; |
389bcebf | 256 | $this->_columnHeaders["{$tableName}_{$fieldName}_start"]['title'] = $field['title'] . ' Beginning'; |
6a488035 TO |
257 | $this->_columnHeaders["{$tableName}_{$fieldName}_start"]['type'] = $field['type']; |
258 | $this->_columnHeaders["{$tableName}_{$fieldName}_start"]['group_by'] = $this->_params['group_bys_freq'][$fieldName]; | |
259 | ||
b44e3f84 | 260 | // just to make sure these values are transferred to rows. |
6a488035 TO |
261 | // since we need that for calculation purpose, |
262 | // e.g making subtotals look nicer or graphs | |
263 | $this->_columnHeaders["{$tableName}_{$fieldName}_interval"] = array('no_display' => TRUE); | |
264 | $this->_columnHeaders["{$tableName}_{$fieldName}_subtotal"] = array('no_display' => TRUE); | |
265 | } | |
266 | $groupBys = TRUE; | |
267 | } | |
268 | } | |
269 | } | |
270 | // end of select | |
271 | ||
272 | if (array_key_exists('fields', $table)) { | |
273 | foreach ($table['fields'] as $fieldName => $field) { | |
9d72cede EM |
274 | if (!empty($field['required']) || |
275 | !empty($this->_params['fields'][$fieldName]) | |
276 | ) { | |
6a488035 TO |
277 | |
278 | // only include statistics columns if set | |
a7488080 | 279 | if (!empty($field['statistics'])) { |
6a488035 TO |
280 | $this->_statFields[] = 'civicrm_membership_member_count'; |
281 | foreach ($field['statistics'] as $stat => $label) { | |
282 | switch (strtolower($stat)) { | |
283 | case 'sum': | |
284 | $select[] = "IFNULL(SUM({$field['dbAlias']}), 0) as {$tableName}_{$fieldName}_{$stat}"; | |
285 | $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label; | |
286 | $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type']; | |
287 | $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}"; | |
288 | break; | |
289 | ||
290 | case 'count': | |
291 | $select[] = "COUNT({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}"; | |
292 | $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type::T_INT; | |
293 | $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label; | |
294 | $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}"; | |
295 | break; | |
296 | ||
297 | case 'avg': | |
298 | $select[] = "IFNULL(ROUND(AVG({$field['dbAlias']}),2), 0) as {$tableName}_{$fieldName}_{$stat}"; | |
299 | $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type']; | |
300 | $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label; | |
301 | $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}"; | |
302 | break; | |
303 | } | |
304 | } | |
305 | } | |
306 | elseif ($fieldName == 'membership_type_id') { | |
9d72cede EM |
307 | if (empty($this->_params['group_bys']['membership_type_id']) && |
308 | !empty($this->_params['group_bys']['join_date']) | |
309 | ) { | |
6a488035 TO |
310 | $select[] = "GROUP_CONCAT(DISTINCT {$field['dbAlias']} ORDER BY {$field['dbAlias']} ) as {$tableName}_{$fieldName}"; |
311 | } | |
312 | else { | |
313 | $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}"; | |
314 | } | |
315 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title']; | |
316 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['operatorType'] = CRM_Utils_Array::value('operatorType', $field); | |
317 | } | |
318 | else { | |
319 | $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}"; | |
320 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title']; | |
321 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['operatorType'] = CRM_Utils_Array::value('operatorType', $field); | |
322 | } | |
323 | } | |
324 | } | |
325 | } | |
9d72cede EM |
326 | $this->_columnHeaders["civicrm_membership_member_count"] = array( |
327 | 'title' => ts('Member Count'), | |
6a488035 TO |
328 | 'type' => CRM_Utils_Type::T_INT, |
329 | ); | |
330 | } | |
331 | //If grouping is availabled then remove join date from field | |
332 | if ($groupBys) { | |
333 | unset($select['joinDate']); | |
334 | unset($this->_columnHeaders["civicrm_membership_member_join_date"]); | |
335 | } | |
d1641c51 | 336 | $this->_selectClauses = $select; |
6a488035 TO |
337 | $this->_select = "SELECT " . implode(', ', $select) . " "; |
338 | } | |
339 | ||
00be9182 | 340 | public function from() { |
6a488035 TO |
341 | $this->_from = " |
342 | FROM civicrm_membership {$this->_aliases['civicrm_membership']} | |
f813f78e | 343 | |
344 | LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']} ON ( {$this->_aliases['civicrm_membership']}.contact_id = {$this->_aliases['civicrm_contact']}.id ) | |
345 | ||
346 | LEFT JOIN civicrm_membership_status | |
6a488035 TO |
347 | ON ({$this->_aliases['civicrm_membership']}.status_id = civicrm_membership_status.id ) |
348 | LEFT JOIN civicrm_membership_payment payment | |
349 | ON ( {$this->_aliases['civicrm_membership']}.id = payment.membership_id ) | |
f813f78e | 350 | LEFT JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']} |
6a488035 TO |
351 | ON payment.contribution_id = {$this->_aliases['civicrm_contribution']}.id"; |
352 | } | |
9d72cede | 353 | |
00be9182 | 354 | public function where() { |
74cf4551 | 355 | $this->_whereClauses[] = "{$this->_aliases['civicrm_membership']}.is_test = 0 AND |
d51206ff | 356 | {$this->_aliases['civicrm_contact']}.is_deleted = 0"; |
aa83a942 | 357 | parent::where(); |
6a488035 TO |
358 | } |
359 | ||
00be9182 | 360 | public function groupBy() { |
47c0ee09 | 361 | $this->_groupBy = ""; |
6a488035 TO |
362 | if (is_array($this->_params['group_bys']) && |
363 | !empty($this->_params['group_bys']) | |
364 | ) { | |
70e504f2 | 365 | foreach ($this->_columns as $table) { |
6a488035 TO |
366 | if (array_key_exists('group_bys', $table)) { |
367 | foreach ($table['group_bys'] as $fieldName => $field) { | |
a7488080 CW |
368 | if (!empty($this->_params['group_bys'][$fieldName])) { |
369 | if (!empty($field['chart'])) { | |
6a488035 TO |
370 | $this->assign('chartSupported', TRUE); |
371 | } | |
9d72cede EM |
372 | if (!empty($table['group_bys'][$fieldName]['frequency']) && |
373 | !empty($this->_params['group_bys_freq'][$fieldName]) | |
374 | ) { | |
6a488035 | 375 | |
20047181 | 376 | $append = "YEAR({$field['dbAlias']})"; |
6a488035 | 377 | if (in_array(strtolower($this->_params['group_bys_freq'][$fieldName]), |
9d72cede EM |
378 | array('year') |
379 | )) { | |
6a488035 TO |
380 | $append = ''; |
381 | } | |
70e504f2 | 382 | $this->_groupByArray[] = $append; |
383 | $this->_groupByArray[] = "{$this->_params['group_bys_freq'][$fieldName]}({$field['dbAlias']})"; | |
6a488035 TO |
384 | $append = TRUE; |
385 | } | |
386 | else { | |
70e504f2 | 387 | $this->_groupByArray[] = $field['dbAlias']; |
6a488035 TO |
388 | } |
389 | } | |
390 | } | |
391 | } | |
392 | } | |
393 | ||
394 | $this->_rollup = ' WITH ROLLUP'; | |
70e504f2 | 395 | $this->_select = CRM_Contact_BAO_Query::appendAnyValueToSelect($this->_selectClauses, array_filter($this->_groupByArray)); |
396 | $this->_groupBy = 'GROUP BY ' . implode(', ', array_filter($this->_groupByArray)) . | |
9d72cede | 397 | " {$this->_rollup} "; |
6a488035 TO |
398 | } |
399 | else { | |
b708c08d | 400 | $this->_groupBy = CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, "{$this->_aliases['civicrm_membership']}.join_date"); |
6a488035 TO |
401 | } |
402 | } | |
403 | ||
74cf4551 EM |
404 | /** |
405 | * @param $rows | |
406 | * | |
407 | * @return array | |
408 | */ | |
00be9182 | 409 | public function statistics(&$rows) { |
6a488035 | 410 | $statistics = parent::statistics($rows); |
6a488035 TO |
411 | $select = " |
412 | SELECT COUNT({$this->_aliases['civicrm_contribution']}.total_amount ) as count, | |
413 | IFNULL(SUM({$this->_aliases['civicrm_contribution']}.total_amount ), 0) as amount, | |
414 | IFNULL(ROUND(AVG({$this->_aliases['civicrm_contribution']}.total_amount), 2),0) as avg, | |
35d8497b | 415 | COUNT( DISTINCT {$this->_aliases['civicrm_membership']}.id ) as memberCount, |
416 | {$this->_aliases['civicrm_contribution']}.currency as currency | |
6a488035 TO |
417 | "; |
418 | ||
35d8497b | 419 | $sql = "{$select} {$this->_from} {$this->_where} |
f813f78e | 420 | GROUP BY {$this->_aliases['civicrm_contribution']}.currency |
35d8497b | 421 | "; |
6a488035 | 422 | |
35d8497b | 423 | $dao = CRM_Core_DAO::executeQuery($sql); |
f813f78e | 424 | |
35d8497b | 425 | $totalAmount = $average = array(); |
426 | $count = $memberCount = 0; | |
427 | while ($dao->fetch()) { | |
389bcebf | 428 | $totalAmount[] = CRM_Utils_Money::format($dao->amount, $dao->currency) . "(" . $dao->count . ")"; |
9d72cede | 429 | $average[] = CRM_Utils_Money::format($dao->avg, $dao->currency); |
35d8497b | 430 | $count += $dao->count; |
431 | $memberCount += $dao->memberCount; | |
432 | } | |
433 | $statistics['counts']['amount'] = array( | |
434 | 'title' => ts('Total Amount'), | |
435 | 'value' => implode(', ', $totalAmount), | |
436 | 'type' => CRM_Utils_Type::T_STRING, | |
437 | ); | |
438 | $statistics['counts']['count'] = array( | |
be205937 | 439 | 'title' => ts('Total Contributions'), |
35d8497b | 440 | 'value' => $count, |
441 | ); | |
442 | $statistics['counts']['memberCount'] = array( | |
443 | 'title' => ts('Total Members'), | |
444 | 'value' => $memberCount, | |
445 | ); | |
446 | $statistics['counts']['avg'] = array( | |
447 | 'title' => ts('Average'), | |
448 | 'value' => implode(', ', $average), | |
449 | 'type' => CRM_Utils_Type::T_STRING, | |
450 | ); | |
6a488035 | 451 | |
9d72cede | 452 | if (!(int) $statistics['counts']['amount']['value']) { |
35d8497b | 453 | //if total amount is zero then hide Chart Options |
454 | $this->assign('chartSupported', FALSE); | |
6a488035 TO |
455 | } |
456 | ||
457 | return $statistics; | |
458 | } | |
459 | ||
00be9182 | 460 | public function postProcess() { |
6a488035 TO |
461 | parent::postProcess(); |
462 | } | |
463 | ||
74cf4551 EM |
464 | /** |
465 | * @param $rows | |
466 | */ | |
00be9182 | 467 | public function buildChart(&$rows) { |
6a488035 TO |
468 | $graphRows = array(); |
469 | $count = 0; | |
470 | $membershipTypeValues = CRM_Member_PseudoConstant::membershipType(); | |
471 | $isMembershipType = CRM_Utils_Array::value('membership_type_id', $this->_params['group_bys']); | |
472 | $isJoiningDate = CRM_Utils_Array::value('join_date', $this->_params['group_bys']); | |
a7488080 | 473 | if (!empty($this->_params['charts'])) { |
6a488035 TO |
474 | foreach ($rows as $key => $row) { |
475 | if (!($row['civicrm_membership_join_date_subtotal'] && | |
9d72cede EM |
476 | $row['civicrm_membership_membership_type_id'] |
477 | ) | |
478 | ) { | |
6a488035 TO |
479 | continue; |
480 | } | |
481 | if ($isMembershipType) { | |
482 | $join_date = CRM_Utils_Array::value('civicrm_membership_join_date_start', $row); | |
483 | $displayInterval = CRM_Utils_Array::value('civicrm_membership_join_date_interval', $row); | |
484 | if ($join_date) { | |
485 | list($year, $month) = explode('-', $join_date); | |
486 | } | |
a7488080 | 487 | if (!empty($row['civicrm_membership_join_date_subtotal'])) { |
6a488035 TO |
488 | |
489 | switch ($this->_interval) { | |
490 | case 'Month': | |
491 | $displayRange = $displayInterval . ' ' . $year; | |
492 | break; | |
493 | ||
494 | case 'Quarter': | |
495 | $displayRange = 'Quarter ' . $displayInterval . ' of ' . $year; | |
496 | break; | |
497 | ||
498 | case 'Week': | |
499 | $displayRange = 'Week ' . $displayInterval . ' of ' . $year; | |
500 | break; | |
501 | ||
502 | case 'Year': | |
503 | $displayRange = $year; | |
504 | break; | |
505 | } | |
9d72cede EM |
506 | $membershipType = $displayRange . "-" . |
507 | $membershipTypeValues[$row['civicrm_membership_membership_type_id']]; | |
6a488035 TO |
508 | } |
509 | else { | |
510 | ||
511 | $membershipType = $membershipTypeValues[$row['civicrm_membership_membership_type_id']]; | |
512 | } | |
513 | ||
514 | $interval[$membershipType] = $membershipType; | |
515 | $display[$membershipType] = $row['civicrm_contribution_total_amount_sum']; | |
516 | } | |
517 | else { | |
518 | $graphRows['receive_date'][] = CRM_Utils_Array::value('civicrm_membership_join_date_start', $row); | |
519 | $graphRows[$this->_interval][] = CRM_Utils_Array::value('civicrm_membership_join_date_interval', $row); | |
520 | $graphRows['value'][] = $row['civicrm_contribution_total_amount_sum']; | |
521 | $count++; | |
522 | } | |
523 | } | |
524 | ||
525 | // build chart. | |
526 | if ($isMembershipType) { | |
527 | $graphRows['value'] = $display; | |
528 | $chartInfo = array( | |
b35e00df | 529 | 'legend' => ts('Membership Summary'), |
e60e0c01 | 530 | 'xname' => ts('Member Since / Member Type'), |
531 | 'yname' => ts('Fees'), | |
6a488035 TO |
532 | ); |
533 | CRM_Utils_OpenFlashChart::reportChart($graphRows, $this->_params['charts'], $interval, $chartInfo); | |
534 | } | |
535 | else { | |
536 | CRM_Utils_OpenFlashChart::chart($graphRows, $this->_params['charts'], $this->_interval); | |
537 | } | |
538 | } | |
539 | $this->assign('chartType', $this->_params['charts']); | |
540 | } | |
541 | ||
74cf4551 | 542 | /** |
ced9bfed EM |
543 | * Alter display of rows. |
544 | * | |
545 | * Iterate through the rows retrieved via SQL and make changes for display purposes, | |
546 | * such as rendering contacts as links. | |
547 | * | |
548 | * @param array $rows | |
549 | * Rows generated by SQL, with an array for each row. | |
74cf4551 | 550 | */ |
00be9182 | 551 | public function alterDisplay(&$rows) { |
6a488035 TO |
552 | $entryFound = FALSE; |
553 | foreach ($rows as $rowNum => $row) { | |
554 | // make count columns point to detail report | |
9d72cede EM |
555 | if (!empty($this->_params['group_bys']['join_date']) && |
556 | !empty($row['civicrm_membership_join_date_start']) && | |
6a488035 TO |
557 | $row['civicrm_membership_join_date_start'] && |
558 | $row['civicrm_membership_join_date_subtotal'] | |
559 | ) { | |
560 | ||
561 | $dateStart = CRM_Utils_Date::customFormat($row['civicrm_membership_join_date_start'], '%Y%m%d'); | |
9d72cede EM |
562 | $endDate = new DateTime($dateStart); |
563 | $dateEnd = array(); | |
6a488035 TO |
564 | |
565 | list($dateEnd['Y'], $dateEnd['M'], $dateEnd['d']) = explode(':', $endDate->format('Y:m:d')); | |
566 | ||
567 | switch (strtolower($this->_params['group_bys_freq']['join_date'])) { | |
568 | case 'month': | |
569 | $dateEnd = date("Ymd", mktime(0, 0, 0, $dateEnd['M'] + 1, | |
9d72cede EM |
570 | $dateEnd['d'] - 1, $dateEnd['Y'] |
571 | )); | |
6a488035 TO |
572 | break; |
573 | ||
574 | case 'year': | |
575 | $dateEnd = date("Ymd", mktime(0, 0, 0, $dateEnd['M'], | |
9d72cede EM |
576 | $dateEnd['d'] - 1, $dateEnd['Y'] + 1 |
577 | )); | |
6a488035 TO |
578 | break; |
579 | ||
580 | case 'yearweek': | |
581 | $dateEnd = date("Ymd", mktime(0, 0, 0, $dateEnd['M'], | |
9d72cede EM |
582 | $dateEnd['d'] + 6, $dateEnd['Y'] |
583 | )); | |
6a488035 TO |
584 | break; |
585 | ||
586 | case 'quarter': | |
587 | $dateEnd = date("Ymd", mktime(0, 0, 0, $dateEnd['M'] + 3, | |
9d72cede EM |
588 | $dateEnd['d'] - 1, $dateEnd['Y'] |
589 | )); | |
6a488035 TO |
590 | break; |
591 | } | |
592 | $typeUrl = ''; | |
a7488080 | 593 | if (!empty($this->_params['group_bys']['membership_type_id']) && |
6a488035 TO |
594 | $typeID = $row['civicrm_membership_membership_type_id'] |
595 | ) { | |
596 | $typeUrl = "&tid_op=in&tid_value={$typeID}"; | |
597 | } | |
598 | $statusUrl = ''; | |
599 | if (!empty($this->_params['status_id_value'])) { | |
9d72cede EM |
600 | $statusUrl = "&sid_op=in&sid_value=" . |
601 | implode(",", $this->_params['status_id_value']); | |
6a488035 TO |
602 | } |
603 | $url = CRM_Report_Utils_Report::getNextUrl('member/detail', | |
604 | "reset=1&force=1&join_date_from={$dateStart}&join_date_to={$dateEnd}{$typeUrl}{$statusUrl}", | |
605 | $this->_absoluteUrl, $this->_id, $this->_drilldownReport | |
606 | ); | |
607 | $row['civicrm_membership_join_date_start'] = CRM_Utils_Date::format($row['civicrm_membership_join_date_start']); | |
608 | $rows[$rowNum]['civicrm_membership_join_date_start_link'] = $url; | |
609 | $rows[$rowNum]['civicrm_membership_join_date_start_hover'] = ts("Lists Summary of Memberships for this date unit."); | |
610 | ||
611 | $entryFound = TRUE; | |
612 | } | |
613 | ||
614 | // handle Membership Types | |
615 | if (array_key_exists('civicrm_membership_membership_type_id', $row)) { | |
616 | if ($value = $row['civicrm_membership_membership_type_id']) { | |
617 | $value = explode(',', $value); | |
618 | foreach ($value as $key => $id) { | |
619 | $value[$key] = CRM_Member_PseudoConstant::membershipType($id, FALSE); | |
620 | } | |
621 | $rows[$rowNum]['civicrm_membership_membership_type_id'] = implode(' , ', $value); | |
622 | } | |
623 | $entryFound = TRUE; | |
624 | } | |
625 | ||
626 | // make subtotals look nicer | |
627 | if (array_key_exists('civicrm_membership_join_date_subtotal', $row) && | |
628 | !$row['civicrm_membership_join_date_subtotal'] | |
629 | ) { | |
630 | $this->fixSubTotalDisplay($rows[$rowNum], $this->_statFields); | |
631 | $entryFound = TRUE; | |
632 | } | |
633 | elseif (array_key_exists('civicrm_membership_join_date_subtotal', $row) && | |
634 | $row['civicrm_membership_join_date_subtotal'] && | |
635 | !$row['civicrm_membership_membership_type_id'] | |
636 | ) { | |
637 | $this->fixSubTotalDisplay($rows[$rowNum], $this->_statFields, FALSE); | |
8c2959c8 | 638 | $rows[$rowNum]['civicrm_membership_membership_type_id'] = '<b>' . ts('Subtotal') . '</b>'; |
6a488035 TO |
639 | $entryFound = TRUE; |
640 | } | |
f813f78e | 641 | |
a7dbbc5d CD |
642 | // If using campaigns, convert campaign_id to campaign title |
643 | if (array_key_exists('civicrm_membership_campaign_id', $row)) { | |
644 | if ($value = $row['civicrm_membership_campaign_id']) { | |
645 | $rows[$rowNum]['civicrm_membership_campaign_id'] = $this->activeCampaigns[$value]; | |
646 | } | |
647 | $entryFound = TRUE; | |
648 | } | |
6a488035 TO |
649 | |
650 | // skip looking further in rows, if first row itself doesn't | |
651 | // have the column we need | |
652 | if (!$entryFound) { | |
653 | break; | |
654 | } | |
655 | } | |
656 | } | |
96025800 | 657 | |
6a488035 | 658 | } |