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_Contribute_Sybunt extends CRM_Report_Form { | |
18 | ||
be2fb01f | 19 | protected $_customGroupExtends = [ |
70bea8e2 | 20 | 'Contact', |
21 | 'Individual', | |
7d793900 | 22 | 'Contribution', |
be2fb01f | 23 | ]; |
70bea8e2 | 24 | |
be2fb01f | 25 | public $_drilldownReport = ['contribute/detail' => 'Link to Detail Report']; |
6a488035 | 26 | |
1728e9a0 | 27 | /** |
eb7b4052 | 28 | * This report has been optimised for group filtering. |
1728e9a0 | 29 | * |
1728e9a0 | 30 | * @var bool |
0e480632 | 31 | * @see https://issues.civicrm.org/jira/browse/CRM-19170 |
1728e9a0 | 32 | */ |
eb7b4052 | 33 | protected $groupFilterNotOptimised = FALSE; |
1728e9a0 | 34 | |
74cf4551 | 35 | /** |
eb7b4052 | 36 | * Class constructor. |
74cf4551 | 37 | */ |
00be9182 | 38 | public function __construct() { |
55f71fa7 | 39 | $this->_rollup = 'WITH ROLLUP'; |
70bea8e2 | 40 | $this->_autoIncludeIndexedFieldsAsOrderBys = 1; |
9d72cede | 41 | $yearsInPast = 10; |
6a488035 | 42 | $yearsInFuture = 1; |
9d72cede EM |
43 | $date = CRM_Core_SelectValues::date('custom', NULL, $yearsInPast, $yearsInFuture); |
44 | $count = $date['maxYear']; | |
6a488035 TO |
45 | while ($date['minYear'] <= $count) { |
46 | $optionYear[$date['minYear']] = $date['minYear']; | |
47 | $date['minYear']++; | |
48 | } | |
49 | ||
be2fb01f CW |
50 | $this->_columns = [ |
51 | 'civicrm_contact' => [ | |
a1a2a83d TO |
52 | 'dao' => 'CRM_Contact_DAO_Contact', |
53 | 'grouping' => 'contact-field', | |
be2fb01f CW |
54 | 'fields' => [ |
55 | 'sort_name' => [ | |
a1a2a83d TO |
56 | 'title' => ts('Donor Name'), |
57 | 'required' => TRUE, | |
be2fb01f CW |
58 | ], |
59 | 'first_name' => [ | |
a1a2a83d | 60 | 'title' => ts('First Name'), |
be2fb01f CW |
61 | ], |
62 | 'middle_name' => [ | |
70bea8e2 | 63 | 'title' => ts('Middle Name'), |
be2fb01f CW |
64 | ], |
65 | 'last_name' => [ | |
a1a2a83d | 66 | 'title' => ts('Last Name'), |
be2fb01f CW |
67 | ], |
68 | 'id' => [ | |
70bea8e2 | 69 | 'no_display' => TRUE, |
70 | 'required' => TRUE, | |
be2fb01f CW |
71 | ], |
72 | 'gender_id' => [ | |
70bea8e2 | 73 | 'title' => ts('Gender'), |
be2fb01f CW |
74 | ], |
75 | 'birth_date' => [ | |
70bea8e2 | 76 | 'title' => ts('Birth Date'), |
be2fb01f CW |
77 | ], |
78 | 'age' => [ | |
70bea8e2 | 79 | 'title' => ts('Age'), |
80 | 'dbAlias' => 'TIMESTAMPDIFF(YEAR, contact_civireport.birth_date, CURDATE())', | |
be2fb01f CW |
81 | ], |
82 | 'contact_type' => [ | |
70bea8e2 | 83 | 'title' => ts('Contact Type'), |
be2fb01f CW |
84 | ], |
85 | 'contact_sub_type' => [ | |
70bea8e2 | 86 | 'title' => ts('Contact Subtype'), |
be2fb01f CW |
87 | ], |
88 | ], | |
89 | 'order_bys' => [ | |
90 | 'sort_name' => [ | |
70bea8e2 | 91 | 'title' => ts('Last Name, First Name'), |
92 | 'default' => '1', | |
93 | 'default_weight' => '0', | |
94 | 'default_order' => 'ASC', | |
be2fb01f CW |
95 | ], |
96 | 'first_name' => [ | |
70bea8e2 | 97 | 'name' => 'first_name', |
98 | 'title' => ts('First Name'), | |
be2fb01f CW |
99 | ], |
100 | 'gender_id' => [ | |
70bea8e2 | 101 | 'name' => 'gender_id', |
102 | 'title' => ts('Gender'), | |
be2fb01f CW |
103 | ], |
104 | 'birth_date' => [ | |
70bea8e2 | 105 | 'name' => 'birth_date', |
106 | 'title' => ts('Birth Date'), | |
be2fb01f CW |
107 | ], |
108 | 'age_at_event' => [ | |
70bea8e2 | 109 | 'name' => 'age_at_event', |
110 | 'title' => ts('Age at Event'), | |
be2fb01f CW |
111 | ], |
112 | 'contact_type' => [ | |
a1a2a83d | 113 | 'title' => ts('Contact Type'), |
be2fb01f CW |
114 | ], |
115 | 'contact_sub_type' => [ | |
a1a2a83d | 116 | 'title' => ts('Contact Subtype'), |
be2fb01f CW |
117 | ], |
118 | ], | |
119 | 'filters' => [ | |
120 | 'sort_name' => [ | |
a1a2a83d TO |
121 | 'title' => ts('Donor Name'), |
122 | 'operator' => 'like', | |
be2fb01f CW |
123 | ], |
124 | 'id' => [ | |
70bea8e2 | 125 | 'title' => ts('Contact ID'), |
126 | 'no_display' => TRUE, | |
be2fb01f CW |
127 | ], |
128 | 'gender_id' => [ | |
70bea8e2 | 129 | 'title' => ts('Gender'), |
130 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
131 | 'options' => CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'), | |
be2fb01f CW |
132 | ], |
133 | 'birth_date' => [ | |
70bea8e2 | 134 | 'title' => ts('Birth Date'), |
135 | 'operatorType' => CRM_Report_Form::OP_DATE, | |
be2fb01f CW |
136 | ], |
137 | 'contact_type' => [ | |
70bea8e2 | 138 | 'title' => ts('Contact Type'), |
be2fb01f CW |
139 | ], |
140 | 'contact_sub_type' => [ | |
70bea8e2 | 141 | 'title' => ts('Contact Subtype'), |
be2fb01f CW |
142 | ], |
143 | ], | |
144 | ], | |
145 | 'civicrm_line_item' => [ | |
b134b8cc | 146 | 'dao' => 'CRM_Price_DAO_LineItem', |
be2fb01f CW |
147 | ], |
148 | 'civicrm_email' => [ | |
a1a2a83d TO |
149 | 'dao' => 'CRM_Core_DAO_Email', |
150 | 'grouping' => 'contact-field', | |
be2fb01f CW |
151 | 'fields' => [ |
152 | 'email' => [ | |
a1a2a83d TO |
153 | 'title' => ts('Email'), |
154 | 'default' => TRUE, | |
be2fb01f CW |
155 | ], |
156 | ], | |
101f8739 | 157 | 'filters' => [ |
158 | 'on_hold' => [ | |
159 | 'title' => ts('On Hold'), | |
160 | 'type' => CRM_Utils_Type::T_INT, | |
161 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
162 | 'options' => ['' => ts('Any')] + CRM_Core_PseudoConstant::emailOnHoldOptions(), | |
163 | ], | |
164 | ], | |
be2fb01f CW |
165 | ], |
166 | 'civicrm_phone' => [ | |
a1a2a83d TO |
167 | 'dao' => 'CRM_Core_DAO_Phone', |
168 | 'grouping' => 'contact-field', | |
be2fb01f CW |
169 | 'fields' => [ |
170 | 'phone' => [ | |
a1a2a83d TO |
171 | 'title' => ts('Phone'), |
172 | 'default' => TRUE, | |
be2fb01f CW |
173 | ], |
174 | ], | |
175 | ], | |
176 | ]; | |
a1a2a83d | 177 | $this->_columns += $this->addAddressFields(); |
be2fb01f CW |
178 | $this->_columns += [ |
179 | 'civicrm_contribution' => [ | |
a1a2a83d | 180 | 'dao' => 'CRM_Contribute_DAO_Contribution', |
be2fb01f CW |
181 | 'fields' => [ |
182 | 'contact_id' => [ | |
a1a2a83d TO |
183 | 'title' => ts('contactId'), |
184 | 'no_display' => TRUE, | |
185 | 'required' => TRUE, | |
186 | 'no_repeat' => TRUE, | |
be2fb01f CW |
187 | ], |
188 | 'total_amount' => [ | |
a1a2a83d TO |
189 | 'title' => ts('Total Amount'), |
190 | 'no_display' => TRUE, | |
191 | 'required' => TRUE, | |
192 | 'no_repeat' => TRUE, | |
be2fb01f CW |
193 | ], |
194 | 'receive_date' => [ | |
a1a2a83d TO |
195 | 'title' => ts('Year'), |
196 | 'no_display' => TRUE, | |
197 | 'required' => TRUE, | |
198 | 'no_repeat' => TRUE, | |
be2fb01f CW |
199 | ], |
200 | ], | |
201 | 'filters' => [ | |
202 | 'yid' => [ | |
a1a2a83d TO |
203 | 'name' => 'receive_date', |
204 | 'title' => ts('This Year'), | |
205 | 'operatorType' => CRM_Report_Form::OP_SELECT, | |
206 | 'options' => $optionYear, | |
207 | 'default' => date('Y'), | |
55f71fa7 | 208 | 'type' => CRM_Utils_Type::T_INT, |
be2fb01f CW |
209 | ], |
210 | 'financial_type_id' => [ | |
a1a2a83d | 211 | 'title' => ts('Financial Type'), |
8ee006e7 | 212 | 'type' => CRM_Utils_Type::T_INT, |
a1a2a83d | 213 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
51d1f926 | 214 | 'options' => CRM_Contribute_BAO_Contribution::buildOptions('financial_type_id', 'search'), |
be2fb01f CW |
215 | ], |
216 | 'contribution_status_id' => [ | |
a1a2a83d TO |
217 | 'title' => ts('Contribution Status'), |
218 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
c0aaecf9 | 219 | 'options' => CRM_Contribute_BAO_Contribution::buildOptions('contribution_status_id', 'search'), |
be2fb01f CW |
220 | 'default' => ['1'], |
221 | ], | |
222 | ], | |
223 | ], | |
224 | ]; | |
225 | $this->_columns += [ | |
226 | 'civicrm_financial_trxn' => [ | |
0ddcd8e9 | 227 | 'dao' => 'CRM_Financial_DAO_FinancialTrxn', |
be2fb01f CW |
228 | 'fields' => [ |
229 | 'card_type_id' => [ | |
d72b084a | 230 | 'title' => ts('Credit Card Type'), |
5e0343e8 | 231 | 'dbAlias' => 'GROUP_CONCAT(financial_trxn_civireport.card_type_id SEPARATOR ",")', |
be2fb01f CW |
232 | ], |
233 | ], | |
234 | 'filters' => [ | |
235 | 'card_type_id' => [ | |
d72b084a | 236 | 'title' => ts('Credit Card Type'), |
0ddcd8e9 E |
237 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
238 | 'default' => NULL, | |
5e0343e8 | 239 | 'options' => CRM_Financial_DAO_FinancialTrxn::buildOptions('card_type_id'), |
0ddcd8e9 | 240 | 'type' => CRM_Utils_Type::T_STRING, |
be2fb01f CW |
241 | ], |
242 | ], | |
243 | ], | |
244 | ]; | |
6a488035 | 245 | |
d62fab33 | 246 | // If we have a campaign, build out the relevant elements |
1be29aad | 247 | $this->addCampaignFields('civicrm_contribution'); |
d62fab33 | 248 | |
f787b3c4 BT |
249 | // Add charts support |
250 | $this->_charts = [ | |
251 | '' => ts('Tabular'), | |
252 | 'barChart' => ts('Bar Chart'), | |
253 | 'pieChart' => ts('Pie Chart'), | |
254 | ]; | |
255 | ||
16e2e80c | 256 | $this->_groupFilter = TRUE; |
6a488035 TO |
257 | $this->_tagFilter = TRUE; |
258 | parent::__construct(); | |
259 | } | |
260 | ||
00be9182 | 261 | public function preProcess() { |
6a488035 TO |
262 | parent::preProcess(); |
263 | } | |
264 | ||
00be9182 | 265 | public function select() { |
be2fb01f CW |
266 | $select = []; |
267 | $this->_columnHeaders = []; | |
6a488035 TO |
268 | $current_year = $this->_params['yid_value']; |
269 | $previous_year = $current_year - 1; | |
270 | $previous_pyear = $current_year - 2; | |
271 | $previous_ppyear = $current_year - 3; | |
272 | $upTo_year = $current_year - 4; | |
273 | ||
274 | foreach ($this->_columns as $tableName => $table) { | |
275 | if (array_key_exists('fields', $table)) { | |
276 | foreach ($table['fields'] as $fieldName => $field) { | |
277 | ||
9d72cede EM |
278 | if (!empty($field['required']) || |
279 | !empty($this->_params['fields'][$fieldName]) | |
280 | ) { | |
6a488035 TO |
281 | if ($fieldName == 'total_amount') { |
282 | $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}"; | |
283 | ||
284 | $this->_columnHeaders["civicrm_upto_{$upTo_year}"]['type'] = $field['type']; | |
be2fb01f | 285 | $this->_columnHeaders["civicrm_upto_{$upTo_year}"]['title'] = ts("Up To %1", [1 => $upTo_year]); |
6a488035 | 286 | |
db36f066 ML |
287 | $this->_columnHeaders["year_{$previous_ppyear}"]['type'] = $field['type']; |
288 | $this->_columnHeaders["year_{$previous_ppyear}"]['title'] = $previous_ppyear; | |
6a488035 | 289 | |
db36f066 ML |
290 | $this->_columnHeaders["year_{$previous_pyear}"]['type'] = $field['type']; |
291 | $this->_columnHeaders["year_{$previous_pyear}"]['title'] = $previous_pyear; | |
6a488035 | 292 | |
db36f066 ML |
293 | $this->_columnHeaders["year_{$previous_year}"]['type'] = $field['type']; |
294 | $this->_columnHeaders["year_{$previous_year}"]['title'] = $previous_year; | |
6a488035 TO |
295 | |
296 | $this->_columnHeaders["civicrm_life_time_total"]['type'] = $field['type']; | |
ccc29f8e | 297 | $this->_columnHeaders["civicrm_life_time_total"]['title'] = ts('LifeTime'); |
6a488035 TO |
298 | } |
299 | elseif ($fieldName == 'receive_date') { | |
9d72cede EM |
300 | $select[] = self::fiscalYearOffset($field['dbAlias']) . |
301 | " as {$tableName}_{$fieldName}"; | |
6a488035 TO |
302 | } |
303 | else { | |
304 | $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}"; | |
9c1bc317 | 305 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = $field['type'] ?? NULL; |
6a488035 TO |
306 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title']; |
307 | } | |
a7488080 | 308 | if (!empty($field['no_display'])) { |
6a488035 TO |
309 | $this->_columnHeaders["{$tableName}_{$fieldName}"]['no_display'] = TRUE; |
310 | } | |
311 | } | |
312 | } | |
313 | } | |
314 | } | |
d1641c51 | 315 | $this->_selectClauses = $select; |
6a488035 TO |
316 | |
317 | $this->_select = "SELECT " . implode(', ', $select) . " "; | |
318 | } | |
319 | ||
00be9182 | 320 | public function from() { |
eb7b4052 | 321 | $this->setFromBase('civicrm_contribution', 'contact_id'); |
322 | $this->_from .= " | |
a27c90f2 NG |
323 | INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact']} |
324 | ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id | |
d4ed4646 | 325 | {$this->_aclFrom}"; |
a27c90f2 | 326 | |
18f511e2 | 327 | $this->joinPhoneFromContact(); |
328 | $this->joinEmailFromContact(); | |
329 | ||
0ddcd8e9 E |
330 | // for credit card type |
331 | $this->addFinancialTrxnFromClause(); | |
332 | ||
18f511e2 | 333 | $this->joinAddressFromContact(); |
6a488035 TO |
334 | } |
335 | ||
00be9182 | 336 | public function where() { |
6a488035 | 337 | $this->_statusClause = ""; |
9f108b4d JJ |
338 | $clauses = [ |
339 | $this->_aliases['civicrm_contribution'] . '.is_test = 0', | |
340 | $this->_aliases['civicrm_contribution'] . '.is_template = 0', | |
341 | ]; | |
6a488035 TO |
342 | foreach ($this->_columns as $tableName => $table) { |
343 | if (array_key_exists('filters', $table)) { | |
344 | foreach ($table['filters'] as $fieldName => $field) { | |
345 | $clause = NULL; | |
346 | if ($fieldName == 'yid') { | |
347 | $clause = "contribution_civireport.contact_id NOT IN | |
348 | (SELECT distinct cont.id FROM civicrm_contact cont, civicrm_contribution contri | |
9d72cede EM |
349 | WHERE cont.id = contri.contact_id AND " . |
350 | self::fiscalYearOffset('contri.receive_date') . | |
9f108b4d | 351 | " = {$this->_params['yid_value']} AND contri.is_test = 0 AND contri.is_template = 0 )"; |
6a488035 | 352 | } |
84178120 | 353 | elseif (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE |
9d72cede | 354 | ) { |
9c1bc317 CW |
355 | $relative = $this->_params["{$fieldName}_relative"] ?? NULL; |
356 | $from = $this->_params["{$fieldName}_from"] ?? NULL; | |
357 | $to = $this->_params["{$fieldName}_to"] ?? NULL; | |
6a488035 TO |
358 | |
359 | if ($relative || $from || $to) { | |
360 | $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']); | |
361 | } | |
362 | } | |
363 | else { | |
9c1bc317 | 364 | $op = $this->_params["{$fieldName}_op"] ?? NULL; |
6a488035 TO |
365 | if ($op) { |
366 | $clause = $this->whereClause($field, | |
367 | $op, | |
368 | CRM_Utils_Array::value("{$fieldName}_value", $this->_params), | |
369 | CRM_Utils_Array::value("{$fieldName}_min", $this->_params), | |
370 | CRM_Utils_Array::value("{$fieldName}_max", $this->_params) | |
371 | ); | |
9d72cede EM |
372 | if (($fieldName == 'contribution_status_id' || |
373 | $fieldName == 'financial_type_id') && !empty($clause) | |
374 | ) { | |
6a488035 TO |
375 | $this->_statusClause .= " AND " . $clause; |
376 | } | |
377 | } | |
378 | } | |
379 | ||
380 | if (!empty($clause)) { | |
381 | $clauses[] = $clause; | |
382 | } | |
383 | } | |
384 | } | |
385 | } | |
386 | ||
387 | $this->_where = "WHERE " . implode(' AND ', $clauses); | |
388 | ||
389 | if ($this->_aclWhere) { | |
390 | $this->_where .= " AND {$this->_aclWhere} "; | |
391 | } | |
392 | } | |
393 | ||
00be9182 | 394 | public function groupBy() { |
6a488035 | 395 | $this->assign('chartSupported', TRUE); |
d1641c51 | 396 | $fiscalYearOffset = self::fiscalYearOffset("{$this->_aliases['civicrm_contribution']}.receive_date"); |
397 | $this->_groupBy = "GROUP BY {$this->_aliases['civicrm_contribution']}.contact_id, {$fiscalYearOffset}"; | |
be2fb01f | 398 | $this->_select = CRM_Contact_BAO_Query::appendAnyValueToSelect($this->_selectClauses, ["{$this->_aliases['civicrm_contribution']}.contact_id", $fiscalYearOffset]); |
d1641c51 | 399 | $this->_groupBy .= " {$this->_rollup}"; |
6a488035 TO |
400 | } |
401 | ||
74cf4551 | 402 | /** |
71d8f758 | 403 | * @param array $rows |
74cf4551 EM |
404 | * |
405 | * @return array | |
406 | */ | |
00be9182 | 407 | public function statistics(&$rows) { |
6a488035 TO |
408 | $statistics = parent::statistics($rows); |
409 | ||
410 | if (!empty($rows)) { | |
411 | $select = " | |
39eb89f4 | 412 | SELECT |
6a488035 TO |
413 | SUM({$this->_aliases['civicrm_contribution']}.total_amount ) as amount "; |
414 | ||
415 | $sql = "{$select} {$this->_from} {$this->_where}"; | |
416 | $dao = CRM_Core_DAO::executeQuery($sql); | |
417 | if ($dao->fetch()) { | |
be2fb01f | 418 | $statistics['counts']['amount'] = [ |
6a488035 | 419 | 'value' => $dao->amount, |
ccc29f8e | 420 | 'title' => ts('Total LifeTime'), |
6a488035 | 421 | 'type' => CRM_Utils_Type::T_MONEY, |
be2fb01f | 422 | ]; |
6a488035 TO |
423 | } |
424 | } | |
425 | return $statistics; | |
426 | } | |
427 | ||
00be9182 | 428 | public function postProcess() { |
6a488035 TO |
429 | // get ready with post process params |
430 | $this->beginPostProcess(); | |
6a488035 | 431 | $this->buildACLClause($this->_aliases['civicrm_contact']); |
8ea1d58d | 432 | $this->buildQuery(); |
d4ed4646 | 433 | |
be2fb01f | 434 | $rows = $contactIds = []; |
a7488080 | 435 | if (empty($this->_params['charts'])) { |
6a488035 TO |
436 | $this->limit(); |
437 | $getContacts = "SELECT SQL_CALC_FOUND_ROWS {$this->_aliases['civicrm_contact']}.id as cid {$this->_from} {$this->_where} GROUP BY {$this->_aliases['civicrm_contact']}.id {$this->_limit}"; | |
438 | ||
439 | $dao = CRM_Core_DAO::executeQuery($getContacts); | |
440 | ||
441 | while ($dao->fetch()) { | |
442 | $contactIds[] = $dao->cid; | |
443 | } | |
6a488035 TO |
444 | $this->setPager(); |
445 | } | |
446 | ||
8cc574cf | 447 | if (!empty($contactIds) || !empty($this->_params['charts'])) { |
a7488080 | 448 | if (!empty($this->_params['charts'])) { |
6a488035 TO |
449 | $sql = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy}"; |
450 | } | |
451 | else { | |
6c552737 | 452 | $sql = "" . |
9d72cede EM |
453 | "{$this->_select} {$this->_from} WHERE {$this->_aliases['civicrm_contact']}.id IN (" . |
454 | implode(',', $contactIds) . | |
9f108b4d | 455 | ") AND {$this->_aliases['civicrm_contribution']}.is_test = 0 AND {$this->_aliases['civicrm_contribution']}.is_template = 0 {$this->_statusClause} {$this->_groupBy} "; |
6a488035 TO |
456 | } |
457 | ||
9d72cede EM |
458 | $current_year = $this->_params['yid_value']; |
459 | $previous_year = $current_year - 1; | |
460 | $previous_pyear = $current_year - 2; | |
6a488035 | 461 | $previous_ppyear = $current_year - 3; |
9d72cede | 462 | $upTo_year = $current_year - 4; |
6a488035 | 463 | |
be2fb01f | 464 | $rows = $row = []; |
9d72cede | 465 | $dao = CRM_Core_DAO::executeQuery($sql); |
6a488035 | 466 | $contributionSum = 0; |
be2fb01f | 467 | $yearcal = []; |
6a488035 TO |
468 | while ($dao->fetch()) { |
469 | if (!$dao->civicrm_contribution_contact_id) { | |
470 | continue; | |
471 | } | |
be2fb01f | 472 | $row = []; |
6a488035 TO |
473 | foreach ($this->_columnHeaders as $key => $value) { |
474 | if (property_exists($dao, $key)) { | |
475 | $rows[$dao->civicrm_contribution_contact_id][$key] = $dao->$key; | |
476 | } | |
477 | } | |
478 | if ($dao->civicrm_contribution_receive_date) { | |
479 | if ($dao->civicrm_contribution_receive_date > $upTo_year) { | |
480 | $contributionSum += $dao->civicrm_contribution_total_amount; | |
db36f066 | 481 | $rows[$dao->civicrm_contribution_contact_id]['year_' . $dao->civicrm_contribution_receive_date] = $dao->civicrm_contribution_total_amount; |
6a488035 TO |
482 | } |
483 | } | |
484 | else { | |
485 | $rows[$dao->civicrm_contribution_contact_id]['civicrm_life_time_total'] = $dao->civicrm_contribution_total_amount; | |
9d72cede EM |
486 | if (($dao->civicrm_contribution_total_amount - $contributionSum) > 0 |
487 | ) { | |
6c552737 TO |
488 | $rows[$dao->civicrm_contribution_contact_id]["civicrm_upto_{$upTo_year}"] |
489 | = $dao->civicrm_contribution_total_amount - $contributionSum; | |
6a488035 TO |
490 | } |
491 | $contributionSum = 0; | |
492 | } | |
493 | } | |
6a488035 TO |
494 | } |
495 | // format result set. | |
496 | $this->formatDisplay($rows, FALSE); | |
497 | ||
498 | // assign variables to templates | |
499 | $this->doTemplateAssignment($rows); | |
500 | ||
501 | // do print / pdf / instance stuff if needed | |
502 | $this->endPostProcess($rows); | |
503 | } | |
504 | ||
74cf4551 EM |
505 | /** |
506 | * @param $rows | |
507 | */ | |
00be9182 | 508 | public function buildChart(&$rows) { |
be2fb01f | 509 | $graphRows = []; |
9d72cede EM |
510 | $count = 0; |
511 | $current_year = $this->_params['yid_value']; | |
512 | $previous_year = $current_year - 1; | |
513 | $previous_two_year = $current_year - 2; | |
6a488035 | 514 | $previous_three_year = $current_year - 3; |
9d72cede | 515 | $upto = $current_year - 4; |
6a488035 TO |
516 | |
517 | $interval[$previous_year] = $previous_year; | |
518 | $interval[$previous_two_year] = $previous_two_year; | |
519 | $interval[$previous_three_year] = $previous_three_year; | |
520 | $interval["upto_{$upto}"] = "Up To {$upto}"; | |
521 | ||
522 | foreach ($rows as $key => $row) { | |
6c552737 | 523 | $display["upto_{$upto}"] |
ccc29f8e | 524 | = CRM_Utils_Array::value("upto_{$upto}", $display) + CRM_Utils_Array::value("civicrm_upto_{$upto}", $row); |
6c552737 | 525 | $display[$previous_year] |
ccc29f8e | 526 | = CRM_Utils_Array::value($previous_year, $display) + CRM_Utils_Array::value($previous_year, $row); |
6c552737 | 527 | $display[$previous_two_year] |
ccc29f8e | 528 | = CRM_Utils_Array::value($previous_two_year, $display) + CRM_Utils_Array::value($previous_two_year, $row); |
6c552737 | 529 | $display[$previous_three_year] |
ccc29f8e | 530 | = CRM_Utils_Array::value($previous_three_year, $display) + CRM_Utils_Array::value($previous_three_year, $row); |
6a488035 TO |
531 | } |
532 | ||
533 | $graphRows['value'] = $display; | |
9d72cede | 534 | $config = CRM_Core_Config::Singleton(); |
be2fb01f | 535 | $chartInfo = [ |
e60e0c01 | 536 | 'legend' => ts('Sybunt Report'), |
537 | 'xname' => ts('Year'), | |
be2fb01f CW |
538 | 'yname' => ts('Amount (%1)', [1 => $config->defaultCurrency]), |
539 | ]; | |
6a488035 TO |
540 | if ($this->_params['charts']) { |
541 | // build the chart. | |
dc61ee93 | 542 | CRM_Utils_Chart::reportChart($graphRows, $this->_params['charts'], $interval, $chartInfo); |
6a488035 TO |
543 | $this->assign('chartType', $this->_params['charts']); |
544 | } | |
545 | } | |
546 | ||
74cf4551 | 547 | /** |
ced9bfed EM |
548 | * Alter display of rows. |
549 | * | |
550 | * Iterate through the rows retrieved via SQL and make changes for display purposes, | |
551 | * such as rendering contacts as links. | |
552 | * | |
553 | * @param array $rows | |
554 | * Rows generated by SQL, with an array for each row. | |
74cf4551 | 555 | */ |
00be9182 | 556 | public function alterDisplay(&$rows) { |
72a9af52 | 557 | $entryFound = FALSE; |
6a488035 TO |
558 | |
559 | foreach ($rows as $rowNum => $row) { | |
560 | //Convert Display name into link | |
561 | if (array_key_exists('civicrm_contact_sort_name', $row) && | |
562 | array_key_exists('civicrm_contribution_contact_id', $row) | |
563 | ) { | |
564 | $url = CRM_Report_Utils_Report::getNextUrl('contribute/detail', | |
9d72cede EM |
565 | 'reset=1&force=1&id_op=eq&id_value=' . |
566 | $row['civicrm_contribution_contact_id'], | |
6a488035 TO |
567 | $this->_absoluteUrl, $this->_id, $this->_drilldownReport |
568 | ); | |
569 | $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url; | |
570 | $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contribution Details for this Contact."); | |
72a9af52 | 571 | $entryFound = TRUE; |
6a488035 | 572 | } |
f813f78e | 573 | |
d62fab33 RN |
574 | // convert campaign_id to campaign title |
575 | if (array_key_exists('civicrm_contribution_campaign_id', $row)) { | |
576 | if ($value = $row['civicrm_contribution_campaign_id']) { | |
1be29aad | 577 | $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->campaigns[$value]; |
d62fab33 RN |
578 | $entryFound = TRUE; |
579 | } | |
580 | } | |
a27c90f2 | 581 | |
72a9af52 | 582 | $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'contribute/detail', 'List all contribution(s)') ? TRUE : $entryFound; |
9b43eb6c | 583 | $entryFound = $this->alterDisplayContactFields($row, $rows, $rowNum, NULL, 'List all contribution(s)') ? TRUE : $entryFound; |
5e0343e8 | 584 | if (!empty($row['civicrm_financial_trxn_card_type_id'])) { |
585 | $rows[$rowNum]['civicrm_financial_trxn_card_type_id'] = $this->getLabels($row['civicrm_financial_trxn_card_type_id'], 'CRM_Financial_DAO_FinancialTrxn', 'card_type_id'); | |
0ddcd8e9 E |
586 | $entryFound = TRUE; |
587 | } | |
588 | ||
72a9af52 NG |
589 | // skip looking further in rows, if first row itself doesn't |
590 | // have the column we need | |
591 | if (!$entryFound) { | |
592 | break; | |
a27c90f2 | 593 | } |
6a488035 TO |
594 | } |
595 | } | |
596 | ||
74cf4551 | 597 | /** |
4f1f1f2a | 598 | * Override "This Year" $op options |
74cf4551 EM |
599 | * @param string $type |
600 | * @param null $fieldName | |
601 | * | |
602 | * @return array | |
603 | */ | |
00be9182 | 604 | public function getOperationPair($type = "string", $fieldName = NULL) { |
6a488035 | 605 | if ($fieldName == 'yid') { |
be2fb01f | 606 | return [ |
9d72cede | 607 | 'calendar' => ts('Is Calendar Year'), |
21dfd5f5 | 608 | 'fiscal' => ts('Fiscal Year Starting'), |
be2fb01f | 609 | ]; |
6a488035 TO |
610 | } |
611 | return parent::getOperationPair($type, $fieldName); | |
612 | } | |
96025800 | 613 | |
6a488035 | 614 | } |