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