CRM-20773: Contribution tab shows Receive Date twice instead of Thank You date
[civicrm-core.git] / CRM / Contribute / Selector / Search.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * @package CRM
30 * @copyright CiviCRM LLC (c) 2004-2017
31 */
32
33 /**
34 * Class to render contribution search results.
35 */
36 class CRM_Contribute_Selector_Search extends CRM_Core_Selector_Base implements CRM_Core_Selector_API {
37
38 /**
39 * Array of action links.
40 *
41 * @var array
42 */
43 static $_links = NULL;
44
45 /**
46 * We use desc to remind us what that column is, name is used in the tpl
47 *
48 * @var array
49 */
50 static $_columnHeaders;
51
52 /**
53 * Properties of contact we're interested in displaying
54 * @var array
55 */
56 static $_properties = array(
57 'contact_id',
58 'contribution_id',
59 'contact_type',
60 'sort_name',
61 'amount_level',
62 'total_amount',
63 'financial_type',
64 'contribution_source',
65 'receive_date',
66 'thankyou_date',
67 'contribution_status_id',
68 'contribution_status',
69 'cancel_date',
70 'product_name',
71 'is_test',
72 'contribution_recur_id',
73 'receipt_date',
74 'membership_id',
75 'currency',
76 'contribution_campaign_id',
77 'contribution_soft_credit_name',
78 'contribution_soft_credit_contact_id',
79 'contribution_soft_credit_amount',
80 'contribution_soft_credit_type',
81 );
82
83 /**
84 * Are we restricting ourselves to a single contact
85 *
86 * @var boolean
87 */
88 protected $_single = FALSE;
89
90 /**
91 * Are we restricting ourselves to a single contact
92 *
93 * @var boolean
94 */
95 protected $_limit = NULL;
96
97 /**
98 * What context are we being invoked from
99 *
100 * @var string
101 */
102 protected $_context = NULL;
103
104 /**
105 * What component context are we being invoked from
106 *
107 * @var string
108 */
109 protected $_compContext = NULL;
110
111 /**
112 * QueryParams is the array returned by exportValues called on
113 * the HTML_QuickForm_Controller for that page.
114 *
115 * @var array
116 */
117 public $_queryParams;
118
119 /**
120 * Represent the type of selector
121 *
122 * @var int
123 */
124 protected $_action;
125
126 /**
127 * The additional clause that we restrict the search with
128 *
129 * @var string
130 */
131 protected $_contributionClause = NULL;
132
133 /**
134 * The query object
135 *
136 * @var string
137 */
138 protected $_query;
139
140 protected $_includeSoftCredits = FALSE;
141
142 /**
143 * Class constructor.
144 *
145 * @param array $queryParams
146 * Array of parameters for query.
147 * @param \const|int $action - action of search basic or advanced.
148 * @param string $contributionClause
149 * If the caller wants to further restrict the search (used in contributions).
150 * @param bool $single
151 * Are we dealing only with one contact?.
152 * @param int $limit
153 * How many contributions do we want returned.
154 *
155 * @param string $context
156 * @param null $compContext
157 *
158 * @return \CRM_Contribute_Selector_Search
159 */
160 public function __construct(
161 &$queryParams,
162 $action = CRM_Core_Action::NONE,
163 $contributionClause = NULL,
164 $single = FALSE,
165 $limit = NULL,
166 $context = 'search',
167 $compContext = NULL
168 ) {
169
170 // submitted form values
171 $this->_queryParams = &$queryParams;
172
173 $this->_single = $single;
174 $this->_limit = $limit;
175 $this->_context = $context;
176 $this->_compContext = $compContext;
177
178 $this->_contributionClause = $contributionClause;
179
180 // type of selector
181 $this->_action = $action;
182 $returnProperties = CRM_Contribute_BAO_Query::selectorReturnProperties();
183 $this->_includeSoftCredits = CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled($this->_queryParams);
184 $this->_query = new CRM_Contact_BAO_Query(
185 $this->_queryParams,
186 $returnProperties,
187 NULL, FALSE, FALSE,
188 CRM_Contact_BAO_Query::MODE_CONTRIBUTE
189 );
190 // @todo the function CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled should handle this
191 // can we remove? if not why not?
192 if ($this->_includeSoftCredits) {
193 $this->_query->_rowCountClause = " count(civicrm_contribution.id)";
194 $this->_query->_groupByComponentClause = " GROUP BY contribution_search_scredit_combined.id, contribution_search_scredit_combined.contact_id, contribution_search_scredit_combined.scredit_id ";
195 }
196 else {
197 $this->_query->_distinctComponentClause = " civicrm_contribution.id";
198 $this->_query->_groupByComponentClause = " GROUP BY civicrm_contribution.id ";
199 }
200 }
201
202 /**
203 * This method returns the links that are given for each search row.
204 * currently the links added for each row are
205 *
206 * - View
207 * - Edit
208 *
209 * @param int $componentId
210 * @param null $componentAction
211 * @param null $key
212 * @param null $compContext
213 *
214 * @return array
215 */
216 public static function &links($componentId = NULL, $componentAction = NULL, $key = NULL, $compContext = NULL) {
217 $extraParams = NULL;
218 if ($componentId) {
219 $extraParams = "&compId={$componentId}&compAction={$componentAction}";
220 }
221 if ($compContext) {
222 $extraParams .= "&compContext={$compContext}";
223 }
224 if ($key) {
225 $extraParams .= "&key={$key}";
226 }
227
228 if (!(self::$_links)) {
229 self::$_links = array(
230 CRM_Core_Action::VIEW => array(
231 'name' => ts('View'),
232 'url' => 'civicrm/contact/view/contribution',
233 'qs' => "reset=1&id=%%id%%&cid=%%cid%%&action=view&context=%%cxt%%&selectedChild=contribute{$extraParams}",
234 'title' => ts('View Contribution'),
235 ),
236 CRM_Core_Action::UPDATE => array(
237 'name' => ts('Edit'),
238 'url' => 'civicrm/contact/view/contribution',
239 'qs' => "reset=1&action=update&id=%%id%%&cid=%%cid%%&context=%%cxt%%{$extraParams}",
240 'title' => ts('Edit Contribution'),
241 ),
242 CRM_Core_Action::DELETE => array(
243 'name' => ts('Delete'),
244 'url' => 'civicrm/contact/view/contribution',
245 'qs' => "reset=1&action=delete&id=%%id%%&cid=%%cid%%&context=%%cxt%%{$extraParams}",
246 'title' => ts('Delete Contribution'),
247 ),
248 );
249 }
250 return self::$_links;
251 }
252
253 /**
254 * Getter for array of the parameters required for creating pager.
255 *
256 * @param $action
257 * @param array $params
258 */
259 public function getPagerParams($action, &$params) {
260 $params['status'] = ts('Contribution') . ' %%StatusMessage%%';
261 $params['csvString'] = NULL;
262 if ($this->_limit) {
263 $params['rowCount'] = $this->_limit;
264 }
265 else {
266 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
267 }
268
269 $params['buttonTop'] = 'PagerTopButton';
270 $params['buttonBottom'] = 'PagerBottomButton';
271 }
272
273 /**
274 * Returns total number of rows for the query.
275 *
276 * @param string $action
277 *
278 * @return int
279 * Total number of rows
280 */
281 public function getTotalCount($action) {
282 return $this->_query->searchQuery(0, 0, NULL,
283 TRUE, FALSE,
284 FALSE, FALSE,
285 FALSE,
286 $this->_contributionClause
287 );
288 }
289
290 /**
291 * Returns all the rows in the given offset and rowCount.
292 *
293 * @param string $action
294 * The action being performed.
295 * @param int $offset
296 * The row number to start from.
297 * @param int $rowCount
298 * The number of rows to return.
299 * @param string $sort
300 * The sql string that describes the sort order.
301 * @param string $output
302 * What should the result set include (web/email/csv).
303 *
304 * @return int
305 * the total number of rows for this action
306 */
307 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
308 if ($this->_includeSoftCredits) {
309 // especial sort order when rows include soft credits
310 $sort = $sort->orderBy() . ", civicrm_contribution.id, civicrm_contribution_soft.id";
311 }
312 $result = $this->_query->searchQuery($offset, $rowCount, $sort,
313 FALSE, FALSE,
314 FALSE, FALSE,
315 FALSE,
316 $this->_contributionClause
317 );
318 // process the result of the query
319 $rows = array();
320
321 //CRM-4418 check for view/edit/delete
322 $permissions = array(CRM_Core_Permission::VIEW);
323 if (CRM_Core_Permission::check('edit contributions')) {
324 $permissions[] = CRM_Core_Permission::EDIT;
325 }
326 if (CRM_Core_Permission::check('delete in CiviContribute')) {
327 $permissions[] = CRM_Core_Permission::DELETE;
328 }
329 $mask = CRM_Core_Action::mask($permissions);
330
331 $qfKey = $this->_key;
332 $componentId = $componentContext = NULL;
333 if ($this->_context != 'contribute') {
334 // @todo explain the significance of context & why we do not get these i that context.
335 $qfKey = CRM_Utils_Request::retrieve('key', 'String');
336 $componentId = CRM_Utils_Request::retrieve('id', 'Positive');
337 $componentAction = CRM_Utils_Request::retrieve('action', 'String');
338 $componentContext = CRM_Utils_Request::retrieve('compContext', 'String');
339
340 if (!$componentContext &&
341 $this->_compContext
342 ) {
343 // @todo explain when this condition might occur.
344 $componentContext = $this->_compContext;
345 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', CRM_Core_DAO::$_nullObject, NULL, FALSE, 'REQUEST');
346 }
347 // CRM-17628 for some reason qfKey is not always set when searching from contribution search.
348 // as a result if the edit link is opened using right-click + open in new tab
349 // then the browser is not returned to the search results on save.
350 // This is an effort to getting the qfKey without, sadly, understanding the intent of those who came before me.
351 if (empty($qfKey)) {
352 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', CRM_Core_DAO::$_nullObject, NULL, FALSE, 'REQUEST');
353 }
354 }
355
356 // get all contribution status
357 $contributionStatuses = CRM_Core_OptionGroup::values('contribution_status',
358 FALSE, FALSE, FALSE, NULL, 'name', FALSE
359 );
360
361 //get all campaigns.
362 $allCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
363
364 while ($result->fetch()) {
365 $this->_query->convertToPseudoNames($result);
366 $links = self::links($componentId,
367 $componentAction,
368 $qfKey,
369 $componentContext
370 );
371 $checkLineItem = FALSE;
372 $row = array();
373 // Now check for lineItems
374 if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus()) {
375 $lineItems = CRM_Price_BAO_LineItem::getLineItemsByContributionID($result->id);
376 foreach ($lineItems as $items) {
377 if (!CRM_Core_Permission::check('view contributions of type ' . CRM_Contribute_PseudoConstant::financialType($items['financial_type_id']))) {
378 $checkLineItem = TRUE;
379 break;
380 }
381 if (!CRM_Core_Permission::check('edit contributions of type ' . CRM_Contribute_PseudoConstant::financialType($items['financial_type_id']))) {
382 unset($links[CRM_Core_Action::UPDATE]);
383 }
384 if (!CRM_Core_Permission::check('delete contributions of type ' . CRM_Contribute_PseudoConstant::financialType($items['financial_type_id']))) {
385 unset($links[CRM_Core_Action::DELETE]);
386 }
387 }
388 if ($checkLineItem) {
389 continue;
390 }
391 if (!CRM_Core_Permission::check('edit contributions of type ' . CRM_Contribute_PseudoConstant::financialType($result->financial_type_id))) {
392 unset($links[CRM_Core_Action::UPDATE]);
393 }
394 if (!CRM_Core_Permission::check('delete contributions of type ' . CRM_Contribute_PseudoConstant::financialType($result->financial_type_id))) {
395 unset($links[CRM_Core_Action::DELETE]);
396 }
397 }
398 // the columns we are interested in
399 foreach (self::$_properties as $property) {
400 if (property_exists($result, $property)) {
401 $row[$property] = $result->$property;
402 }
403 }
404
405 //carry campaign on selectors.
406 // @todo - I can't find any evidence that 'carrying' the campaign on selectors actually
407 // results in it being displayed anywhere so why do we do this???
408 $row['campaign'] = CRM_Utils_Array::value($result->contribution_campaign_id, $allCampaigns);
409 $row['campaign_id'] = $result->contribution_campaign_id;
410
411 // add contribution status name
412 $row['contribution_status_name'] = CRM_Utils_Array::value($row['contribution_status_id'],
413 $contributionStatuses
414 );
415
416 $isPayLater = FALSE;
417 if ($result->is_pay_later && CRM_Utils_Array::value('contribution_status_name', $row) == 'Pending') {
418 $isPayLater = TRUE;
419 $row['contribution_status'] .= ' (' . ts('Pay Later') . ')';
420 $links[CRM_Core_Action::ADD] = array(
421 'name' => ts('Pay with Credit Card'),
422 'url' => 'civicrm/contact/view/contribution',
423 'qs' => 'reset=1&action=update&id=%%id%%&cid=%%cid%%&context=%%cxt%%&mode=live',
424 'title' => ts('Pay with Credit Card'),
425 );
426 }
427 elseif (CRM_Utils_Array::value('contribution_status_name', $row) == 'Pending') {
428 $row['contribution_status'] .= ' (' . ts('Incomplete Transaction') . ')';
429 }
430
431 if ($row['is_test']) {
432 $row['financial_type'] = $row['financial_type'] . ' (' . ts('test') . ')';
433 }
434
435 $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->contribution_id;
436
437 $actions = array(
438 'id' => $result->contribution_id,
439 'cid' => $result->contact_id,
440 'cxt' => $this->_context,
441 );
442
443 if (in_array($row['contribution_status_name'], array('Partially paid', 'Pending refund')) || $isPayLater) {
444 $buttonName = ts('Record Payment');
445 if ($row['contribution_status_name'] == 'Pending refund') {
446 $buttonName = ts('Record Refund');
447 }
448 elseif (CRM_Core_Config::isEnabledBackOfficeCreditCardPayments()) {
449 $links[CRM_Core_Action::BASIC] = array(
450 'name' => ts('Submit Credit Card payment'),
451 'url' => 'civicrm/payment/add',
452 'qs' => 'reset=1&id=%%id%%&cid=%%cid%%&action=add&component=contribution&mode=live',
453 'title' => ts('Submit Credit Card payment'),
454 );
455 }
456 $links[CRM_Core_Action::ADD] = array(
457 'name' => $buttonName,
458 'url' => 'civicrm/payment',
459 'qs' => 'reset=1&id=%%id%%&cid=%%cid%%&action=add&component=contribution',
460 'title' => $buttonName,
461 );
462 }
463
464 $row['action'] = CRM_Core_Action::formLink(
465 $links,
466 $mask, $actions,
467 ts('more'),
468 FALSE,
469 'contribution.selector.row',
470 'Contribution',
471 $result->contribution_id
472 );
473
474 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ? $result->contact_sub_type : $result->contact_type, FALSE, $result->contact_id
475 );
476
477 if (!empty($row['amount_level'])) {
478 CRM_Event_BAO_Participant::fixEventLevel($row['amount_level']);
479 }
480
481 $rows[] = $row;
482 }
483
484 return $rows;
485 }
486
487 /**
488 * @inheritDoc
489 */
490 public function getQILL() {
491 return $this->_query->qill();
492 }
493
494 /**
495 * Returns the column headers as an array of tuples:
496 * (name, sortName (key to the sort array))
497 *
498 * @param string $action
499 * The action being performed.
500 * @param string $output
501 * What should the result set include (web/email/csv).
502 *
503 * @return array
504 * the column headers that need to be displayed
505 */
506 public function &getColumnHeaders($action = NULL, $output = NULL) {
507 $pre = array();
508 self::$_columnHeaders = array(
509 array(
510 'name' => $this->_includeSoftCredits ? ts('Contribution Amount') : ts('Amount'),
511 'sort' => 'total_amount',
512 'direction' => CRM_Utils_Sort::DONTCARE,
513 'field_name' => 'total_amount',
514 ),
515 );
516 if ($this->_includeSoftCredits) {
517 self::$_columnHeaders
518 = array_merge(
519 self::$_columnHeaders,
520 array(
521 array(
522 'name' => ts('Soft Credit Amount'),
523 'sort' => 'contribution_soft_credit_amount',
524 'field_name' => 'contribution_soft_credit_amount',
525 'direction' => CRM_Utils_Sort::DONTCARE,
526 ),
527 )
528 );
529 }
530 self::$_columnHeaders
531 = array_merge(
532 self::$_columnHeaders,
533 array(
534 array(
535 'name' => ts('Type'),
536 'sort' => 'financial_type',
537 'field_name' => 'financial_type',
538 'direction' => CRM_Utils_Sort::DONTCARE,
539 ),
540 array(
541 'name' => ts('Source'),
542 'sort' => 'contribution_source',
543 'field_name' => 'contribution_source',
544 'direction' => CRM_Utils_Sort::DONTCARE,
545 ),
546 array(
547 'name' => ts('Received'),
548 'sort' => 'receive_date',
549 'field_name' => 'receive_date',
550 'type' => 'date',
551 'direction' => CRM_Utils_Sort::DESCENDING,
552 ),
553 array(
554 'name' => ts('Thank-you Sent'),
555 'sort' => 'thankyou_date',
556 'field_name' => 'thankyou_date',
557 'type' => 'date',
558 'direction' => CRM_Utils_Sort::DONTCARE,
559 ),
560 array(
561 'name' => ts('Status'),
562 'sort' => 'contribution_status',
563 'field_name' => 'contribution_status',
564 'direction' => CRM_Utils_Sort::DONTCARE,
565 ),
566 array(
567 'name' => ts('Premium'),
568 'sort' => 'product_name',
569 'field_name' => 'product_name',
570 'direction' => CRM_Utils_Sort::DONTCARE,
571 ),
572 )
573 );
574 if (!$this->_single) {
575 $pre = array(
576 array(
577 'name' => ts('Name'),
578 'sort' => 'sort_name',
579 'direction' => CRM_Utils_Sort::DONTCARE,
580 ),
581 );
582 }
583 self::$_columnHeaders = array_merge($pre, self::$_columnHeaders);
584 if ($this->_includeSoftCredits) {
585 self::$_columnHeaders = array_merge(
586 self::$_columnHeaders,
587 array(
588 array(
589 'name' => ts('Soft Credit For'),
590 'sort' => 'contribution_soft_credit_name',
591 'direction' => CRM_Utils_Sort::DONTCARE,
592 ),
593 array(
594 'name' => ts('Soft Credit Type'),
595 'sort' => 'contribution_soft_credit_type',
596 'direction' => CRM_Utils_Sort::ASCENDING,
597 ),
598 )
599 );
600 }
601 self::$_columnHeaders
602 = array_merge(
603 self::$_columnHeaders, array(
604 array('desc' => ts('Actions'), 'type' => 'actions'),
605 )
606 );
607 foreach (array_keys(self::$_columnHeaders) as $index) {
608 // Add weight & space it out a bit to allow headers to be inserted.
609 self::$_columnHeaders[$index]['weight'] = $index * 10;
610 }
611
612 CRM_Core_Smarty::singleton()->assign('softCreditColumns', $this->_includeSoftCredits);
613 return self::$_columnHeaders;
614 }
615
616 /**
617 * @return mixed
618 */
619 public function alphabetQuery() {
620 return $this->_query->searchQuery(NULL, NULL, NULL, FALSE, FALSE, TRUE);
621 }
622
623 /**
624 * @return string
625 */
626 public function &getQuery() {
627 return $this->_query;
628 }
629
630 /**
631 * Name of export file.
632 *
633 * @param string $output
634 * Type of output.
635 *
636 * @return string
637 * name of the file
638 */
639 public function getExportFileName($output = 'csv') {
640 return ts('CiviCRM Contribution Search');
641 }
642
643 /**
644 * @return mixed
645 */
646 public function getSummary() {
647 return $this->_query->summaryContribution($this->_context);
648 }
649
650 }