Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | class CRM_Report_Form_Grant_Statistics extends CRM_Report_Form { | |
18 | ||
be2fb01f | 19 | protected $_customGroupExtends = ['Grant']; |
6a488035 TO |
20 | |
21 | protected $_add2groupSupported = FALSE; | |
74cf4551 EM |
22 | |
23 | /** | |
73b448bf | 24 | * Class constructor. |
74cf4551 | 25 | */ |
00be9182 | 26 | public function __construct() { |
be2fb01f CW |
27 | $this->_columns = [ |
28 | 'civicrm_grant' => [ | |
6a488035 | 29 | 'dao' => 'CRM_Grant_DAO_Grant', |
be2fb01f CW |
30 | 'fields' => [ |
31 | 'summary_statistics' => [ | |
6a488035 TO |
32 | 'name' => 'id', |
33 | 'title' => ts('Summary Statistics'), | |
34 | 'required' => TRUE, | |
be2fb01f CW |
35 | ], |
36 | 'grant_type_id' => [ | |
6a488035 TO |
37 | 'name' => 'grant_type_id', |
38 | 'title' => ts('By Grant Type'), | |
be2fb01f CW |
39 | ], |
40 | 'status_id' => [ | |
6a488035 TO |
41 | 'no_display' => TRUE, |
42 | 'required' => TRUE, | |
be2fb01f CW |
43 | ], |
44 | 'amount_total' => [ | |
6a488035 TO |
45 | 'no_display' => TRUE, |
46 | 'required' => TRUE, | |
be2fb01f CW |
47 | ], |
48 | 'grant_report_received' => [ | |
6a488035 TO |
49 | 'no_display' => TRUE, |
50 | 'required' => TRUE, | |
be2fb01f CW |
51 | ], |
52 | 'currency' => [ | |
6a488035 TO |
53 | 'no_display' => TRUE, |
54 | 'required' => TRUE, | |
be2fb01f CW |
55 | ], |
56 | ], | |
57 | 'filters' => [ | |
58 | 'application_received_date' => [ | |
6a488035 TO |
59 | 'name' => 'application_received_date', |
60 | 'title' => ts('Application Received'), | |
61 | 'operatorType' => CRM_Report_Form::OP_DATE, | |
62 | 'type' => CRM_Utils_Type::T_DATE, | |
be2fb01f CW |
63 | ], |
64 | 'decision_date' => [ | |
6a488035 TO |
65 | 'name' => 'decision_date', |
66 | 'title' => ts('Grant Decision'), | |
67 | 'operatorType' => CRM_Report_Form::OP_DATE, | |
68 | 'type' => CRM_Utils_Type::T_DATE, | |
be2fb01f CW |
69 | ], |
70 | 'money_transfer_date' => [ | |
6a488035 TO |
71 | 'name' => 'money_transfer_date', |
72 | 'title' => ts('Money Transferred'), | |
73 | 'operatorType' => CRM_Report_Form::OP_DATE, | |
74 | 'type' => CRM_Utils_Type::T_DATE, | |
be2fb01f CW |
75 | ], |
76 | 'grant_due_date' => [ | |
6a488035 TO |
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, | |
be2fb01f CW |
81 | ], |
82 | 'grant_type' => [ | |
6a488035 TO |
83 | 'name' => 'grant_type_id', |
84 | 'title' => ts('Grant Type'), | |
85 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
fb1fd730 | 86 | 'options' => CRM_Core_PseudoConstant::get('CRM_Grant_DAO_Grant', 'grant_type_id'), |
be2fb01f CW |
87 | ], |
88 | 'status_id' => [ | |
6a488035 TO |
89 | 'name' => 'status_id', |
90 | 'title' => ts('Grant Status'), | |
09b5cdcf | 91 | 'type' => CRM_Utils_Type::T_INT, |
6a488035 | 92 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
fb1fd730 | 93 | 'options' => CRM_Core_PseudoConstant::get('CRM_Grant_DAO_Grant', 'status_id'), |
be2fb01f CW |
94 | ], |
95 | 'amount_requested' => [ | |
6a488035 TO |
96 | 'name' => 'amount_requested', |
97 | 'title' => ts('Amount Requested'), | |
98 | 'type' => CRM_Utils_Type::T_MONEY, | |
be2fb01f CW |
99 | ], |
100 | 'amount_granted' => [ | |
6a488035 TO |
101 | 'name' => 'amount_granted', |
102 | 'title' => ts('Amount Granted'), | |
be2fb01f CW |
103 | ], |
104 | 'grant_report_received' => [ | |
6a488035 TO |
105 | 'name' => 'grant_report_received', |
106 | 'title' => ts('Report Received'), | |
107 | 'operatorType' => CRM_Report_Form::OP_SELECT, | |
be2fb01f | 108 | 'options' => [ |
9d72cede | 109 | '' => ts('- select -'), |
6a488035 TO |
110 | 0 => ts('No'), |
111 | 1 => ts('Yes'), | |
be2fb01f CW |
112 | ], |
113 | ], | |
114 | ], | |
115 | ], | |
116 | 'civicrm_contact' => [ | |
6a488035 | 117 | 'dao' => 'CRM_Contact_DAO_Contact', |
be2fb01f CW |
118 | 'fields' => [ |
119 | 'id' => [ | |
6a488035 TO |
120 | 'required' => TRUE, |
121 | 'no_display' => TRUE, | |
be2fb01f CW |
122 | ], |
123 | 'gender_id' => [ | |
6a488035 TO |
124 | 'name' => 'gender_id', |
125 | 'title' => ts('By Gender'), | |
be2fb01f CW |
126 | ], |
127 | 'contact_type' => [ | |
6a488035 TO |
128 | 'name' => 'contact_type', |
129 | 'title' => ts('By Contact Type'), | |
be2fb01f CW |
130 | ], |
131 | ], | |
132 | 'filters' => [ | |
133 | 'gender_id' => [ | |
6a488035 TO |
134 | 'name' => 'gender_id', |
135 | 'title' => ts('Gender'), | |
136 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
26cf88b5 | 137 | 'options' => CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'), |
be2fb01f CW |
138 | ], |
139 | 'contact_type' => [ | |
6a488035 TO |
140 | 'name' => 'contact_type', |
141 | 'title' => ts('Contact Type'), | |
142 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
143 | 'options' => CRM_Contact_BAO_ContactType::basicTypePairs(), | |
be2fb01f CW |
144 | ], |
145 | ], | |
6a488035 | 146 | 'grouping' => 'contact-fields', |
be2fb01f CW |
147 | ], |
148 | 'civicrm_worldregion' => [ | |
6a488035 | 149 | 'dao' => 'CRM_Core_DAO_Worldregion', |
be2fb01f CW |
150 | 'fields' => [ |
151 | 'id' => [ | |
6a488035 | 152 | 'no_display' => TRUE, |
be2fb01f CW |
153 | ], |
154 | 'name' => [ | |
6a488035 TO |
155 | 'name' => 'name', |
156 | 'title' => ts('By World Region'), | |
be2fb01f CW |
157 | ], |
158 | ], | |
159 | 'filters' => [ | |
160 | 'region_id' => [ | |
6a488035 TO |
161 | 'name' => 'id', |
162 | 'title' => ts('World Region'), | |
09b5cdcf | 163 | 'type' => CRM_Utils_Type::T_INT, |
6a488035 TO |
164 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
165 | 'options' => CRM_Core_PseudoConstant::worldRegion(), | |
be2fb01f CW |
166 | ], |
167 | ], | |
168 | ], | |
169 | 'civicrm_address' => [ | |
6a488035 | 170 | 'dao' => 'CRM_Core_DAO_Address', |
be2fb01f CW |
171 | 'fields' => [ |
172 | 'country_id' => [ | |
6a488035 TO |
173 | 'name' => 'country_id', |
174 | 'title' => ts('By Country'), | |
be2fb01f CW |
175 | ], |
176 | ], | |
177 | 'filters' => [ | |
178 | 'country_id' => [ | |
9d72cede | 179 | 'title' => ts('Country'), |
09b5cdcf | 180 | 'type' => CRM_Utils_Type::T_INT, |
6a488035 TO |
181 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
182 | 'options' => CRM_Core_PseudoConstant::country(), | |
be2fb01f CW |
183 | ], |
184 | ], | |
185 | ], | |
186 | ]; | |
6a488035 TO |
187 | parent::__construct(); |
188 | } | |
189 | ||
00be9182 | 190 | public function select() { |
be2fb01f | 191 | $select = []; |
6a488035 | 192 | |
be2fb01f | 193 | $this->_columnHeaders = []; |
6a488035 | 194 | foreach ($this->_columns as $tableName => $table) { |
6a488035 TO |
195 | if (array_key_exists('fields', $table)) { |
196 | foreach ($table['fields'] as $fieldName => $field) { | |
9d72cede EM |
197 | if (!empty($field['required']) || |
198 | !empty($this->_params['fields'][$fieldName]) | |
199 | ) { | |
6a488035 TO |
200 | |
201 | $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}"; | |
202 | ||
9c1bc317 CW |
203 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title'] ?? NULL; |
204 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = $field['type'] ?? NULL; | |
6a488035 TO |
205 | } |
206 | } | |
207 | } | |
208 | } | |
d1641c51 | 209 | $this->_selectClauses = $select; |
6a488035 TO |
210 | |
211 | $this->_select = "SELECT " . implode(', ', $select) . " "; | |
212 | } | |
213 | ||
00be9182 | 214 | public function from() { |
6a488035 TO |
215 | $this->_from = " |
216 | FROM civicrm_grant {$this->_aliases['civicrm_grant']} | |
39eb89f4 | 217 | LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']} |
6a488035 | 218 | ON ({$this->_aliases['civicrm_grant']}.contact_id = {$this->_aliases['civicrm_contact']}.id ) "; |
6d81d64a | 219 | |
220 | $this->joinAddressFromContact(); | |
221 | $this->joinCountryFromAddress(); | |
222 | if ($this->isTableSelected('civicrm_worldregion')) { | |
6a488035 | 223 | $this->_from .= " |
6d81d64a | 224 | LEFT JOIN civicrm_worldregion {$this->_aliases['civicrm_worldregion']} |
225 | ON {$this->_aliases['civicrm_country']}.region_id = | |
226 | {$this->_aliases['civicrm_worldregion']}.id"; | |
6a488035 TO |
227 | } |
228 | } | |
229 | ||
00be9182 | 230 | public function where() { |
39eb89f4 DL |
231 | $whereClause = " |
232 | WHERE {$this->_aliases['civicrm_grant']}.amount_total IS NOT NULL | |
6a488035 | 233 | AND {$this->_aliases['civicrm_grant']}.amount_total > 0"; |
7bed22fc | 234 | $this->_where = $whereClause; |
6a488035 TO |
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) { | |
9c1bc317 CW |
242 | $relative = $this->_params["{$fieldName}_relative"] ?? NULL; |
243 | $from = $this->_params["{$fieldName}_from"] ?? NULL; | |
244 | $to = $this->_params["{$fieldName}_to"] ?? NULL; | |
6a488035 TO |
245 | |
246 | if ($relative || $from || $to) { | |
247 | $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']); | |
248 | } | |
249 | } | |
250 | else { | |
9c1bc317 | 251 | $op = $this->_params["{$fieldName}_op"] ?? NULL; |
6a488035 | 252 | if (($fieldName == 'grant_report_received') && |
9d72cede EM |
253 | (CRM_Utils_Array::value("{$fieldName}_value", $this->_params) === |
254 | 0) | |
6a488035 TO |
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; | |
6a488035 TO |
270 | } |
271 | } | |
272 | } | |
273 | } | |
e437ccce | 274 | if (!empty($clauses)) { |
6cfcf9d1 | 275 | $this->_where = "WHERE " . implode(' AND ', $clauses); |
276 | $this->_whereClause = $whereClause . " AND " . implode(' AND ', $clauses); | |
277 | } | |
6a488035 TO |
278 | } |
279 | ||
00be9182 | 280 | public function groupBy() { |
6a488035 TO |
281 | $this->_groupBy = ''; |
282 | ||
a7488080 | 283 | if (!empty($this->_params['fields']) && |
6a488035 TO |
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) { | |
a7488080 | 290 | if (!empty($this->_params['fields'][$fieldName])) { |
d1641c51 | 291 | $groupBy[] = $field['dbAlias']; |
6a488035 TO |
292 | } |
293 | } | |
294 | } | |
295 | } | |
296 | } | |
b708c08d | 297 | if (!empty($groupBy)) { |
298 | $this->_groupBy = CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, $groupBy); | |
6a488035 TO |
299 | } |
300 | } | |
301 | ||
00be9182 | 302 | public function postProcess() { |
6a488035 TO |
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 | ||
74cf4551 | 323 | /** |
ced9bfed EM |
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. | |
74cf4551 | 331 | */ |
00be9182 | 332 | public function alterDisplay(&$rows) { |
be2fb01f | 333 | $totalStatistics = $grantStatistics = []; |
9d72cede | 334 | $totalStatistics = parent::statistics($rows); |
6a488035 | 335 | $awardedGrantsAmount = $grantsReceived = $totalAmount = $awardedGrants = $grantReportsReceived = 0; |
be2fb01f | 336 | $grantStatistics = []; |
6a488035 | 337 | |
fb1fd730 | 338 | $grantTypes = CRM_Core_PseudoConstant::get('CRM_Grant_DAO_Grant', 'grant_type_id'); |
9d72cede EM |
339 | $countries = CRM_Core_PseudoConstant::country(); |
340 | $gender = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'); | |
6a488035 TO |
341 | |
342 | $grantAmountTotal = " | |
39eb89f4 DL |
343 | SELECT COUNT({$this->_aliases['civicrm_grant']}.id) as count , |
344 | SUM({$this->_aliases['civicrm_grant']}.amount_total) as totalAmount | |
6a488035 TO |
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 = " | |
39eb89f4 | 362 | SELECT COUNT({$this->_aliases['civicrm_grant']}.id) as count , |
6a488035 TO |
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()) { | |
9d72cede | 372 | $awardedGrants = $values->count; |
6a488035 | 373 | $awardedGrantsAmount = $values->totalAmount; |
9d72cede | 374 | $amountGranted = $values->grantedAmount; |
6a488035 TO |
375 | } |
376 | ||
377 | foreach ($rows as $key => $values) { | |
a7488080 | 378 | if (!empty($values['civicrm_grant_grant_report_received'])) { |
6a488035 TO |
379 | $grantReportsReceived++; |
380 | } | |
381 | ||
a7488080 | 382 | if (!empty($values['civicrm_grant_grant_type_id'])) { |
9c1bc317 | 383 | $grantType = $grantTypes[$values['civicrm_grant_grant_type_id']] ?? NULL; |
6a488035 TO |
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 | ||
6d81d64a | 390 | if (array_key_exists('civicrm_worldregion_name', $values)) { |
2cfe2cf4 | 391 | $region = $values['civicrm_worldregion_name'] ?: 'Unassigned'; |
6d81d64a | 392 | $grantStatistics['civicrm_worldregion_name']['title'] = ts('By Region'); |
393 | self::getStatistics($grantStatistics['civicrm_worldregion_name'], $region, $values, | |
6a488035 TO |
394 | $awardedGrants, $awardedGrantsAmount |
395 | ); | |
396 | } | |
397 | ||
398 | if (array_key_exists('civicrm_address_country_id', $values)) { | |
2cfe2cf4 | 399 | $country = $countries[$values['civicrm_address_country_id']] ?? 'Unassigned'; |
6a488035 TO |
400 | $grantStatistics['civicrm_address_country_id']['title'] = ts('By Country'); |
401 | self::getStatistics($grantStatistics['civicrm_address_country_id'], $country, $values, | |
402 | $awardedGrants, $awardedGrantsAmount | |
403 | ); | |
404 | } | |
405 | ||
406 | if ($type = CRM_Utils_Array::value('civicrm_contact_contact_type', $values)) { | |
407 | $grantStatistics['civicrm_contact_contact_type']['title'] = ts('By Contact Type'); | |
408 | $title = "Total Number of {$type}(s)"; | |
409 | self::getStatistics($grantStatistics['civicrm_contact_contact_type'], $title, $values, | |
410 | $awardedGrants, $awardedGrantsAmount | |
411 | ); | |
412 | } | |
413 | ||
414 | if (array_key_exists('civicrm_contact_gender_id', $values)) { | |
2cfe2cf4 | 415 | $genderLabel = $gender[$values['civicrm_contact_gender_id']] ?? 'Unassigned'; |
6a488035 TO |
416 | $grantStatistics['civicrm_contact_gender_id']['title'] = ts('By Gender'); |
417 | self::getStatistics($grantStatistics['civicrm_contact_gender_id'], $genderLabel, $values, | |
418 | $awardedGrants, $awardedGrantsAmount | |
419 | ); | |
420 | } | |
421 | ||
422 | foreach ($values as $customField => $customValue) { | |
423 | if (strstr($customField, 'civicrm_value_')) { | |
9c1bc317 | 424 | $customFieldTitle = $this->_columnHeaders[$customField]['title'] ?? NULL; |
6a488035 TO |
425 | $customGroupTitle = explode('_custom', strstr($customField, 'civicrm_value_')); |
426 | $customGroupTitle = $this->_columns[$customGroupTitle[0]]['group_title']; | |
be2fb01f | 427 | $grantStatistics[$customGroupTitle]['title'] = ts('By %1', [1 => $customGroupTitle]); |
6a488035 | 428 | |
6a488035 | 429 | self::getStatistics($grantStatistics[$customGroupTitle], $customFieldTitle, $values, |
91768280 | 430 | $awardedGrants, $awardedGrantsAmount, !$customValue |
6a488035 TO |
431 | ); |
432 | } | |
433 | } | |
434 | } | |
435 | ||
be2fb01f CW |
436 | $totalStatistics['total_statistics'] = [ |
437 | 'grants_received' => [ | |
9d72cede | 438 | 'title' => ts('Grant Requests Received'), |
6a488035 TO |
439 | 'count' => $grantsReceived, |
440 | 'amount' => $totalAmount, | |
be2fb01f CW |
441 | ], |
442 | 'grants_awarded' => [ | |
9d72cede | 443 | 'title' => ts('Grants Awarded'), |
6a488035 TO |
444 | 'count' => $awardedGrants, |
445 | 'amount' => $amountGranted, | |
be2fb01f CW |
446 | ], |
447 | 'grants_report_received' => [ | |
9d72cede | 448 | 'title' => ts('Grant Reports Received'), |
6a488035 | 449 | 'count' => $grantReportsReceived, |
be2fb01f CW |
450 | ], |
451 | ]; | |
6a488035 TO |
452 | |
453 | $this->assign('totalStatistics', $totalStatistics); | |
454 | $this->assign('grantStatistics', $grantStatistics); | |
455 | ||
456 | if ($this->_outputMode == 'csv' || | |
457 | $this->_outputMode == 'pdf' | |
458 | ) { | |
be2fb01f CW |
459 | $row = []; |
460 | $this->_columnHeaders = [ | |
461 | 'civicrm_grant_total_grants' => ['title' => ts('Summary')], | |
462 | 'civicrm_grant_count' => ['title' => ts('Count')], | |
463 | 'civicrm_grant_amount' => ['title' => ts('Amount')], | |
464 | ]; | |
6a488035 | 465 | foreach ($totalStatistics['total_statistics'] as $title => $value) { |
be2fb01f | 466 | $row[] = [ |
6a488035 TO |
467 | 'civicrm_grant_total_grants' => $value['title'], |
468 | 'civicrm_grant_count' => $value['count'], | |
469 | 'civicrm_grant_amount' => $value['amount'], | |
be2fb01f | 470 | ]; |
6a488035 TO |
471 | } |
472 | ||
473 | if (!empty($grantStatistics)) { | |
474 | foreach ($grantStatistics as $key => $value) { | |
be2fb01f | 475 | $row[] = [ |
6a488035 TO |
476 | 'civicrm_grant_total_grants' => $value['title'], |
477 | 'civicrm_grant_count' => ts('Number of Grants') . ' (%)', | |
478 | 'civicrm_grant_amount' => ts('Total Amount') . ' (%)', | |
be2fb01f | 479 | ]; |
6a488035 TO |
480 | |
481 | foreach ($value['value'] as $field => $values) { | |
482 | foreach ($values['currency'] as $currency => $amount) { | |
9d72cede EM |
483 | $totalAmount[$currency] = $currency . $amount['value'] . |
484 | "({$values['percentage']}%)"; | |
6a488035 TO |
485 | } |
486 | $totalAmt = implode(', ', $totalAmount); | |
596a8bdf | 487 | $count = empty($values['count']) ? '' : "{$values['count']} ({$values['percentage']}%)"; |
be2fb01f | 488 | $row[] = [ |
6a488035 TO |
489 | 'civicrm_grant_total_grants' => $field, |
490 | 'civicrm_grant_count' => $count, | |
491 | 'civicrm_grant_amount' => $totalAmt, | |
be2fb01f | 492 | ]; |
6a488035 TO |
493 | } |
494 | } | |
495 | } | |
496 | $rows = $row; | |
497 | } | |
498 | } | |
499 | ||
74cf4551 EM |
500 | /** |
501 | * @param $grantStatistics | |
502 | * @param $fieldValue | |
503 | * @param $values | |
504 | * @param $awardedGrants | |
505 | * @param $awardedGrantsAmount | |
506 | * @param bool $customData | |
507 | */ | |
59f4c9ee | 508 | public static function getStatistics( |
7d8c1168 TO |
509 | &$grantStatistics, $fieldValue, $values, |
510 | $awardedGrants, $awardedGrantsAmount, $customData = FALSE | |
6a488035 TO |
511 | ) { |
512 | if (!$awardedGrantsAmount) { | |
513 | return; | |
514 | } | |
515 | ||
be2fb01f | 516 | $currencies = CRM_Core_PseudoConstant::get('CRM_Grant_DAO_Grant', 'currency', ['labelColumn' => 'symbol']); |
6a488035 TO |
517 | $currency = $currencies[$values['civicrm_grant_currency']]; |
518 | ||
519 | if (!$customData) { | |
74cf4551 | 520 | if (!isset($grantStatistics['value'][$fieldValue]['currency'][$currency]) |
9d72cede EM |
521 | || |
522 | !isset($grantStatistics['value'][$fieldValue]['currency'][$currency]['value']) | |
523 | ) { | |
7bed22fc PN |
524 | $grantStatistics['value'][$fieldValue]['currency'][$currency]['value'] = 0; |
525 | } | |
6a488035 | 526 | $grantStatistics['value'][$fieldValue]['currency'][$currency]['value'] += $values['civicrm_grant_amount_total']; |
9d72cede EM |
527 | $grantStatistics['value'][$fieldValue]['currency'][$currency]['percentage'] = round(($grantStatistics['value'][$fieldValue]['currency'][$currency]['value'] / |
528 | $awardedGrantsAmount) * 100); | |
7bed22fc PN |
529 | if (!isset($grantStatistics['value'][$fieldValue]['count'])) { |
530 | $grantStatistics['value'][$fieldValue]['count'] = 0; | |
531 | } | |
6a488035 | 532 | $grantStatistics['value'][$fieldValue]['count']++; |
9d72cede EM |
533 | $grantStatistics['value'][$fieldValue]['percentage'] = round(($grantStatistics['value'][$fieldValue]['count'] / |
534 | $awardedGrants) * 100); | |
6a488035 TO |
535 | } |
536 | else { | |
74cf4551 | 537 | if (!isset($grantStatistics['value'][$fieldValue]['unassigned_currency'][$currency]) |
9d72cede EM |
538 | || |
539 | !isset($grantStatistics['value'][$fieldValue]['unassigned_currency'][$currency]['value']) | |
540 | ) { | |
7bed22fc | 541 | $grantStatistics['value'][$fieldValue]['unassigned_currency'][$currency]['value'] = 0; |
74cf4551 | 542 | } |
6a488035 | 543 | $grantStatistics['value'][$fieldValue]['unassigned_currency'][$currency]['value'] += $values['civicrm_grant_amount_total']; |
9d72cede EM |
544 | $grantStatistics['value'][$fieldValue]['unassigned_currency'][$currency]['percentage'] = round(($grantStatistics['value'][$fieldValue]['unassigned_currency'][$currency]['value'] / |
545 | $awardedGrantsAmount) * 100); | |
6a488035 | 546 | $grantStatistics['value'][$fieldValue]['unassigned_count']++; |
9d72cede EM |
547 | $grantStatistics['value'][$fieldValue]['unassigned_percentage'] = round(($grantStatistics['value'][$fieldValue]['unassigned_count'] / |
548 | $awardedGrants) * 100); | |
6a488035 TO |
549 | } |
550 | } | |
96025800 | 551 | |
6a488035 | 552 | } |