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