Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
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 | } | |
0e6e8724 | 117 | |
6a488035 TO |
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)); | |
0e6e8724 | 148 | |
6a488035 TO |
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 | } | |
0e6e8724 | 154 | |
6a488035 TO |
155 | } |
156 | ||
6a488035 TO |
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 | */ | |
3f4b742c | 167 | static function buildPaymentReminders(&$form) { |
6a488035 TO |
168 | //PaymentReminders section |
169 | $form->add('hidden', 'hidden_PaymentReminders', 1); | |
170 | $form->add('text', 'initial_reminder_day', ts('Send Initial Reminder'), array('size' => 3)); | |
3f4b742c | 171 | $form->addRule('initial_reminder_day', ts('Please enter a valid reminder day.'), 'positiveInteger'); |
6a488035 | 172 | $form->add('text', 'max_reminders', ts('Send up to'), array('size' => 3)); |
3f4b742c | 173 | $form->addRule('max_reminders', ts('Please enter a valid No. of reminders.'), 'positiveInteger'); |
6a488035 | 174 | $form->add('text', 'additional_reminder_day', ts('Send additional reminders'), array('size' => 3)); |
3f4b742c | 175 | $form->addRule('additional_reminder_day', ts('Please enter a valid additional reminder day.'), 'positiveInteger'); |
6a488035 TO |
176 | } |
177 | ||
178 | /** | |
179 | * Function to process the Premium Information | |
180 | * | |
181 | * @access public | |
182 | * | |
355ba699 | 183 | * @return void |
6a488035 TO |
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); | |
a7488080 | 198 | if (!empty($options[$params['product_name'][0]])) { |
6a488035 TO |
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 | * | |
355ba699 | 239 | * @return void |
6a488035 TO |
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 | * | |
355ba699 | 262 | * @return void |
6a488035 TO |
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', | |
6a488035 TO |
273 | 'contribution_page_id', |
274 | ); | |
275 | foreach ($fields as $f) { | |
276 | $formatted[$f] = CRM_Utils_Array::value($f, $params); | |
277 | } | |
278 | ||
a7488080 | 279 | if (!empty($params['thankyou_date']) && !CRM_Utils_System::isNull($params['thankyou_date'])) { |
6a488035 TO |
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 | ||
a7488080 | 286 | if (!empty($params['is_email_receipt'])) { |
6a488035 TO |
287 | $params['receipt_date'] = $formatted['receipt_date'] = date('YmdHis'); |
288 | } | |
289 | ||
6a488035 TO |
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 | * | |
355ba699 | 314 | * @return void. |
6a488035 | 315 | */ |
3f4b742c KJ |
316 | static function emailReceipt(&$form, &$params, $ccContribution = FALSE) { |
317 | $form->assign('receiptType', 'contribution'); | |
6a488035 TO |
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']); | |
a7488080 | 321 | if (!empty($params['payment_instrument_id'])) { |
6a488035 TO |
322 | $paymentInstrument = CRM_Contribute_PseudoConstant::paymentInstrument(); |
323 | $params['paidBy'] = $paymentInstrument[$params['payment_instrument_id']]; | |
324 | } | |
325 | ||
326 | // retrieve individual prefix value for honoree | |
a7488080 | 327 | if (!empty($params['hidden_Honoree'])) { |
e6c4755b | 328 | $individualPrefix = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id'); |
cbf48754 | 329 | $honor = CRM_Core_PseudoConstant::get('CRM_Contribute_DAO_Contribution', 'honor_type_id'); |
6a488035 TO |
330 | $params['honor_prefix'] = CRM_Utils_Array::value(CRM_Utils_Array::value('honor_prefix_id', |
331 | $params | |
332 | ), | |
333 | $individualPrefix | |
334 | ); | |
335 | $params["honor_type"] = CRM_Utils_Array::value(CRM_Utils_Array::value('honor_type_id', | |
336 | $params | |
337 | ), | |
338 | $honor | |
339 | ); | |
340 | } | |
341 | ||
342 | // retrieve premium product name and assigned fulfilled | |
343 | // date to template | |
a7488080 | 344 | if (!empty($params['hidden_Premium'])) { |
6a488035 TO |
345 | if (isset($params['product_name']) && |
346 | is_array($params['product_name']) && | |
347 | !empty($params['product_name']) | |
348 | ) { | |
349 | $productDAO = new CRM_Contribute_DAO_Product(); | |
350 | $productDAO->id = $params['product_name'][0]; | |
351 | $productDAO->find(TRUE); | |
352 | $params['product_name'] = $productDAO->name; | |
353 | $params['product_sku'] = $productDAO->sku; | |
354 | ||
8cc574cf CW |
355 | if (empty($params['product_option']) && !empty($form->_options |
356 | [$params['product_name'][0]])) { | |
6a488035 TO |
357 | $params['product_option'] = $form->_options[$params['product_name'][0]][$params['product_name'][1]]; |
358 | } | |
359 | } | |
360 | ||
a7488080 | 361 | if (!empty($params['fulfilled_date'])) { |
3f4b742c | 362 | $form->assign('fulfilled_date', CRM_Utils_Date::processDate($params['fulfilled_date'])); |
6a488035 TO |
363 | } |
364 | } | |
365 | ||
3f4b742c | 366 | $form->assign('ccContribution', $ccContribution); |
6a488035 TO |
367 | if ($ccContribution) { |
368 | //build the name. | |
369 | $name = CRM_Utils_Array::value('billing_first_name', $params); | |
a7488080 | 370 | if (!empty($params['billing_middle_name'])) { |
6a488035 TO |
371 | $name .= " {$params['billing_middle_name']}"; |
372 | } | |
373 | $name .= ' ' . CRM_Utils_Array::value('billing_last_name', $params); | |
374 | $name = trim($name); | |
3f4b742c | 375 | $form->assign('billingName', $name); |
6a488035 TO |
376 | |
377 | //assign the address formatted up for display | |
378 | $addressParts = array( | |
379 | "street_address" => "billing_street_address-{$form->_bltID}", | |
380 | "city" => "billing_city-{$form->_bltID}", | |
381 | "postal_code" => "billing_postal_code-{$form->_bltID}", | |
382 | "state_province" => "state_province-{$form->_bltID}", | |
383 | "country" => "country-{$form->_bltID}", | |
384 | ); | |
385 | ||
386 | $addressFields = array(); | |
387 | foreach ($addressParts as $name => $field) { | |
388 | $addressFields[$name] = CRM_Utils_Array::value($field, $params); | |
389 | } | |
3f4b742c | 390 | $form->assign('address', CRM_Utils_Address::format($addressFields)); |
6a488035 TO |
391 | |
392 | $date = CRM_Utils_Date::format($params['credit_card_exp_date']); | |
393 | $date = CRM_Utils_Date::mysqlToIso($date); | |
3f4b742c KJ |
394 | $form->assign('credit_card_type', CRM_Utils_Array::value('credit_card_type', $params)); |
395 | $form->assign('credit_card_exp_date', $date); | |
396 | $form->assign('credit_card_number', | |
6a488035 TO |
397 | CRM_Utils_System::mungeCreditCard($params['credit_card_number']) |
398 | ); | |
399 | } | |
400 | else { | |
401 | //offline contribution | |
402 | // assigned various dates to the templates | |
403 | $form->assign('receipt_date', CRM_Utils_Date::processDate($params['receipt_date'])); | |
404 | ||
a7488080 | 405 | if (!empty($params['cancel_date'])) { |
6a488035 TO |
406 | $form->assign('cancel_date', CRM_Utils_Date::processDate($params['cancel_date'])); |
407 | } | |
a7488080 | 408 | if (!empty($params['thankyou_date'])) { |
6a488035 TO |
409 | $form->assign('thankyou_date', CRM_Utils_Date::processDate($params['thankyou_date'])); |
410 | } | |
411 | if ($form->_action & CRM_Core_Action::UPDATE) { | |
412 | $form->assign('lineItem', empty($form->_lineItems) ? FALSE : $form->_lineItems); | |
413 | } | |
414 | } | |
415 | ||
416 | //handle custom data | |
a7488080 | 417 | if (!empty($params['hidden_custom'])) { |
6a488035 TO |
418 | $contribParams = array(array('contribution_id', '=', $params['contribution_id'], 0, 0)); |
419 | if ($form->_mode == 'test') { | |
420 | $contribParams[] = array('contribution_test', '=', 1, 0, 0); | |
421 | } | |
422 | ||
423 | //retrieve custom data | |
424 | $customGroup = array(); | |
425 | ||
426 | foreach ($form->_groupTree as $groupID => $group) { | |
427 | $customFields = $customValues = array(); | |
428 | if ($groupID == 'info') { | |
429 | continue; | |
430 | } | |
431 | foreach ($group['fields'] as $k => $field) { | |
432 | $field['title'] = $field['label']; | |
433 | $customFields["custom_{$k}"] = $field; | |
434 | } | |
435 | ||
436 | //build the array of customgroup contain customfields. | |
437 | CRM_Core_BAO_UFGroup::getValues($params['contact_id'], $customFields, $customValues, FALSE, $contribParams); | |
438 | $customGroup[$group['title']] = $customValues; | |
439 | } | |
440 | //assign all custom group and corresponding fields to template. | |
441 | $form->assign('customGroup', $customGroup); | |
442 | } | |
443 | ||
444 | $form->assign_by_ref('formValues', $params); | |
445 | list($contributorDisplayName, | |
446 | $contributorEmail | |
447 | ) = CRM_Contact_BAO_Contact_Location::getEmailDetails($params['contact_id']); | |
3f4b742c KJ |
448 | $form->assign('contactID', $params['contact_id']); |
449 | $form->assign('contributionID', $params['contribution_id']); | |
6a488035 | 450 | |
a7488080 | 451 | if (!empty($params['currency'])) { |
3f4b742c | 452 | $form->assign('currency', $params['currency']); |
6a488035 TO |
453 | } |
454 | ||
a7488080 | 455 | if (!empty($params['receive_date'])) { |
3f4b742c | 456 | $form->assign('receive_date', CRM_Utils_Date::processDate($params['receive_date'])); |
6a488035 TO |
457 | } |
458 | ||
c6327d7d | 459 | list($sendReceipt, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate( |
6a488035 TO |
460 | array( |
461 | 'groupName' => 'msg_tpl_workflow_contribution', | |
462 | 'valueName' => 'contribution_offline_receipt', | |
463 | 'contactId' => $params['contact_id'], | |
464 | 'contributionId' => $params['contribution_id'], | |
465 | 'from' => $params['from_email_address'], | |
466 | 'toName' => $contributorDisplayName, | |
467 | 'toEmail' => $contributorEmail, | |
468 | 'isTest' => $form->_mode == 'test', | |
99deba35 | 469 | 'PDFFilename' => ts('receipt').'.pdf', |
6a488035 TO |
470 | ) |
471 | ); | |
472 | ||
473 | return $sendReceipt; | |
474 | } | |
475 | ||
476 | } | |
477 | ||
478 |