Commit | Line | Data |
---|---|---|
ed28afa9 SL |
1 | <?php |
2 | ||
3 | /** | |
4 | * Shows list of contributions done as payments within a recurring contribution. | |
5 | */ | |
6 | class CRM_Contribute_Page_ContributionRecurPayments extends CRM_Core_Page { | |
7 | ||
8 | /** | |
9 | * Contribution ID | |
10 | * | |
11 | * @var int | |
12 | */ | |
13 | private $id = NULL; | |
14 | ||
15 | /** | |
16 | * Contact ID | |
17 | * | |
18 | * @var int | |
19 | */ | |
20 | private $contactId = NULL; | |
21 | ||
22 | /** | |
23 | * Builds list of contributions for a given recurring contribution. | |
24 | * | |
25 | * @return null | |
26 | */ | |
27 | public function run() { | |
28 | $this->id = CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE); | |
29 | $this->contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE); | |
30 | ||
31 | $this->loadRelatedContributions(); | |
32 | ||
33 | return parent::run(); | |
34 | } | |
35 | ||
36 | /** | |
37 | * Loads contributions associated to the current recurring contribution being | |
38 | * viewed. | |
39 | */ | |
40 | private function loadRelatedContributions() { | |
be2fb01f | 41 | $relatedContributions = []; |
ed28afa9 | 42 | |
be2fb01f | 43 | $relatedContributionsResult = civicrm_api3('Contribution', 'get', [ |
ed28afa9 SL |
44 | 'sequential' => 1, |
45 | 'contribution_recur_id' => $this->id, | |
46 | 'contact_id' => $this->contactId, | |
be2fb01f | 47 | 'options' => ['limit' => 0], |
de09f942 | 48 | 'contribution_test' => '', |
be2fb01f | 49 | ]); |
ed28afa9 SL |
50 | |
51 | foreach ($relatedContributionsResult['values'] as $contribution) { | |
52 | $this->insertAmountExpandingPaymentsControl($contribution); | |
53 | $this->fixDateFormats($contribution); | |
54 | $this->insertStatusLabels($contribution); | |
55 | $this->insertContributionActions($contribution); | |
56 | ||
cd355351 MWMC |
57 | if ($contribution['is_test']) { |
58 | $contribution['financial_type'] = CRM_Core_TestEntity::appendTestText($contribution['financial_type']); | |
59 | } | |
ed28afa9 SL |
60 | $relatedContributions[] = $contribution; |
61 | } | |
62 | ||
63 | if (count($relatedContributions) > 0) { | |
64 | $this->assign('contributionsCount', count($relatedContributions)); | |
65 | $this->assign('relatedContributions', json_encode($relatedContributions)); | |
66 | } | |
67 | } | |
68 | ||
69 | /** | |
70 | * Inserts a string into the array with the html used to show the expanding | |
71 | * payments control, which loads when user clicks on the amount. | |
72 | * | |
73 | * @param array $contribution | |
74 | * Reference to the array holding the contribution's data and where the | |
75 | * control will be inserted into | |
76 | */ | |
77 | private function insertAmountExpandingPaymentsControl(&$contribution) { | |
78 | $amount = CRM_Utils_Money::format($contribution['total_amount'], $contribution['currency']); | |
79 | ||
80 | $expandPaymentsUrl = CRM_Utils_System::url('civicrm/payment', | |
be2fb01f | 81 | [ |
ed28afa9 SL |
82 | 'view' => 'transaction', |
83 | 'component' => 'contribution', | |
84 | 'action' => 'browse', | |
85 | 'cid' => $this->contactId, | |
86 | 'id' => $contribution['contribution_id'], | |
87 | 'selector' => 1, | |
be2fb01f | 88 | ], |
ed28afa9 SL |
89 | FALSE, NULL, TRUE |
90 | ); | |
91 | ||
92 | $contribution['amount_control'] = ' | |
93 | <a class="nowrap bold crm-expand-row" title="view payments" href="' . $expandPaymentsUrl . '"> | |
94 | ' . $amount . ' | |
95 | </a> | |
96 | '; | |
97 | } | |
98 | ||
99 | /** | |
100 | * Fixes date fields present in the given contribution. | |
101 | * | |
102 | * @param array $contribution | |
103 | * Reference to the array holding the contribution's data | |
104 | */ | |
105 | private function fixDateFormats(&$contribution) { | |
106 | $config = CRM_Core_Config::singleton(); | |
107 | ||
108 | $contribution['formatted_receive_date'] = CRM_Utils_Date::customFormat($contribution['receive_date'], $config->dateformatDatetime); | |
109 | $contribution['formatted_thankyou_date'] = CRM_Utils_Date::customFormat($contribution['thankyou_date'], $config->dateformatDatetime); | |
110 | } | |
111 | ||
112 | /** | |
113 | * Inserts a contribution_status_label key into the array, with the value | |
114 | * showing the current status plus observations on the current status. | |
115 | * | |
116 | * @param array $contribution | |
117 | * Reference to the array holding the contribution's data and where the new | |
118 | * position will be inserted | |
119 | */ | |
120 | private function insertStatusLabels(&$contribution) { | |
121 | $contribution['contribution_status_label'] = $contribution['contribution_status']; | |
122 | ||
123 | if ($contribution['is_pay_later'] && CRM_Utils_Array::value('contribution_status', $contribution) == 'Pending') { | |
124 | $contribution['contribution_status_label'] .= ' (' . ts('Pay Later') . ')'; | |
125 | } | |
126 | elseif (CRM_Utils_Array::value('contribution_status', $contribution) == 'Pending') { | |
127 | $contribution['contribution_status_label'] .= ' (' . ts('Incomplete Transaction') . ')'; | |
128 | } | |
129 | } | |
130 | ||
131 | /** | |
132 | * Inserts into the given array a string with the 'action' key, holding the | |
133 | * html to be used to show available actions for the contribution. | |
134 | * | |
135 | * @param $contribution | |
136 | * Reference to the array holding the contribution's data. It is also the | |
137 | * array where the new 'action' key will be inserted. | |
138 | */ | |
139 | private function insertContributionActions(&$contribution) { | |
140 | $contribution['action'] = CRM_Core_Action::formLink( | |
141 | $this->buildContributionLinks($contribution), | |
142 | $this->getContributionPermissionsMask(), | |
be2fb01f | 143 | [ |
ed28afa9 SL |
144 | 'id' => $contribution['contribution_id'], |
145 | 'cid' => $contribution['contact_id'], | |
146 | 'cxt' => 'contribution', | |
be2fb01f | 147 | ], |
ed28afa9 SL |
148 | ts('more'), |
149 | FALSE, | |
150 | 'contribution.selector.row', | |
151 | 'Contribution', | |
152 | $contribution['contribution_id'] | |
153 | ); | |
154 | } | |
155 | ||
156 | /** | |
157 | * Builds list of links for authorized actions that can be done on given | |
158 | * contribution. | |
159 | * | |
160 | * @param array $contribution | |
161 | * | |
162 | * @return array | |
163 | */ | |
164 | private function buildContributionLinks($contribution) { | |
165 | $links = CRM_Contribute_Selector_Search::links($contribution['contribution_id'], | |
166 | CRM_Utils_Request::retrieve('action', 'String'), | |
167 | NULL, | |
168 | NULL | |
169 | ); | |
170 | ||
171 | $isPayLater = FALSE; | |
172 | if ($contribution['is_pay_later'] && CRM_Utils_Array::value('contribution_status', $contribution) == 'Pending') { | |
173 | $isPayLater = TRUE; | |
174 | ||
be2fb01f | 175 | $links[CRM_Core_Action::ADD] = [ |
ed28afa9 SL |
176 | 'name' => ts('Pay with Credit Card'), |
177 | 'url' => 'civicrm/contact/view/contribution', | |
178 | 'qs' => 'reset=1&action=update&id=%%id%%&cid=%%cid%%&context=%%cxt%%&mode=live', | |
179 | 'title' => ts('Pay with Credit Card'), | |
be2fb01f | 180 | ]; |
ed28afa9 SL |
181 | } |
182 | ||
be2fb01f | 183 | if (in_array($contribution['contribution_status'], ['Partially paid', 'Pending refund']) || $isPayLater) { |
ed28afa9 SL |
184 | $buttonName = ts('Record Payment'); |
185 | ||
186 | if ($contribution['contribution_status'] == 'Pending refund') { | |
187 | $buttonName = ts('Record Refund'); | |
188 | } | |
189 | elseif (CRM_Core_Config::isEnabledBackOfficeCreditCardPayments()) { | |
be2fb01f | 190 | $links[CRM_Core_Action::BASIC] = [ |
ed28afa9 SL |
191 | 'name' => ts('Submit Credit Card payment'), |
192 | 'url' => 'civicrm/payment/add', | |
193 | 'qs' => 'reset=1&id=%%id%%&cid=%%cid%%&action=add&component=contribution&mode=live', | |
194 | 'title' => ts('Submit Credit Card payment'), | |
be2fb01f | 195 | ]; |
ed28afa9 | 196 | } |
be2fb01f | 197 | $links[CRM_Core_Action::ADD] = [ |
ed28afa9 SL |
198 | 'name' => $buttonName, |
199 | 'url' => 'civicrm/payment', | |
200 | 'qs' => 'reset=1&id=%%id%%&cid=%%cid%%&action=add&component=contribution', | |
201 | 'title' => $buttonName, | |
be2fb01f | 202 | ]; |
ed28afa9 SL |
203 | } |
204 | ||
205 | return $links; | |
206 | } | |
207 | ||
208 | /** | |
209 | * Builds a mask with allowed contribution related permissions. | |
210 | * | |
211 | * @return int | |
212 | */ | |
213 | private function getContributionPermissionsMask() { | |
be2fb01f | 214 | $permissions = [CRM_Core_Permission::VIEW]; |
ed28afa9 SL |
215 | if (CRM_Core_Permission::check('edit contributions')) { |
216 | $permissions[] = CRM_Core_Permission::EDIT; | |
217 | } | |
218 | if (CRM_Core_Permission::check('delete in CiviContribute')) { | |
219 | $permissions[] = CRM_Core_Permission::DELETE; | |
220 | } | |
221 | ||
222 | return CRM_Core_Action::mask($permissions); | |
223 | } | |
224 | ||
225 | } |