Commit | Line | Data |
---|---|---|
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 | */ | |
35 | ||
36 | /** | |
2e4a81d5 | 37 | * This class provides the functionality for batch entry for contributions/memberships |
6a488035 TO |
38 | */ |
39 | class CRM_Batch_Form_Entry extends CRM_Core_Form { | |
40 | ||
41 | /** | |
42 | * maximum profile fields that will be displayed | |
43 | * | |
44 | */ | |
45 | protected $_rowCount = 1; | |
46 | ||
47 | /** | |
48 | * Batch id | |
49 | */ | |
50 | protected $_batchId; | |
51 | ||
52 | /** | |
2e4a81d5 | 53 | * Batch information |
6a488035 TO |
54 | */ |
55 | protected $_batchInfo = array(); | |
56 | ||
57 | /** | |
58 | * store the profile id associated with the batch type | |
59 | */ | |
60 | protected $_profileId; | |
61 | ||
62 | public $_action; | |
8ef12e64 | 63 | |
6a488035 TO |
64 | public $_mode; |
65 | ||
66 | public $_params; | |
67 | ||
68 | public $_membershipId = null; | |
69 | /** | |
70 | * when not to reset sort_name | |
71 | */ | |
72 | protected $_preserveDefault = TRUE; | |
73 | ||
74 | /** | |
75 | * Contact fields | |
76 | */ | |
77 | protected $_contactFields = array(); | |
78 | ||
afe349ef | 79 | /** |
80 | * Fields array of fields in the batch profile | |
81 | * (based on the uf_field table data) | |
82 | * (this can't be protected as it is passed into the CRM_Contact_Form_Task_Batch::parseStreetAddress function | |
83 | * (although a future refactoring might hopefully change that so it uses the api & the function is not | |
84 | * required | |
85 | * @var array | |
86 | */ | |
87 | public $_fields = array(); | |
6a488035 TO |
88 | /** |
89 | * build all the data structures needed to build the form | |
90 | * | |
91 | * @return void | |
92 | * @access public | |
8ef12e64 | 93 | */ |
6a488035 TO |
94 | function preProcess() { |
95 | $this->_batchId = CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE); | |
96 | ||
97 | $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse'); | |
8ef12e64 | 98 | |
6a488035 TO |
99 | if (empty($this->_batchInfo)) { |
100 | $params = array('id' => $this->_batchId); | |
101 | CRM_Batch_BAO_Batch::retrieve($params, $this->_batchInfo); | |
102 | ||
57465e2a | 103 | $this->assign('batchTotal', !empty($this->_batchInfo['total']) ? $this->_batchInfo['total'] : NULL); |
6a488035 TO |
104 | $this->assign('batchType', $this->_batchInfo['type_id']); |
105 | ||
106 | // get the profile id associted with this batch type | |
107 | $this->_profileId = CRM_Batch_BAO_Batch::getProfileId($this->_batchInfo['type_id']); | |
108 | } | |
aefb0b79 | 109 | CRM_Core_Resources::singleton() |
96ed17aa | 110 | ->addScriptFile('civicrm', 'templates/CRM/Batch/Form/Entry.js', 1, 'html-header') |
111f65f4 | 111 | ->addSetting(array('batch' => array('type_id' => $this->_batchInfo['type_id']))) |
112 | ->addSetting(array('setting' => array('monetaryThousandSeparator' => CRM_Core_Config::singleton()->monetaryThousandSeparator))) | |
113 | ->addSetting(array('setting' => array('monetaryDecimalPoint' => CRM_Core_Config::singleton()->monetaryDecimalPoint))); | |
114 | ||
6a488035 TO |
115 | } |
116 | ||
117 | /** | |
118 | * Build the form | |
119 | * | |
120 | * @access public | |
121 | * | |
122 | * @return void | |
123 | */ | |
124 | function buildQuickForm() { | |
125 | if (!$this->_profileId) { | |
126 | CRM_Core_Error::fatal(ts('Profile for bulk data entry is missing.')); | |
127 | } | |
128 | ||
129 | $this->addElement('hidden', 'batch_id', $this->_batchId); | |
130 | ||
131 | // get the profile information | |
132 | if ($this->_batchInfo['type_id'] == 1) { | |
133 | CRM_Utils_System::setTitle(ts('Batch Data Entry for Contributions')); | |
134 | $customFields = CRM_Core_BAO_CustomField::getFields('Contribution'); | |
135 | } | |
136 | else { | |
137 | CRM_Utils_System::setTitle(ts('Batch Data Entry for Memberships')); | |
138 | $customFields = CRM_Core_BAO_CustomField::getFields('Membership'); | |
139 | } | |
140 | ||
141 | $this->_fields = array(); | |
142 | $this->_fields = CRM_Core_BAO_UFGroup::getFields($this->_profileId, FALSE, CRM_Core_Action::VIEW); | |
143 | ||
144 | // remove file type field and then limit fields | |
145 | $suppressFields = FALSE; | |
146 | $removehtmlTypes = array('File', 'Autocomplete-Select'); | |
147 | foreach ($this->_fields as $name => $field) { | |
148 | if ($cfID = CRM_Core_BAO_CustomField::getKeyID($name) && | |
149 | in_array($this->_fields[$name]['html_type'], $removehtmlTypes) | |
150 | ) { | |
151 | $suppressFields = TRUE; | |
152 | unset($this->_fields[$name]); | |
153 | } | |
154 | ||
155 | //fix to reduce size as we are using this field in grid | |
156 | if (is_array($field['attributes']) && $this->_fields[$name]['attributes']['size'] > 19) { | |
157 | //shrink class to "form-text-medium" | |
158 | $this->_fields[$name]['attributes']['size'] = 19; | |
159 | } | |
160 | } | |
161 | ||
162 | $this->addFormRule(array('CRM_Batch_Form_Entry', 'formRule'), $this); | |
163 | ||
164 | // add the force save button | |
165 | $forceSave = $this->getButtonName('upload', 'force'); | |
166 | ||
167 | $this->addElement('submit', | |
168 | $forceSave, | |
169 | ts('Ignore Mismatch & Process the Batch?') | |
170 | ); | |
171 | ||
172 | $this->addButtons(array( | |
173 | array( | |
174 | 'type' => 'upload', | |
175 | 'name' => ts('Validate & Process the Batch'), | |
176 | 'isDefault' => TRUE | |
177 | ), | |
178 | array( | |
179 | 'type' => 'cancel', | |
180 | 'name' => ts('Save & Continue Later'), | |
181 | ) | |
182 | ) | |
183 | ); | |
184 | ||
185 | $this->assign('rowCount', $this->_batchInfo['item_count'] + 1); | |
186 | ||
187 | $fileFieldExists = FALSE; | |
188 | $preserveDefaultsArray = array( | |
189 | 'first_name', 'last_name', 'middle_name', | |
190 | 'organization_name', | |
191 | 'household_name', | |
192 | ); | |
193 | ||
194 | $contactTypes = array('Contact', 'Individual', 'Household', 'Organization'); | |
70223980 | 195 | $contactReturnProperties = array(); |
6a488035 | 196 | for ($rowNumber = 1; $rowNumber <= $this->_batchInfo['item_count']; $rowNumber++) { |
fe430658 | 197 | $this->addEntityRef("primary_contact_id[{$rowNumber}]", '', array('create' => TRUE, 'placeholder' => ts('- select -'))); |
6a488035 TO |
198 | |
199 | // special field specific to membership batch udpate | |
200 | if ($this->_batchInfo['type_id'] == 2) { | |
201 | $options = array( | |
202 | 1 => ts('Add Membership'), | |
203 | 2 => ts('Renew Membership'), | |
204 | ); | |
205 | $this->add('select', "member_option[$rowNumber]", '', $options); | |
206 | } | |
207 | ||
208 | foreach ($this->_fields as $name => $field) { | |
209 | if (in_array($field['field_type'], $contactTypes)) { | |
57465e2a | 210 | $fld = explode('-', $field['name']); |
5b1666f7 | 211 | $contactReturnProperties[$field['name']] = $fld[0]; |
6a488035 TO |
212 | } |
213 | CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, NULL, FALSE, FALSE, $rowNumber); | |
214 | ||
215 | if (in_array($field['name'], $preserveDefaultsArray)) { | |
216 | $this->_preserveDefault = FALSE; | |
217 | } | |
218 | } | |
219 | } | |
220 | ||
221 | $this->assign('fields', $this->_fields); | |
57465e2a | 222 | CRM_Core_Resources::singleton() |
223 | ->addSetting(array('contact' => array( | |
224 | 'return' => implode(',', $contactReturnProperties), | |
5b1666f7 | 225 | 'fieldmap' => array_flip($contactReturnProperties), |
57465e2a | 226 | ))); |
6a488035 TO |
227 | |
228 | // don't set the status message when form is submitted. | |
229 | $buttonName = $this->controller->getButtonName('submit'); | |
230 | ||
231 | if ($suppressFields && $buttonName != '_qf_Entry_next') { | |
232 | CRM_Core_Session::setStatus(ts("FILE or Autocomplete Select type field(s) in the selected profile are not supported for Batch Update."), ts("Some fields have been excluded."), "info"); | |
233 | } | |
234 | } | |
235 | ||
236 | /** | |
237 | * form validations | |
238 | * | |
239 | * @param array $params posted values of the form | |
240 | * @param array $files list of errors to be posted back to the form | |
241 | * @param array $self form object | |
242 | * | |
243 | * @return array list of errors to be posted back to the form | |
244 | * @static | |
245 | * @access public | |
246 | */ | |
247 | static function formRule($params, $files, $self) { | |
248 | $errors = array(); | |
249 | ||
a7488080 | 250 | if (!empty($params['_qf_Entry_upload_force'])) { |
6a488035 TO |
251 | return TRUE; |
252 | } | |
253 | ||
254 | $batchTotal = 0; | |
255 | foreach ($params['field'] as $key => $value) { | |
256 | $batchTotal += $value['total_amount']; | |
257 | ||
5ee60152 | 258 | //validate for soft credit fields |
ccec9d6b | 259 | if (!empty($params['soft_credit_contact_id'][$key]) && empty($params['soft_credit_amount'][$key])) { |
d1401e86 | 260 | $errors["soft_credit_amount[$key]"] = ts('Please enter the soft credit amount.'); |
5ee60152 | 261 | } |
8cc574cf | 262 | if (!empty($params['soft_credit_amount']) && !empty($params['soft_credit_amount'][$key]) && CRM_Utils_Rule::cleanMoney(CRM_Utils_Array::value($key, $params['soft_credit_amount'])) > CRM_Utils_Rule::cleanMoney($value['total_amount'])) { |
5ee60152 RN |
263 | $errors["soft_credit_amount[$key]"] = ts('Soft credit amount should not be greater than the total amount'); |
264 | } | |
8ef12e64 | 265 | |
6a488035 TO |
266 | //membership type is required for membership batch entry |
267 | if ( $self->_batchInfo['type_id'] == 2 ) { | |
a7488080 | 268 | if (empty($value['membership_type'][1])) { |
6a488035 TO |
269 | $errors["field[$key][membership_type]"] = ts('Membership type is a required field.'); |
270 | } | |
271 | } | |
272 | } | |
273 | ||
274 | if ($batchTotal != $self->_batchInfo['total']) { | |
275 | $self->assign('batchAmountMismatch', TRUE); | |
276 | $errors['_qf_defaults'] = ts('Total for amounts entered below does not match the expected batch total.'); | |
277 | } | |
278 | ||
279 | if (!empty($errors)) { | |
280 | return $errors; | |
281 | } | |
282 | ||
283 | $self->assign('batchAmountMismatch', FALSE); | |
284 | return TRUE; | |
285 | } | |
286 | ||
287 | /** | |
288 | * Override default cancel action | |
289 | */ | |
290 | function cancelAction() { | |
291 | // redirect to batch listing | |
292 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/batch', 'reset=1')); | |
293 | CRM_Utils_System::civiExit(); | |
294 | } | |
295 | ||
296 | /** | |
297 | * This function sets the default values for the form. | |
298 | * | |
299 | * @access public | |
300 | * | |
355ba699 | 301 | * @return void |
6a488035 TO |
302 | */ |
303 | function setDefaultValues() { | |
304 | if (empty($this->_fields)) { | |
305 | return; | |
306 | } | |
307 | ||
308 | // for add mode set smart defaults | |
309 | if ( $this->_action & CRM_Core_Action::ADD ) { | |
310 | list( $currentDate, $currentTime ) = CRM_Utils_Date::setDateDefaults( NULL, 'activityDateTime' ); | |
311 | ||
312 | //get all status | |
313 | $allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
8ef12e64 | 314 | $completeStatus = array_search( 'Completed', $allStatus ); |
315 | $specialFields = array( | |
6a488035 TO |
316 | 'join_date' => $currentDate, |
317 | 'receive_date' => $currentDate, | |
318 | 'receive_date_time' => $currentTime, | |
8ef12e64 | 319 | 'contribution_status_id' => $completeStatus |
6a488035 TO |
320 | ); |
321 | ||
322 | for ($rowNumber = 1; $rowNumber <= $this->_batchInfo['item_count']; $rowNumber++) { | |
323 | foreach ($specialFields as $key => $value ) { | |
8ef12e64 | 324 | $defaults['field'][$rowNumber][$key] = $value; |
6a488035 | 325 | } |
8ef12e64 | 326 | } |
6a488035 TO |
327 | } |
328 | else { | |
6c97864e | 329 | // get the cached info from data column of civicrm_batch |
330 | $data = CRM_Core_DAO::getFieldValue('CRM_Batch_BAO_Batch', $this->_batchId, 'data'); | |
331 | $defaults = json_decode($data, TRUE); | |
c0d307ec | 332 | $defaults = $defaults['values']; |
6a488035 | 333 | } |
6c97864e | 334 | |
6a488035 TO |
335 | return $defaults; |
336 | } | |
337 | ||
338 | /** | |
339 | * process the form after the input has been submitted and validated | |
340 | * | |
341 | * @access public | |
342 | * | |
355ba699 | 343 | * @return void |
6a488035 TO |
344 | */ |
345 | public function postProcess() { | |
346 | $params = $this->controller->exportValues($this->_name); | |
347 | ||
348 | $params['actualBatchTotal'] = 0; | |
349 | ||
350 | // get the profile information | |
351 | if ($this->_batchInfo['type_id'] == 1) { | |
352 | $this->processContribution($params); | |
353 | } | |
354 | else { | |
355 | $this->processMembership($params); | |
356 | } | |
357 | ||
358 | // update batch to close status | |
359 | $paramValues = array( | |
360 | 'id' => $this->_batchId, | |
361 | // close status | |
2d818e4a | 362 | 'status_id' => CRM_Core_OptionGroup::getValue('batch_status', 'Closed', 'name'), |
6a488035 TO |
363 | 'total' => $params['actualBatchTotal'], |
364 | ); | |
365 | ||
366 | CRM_Batch_BAO_Batch::create($paramValues); | |
367 | ||
6a488035 TO |
368 | // set success status |
369 | CRM_Core_Session::setStatus("", ts("Batch Processed."), "success"); | |
370 | ||
371 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/batch', 'reset=1')); | |
372 | } | |
373 | ||
374 | /** | |
375 | * process contribution records | |
376 | * | |
377 | * @param array $params associated array of submitted values | |
378 | * | |
379 | * @access public | |
380 | * | |
355ba699 | 381 | * @return void |
6a488035 TO |
382 | */ |
383 | private function processContribution(&$params) { | |
384 | $dates = array( | |
385 | 'receive_date', | |
386 | 'receipt_date', | |
387 | 'thankyou_date', | |
388 | 'cancel_date', | |
389 | ); | |
390 | ||
391 | // get the price set associated with offline contribution record. | |
9da8dc8c | 392 | $priceSetId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', 'default_contribution_amount', 'id', 'name'); |
393 | $this->_priceSet = current(CRM_Price_BAO_PriceSet::getSetDetail($priceSetId)); | |
85020e43 EM |
394 | $priceFieldID = CRM_Price_BAO_PriceSet::getOnlyPriceFieldID($this->_priceSet); |
395 | $priceFieldValueID = CRM_Price_BAO_PriceSet::getOnlyPriceFieldValueID($this->_priceSet); | |
6a488035 | 396 | |
6a488035 TO |
397 | if (isset($params['field'])) { |
398 | foreach ($params['field'] as $key => $value) { | |
399 | // if contact is not selected we should skip the row | |
ccec9d6b | 400 | if (empty($params['primary_contact_id'][$key])) { |
6a488035 TO |
401 | continue; |
402 | } | |
403 | ||
ccec9d6b | 404 | $value['contact_id'] = CRM_Utils_Array::value($key, $params['primary_contact_id']); |
6a488035 TO |
405 | |
406 | // update contact information | |
407 | $this->updateContactInfo($value); | |
408 | ||
5ee60152 | 409 | //build soft credit params |
ccec9d6b CW |
410 | if (!empty($params['soft_credit_contact_id'][$key]) && !empty($params['soft_credit_amount'][$key])) { |
411 | $value['soft_credit'][$key]['contact_id'] = $params['soft_credit_contact_id'][$key]; | |
0689c15c | 412 | $value['soft_credit'][$key]['amount'] = CRM_Utils_Rule::cleanMoney($params['soft_credit_amount'][$key]); |
9e1854a1 | 413 | |
414 | //CRM-15350: if soft-credit-type profile field is disabled or removed then | |
415 | //we choose configured SCT default value | |
416 | if (!empty($params['soft_credit_type'][$key])) { | |
417 | $value['soft_credit'][$key]['soft_credit_type_id'] = $params['soft_credit_type'][$key]; | |
418 | } | |
419 | else { | |
420 | $value['soft_credit'][$key]['soft_credit_type_id'] = CRM_Core_OptionGroup::getDefaultValue("soft_credit_type"); | |
421 | } | |
6a488035 TO |
422 | } |
423 | ||
424 | $value['custom'] = CRM_Core_BAO_CustomField::postProcess($value, | |
425 | CRM_Core_DAO::$_nullObject, | |
df1f662a | 426 | NULL, |
6a488035 TO |
427 | 'Contribution' |
428 | ); | |
429 | ||
430 | foreach ($dates as $val) { | |
a7488080 | 431 | if (!empty($value[$val])) { |
6a488035 TO |
432 | $value[$val] = CRM_Utils_Date::processDate( $value[$val], $value[$val . '_time'], TRUE ); |
433 | } | |
434 | } | |
435 | ||
a7488080 | 436 | if (!empty($value['send_receipt'])) { |
6a488035 TO |
437 | $value['receipt_date'] = date('Y-m-d His'); |
438 | } | |
2e4a81d5 EM |
439 | // these translations & date handling are required because we are calling BAO directly rather than the api |
440 | $fieldTranslations = array( | |
441 | 'financial_type' => 'financial_type_id', | |
442 | 'payment_instrument' => 'payment_instrument_id', | |
443 | 'contribution_source' => 'source', | |
444 | 'contribution_note' => 'note', | |
6a488035 | 445 | |
2e4a81d5 EM |
446 | ); |
447 | foreach ($fieldTranslations as $formField => $baoField) { | |
448 | if(isset($value[$formField])) { | |
449 | $value[$baoField] = $value[$formField]; | |
450 | } | |
451 | unset($value[$formField]); | |
6a488035 TO |
452 | } |
453 | ||
454 | $params['actualBatchTotal'] += $value['total_amount']; | |
6a488035 TO |
455 | $value['batch_id'] = $this->_batchId; |
456 | $value['skipRecentView'] = TRUE; | |
457 | ||
458 | // build line item params | |
9d3888dc EM |
459 | $this->_priceSet['fields'][$priceFieldID]['options'][$priceFieldValueID ]['amount'] = $value['total_amount']; |
460 | $value['price_'. $priceFieldID] = 1; | |
6a488035 TO |
461 | |
462 | $lineItem = array(); | |
9da8dc8c | 463 | CRM_Price_BAO_PriceSet::processAmount($this->_priceSet['fields'], $value, $lineItem[$priceSetId]); |
6a488035 TO |
464 | |
465 | //unset amount level since we always use quick config price set | |
466 | unset($value['amount_level']); | |
467 | ||
2e4a81d5 | 468 | //CRM-11529 for back office transactions |
8ef12e64 | 469 | //when financial_type_id is passed in form, update the |
2e4a81d5 | 470 | //line items with the financial type selected in form |
8cc574cf | 471 | if (!empty($value['financial_type_id']) && !empty($lineItem[$priceSetId])) { |
6a488035 TO |
472 | foreach ($lineItem[$priceSetId] as &$values) { |
473 | $values['financial_type_id'] = $value['financial_type_id']; | |
474 | } | |
475 | } | |
476 | $value['line_item'] = $lineItem; | |
477 | ||
478 | //finally call contribution create for all the magic | |
479 | $contribution = CRM_Contribute_BAO_Contribution::create($value, CRM_Core_DAO::$_nullArray); | |
480 | ||
481 | //process premiums | |
a7488080 | 482 | if (!empty($value['product_name'])) { |
6a488035 TO |
483 | if ($value['product_name'][0] > 0) { |
484 | list($products, $options) = CRM_Contribute_BAO_Premium::getPremiumProductInfo(); | |
485 | ||
486 | $value['hidden_Premium'] = 1; | |
487 | $value['product_option'] = CRM_Utils_Array::value( | |
488 | $value['product_name'][1], | |
489 | $options[$value['product_name'][0]] | |
490 | ); | |
491 | ||
492 | $premiumParams = array( | |
493 | 'product_id' => $value['product_name'][0], | |
494 | 'contribution_id' => $contribution->id, | |
495 | 'product_option' => $value['product_option'], | |
496 | 'quantity' => 1, | |
497 | ); | |
498 | CRM_Contribute_BAO_Contribution::addPremium($premiumParams); | |
499 | } | |
500 | } | |
501 | // end of premium | |
502 | ||
503 | //send receipt mail. | |
8cc574cf | 504 | if ( $contribution->id && !empty($value['send_receipt'])) { |
6a488035 TO |
505 | // add the domain email id |
506 | $domainEmail = CRM_Core_BAO_Domain::getNameAndEmail(); | |
507 | $domainEmail = "$domainEmail[0] <$domainEmail[1]>"; | |
508 | ||
509 | $value['from_email_address'] = $domainEmail; | |
510 | $value['contribution_id'] = $contribution->id; | |
511 | CRM_Contribute_Form_AdditionalInfo::emailReceipt( $this, $value ); | |
512 | } | |
513 | } | |
514 | } | |
2e4a81d5 | 515 | return TRUE; |
6a488035 TO |
516 | } |
517 | //end of function | |
518 | ||
519 | /** | |
520 | * process membership records | |
521 | * | |
522 | * @param array $params associated array of submitted values | |
523 | * | |
524 | * @access public | |
525 | * | |
2e4a81d5 | 526 | * @return bool |
6a488035 TO |
527 | */ |
528 | private function processMembership(&$params) { | |
529 | $dateTypes = array( | |
530 | 'join_date' => 'joinDate', | |
531 | 'membership_start_date' => 'startDate', | |
532 | 'membership_end_date' => 'endDate' | |
533 | ); | |
8ef12e64 | 534 | |
6a488035 TO |
535 | $dates = array( |
536 | 'join_date', | |
537 | 'start_date', | |
538 | 'end_date', | |
539 | 'reminder_date' | |
540 | ); | |
541 | ||
542 | // get the price set associated with offline memebership | |
9da8dc8c | 543 | $priceSetId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', 'default_membership_type_amount', 'id', 'name'); |
544 | $this->_priceSet = $priceSets = current(CRM_Price_BAO_PriceSet::getSetDetail($priceSetId)); | |
6a488035 TO |
545 | |
546 | if (isset($params['field'])) { | |
547 | $customFields = array(); | |
548 | foreach ($params['field'] as $key => $value) { | |
549 | // if contact is not selected we should skip the row | |
ccec9d6b | 550 | if (empty($params['primary_contact_id'][$key])) { |
6a488035 TO |
551 | continue; |
552 | } | |
553 | ||
ccec9d6b | 554 | $value['contact_id'] = CRM_Utils_Array::value($key, $params['primary_contact_id']); |
6a488035 TO |
555 | |
556 | // update contact information | |
557 | $this->updateContactInfo($value); | |
558 | ||
559 | $membershipTypeId = $value['membership_type_id'] = $value['membership_type'][1]; | |
560 | ||
561 | foreach ($dateTypes as $dateField => $dateVariable) { | |
562 | $$dateVariable = CRM_Utils_Date::processDate($value[$dateField]); | |
563 | } | |
564 | ||
565 | $calcDates = array(); | |
566 | $calcDates[$membershipTypeId] = CRM_Member_BAO_MembershipType::getDatesForMembershipType($membershipTypeId, | |
567 | $joinDate, $startDate, $endDate | |
568 | ); | |
569 | ||
570 | foreach ($calcDates as $memType => $calcDate) { | |
571 | foreach ($dates as $d) { | |
572 | //first give priority to form values then calDates. | |
573 | $date = CRM_Utils_Array::value($d, $value); | |
574 | if (!$date) { | |
575 | $date = CRM_Utils_Array::value($d, $calcDate); | |
576 | } | |
577 | ||
578 | $value[$d] = CRM_Utils_Date::processDate($date); | |
579 | } | |
580 | } | |
581 | ||
a7488080 | 582 | if (!empty($value['send_receipt'])) { |
6a488035 TO |
583 | $value['receipt_date'] = date('Y-m-d His'); |
584 | } | |
585 | ||
a7488080 | 586 | if (!empty($value['membership_source'])) { |
6a488035 TO |
587 | $value['source'] = $value['membership_source']; |
588 | } | |
589 | ||
590 | unset($value['membership_source']); | |
591 | ||
592 | //Get the membership status | |
a7488080 | 593 | if (!empty($value['membership_status'])) { |
6a488035 TO |
594 | $value['status_id'] = $value['membership_status']; |
595 | unset($value['membership_status']); | |
596 | } | |
597 | ||
598 | if (empty($customFields)) { | |
599 | // membership type custom data | |
600 | $customFields = CRM_Core_BAO_CustomField::getFields('Membership', FALSE, FALSE, $membershipTypeId); | |
601 | ||
602 | $customFields = CRM_Utils_Array::crmArrayMerge($customFields, | |
603 | CRM_Core_BAO_CustomField::getFields('Membership', | |
604 | FALSE, FALSE, NULL, NULL, TRUE | |
605 | ) | |
606 | ); | |
607 | } | |
608 | ||
609 | //check for custom data | |
610 | $value['custom'] = CRM_Core_BAO_CustomField::postProcess($params['field'][$key], | |
611 | $customFields, | |
612 | $key, | |
613 | 'Membership', | |
614 | $membershipTypeId | |
615 | ); | |
616 | ||
a7488080 | 617 | if (!empty($value['financial_type'])) { |
6a488035 TO |
618 | $value['financial_type_id'] = $value['financial_type']; |
619 | } | |
620 | ||
a7488080 | 621 | if (!empty($value['payment_instrument'])) { |
6a488035 TO |
622 | $value['payment_instrument_id'] = $value['payment_instrument']; |
623 | } | |
624 | ||
625 | // handle soft credit | |
9e1854a1 | 626 | if (!empty($params['soft_credit_contact_id'][$key]) && !empty($params['soft_credit_amount'][$key])) { |
ccec9d6b | 627 | $value['soft_credit'][$key]['contact_id'] = $params['soft_credit_contact_id'][$key]; |
0689c15c | 628 | $value['soft_credit'][$key]['amount'] = CRM_Utils_Rule::cleanMoney($params['soft_credit_amount'][$key]); |
9e1854a1 | 629 | |
630 | //CRM-15350: if soft-credit-type profile field is disabled or removed then | |
631 | //we choose Gift as default value as per Gift Membership rule | |
632 | if (!empty($params['soft_credit_type'][$key])) { | |
633 | $value['soft_credit'][$key]['soft_credit_type_id'] = $params['soft_credit_type'][$key]; | |
634 | } | |
635 | else { | |
636 | $value['soft_credit'][$key]['soft_credit_type_id'] = CRM_Core_OptionGroup::getValue('soft_credit_type', 'Gift', 'name'); | |
637 | } | |
6a488035 TO |
638 | } |
639 | ||
a7488080 | 640 | if (!empty($value['receive_date'])) { |
6a488035 TO |
641 | $value['receive_date'] = CRM_Utils_Date::processDate( $value['receive_date'], $value['receive_date_time'] , TRUE ); |
642 | } | |
643 | ||
644 | $params['actualBatchTotal'] += $value['total_amount']; | |
645 | ||
646 | unset($value['financial_type']); | |
647 | unset($value['payment_instrument']); | |
648 | ||
649 | $value['batch_id'] = $this->_batchId; | |
650 | $value['skipRecentView'] = TRUE; | |
651 | ||
652 | // make entry in line item for contribution | |
8ef12e64 | 653 | |
6a488035 TO |
654 | $editedFieldParams = array( |
655 | 'price_set_id' => $priceSetId, | |
8ef12e64 | 656 | 'name' => $value['membership_type'][0] |
6a488035 TO |
657 | ); |
658 | ||
659 | $editedResults = array(); | |
9da8dc8c | 660 | CRM_Price_BAO_PriceField::retrieve($editedFieldParams, $editedResults); |
6a488035 TO |
661 | |
662 | if (!empty($editedResults)) { | |
663 | unset($this->_priceSet['fields']); | |
664 | $this->_priceSet['fields'][$editedResults['id']] = $priceSets['fields'][$editedResults['id']]; | |
665 | unset($this->_priceSet['fields'][$editedResults['id']]['options']); | |
666 | $fid = $editedResults['id']; | |
667 | $editedFieldParams = array( | |
668 | 'price_field_id' => $editedResults['id'], | |
669 | 'membership_type_id' => $value['membership_type_id'] | |
670 | ); | |
671 | ||
672 | $editedResults = array(); | |
9da8dc8c | 673 | CRM_Price_BAO_PriceFieldValue::retrieve($editedFieldParams, $editedResults); |
6a488035 | 674 | $this->_priceSet['fields'][$fid]['options'][$editedResults['id']] = $priceSets['fields'][$fid]['options'][$editedResults['id']]; |
a7488080 | 675 | if (!empty($value['total_amount'])) { |
6a488035 TO |
676 | $this->_priceSet['fields'][$fid]['options'][$editedResults['id']]['amount'] = $value['total_amount']; |
677 | } | |
678 | ||
679 | $fieldID = key($this->_priceSet['fields']); | |
680 | $value['price_' . $fieldID] = $editedResults['id']; | |
681 | ||
8ef12e64 | 682 | $lineItem = array(); |
9da8dc8c | 683 | CRM_Price_BAO_PriceSet::processAmount($this->_priceSet['fields'], |
6a488035 TO |
684 | $value, $lineItem[$priceSetId] |
685 | ); | |
686 | ||
8ef12e64 | 687 | //CRM-11529 for backoffice transactions |
688 | //when financial_type_id is passed in form, update the | |
6a488035 | 689 | //lineitems with the financial type selected in form |
8cc574cf | 690 | if (!empty($value['financial_type_id']) && !empty($lineItem[$priceSetId])) { |
6a488035 TO |
691 | foreach ($lineItem[$priceSetId] as &$values) { |
692 | $values['financial_type_id'] = $value['financial_type_id']; | |
693 | } | |
694 | } | |
8ef12e64 | 695 | |
6a488035 TO |
696 | $value['lineItems'] = $lineItem; |
697 | $value['processPriceSet'] = TRUE; | |
698 | } | |
699 | // end of contribution related section | |
700 | ||
701 | unset($value['membership_type']); | |
702 | unset($value['membership_start_date']); | |
703 | unset($value['membership_end_date']); | |
8ef12e64 | 704 | |
705 | $value['is_renew'] = false; | |
a7488080 | 706 | if (!empty($params['member_option']) && CRM_Utils_Array::value( $key, $params['member_option'] ) == 2 ) { |
6a488035 | 707 | $this->_params = $params; |
8ef12e64 | 708 | $value['is_renew'] = true; |
c98997b9 | 709 | $membership = CRM_Member_BAO_Membership::renewMembershipFormWrapper( |
6a488035 TO |
710 | $value['contact_id'], |
711 | $value['membership_type_id'], | |
712 | FALSE, $this, NULL, NULL, | |
713 | $value['custom'] | |
714 | ); | |
715 | ||
716 | // make contribution entry | |
28cc62fc | 717 | $contrbutionParams = array_merge($value, array('membership_id' => $membership->id)); |
718 | CRM_Member_BAO_Membership::recordMembershipContribution($contrbutionParams); | |
8ef12e64 | 719 | } |
6a488035 TO |
720 | else { |
721 | $membership = CRM_Member_BAO_Membership::create($value, CRM_Core_DAO::$_nullArray); | |
722 | } | |
723 | ||
724 | //process premiums | |
a7488080 | 725 | if (!empty($value['product_name'])) { |
6a488035 TO |
726 | if ($value['product_name'][0] > 0) { |
727 | list($products, $options) = CRM_Contribute_BAO_Premium::getPremiumProductInfo(); | |
728 | ||
729 | $value['hidden_Premium'] = 1; | |
730 | $value['product_option'] = CRM_Utils_Array::value( | |
731 | $value['product_name'][1], | |
732 | $options[$value['product_name'][0]] | |
733 | ); | |
734 | ||
735 | $premiumParams = array( | |
736 | 'product_id' => $value['product_name'][0], | |
737 | 'contribution_id' => $value['contribution_id'], | |
738 | 'product_option' => $value['product_option'], | |
739 | 'quantity' => 1, | |
740 | ); | |
741 | CRM_Contribute_BAO_Contribution::addPremium($premiumParams); | |
742 | } | |
743 | } | |
744 | // end of premium | |
745 | ||
746 | //send receipt mail. | |
8cc574cf | 747 | if ( $membership->id && !empty($value['send_receipt'])) { |
6a488035 TO |
748 | |
749 | // add the domain email id | |
750 | $domainEmail = CRM_Core_BAO_Domain::getNameAndEmail(); | |
751 | $domainEmail = "$domainEmail[0] <$domainEmail[1]>"; | |
752 | ||
753 | $value['from_email_address'] = $domainEmail; | |
754 | $value['membership_id'] = $membership->id; | |
755 | CRM_Member_Form_Membership::emailReceipt( $this, $value, $membership ); | |
756 | } | |
757 | } | |
758 | } | |
afe349ef | 759 | return TRUE; |
6a488035 TO |
760 | } |
761 | ||
762 | /** | |
763 | * update contact information | |
764 | * | |
765 | * @param array $value associated array of submitted values | |
766 | * | |
767 | * @access public | |
768 | * | |
355ba699 | 769 | * @return void |
6a488035 TO |
770 | */ |
771 | private function updateContactInfo(&$value) { | |
772 | $value['preserveDBName'] = $this->_preserveDefault; | |
773 | ||
774 | //parse street address, CRM-7768 | |
775 | CRM_Contact_Form_Task_Batch::parseStreetAddress($value, $this); | |
776 | ||
777 | CRM_Contact_BAO_Contact::createProfileContact($value, $this->_fields, | |
778 | $value['contact_id'] | |
779 | ); | |
780 | } | |
cab024d4 | 781 | |
afe349ef | 782 | /** |
783 | * Function exists purely for unit testing purposes. If you feel tempted to use this in live code | |
784 | * then it probably means there is some functionality that needs to be moved | |
785 | * out of the form layer | |
cab024d4 | 786 | * |
afe349ef | 787 | * @param unknown_type $params |
cab024d4 EM |
788 | * |
789 | * @return bool | |
afe349ef | 790 | */ |
791 | function testProcessMembership($params) { | |
792 | return $this->processMembership($params); | |
793 | } | |
cab024d4 EM |
794 | |
795 | /** | |
796 | * Function exists purely for unit testing purposes. If you feel tempted to use this in live code | |
797 | * then it probably means there is some functionality that needs to be moved | |
798 | * out of the form layer | |
799 | * | |
800 | * @param array $params | |
801 | * | |
802 | * @return bool | |
803 | */ | |
804 | function testProcessContribution($params) { | |
805 | return $this->processContribution($params); | |
806 | } | |
6a488035 TO |
807 | } |
808 |