Merge pull request #21876 from mariav0/patch-14
[civicrm-core.git] / CRM / Pledge / Form / Pledge.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class generates form components for processing a pledge
20 */
21 class CRM_Pledge_Form_Pledge extends CRM_Core_Form {
22 public $_action;
23
24 /**
25 * The id of the pledge that we are proceessing.
26 *
27 * @var int
28 */
29 public $_id;
30
31 /**
32 * The id of the contact associated with this pledge.
33 *
34 * @var int
35 */
36 public $_contactID;
37
38 /**
39 * The Pledge values if an existing pledge.
40 * @var array
41 */
42 public $_values;
43
44 /**
45 * The Pledge frequency Units.
46 * @var array
47 */
48 public $_freqUnits;
49
50 /**
51 * Is current pledge pending.
52 * @var bool
53 */
54 public $_isPending = FALSE;
55
56 /**
57 * Set variables up before form is built.
58 *
59 * @throws \CRM_Core_Exception
60 */
61 public function preProcess() {
62 $this->_contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
63 $this->_action = CRM_Utils_Request::retrieve('action', 'String',
64 $this, FALSE, 'add'
65 );
66 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
67 $this->_context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this);
68
69 // check for action permissions.
70 if (!CRM_Core_Permission::checkActionPermission('CiviPledge', $this->_action)) {
71 CRM_Core_Error::statusBounce(ts('You do not have permission to access this page.'));
72 }
73
74 $this->assign('action', $this->_action);
75 $this->assign('context', $this->_context);
76 if ($this->_action & CRM_Core_Action::DELETE) {
77 return;
78 }
79
80 $this->userDisplayName = $this->userEmail = NULL;
81 if ($this->_contactID) {
82 list($this->userDisplayName,
83 $this->userEmail
84 ) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactID);
85 }
86
87 $this->setPageTitle(ts('Pledge'));
88
89 // build custom data
90 CRM_Custom_Form_CustomData::preProcess($this, NULL, NULL, 1, 'Pledge', $this->_id);
91 $this->_values = [];
92 // current pledge id
93 if ($this->_id) {
94 // get the contribution id
95 $this->_contributionID = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment',
96 $this->_id, 'contribution_id', 'pledge_id'
97 );
98 $params = ['id' => $this->_id];
99 CRM_Pledge_BAO_Pledge::getValues($params, $this->_values);
100
101 $this->_isPending = (CRM_Pledge_BAO_Pledge::pledgeHasFinancialTransactions($this->_id, CRM_Utils_Array::value('status_id', $this->_values))) ? FALSE : TRUE;
102 }
103
104 // get the pledge frequency units.
105 $this->_freqUnits = CRM_Core_OptionGroup::values('recur_frequency_units');
106
107 $this->_fromEmails = CRM_Core_BAO_Email::getFromEmail();
108 }
109
110 /**
111 * Set default values for the form.
112 * The default values are retrieved from the database.
113 */
114 public function setDefaultValues() {
115 $defaults = $this->_values;
116
117 if ($this->_action & CRM_Core_Action::DELETE) {
118 return $defaults;
119 }
120
121 if (!empty($defaults['is_test'])) {
122 $this->assign('is_test', TRUE);
123 }
124
125 if ($this->_id) {
126 // check is this pledge pending.
127 // fix the display of the monetary value, CRM-4038.
128 if ($this->_isPending) {
129 $defaults['eachPaymentAmount'] = $this->_values['amount'] / $this->_values['installments'];
130 $defaults['eachPaymentAmount'] = CRM_Utils_Money::formatLocaleNumericRoundedForDefaultCurrency($defaults['eachPaymentAmount']);
131 }
132
133 // fix the display of the monetary value, CRM-4038
134 if (isset($this->_values['amount'])) {
135 $defaults['amount'] = CRM_Utils_Money::formatLocaleNumericRoundedForDefaultCurrency($this->_values['amount']);
136 }
137 $this->assign('amount', $this->_values['amount']);
138 $this->assign('installments', $defaults['installments']);
139 }
140 else {
141 if ($this->_contactID) {
142 $defaults['contact_id'] = $this->_contactID;
143 }
144 // default values.
145 $defaults['create_date'] = date('Y-m-d');
146 $defaults['start_date'] = date('Y-m-d');
147 $defaults['installments'] = 12;
148 $defaults['frequency_interval'] = 1;
149 $defaults['frequency_day'] = 1;
150 $defaults['initial_reminder_day'] = 5;
151 $defaults['max_reminders'] = 1;
152 $defaults['additional_reminder_day'] = 5;
153 $defaults['frequency_unit'] = array_search('month', $this->_freqUnits);
154 $defaults['financial_type_id'] = array_search('Donation', CRM_Contribute_PseudoConstant::financialType());
155 }
156
157 $pledgeStatus = CRM_Pledge_BAO_Pledge::buildOptions('status_id');
158 $pledgeStatusNames = CRM_Core_OptionGroup::values('pledge_status',
159 FALSE, FALSE, FALSE, NULL, 'name', TRUE
160 );
161 // get default status label (pending)
162 $defaultPledgeStatus = CRM_Utils_Array::value(array_search('Pending', $pledgeStatusNames),
163 $pledgeStatus
164 );
165
166 // assign status.
167 $this->assign('status', CRM_Utils_Array::value(CRM_Utils_Array::value('status_id', $this->_values),
168 $pledgeStatus,
169 $defaultPledgeStatus
170 ));
171
172 if (isset($this->userEmail)) {
173 $this->assign('email', $this->userEmail);
174 }
175
176 // custom data set defaults
177 $defaults += CRM_Custom_Form_CustomData::setDefaultValues($this);
178
179 return $defaults;
180 }
181
182 /**
183 * Build the form object.
184 */
185 public function buildQuickForm() {
186 if ($this->_action & CRM_Core_Action::DELETE) {
187 $this->addButtons([
188 [
189 'type' => 'next',
190 'name' => ts('Delete'),
191 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
192 'isDefault' => TRUE,
193 ],
194 [
195 'type' => 'cancel',
196 'name' => ts('Cancel'),
197 ],
198 ]);
199 return;
200 }
201
202 $contactField = $this->addEntityRef('contact_id', ts('Pledge by'), ['create' => TRUE, 'api' => ['extra' => ['email']]], TRUE);
203 if ($this->_context != 'standalone') {
204 $contactField->freeze();
205 }
206
207 $showAdditionalInfo = FALSE;
208 $this->_formType = $_GET['formType'] ?? NULL;
209
210 $defaults = [];
211
212 $paneNames = [
213 'Payment Reminders' => 'PaymentReminders',
214 ];
215 foreach ($paneNames as $name => $type) {
216 $urlParams = "snippet=4&formType={$type}";
217 $allPanes[$name] = [
218 'url' => CRM_Utils_System::url('civicrm/contact/view/pledge', $urlParams),
219 'open' => 'false',
220 'id' => $type,
221 ];
222 // see if we need to include this paneName in the current form
223 if ($this->_formType == $type || !empty($_POST["hidden_{$type}"]) ||
224 !empty($defaults["hidden_{$type}"])
225 ) {
226 $showAdditionalInfo = TRUE;
227 $allPanes[$name]['open'] = 'true';
228 }
229 $fnName = "build{$type}";
230 CRM_Contribute_Form_AdditionalInfo::$fnName($this);
231 }
232
233 $this->assign('allPanes', $allPanes);
234 $this->assign('showAdditionalInfo', $showAdditionalInfo);
235
236 if ($this->_formType) {
237 $this->assign('formType', $this->_formType);
238 return;
239 }
240
241 $this->applyFilter('__ALL__', 'trim');
242
243 // pledge fields.
244 $attributes = CRM_Core_DAO::getAttribute('CRM_Pledge_DAO_Pledge');
245
246 $this->assign('isPending', $this->_isPending);
247
248 $js = [
249 'onblur' => "calculatedPaymentAmount( );",
250 'onkeyup' => "calculatedPaymentAmount( );",
251 ];
252
253 $amount = $this->addMoney('amount', ts('Total Pledge Amount'), TRUE,
254 array_merge($attributes['pledge_amount'], $js), TRUE,
255 'currency', NULL, $this->_id && !$this->_isPending
256 );
257
258 $installments = &$this->add('text', 'installments', ts('To be paid in'),
259 array_merge($attributes['installments'], $js), TRUE
260 );
261 $this->addRule('installments', ts('Please enter a valid number of installments.'), 'positiveInteger');
262
263 $frequencyInterval = $this->add('number', 'frequency_interval', ts('every'),
264 $attributes['pledge_frequency_interval'], TRUE
265 );
266 $this->addRule('frequency_interval', ts('Please enter a number for frequency (e.g. every "3" months).'), 'positiveInteger');
267
268 // Fix frequency unit display for use with frequency_interval
269 $freqUnitsDisplay = [];
270 foreach ($this->_freqUnits as $val => $label) {
271 $freqUnitsDisplay[$val] = ts('%1(s)', [1 => $label]);
272 }
273 $frequencyUnit = $this->add('select', 'frequency_unit',
274 ts('Frequency'),
275 ['' => ts('- select -')] + $freqUnitsDisplay,
276 TRUE
277 );
278
279 $frequencyDay = $this->add('number', 'frequency_day', ts('Payments are due on the'), $attributes['frequency_day'], TRUE);
280 $this->addRule('frequency_day', ts('Please enter a valid payment due day.'), 'positiveInteger');
281
282 $this->add('text', 'eachPaymentAmount', ts('each'), [
283 'size' => 10,
284 'style' => "background-color:#EBECE4",
285 // WTF, preserved because its inexplicable
286 0 => 'READONLY',
287 ]);
288
289 // add various dates
290 $createDate = $this->add('datepicker', 'create_date', ts('Pledge Made'), [], TRUE, ['time' => FALSE]);
291 $startDate = $this->add('datepicker', 'start_date', ts('Payments Start'), [], TRUE, ['time' => FALSE]);
292
293 if (!empty($this->_values['currency'])) {
294 $this->assign('currency', $this->_values['currency']);
295 }
296 elseif (!empty($this->_submitValues['currency'])) {
297 $this->assign('currency', $this->_submitValues['currency']);
298 }
299
300 if ($this->_id && !$this->_isPending) {
301 $amount->freeze();
302 $installments->freeze();
303 $createDate->freeze();
304 $startDate->freeze();
305 $frequencyInterval->freeze();
306 $frequencyUnit->freeze();
307 $frequencyDay->freeze();
308 $eachPaymentAmount = $this->_values['original_installment_amount'];
309 $this->assign('eachPaymentAmount', $eachPaymentAmount);
310 }
311
312 if (CRM_Utils_Array::value('status_id', $this->_values) !=
313 CRM_Core_PseudoConstant::getKey('CRM_Pledge_BAO_Pledge', 'status_id', 'Cancelled')
314 ) {
315
316 $this->addElement('checkbox', 'is_acknowledge', ts('Send Acknowledgment?'), NULL,
317 ['onclick' => "showHideByValue( 'is_acknowledge', '', 'acknowledgeDate', 'table-row', 'radio', true); showHideByValue( 'is_acknowledge', '', 'fromEmail', 'table-row', 'radio', false );"]
318 );
319
320 $this->add('select', 'from_email_address', ts('Receipt From'), $this->_fromEmails);
321 }
322
323 $this->add('datepicker', 'acknowledge_date', ts('Acknowledgment Date'), [], FALSE, ['time' => FALSE]);
324
325 $this->add('select', 'financial_type_id',
326 ts('Financial Type'),
327 ['' => ts('- select -')] + CRM_Contribute_PseudoConstant::financialType(),
328 TRUE
329 );
330
331 // CRM-7362 --add campaigns.
332 CRM_Campaign_BAO_Campaign::addCampaign($this, CRM_Utils_Array::value('campaign_id', $this->_values));
333
334 $pageIds = [];
335 CRM_Core_DAO::commonRetrieveAll('CRM_Pledge_DAO_PledgeBlock', 'entity_table',
336 'civicrm_contribution_page', $pageIds, ['entity_id']
337 );
338 $pages = CRM_Contribute_PseudoConstant::contributionPage();
339 $pledgePages = [];
340 foreach ($pageIds as $key => $value) {
341 $pledgePages[$value['entity_id']] = $pages[$value['entity_id']];
342 }
343 $this->add('select', 'contribution_page_id', ts('Self-service Payments Page'),
344 ['' => ts('- select -')] + $pledgePages
345 );
346
347 $mailingInfo = Civi::settings()->get('mailing_backend');
348 $this->assign('outBound_option', $mailingInfo['outBound_option']);
349
350 // build custom data
351 CRM_Custom_Form_CustomData::buildQuickForm($this);
352
353 // make this form an upload since we dont know if the custom data injected dynamically
354 // is of type file etc $uploadNames = $this->get( 'uploadNames' );
355 $this->addButtons([
356 [
357 'type' => 'upload',
358 'name' => ts('Save'),
359 'js' => ['onclick' => "return verify( );"],
360 'isDefault' => TRUE,
361 ],
362 [
363 'type' => 'upload',
364 'name' => ts('Save and New'),
365 'js' => ['onclick' => "return verify( );"],
366 'subName' => 'new',
367 ],
368 [
369 'type' => 'cancel',
370 'name' => ts('Cancel'),
371 ],
372 ]);
373
374 $this->addFormRule(['CRM_Pledge_Form_Pledge', 'formRule'], $this);
375
376 if ($this->_action & CRM_Core_Action::VIEW) {
377 $this->freeze();
378 }
379 }
380
381 /**
382 * Global form rule.
383 *
384 * @param array $fields
385 * The input form values.
386 * @param array $files
387 * The uploaded files if any.
388 * @param $self
389 *
390 *
391 * @return bool|array
392 * true if no errors, else array of errors
393 */
394 public static function formRule($fields, $files, $self) {
395 $errors = [];
396
397 if ($fields['amount'] <= 0) {
398 $errors['amount'] = ts('Total Pledge Amount should be greater than zero.');
399 }
400 if ($fields['installments'] <= 0) {
401 $errors['installments'] = ts('Installments should be greater than zero.');
402 }
403
404 if ($fields['frequency_unit'] != 'week') {
405 if ($fields['frequency_day'] > 31 || $fields['frequency_day'] == 0) {
406 $errors['frequency_day'] = ts('Please enter a valid frequency day ie. 1 through 31.');
407 }
408 }
409 elseif ($fields['frequency_unit'] == 'week') {
410 if ($fields['frequency_day'] > 7 || $fields['frequency_day'] == 0) {
411 $errors['frequency_day'] = ts('Please enter a valid frequency day ie. 1 through 7.');
412 }
413 }
414 return $errors;
415 }
416
417 /**
418 * Process the form submission.
419 */
420 public function postProcess() {
421 if ($this->_action & CRM_Core_Action::DELETE) {
422 CRM_Pledge_BAO_Pledge::deletePledge($this->_id);
423 return;
424 }
425
426 // get the submitted form values.
427 $formValues = $this->controller->exportValues($this->_name);
428
429 // set the contact, when contact is selected
430 if (!empty($formValues['contact_id'])) {
431 $this->_contactID = $formValues['contact_id'];
432 }
433
434 $session = CRM_Core_Session::singleton();
435
436 $fields = [
437 'frequency_unit',
438 'frequency_interval',
439 'frequency_day',
440 'installments',
441 'financial_type_id',
442 'initial_reminder_day',
443 'max_reminders',
444 'additional_reminder_day',
445 'contribution_page_id',
446 'campaign_id',
447 ];
448 foreach ($fields as $f) {
449 $params[$f] = $formValues[$f] ?? NULL;
450 }
451
452 // format amount
453 $params['amount'] = CRM_Utils_Rule::cleanMoney(CRM_Utils_Array::value('amount', $formValues));
454 $params['currency'] = $formValues['currency'] ?? NULL;
455 $params['original_installment_amount'] = ($params['amount'] / $params['installments']);
456
457 $dates = ['create_date', 'start_date', 'acknowledge_date', 'cancel_date'];
458 foreach ($dates as $d) {
459 if ($this->_id && (!$this->_isPending) && !empty($this->_values[$d])) {
460 if ($d == 'start_date') {
461 $params['scheduled_date'] = CRM_Utils_Date::processDate($this->_values[$d]);
462 }
463 $params[$d] = CRM_Utils_Date::processDate($this->_values[$d]);
464 }
465 elseif (!empty($formValues[$d]) && !CRM_Utils_System::isNull($formValues[$d])) {
466 if ($d == 'start_date') {
467 $params['scheduled_date'] = CRM_Utils_Date::processDate($formValues[$d]);
468 }
469 $params[$d] = CRM_Utils_Date::processDate($formValues[$d]);
470 }
471 else {
472 $params[$d] = 'null';
473 }
474 }
475
476 if (!empty($formValues['is_acknowledge'])) {
477 $params['acknowledge_date'] = date('Y-m-d');
478 }
479
480 // assign id only in update mode
481 if ($this->_action & CRM_Core_Action::UPDATE) {
482 $params['id'] = $this->_id;
483 }
484
485 $params['contact_id'] = $this->_contactID;
486
487 // format custom data
488 if (!empty($formValues['hidden_custom'])) {
489 $params['hidden_custom'] = 1;
490
491 $params['custom'] = CRM_Core_BAO_CustomField::postProcess($formValues,
492 $this->_id,
493 'Pledge'
494 );
495 }
496
497 // handle pending pledge.
498 $params['is_pledge_pending'] = $this->_isPending;
499
500 // create pledge record.
501 $pledge = CRM_Pledge_BAO_Pledge::create($params);
502
503 $statusMsg = NULL;
504
505 if ($pledge->id) {
506 // set the status msg.
507 if ($this->_action & CRM_Core_Action::ADD) {
508 $statusMsg = ts('Pledge has been recorded and the payment schedule has been created.<br />');
509 }
510 elseif ($this->_action & CRM_Core_Action::UPDATE) {
511 $statusMsg = ts('Pledge has been updated.<br />');
512 }
513 }
514
515 // handle Acknowledgment.
516 if (!empty($formValues['is_acknowledge']) && $pledge->id) {
517
518 // calculate scheduled amount.
519 $params['scheduled_amount'] = round($params['amount'] / $params['installments']);
520 $params['total_pledge_amount'] = $params['amount'];
521 // get some required pledge values in params.
522 $params['id'] = $pledge->id;
523 $params['acknowledge_date'] = $pledge->acknowledge_date;
524 $params['is_test'] = $pledge->is_test;
525 $params['currency'] = $pledge->currency;
526 // retrieve 'from email id' for acknowledgement
527 $params['from_email_id'] = $formValues['from_email_address'];
528
529 $this->paymentId = NULL;
530 // send Acknowledgment mail.
531 CRM_Pledge_BAO_Pledge::sendAcknowledgment($this, $params);
532
533 $statusMsg .= ' ' . ts("An acknowledgment email has been sent to %1.<br />", [1 => $this->userEmail]);
534
535 // build the payment urls.
536 if ($this->paymentId) {
537 $urlParams = "reset=1&action=add&cid={$this->_contactID}&ppid={$this->paymentId}&context=pledge";
538 $contribURL = CRM_Utils_System::url('civicrm/contact/view/contribution', $urlParams);
539 $urlParams .= "&mode=live";
540 $creditURL = CRM_Utils_System::url('civicrm/contact/view/contribution', $urlParams);
541
542 // check if we can process credit card payment.
543 $processors = CRM_Core_PseudoConstant::paymentProcessor(FALSE, FALSE,
544 "billing_mode IN ( 1, 3 )"
545 );
546 if (count($processors) > 0) {
547 $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>.", [
548 1 => $contribURL,
549 2 => $creditURL,
550 ]);
551 }
552 else {
553 $statusMsg .= ' ' . ts("If a payment is due now, you can record <a href='%1'>a check, EFT, or cash payment for this pledge</a>.", [1 => $contribURL]);
554 }
555 }
556 }
557 CRM_Core_Session::setStatus($statusMsg, ts('Payment Due'), 'info');
558
559 $buttonName = $this->controller->getButtonName();
560 if ($this->_context == 'standalone') {
561 if ($buttonName == $this->getButtonName('upload', 'new')) {
562 $session->replaceUserContext(CRM_Utils_System::url('civicrm/pledge/add',
563 'reset=1&action=add&context=standalone'
564 ));
565 }
566 else {
567 $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view',
568 "reset=1&cid={$this->_contactID}&selectedChild=pledge"
569 ));
570 }
571 }
572 elseif ($buttonName == $this->getButtonName('upload', 'new')) {
573 $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view/pledge',
574 "reset=1&action=add&context=pledge&cid={$this->_contactID}"
575 ));
576 }
577 }
578
579 }