Merge pull request #15813 from eileenmcnaughton/fee
[civicrm-core.git] / CRM / Contribute / Page / UserDashboard.php
CommitLineData
6a488035
TO
1<?php
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 */
17class CRM_Contribute_Page_UserDashboard extends CRM_Contact_Page_View_UserDashBoard {
18
19 /**
fe482240 20 * called when action is browse.
6a488035 21 */
00be9182 22 public function listContribution() {
8bfce657 23 $rows = civicrm_api3('Contribution', 'get', [
4a94e641
AH
24 'options' => [
25 'limit' => 12,
26 'sort' => 'receive_date DESC',
27 ],
8bfce657 28 'sequential' => 1,
29 'contact_id' => $this->_contactId,
30 'return' => [
31 'total_amount',
32 'contribution_recur_id',
33 'financial_type',
34 'receive_date',
35 'receipt_date',
36 'contribution_status',
37 'currency',
38 'amount_level',
39 'contact_id,',
40 'contribution_source',
41 ],
42 ])['values'];
43
4a94e641
AH
44 // We want oldest first, just among the most recent contributions
45 $rows = array_reverse($rows);
46
10f949e6 47 foreach ($rows as $index => &$row) {
8bfce657 48 // This is required for tpl logic. We should move away from hard-code this to adding an array of actions to the row
49 // which the tpl can iterate through - this should allow us to cope with competing attempts to add new buttons
50 // and allow extensions to assign new ones through the pageRun hook
10f949e6 51 if ('Pending' === CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $row['contribution_status_id'])) {
52 $row['buttons']['pay'] = [
53 'class' => 'button',
54 'label' => ts('Pay Now'),
55 'url' => CRM_Utils_System::url('civicrm/contribute/transact', [
56 'reset' => 1,
57 'id' => CRM_Invoicing_Utils::getDefaultPaymentPage(),
58 'ccid' => $row['contribution_id'],
59 'cs' => $this->getUserChecksum(),
60 'cid' => $row['contact_id'],
5ba7d840 61 ]),
10f949e6 62 ];
63 }
8bfce657 64 }
65
66 $this->assign('contribute_rows', $rows);
67 $this->assign('contributionSummary', ['total_amount' => civicrm_api3('Contribution', 'getcount', ['contact_id' => $this->_contactId])]);
6a488035
TO
68
69 //add honor block
6a488035
TO
70 $params = CRM_Contribute_BAO_Contribution::getHonorContacts($this->_contactId);
71
72 if (!empty($params)) {
73 // assign vars to templates
74 $this->assign('honorRows', $params);
75 $this->assign('honor', TRUE);
76 }
77
353ffa53 78 $recur = new CRM_Contribute_DAO_ContributionRecur();
6a488035 79 $recur->contact_id = $this->_contactId;
353ffa53 80 $recur->is_test = 0;
6a488035
TO
81 $recur->find();
82
c3b82060 83 $recurStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'label');
6a488035 84
be2fb01f
CW
85 $recurRow = [];
86 $recurIDs = [];
6a488035 87 while ($recur->fetch()) {
1a18d24f 88 if (empty($recur->payment_processor_id)) {
89 // it's not clear why we continue here as any without a processor id would likely
90 // be imported from another system & still seem valid.
6a488035
TO
91 continue;
92 }
93
6a488035
TO
94 require_once 'api/v3/utils.php';
95 //@todo calling api functions directly is not supported
96 _civicrm_api3_object_to_array($recur, $values);
97
98 $values['recur_status'] = $recurStatus[$values['contribution_status_id']];
99 $recurRow[$values['id']] = $values;
100
101 $action = array_sum(array_keys(CRM_Contribute_Page_Tab::recurLinks($recur->id, 'dashboard')));
102
103 $details = CRM_Contribute_BAO_ContributionRecur::getSubscriptionDetails($recur->id, 'recur');
104 $hideUpdate = $details->membership_id & $details->auto_renew;
105
106 if ($hideUpdate) {
107 $action -= CRM_Core_Action::UPDATE;
108 }
109
110 $recurRow[$values['id']]['action'] = CRM_Core_Action::formLink(CRM_Contribute_Page_Tab::recurLinks($recur->id, 'dashboard'),
be2fb01f 111 $action, [
6a488035
TO
112 'cid' => $this->_contactId,
113 'crid' => $values['id'],
114 'cxt' => 'contribution',
be2fb01f 115 ],
87dab4a4
AH
116 ts('more'),
117 FALSE,
118 'contribution.dashboard.recurring',
119 'Contribution',
120 $values['id']
6a488035
TO
121 );
122
123 $recurIDs[] = $values['id'];
6a488035
TO
124 }
125 if (is_array($recurIDs) && !empty($recurIDs)) {
126 $getCount = CRM_Contribute_BAO_ContributionRecur::getCount($recurIDs);
127 foreach ($getCount as $key => $val) {
128 $recurRow[$key]['completed'] = $val;
129 $recurRow[$key]['link'] = CRM_Utils_System::url('civicrm/contribute/search',
130 "reset=1&force=1&recur=$key"
131 );
132 }
133 }
134
135 $this->assign('recurRows', $recurRow);
136 if (!empty($recurRow)) {
137 $this->assign('recur', TRUE);
138 }
139 else {
140 $this->assign('recur', FALSE);
141 }
142 }
143
10f949e6 144 /**
145 * Should invoice links be displayed on the template.
146 *
147 * @todo This should be moved to a hook-like structure on the invoicing class
148 * (currently CRM_Utils_Invoicing) with a view to possible removal from core.
149 */
150 public function isIncludeInvoiceLinks() {
151 if (!CRM_Invoicing_Utils::isInvoicingEnabled()) {
152 return FALSE;
153 }
154 $dashboardOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
155 'user_dashboard_options'
156 );
157 return $dashboardOptions['Invoices / Credit Notes'];
158 }
159
6a488035 160 /**
dc195289 161 * the main function that is called when the page
6a488035 162 * loads, it decides the which action has to be taken for the page.
6a488035 163 */
00be9182 164 public function run() {
10f949e6 165 $this->assign('isIncludeInvoiceLinks', $this->isIncludeInvoiceLinks());
6a488035
TO
166 parent::preProcess();
167 $this->listContribution();
168 }
96025800 169
6a488035 170}