Fix pledge payment not to refer to contribution status
[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 use Civi\Api4\PledgePayment;
19
20 /**
21 * This class generates form components for processing a pledge
22 */
23 class CRM_Pledge_Form_Pledge extends CRM_Core_Form {
24 public $_action;
25
26 /**
27 * The id of the pledge that we are proceessing.
28 *
29 * @var int
30 */
31 public $_id;
32
33 /**
34 * The id of the contact associated with this pledge.
35 *
36 * @var int
37 */
38 public $_contactID;
39
40 /**
41 * The Pledge values if an existing pledge.
42 * @var array
43 */
44 public $_values;
45
46 /**
47 * The Pledge frequency Units.
48 * @var array
49 */
50 public $_freqUnits;
51
52 /**
53 * Is current pledge pending.
54 * @var bool
55 */
56 public $_isPending = FALSE;
57
58 /**
59 * Set variables up before form is built.
60 *
61 * @throws \CRM_Core_Exception
62 */
63 public function preProcess() {
64 $this->_contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
65 $this->_action = CRM_Utils_Request::retrieve('action', 'String',
66 $this, FALSE, 'add'
67 );
68 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
69 $this->_context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this);
70
71 // check for action permissions.
72 if (!CRM_Core_Permission::checkActionPermission('CiviPledge', $this->_action)) {
73 CRM_Core_Error::statusBounce(ts('You do not have permission to access this page.'));
74 }
75
76 $this->assign('action', $this->_action);
77 $this->assign('context', $this->_context);
78 if ($this->_action & CRM_Core_Action::DELETE) {
79 return;
80 }
81
82 $this->userDisplayName = $this->userEmail = NULL;
83 if ($this->_contactID) {
84 [$this->userDisplayName, $this->userEmail] = 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 ts('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 $this->assign('formType', $this->_formType);
237 if ($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 $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 * @throws \API_Exception
421 */
422 public function postProcess(): void {
423 if ($this->_action & CRM_Core_Action::DELETE) {
424 CRM_Pledge_BAO_Pledge::deletePledge($this->_id);
425 return;
426 }
427
428 // get the submitted form values.
429 $formValues = $this->controller->exportValues($this->_name);
430
431 // set the contact, when contact is selected
432 if (!empty($formValues['contact_id'])) {
433 $this->_contactID = $formValues['contact_id'];
434 }
435
436 $session = CRM_Core_Session::singleton();
437
438 $fields = [
439 'frequency_unit',
440 'frequency_interval',
441 'frequency_day',
442 'installments',
443 'financial_type_id',
444 'initial_reminder_day',
445 'max_reminders',
446 'additional_reminder_day',
447 'contribution_page_id',
448 'campaign_id',
449 ];
450 foreach ($fields as $f) {
451 $params[$f] = $formValues[$f] ?? NULL;
452 }
453
454 // format amount
455 $params['amount'] = CRM_Utils_Rule::cleanMoney(CRM_Utils_Array::value('amount', $formValues));
456 $params['currency'] = $formValues['currency'] ?? NULL;
457 $params['original_installment_amount'] = ($params['amount'] / $params['installments']);
458
459 $dates = ['create_date', 'start_date', 'acknowledge_date', 'cancel_date'];
460 foreach ($dates as $d) {
461 if ($this->_id && (!$this->_isPending) && !empty($this->_values[$d])) {
462 if ($d === 'start_date') {
463 $params['scheduled_date'] = CRM_Utils_Date::processDate($this->_values[$d]);
464 }
465 $params[$d] = CRM_Utils_Date::processDate($this->_values[$d]);
466 }
467 elseif (!empty($formValues[$d]) && !CRM_Utils_System::isNull($formValues[$d])) {
468 if ($d === 'start_date') {
469 $params['scheduled_date'] = CRM_Utils_Date::processDate($formValues[$d]);
470 }
471 $params[$d] = CRM_Utils_Date::processDate($formValues[$d]);
472 }
473 else {
474 $params[$d] = 'null';
475 }
476 }
477
478 if (!empty($formValues['is_acknowledge'])) {
479 $params['acknowledge_date'] = date('Y-m-d');
480 }
481
482 // assign id only in update mode
483 if ($this->_action & CRM_Core_Action::UPDATE) {
484 $params['id'] = $this->_id;
485 }
486
487 $params['contact_id'] = $this->_contactID;
488
489 // format custom data
490 if (!empty($formValues['hidden_custom'])) {
491 $params['hidden_custom'] = 1;
492
493 $params['custom'] = CRM_Core_BAO_CustomField::postProcess($formValues,
494 $this->_id,
495 'Pledge'
496 );
497 }
498
499 // handle pending pledge.
500 $params['is_pledge_pending'] = $this->_isPending;
501
502 // create pledge record.
503 $pledge = CRM_Pledge_BAO_Pledge::create($params);
504 $pledgeID = $pledge->id;
505
506 $statusMsg = NULL;
507
508 if ($pledgeID) {
509 // set the status msg.
510 if ($this->_action & CRM_Core_Action::ADD) {
511 $statusMsg = ts('Pledge has been recorded and the payment schedule has been created.<br />');
512 }
513 elseif ($this->_action & CRM_Core_Action::UPDATE) {
514 $statusMsg = ts('Pledge has been updated.<br />');
515 }
516 }
517
518 // handle Acknowledgment.
519 if (!empty($formValues['is_acknowledge'])) {
520
521 // calculate scheduled amount.
522 $params['scheduled_amount'] = round($params['amount'] / $params['installments']);
523 $params['total_pledge_amount'] = $params['amount'];
524 // get some required pledge values in params.
525 $params['id'] = $pledgeID;
526 $params['acknowledge_date'] = $pledge->acknowledge_date;
527 $params['is_test'] = $pledge->is_test;
528 $params['currency'] = $pledge->currency;
529 // retrieve 'from email id' for acknowledgement
530 $params['from_email_id'] = $formValues['from_email_address'];
531
532 // send Acknowledgment mail.
533 CRM_Pledge_BAO_Pledge::sendAcknowledgment($this, $params);
534
535 $statusMsg .= ' ' . ts("An acknowledgment email has been sent to %1.<br />", [1 => $this->userEmail]);
536 // get the first valid payment id.
537 $nextPaymentID = PledgePayment::get()
538 ->addWhere('pledge_id', '=', $pledgeID)
539 ->addWhere('status_id:name', 'IN', ['Pending', 'Overdue'])
540 ->addOrderBy('scheduled_date')->setLimit(1)->execute()->first()['id'];
541 // build the payment urls.
542 if ($nextPaymentID) {
543 $urlParams = "reset=1&action=add&cid={$this->_contactID}&ppid={$nextPaymentID}&context=pledge";
544 $contribURL = CRM_Utils_System::url('civicrm/contact/view/contribution', $urlParams);
545 $urlParams .= "&mode=live";
546 $creditURL = CRM_Utils_System::url('civicrm/contact/view/contribution', $urlParams);
547
548 // check if we can process credit card payment.
549 $processors = CRM_Core_PseudoConstant::paymentProcessor(FALSE, FALSE,
550 "billing_mode IN ( 1, 3 )"
551 );
552 if (count($processors) > 0) {
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> OR <a href='%2'>submit a credit card payment</a>.", [
554 1 => $contribURL,
555 2 => $creditURL,
556 ]);
557 }
558 else {
559 $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]);
560 }
561 }
562 }
563 CRM_Core_Session::setStatus($statusMsg, ts('Payment Due'), 'info');
564
565 $buttonName = $this->controller->getButtonName();
566 if ($this->_context == 'standalone') {
567 if ($buttonName == $this->getButtonName('upload', 'new')) {
568 $session->replaceUserContext(CRM_Utils_System::url('civicrm/pledge/add',
569 'reset=1&action=add&context=standalone'
570 ));
571 }
572 else {
573 $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view',
574 "reset=1&cid={$this->_contactID}&selectedChild=pledge"
575 ));
576 }
577 }
578 elseif ($buttonName == $this->getButtonName('upload', 'new')) {
579 $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view/pledge',
580 "reset=1&action=add&context=pledge&cid={$this->_contactID}"
581 ));
582 }
583 }
584
585 }