Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
8c9251b3 | 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
6a488035 | 29 | * @package CRM |
8c9251b3 | 30 | * @copyright CiviCRM LLC (c) 2004-2018 |
6a488035 TO |
31 | */ |
32 | ||
33 | /** | |
c0f62075 | 34 | * This class generates form components generic to recurring contributions. |
6a488035 TO |
35 | * |
36 | * It delegates the work to lower level subclasses and integrates the changes | |
37 | * back in. It also uses a lot of functionality with the CRM API's, so any change | |
38 | * made here could potentially affect the API etc. Be careful, be aware, use unit tests. | |
6a488035 TO |
39 | */ |
40 | class CRM_Contribute_Form_UpdateSubscription extends CRM_Core_Form { | |
41 | ||
42 | /** | |
0c6c47a5 | 43 | * The recurring contribution id, used when editing the recurring contribution. |
6a488035 TO |
44 | * |
45 | * @var int | |
46 | */ | |
0c6c47a5 | 47 | protected $contributionRecurID = NULL; |
6a488035 TO |
48 | |
49 | protected $_coid = NULL; | |
50 | ||
51 | protected $_subscriptionDetails = NULL; | |
52 | ||
53 | protected $_selfService = FALSE; | |
54 | ||
55 | public $_paymentProcessor = NULL; | |
56 | ||
57 | public $_paymentProcessorObj = NULL; | |
58 | ||
0c6c47a5 | 59 | /** |
60 | * Fields that affect the schedule and are defined as editable by the processor. | |
61 | * | |
62 | * @var array | |
63 | */ | |
64 | protected $editableScheduleFields = array(); | |
65 | ||
6a488035 | 66 | /** |
fe482240 | 67 | * The id of the contact associated with this recurring contribution. |
6a488035 TO |
68 | * |
69 | * @var int | |
6a488035 | 70 | */ |
430ae6dd TO |
71 | public $_contactID; |
72 | ||
c0f62075 | 73 | /** |
74 | * Pre-processing for the form. | |
75 | * | |
76 | * @throws \Exception | |
77 | */ | |
00be9182 | 78 | public function preProcess() { |
6a488035 | 79 | |
a0d322b1 KW |
80 | $this->setAction(CRM_Core_Action::UPDATE); |
81 | ||
0c6c47a5 | 82 | $this->contributionRecurID = CRM_Utils_Request::retrieve('crid', 'Integer', $this, FALSE); |
83 | if ($this->contributionRecurID) { | |
b3e340d9 | 84 | try { |
cd3ad80e | 85 | $this->_paymentProcessorObj = CRM_Financial_BAO_PaymentProcessor::getPaymentProcessorForRecurringContribution($this->contributionRecurID); |
b3e340d9 | 86 | } |
87 | catch (CRM_Core_Exception $e) { | |
0c6c47a5 | 88 | CRM_Core_Error::statusBounce(ts('There is no valid processor for this subscription so it cannot be edited.')); |
89 | } | |
85b68a11 CR |
90 | catch (CiviCRM_API3_Exception $e) { |
91 | CRM_Core_Error::statusBounce(ts('There is no valid processor for this subscription so it cannot be edited.')); | |
92 | } | |
0c6c47a5 | 93 | $this->_subscriptionDetails = CRM_Contribute_BAO_ContributionRecur::getSubscriptionDetails($this->contributionRecurID); |
6a488035 TO |
94 | } |
95 | ||
96 | $this->_coid = CRM_Utils_Request::retrieve('coid', 'Integer', $this, FALSE); | |
97 | if ($this->_coid) { | |
98 | $this->_paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getProcessorForEntity($this->_coid, 'contribute', 'info'); | |
cd3ad80e | 99 | // @todo test & replace with $this->_paymentProcessorObj = Civi\Payment\System::singleton()->getById($this->_paymentProcessor['id']); |
6a488035 TO |
100 | $this->_paymentProcessorObj = CRM_Financial_BAO_PaymentProcessor::getProcessorForEntity($this->_coid, 'contribute', 'obj'); |
101 | $this->_subscriptionDetails = CRM_Contribute_BAO_ContributionRecur::getSubscriptionDetails($this->_coid, 'contribution'); | |
0c6c47a5 | 102 | $this->contributionRecurID = $this->_subscriptionDetails->recur_id; |
6a488035 | 103 | } |
0c6c47a5 | 104 | elseif ($this->contributionRecurID) { |
105 | $this->_coid = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $this->contributionRecurID, 'id', 'contribution_recur_id'); | |
8de2c073 | 106 | } |
6a488035 | 107 | |
1273d77c | 108 | if (!$this->contributionRecurID || !$this->_subscriptionDetails) { |
7bcd892a | 109 | CRM_Core_Error::statusBounce(ts('Required information missing.')); |
6a488035 TO |
110 | } |
111 | ||
112 | if ($this->_subscriptionDetails->membership_id && $this->_subscriptionDetails->auto_renew) { | |
14b61354 MW |
113 | // Add Membership details to form |
114 | $membership = civicrm_api3('Membership', 'get', array( | |
115 | 'contribution_recur_id' => $this->contributionRecurID, | |
116 | )); | |
117 | if (!empty($membership['count'])) { | |
118 | $membershipDetails = reset($membership['values']); | |
119 | $values['membership_id'] = $membershipDetails['id']; | |
120 | $values['membership_name'] = $membershipDetails['membership_name']; | |
121 | } | |
122 | $this->assign('recurMembership', $values); | |
123 | $this->assign('contactId', $this->_subscriptionDetails->contact_id); | |
6a488035 TO |
124 | } |
125 | ||
126 | if (!CRM_Core_Permission::check('edit contributions')) { | |
127 | $userChecksum = CRM_Utils_Request::retrieve('cs', 'String', $this, FALSE); | |
128 | if (!CRM_Contact_BAO_Contact_Utils::validChecksum($this->_subscriptionDetails->contact_id, $userChecksum)) { | |
7bcd892a | 129 | CRM_Core_Error::statusBounce(ts('You do not have permission to update subscription.')); |
6a488035 TO |
130 | } |
131 | $this->_selfService = TRUE; | |
132 | } | |
133 | $this->assign('self_service', $this->_selfService); | |
134 | ||
0c6c47a5 | 135 | $this->editableScheduleFields = $this->_paymentProcessorObj->getEditableRecurringScheduleFields(); |
136 | ||
137 | $changeHelpText = $this->_paymentProcessorObj->getRecurringScheduleUpdateHelpText(); | |
138 | if (!in_array('amount', $this->editableScheduleFields)) { | |
139 | // Not sure if this is good behaviour - maintaining this existing behaviour for now. | |
140 | CRM_Core_Session::setStatus($changeHelpText, ts('Warning'), 'alert'); | |
141 | } | |
142 | else { | |
143 | $this->assign('changeHelpText', $changeHelpText); | |
144 | } | |
145 | $alreadyHardCodedFields = array('amount', 'installments'); | |
146 | foreach ($this->editableScheduleFields as $editableScheduleField) { | |
147 | if (!in_array($editableScheduleField, $alreadyHardCodedFields)) { | |
95d72a5f | 148 | $this->addField($editableScheduleField, array('entity' => 'ContributionRecur')); |
0c6c47a5 | 149 | } |
6a488035 TO |
150 | } |
151 | ||
0699333e MW |
152 | // when custom data is included in this page |
153 | if (!empty($_POST['hidden_custom'])) { | |
154 | CRM_Custom_Form_CustomData::preProcess($this, NULL, NULL, 1, 'ContributionRecur', $this->contributionRecurID); | |
155 | CRM_Custom_Form_CustomData::buildQuickForm($this); | |
156 | CRM_Custom_Form_CustomData::setDefaultValues($this); | |
157 | } | |
158 | ||
0c6c47a5 | 159 | $this->assign('editableScheduleFields', array_diff($this->editableScheduleFields, $alreadyHardCodedFields)); |
6a488035 TO |
160 | |
161 | if ($this->_subscriptionDetails->contact_id) { | |
162 | list($this->_donorDisplayName, $this->_donorEmail) = CRM_Contact_BAO_Contact::getContactDetails($this->_subscriptionDetails->contact_id); | |
163 | } | |
164 | ||
165 | CRM_Utils_System::setTitle(ts('Update Recurring Contribution')); | |
166 | ||
0c6c47a5 | 167 | // Handle context redirection. |
6a488035 TO |
168 | CRM_Contribute_BAO_ContributionRecur::setSubscriptionContext(); |
169 | } | |
170 | ||
171 | /** | |
347e061b | 172 | * Set default values for the form. |
6a488035 | 173 | * |
347e061b | 174 | * Note that in edit/view mode the default values are retrieved from the database. |
6a488035 | 175 | */ |
00be9182 | 176 | public function setDefaultValues() { |
6a488035 TO |
177 | $this->_defaults = array(); |
178 | $this->_defaults['amount'] = $this->_subscriptionDetails->amount; | |
179 | $this->_defaults['installments'] = $this->_subscriptionDetails->installments; | |
7083dc2a | 180 | $this->_defaults['campaign_id'] = $this->_subscriptionDetails->campaign_id; |
467fe956 | 181 | $this->_defaults['financial_type_id'] = $this->_subscriptionDetails->financial_type_id; |
6a488035 | 182 | $this->_defaults['is_notify'] = 1; |
0c6c47a5 | 183 | foreach ($this->editableScheduleFields as $field) { |
0699333e | 184 | $this->_defaults[$field] = isset($this->_subscriptionDetails->$field) ? $this->_subscriptionDetails->$field : NULL; |
0c6c47a5 | 185 | } |
6a488035 TO |
186 | |
187 | return $this->_defaults; | |
188 | } | |
189 | ||
190 | /** | |
fe482240 | 191 | * Actually build the components of the form. |
6a488035 TO |
192 | */ |
193 | public function buildQuickForm() { | |
8de2c073 | 194 | // CRM-16398: If current recurring contribution got > 1 lineitems then make amount field readonly |
195 | $amtAttr = array('size' => 20); | |
196 | $lineItems = CRM_Price_BAO_LineItem::getLineItemsByContributionID($this->_coid); | |
197 | if (count($lineItems) > 1) { | |
198 | $amtAttr += array('readonly' => TRUE); | |
199 | } | |
200 | $this->addMoney('amount', ts('Recurring Contribution Amount'), TRUE, $amtAttr, | |
201 | TRUE, 'currency', $this->_subscriptionDetails->currency, TRUE | |
6a488035 TO |
202 | ); |
203 | ||
467fe956 | 204 | $this->add('text', 'installments', ts('Number of Installments'), array('size' => 20), FALSE); |
6a488035 TO |
205 | |
206 | if ($this->_donorEmail) { | |
207 | $this->add('checkbox', 'is_notify', ts('Notify Contributor?')); | |
208 | } | |
209 | ||
7083dc2a | 210 | if (CRM_Core_Permission::check('edit contributions')) { |
edb227cd | 211 | CRM_Campaign_BAO_Campaign::addCampaign($this, $this->_subscriptionDetails->campaign_id); |
467fe956 | 212 | } |
213 | ||
0c6c47a5 | 214 | if (CRM_Contribute_BAO_ContributionRecur::supportsFinancialTypeChange($this->contributionRecurID)) { |
0d93f90b | 215 | $this->addEntityRef('financial_type_id', ts('Financial Type'), array('entity' => 'FinancialType'), !$this->_selfService); |
7083dc2a | 216 | } |
217 | ||
0699333e MW |
218 | // Add custom data |
219 | $this->assign('customDataType', 'ContributionRecur'); | |
220 | $this->assign('entityID', $this->contributionRecurID); | |
221 | ||
6a488035 | 222 | $type = 'next'; |
481a74f4 | 223 | if ($this->_selfService) { |
6a488035 TO |
224 | $type = 'submit'; |
225 | } | |
226 | ||
227 | // define the buttons | |
228 | $this->addButtons(array( | |
229 | array( | |
230 | 'type' => $type, | |
231 | 'name' => ts('Save'), | |
232 | 'isDefault' => TRUE, | |
233 | ), | |
234 | array( | |
235 | 'type' => 'cancel', | |
236 | 'name' => ts('Cancel'), | |
237 | ), | |
238 | ) | |
239 | ); | |
240 | } | |
241 | ||
242 | /** | |
347e061b | 243 | * Called after the user submits the form. |
6a488035 TO |
244 | */ |
245 | public function postProcess() { | |
246 | // store the submitted values in an array | |
247 | $params = $this->exportValues(); | |
248 | ||
249 | if ($this->_selfService && $this->_donorEmail) { | |
250 | // for self service force notify | |
251 | $params['is_notify'] = 1; | |
252 | } | |
253 | ||
254 | // if this is an update of an existing recurring contribution, pass the ID | |
255 | $params['id'] = $this->_subscriptionDetails->recur_id; | |
256 | $message = ''; | |
257 | ||
258 | $params['subscriptionId'] = $this->_subscriptionDetails->subscription_id; | |
4eeb9a5b | 259 | $updateSubscription = TRUE; |
6a488035 | 260 | if ($this->_paymentProcessorObj->isSupported('changeSubscriptionAmount')) { |
353ffa53 | 261 | $updateSubscription = $this->_paymentProcessorObj->changeSubscriptionAmount($message, $params); |
6a488035 TO |
262 | } |
263 | if (is_a($updateSubscription, 'CRM_Core_Error')) { | |
353ffa53 TO |
264 | CRM_Core_Error::displaySessionError($updateSubscription); |
265 | $status = ts('Could not update the Recurring contribution details'); | |
266 | $msgTitle = ts('Update Error'); | |
267 | $msgType = 'error'; | |
6a488035 TO |
268 | } |
269 | elseif ($updateSubscription) { | |
0699333e MW |
270 | // Handle custom data |
271 | $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, $this->contributionRecurID, 'ContributionRecur'); | |
353ffa53 | 272 | // save the changes |
0699333e | 273 | CRM_Contribute_BAO_ContributionRecur::add($params); |
353ffa53 TO |
274 | $status = ts('Recurring contribution has been updated to: %1, every %2 %3(s) for %4 installments.', |
275 | array( | |
276 | 1 => CRM_Utils_Money::format($params['amount'], $this->_subscriptionDetails->currency), | |
277 | 2 => $this->_subscriptionDetails->frequency_interval, | |
278 | 3 => $this->_subscriptionDetails->frequency_unit, | |
279 | 4 => $params['installments'], | |
280 | ) | |
281 | ); | |
282 | ||
283 | $msgTitle = ts('Update Success'); | |
284 | $msgType = 'success'; | |
c6c91efc | 285 | $msg = ts('Recurring Contribution Updated'); |
353ffa53 TO |
286 | $contactID = $this->_subscriptionDetails->contact_id; |
287 | ||
288 | if ($this->_subscriptionDetails->amount != $params['amount']) { | |
289 | $message .= "<br /> " . ts("Recurring contribution amount has been updated from %1 to %2 for this subscription.", | |
290 | array( | |
291 | 1 => CRM_Utils_Money::format($this->_subscriptionDetails->amount, $this->_subscriptionDetails->currency), | |
292 | 2 => CRM_Utils_Money::format($params['amount'], $this->_subscriptionDetails->currency), | |
293 | )) . ' '; | |
c6c91efc | 294 | if ($this->_subscriptionDetails->amount < $params['amount']) { |
295 | $msg = ts('Recurring Contribution Updated - increased installment amount'); | |
296 | } | |
297 | else { | |
298 | $msg = ts('Recurring Contribution Updated - decreased installment amount'); | |
299 | } | |
353ffa53 | 300 | } |
6a488035 | 301 | |
353ffa53 TO |
302 | if ($this->_subscriptionDetails->installments != $params['installments']) { |
303 | $message .= "<br /> " . ts("Recurring contribution installments have been updated from %1 to %2 for this subscription.", array( | |
304 | 1 => $this->_subscriptionDetails->installments, | |
317fceb4 | 305 | 2 => $params['installments'], |
353ffa53 TO |
306 | )) . ' '; |
307 | } | |
6a488035 | 308 | |
353ffa53 TO |
309 | $activityParams = array( |
310 | 'source_contact_id' => $contactID, | |
12853dec | 311 | 'activity_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Update Recurring Contribution'), |
c6c91efc | 312 | 'subject' => $msg, |
353ffa53 TO |
313 | 'details' => $message, |
314 | 'activity_date_time' => date('YmdHis'), | |
12853dec | 315 | 'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Completed'), |
353ffa53 | 316 | ); |
12853dec | 317 | |
353ffa53 TO |
318 | $session = CRM_Core_Session::singleton(); |
319 | $cid = $session->get('userID'); | |
6a488035 | 320 | |
353ffa53 TO |
321 | if ($cid) { |
322 | $activityParams['target_contact_id'][] = $activityParams['source_contact_id']; | |
323 | $activityParams['source_contact_id'] = $cid; | |
324 | } | |
325 | CRM_Activity_BAO_Activity::create($activityParams); | |
326 | ||
327 | if (!empty($params['is_notify'])) { | |
328 | // send notification | |
329 | if ($this->_subscriptionDetails->contribution_page_id) { | |
330 | CRM_Core_DAO::commonRetrieveAll('CRM_Contribute_DAO_ContributionPage', 'id', | |
331 | $this->_subscriptionDetails->contribution_page_id, $value, array( | |
332 | 'title', | |
333 | 'receipt_from_name', | |
334 | 'receipt_from_email', | |
335 | ) | |
6a488035 | 336 | ); |
353ffa53 TO |
337 | $receiptFrom = '"' . CRM_Utils_Array::value('receipt_from_name', $value[$this->_subscriptionDetails->contribution_page_id]) . '" <' . $value[$this->_subscriptionDetails->contribution_page_id]['receipt_from_email'] . '>'; |
338 | } | |
339 | else { | |
340 | $domainValues = CRM_Core_BAO_Domain::getNameAndEmail(); | |
341 | $receiptFrom = "$domainValues[0] <$domainValues[1]>"; | |
6a488035 | 342 | } |
353ffa53 TO |
343 | |
344 | list($donorDisplayName, $donorEmail) = CRM_Contact_BAO_Contact::getContactDetails($contactID); | |
345 | ||
346 | $tplParams = array( | |
347 | 'recur_frequency_interval' => $this->_subscriptionDetails->frequency_interval, | |
348 | 'recur_frequency_unit' => $this->_subscriptionDetails->frequency_unit, | |
349 | 'amount' => CRM_Utils_Money::format($params['amount']), | |
350 | 'installments' => $params['installments'], | |
351 | ); | |
352 | ||
353 | $tplParams['contact'] = array('display_name' => $donorDisplayName); | |
354 | $tplParams['receipt_from_email'] = $receiptFrom; | |
355 | ||
356 | $sendTemplateParams = array( | |
357 | 'groupName' => 'msg_tpl_workflow_contribution', | |
358 | 'valueName' => 'contribution_recurring_edit', | |
359 | 'contactId' => $contactID, | |
360 | 'tplParams' => $tplParams, | |
361 | 'isTest' => $this->_subscriptionDetails->is_test, | |
362 | 'PDFFilename' => 'receipt.pdf', | |
363 | 'from' => $receiptFrom, | |
364 | 'toName' => $donorDisplayName, | |
365 | 'toEmail' => $donorEmail, | |
366 | ); | |
367 | list($sent) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams); | |
6a488035 | 368 | } |
353ffa53 | 369 | } |
6a488035 TO |
370 | |
371 | $session = CRM_Core_Session::singleton(); | |
353ffa53 | 372 | $userID = $session->get('userID'); |
481a74f4 | 373 | if ($userID && $status) { |
6a488035 | 374 | CRM_Core_Session::setStatus($status, $msgTitle, $msgType); |
0db6c3e1 | 375 | } |
4c9b6178 | 376 | elseif (!$userID) { |
317fceb4 | 377 | if ($status) { |
6a488035 | 378 | CRM_Utils_System::setUFMessage($status); |
317fceb4 | 379 | } |
6a488035 TO |
380 | // keep result as 1, since we not displaying anything on the redirected page anyway |
381 | return CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contribute/subscriptionstatus', | |
353ffa53 | 382 | "reset=1&task=update&result=1")); |
6a488035 TO |
383 | } |
384 | } | |
96025800 | 385 | |
0c6c47a5 | 386 | /** |
387 | * Explicitly declare the form context. | |
388 | */ | |
389 | public function getDefaultContext() { | |
390 | return 'create'; | |
391 | } | |
392 | ||
6a488035 | 393 | } |