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