3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Contribute_Form_AdditionalInfo
{
20 * Build the form object for Premium Information.
22 * Called from the CRM_Contribute_Form_Contribute function and seemingly nowhere else.
24 * Probably this should be on the form that uses it since it is not used on multiple forms.
26 * Putting it on this class doesn't seem to reduce complexity.
28 * @param CRM_Core_Form $form
30 public static function buildPremium(&$form) {
32 $form->add('hidden', 'hidden_Premium', 1);
35 $dao = new CRM_Contribute_DAO_Product();
39 $sel1[0] = ts('-select product-');
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);
47 if ($options[0] != '') {
48 $sel2[$dao->id
] = $options;
50 $form->assign('premiums', TRUE);
52 $form->_options
= $sel2;
53 $form->assign('mincontribution', $min_amount);
54 $sel = &$form->addElement('hierselect', "product_name", ts('Premium'), 'onclick="showMinContrib();"');
55 $js = "<script type='text/javascript'>\n";
56 $formName = 'document.forms.' . $form->getName();
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";
64 $sel->setOptions([$sel1, $sel2]);
66 $form->assign('initHideBoxes', $js);
68 $form->add('datepicker', 'fulfilled_date', ts('Fulfilled'), [], FALSE, ['time' => FALSE]);
69 $form->addElement('text', 'min_amount', ts('Minimum Contribution Amount'));
73 * Build the form object for Additional Details.
75 * @param CRM_Core_Form $form
77 public static function buildAdditionalDetail(&$form) {
78 //Additional information section
79 $form->add('hidden', 'hidden_AdditionalDetail', 1);
81 $attributes = CRM_Core_DAO
::getAttribute('CRM_Contribute_DAO_Contribution');
83 $form->addField('thankyou_date', ['entity' => 'contribution'], FALSE, FALSE);
85 // add various amounts
86 $nonDeductAmount = &$form->add('text', 'non_deductible_amount', ts('Non-deductible Amount'),
87 $attributes['non_deductible_amount']
89 $form->addRule('non_deductible_amount', ts('Please enter a valid monetary value for Non-deductible Amount.'), 'money');
92 $nonDeductAmount->freeze();
94 $feeAmount = &$form->add('text', 'fee_amount', ts('Fee Amount'),
95 $attributes['fee_amount']
97 $form->addRule('fee_amount', ts('Please enter a valid monetary value for Fee Amount.'), 'money');
102 $element = &$form->add('text', 'invoice_id', ts('Invoice ID'),
103 $attributes['invoice_id']
105 if ($form->_online
) {
109 $form->addRule('invoice_id',
110 ts('This Invoice ID already exists in the database.'),
112 ['CRM_Contribute_DAO_Contribution', $form->_id
, 'invoice_id']
115 $element = $form->add('text', 'creditnote_id', ts('Credit Note ID'),
116 $attributes['creditnote_id']
118 if ($form->_online
) {
122 $form->addRule('creditnote_id',
123 ts('This Credit Note ID already exists in the database.'),
125 ['CRM_Contribute_DAO_Contribution', $form->_id
, 'creditnote_id']
129 $form->add('select', 'contribution_page_id',
130 ts('Online Contribution Page'),
132 '' => ts('- select -'),
134 CRM_Contribute_PseudoConstant
::contributionPage()
137 $form->add('textarea', 'note', ts('Notes'), ["rows" => 4, "cols" => 60]);
139 $statusName = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
140 if ($form->_id
&& $form->_values
['contribution_status_id'] == array_search('Cancelled', $statusName)) {
141 $feeAmount->freeze();
147 * used by CRM/Pledge/Form/Pledge.php
149 * Build the form object for PaymentReminders Information.
151 * @param CRM_Core_Form $form
153 public static function buildPaymentReminders(&$form) {
154 //PaymentReminders section
155 $form->add('hidden', 'hidden_PaymentReminders', 1);
156 $form->add('text', 'initial_reminder_day', ts('Send Initial Reminder'), ['size' => 3]);
157 $form->addRule('initial_reminder_day', ts('Please enter a valid reminder day.'), 'positiveInteger');
158 $form->add('text', 'max_reminders', ts('Send up to'), ['size' => 3]);
159 $form->addRule('max_reminders', ts('Please enter a valid No. of reminders.'), 'positiveInteger');
160 $form->add('text', 'additional_reminder_day', ts('Send additional reminders'), ['size' => 3]);
161 $form->addRule('additional_reminder_day', ts('Please enter a valid additional reminder day.'), 'positiveInteger');
165 * Process the Premium Information.
167 * @param array $params
168 * @param int $contributionID
169 * @param int $premiumID
170 * @param array $options
172 public static function processPremium($params, $contributionID, $premiumID = NULL, $options = []) {
173 $selectedProductID = $params['product_name'][0];
174 $selectedProductOptionID = $params['product_name'][1] ??
NULL;
176 $dao = new CRM_Contribute_DAO_ContributionProduct();
177 $dao->contribution_id
= $contributionID;
178 $dao->product_id
= $selectedProductID;
179 $dao->fulfilled_date
= $params['fulfilled_date'];
184 'id' => $selectedProductID,
187 $productDetails = [];
188 CRM_Contribute_BAO_Product
::retrieve($premiumParams, $productDetails);
189 $dao->financial_type_id
= $productDetails['financial_type_id'] ??
NULL;
190 if (!empty($options[$selectedProductID])) {
191 $dao->product_option
= $options[$selectedProductID][$selectedProductOptionID];
194 $ContributionProduct = new CRM_Contribute_DAO_ContributionProduct();
195 $ContributionProduct->id
= $premiumID;
196 $ContributionProduct->find(TRUE);
197 if ($ContributionProduct->product_id
== $selectedProductID) {
198 $dao->id
= $premiumID;
201 $ContributionProduct->delete();
208 if ($premiumID == NULL ||
$isDeleted) {
210 'cost' => $productDetails['cost'] ??
NULL,
211 'currency' => $productDetails['currency'] ??
NULL,
212 'financial_type_id' => $productDetails['financial_type_id'] ??
NULL,
213 'contributionId' => $contributionID,
216 $premiumParams['oldPremium']['product_id'] = $ContributionProduct->product_id
;
217 $premiumParams['oldPremium']['contribution_id'] = $ContributionProduct->contribution_id
;
219 CRM_Core_BAO_FinancialTrxn
::createPremiumTrxn($premiumParams);
227 * @param array $params
228 * @param int $contactID
229 * @param int $contributionID
230 * @param int $contributionNoteID
232 public static function processNote($params, $contactID, $contributionID, $contributionNoteID = NULL) {
233 if (CRM_Utils_System
::isNull($params['note']) && $contributionNoteID) {
234 CRM_Core_BAO_Note
::del($contributionNoteID);
239 'entity_table' => 'civicrm_contribution',
240 'note' => $params['note'],
241 'entity_id' => $contributionID,
242 'contact_id' => $contactID,
245 if ($contributionNoteID) {
246 $noteID = ["id" => $contributionNoteID];
247 $noteParams['note'] = $noteParams['note'] ?
$noteParams['note'] : "null";
249 CRM_Core_BAO_Note
::add($noteParams, $noteID);
253 * Process the Common data.
255 * @param array $params
256 * @param array $formatted
257 * @param CRM_Core_Form $form
259 public static function postProcessCommon(&$params, &$formatted, &$form) {
261 'non_deductible_amount',
268 'contribution_page_id',
270 foreach ($fields as $f) {
271 $formatted[$f] = $params[$f] ??
NULL;
274 if (!empty($params['thankyou_date']) && !CRM_Utils_System
::isNull($params['thankyou_date'])) {
275 $formatted['thankyou_date'] = CRM_Utils_Date
::processDate($params['thankyou_date'], $params['thankyou_date_time']);
278 $formatted['thankyou_date'] = 'null';
281 if (!empty($params['is_email_receipt'])) {
282 $params['receipt_date'] = $formatted['receipt_date'] = date('YmdHis');
285 $formatted['custom'] = CRM_Core_BAO_CustomField
::postProcess($params,
286 $params['id'] ??
NULL,
292 * Send email receipt.
294 * @param \CRM_Core_Form $form
295 * instance of Contribution form.
296 * @param array $params
297 * (reference ) an assoc array of name/value pairs.
298 * @param bool $ccContribution
299 * is it credit card contribution.
302 * @throws \CRM_Core_Exception
304 public static function emailReceipt(&$form, &$params, $ccContribution = FALSE) {
305 $form->assign('receiptType', 'contribution');
306 // Retrieve Financial Type Name from financial_type_id
307 $params['contributionType_name'] = CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_FinancialType',
308 $params['financial_type_id']);
309 if (!empty($params['payment_instrument_id'])) {
310 $paymentInstrument = CRM_Contribute_PseudoConstant
::paymentInstrument();
311 $params['paidBy'] = $paymentInstrument[$params['payment_instrument_id']];
312 if ($params['paidBy'] != 'Check' && isset($params['check_number'])) {
313 unset($params['check_number']);
317 // retrieve individual prefix value for honoree
318 if (isset($params['soft_credit'])) {
319 $softCreditTypes = $softCredits = [];
320 foreach ($params['soft_credit'] as $key => $softCredit) {
321 $softCredits[$key] = [
322 'Name' => $softCredit['contact_name'],
323 'Amount' => CRM_Utils_Money
::format($softCredit['amount'], $softCredit['currency']),
325 $softCreditTypes[$key] = $softCredit['soft_credit_type_label'];
327 $form->assign('softCreditTypes', $softCreditTypes);
328 $form->assign('softCredits', $softCredits);
331 // retrieve premium product name and assigned fulfilled
333 if (!empty($params['hidden_Premium'])) {
334 if (isset($params['product_name']) &&
335 is_array($params['product_name']) &&
336 !empty($params['product_name'])
338 $productDAO = new CRM_Contribute_DAO_Product();
339 $productDAO->id
= $params['product_name'][0];
340 $productOptionID = $params['product_name'][1];
341 $productDAO->find(TRUE);
342 $params['product_name'] = $productDAO->name
;
343 $params['product_sku'] = $productDAO->sku
;
345 if (empty($params['product_option']) && !empty($form->_options
[$productDAO->id
])) {
346 $params['product_option'] = $form->_options
[$productDAO->id
][$productOptionID];
349 if (!empty($params['fulfilled_date'])) {
350 $form->assign('fulfilled_date', $params['fulfilled_date']);
354 $form->assign('ccContribution', $ccContribution);
355 if ($ccContribution) {
356 $form->assignBillingName($params);
357 $form->assign('address', CRM_Utils_Address
::getFormattedBillingAddressFieldsFromParameters(
362 $valuesForForm = CRM_Contribute_Form_AbstractEditPayment
::formatCreditCardDetails($params);
363 $form->assignVariables($valuesForForm, ['credit_card_exp_date', 'credit_card_type', 'credit_card_number']);
366 //offline contribution
367 // assigned various dates to the templates
368 $form->assign('receipt_date', CRM_Utils_Date
::processDate($params['receipt_date']));
370 if (!empty($params['cancel_date'])) {
371 $form->assign('cancel_date', CRM_Utils_Date
::processDate($params['cancel_date']));
373 if (!empty($params['thankyou_date'])) {
374 $form->assign('thankyou_date', CRM_Utils_Date
::processDate($params['thankyou_date']));
376 if ($form->_action
& CRM_Core_Action
::UPDATE
) {
377 $form->assign('lineItem', empty($form->_lineItems
) ?
FALSE : $form->_lineItems
);
382 if (!empty($params['hidden_custom'])) {
383 $contribParams = [['contribution_id', '=', $params['contribution_id'], 0, 0]];
384 if ($form->_mode
== 'test') {
385 $contribParams[] = ['contribution_test', '=', 1, 0, 0];
388 //retrieve custom data
391 foreach ($form->_groupTree
as $groupID => $group) {
392 $customFields = $customValues = [];
393 if ($groupID == 'info') {
396 foreach ($group['fields'] as $k => $field) {
397 $field['title'] = $field['label'];
398 $customFields["custom_{$k}"] = $field;
401 //build the array of customgroup contain customfields.
402 CRM_Core_BAO_UFGroup
::getValues($params['contact_id'], $customFields, $customValues, FALSE, $contribParams);
403 $customGroup[$group['title']] = $customValues;
405 //assign all custom group and corresponding fields to template.
406 $form->assign('customGroup', $customGroup);
409 $form->assign_by_ref('formValues', $params);
410 list($contributorDisplayName,
412 ) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($params['contact_id']);
413 $form->assign('contactID', $params['contact_id']);
414 $form->assign('contributionID', $params['contribution_id']);
416 if (!empty($params['currency'])) {
417 $form->assign('currency', $params['currency']);
420 if (!empty($params['receive_date'])) {
421 $form->assign('receive_date', CRM_Utils_Date
::processDate($params['receive_date']));
424 $template = CRM_Core_Smarty
::singleton();
425 $taxAmt = $template->get_template_vars('dataArray');
426 $eventTaxAmt = $template->get_template_vars('totalTaxAmount');
427 $prefixValue = Civi
::settings()->get('contribution_invoice_settings');
428 $invoicing = $prefixValue['invoicing'] ??
NULL;
429 if ((!empty($taxAmt) ||
isset($eventTaxAmt)) && (isset($invoicing) && isset($prefixValue['is_email_pdf']))) {
436 list($sendReceipt, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate
::sendTemplate(
438 'groupName' => 'msg_tpl_workflow_contribution',
439 'valueName' => 'contribution_offline_receipt',
440 'contactId' => $params['contact_id'],
441 'contributionId' => $params['contribution_id'],
442 'from' => $params['from_email_address'],
443 'toName' => $contributorDisplayName,
444 'toEmail' => $contributorEmail,
445 'isTest' => $form->_mode
== 'test',
446 'PDFFilename' => ts('receipt') . '.pdf',
447 'isEmailPdf' => $isEmailPdf,