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