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