dev/core#1410 Fix E-notice when doin a force case search with a predefined case subje...
[civicrm-core.git] / CRM / Contribute / Form / AdditionalInfo.php
CommitLineData
6a488035
TO
1<?php
2/*
bc77d7c0
TO
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
e70a7fc0 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17class CRM_Contribute_Form_AdditionalInfo {
18
19 /**
c490a46a 20 * Build the form object for Premium Information.
6a488035 21 *
3e6a1f4a 22 * Called from the CRM_Contribute_Form_Contribute function and seemingly nowhere else.
6a488035 23 *
3e6a1f4a 24 * Probably this should be on the form that uses it since it is not used on multiple forms.
dd244018 25 *
3e6a1f4a
EM
26 * Putting it on this class doesn't seem to reduce complexity.
27 *
28 * @param CRM_Core_Form $form
6a488035 29 */
00be9182 30 public static function buildPremium(&$form) {
6a488035
TO
31 //premium section
32 $form->add('hidden', 'hidden_Premium', 1);
be2fb01f 33 $sel1 = $sel2 = [];
6a488035
TO
34
35 $dao = new CRM_Contribute_DAO_Product();
36 $dao->is_active = 1;
37 $dao->find();
be2fb01f 38 $min_amount = [];
ccc29f8e 39 $sel1[0] = ts('-select product-');
6a488035
TO
40 while ($dao->fetch()) {
41 $sel1[$dao->id] = $dao->name . " ( " . $dao->sku . " )";
42 $min_amount[$dao->id] = $dao->min_contribution;
43 $options = explode(',', $dao->options);
44 foreach ($options as $k => $v) {
45 $options[$k] = trim($v);
46 }
47 if ($options[0] != '') {
48 $sel2[$dao->id] = $options;
49 }
50 $form->assign('premiums', TRUE);
51 }
52 $form->_options = $sel2;
53 $form->assign('mincontribution', $min_amount);
353ffa53 54 $sel = &$form->addElement('hierselect', "product_name", ts('Premium'), 'onclick="showMinContrib();"');
6a488035
TO
55 $js = "<script type='text/javascript'>\n";
56 $formName = 'document.forms.' . $form->getName();
57
58 for ($k = 1; $k < 2; $k++) {
59 if (!isset($defaults['product_name'][$k]) || (!$defaults['product_name'][$k])) {
60 $js .= "{$formName}['product_name[$k]'].style.display = 'none';\n";
61 }
62 }
63
be2fb01f 64 $sel->setOptions([$sel1, $sel2]);
6a488035
TO
65 $js .= "</script>\n";
66 $form->assign('initHideBoxes', $js);
67
be2fb01f 68 $form->add('datepicker', 'fulfilled_date', ts('Fulfilled'), [], FALSE, ['time' => FALSE]);
6a488035
TO
69 $form->addElement('text', 'min_amount', ts('Minimum Contribution Amount'));
70 }
71
72 /**
c490a46a 73 * Build the form object for Additional Details.
6a488035 74 *
c490a46a 75 * @param CRM_Core_Form $form
6a488035 76 */
00be9182 77 public static function buildAdditionalDetail(&$form) {
6a488035
TO
78 //Additional information section
79 $form->add('hidden', 'hidden_AdditionalDetail', 1);
80
81 $attributes = CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Contribution');
82
be2fb01f 83 $form->addField('thankyou_date', ['entity' => 'contribution'], FALSE, FALSE);
6a488035
TO
84
85 // add various amounts
353ffa53
TO
86 $nonDeductAmount = &$form->add('text', 'non_deductible_amount', ts('Non-deductible Amount'),
87 $attributes['non_deductible_amount']
6a488035
TO
88 );
89 $form->addRule('non_deductible_amount', ts('Please enter a valid monetary value for Non-deductible Amount.'), 'money');
90
91 if ($form->_online) {
92 $nonDeductAmount->freeze();
93 }
353ffa53
TO
94 $feeAmount = &$form->add('text', 'fee_amount', ts('Fee Amount'),
95 $attributes['fee_amount']
6a488035
TO
96 );
97 $form->addRule('fee_amount', ts('Please enter a valid monetary value for Fee Amount.'), 'money');
98 if ($form->_online) {
99 $feeAmount->freeze();
100 }
0e6e8724 101
353ffa53
TO
102 $element = &$form->add('text', 'invoice_id', ts('Invoice ID'),
103 $attributes['invoice_id']
6a488035
TO
104 );
105 if ($form->_online) {
106 $element->freeze();
107 }
108 else {
109 $form->addRule('invoice_id',
110 ts('This Invoice ID already exists in the database.'),
111 'objectExists',
be2fb01f 112 ['CRM_Contribute_DAO_Contribution', $form->_id, 'invoice_id']
6a488035
TO
113 );
114 }
933c5f44 115 $element = $form->add('text', 'creditnote_id', ts('Credit Note ID'),
116 $attributes['creditnote_id']
117 );
118 if ($form->_online) {
119 $element->freeze();
120 }
121 else {
122 $form->addRule('creditnote_id',
123 ts('This Credit Note ID already exists in the database.'),
124 'objectExists',
be2fb01f 125 ['CRM_Contribute_DAO_Contribution', $form->_id, 'creditnote_id']
933c5f44 126 );
127 }
6a488035
TO
128
129 $form->add('select', 'contribution_page_id',
130 ts('Online Contribution Page'),
be2fb01f 131 [
21dfd5f5 132 '' => ts('- select -'),
be2fb01f 133 ] +
6a488035
TO
134 CRM_Contribute_PseudoConstant::contributionPage()
135 );
136
be2fb01f 137 $form->add('textarea', 'note', ts('Notes'), ["rows" => 4, "cols" => 60]);
0e6e8724 138
6a488035
TO
139 $statusName = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
140 if ($form->_id && $form->_values['contribution_status_id'] == array_search('Cancelled', $statusName)) {
6a488035
TO
141 $feeAmount->freeze();
142 }
0e6e8724 143
6a488035
TO
144 }
145
6a488035 146 /**
dc195289 147 * used by CRM/Pledge/Form/Pledge.php
6a488035 148 *
c490a46a 149 * Build the form object for PaymentReminders Information.
6a488035 150 *
c490a46a 151 * @param CRM_Core_Form $form
6a488035 152 */
00be9182 153 public static function buildPaymentReminders(&$form) {
6a488035
TO
154 //PaymentReminders section
155 $form->add('hidden', 'hidden_PaymentReminders', 1);
be2fb01f 156 $form->add('text', 'initial_reminder_day', ts('Send Initial Reminder'), ['size' => 3]);
3f4b742c 157 $form->addRule('initial_reminder_day', ts('Please enter a valid reminder day.'), 'positiveInteger');
be2fb01f 158 $form->add('text', 'max_reminders', ts('Send up to'), ['size' => 3]);
3f4b742c 159 $form->addRule('max_reminders', ts('Please enter a valid No. of reminders.'), 'positiveInteger');
be2fb01f 160 $form->add('text', 'additional_reminder_day', ts('Send additional reminders'), ['size' => 3]);
3f4b742c 161 $form->addRule('additional_reminder_day', ts('Please enter a valid additional reminder day.'), 'positiveInteger');
6a488035
TO
162 }
163
164 /**
fe482240 165 * Process the Premium Information.
6a488035 166 *
c490a46a 167 * @param array $params
100fef9d
CW
168 * @param int $contributionID
169 * @param int $premiumID
d6412fc1 170 * @param array $options
6a488035 171 */
be2fb01f 172 public static function processPremium($params, $contributionID, $premiumID = NULL, $options = []) {
3e6a1f4a 173 $selectedProductID = $params['product_name'][0];
99af166d 174 $selectedProductOptionID = CRM_Utils_Array::value(1, $params['product_name']);
3e6a1f4a 175
6a488035
TO
176 $dao = new CRM_Contribute_DAO_ContributionProduct();
177 $dao->contribution_id = $contributionID;
3e6a1f4a 178 $dao->product_id = $selectedProductID;
a69cf1d1 179 $dao->fulfilled_date = $params['fulfilled_date'];
353ffa53 180 $isDeleted = FALSE;
3e6a1f4a 181
6a488035 182 //CRM-11106
be2fb01f 183 $premiumParams = [
3e6a1f4a 184 'id' => $selectedProductID,
be2fb01f 185 ];
3e6a1f4a 186
be2fb01f 187 $productDetails = [];
37828d4f 188 CRM_Contribute_BAO_Product::retrieve($premiumParams, $productDetails);
6a488035 189 $dao->financial_type_id = CRM_Utils_Array::value('financial_type_id', $productDetails);
3e6a1f4a
EM
190 if (!empty($options[$selectedProductID])) {
191 $dao->product_option = $options[$selectedProductID][$selectedProductOptionID];
6a488035
TO
192 }
193 if ($premiumID) {
3e6a1f4a
EM
194 $ContributionProduct = new CRM_Contribute_DAO_ContributionProduct();
195 $ContributionProduct->id = $premiumID;
196 $ContributionProduct->find(TRUE);
197 if ($ContributionProduct->product_id == $selectedProductID) {
6a488035 198 $dao->id = $premiumID;
6a488035
TO
199 }
200 else {
3e6a1f4a 201 $ContributionProduct->delete();
6a488035 202 $isDeleted = TRUE;
6a488035
TO
203 }
204 }
3e6a1f4a
EM
205
206 $dao->save();
6a488035
TO
207 //CRM-11106
208 if ($premiumID == NULL || $isDeleted) {
be2fb01f 209 $premiumParams = [
6a488035
TO
210 'cost' => CRM_Utils_Array::value('cost', $productDetails),
211 'currency' => CRM_Utils_Array::value('currency', $productDetails),
212 'financial_type_id' => CRM_Utils_Array::value('financial_type_id', $productDetails),
21dfd5f5 213 'contributionId' => $contributionID,
be2fb01f 214 ];
6a488035 215 if ($isDeleted) {
3e6a1f4a
EM
216 $premiumParams['oldPremium']['product_id'] = $ContributionProduct->product_id;
217 $premiumParams['oldPremium']['contribution_id'] = $ContributionProduct->contribution_id;
6a488035 218 }
3e6a1f4a 219 CRM_Core_BAO_FinancialTrxn::createPremiumTrxn($premiumParams);
6a488035
TO
220 }
221 }
222
223 /**
fe482240 224 * Process the Note.
6a488035 225 *
6a488035 226 *
c490a46a 227 * @param array $params
100fef9d
CW
228 * @param int $contactID
229 * @param int $contributionID
230 * @param int $contributionNoteID
6a488035 231 */
945f423d 232 public static function processNote($params, $contactID, $contributionID, $contributionNoteID = NULL) {
5888a52d 233 if (CRM_Utils_System::isNull($params['note']) && $contributionNoteID) {
234 CRM_Core_BAO_Note::del($contributionNoteID);
235 return;
236 }
6a488035 237 //process note
be2fb01f 238 $noteParams = [
6a488035
TO
239 'entity_table' => 'civicrm_contribution',
240 'note' => $params['note'],
241 'entity_id' => $contributionID,
242 'contact_id' => $contactID,
be2fb01f
CW
243 ];
244 $noteID = [];
6a488035 245 if ($contributionNoteID) {
be2fb01f 246 $noteID = ["id" => $contributionNoteID];
6a488035
TO
247 $noteParams['note'] = $noteParams['note'] ? $noteParams['note'] : "null";
248 }
249 CRM_Core_BAO_Note::add($noteParams, $noteID);
250 }
251
252 /**
fe482240 253 * Process the Common data.
6a488035 254 *
c490a46a 255 * @param array $params
95cdcc0f 256 * @param array $formatted
c490a46a 257 * @param CRM_Core_Form $form
6a488035 258 */
00be9182 259 public static function postProcessCommon(&$params, &$formatted, &$form) {
be2fb01f 260 $fields = [
6a488035
TO
261 'non_deductible_amount',
262 'total_amount',
263 'fee_amount',
6a488035
TO
264 'trxn_id',
265 'invoice_id',
933c5f44 266 'creditnote_id',
6a488035 267 'campaign_id',
6a488035 268 'contribution_page_id',
be2fb01f 269 ];
6a488035
TO
270 foreach ($fields as $f) {
271 $formatted[$f] = CRM_Utils_Array::value($f, $params);
272 }
273
a7488080 274 if (!empty($params['thankyou_date']) && !CRM_Utils_System::isNull($params['thankyou_date'])) {
6a488035
TO
275 $formatted['thankyou_date'] = CRM_Utils_Date::processDate($params['thankyou_date'], $params['thankyou_date_time']);
276 }
277 else {
278 $formatted['thankyou_date'] = 'null';
279 }
280
a7488080 281 if (!empty($params['is_email_receipt'])) {
6a488035
TO
282 $params['receipt_date'] = $formatted['receipt_date'] = date('YmdHis');
283 }
284
6a488035
TO
285 //special case to handle if all checkboxes are unchecked
286 $customFields = CRM_Core_BAO_CustomField::getFields('Contribution',
287 FALSE,
288 FALSE,
289 CRM_Utils_Array::value('financial_type_id',
290 $params
291 )
292 );
293 $formatted['custom'] = CRM_Core_BAO_CustomField::postProcess($params,
6a488035
TO
294 CRM_Utils_Array::value('id', $params, NULL),
295 'Contribution'
296 );
297 }
298
299 /**
100fef9d 300 * Send email receipt.
6a488035 301 *
c490a46a 302 * @param CRM_Core_Form $form
76e7a76c 303 * instance of Contribution form.
014c4014
TO
304 * @param array $params
305 * (reference ) an assoc array of name/value pairs.
da6b46f4 306 * @param bool $ccContribution
76e7a76c 307 * is it credit card contribution.
6a488035 308 *
d77a0a58 309 * @return array
6a488035 310 */
00be9182 311 public static function emailReceipt(&$form, &$params, $ccContribution = FALSE) {
3f4b742c 312 $form->assign('receiptType', 'contribution');
6a488035
TO
313 // Retrieve Financial Type Name from financial_type_id
314 $params['contributionType_name'] = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType',
315 $params['financial_type_id']);
a7488080 316 if (!empty($params['payment_instrument_id'])) {
6a488035
TO
317 $paymentInstrument = CRM_Contribute_PseudoConstant::paymentInstrument();
318 $params['paidBy'] = $paymentInstrument[$params['payment_instrument_id']];
43f77a2c 319 if ($params['paidBy'] != 'Check' && isset($params['check_number'])) {
5e8ae730 320 unset($params['check_number']);
43f77a2c 321 }
6a488035
TO
322 }
323
324 // retrieve individual prefix value for honoree
1421174e 325 if (isset($params['soft_credit'])) {
be2fb01f 326 $softCreditTypes = $softCredits = [];
1421174e 327 foreach ($params['soft_credit'] as $key => $softCredit) {
be2fb01f 328 $softCredits[$key] = [
1421174e 329 'Name' => $softCredit['contact_name'],
21dfd5f5 330 'Amount' => CRM_Utils_Money::format($softCredit['amount'], $softCredit['currency']),
be2fb01f 331 ];
1421174e 332 $softCreditTypes[$key] = $softCredit['soft_credit_type_label'];
333 }
334 $form->assign('softCreditTypes', $softCreditTypes);
335 $form->assign('softCredits', $softCredits);
6a488035
TO
336 }
337
338 // retrieve premium product name and assigned fulfilled
339 // date to template
a7488080 340 if (!empty($params['hidden_Premium'])) {
6a488035
TO
341 if (isset($params['product_name']) &&
342 is_array($params['product_name']) &&
343 !empty($params['product_name'])
344 ) {
345 $productDAO = new CRM_Contribute_DAO_Product();
346 $productDAO->id = $params['product_name'][0];
43bf07d6 347 $productOptionID = $params['product_name'][1];
6a488035
TO
348 $productDAO->find(TRUE);
349 $params['product_name'] = $productDAO->name;
350 $params['product_sku'] = $productDAO->sku;
351
43bf07d6
EM
352 if (empty($params['product_option']) && !empty($form->_options[$productDAO->id])) {
353 $params['product_option'] = $form->_options[$productDAO->id][$productOptionID];
6a488035
TO
354 }
355 }
a7488080 356 if (!empty($params['fulfilled_date'])) {
a69cf1d1 357 $form->assign('fulfilled_date', $params['fulfilled_date']);
6a488035
TO
358 }
359 }
360
3f4b742c 361 $form->assign('ccContribution', $ccContribution);
6a488035 362 if ($ccContribution) {
0b50eca0 363 $form->assignBillingName($params);
364 $form->assign('address', CRM_Utils_Address::getFormattedBillingAddressFieldsFromParameters(
365 $params,
366 $form->_bltID
367 ));
6a488035
TO
368
369 $date = CRM_Utils_Date::format($params['credit_card_exp_date']);
370 $date = CRM_Utils_Date::mysqlToIso($date);
3f4b742c
KJ
371 $form->assign('credit_card_type', CRM_Utils_Array::value('credit_card_type', $params));
372 $form->assign('credit_card_exp_date', $date);
373 $form->assign('credit_card_number',
6a488035
TO
374 CRM_Utils_System::mungeCreditCard($params['credit_card_number'])
375 );
376 }
377 else {
378 //offline contribution
379 // assigned various dates to the templates
380 $form->assign('receipt_date', CRM_Utils_Date::processDate($params['receipt_date']));
381
a7488080 382 if (!empty($params['cancel_date'])) {
6a488035
TO
383 $form->assign('cancel_date', CRM_Utils_Date::processDate($params['cancel_date']));
384 }
a7488080 385 if (!empty($params['thankyou_date'])) {
6a488035
TO
386 $form->assign('thankyou_date', CRM_Utils_Date::processDate($params['thankyou_date']));
387 }
388 if ($form->_action & CRM_Core_Action::UPDATE) {
389 $form->assign('lineItem', empty($form->_lineItems) ? FALSE : $form->_lineItems);
390 }
391 }
392
393 //handle custom data
a7488080 394 if (!empty($params['hidden_custom'])) {
be2fb01f 395 $contribParams = [['contribution_id', '=', $params['contribution_id'], 0, 0]];
6a488035 396 if ($form->_mode == 'test') {
be2fb01f 397 $contribParams[] = ['contribution_test', '=', 1, 0, 0];
6a488035
TO
398 }
399
400 //retrieve custom data
be2fb01f 401 $customGroup = [];
6a488035
TO
402
403 foreach ($form->_groupTree as $groupID => $group) {
be2fb01f 404 $customFields = $customValues = [];
6a488035
TO
405 if ($groupID == 'info') {
406 continue;
407 }
408 foreach ($group['fields'] as $k => $field) {
409 $field['title'] = $field['label'];
410 $customFields["custom_{$k}"] = $field;
411 }
412
413 //build the array of customgroup contain customfields.
414 CRM_Core_BAO_UFGroup::getValues($params['contact_id'], $customFields, $customValues, FALSE, $contribParams);
415 $customGroup[$group['title']] = $customValues;
416 }
417 //assign all custom group and corresponding fields to template.
418 $form->assign('customGroup', $customGroup);
419 }
420
421 $form->assign_by_ref('formValues', $params);
422 list($contributorDisplayName,
423 $contributorEmail
424 ) = CRM_Contact_BAO_Contact_Location::getEmailDetails($params['contact_id']);
3f4b742c
KJ
425 $form->assign('contactID', $params['contact_id']);
426 $form->assign('contributionID', $params['contribution_id']);
6a488035 427
a7488080 428 if (!empty($params['currency'])) {
3f4b742c 429 $form->assign('currency', $params['currency']);
6a488035
TO
430 }
431
a7488080 432 if (!empty($params['receive_date'])) {
3f4b742c 433 $form->assign('receive_date', CRM_Utils_Date::processDate($params['receive_date']));
6a488035
TO
434 }
435
353ffa53
TO
436 $template = CRM_Core_Smarty::singleton();
437 $taxAmt = $template->get_template_vars('dataArray');
438 $eventTaxAmt = $template->get_template_vars('totalTaxAmount');
aaffa79f 439 $prefixValue = Civi::settings()->get('contribution_invoice_settings');
353ffa53
TO
440 $invoicing = CRM_Utils_Array::value('invoicing', $prefixValue);
441 if ((!empty($taxAmt) || isset($eventTaxAmt)) && (isset($invoicing) && isset($prefixValue['is_email_pdf']))) {
442 $isEmailPdf = TRUE;
443 }
444 else {
445 $isEmailPdf = FALSE;
446 }
d75f2f47 447
c6327d7d 448 list($sendReceipt, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate(
be2fb01f 449 [
6a488035
TO
450 'groupName' => 'msg_tpl_workflow_contribution',
451 'valueName' => 'contribution_offline_receipt',
452 'contactId' => $params['contact_id'],
453 'contributionId' => $params['contribution_id'],
454 'from' => $params['from_email_address'],
455 'toName' => $contributorDisplayName,
456 'toEmail' => $contributorEmail,
457 'isTest' => $form->_mode == 'test',
92fcb95f 458 'PDFFilename' => ts('receipt') . '.pdf',
9161952c 459 'isEmailPdf' => $isEmailPdf,
be2fb01f 460 ]
6a488035
TO
461 );
462
463 return $sendReceipt;
464 }
465
466}