Merge pull request #23741 from ufundo/entitybatchcurrency
[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
dbfe939a
JG
51 $row['balance_amount'] = CRM_Contribute_BAO_Contribution::getContributionBalance($row['contribution_id']);
52 $contributionStatus = CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $row['contribution_status_id']);
53
54 if (in_array($contributionStatus, ['Pending', 'Partially paid'])) {
10f949e6 55 $row['buttons']['pay'] = [
56 'class' => 'button',
57 'label' => ts('Pay Now'),
58 'url' => CRM_Utils_System::url('civicrm/contribute/transact', [
59 'reset' => 1,
ae1c8b1f 60 'id' => Civi::settings()->get('default_invoice_page'),
10f949e6 61 'ccid' => $row['contribution_id'],
62 'cs' => $this->getUserChecksum(),
63 'cid' => $row['contact_id'],
5ba7d840 64 ]),
10f949e6 65 ];
66 }
8bfce657 67 }
68
69 $this->assign('contribute_rows', $rows);
70 $this->assign('contributionSummary', ['total_amount' => civicrm_api3('Contribution', 'getcount', ['contact_id' => $this->_contactId])]);
6a488035
TO
71
72 //add honor block
6a488035
TO
73 $params = CRM_Contribute_BAO_Contribution::getHonorContacts($this->_contactId);
74
75 if (!empty($params)) {
76 // assign vars to templates
77 $this->assign('honorRows', $params);
78 $this->assign('honor', TRUE);
79 }
80
353ffa53 81 $recur = new CRM_Contribute_DAO_ContributionRecur();
6a488035 82 $recur->contact_id = $this->_contactId;
353ffa53 83 $recur->is_test = 0;
6a488035
TO
84 $recur->find();
85
c3b82060 86 $recurStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'label');
6a488035 87
be2fb01f
CW
88 $recurRow = [];
89 $recurIDs = [];
6a488035 90 while ($recur->fetch()) {
1a18d24f 91 if (empty($recur->payment_processor_id)) {
92 // it's not clear why we continue here as any without a processor id would likely
93 // be imported from another system & still seem valid.
6a488035
TO
94 continue;
95 }
96
6a488035
TO
97 require_once 'api/v3/utils.php';
98 //@todo calling api functions directly is not supported
99 _civicrm_api3_object_to_array($recur, $values);
100
101 $values['recur_status'] = $recurStatus[$values['contribution_status_id']];
102 $recurRow[$values['id']] = $values;
103
2d852c61 104 $action = array_sum(array_keys(CRM_Contribute_Page_Tab::dashboardRecurLinks((int) $recur->id, (int) $recur->contact_id)));
6a488035
TO
105
106 $details = CRM_Contribute_BAO_ContributionRecur::getSubscriptionDetails($recur->id, 'recur');
107 $hideUpdate = $details->membership_id & $details->auto_renew;
108
109 if ($hideUpdate) {
110 $action -= CRM_Core_Action::UPDATE;
111 }
112
2d852c61 113 $recurRow[$values['id']]['action'] = CRM_Core_Action::formLink(CRM_Contribute_Page_Tab::dashboardRecurLinks((int) $recur->id, (int) $this->_contactId),
be2fb01f 114 $action, [
6a488035
TO
115 'cid' => $this->_contactId,
116 'crid' => $values['id'],
117 'cxt' => 'contribution',
be2fb01f 118 ],
87dab4a4
AH
119 ts('more'),
120 FALSE,
121 'contribution.dashboard.recurring',
122 'Contribution',
123 $values['id']
6a488035
TO
124 );
125
126 $recurIDs[] = $values['id'];
6a488035
TO
127 }
128 if (is_array($recurIDs) && !empty($recurIDs)) {
129 $getCount = CRM_Contribute_BAO_ContributionRecur::getCount($recurIDs);
130 foreach ($getCount as $key => $val) {
131 $recurRow[$key]['completed'] = $val;
132 $recurRow[$key]['link'] = CRM_Utils_System::url('civicrm/contribute/search',
133 "reset=1&force=1&recur=$key"
134 );
135 }
136 }
137
138 $this->assign('recurRows', $recurRow);
139 if (!empty($recurRow)) {
140 $this->assign('recur', TRUE);
141 }
142 else {
143 $this->assign('recur', FALSE);
144 }
145 }
146
10f949e6 147 /**
148 * Should invoice links be displayed on the template.
149 *
150 * @todo This should be moved to a hook-like structure on the invoicing class
151 * (currently CRM_Utils_Invoicing) with a view to possible removal from core.
152 */
153 public function isIncludeInvoiceLinks() {
154 if (!CRM_Invoicing_Utils::isInvoicingEnabled()) {
155 return FALSE;
156 }
157 $dashboardOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
158 'user_dashboard_options'
159 );
160 return $dashboardOptions['Invoices / Credit Notes'];
161 }
162
6a488035 163 /**
dc195289 164 * the main function that is called when the page
6a488035 165 * loads, it decides the which action has to be taken for the page.
6a488035 166 */
00be9182 167 public function run() {
10f949e6 168 $this->assign('isIncludeInvoiceLinks', $this->isIncludeInvoiceLinks());
6a488035
TO
169 parent::preProcess();
170 $this->listContribution();
171 }
96025800 172
6a488035 173}