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