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