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