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