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