4 * Shows list of contributions done as payments within a recurring contribution.
6 class CRM_Contribute_Page_ContributionRecurPayments
extends CRM_Core_Page
{
20 private $contactId = NULL;
23 * Builds list of contributions for a given recurring contribution.
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);
31 $this->loadRelatedContributions();
37 * Loads contributions associated to the current recurring contribution being
40 private function loadRelatedContributions() {
41 $relatedContributions = [];
43 $relatedContributionsResult = civicrm_api3('Contribution', 'get', [
45 'contribution_recur_id' => $this->id
,
46 'contact_id' => $this->contactId
,
47 'options' => ['limit' => 0],
48 'contribution_test' => '',
51 foreach ($relatedContributionsResult['values'] as $contribution) {
52 $this->insertAmountExpandingPaymentsControl($contribution);
53 $this->fixDateFormats($contribution);
54 $this->insertStatusLabels($contribution);
55 $this->insertContributionActions($contribution);
57 if ($contribution['is_test']) {
58 $contribution['financial_type'] = CRM_Core_TestEntity
::appendTestText($contribution['financial_type']);
60 $relatedContributions[] = $contribution;
63 if (count($relatedContributions) > 0) {
64 $this->assign('contributionsCount', count($relatedContributions));
65 $this->assign('relatedContributions', json_encode($relatedContributions));
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.
73 * @param array $contribution
74 * Reference to the array holding the contribution's data and where the
75 * control will be inserted into
77 private function insertAmountExpandingPaymentsControl(&$contribution) {
78 $amount = CRM_Utils_Money
::format($contribution['total_amount'], $contribution['currency']);
80 $expandPaymentsUrl = CRM_Utils_System
::url('civicrm/payment',
82 'view' => 'transaction',
83 'component' => 'contribution',
85 'cid' => $this->contactId
,
86 'id' => $contribution['contribution_id'],
92 $contribution['amount_control'] = '
93 <a class="nowrap bold crm-expand-row" title="view payments" href="' . $expandPaymentsUrl . '">
94 ' . $amount . '
100 * Fixes date fields present in the given contribution.
102 * @param array $contribution
103 * Reference to the array holding the contribution's data
105 private function fixDateFormats(&$contribution) {
106 $config = CRM_Core_Config
::singleton();
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
);
113 * Inserts a contribution_status_label key into the array, with the value
114 * showing the current status plus observations on the current status.
116 * @param array $contribution
117 * Reference to the array holding the contribution's data and where the new
118 * position will be inserted
120 private function insertStatusLabels(&$contribution) {
121 $contribution['contribution_status_label'] = $contribution['contribution_status'];
123 if ($contribution['is_pay_later'] && CRM_Utils_Array
::value('contribution_status', $contribution) == 'Pending') {
124 $contribution['contribution_status_label'] .= ' (' . ts('Pay Later') . ')';
126 elseif (CRM_Utils_Array
::value('contribution_status', $contribution) == 'Pending') {
127 $contribution['contribution_status_label'] .= ' (' . ts('Incomplete Transaction') . ')';
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.
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.
139 private function insertContributionActions(&$contribution) {
140 $contribution['action'] = CRM_Core_Action
::formLink(
141 $this->buildContributionLinks($contribution),
142 $this->getContributionPermissionsMask(),
144 'id' => $contribution['contribution_id'],
145 'cid' => $contribution['contact_id'],
146 'cxt' => 'contribution',
150 'contribution.selector.row',
152 $contribution['contribution_id']
157 * Builds list of links for authorized actions that can be done on given
160 * @param array $contribution
164 private function buildContributionLinks($contribution) {
165 $links = CRM_Contribute_Selector_Search
::links($contribution['contribution_id'],
166 CRM_Utils_Request
::retrieve('action', 'String'),
172 if ($contribution['is_pay_later'] && CRM_Utils_Array
::value('contribution_status', $contribution) == 'Pending') {
175 $links[CRM_Core_Action
::ADD
] = [
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'),
183 if (in_array($contribution['contribution_status'], ['Partially paid', 'Pending refund']) ||
$isPayLater) {
184 $buttonName = ts('Record Payment');
186 if ($contribution['contribution_status'] == 'Pending refund') {
187 $buttonName = ts('Record Refund');
189 elseif (CRM_Core_Config
::isEnabledBackOfficeCreditCardPayments()) {
190 $links[CRM_Core_Action
::BASIC
] = [
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'),
197 $links[CRM_Core_Action
::ADD
] = [
198 'name' => $buttonName,
199 'url' => 'civicrm/payment',
200 'qs' => 'reset=1&id=%%id%%&cid=%%cid%%&action=add&component=contribution',
201 'title' => $buttonName,
209 * Builds a mask with allowed contribution related permissions.
213 private function getContributionPermissionsMask() {
214 $permissions = [CRM_Core_Permission
::VIEW
];
215 if (CRM_Core_Permission
::check('edit contributions')) {
216 $permissions[] = CRM_Core_Permission
::EDIT
;
218 if (CRM_Core_Permission
::check('delete in CiviContribute')) {
219 $permissions[] = CRM_Core_Permission
::DELETE
;
222 return CRM_Core_Action
::mask($permissions);