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