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