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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
35 * This class generates form components for processing a pledge
37 class CRM_Pledge_Form_Pledge
extends CRM_Core_Form
{
41 * The id of the pledge that we are proceessing.
48 * The id of the contact associated with this pledge.
55 * The Pledge values if an existing pledge.
60 * The Pledge frequency Units.
65 * Is current pledge pending.
67 public $_isPending = FALSE;
70 * Set variables up before form is built.
72 public function preProcess() {
73 $this->_contactID
= CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
74 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String',
77 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
78 $this->_context
= CRM_Utils_Request
::retrieve('context', 'Alphanumeric', $this);
80 // check for action permissions.
81 if (!CRM_Core_Permission
::checkActionPermission('CiviPledge', $this->_action
)) {
82 CRM_Core_Error
::fatal(ts('You do not have permission to access this page.'));
85 $this->assign('action', $this->_action
);
86 $this->assign('context', $this->_context
);
87 if ($this->_action
& CRM_Core_Action
::DELETE
) {
91 $this->userDisplayName
= $this->userEmail
= NULL;
92 if ($this->_contactID
) {
93 list($this->userDisplayName
,
95 ) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($this->_contactID
);
96 $this->assign('displayName', $this->userDisplayName
);
99 $this->setPageTitle(ts('Pledge'));
102 CRM_Custom_Form_CustomData
::preProcess($this, NULL, NULL, 1, 'Pledge', $this->_id
);
103 $this->_values
= array();
106 // get the contribution id
107 $this->_contributionID
= CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_PledgePayment',
108 $this->_id
, 'contribution_id', 'pledge_id'
110 $params = array('id' => $this->_id
);
111 CRM_Pledge_BAO_Pledge
::getValues($params, $this->_values
);
113 $this->_isPending
= (CRM_Pledge_BAO_Pledge
::pledgeHasFinancialTransactions($this->_id
, CRM_Utils_Array
::value('status_id', $this->_values
))) ?
FALSE : TRUE;
116 // get the pledge frequency units.
117 $this->_freqUnits
= CRM_Core_OptionGroup
::values('recur_frequency_units');
119 $this->_fromEmails
= CRM_Core_BAO_Email
::getFromEmail();
124 * Set default values for the form.
125 * The default values are retrieved from the database.
127 public function setDefaultValues() {
128 $defaults = $this->_values
;
130 if ($this->_action
& CRM_Core_Action
::DELETE
) {
134 if (!empty($defaults['is_test'])) {
135 $this->assign('is_test', TRUE);
139 // check is this pledge pending.
140 // fix the display of the monetary value, CRM-4038.
141 if ($this->_isPending
) {
142 $defaults['eachPaymentAmount'] = $this->_values
['amount'] / $this->_values
['installments'];
143 $defaults['eachPaymentAmount'] = CRM_Utils_Money
::format($defaults['eachPaymentAmount'], NULL, '%a');
146 // fix the display of the monetary value, CRM-4038
147 if (isset($this->_values
['amount'])) {
148 $defaults['amount'] = CRM_Utils_Money
::format($this->_values
['amount'], NULL, '%a');
150 $this->assign('amount', $this->_values
['amount']);
151 $this->assign('installments', $defaults['installments']);
155 $defaults['create_date'] = date('Y-m-d');
156 $defaults['start_date'] = date('Y-m-d');
157 $defaults['installments'] = 12;
158 $defaults['frequency_interval'] = 1;
159 $defaults['frequency_day'] = 1;
160 $defaults['initial_reminder_day'] = 5;
161 $defaults['max_reminders'] = 1;
162 $defaults['additional_reminder_day'] = 5;
163 $defaults['frequency_unit'] = array_search('month', $this->_freqUnits
);
164 $defaults['financial_type_id'] = array_search('Donation', CRM_Contribute_PseudoConstant
::financialType());
167 $pledgeStatus = CRM_Pledge_BAO_Pledge
::buildOptions('status_id');
168 $pledgeStatusNames = CRM_Core_OptionGroup
::values('pledge_status',
169 FALSE, FALSE, FALSE, NULL, 'name', TRUE
171 // get default status label (pending)
172 $defaultPledgeStatus = CRM_Utils_Array
::value(array_search('Pending', $pledgeStatusNames),
177 $this->assign('status', CRM_Utils_Array
::value(CRM_Utils_Array
::value('status_id', $this->_values
),
182 if (isset($this->userEmail
)) {
183 $this->assign('email', $this->userEmail
);
186 // custom data set defaults
187 $defaults +
= CRM_Custom_Form_CustomData
::setDefaultValues($this);
193 * Build the form object.
195 public function buildQuickForm() {
196 if ($this->_action
& CRM_Core_Action
::DELETE
) {
197 $this->addButtons(array(
200 'name' => ts('Delete'),
201 'spacing' => ' ',
206 'name' => ts('Cancel'),
213 if ($this->_context
== 'standalone') {
214 $this->addEntityRef('contact_id', ts('Contact'), array(
216 'api' => array('extra' => array('email')),
220 $showAdditionalInfo = FALSE;
221 $this->_formType
= CRM_Utils_Array
::value('formType', $_GET);
226 'Payment Reminders' => 'PaymentReminders',
228 foreach ($paneNames as $name => $type) {
229 $urlParams = "snippet=4&formType={$type}";
230 $allPanes[$name] = array(
231 'url' => CRM_Utils_System
::url('civicrm/contact/view/pledge', $urlParams),
235 // see if we need to include this paneName in the current form
236 if ($this->_formType
== $type ||
!empty($_POST["hidden_{$type}"]) ||
237 CRM_Utils_Array
::value("hidden_{$type}", $defaults)
239 $showAdditionalInfo = TRUE;
240 $allPanes[$name]['open'] = 'true';
242 $fnName = "build{$type}";
243 CRM_Contribute_Form_AdditionalInfo
::$fnName($this);
246 $this->assign('allPanes', $allPanes);
247 $this->assign('showAdditionalInfo', $showAdditionalInfo);
249 if ($this->_formType
) {
250 $this->assign('formType', $this->_formType
);
254 $this->applyFilter('__ALL__', 'trim');
257 $attributes = CRM_Core_DAO
::getAttribute('CRM_Pledge_DAO_Pledge');
259 $this->assign('isPending', $this->_isPending
);
262 'onblur' => "calculatedPaymentAmount( );",
263 'onkeyup' => "calculatedPaymentAmount( );",
266 $amount = $this->addMoney('amount', ts('Total Pledge Amount'), TRUE,
267 array_merge($attributes['pledge_amount'], $js), TRUE,
268 'currency', NULL, $this->_id
&& !$this->_isPending
271 $installments = &$this->add('text', 'installments', ts('To be paid in'),
272 array_merge($attributes['installments'], $js), TRUE
274 $this->addRule('installments', ts('Please enter a valid number of installments.'), 'positiveInteger');
276 $frequencyInterval = $this->add('number', 'frequency_interval', ts('every'),
277 $attributes['pledge_frequency_interval'], TRUE
279 $this->addRule('frequency_interval', ts('Please enter a number for frequency (e.g. every "3" months).'), 'positiveInteger');
281 // Fix frequency unit display for use with frequency_interval
282 $freqUnitsDisplay = array();
283 foreach ($this->_freqUnits
as $val => $label) {
284 $freqUnitsDisplay[$val] = ts('%1(s)', array(1 => $label));
286 $frequencyUnit = $this->add('select', 'frequency_unit',
288 array('' => ts('- select -')) +
$freqUnitsDisplay,
292 $frequencyDay = $this->add('number', 'frequency_day', ts('Payments are due on the'), $attributes['frequency_day'], TRUE);
293 $this->addRule('frequency_day', ts('Please enter a valid payment due day.'), 'positiveInteger');
295 $this->add('text', 'eachPaymentAmount', ts('each'), array(
297 'style' => "background-color:#EBECE4",
298 0 => 'READONLY', // WTF, preserved because its inexplicable
302 $createDate = $this->add('datepicker', 'create_date', ts('Pledge Made'), [], TRUE, ['time' => FALSE]);
303 $startDate = $this->add('datepicker', 'start_date', ts('Payments Start'), [], TRUE, ['time' => FALSE]);
305 if (!empty($this->_values
['currency'])) {
306 $this->assign('currency', $this->_values
['currency']);
308 elseif (!empty($this->_submitValues
['currency'])) {
309 $this->assign('currency', $this->_submitValues
['currency']);
312 if ($this->_id
&& !$this->_isPending
) {
314 $installments->freeze();
315 $createDate->freeze();
316 $startDate->freeze();
317 $frequencyInterval->freeze();
318 $frequencyUnit->freeze();
319 $frequencyDay->freeze();
320 $eachPaymentAmount = $this->_values
['original_installment_amount'];
321 $this->assign('eachPaymentAmount', $eachPaymentAmount);
324 if (CRM_Utils_Array
::value('status_id', $this->_values
) !=
325 CRM_Core_PseudoConstant
::getKey('CRM_Pledge_BAO_Pledge', 'status_id', 'Cancelled')
328 $this->addElement('checkbox', 'is_acknowledge', ts('Send Acknowledgment?'), NULL,
329 array('onclick' => "showHideByValue( 'is_acknowledge', '', 'acknowledgeDate', 'table-row', 'radio', true); showHideByValue( 'is_acknowledge', '', 'fromEmail', 'table-row', 'radio', false );")
332 $this->add('select', 'from_email_address', ts('Receipt From'), $this->_fromEmails
);
335 $this->add('datepicker', 'acknowledge_date', ts('Acknowledgment Date'), [], FALSE, ['time' => FALSE]);
337 $this->add('select', 'financial_type_id',
338 ts('Financial Type'),
339 array('' => ts('- select -')) + CRM_Contribute_PseudoConstant
::financialType(),
343 // CRM-7362 --add campaigns.
344 CRM_Campaign_BAO_Campaign
::addCampaign($this, CRM_Utils_Array
::value('campaign_id', $this->_values
));
347 CRM_Core_DAO
::commonRetrieveAll('CRM_Pledge_DAO_PledgeBlock', 'entity_table',
348 'civicrm_contribution_page', $pageIds, array('entity_id')
350 $pages = CRM_Contribute_PseudoConstant
::contributionPage();
351 $pledgePages = array();
352 foreach ($pageIds as $key => $value) {
353 $pledgePages[$value['entity_id']] = $pages[$value['entity_id']];
355 $this->add('select', 'contribution_page_id', ts('Self-service Payments Page'),
356 array('' => ts('- select -')) +
$pledgePages
359 $mailingInfo = Civi
::settings()->get('mailing_backend');
360 $this->assign('outBound_option', $mailingInfo['outBound_option']);
363 CRM_Custom_Form_CustomData
::buildQuickForm($this);
365 // make this form an upload since we dont know if the custom data injected dynamically
366 // is of type file etc $uploadNames = $this->get( 'uploadNames' );
367 $this->addButtons(array(
370 'name' => ts('Save'),
371 'js' => array('onclick' => "return verify( );"),
376 'name' => ts('Save and New'),
377 'js' => array('onclick' => "return verify( );"),
382 'name' => ts('Cancel'),
387 $this->addFormRule(array('CRM_Pledge_Form_Pledge', 'formRule'), $this);
389 if ($this->_action
& CRM_Core_Action
::VIEW
) {
397 * @param array $fields
398 * The input form values.
399 * @param array $files
400 * The uploaded files if any.
405 * true if no errors, else array of errors
407 public static function formRule($fields, $files, $self) {
410 if ($fields['amount'] <= 0) {
411 $errors['amount'] = ts('Total Pledge Amount should be greater than zero.');
413 if ($fields['installments'] <= 0) {
414 $errors['installments'] = ts('Installments should be greater than zero.');
417 if ($fields['frequency_unit'] != 'week') {
418 if ($fields['frequency_day'] > 31 ||
$fields['frequency_day'] == 0) {
419 $errors['frequency_day'] = ts('Please enter a valid frequency day ie. 1 through 31.');
422 elseif ($fields['frequency_unit'] == 'week') {
423 if ($fields['frequency_day'] > 7 ||
$fields['frequency_day'] == 0) {
424 $errors['frequency_day'] = ts('Please enter a valid frequency day ie. 1 through 7.');
431 * Process the form submission.
433 public function postProcess() {
434 if ($this->_action
& CRM_Core_Action
::DELETE
) {
435 CRM_Pledge_BAO_Pledge
::deletePledge($this->_id
);
439 // get the submitted form values.
440 $formValues = $this->controller
->exportValues($this->_name
);
442 // set the contact, when contact is selected
443 if (!empty($formValues['contact_id'])) {
444 $this->_contactID
= $formValues['contact_id'];
447 $session = CRM_Core_Session
::singleton();
451 'frequency_interval',
455 'initial_reminder_day',
457 'additional_reminder_day',
458 'contribution_page_id',
461 foreach ($fields as $f) {
462 $params[$f] = CRM_Utils_Array
::value($f, $formValues);
466 $params['amount'] = CRM_Utils_Rule
::cleanMoney(CRM_Utils_Array
::value('amount', $formValues));
467 $params['currency'] = CRM_Utils_Array
::value('currency', $formValues);
468 $params['original_installment_amount'] = ($params['amount'] / $params['installments']);
470 $dates = array('create_date', 'start_date', 'acknowledge_date', 'cancel_date');
471 foreach ($dates as $d) {
472 if ($this->_id
&& (!$this->_isPending
) && !empty($this->_values
[$d])) {
473 if ($d == 'start_date') {
474 $params['scheduled_date'] = CRM_Utils_Date
::processDate($this->_values
[$d]);
476 $params[$d] = CRM_Utils_Date
::processDate($this->_values
[$d]);
478 elseif (!empty($formValues[$d]) && !CRM_Utils_System
::isNull($formValues[$d])) {
479 if ($d == 'start_date') {
480 $params['scheduled_date'] = CRM_Utils_Date
::processDate($formValues[$d]);
482 $params[$d] = CRM_Utils_Date
::processDate($formValues[$d]);
485 $params[$d] = 'null';
489 if (!empty($formValues['is_acknowledge'])) {
490 $params['acknowledge_date'] = date('Y-m-d');
493 // assign id only in update mode
494 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
495 $params['id'] = $this->_id
;
498 $params['contact_id'] = $this->_contactID
;
500 // format custom data
501 if (!empty($formValues['hidden_custom'])) {
502 $params['hidden_custom'] = 1;
504 $customFields = CRM_Core_BAO_CustomField
::getFields('Pledge');
505 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess($formValues,
511 // handle pending pledge.
512 $params['is_pledge_pending'] = $this->_isPending
;
514 // create pledge record.
515 $pledge = CRM_Pledge_BAO_Pledge
::create($params);
520 // set the status msg.
521 if ($this->_action
& CRM_Core_Action
::ADD
) {
522 $statusMsg = ts('Pledge has been recorded and the payment schedule has been created.<br />');
524 elseif ($this->_action
& CRM_Core_Action
::UPDATE
) {
525 $statusMsg = ts('Pledge has been updated.<br />');
529 // handle Acknowledgment.
530 if (!empty($formValues['is_acknowledge']) && $pledge->id
) {
532 // calculate scheduled amount.
533 $params['scheduled_amount'] = round($params['amount'] / $params['installments']);
534 $params['total_pledge_amount'] = $params['amount'];
535 // get some required pledge values in params.
536 $params['id'] = $pledge->id
;
537 $params['acknowledge_date'] = $pledge->acknowledge_date
;
538 $params['is_test'] = $pledge->is_test
;
539 $params['currency'] = $pledge->currency
;
540 // retrieve 'from email id' for acknowledgement
541 $params['from_email_id'] = $formValues['from_email_address'];
543 $this->paymentId
= NULL;
544 // send Acknowledgment mail.
545 CRM_Pledge_BAO_Pledge
::sendAcknowledgment($this, $params);
547 if (!isset($this->userEmail
)) {
548 list($this->userDisplayName
,
550 ) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($this->_contactID
);
553 $statusMsg .= ' ' . ts("An acknowledgment email has been sent to %1.<br />", array(1 => $this->userEmail
));
555 // build the payment urls.
556 if ($this->paymentId
) {
557 $urlParams = "reset=1&action=add&cid={$this->_contactID}&ppid={$this->paymentId}&context=pledge";
558 $contribURL = CRM_Utils_System
::url('civicrm/contact/view/contribution', $urlParams);
559 $urlParams .= "&mode=live";
560 $creditURL = CRM_Utils_System
::url('civicrm/contact/view/contribution', $urlParams);
562 // check if we can process credit card payment.
563 $processors = CRM_Core_PseudoConstant
::paymentProcessor(FALSE, FALSE,
564 "billing_mode IN ( 1, 3 )"
566 if (count($processors) > 0) {
567 $statusMsg .= ' ' . ts("If a payment is due now, you can record <a href='%1'>a check, EFT, or cash payment for this pledge</a> OR <a href='%2'>submit a credit card payment</a>.", array(
573 $statusMsg .= ' ' . ts("If a payment is due now, you can record <a href='%1'>a check, EFT, or cash payment for this pledge</a>.", array(1 => $contribURL));
577 CRM_Core_Session
::setStatus($statusMsg, ts('Payment Due'), 'info');
579 $buttonName = $this->controller
->getButtonName();
580 if ($this->_context
== 'standalone') {
581 if ($buttonName == $this->getButtonName('upload', 'new')) {
582 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/pledge/add',
583 'reset=1&action=add&context=standalone'
587 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/contact/view',
588 "reset=1&cid={$this->_contactID}&selectedChild=pledge"
592 elseif ($buttonName == $this->getButtonName('upload', 'new')) {
593 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/contact/view/pledge',
594 "reset=1&action=add&context=pledge&cid={$this->_contactID}"