Cleanup reverse boolean expressions
[civicrm-core.git] / CRM / Report / Form / Grant / Statistics.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_Grant_Statistics extends CRM_Report_Form {
18
19 protected $_customGroupExtends = ['Grant'];
20
21 protected $_add2groupSupported = FALSE;
22
23 /**
24 * Class constructor.
25 */
26 public function __construct() {
27 $this->_columns = [
28 'civicrm_grant' => [
29 'dao' => 'CRM_Grant_DAO_Grant',
30 'fields' => [
31 'summary_statistics' => [
32 'name' => 'id',
33 'title' => ts('Summary Statistics'),
34 'required' => TRUE,
35 ],
36 'grant_type_id' => [
37 'name' => 'grant_type_id',
38 'title' => ts('By Grant Type'),
39 ],
40 'status_id' => [
41 'no_display' => TRUE,
42 'required' => TRUE,
43 ],
44 'amount_total' => [
45 'no_display' => TRUE,
46 'required' => TRUE,
47 ],
48 'grant_report_received' => [
49 'no_display' => TRUE,
50 'required' => TRUE,
51 ],
52 'currency' => [
53 'no_display' => TRUE,
54 'required' => TRUE,
55 ],
56 ],
57 'filters' => [
58 'application_received_date' => [
59 'name' => 'application_received_date',
60 'title' => ts('Application Received'),
61 'operatorType' => CRM_Report_Form::OP_DATE,
62 'type' => CRM_Utils_Type::T_DATE,
63 ],
64 'decision_date' => [
65 'name' => 'decision_date',
66 'title' => ts('Grant Decision'),
67 'operatorType' => CRM_Report_Form::OP_DATE,
68 'type' => CRM_Utils_Type::T_DATE,
69 ],
70 'money_transfer_date' => [
71 'name' => 'money_transfer_date',
72 'title' => ts('Money Transferred'),
73 'operatorType' => CRM_Report_Form::OP_DATE,
74 'type' => CRM_Utils_Type::T_DATE,
75 ],
76 'grant_due_date' => [
77 'name' => 'grant_due_date',
78 'title' => ts('Grant Report Due'),
79 'operatorType' => CRM_Report_Form::OP_DATE,
80 'type' => CRM_Utils_Type::T_DATE,
81 ],
82 'grant_type' => [
83 'name' => 'grant_type_id',
84 'title' => ts('Grant Type'),
85 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
86 'options' => CRM_Core_PseudoConstant::get('CRM_Grant_DAO_Grant', 'grant_type_id'),
87 ],
88 'status_id' => [
89 'name' => 'status_id',
90 'title' => ts('Grant Status'),
91 'type' => CRM_Utils_Type::T_INT,
92 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
93 'options' => CRM_Core_PseudoConstant::get('CRM_Grant_DAO_Grant', 'status_id'),
94 ],
95 'amount_requested' => [
96 'name' => 'amount_requested',
97 'title' => ts('Amount Requested'),
98 'type' => CRM_Utils_Type::T_MONEY,
99 ],
100 'amount_granted' => [
101 'name' => 'amount_granted',
102 'title' => ts('Amount Granted'),
103 ],
104 'grant_report_received' => [
105 'name' => 'grant_report_received',
106 'title' => ts('Report Received'),
107 'operatorType' => CRM_Report_Form::OP_SELECT,
108 'options' => [
109 '' => ts('- select -'),
110 0 => ts('No'),
111 1 => ts('Yes'),
112 ],
113 ],
114 ],
115 ],
116 'civicrm_contact' => [
117 'dao' => 'CRM_Contact_DAO_Contact',
118 'fields' => [
119 'id' => [
120 'required' => TRUE,
121 'no_display' => TRUE,
122 ],
123 'gender_id' => [
124 'name' => 'gender_id',
125 'title' => ts('By Gender'),
126 ],
127 'contact_type' => [
128 'name' => 'contact_type',
129 'title' => ts('By Contact Type'),
130 ],
131 ],
132 'filters' => [
133 'gender_id' => [
134 'name' => 'gender_id',
135 'title' => ts('Gender'),
136 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
137 'options' => CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'),
138 ],
139 'contact_type' => [
140 'name' => 'contact_type',
141 'title' => ts('Contact Type'),
142 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
143 'options' => CRM_Contact_BAO_ContactType::basicTypePairs(),
144 ],
145 ],
146 'grouping' => 'contact-fields',
147 ],
148 'civicrm_worldregion' => [
149 'dao' => 'CRM_Core_DAO_Worldregion',
150 'fields' => [
151 'id' => [
152 'no_display' => TRUE,
153 ],
154 'name' => [
155 'name' => 'name',
156 'title' => ts('By World Region'),
157 ],
158 ],
159 'filters' => [
160 'region_id' => [
161 'name' => 'id',
162 'title' => ts('World Region'),
163 'type' => CRM_Utils_Type::T_INT,
164 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
165 'options' => CRM_Core_PseudoConstant::worldRegion(),
166 ],
167 ],
168 ],
169 'civicrm_address' => [
170 'dao' => 'CRM_Core_DAO_Address',
171 'fields' => [
172 'country_id' => [
173 'name' => 'country_id',
174 'title' => ts('By Country'),
175 ],
176 ],
177 'filters' => [
178 'country_id' => [
179 'title' => ts('Country'),
180 'type' => CRM_Utils_Type::T_INT,
181 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
182 'options' => CRM_Core_PseudoConstant::country(),
183 ],
184 ],
185 ],
186 ];
187 parent::__construct();
188 }
189
190 public function select() {
191 $select = [];
192
193 $this->_columnHeaders = [];
194 foreach ($this->_columns as $tableName => $table) {
195 if (array_key_exists('fields', $table)) {
196 foreach ($table['fields'] as $fieldName => $field) {
197 if (!empty($field['required']) ||
198 !empty($this->_params['fields'][$fieldName])
199 ) {
200
201 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
202
203 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title'] ?? NULL;
204 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = $field['type'] ?? NULL;
205 }
206 }
207 }
208 }
209 $this->_selectClauses = $select;
210
211 $this->_select = "SELECT " . implode(', ', $select) . " ";
212 }
213
214 public function from() {
215 $this->_from = "
216 FROM civicrm_grant {$this->_aliases['civicrm_grant']}
217 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
218 ON ({$this->_aliases['civicrm_grant']}.contact_id = {$this->_aliases['civicrm_contact']}.id ) ";
219
220 $this->joinAddressFromContact();
221 $this->joinCountryFromAddress();
222 if ($this->isTableSelected('civicrm_worldregion')) {
223 $this->_from .= "
224 LEFT JOIN civicrm_worldregion {$this->_aliases['civicrm_worldregion']}
225 ON {$this->_aliases['civicrm_country']}.region_id =
226 {$this->_aliases['civicrm_worldregion']}.id";
227 }
228 }
229
230 public function where() {
231 $whereClause = "
232 WHERE {$this->_aliases['civicrm_grant']}.amount_total IS NOT NULL
233 AND {$this->_aliases['civicrm_grant']}.amount_total > 0";
234 $this->_where = $whereClause;
235
236 foreach ($this->_columns as $tableName => $table) {
237 if (array_key_exists('filters', $table)) {
238 foreach ($table['filters'] as $fieldName => $field) {
239
240 $clause = NULL;
241 if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) {
242 $relative = $this->_params["{$fieldName}_relative"] ?? NULL;
243 $from = $this->_params["{$fieldName}_from"] ?? NULL;
244 $to = $this->_params["{$fieldName}_to"] ?? NULL;
245
246 if ($relative || $from || $to) {
247 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
248 }
249 }
250 else {
251 $op = $this->_params["{$fieldName}_op"] ?? NULL;
252 if (($fieldName == 'grant_report_received') &&
253 (CRM_Utils_Array::value("{$fieldName}_value", $this->_params) ===
254 0)
255 ) {
256 $op = 'nll';
257 $this->_params["{$fieldName}_value"] = NULL;
258 }
259 if ($op) {
260 $clause = $this->whereClause($field,
261 $op,
262 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
263 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
264 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
265 );
266 }
267 }
268 if (!empty($clause)) {
269 $clauses[] = $clause;
270 }
271 }
272 }
273 }
274 if (!empty($clauses)) {
275 $this->_where = "WHERE " . implode(' AND ', $clauses);
276 $this->_whereClause = $whereClause . " AND " . implode(' AND ', $clauses);
277 }
278 }
279
280 public function groupBy() {
281 $this->_groupBy = '';
282
283 if (!empty($this->_params['fields']) &&
284 is_array($this->_params['fields']) &&
285 !empty($this->_params['fields'])
286 ) {
287 foreach ($this->_columns as $tableName => $table) {
288 if (array_key_exists('fields', $table)) {
289 foreach ($table['fields'] as $fieldName => $field) {
290 if (!empty($this->_params['fields'][$fieldName])) {
291 $groupBy[] = $field['dbAlias'];
292 }
293 }
294 }
295 }
296 }
297 if (!empty($groupBy)) {
298 $this->_groupBy = CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, $groupBy);
299 }
300 }
301
302 public function postProcess() {
303 // get ready with post process params
304 $this->beginPostProcess();
305
306 // build query, do not apply limit
307 $sql = $this->buildQuery(FALSE);
308
309 // build array of result based on column headers. This method also allows
310 // modifying column headers before using it to build result set i.e $rows.
311 $this->buildRows($sql, $rows);
312
313 // format result set.
314 $this->formatDisplay($rows);
315
316 // assign variables to templates
317 $this->doTemplateAssignment($rows);
318
319 // do print / pdf / instance stuff if needed
320 $this->endPostProcess($rows);
321 }
322
323 /**
324 * Alter display of rows.
325 *
326 * Iterate through the rows retrieved via SQL and make changes for display purposes,
327 * such as rendering contacts as links.
328 *
329 * @param array $rows
330 * Rows generated by SQL, with an array for each row.
331 */
332 public function alterDisplay(&$rows) {
333 $totalStatistics = $grantStatistics = [];
334 $totalStatistics = parent::statistics($rows);
335 $awardedGrantsAmount = $grantsReceived = $totalAmount = $awardedGrants = $grantReportsReceived = 0;
336 $grantStatistics = [];
337
338 $grantTypes = CRM_Core_PseudoConstant::get('CRM_Grant_DAO_Grant', 'grant_type_id');
339 $countries = CRM_Core_PseudoConstant::country();
340 $gender = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id');
341
342 $grantAmountTotal = "
343 SELECT COUNT({$this->_aliases['civicrm_grant']}.id) as count ,
344 SUM({$this->_aliases['civicrm_grant']}.amount_total) as totalAmount
345 {$this->_from} ";
346
347 if (!empty($this->_whereClause)) {
348 $grantAmountTotal .= " {$this->_whereClause}";
349 }
350
351 $result = CRM_Core_DAO::executeQuery($grantAmountTotal);
352 while ($result->fetch()) {
353 $grantsReceived = $result->count;
354 $totalAmount = $result->totalAmount;
355 }
356
357 if (!$grantsReceived) {
358 return;
359 }
360
361 $grantAmountAwarded = "
362 SELECT COUNT({$this->_aliases['civicrm_grant']}.id) as count ,
363 SUM({$this->_aliases['civicrm_grant']}.amount_granted) as grantedAmount,
364 SUM({$this->_aliases['civicrm_grant']}.amount_total) as totalAmount
365 {$this->_from} ";
366
367 if (!empty($this->_where)) {
368 $grantAmountAwarded .= " {$this->_where}";
369 }
370 $values = CRM_Core_DAO::executeQuery($grantAmountAwarded);
371 while ($values->fetch()) {
372 $awardedGrants = $values->count;
373 $awardedGrantsAmount = $values->totalAmount;
374 $amountGranted = $values->grantedAmount;
375 }
376
377 foreach ($rows as $key => $values) {
378 if (!empty($values['civicrm_grant_grant_report_received'])) {
379 $grantReportsReceived++;
380 }
381
382 if (!empty($values['civicrm_grant_grant_type_id'])) {
383 $grantType = $grantTypes[$values['civicrm_grant_grant_type_id']] ?? NULL;
384 $grantStatistics['civicrm_grant_grant_type_id']['title'] = ts('By Grant Type');
385 self::getStatistics($grantStatistics['civicrm_grant_grant_type_id'], $grantType, $values,
386 $awardedGrants, $awardedGrantsAmount
387 );
388 }
389
390 if (array_key_exists('civicrm_worldregion_name', $values)) {
391 $region = $values['civicrm_worldregion_name'] ?? NULL;
392 $region = ($region) ? $region : 'Unassigned';
393 $grantStatistics['civicrm_worldregion_name']['title'] = ts('By Region');
394 self::getStatistics($grantStatistics['civicrm_worldregion_name'], $region, $values,
395 $awardedGrants, $awardedGrantsAmount
396 );
397 }
398
399 if (array_key_exists('civicrm_address_country_id', $values)) {
400 $country = $countries[$values['civicrm_address_country_id']] ?? NULL;
401 $country = ($country) ? $country : 'Unassigned';
402 $grantStatistics['civicrm_address_country_id']['title'] = ts('By Country');
403 self::getStatistics($grantStatistics['civicrm_address_country_id'], $country, $values,
404 $awardedGrants, $awardedGrantsAmount
405 );
406 }
407
408 if ($type = CRM_Utils_Array::value('civicrm_contact_contact_type', $values)) {
409 $grantStatistics['civicrm_contact_contact_type']['title'] = ts('By Contact Type');
410 $title = "Total Number of {$type}(s)";
411 self::getStatistics($grantStatistics['civicrm_contact_contact_type'], $title, $values,
412 $awardedGrants, $awardedGrantsAmount
413 );
414 }
415
416 if (array_key_exists('civicrm_contact_gender_id', $values)) {
417 $genderLabel = $gender[$values['civicrm_contact_gender_id']] ?? NULL;
418 $genderLabel = ($genderLabel) ? $genderLabel : 'Unassigned';
419 $grantStatistics['civicrm_contact_gender_id']['title'] = ts('By Gender');
420 self::getStatistics($grantStatistics['civicrm_contact_gender_id'], $genderLabel, $values,
421 $awardedGrants, $awardedGrantsAmount
422 );
423 }
424
425 foreach ($values as $customField => $customValue) {
426 if (strstr($customField, 'civicrm_value_')) {
427 $customFieldTitle = $this->_columnHeaders[$customField]['title'] ?? NULL;
428 $customGroupTitle = explode('_custom', strstr($customField, 'civicrm_value_'));
429 $customGroupTitle = $this->_columns[$customGroupTitle[0]]['group_title'];
430 $grantStatistics[$customGroupTitle]['title'] = ts('By %1', [1 => $customGroupTitle]);
431
432 self::getStatistics($grantStatistics[$customGroupTitle], $customFieldTitle, $values,
433 $awardedGrants, $awardedGrantsAmount, !$customValue
434 );
435 }
436 }
437 }
438
439 $totalStatistics['total_statistics'] = [
440 'grants_received' => [
441 'title' => ts('Grant Requests Received'),
442 'count' => $grantsReceived,
443 'amount' => $totalAmount,
444 ],
445 'grants_awarded' => [
446 'title' => ts('Grants Awarded'),
447 'count' => $awardedGrants,
448 'amount' => $amountGranted,
449 ],
450 'grants_report_received' => [
451 'title' => ts('Grant Reports Received'),
452 'count' => $grantReportsReceived,
453 ],
454 ];
455
456 $this->assign('totalStatistics', $totalStatistics);
457 $this->assign('grantStatistics', $grantStatistics);
458
459 if ($this->_outputMode == 'csv' ||
460 $this->_outputMode == 'pdf'
461 ) {
462 $row = [];
463 $this->_columnHeaders = [
464 'civicrm_grant_total_grants' => ['title' => ts('Summary')],
465 'civicrm_grant_count' => ['title' => ts('Count')],
466 'civicrm_grant_amount' => ['title' => ts('Amount')],
467 ];
468 foreach ($totalStatistics['total_statistics'] as $title => $value) {
469 $row[] = [
470 'civicrm_grant_total_grants' => $value['title'],
471 'civicrm_grant_count' => $value['count'],
472 'civicrm_grant_amount' => $value['amount'],
473 ];
474 }
475
476 if (!empty($grantStatistics)) {
477 foreach ($grantStatistics as $key => $value) {
478 $row[] = [
479 'civicrm_grant_total_grants' => $value['title'],
480 'civicrm_grant_count' => ts('Number of Grants') . ' (%)',
481 'civicrm_grant_amount' => ts('Total Amount') . ' (%)',
482 ];
483
484 foreach ($value['value'] as $field => $values) {
485 foreach ($values['currency'] as $currency => $amount) {
486 $totalAmount[$currency] = $currency . $amount['value'] .
487 "({$values['percentage']}%)";
488 }
489 $totalAmt = implode(', ', $totalAmount);
490 $count = (boolean) CRM_Utils_Array::value('count', $values, 0) ? $values['count'] . " ({$values['percentage']}%)" : '';
491 $row[] = [
492 'civicrm_grant_total_grants' => $field,
493 'civicrm_grant_count' => $count,
494 'civicrm_grant_amount' => $totalAmt,
495 ];
496 }
497 }
498 }
499 $rows = $row;
500 }
501 }
502
503 /**
504 * @param $grantStatistics
505 * @param $fieldValue
506 * @param $values
507 * @param $awardedGrants
508 * @param $awardedGrantsAmount
509 * @param bool $customData
510 */
511 public static function getStatistics(
512 &$grantStatistics, $fieldValue, $values,
513 $awardedGrants, $awardedGrantsAmount, $customData = FALSE
514 ) {
515 if (!$awardedGrantsAmount) {
516 return;
517 }
518
519 $currencies = CRM_Core_PseudoConstant::get('CRM_Grant_DAO_Grant', 'currency', ['labelColumn' => 'symbol']);
520 $currency = $currencies[$values['civicrm_grant_currency']];
521
522 if (!$customData) {
523 if (!isset($grantStatistics['value'][$fieldValue]['currency'][$currency])
524 ||
525 !isset($grantStatistics['value'][$fieldValue]['currency'][$currency]['value'])
526 ) {
527 $grantStatistics['value'][$fieldValue]['currency'][$currency]['value'] = 0;
528 }
529 $grantStatistics['value'][$fieldValue]['currency'][$currency]['value'] += $values['civicrm_grant_amount_total'];
530 $grantStatistics['value'][$fieldValue]['currency'][$currency]['percentage'] = round(($grantStatistics['value'][$fieldValue]['currency'][$currency]['value'] /
531 $awardedGrantsAmount) * 100);
532 if (!isset($grantStatistics['value'][$fieldValue]['count'])) {
533 $grantStatistics['value'][$fieldValue]['count'] = 0;
534 }
535 $grantStatistics['value'][$fieldValue]['count']++;
536 $grantStatistics['value'][$fieldValue]['percentage'] = round(($grantStatistics['value'][$fieldValue]['count'] /
537 $awardedGrants) * 100);
538 }
539 else {
540 if (!isset($grantStatistics['value'][$fieldValue]['unassigned_currency'][$currency])
541 ||
542 !isset($grantStatistics['value'][$fieldValue]['unassigned_currency'][$currency]['value'])
543 ) {
544 $grantStatistics['value'][$fieldValue]['unassigned_currency'][$currency]['value'] = 0;
545 }
546 $grantStatistics['value'][$fieldValue]['unassigned_currency'][$currency]['value'] += $values['civicrm_grant_amount_total'];
547 $grantStatistics['value'][$fieldValue]['unassigned_currency'][$currency]['percentage'] = round(($grantStatistics['value'][$fieldValue]['unassigned_currency'][$currency]['value'] /
548 $awardedGrantsAmount) * 100);
549 $grantStatistics['value'][$fieldValue]['unassigned_count']++;
550 $grantStatistics['value'][$fieldValue]['unassigned_percentage'] = round(($grantStatistics['value'][$fieldValue]['unassigned_count'] /
551 $awardedGrants) * 100);
552 }
553 }
554
555 }