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