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