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