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 | /** | |
100fef9d | 44 | * The id of the pledge that we are proceessing |
6a488035 TO |
45 | * |
46 | * @var int | |
47 | * @public | |
48 | */ | |
49 | public $_id; | |
50 | ||
51 | /** | |
100fef9d | 52 | * The id of the contact associated with this pledge |
6a488035 TO |
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 | /** | |
100fef9d | 72 | * Is current pledge pending. |
6a488035 TO |
73 | * @public |
74 | */ | |
75 | public $_isPending = FALSE; | |
76 | ||
77 | /** | |
100fef9d | 78 | * Set variables up before form is built |
6a488035 TO |
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)) { | |
0499b0ad | 93 | CRM_Core_Error::fatal(ts('You do not have permission to access this page.')); |
6a488035 TO |
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 | 134 | /** |
c490a46a | 135 | * Set default values for the form. |
6a488035 TO |
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 | /** | |
c490a46a | 222 | * Build the form object |
6a488035 | 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 | /** | |
100fef9d | 449 | * Global form rule |
6a488035 | 450 | * |
2a6da8d7 EM |
451 | * @param array $fields the input form values |
452 | * @param array $files the uploaded files if any | |
453 | * @param $self | |
454 | * | |
6a488035 TO |
455 | * |
456 | * @return true if no errors, else array of errors | |
457 | * @access public | |
458 | * @static | |
459 | */ | |
460 | static function formRule($fields, $files, $self) { | |
461 | $errors = array(); | |
462 | ||
6a488035 TO |
463 | if ($fields['amount'] <= 0) { |
464 | $errors['amount'] = ts('Total Pledge Amount should be greater than zero.'); | |
465 | } | |
466 | if ($fields['installments'] <= 0) { | |
467 | $errors['installments'] = ts('Installments should be greater than zero.'); | |
468 | } | |
469 | ||
470 | if ($fields['frequency_unit'] != 'week') { | |
471 | if ($fields['frequency_day'] > 31 || $fields['frequency_day'] == 0) { | |
472 | $errors['frequency_day'] = ts('Please enter a valid frequency day ie. 1 through 31.'); | |
473 | } | |
474 | } | |
475 | elseif ($fields['frequency_unit'] == 'week') { | |
476 | if ($fields['frequency_day'] > 7 || $fields['frequency_day'] == 0) { | |
477 | $errors['frequency_day'] = ts('Please enter a valid frequency day ie. 1 through 7.'); | |
478 | } | |
479 | } | |
480 | return $errors; | |
481 | } | |
482 | ||
483 | /** | |
c490a46a | 484 | * Process the form submission |
6a488035 TO |
485 | * |
486 | * @access public | |
487 | * | |
355ba699 | 488 | * @return void |
6a488035 TO |
489 | */ |
490 | public function postProcess() { | |
491 | if ($this->_action & CRM_Core_Action::DELETE) { | |
492 | CRM_Pledge_BAO_Pledge::deletePledge($this->_id); | |
493 | return; | |
494 | } | |
495 | ||
496 | //get the submitted form values. | |
497 | $formValues = $this->controller->exportValues($this->_name); | |
498 | ||
499 | // set the contact, when contact is selected | |
b9aa8f56 CW |
500 | if (!empty($formValues['contact_id'])) { |
501 | $this->_contactID = $formValues['contact_id']; | |
6a488035 TO |
502 | } |
503 | ||
6a488035 TO |
504 | $session = CRM_Core_Session::singleton(); |
505 | ||
506 | //get All Payments status types. | |
507 | $paymentStatusTypes = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
508 | ||
509 | $fields = array( | |
510 | 'frequency_unit', | |
511 | 'frequency_interval', | |
512 | 'frequency_day', | |
513 | 'installments', | |
b9aa8f56 | 514 | 'financial_type_id', |
6a488035 TO |
515 | 'initial_reminder_day', |
516 | 'max_reminders', | |
517 | 'additional_reminder_day', | |
6a488035 TO |
518 | 'contribution_page_id', |
519 | 'campaign_id', | |
520 | ); | |
521 | foreach ($fields as $f) { | |
522 | $params[$f] = CRM_Utils_Array::value($f, $formValues); | |
523 | } | |
524 | ||
525 | //defaults status is "Pending". | |
526 | //if update get status. | |
527 | if ($this->_id) { | |
528 | $params['pledge_status_id'] = $params['status_id'] = $this->_values['status_id']; | |
529 | } | |
530 | else { | |
531 | $params['pledge_status_id'] = $params['status_id'] = array_search('Pending', $paymentStatusTypes); | |
532 | } | |
533 | //format amount | |
534 | $params['amount'] = CRM_Utils_Rule::cleanMoney(CRM_Utils_Array::value('amount', $formValues)); | |
535 | $params['currency'] = CRM_Utils_Array::value('currency', $formValues); | |
536 | $params['original_installment_amount'] = ($params['amount'] / $params['installments']); | |
537 | ||
538 | $dates = array('create_date', 'start_date', 'acknowledge_date', 'cancel_date'); | |
539 | foreach ($dates as $d) { | |
8cc574cf | 540 | if ($this->_id && (!$this->_isPending) && !empty($this->_values[$d])) { |
6a488035 TO |
541 | if ($d == 'start_date') { |
542 | $params['scheduled_date'] = CRM_Utils_Date::processDate($this->_values[$d]); | |
543 | } | |
544 | $params[$d] = CRM_Utils_Date::processDate($this->_values[$d]); | |
545 | } | |
a7488080 | 546 | elseif (!empty($formValues[$d]) && !CRM_Utils_System::isNull($formValues[$d])) { |
6a488035 TO |
547 | if ($d == 'start_date') { |
548 | $params['scheduled_date'] = CRM_Utils_Date::processDate($formValues[$d]); | |
549 | } | |
550 | $params[$d] = CRM_Utils_Date::processDate($formValues[$d]); | |
551 | } | |
552 | else { | |
553 | $params[$d] = 'null'; | |
554 | } | |
555 | } | |
556 | ||
a7488080 | 557 | if (!empty($formValues['is_acknowledge'])) { |
6a488035 TO |
558 | $params['acknowledge_date'] = date('Y-m-d'); |
559 | } | |
560 | ||
561 | // assign id only in update mode | |
562 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
563 | $params['id'] = $this->_id; | |
564 | } | |
565 | ||
566 | $params['contact_id'] = $this->_contactID; | |
567 | ||
6a488035 | 568 | //format custom data |
a7488080 | 569 | if (!empty($formValues['hidden_custom'])) { |
6a488035 TO |
570 | $params['hidden_custom'] = 1; |
571 | ||
572 | $customFields = CRM_Core_BAO_CustomField::getFields('Pledge'); | |
573 | $params['custom'] = CRM_Core_BAO_CustomField::postProcess($formValues, | |
574 | $customFields, | |
575 | $this->_id, | |
576 | 'Pledge' | |
577 | ); | |
578 | } | |
579 | ||
580 | //handle pending pledge. | |
581 | $params['is_pledge_pending'] = $this->_isPending; | |
582 | ||
583 | //create pledge record. | |
584 | $pledge = CRM_Pledge_BAO_Pledge::create($params); | |
585 | ||
586 | $statusMsg = NULL; | |
587 | ||
588 | if ($pledge->id) { | |
589 | //set the status msg. | |
590 | if ($this->_action & CRM_Core_Action::ADD) { | |
591 | $statusMsg = ts('Pledge has been recorded and the payment schedule has been created.<br />'); | |
592 | } | |
593 | elseif ($this->_action & CRM_Core_Action::UPDATE) { | |
594 | $statusMsg = ts('Pledge has been updated.<br />'); | |
595 | } | |
596 | } | |
597 | ||
598 | //handle Acknowledgment. | |
a7488080 | 599 | if (!empty($formValues['is_acknowledge']) && $pledge->id) { |
6a488035 TO |
600 | |
601 | //calculate scheduled amount. | |
602 | $params['scheduled_amount'] = round($params['amount'] / $params['installments']); | |
603 | $params['total_pledge_amount'] = $params['amount']; | |
604 | //get some required pledge values in params. | |
605 | $params['id'] = $pledge->id; | |
606 | $params['acknowledge_date'] = $pledge->acknowledge_date; | |
607 | $params['is_test'] = $pledge->is_test; | |
608 | $params['currency'] = $pledge->currency; | |
609 | // retrieve 'from email id' for acknowledgement | |
610 | $params['from_email_id'] = $formValues['from_email_address']; | |
611 | ||
612 | $this->paymentId = NULL; | |
613 | //send Acknowledgment mail. | |
614 | CRM_Pledge_BAO_Pledge::sendAcknowledgment($this, $params); | |
615 | ||
616 | if (!isset($this->userEmail)) { | |
617 | list($this->userDisplayName, | |
618 | $this->userEmail | |
619 | ) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactID); | |
620 | } | |
621 | ||
622 | $statusMsg .= ' ' . ts("An acknowledgment email has been sent to %1.<br />", array(1 => $this->userEmail)); | |
623 | ||
624 | //build the payment urls. | |
625 | if ($this->paymentId) { | |
626 | $urlParams = "reset=1&action=add&cid={$this->_contactID}&ppid={$this->paymentId}&context=pledge"; | |
627 | $contribURL = CRM_Utils_System::url('civicrm/contact/view/contribution', $urlParams); | |
628 | $urlParams .= "&mode=live"; | |
629 | $creditURL = CRM_Utils_System::url('civicrm/contact/view/contribution', $urlParams); | |
630 | ||
631 | //check if we can process credit card payment. | |
632 | $processors = CRM_Core_PseudoConstant::paymentProcessor(FALSE, FALSE, | |
633 | "billing_mode IN ( 1, 3 )" | |
634 | ); | |
635 | if (count($processors) > 0) { | |
636 | $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)); | |
637 | } | |
638 | else { | |
639 | $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)); | |
640 | } | |
641 | } | |
642 | } | |
643 | CRM_Core_Session::setStatus($statusMsg, ts('Payment Due'), 'info'); | |
644 | ||
645 | $buttonName = $this->controller->getButtonName(); | |
646 | if ($this->_context == 'standalone') { | |
647 | if ($buttonName == $this->getButtonName('upload', 'new')) { | |
648 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/pledge/add', | |
649 | 'reset=1&action=add&context=standalone' | |
650 | )); | |
651 | } | |
652 | else { | |
653 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view', | |
654 | "reset=1&cid={$this->_contactID}&selectedChild=pledge" | |
655 | )); | |
656 | } | |
657 | } | |
658 | elseif ($buttonName == $this->getButtonName('upload', 'new')) { | |
659 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view/pledge', | |
660 | "reset=1&action=add&context=pledge&cid={$this->_contactID}" | |
661 | )); | |
662 | } | |
663 | } | |
664 | } | |
665 |