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