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