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