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 = array();
43 $relatedContributionsResult = civicrm_api3('Contribution', 'get', array(
45 'contribution_recur_id' => $this->id
,
46 'contact_id' => $this->contactId
,
47 'options' => array('limit' => 0),
50 foreach ($relatedContributionsResult['values'] as $contribution) {
51 $this->insertAmountExpandingPaymentsControl($contribution);
52 $this->fixDateFormats($contribution);
53 $this->insertStatusLabels($contribution);
54 $this->insertContributionActions($contribution);
56 $relatedContributions[] = $contribution;
59 if (count($relatedContributions) > 0) {
60 $this->assign('contributionsCount', count($relatedContributions));
61 $this->assign('relatedContributions', json_encode($relatedContributions));
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.
69 * @param array $contribution
70 * Reference to the array holding the contribution's data and where the
71 * control will be inserted into
73 private function insertAmountExpandingPaymentsControl(&$contribution) {
74 $amount = CRM_Utils_Money
::format($contribution['total_amount'], $contribution['currency']);
76 $expandPaymentsUrl = CRM_Utils_System
::url('civicrm/payment',
78 'view' => 'transaction',
79 'component' => 'contribution',
81 'cid' => $this->contactId
,
82 'id' => $contribution['contribution_id'],
88 $contribution['amount_control'] = '
89 <a class="nowrap bold crm-expand-row" title="view payments" href="' . $expandPaymentsUrl . '">
90 ' . $amount . '
96 * Fixes date fields present in the given contribution.
98 * @param array $contribution
99 * Reference to the array holding the contribution's data
101 private function fixDateFormats(&$contribution) {
102 $config = CRM_Core_Config
::singleton();
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
);
109 * Inserts a contribution_status_label key into the array, with the value
110 * showing the current status plus observations on the current status.
112 * @param array $contribution
113 * Reference to the array holding the contribution's data and where the new
114 * position will be inserted
116 private function insertStatusLabels(&$contribution) {
117 $contribution['contribution_status_label'] = $contribution['contribution_status'];
119 if ($contribution['is_pay_later'] && CRM_Utils_Array
::value('contribution_status', $contribution) == 'Pending') {
120 $contribution['contribution_status_label'] .= ' (' . ts('Pay Later') . ')';
122 elseif (CRM_Utils_Array
::value('contribution_status', $contribution) == 'Pending') {
123 $contribution['contribution_status_label'] .= ' (' . ts('Incomplete Transaction') . ')';
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.
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.
135 private function insertContributionActions(&$contribution) {
136 $contribution['action'] = CRM_Core_Action
::formLink(
137 $this->buildContributionLinks($contribution),
138 $this->getContributionPermissionsMask(),
140 'id' => $contribution['contribution_id'],
141 'cid' => $contribution['contact_id'],
142 'cxt' => 'contribution',
146 'contribution.selector.row',
148 $contribution['contribution_id']
153 * Builds list of links for authorized actions that can be done on given
156 * @param array $contribution
160 private function buildContributionLinks($contribution) {
161 $links = CRM_Contribute_Selector_Search
::links($contribution['contribution_id'],
162 CRM_Utils_Request
::retrieve('action', 'String'),
168 if ($contribution['is_pay_later'] && CRM_Utils_Array
::value('contribution_status', $contribution) == 'Pending') {
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'),
179 if (in_array($contribution['contribution_status'], array('Partially paid', 'Pending refund')) ||
$isPayLater) {
180 $buttonName = ts('Record Payment');
182 if ($contribution['contribution_status'] == 'Pending refund') {
183 $buttonName = ts('Record Refund');
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'),
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,
205 * Builds a mask with allowed contribution related permissions.
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
;
214 if (CRM_Core_Permission
::check('delete in CiviContribute')) {
215 $permissions[] = CRM_Core_Permission
::DELETE
;
218 return CRM_Core_Action
::mask($permissions);