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