Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-04-04-00-08-28
[civicrm-core.git] / CRM / Contribute / Form / AdditionalInfo.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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 * This function is used by CRM/Pledge/Form/Pledge.php
159 *
160 * Function to build the form for PaymentReminders Information.
161 *
162 * @access public
163 *
164 * @return void
165 *
166 */
167 static function buildPaymentReminders(&$form) {
168 //PaymentReminders section
169 $form->add('hidden', 'hidden_PaymentReminders', 1);
170 $form->add('text', 'initial_reminder_day', ts('Send Initial Reminder'), array('size' => 3));
171 $form->addRule('initial_reminder_day', ts('Please enter a valid reminder day.'), 'positiveInteger');
172 $form->add('text', 'max_reminders', ts('Send up to'), array('size' => 3));
173 $form->addRule('max_reminders', ts('Please enter a valid No. of reminders.'), 'positiveInteger');
174 $form->add('text', 'additional_reminder_day', ts('Send additional reminders'), array('size' => 3));
175 $form->addRule('additional_reminder_day', ts('Please enter a valid additional reminder day.'), 'positiveInteger');
176 }
177
178 /**
179 * Function to process the Premium Information
180 *
181 * @access public
182 *
183 * @return void
184 */
185 static function processPremium(&$params, $contributionID, $premiumID = NULL, &$options = NULL) {
186 $dao = new CRM_Contribute_DAO_ContributionProduct();
187 $dao->contribution_id = $contributionID;
188 $dao->product_id = $params['product_name'][0];
189 $dao->fulfilled_date = CRM_Utils_Date::processDate($params['fulfilled_date'], NULL, TRUE);
190 $isDeleted = False;
191 //CRM-11106
192 $premiumParams = array(
193 'id' => $params['product_name'][0],
194 );
195 $productDetails = array();
196 CRM_Contribute_BAO_ManagePremiums::retrieve($premiumParams, $productDetails);
197 $dao->financial_type_id = CRM_Utils_Array::value('financial_type_id', $productDetails);
198 if (!empty($options[$params['product_name'][0]])) {
199 $dao->product_option = $options[$params['product_name'][0]][$params['product_name'][1]];
200 }
201 if ($premiumID) {
202 $premoumDAO = new CRM_Contribute_DAO_ContributionProduct();
203 $premoumDAO->id = $premiumID;
204 $premoumDAO->find(TRUE);
205 if ($premoumDAO->product_id == $params['product_name'][0]) {
206 $dao->id = $premiumID;
207 $premium = $dao->save();
208 }
209 else {
210 $premoumDAO->delete();
211 $isDeleted = TRUE;
212 $premium = $dao->save();
213 }
214 }
215 else {
216 $premium = $dao->save();
217 }
218 //CRM-11106
219 if ($premiumID == NULL || $isDeleted) {
220 $params = array(
221 'cost' => CRM_Utils_Array::value('cost', $productDetails),
222 'currency' => CRM_Utils_Array::value('currency', $productDetails),
223 'financial_type_id' => CRM_Utils_Array::value('financial_type_id', $productDetails),
224 'contributionId' => $contributionID
225 );
226 if ($isDeleted) {
227 $params['oldPremium']['product_id'] = $premoumDAO->product_id;
228 $params['oldPremium']['contribution_id'] = $premoumDAO->contribution_id;
229 }
230 CRM_Core_BAO_FinancialTrxn::createPremiumTrxn($params);
231 }
232 }
233
234 /**
235 * Function to process the Note
236 *
237 * @access public
238 *
239 * @return void
240 */
241 static function processNote(&$params, $contactID, $contributionID, $contributionNoteID = NULL) {
242 //process note
243 $noteParams = array(
244 'entity_table' => 'civicrm_contribution',
245 'note' => $params['note'],
246 'entity_id' => $contributionID,
247 'contact_id' => $contactID,
248 );
249 $noteID = array();
250 if ($contributionNoteID) {
251 $noteID = array("id" => $contributionNoteID);
252 $noteParams['note'] = $noteParams['note'] ? $noteParams['note'] : "null";
253 }
254 CRM_Core_BAO_Note::add($noteParams, $noteID);
255 }
256
257 /**
258 * Function to process the Common data
259 *
260 * @access public
261 *
262 * @return void
263 */
264 static function postProcessCommon(&$params, &$formatted, &$form) {
265 $fields = array(
266 'non_deductible_amount',
267 'total_amount',
268 'fee_amount',
269 'net_amount',
270 'trxn_id',
271 'invoice_id',
272 'campaign_id',
273 'contribution_page_id',
274 );
275 foreach ($fields as $f) {
276 $formatted[$f] = CRM_Utils_Array::value($f, $params);
277 }
278
279 if (!empty($params['thankyou_date']) && !CRM_Utils_System::isNull($params['thankyou_date'])) {
280 $formatted['thankyou_date'] = CRM_Utils_Date::processDate($params['thankyou_date'], $params['thankyou_date_time']);
281 }
282 else {
283 $formatted['thankyou_date'] = 'null';
284 }
285
286 if (!empty($params['is_email_receipt'])) {
287 $params['receipt_date'] = $formatted['receipt_date'] = date('YmdHis');
288 }
289
290 //special case to handle if all checkboxes are unchecked
291 $customFields = CRM_Core_BAO_CustomField::getFields('Contribution',
292 FALSE,
293 FALSE,
294 CRM_Utils_Array::value('financial_type_id',
295 $params
296 )
297 );
298 $formatted['custom'] = CRM_Core_BAO_CustomField::postProcess($params,
299 $customFields,
300 CRM_Utils_Array::value('id', $params, NULL),
301 'Contribution'
302 );
303 }
304
305 /**
306 * Function to send email receipt.
307 *
308 * @form object of Contribution form.
309 *
310 * @param array $params (reference ) an assoc array of name/value pairs.
311 * @$ccContribution boolen, is it credit card contribution.
312 * @access public.
313 *
314 * @return void.
315 */
316 static function emailReceipt(&$form, &$params, $ccContribution = FALSE) {
317 $form->assign('receiptType', 'contribution');
318 // Retrieve Financial Type Name from financial_type_id
319 $params['contributionType_name'] = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType',
320 $params['financial_type_id']);
321 if (!empty($params['payment_instrument_id'])) {
322 $paymentInstrument = CRM_Contribute_PseudoConstant::paymentInstrument();
323 $params['paidBy'] = $paymentInstrument[$params['payment_instrument_id']];
324 }
325
326 // retrieve individual prefix value for honoree
327 if (isset($params['soft_credit'])) {
328 $softCreditTypes = $softCredits = array();
329 foreach ($params['soft_credit'] as $key => $softCredit) {
330 $softCredits[$key] = array(
331 'Name' => $softCredit['contact_name'],
332 'Amount' => CRM_Utils_Money::format($softCredit['amount'], $softCredit['currency'])
333 );
334 $softCreditTypes[$key] = $softCredit['soft_credit_type_label'];
335 }
336 $form->assign('softCreditTypes', $softCreditTypes);
337 $form->assign('softCredits', $softCredits);
338 }
339
340 // retrieve premium product name and assigned fulfilled
341 // date to template
342 if (!empty($params['hidden_Premium'])) {
343 if (isset($params['product_name']) &&
344 is_array($params['product_name']) &&
345 !empty($params['product_name'])
346 ) {
347 $productDAO = new CRM_Contribute_DAO_Product();
348 $productDAO->id = $params['product_name'][0];
349 $productDAO->find(TRUE);
350 $params['product_name'] = $productDAO->name;
351 $params['product_sku'] = $productDAO->sku;
352
353 if (empty($params['product_option']) && !empty($form->_options
354 [$params['product_name'][0]])) {
355 $params['product_option'] = $form->_options[$params['product_name'][0]][$params['product_name'][1]];
356 }
357 }
358
359 if (!empty($params['fulfilled_date'])) {
360 $form->assign('fulfilled_date', CRM_Utils_Date::processDate($params['fulfilled_date']));
361 }
362 }
363
364 $form->assign('ccContribution', $ccContribution);
365 if ($ccContribution) {
366 //build the name.
367 $name = CRM_Utils_Array::value('billing_first_name', $params);
368 if (!empty($params['billing_middle_name'])) {
369 $name .= " {$params['billing_middle_name']}";
370 }
371 $name .= ' ' . CRM_Utils_Array::value('billing_last_name', $params);
372 $name = trim($name);
373 $form->assign('billingName', $name);
374
375 //assign the address formatted up for display
376 $addressParts = array(
377 "street_address" => "billing_street_address-{$form->_bltID}",
378 "city" => "billing_city-{$form->_bltID}",
379 "postal_code" => "billing_postal_code-{$form->_bltID}",
380 "state_province" => "state_province-{$form->_bltID}",
381 "country" => "country-{$form->_bltID}",
382 );
383
384 $addressFields = array();
385 foreach ($addressParts as $name => $field) {
386 $addressFields[$name] = CRM_Utils_Array::value($field, $params);
387 }
388 $form->assign('address', CRM_Utils_Address::format($addressFields));
389
390 $date = CRM_Utils_Date::format($params['credit_card_exp_date']);
391 $date = CRM_Utils_Date::mysqlToIso($date);
392 $form->assign('credit_card_type', CRM_Utils_Array::value('credit_card_type', $params));
393 $form->assign('credit_card_exp_date', $date);
394 $form->assign('credit_card_number',
395 CRM_Utils_System::mungeCreditCard($params['credit_card_number'])
396 );
397 }
398 else {
399 //offline contribution
400 // assigned various dates to the templates
401 $form->assign('receipt_date', CRM_Utils_Date::processDate($params['receipt_date']));
402
403 if (!empty($params['cancel_date'])) {
404 $form->assign('cancel_date', CRM_Utils_Date::processDate($params['cancel_date']));
405 }
406 if (!empty($params['thankyou_date'])) {
407 $form->assign('thankyou_date', CRM_Utils_Date::processDate($params['thankyou_date']));
408 }
409 if ($form->_action & CRM_Core_Action::UPDATE) {
410 $form->assign('lineItem', empty($form->_lineItems) ? FALSE : $form->_lineItems);
411 }
412 }
413
414 //handle custom data
415 if (!empty($params['hidden_custom'])) {
416 $contribParams = array(array('contribution_id', '=', $params['contribution_id'], 0, 0));
417 if ($form->_mode == 'test') {
418 $contribParams[] = array('contribution_test', '=', 1, 0, 0);
419 }
420
421 //retrieve custom data
422 $customGroup = array();
423
424 foreach ($form->_groupTree as $groupID => $group) {
425 $customFields = $customValues = array();
426 if ($groupID == 'info') {
427 continue;
428 }
429 foreach ($group['fields'] as $k => $field) {
430 $field['title'] = $field['label'];
431 $customFields["custom_{$k}"] = $field;
432 }
433
434 //build the array of customgroup contain customfields.
435 CRM_Core_BAO_UFGroup::getValues($params['contact_id'], $customFields, $customValues, FALSE, $contribParams);
436 $customGroup[$group['title']] = $customValues;
437 }
438 //assign all custom group and corresponding fields to template.
439 $form->assign('customGroup', $customGroup);
440 }
441
442 $form->assign_by_ref('formValues', $params);
443 list($contributorDisplayName,
444 $contributorEmail
445 ) = CRM_Contact_BAO_Contact_Location::getEmailDetails($params['contact_id']);
446 $form->assign('contactID', $params['contact_id']);
447 $form->assign('contributionID', $params['contribution_id']);
448
449 if (!empty($params['currency'])) {
450 $form->assign('currency', $params['currency']);
451 }
452
453 if (!empty($params['receive_date'])) {
454 $form->assign('receive_date', CRM_Utils_Date::processDate($params['receive_date']));
455 }
456
457 list($sendReceipt, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate(
458 array(
459 'groupName' => 'msg_tpl_workflow_contribution',
460 'valueName' => 'contribution_offline_receipt',
461 'contactId' => $params['contact_id'],
462 'contributionId' => $params['contribution_id'],
463 'from' => $params['from_email_address'],
464 'toName' => $contributorDisplayName,
465 'toEmail' => $contributorEmail,
466 'isTest' => $form->_mode == 'test',
467 'PDFFilename' => ts('receipt').'.pdf',
468 )
469 );
470
471 return $sendReceipt;
472 }
473
474 }
475
476