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