Merge pull request #15524 from seamuslee001/5_18_3_release_notes
[civicrm-core.git] / CRM / Report / Form / Member / Detail.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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
31 * @copyright CiviCRM LLC (c) 2004-2019
32 */
33 class CRM_Report_Form_Member_Detail extends CRM_Report_Form {
34
35 protected $_summary = NULL;
36
37 protected $_customGroupExtends = [
38 'Membership',
39 'Contribution',
40 'Contact',
41 'Individual',
42 'Household',
43 'Organization',
44 ];
45
46 protected $_customGroupGroupBy = FALSE;
47
48 /**
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
58 */
59 protected $groupFilterNotOptimised = FALSE;
60
61 /**
62 * Class constructor.
63 */
64 public function __construct() {
65 $this->_columns = [
66 'civicrm_contact' => [
67 'dao' => 'CRM_Contact_DAO_Contact',
68 'fields' => $this->getBasicContactFields(),
69 'filters' => [
70 'sort_name' => [
71 'title' => ts('Contact Name'),
72 'operator' => 'like',
73 ],
74 'is_deleted' => [
75 'title' => ts('Is Deleted'),
76 'default' => 0,
77 'type' => CRM_Utils_Type::T_BOOLEAN,
78 ],
79 'id' => ['no_display' => TRUE],
80 ],
81 'order_bys' => [
82 'sort_name' => [
83 'title' => ts('Last Name, First Name'),
84 'default' => '1',
85 'default_weight' => '0',
86 'default_order' => 'ASC',
87 ],
88 ],
89 'grouping' => 'contact-fields',
90 ],
91 'civicrm_membership' => [
92 'dao' => 'CRM_Member_DAO_Membership',
93 'fields' => [
94 'membership_type_id' => [
95 'title' => ts('Membership Type'),
96 'required' => TRUE,
97 'no_repeat' => TRUE,
98 ],
99 'membership_start_date' => [
100 'title' => ts('Start Date'),
101 'default' => TRUE,
102 ],
103 'membership_end_date' => [
104 'title' => ts('End Date'),
105 'default' => TRUE,
106 ],
107 'owner_membership_id' => [
108 'title' => ts('Primary/Inherited?'),
109 'default' => TRUE,
110 ],
111 'join_date' => [
112 'title' => ts('Join Date'),
113 'default' => TRUE,
114 ],
115 'source' => ['title' => ts('Source')],
116 ],
117 'filters' => [
118 'membership_join_date' => ['operatorType' => CRM_Report_Form::OP_DATE],
119 'membership_start_date' => ['operatorType' => CRM_Report_Form::OP_DATE],
120 'membership_end_date' => ['operatorType' => CRM_Report_Form::OP_DATE],
121 'owner_membership_id' => [
122 'title' => ts('Primary Membership'),
123 'operatorType' => CRM_Report_Form::OP_INT,
124 ],
125 'tid' => [
126 'name' => 'membership_type_id',
127 'title' => ts('Membership Types'),
128 'type' => CRM_Utils_Type::T_INT,
129 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
130 'options' => CRM_Member_PseudoConstant::membershipType(),
131 ],
132 ],
133 'order_bys' => [
134 'membership_type_id' => [
135 'title' => ts('Membership Type'),
136 'default' => '0',
137 'default_weight' => '1',
138 'default_order' => 'ASC',
139 ],
140 ],
141 'grouping' => 'member-fields',
142 'group_bys' => [
143 'id' => [
144 'title' => ts('Membership'),
145 'default' => TRUE,
146 ],
147 ],
148 ],
149 'civicrm_membership_status' => [
150 'dao' => 'CRM_Member_DAO_MembershipStatus',
151 'alias' => 'mem_status',
152 'fields' => [
153 'name' => [
154 'title' => ts('Status'),
155 'default' => TRUE,
156 ],
157 ],
158 'filters' => [
159 'sid' => [
160 'name' => 'id',
161 'title' => ts('Status'),
162 'type' => CRM_Utils_Type::T_INT,
163 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
164 'options' => CRM_Member_PseudoConstant::membershipStatus(NULL, NULL, 'label'),
165 ],
166 ],
167 'grouping' => 'member-fields',
168 ],
169 'civicrm_email' => [
170 'dao' => 'CRM_Core_DAO_Email',
171 'fields' => ['email' => NULL],
172 'grouping' => 'contact-fields',
173 ],
174 'civicrm_phone' => [
175 'dao' => 'CRM_Core_DAO_Phone',
176 'fields' => ['phone' => NULL],
177 'grouping' => 'contact-fields',
178 ],
179 'civicrm_contribution' => [
180 'dao' => 'CRM_Contribute_DAO_Contribution',
181 'fields' => [
182 'contribution_id' => [
183 'name' => 'id',
184 'no_display' => TRUE,
185 'required' => TRUE,
186 ],
187 'financial_type_id' => ['title' => ts('Financial Type')],
188 'contribution_status_id' => ['title' => ts('Contribution Status')],
189 'payment_instrument_id' => ['title' => ts('Payment Type')],
190 'currency' => [
191 'required' => TRUE,
192 'no_display' => TRUE,
193 ],
194 'trxn_id' => NULL,
195 'receive_date' => NULL,
196 'receipt_date' => NULL,
197 'fee_amount' => NULL,
198 'net_amount' => NULL,
199 'total_amount' => [
200 'title' => ts('Payment Amount (most recent)'),
201 'statistics' => ['sum' => ts('Amount')],
202 ],
203 ],
204 'filters' => [
205 'receive_date' => ['operatorType' => CRM_Report_Form::OP_DATE],
206 'financial_type_id' => [
207 'title' => ts('Financial Type'),
208 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
209 'options' => CRM_Contribute_PseudoConstant::financialType(),
210 'type' => CRM_Utils_Type::T_INT,
211 ],
212 'payment_instrument_id' => [
213 'title' => ts('Payment Type'),
214 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
215 'options' => CRM_Contribute_PseudoConstant::paymentInstrument(),
216 'type' => CRM_Utils_Type::T_INT,
217 ],
218 'currency' => [
219 'title' => ts('Currency'),
220 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
221 'options' => CRM_Core_OptionGroup::values('currencies_enabled'),
222 'default' => NULL,
223 'type' => CRM_Utils_Type::T_STRING,
224 ],
225 'contribution_status_id' => [
226 'title' => ts('Contribution Status'),
227 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
228 'options' => CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'label'),
229 'type' => CRM_Utils_Type::T_INT,
230 ],
231 'total_amount' => ['title' => ts('Contribution Amount')],
232 ],
233 'order_bys' => [
234 'receive_date' => [
235 'title' => ts('Date Received'),
236 'default_weight' => '2',
237 'default_order' => 'DESC',
238 ],
239 ],
240 'grouping' => 'contri-fields',
241 ],
242 ] + $this->getAddressColumns([
243 // These options are only excluded because they were not previously present.
244 'order_by' => FALSE,
245 'group_by' => FALSE,
246 ]);
247 $this->_groupFilter = TRUE;
248 $this->_tagFilter = TRUE;
249
250 // If we have campaigns enabled, add those elements to both the fields, filters and sorting
251 $this->addCampaignFields('civicrm_membership', FALSE, TRUE);
252
253 $this->_currencyColumn = 'civicrm_contribution_currency';
254 parent::__construct();
255 }
256
257 public function preProcess() {
258 $this->assign('reportTitle', ts('Membership Detail Report'));
259 parent::preProcess();
260 }
261
262 public function from() {
263 $this->setFromBase('civicrm_contact');
264 $this->_from .= "
265 {$this->_aclFrom}
266 INNER JOIN civicrm_membership {$this->_aliases['civicrm_membership']}
267 ON {$this->_aliases['civicrm_contact']}.id =
268 {$this->_aliases['civicrm_membership']}.contact_id AND {$this->_aliases['civicrm_membership']}.is_test = 0
269 LEFT JOIN civicrm_membership_status {$this->_aliases['civicrm_membership_status']}
270 ON {$this->_aliases['civicrm_membership_status']}.id =
271 {$this->_aliases['civicrm_membership']}.status_id ";
272
273 $this->joinAddressFromContact();
274 $this->joinPhoneFromContact();
275 $this->joinEmailFromContact();
276
277 //used when contribution field is selected.
278 if ($this->isTableSelected('civicrm_contribution')) {
279 $this->_from .= "
280 LEFT JOIN civicrm_membership_payment cmp
281 ON {$this->_aliases['civicrm_membership']}.id = cmp.membership_id
282 LEFT JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
283 ON cmp.contribution_id={$this->_aliases['civicrm_contribution']}.id\n";
284 }
285 }
286
287 public function getOperationPair($type = "string", $fieldName = NULL) {
288 //re-name IS NULL/IS NOT NULL for clarity
289 if ($fieldName == 'owner_membership_id') {
290 $result = [];
291 $result['nll'] = ts('Primary members only');
292 $result['nnll'] = ts('Non-primary members only');
293 $options = parent::getOperationPair($type, $fieldName);
294 foreach ($options as $key => $label) {
295 if (!array_key_exists($key, $result)) {
296 $result[$key] = $label;
297 }
298 }
299 }
300 else {
301 $result = parent::getOperationPair($type, $fieldName);
302 }
303 return $result;
304 }
305
306 /**
307 * Alter display of rows.
308 *
309 * Iterate through the rows retrieved via SQL and make changes for display purposes,
310 * such as rendering contacts as links.
311 *
312 * @param array $rows
313 * Rows generated by SQL, with an array for each row.
314 */
315 public function alterDisplay(&$rows) {
316 $entryFound = FALSE;
317 $checkList = [];
318
319 $contributionTypes = CRM_Contribute_PseudoConstant::financialType();
320 $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'label');
321 $paymentInstruments = CRM_Contribute_PseudoConstant::paymentInstrument();
322
323 $repeatFound = FALSE;
324 foreach ($rows as $rowNum => $row) {
325 if ($repeatFound == FALSE ||
326 $repeatFound < $rowNum - 1
327 ) {
328 unset($checkList);
329 $checkList = [];
330 }
331 if (!empty($this->_noRepeats) && $this->_outputMode != 'csv') {
332 // not repeat contact display names if it matches with the one
333 // in previous row
334 foreach ($row as $colName => $colVal) {
335 if (in_array($colName, $this->_noRepeats) &&
336 $rowNum > 0
337 ) {
338 if ($rows[$rowNum][$colName] == $rows[$rowNum - 1][$colName] ||
339 (!empty($checkList[$colName]) &&
340 in_array($colVal, $checkList[$colName]))
341 ) {
342 $rows[$rowNum][$colName] = "";
343 // CRM-15917: Don't blank the name if it's a different contact
344 if ($colName == 'civicrm_contact_exposed_id') {
345 $rows[$rowNum]['civicrm_contact_sort_name'] = "";
346 }
347 $repeatFound = $rowNum;
348 }
349 }
350 if (in_array($colName, $this->_noRepeats)) {
351 $checkList[$colName][] = $colVal;
352 }
353 }
354 }
355
356 if (array_key_exists('civicrm_membership_membership_type_id', $row)) {
357 if ($value = $row['civicrm_membership_membership_type_id']) {
358 $rows[$rowNum]['civicrm_membership_membership_type_id'] = CRM_Member_PseudoConstant::membershipType($value, FALSE);
359 }
360 $entryFound = TRUE;
361 }
362
363 if (array_key_exists('civicrm_contact_sort_name', $row) &&
364 $rows[$rowNum]['civicrm_contact_sort_name'] &&
365 array_key_exists('civicrm_contact_id', $row)
366 ) {
367 $url = CRM_Utils_System::url("civicrm/contact/view",
368 'reset=1&cid=' . $row['civicrm_contact_id'],
369 $this->_absoluteUrl
370 );
371 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
372 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact Summary for this Contact.");
373 $entryFound = TRUE;
374 }
375
376 if ($value = CRM_Utils_Array::value('civicrm_contribution_financial_type_id', $row)) {
377 $rows[$rowNum]['civicrm_contribution_financial_type_id'] = $contributionTypes[$value];
378 $entryFound = TRUE;
379 }
380 if ($value = CRM_Utils_Array::value('civicrm_contribution_contribution_status_id', $row)) {
381 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = $contributionStatus[$value];
382 $entryFound = TRUE;
383 }
384 if ($value = CRM_Utils_Array::value('civicrm_contribution_payment_instrument_id', $row)) {
385 $rows[$rowNum]['civicrm_contribution_payment_instrument_id'] = $paymentInstruments[$value];
386 $entryFound = TRUE;
387 }
388
389 if (array_key_exists('civicrm_membership_owner_membership_id', $row)) {
390 $value = $row['civicrm_membership_owner_membership_id'];
391 $rows[$rowNum]['civicrm_membership_owner_membership_id'] = ($value != '') ? 'Inherited' : 'Primary';
392 $entryFound = TRUE;
393 }
394
395 // Convert campaign_id to campaign title
396 if (array_key_exists('civicrm_membership_campaign_id', $row)) {
397 if ($value = $row['civicrm_membership_campaign_id']) {
398 $rows[$rowNum]['civicrm_membership_campaign_id'] = $this->campaigns[$value];
399 $entryFound = TRUE;
400 }
401 }
402 $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'member/detail', 'List all memberships(s) for this ') ? TRUE : $entryFound;
403 $entryFound = $this->alterDisplayContactFields($row, $rows, $rowNum, 'member/detail', 'List all memberships(s) for this ') ? TRUE : $entryFound;
404
405 if (!$entryFound) {
406 break;
407 }
408 }
409 }
410
411 }