3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
30 * @copyright CiviCRM LLC (c) 2004-2019
34 * This class generates form components generic to recurring contributions.
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.
40 class CRM_Contribute_Form_UpdateSubscription
extends CRM_Contribute_Form_ContributionRecur
{
42 protected $_subscriptionDetails = NULL;
44 public $_paymentProcessor = NULL;
46 public $_paymentProcessorObj = NULL;
49 * Fields that affect the schedule and are defined as editable by the processor.
53 protected $editableScheduleFields = [];
56 * The id of the contact associated with this recurring contribution.
63 * Pre-processing for the form.
67 public function preProcess() {
70 $this->setAction(CRM_Core_Action
::UPDATE
);
73 $this->_paymentProcessor
= CRM_Financial_BAO_PaymentProcessor
::getProcessorForEntity($this->_coid
, 'contribute', 'info');
74 // @todo test & replace with $this->_paymentProcessorObj = Civi\Payment\System::singleton()->getById($this->_paymentProcessor['id']);
75 $this->_paymentProcessorObj
= CRM_Financial_BAO_PaymentProcessor
::getProcessorForEntity($this->_coid
, 'contribute', 'obj');
76 $this->contributionRecurID
= $this->_subscriptionDetails
->recur_id
;
78 elseif ($this->contributionRecurID
) {
79 $this->_coid
= CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_Contribution', $this->contributionRecurID
, 'id', 'contribution_recur_id');
82 if (!$this->contributionRecurID ||
!$this->_subscriptionDetails
) {
83 CRM_Core_Error
::statusBounce(ts('Required information missing.'));
86 if ($this->_subscriptionDetails
->membership_id
&& $this->_subscriptionDetails
->auto_renew
) {
87 // Add Membership details to form
88 $membership = civicrm_api3('Membership', 'get', [
89 'contribution_recur_id' => $this->contributionRecurID
,
91 if (!empty($membership['count'])) {
92 $membershipDetails = reset($membership['values']);
93 $values['membership_id'] = $membershipDetails['id'];
94 $values['membership_name'] = $membershipDetails['membership_name'];
96 $this->assign('recurMembership', $values);
97 $this->assign('contactId', $this->_subscriptionDetails
->contact_id
);
100 $this->assign('self_service', $this->isSelfService());
102 $this->editableScheduleFields
= $this->_paymentProcessorObj
->getEditableRecurringScheduleFields();
104 $changeHelpText = $this->_paymentProcessorObj
->getRecurringScheduleUpdateHelpText();
105 if (!in_array('amount', $this->editableScheduleFields
)) {
106 // Not sure if this is good behaviour - maintaining this existing behaviour for now.
107 CRM_Core_Session
::setStatus($changeHelpText, ts('Warning'), 'alert');
110 $this->assign('changeHelpText', $changeHelpText);
112 $alreadyHardCodedFields = ['amount', 'installments'];
113 foreach ($this->editableScheduleFields
as $editableScheduleField) {
114 if (!in_array($editableScheduleField, $alreadyHardCodedFields)) {
115 $this->addField($editableScheduleField, ['entity' => 'ContributionRecur'], FALSE, FALSE);
119 // when custom data is included in this page
120 if (!empty($_POST['hidden_custom']) && !$this->isSelfService()) {
121 CRM_Custom_Form_CustomData
::preProcess($this, NULL, NULL, 1, 'ContributionRecur', $this->contributionRecurID
);
122 CRM_Custom_Form_CustomData
::buildQuickForm($this);
123 CRM_Custom_Form_CustomData
::setDefaultValues($this);
126 $this->assign('editableScheduleFields', array_diff($this->editableScheduleFields
, $alreadyHardCodedFields));
128 if ($this->_subscriptionDetails
->contact_id
) {
129 list($this->_donorDisplayName
, $this->_donorEmail
) = CRM_Contact_BAO_Contact
::getContactDetails($this->_subscriptionDetails
->contact_id
);
132 CRM_Utils_System
::setTitle(ts('Update Recurring Contribution'));
134 // Handle context redirection.
135 CRM_Contribute_BAO_ContributionRecur
::setSubscriptionContext();
139 * Set default values for the form.
141 * Note that in edit/view mode the default values are retrieved from the database.
143 public function setDefaultValues() {
144 $this->_defaults
= [];
145 $this->_defaults
['amount'] = $this->_subscriptionDetails
->amount
;
146 $this->_defaults
['installments'] = $this->_subscriptionDetails
->installments
;
147 $this->_defaults
['campaign_id'] = $this->_subscriptionDetails
->campaign_id
;
148 $this->_defaults
['financial_type_id'] = $this->_subscriptionDetails
->financial_type_id
;
149 $this->_defaults
['is_notify'] = 1;
150 foreach ($this->editableScheduleFields
as $field) {
151 $this->_defaults
[$field] = isset($this->_subscriptionDetails
->$field) ?
$this->_subscriptionDetails
->$field : NULL;
154 return $this->_defaults
;
158 * Actually build the components of the form.
160 public function buildQuickForm() {
161 // CRM-16398: If current recurring contribution got > 1 lineitems then make amount field readonly
162 $amtAttr = ['size' => 20];
163 $lineItems = CRM_Price_BAO_LineItem
::getLineItemsByContributionID($this->_coid
);
164 if (count($lineItems) > 1) {
165 $amtAttr +
= ['readonly' => TRUE];
167 $this->addMoney('amount', ts('Recurring Contribution Amount'), TRUE, $amtAttr,
168 TRUE, 'currency', $this->_subscriptionDetails
->currency
, TRUE
171 $this->add('text', 'installments', ts('Number of Installments'), ['size' => 20], FALSE);
173 if ($this->_donorEmail
) {
174 $this->add('checkbox', 'is_notify', ts('Notify Contributor?'));
177 if (CRM_Core_Permission
::check('edit contributions')) {
178 CRM_Campaign_BAO_Campaign
::addCampaign($this, $this->_subscriptionDetails
->campaign_id
);
181 if (CRM_Contribute_BAO_ContributionRecur
::supportsFinancialTypeChange($this->contributionRecurID
)) {
182 $this->addEntityRef('financial_type_id', ts('Financial Type'), ['entity' => 'FinancialType'], !$this->isSelfService());
186 $this->assign('customDataType', 'ContributionRecur');
187 $this->assign('entityID', $this->contributionRecurID
);
190 if ($this->isSelfService()) {
194 // define the buttons
198 'name' => ts('Save'),
203 'name' => ts('Cancel'),
209 * Called after the user submits the form.
211 public function postProcess() {
212 // store the submitted values in an array
213 $params = $this->exportValues();
215 if ($this->isSelfService() && $this->_donorEmail
) {
216 // for self service force notify
217 $params['is_notify'] = 1;
220 // if this is an update of an existing recurring contribution, pass the ID
221 $params['id'] = $this->_subscriptionDetails
->recur_id
;
224 $params['subscriptionId'] = $this->_subscriptionDetails
->subscription_id
;
225 $updateSubscription = TRUE;
226 if ($this->_paymentProcessorObj
->supports('changeSubscriptionAmount')) {
227 $updateSubscription = $this->_paymentProcessorObj
->changeSubscriptionAmount($message, $params);
229 if (is_a($updateSubscription, 'CRM_Core_Error')) {
230 CRM_Core_Error
::displaySessionError($updateSubscription);
231 $status = ts('Could not update the Recurring contribution details');
232 $msgTitle = ts('Update Error');
235 elseif ($updateSubscription) {
236 // Handle custom data
237 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess($params, $this->contributionRecurID
, 'ContributionRecur');
239 CRM_Contribute_BAO_ContributionRecur
::add($params);
240 $status = ts('Recurring contribution has been updated to: %1, every %2 %3(s) for %4 installments.',
242 1 => CRM_Utils_Money
::format($params['amount'], $this->_subscriptionDetails
->currency
),
243 2 => $this->_subscriptionDetails
->frequency_interval
,
244 3 => $this->_subscriptionDetails
->frequency_unit
,
245 4 => $params['installments'],
249 $msgTitle = ts('Update Success');
250 $msgType = 'success';
251 $msg = ts('Recurring Contribution Updated');
252 $contactID = $this->_subscriptionDetails
->contact_id
;
254 if ($this->_subscriptionDetails
->amount
!= $params['amount']) {
255 $message .= "<br /> " . ts("Recurring contribution amount has been updated from %1 to %2 for this subscription.",
257 1 => CRM_Utils_Money
::format($this->_subscriptionDetails
->amount
, $this->_subscriptionDetails
->currency
),
258 2 => CRM_Utils_Money
::format($params['amount'], $this->_subscriptionDetails
->currency
),
260 if ($this->_subscriptionDetails
->amount
< $params['amount']) {
261 $msg = ts('Recurring Contribution Updated - increased installment amount');
264 $msg = ts('Recurring Contribution Updated - decreased installment amount');
268 if ($this->_subscriptionDetails
->installments
!= $params['installments']) {
269 $message .= "<br /> " . ts("Recurring contribution installments have been updated from %1 to %2 for this subscription.", [
270 1 => $this->_subscriptionDetails
->installments
,
271 2 => $params['installments'],
276 'source_contact_id' => $contactID,
277 'activity_type_id' => CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Update Recurring Contribution'),
279 'details' => $message,
280 'activity_date_time' => date('YmdHis'),
281 'status_id' => CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Completed'),
284 $session = CRM_Core_Session
::singleton();
285 $cid = $session->get('userID');
288 $activityParams['target_contact_id'][] = $activityParams['source_contact_id'];
289 $activityParams['source_contact_id'] = $cid;
291 CRM_Activity_BAO_Activity
::create($activityParams);
293 if (!empty($params['is_notify'])) {
295 if ($this->_subscriptionDetails
->contribution_page_id
) {
296 CRM_Core_DAO
::commonRetrieveAll('CRM_Contribute_DAO_ContributionPage', 'id',
297 $this->_subscriptionDetails
->contribution_page_id
, $value, [
300 'receipt_from_email',
303 $receiptFrom = '"' . CRM_Utils_Array
::value('receipt_from_name', $value[$this->_subscriptionDetails
->contribution_page_id
]) . '" <' . $value[$this->_subscriptionDetails
->contribution_page_id
]['receipt_from_email'] . '>';
306 $domainValues = CRM_Core_BAO_Domain
::getNameAndEmail();
307 $receiptFrom = "$domainValues[0] <$domainValues[1]>";
310 list($donorDisplayName, $donorEmail) = CRM_Contact_BAO_Contact
::getContactDetails($contactID);
313 'recur_frequency_interval' => $this->_subscriptionDetails
->frequency_interval
,
314 'recur_frequency_unit' => $this->_subscriptionDetails
->frequency_unit
,
315 'amount' => CRM_Utils_Money
::format($params['amount']),
316 'installments' => $params['installments'],
319 $tplParams['contact'] = ['display_name' => $donorDisplayName];
320 $tplParams['receipt_from_email'] = $receiptFrom;
322 $sendTemplateParams = [
323 'groupName' => 'msg_tpl_workflow_contribution',
324 'valueName' => 'contribution_recurring_edit',
325 'contactId' => $contactID,
326 'tplParams' => $tplParams,
327 'isTest' => $this->_subscriptionDetails
->is_test
,
328 'PDFFilename' => 'receipt.pdf',
329 'from' => $receiptFrom,
330 'toName' => $donorDisplayName,
331 'toEmail' => $donorEmail,
333 list($sent) = CRM_Core_BAO_MessageTemplate
::sendTemplate($sendTemplateParams);
337 $session = CRM_Core_Session
::singleton();
338 $userID = $session->get('userID');
339 if ($userID && $status) {
340 CRM_Core_Session
::setStatus($status, $msgTitle, $msgType);
344 CRM_Utils_System
::setUFMessage($status);
346 // keep result as 1, since we not displaying anything on the redirected page anyway
347 return CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/contribute/subscriptionstatus',
348 "reset=1&task=update&result=1"));