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