Add case ID and make subject optional for case detail report
[civicrm-core.git] / CRM / Report / Form / Contribute / HouseholdSummary.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Report_Form_Contribute_HouseholdSummary extends CRM_Report_Form {
18
19 public $_drilldownReport = ['contribute/detail' => 'Link to Detail Report'];
20
21 protected $_summary = NULL;
22
23 /**
24 * Class constructor.
25 */
26 public function __construct() {
27 self::validRelationships();
28
29 $this->_columns = [
30 'civicrm_contact_household' => [
31 'dao' => 'CRM_Contact_DAO_Contact',
32 'fields' => [
33 'household_name' => [
34 'title' => ts('Household Name'),
35 'required' => TRUE,
36 ],
37 'id' => [
38 'no_display' => TRUE,
39 'required' => TRUE,
40 ],
41 'contact_type' => [
42 'title' => ts('Contact Type'),
43 ],
44 'contact_sub_type' => [
45 'title' => ts('Contact Subtype'),
46 ],
47 ],
48 'filters' => [
49 'household_name' => [
50 'title' => ts('Household Name'),
51 ],
52 'is_deleted' => [
53 'default' => 0,
54 'type' => CRM_Utils_Type::T_BOOLEAN,
55 ],
56 ],
57 'grouping' => 'household-fields',
58 ],
59 'civicrm_line_item' => [
60 'dao' => 'CRM_Price_DAO_LineItem',
61 ],
62 'civicrm_relationship' => [
63 'dao' => 'CRM_Contact_DAO_Relationship',
64 'fields' => [
65 'relationship_type_id' => [
66 'title' => ts('Relationship Type'),
67 ],
68 ],
69 'filters' => [
70 'relationship_type_id' => [
71 'title' => ts('Relationship Type'),
72 'type' => CRM_Utils_Type::T_INT,
73 'operatorType' => CRM_Report_Form::OP_SELECT,
74 'options' => $this->relationTypes,
75 'default' => key($this->relationTypes),
76 ],
77 ],
78 'grouping' => 'household-fields',
79 ],
80 'civicrm_contact' => [
81 'dao' => 'CRM_Contact_DAO_Contact',
82 'fields' => [
83 'sort_name' => [
84 'title' => ts('Contact Name'),
85 'required' => TRUE,
86 ],
87 'id' => [
88 'no_display' => TRUE,
89 'required' => TRUE,
90 ],
91 ],
92 'grouping' => 'contact-fields',
93 ],
94 'civicrm_contribution' => [
95 'dao' => 'CRM_Contribute_DAO_Contribution',
96 'fields' => [
97 'total_amount' => [
98 'title' => ts('Amount'),
99 'required' => TRUE,
100 ],
101 'id' => [
102 'no_display' => TRUE,
103 'required' => TRUE,
104 ],
105 'contribution_status_id' => [
106 'title' => ts('Contribution Status'),
107 'default' => TRUE,
108 ],
109 'check_number' => [
110 'title' => ts('Check Number'),
111 ],
112 'currency' => [
113 'required' => TRUE,
114 'no_display' => TRUE,
115 ],
116 'financial_type_id' => [
117 'title' => ts('Financial Type'),
118 ],
119 'trxn_id' => NULL,
120 'receive_date' => ['default' => TRUE],
121 'receipt_date' => NULL,
122 ],
123 'filters' => [
124 'receive_date' => ['operatorType' => CRM_Report_Form::OP_DATE],
125 'receipt_date' => ['operatorType' => CRM_Report_Form::OP_DATE],
126 'total_amount' => ['title' => ts('Amount Between')],
127 'currency' => [
128 'title' => ts('Currency'),
129 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
130 'options' => CRM_Core_OptionGroup::values('currencies_enabled'),
131 'default' => NULL,
132 'type' => CRM_Utils_Type::T_STRING,
133 ],
134 'contribution_status_id' => [
135 'title' => ts('Contribution Status'),
136 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
137 'options' => CRM_Contribute_BAO_Contribution::buildOptions('contribution_status_id', 'search'),
138 'default' => [1],
139 ],
140 'financial_type_id' => [
141 'title' => ts('Financial Type'),
142 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
143 'options' => CRM_Contribute_PseudoConstant::financialType(),
144 ],
145 ],
146 'grouping' => 'contri-fields',
147 ],
148 'civicrm_financial_trxn' => [
149 'dao' => 'CRM_Financial_DAO_FinancialTrxn',
150 'fields' => [
151 'card_type_id' => [
152 'title' => ts('Credit Card Type'),
153 'dbAlias' => 'GROUP_CONCAT(financial_trxn_civireport.card_type_id SEPARATOR ",")',
154 ],
155 ],
156 'filters' => [
157 'card_type_id' => [
158 'title' => ts('Credit Card Type'),
159 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
160 'options' => CRM_Financial_DAO_FinancialTrxn::buildOptions('card_type_id'),
161 'default' => NULL,
162 'type' => CRM_Utils_Type::T_STRING,
163 ],
164 ],
165 ],
166 'civicrm_address' => [
167 'dao' => 'CRM_Core_DAO_Address',
168 'fields' => [
169 'street_address' => NULL,
170 'city' => NULL,
171 'postal_code' => NULL,
172 'state_province_id' => [
173 'title' => ts('State/Province'),
174 ],
175 'country_id' => [
176 'title' => ts('Country'),
177 ],
178 ],
179 'grouping' => 'contact-fields',
180 ],
181 'civicrm_email' => [
182 'dao' => 'CRM_Core_DAO_Email',
183 'fields' => [
184 'email' => NULL,
185 ],
186 'filters' => [
187 'on_hold' => [
188 'title' => ts('On Hold'),
189 'type' => CRM_Utils_Type::T_INT,
190 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
191 'options' => ['' => ts('Any')] + CRM_Core_PseudoConstant::emailOnHoldOptions(),
192 ],
193 ],
194 'grouping' => 'contact-fields',
195 ],
196 ];
197
198 // If we have a campaign, build out the relevant elements
199 $this->addCampaignFields('civicrm_contribution');
200
201 $this->_currencyColumn = 'civicrm_contribution_currency';
202 parent::__construct();
203 }
204
205 public function select() {
206 // @todo remove this & use parent select.
207 $this->_columnHeaders = $select = [];
208 foreach ($this->_columns as $tableName => $table) {
209 if (array_key_exists('fields', $table)) {
210 foreach ($table['fields'] as $fieldName => $field) {
211 if (!empty($field['required']) ||
212 !empty($this->_params['fields'][$fieldName])
213 ) {
214
215 if (!empty($field['statistics'])) {
216 foreach ($field['statistics'] as $stat => $label) {
217 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}_{$stat}";
218 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
219 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
220 $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}";
221 }
222 }
223 else {
224 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
225
226 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title'] ?? NULL;
227 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = $field['type'] ?? NULL;
228 }
229 }
230 }
231 }
232 }
233 $this->_selectClauses = $select;
234 $this->_select = "SELECT " . implode(', ', $select) . " ";
235 }
236
237 public function from() {
238 $this->_from = "
239 FROM civicrm_relationship {$this->_aliases['civicrm_relationship']}
240 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact_household']} ON
241 ({$this->_aliases['civicrm_contact_household']}.id = {$this->_aliases['civicrm_relationship']}.$this->householdContact AND {$this->_aliases['civicrm_contact_household']}.contact_type='Household')
242 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']} ON
243 ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_relationship']}.$this->otherContact )
244 {$this->_aclFrom}
245 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']} ON
246 ({$this->_aliases['civicrm_contribution']}.contact_id = {$this->_aliases['civicrm_relationship']}.$this->otherContact ) AND {$this->_aliases['civicrm_contribution']}.is_test = 0 AND {$this->_aliases['civicrm_contribution']}.is_template = 0 ";
247
248 $this->joinAddressFromContact();
249 $this->joinEmailFromContact();
250
251 // for credit card type
252 $this->addFinancialTrxnFromClause();
253 }
254
255 public function where() {
256 $clauses = [];
257 foreach ($this->_columns as $tableName => $table) {
258 if (array_key_exists('filters', $table)) {
259 foreach ($table['filters'] as $fieldName => $field) {
260 $clause = NULL;
261 if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) {
262 $relative = $this->_params["{$fieldName}_relative"] ?? NULL;
263 $from = $this->_params["{$fieldName}_from"] ?? NULL;
264 $to = $this->_params["{$fieldName}_to"] ?? NULL;
265 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
266 }
267 else {
268 $op = $this->_params["{$fieldName}_op"] ?? NULL;
269 if ($op) {
270 if ($fieldName == 'relationship_type_id') {
271 $clause = "{$this->_aliases['civicrm_relationship']}.relationship_type_id=" . $this->relationshipId;
272 }
273 else {
274 $clause = $this->whereClause($field,
275 $op,
276 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
277 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
278 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
279 );
280 }
281 }
282 }
283
284 if (!empty($clause)) {
285 $clauses[] = $clause;
286 }
287 }
288 }
289 }
290
291 if (empty($clauses)) {
292 $this->_where = "WHERE ( 1 )";
293 }
294 else {
295 $this->_where = "WHERE " . implode(' AND ', $clauses);
296 }
297
298 if ($this->_aclWhere) {
299 $this->_where .= " AND {$this->_aclWhere} ";
300 }
301 }
302
303 public function groupBy() {
304 $groupBy = [
305 "{$this->_aliases['civicrm_relationship']}.$this->householdContact",
306 "{$this->_aliases['civicrm_relationship']}.$this->otherContact",
307 "{$this->_aliases['civicrm_contribution']}.id",
308 "{$this->_aliases['civicrm_relationship']}.relationship_type_id",
309 ];
310 $this->_groupBy = CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, $groupBy);
311 }
312
313 public function orderBy() {
314 $this->_orderBy = " ORDER BY {$this->_aliases['civicrm_contact_household']}.household_name, {$this->_aliases['civicrm_relationship']}.$this->householdContact, {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_relationship']}.$this->otherContact";
315 }
316
317 /**
318 * @param array $rows
319 *
320 * @return array
321 */
322 public function statistics(&$rows) {
323 $statistics = parent::statistics($rows);
324
325 //hack filter display for relationship type
326 $type = substr($this->_params['relationship_type_id_value'], -3);
327 foreach ($statistics['filters'] as $id => $value) {
328 if (
329 $value['title'] == 'Relationship Type' &&
330 isset($this->relationTypes[$this->relationshipId . '_' . $type])
331 ) {
332 $statistics['filters'][$id]['value'] = 'Is equal to ' .
333 $this->relationTypes[$this->relationshipId . '_' . $type];
334 }
335 }
336 return $statistics;
337 }
338
339 public function postProcess() {
340
341 $this->beginPostProcess();
342 $this->buildACLClause([
343 $this->_aliases['civicrm_contact'],
344 $this->_aliases['civicrm_contact_household'],
345 ]);
346 $sql = $this->buildQuery(TRUE);
347 $rows = [];
348
349 $this->buildRows($sql, $rows);
350 $this->formatDisplay($rows);
351 $this->doTemplateAssignment($rows);
352 $this->endPostProcess($rows);
353 }
354
355 /**
356 * Set variables to be accessed by API and form layer in processing.
357 */
358 public function beginPostProcessCommon() {
359 $getRelationship = $this->_params['relationship_type_id_value'];
360 $type = substr($getRelationship, -3);
361 $this->relationshipId = intval((substr($getRelationship, 0, strpos($getRelationship, '_'))));
362 if ($type == 'b_a') {
363 $this->householdContact = 'contact_id_b';
364 $this->otherContact = 'contact_id_a';
365 }
366 else {
367 $this->householdContact = 'contact_id_a';
368 $this->otherContact = 'contact_id_b';
369 }
370 }
371
372 public function validRelationships() {
373 $this->relationTypes = $relationTypes = [];
374
375 $params = ['contact_type_b' => 'Household', 'version' => 3];
376 $typesA = civicrm_api('relationship_type', 'get', $params);
377 if (empty($typesA['is_error'])) {
378 foreach ($typesA['values'] as $rel) {
379 $relationTypes[$rel['id']][$rel['id'] . '_b_a'] = $rel['label_b_a'];
380 }
381 }
382 $params = ['contact_type_a' => 'Household', 'version' => 3];
383 $typesB = civicrm_api('relationship_type', 'get', $params);
384 if (empty($typesB['is_error'])) {
385 foreach ($typesB['values'] as $rel) {
386 $relationTypes[$rel['id']][$rel['id'] . '_a_b'] = $rel['label_a_b'];
387 //$this->relationTypes[$rel['id'].'_a_b'] = $rel['label_a_b'];
388 }
389 }
390
391 ksort($relationTypes);
392 foreach ($relationTypes as $relationship) {
393 foreach ($relationship as $index => $label) {
394 $this->relationTypes[$index] = $label;
395 }
396 }
397 }
398
399 /**
400 * Alter display of rows.
401 *
402 * Iterate through the rows retrieved via SQL and make changes for display purposes,
403 * such as rendering contacts as links.
404 *
405 * @param array $rows
406 * Rows generated by SQL, with an array for each row.
407 */
408 public function alterDisplay(&$rows) {
409 $type = substr($this->_params['relationship_type_id_value'], -3);
410
411 $entryFound = FALSE;
412 $flagHousehold = $flagContact = 0;
413 foreach ($rows as $rowNum => $row) {
414
415 //replace retionship id by relationship name
416 if (array_key_exists('civicrm_relationship_relationship_type_id', $row)) {
417 if ($value = $row['civicrm_relationship_relationship_type_id']) {
418 $rows[$rowNum]['civicrm_relationship_relationship_type_id'] = $this->relationTypes[$value . '_' . $type];
419 $entryFound = TRUE;
420 }
421 }
422
423 //remove duplicate Organization names
424 if (array_key_exists('civicrm_contact_household_household_name', $row) &&
425 $this->_outputMode != 'csv'
426 ) {
427 if ($value = $row['civicrm_contact_household_household_name']) {
428 if ($rowNum == 0) {
429 $priviousHousehold = $value;
430 }
431 else {
432 if ($priviousHousehold == $value) {
433 $flagHousehold = 1;
434 $priviousHousehold = $value;
435 }
436 else {
437 $flagHousehold = 0;
438 $priviousHousehold = $value;
439 }
440 }
441
442 if ($flagHousehold == 1) {
443 $rows[$rowNum]['civicrm_contact_household_household_name'] = "";
444 }
445 else {
446 $url = CRM_Utils_System::url('civicrm/contact/view',
447 'reset=1&cid=' . $rows[$rowNum]['civicrm_contact_household_id'],
448 $this->_absoluteUrl
449 );
450
451 $rows[$rowNum]['civicrm_contact_household_household_name'] = "<a href='$url' title='" . ts('View contact summary for this househould') . "'>" . $value . '</a>';
452 }
453 $entryFound = TRUE;
454 }
455 }
456
457 //remove duplicate Contact names and relationship type
458 if (array_key_exists('civicrm_contact_id', $row) &&
459 $this->_outputMode != 'csv'
460 ) {
461 if ($value = $row['civicrm_contact_id']) {
462 if ($rowNum == 0) {
463 $priviousContact = $value;
464 }
465 else {
466 if ($priviousContact == $value) {
467 $flagContact = 1;
468 $priviousContact = $value;
469 }
470 else {
471 $flagContact = 0;
472 $priviousContact = $value;
473 }
474 }
475
476 if ($flagContact == 1 && $flagHousehold == 1) {
477 $rows[$rowNum]['civicrm_contact_sort_name'] = "";
478 $rows[$rowNum]['civicrm_relationship_relationship_type_id'] = "";
479 }
480
481 $entryFound = TRUE;
482 }
483 }
484
485 if (array_key_exists('civicrm_contribution_contribution_status_id', $row)) {
486 if ($value = $row['civicrm_contribution_contribution_status_id']) {
487 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = CRM_Contribute_PseudoConstant::contributionStatus($value, 'label');
488 }
489 }
490
491 if (array_key_exists('civicrm_contribution_financial_type_id', $row)) {
492 if ($value = $row['civicrm_contribution_financial_type_id']) {
493 $rows[$rowNum]['civicrm_contribution_financial_type_id'] = CRM_Contribute_PseudoConstant::financialType($value);
494 }
495 }
496
497 // convert display name to links
498 if (array_key_exists('civicrm_contact_sort_name', $row) &&
499 $rows[$rowNum]['civicrm_contact_sort_name'] &&
500 array_key_exists('civicrm_contact_id', $row)
501 ) {
502 $url = CRM_Report_Utils_Report::getNextUrl('contribute/detail',
503 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
504 $this->_absoluteUrl, $this->_id, $this->_drilldownReport
505 );
506 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
507 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts('View contribution details for this individual');
508
509 $entryFound = TRUE;
510 }
511
512 if (!empty($row['civicrm_contribution_total_amount'])) {
513 $row['civicrm_contribution_total_amount'] = CRM_Utils_Money::format($row['civicrm_contribution_total_amount'], $row['civicrm_contribution_currency']);
514 }
515
516 if (!empty($row['civicrm_financial_trxn_card_type_id'])) {
517 $rows[$rowNum]['civicrm_financial_trxn_card_type_id'] = $this->getLabels($row['civicrm_financial_trxn_card_type_id'], 'CRM_Financial_DAO_FinancialTrxn', 'card_type_id');
518 $entryFound = TRUE;
519 }
520
521 // Contribution amount links to view contribution
522 if (($value = CRM_Utils_Array::value('civicrm_contribution_total_amount', $row)) &&
523 CRM_Core_Permission::check('access CiviContribute')
524 ) {
525 $url = CRM_Utils_System::url("civicrm/contact/view/contribution",
526 "reset=1&id=" . $row['civicrm_contribution_id'] . "&cid=" .
527 $row['civicrm_contact_id'] .
528 "&action=view&context=contribution&selectedChild=contribute",
529 $this->_absoluteUrl
530 );
531 $rows[$rowNum]['civicrm_contribution_total_amount_link'] = $url;
532 $rows[$rowNum]['civicrm_contribution_total_amount_hover'] = ts("View this contribution.");
533 $entryFound = TRUE;
534 }
535
536 // convert campaign_id to campaign title
537 if (array_key_exists('civicrm_contribution_campaign_id', $row)) {
538 if ($value = $row['civicrm_contribution_campaign_id']) {
539 $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->campaigns[$value];
540 $entryFound = TRUE;
541 }
542 }
543
544 $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, NULL, NULL) ? TRUE : $entryFound;
545
546 // skip looking further in rows, if first row itself doesn't
547 if (!$entryFound) {
548 break;
549 }
550 $lastKey = $rowNum;
551 }
552 }
553
554 }