| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.6 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 9 | | | |
| 10 | | CiviCRM is free software; you can copy, modify, and distribute it | |
| 11 | | under the terms of the GNU Affero General Public License | |
| 12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
| 13 | | | |
| 14 | | CiviCRM is distributed in the hope that it will be useful, but | |
| 15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 17 | | See the GNU Affero General Public License for more details. | |
| 18 | | | |
| 19 | | You should have received a copy of the GNU Affero General Public | |
| 20 | | License and the CiviCRM Licensing Exception along | |
| 21 | | with this program; if not, contact CiviCRM LLC | |
| 22 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
| 23 | | GNU Affero General Public License or the licensing of CiviCRM, | |
| 24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
| 25 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * This class provides the functionality for batch entry for contributions/memberships |
| 38 | */ |
| 39 | class CRM_Batch_Form_Entry extends CRM_Core_Form { |
| 40 | |
| 41 | /** |
| 42 | * Maximum profile fields that will be displayed. |
| 43 | */ |
| 44 | protected $_rowCount = 1; |
| 45 | |
| 46 | /** |
| 47 | * Batch id. |
| 48 | */ |
| 49 | protected $_batchId; |
| 50 | |
| 51 | /** |
| 52 | * Batch information. |
| 53 | */ |
| 54 | protected $_batchInfo = array(); |
| 55 | |
| 56 | /** |
| 57 | * Store the profile id associated with the batch type. |
| 58 | */ |
| 59 | protected $_profileId; |
| 60 | |
| 61 | public $_action; |
| 62 | |
| 63 | public $_mode; |
| 64 | |
| 65 | public $_params; |
| 66 | |
| 67 | public $_membershipId = NULL; |
| 68 | /** |
| 69 | * When not to reset sort_name. |
| 70 | */ |
| 71 | protected $_preserveDefault = TRUE; |
| 72 | |
| 73 | /** |
| 74 | * Contact fields. |
| 75 | */ |
| 76 | protected $_contactFields = array(); |
| 77 | |
| 78 | /** |
| 79 | * Fields array of fields in the batch profile. |
| 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(); |
| 87 | |
| 88 | /** |
| 89 | * Build all the data structures needed to build the form. |
| 90 | * |
| 91 | * @return void |
| 92 | */ |
| 93 | public function preProcess() { |
| 94 | $this->_batchId = CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE); |
| 95 | |
| 96 | $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse'); |
| 97 | |
| 98 | if (empty($this->_batchInfo)) { |
| 99 | $params = array('id' => $this->_batchId); |
| 100 | CRM_Batch_BAO_Batch::retrieve($params, $this->_batchInfo); |
| 101 | |
| 102 | $this->assign('batchTotal', !empty($this->_batchInfo['total']) ? $this->_batchInfo['total'] : NULL); |
| 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 | } |
| 108 | CRM_Core_Resources::singleton() |
| 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))); |
| 113 | |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Build the form object. |
| 118 | * |
| 119 | * |
| 120 | * @return void |
| 121 | */ |
| 122 | public function buildQuickForm() { |
| 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 | |
| 129 | $batchTypes = CRM_Core_Pseudoconstant::get('CRM_Batch_DAO_Batch', 'type_id', array('flip' => 1), 'validate'); |
| 130 | // get the profile information |
| 131 | if ($this->_batchInfo['type_id'] == $batchTypes['Contribution']) { |
| 132 | CRM_Utils_System::setTitle(ts('Batch Data Entry for Contributions')); |
| 133 | $customFields = CRM_Core_BAO_CustomField::getFields('Contribution'); |
| 134 | } |
| 135 | elseif ($this->_batchInfo['type_id'] == $batchTypes['Membership']) { |
| 136 | CRM_Utils_System::setTitle(ts('Batch Data Entry for Memberships')); |
| 137 | $customFields = CRM_Core_BAO_CustomField::getFields('Membership'); |
| 138 | } |
| 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 | } |
| 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', |
| 192 | 'last_name', |
| 193 | 'middle_name', |
| 194 | 'organization_name', |
| 195 | 'household_name', |
| 196 | ); |
| 197 | |
| 198 | $contactTypes = array('Contact', 'Individual', 'Household', 'Organization'); |
| 199 | $contactReturnProperties = array(); |
| 200 | $config = CRM_Core_Config::singleton(); |
| 201 | |
| 202 | for ($rowNumber = 1; $rowNumber <= $this->_batchInfo['item_count']; $rowNumber++) { |
| 203 | $this->addEntityRef("primary_contact_id[{$rowNumber}]", '', array( |
| 204 | 'create' => TRUE, |
| 205 | 'placeholder' => ts('- select -'), |
| 206 | )); |
| 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 | } |
| 216 | if ($this->_batchInfo['type_id'] == $batchTypes['Pledge Payment']) { |
| 217 | $options = array('' => '-select-'); |
| 218 | $optionTypes = array( |
| 219 | '1' => ts('Adjust Pledge Payment Schedule?'), |
| 220 | '2' => ts('Adjust Total Pledge Amount?'), |
| 221 | ); |
| 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); |
| 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); |
| 229 | $options += array($pledgeID => CRM_Utils_Date::customFormat($pledgePayment['schedule_date'], '%m/%d/%Y') . ', ' . $pledgePayment['amount'] . ' ' . $pledgePayment['currency']); |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | $this->add('select', "open_pledges[$rowNumber]", '', $options); |
| 235 | } |
| 236 | |
| 237 | foreach ($this->_fields as $name => $field) { |
| 238 | if (in_array($field['field_type'], $contactTypes)) { |
| 239 | $fld = explode('-', $field['name']); |
| 240 | $contactReturnProperties[$field['name']] = $fld[0]; |
| 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); |
| 251 | CRM_Core_Resources::singleton() |
| 252 | ->addSetting(array( |
| 253 | 'contact' => array( |
| 254 | 'return' => implode(',', $contactReturnProperties), |
| 255 | 'fieldmap' => array_flip($contactReturnProperties), |
| 256 | ), |
| 257 | )); |
| 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') { |
| 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'); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Form validations. |
| 269 | * |
| 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. |
| 276 | * |
| 277 | * @return array |
| 278 | * list of errors to be posted back to the form |
| 279 | */ |
| 280 | public static function formRule($params, $files, $self) { |
| 281 | $errors = array(); |
| 282 | $batchTypes = CRM_Core_Pseudoconstant::get('CRM_Batch_DAO_Batch', 'type_id', array('flip' => 1), 'validate'); |
| 283 | |
| 284 | if (!empty($params['_qf_Entry_upload_force'])) { |
| 285 | return TRUE; |
| 286 | } |
| 287 | |
| 288 | $batchTotal = 0; |
| 289 | foreach ($params['field'] as $key => $value) { |
| 290 | $batchTotal += $value['total_amount']; |
| 291 | |
| 292 | //validate for soft credit fields |
| 293 | if (!empty($params['soft_credit_contact_id'][$key]) && empty($params['soft_credit_amount'][$key])) { |
| 294 | $errors["soft_credit_amount[$key]"] = ts('Please enter the soft credit amount.'); |
| 295 | } |
| 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'])) { |
| 297 | $errors["soft_credit_amount[$key]"] = ts('Soft credit amount should not be greater than the total amount'); |
| 298 | } |
| 299 | |
| 300 | //membership type is required for membership batch entry |
| 301 | if ($self->_batchInfo['type_id'] == $batchTypes['Membership']) { |
| 302 | if (empty($value['membership_type'][1])) { |
| 303 | $errors["field[$key][membership_type]"] = ts('Membership type is a required field.'); |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | if ($self->_batchInfo['type_id'] == $batchTypes['Pledge Payment']) { |
| 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) { |
| 312 | $errors["open_pledges[$key]"] = ts('You can not record two payments for the same pledge in a single batch.'); |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | } |
| 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 | /** |
| 331 | * Override default cancel action. |
| 332 | */ |
| 333 | public function cancelAction() { |
| 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 | /** |
| 340 | * Set default values for the form. |
| 341 | * |
| 342 | * |
| 343 | * @return void |
| 344 | */ |
| 345 | public function setDefaultValues() { |
| 346 | if (empty($this->_fields)) { |
| 347 | return; |
| 348 | } |
| 349 | |
| 350 | // for add mode set smart defaults |
| 351 | if ($this->_action & CRM_Core_Action::ADD) { |
| 352 | list($currentDate, $currentTime) = CRM_Utils_Date::setDateDefaults(NULL, 'activityDateTime'); |
| 353 | |
| 354 | //get all status |
| 355 | $allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); |
| 356 | $completeStatus = array_search('Completed', $allStatus); |
| 357 | $specialFields = array( |
| 358 | 'join_date' => $currentDate, |
| 359 | 'receive_date' => $currentDate, |
| 360 | 'receive_date_time' => $currentTime, |
| 361 | 'contribution_status_id' => $completeStatus, |
| 362 | ); |
| 363 | |
| 364 | for ($rowNumber = 1; $rowNumber <= $this->_batchInfo['item_count']; $rowNumber++) { |
| 365 | foreach ($specialFields as $key => $value) { |
| 366 | $defaults['field'][$rowNumber][$key] = $value; |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | else { |
| 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); |
| 374 | $defaults = $defaults['values']; |
| 375 | } |
| 376 | |
| 377 | return $defaults; |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * Process the form after the input has been submitted and validated. |
| 382 | * |
| 383 | * |
| 384 | * @return void |
| 385 | */ |
| 386 | public function postProcess() { |
| 387 | $params = $this->controller->exportValues($this->_name); |
| 388 | $params['actualBatchTotal'] = 0; |
| 389 | |
| 390 | // get the profile information |
| 391 | $batchTypes = CRM_Core_Pseudoconstant::get('CRM_Batch_DAO_Batch', 'type_id', array('flip' => 1), 'validate'); |
| 392 | if (in_array($this->_batchInfo['type_id'], array($batchTypes['Pledge Payment'], $batchTypes['Contribution']))) { |
| 393 | $this->processContribution($params); |
| 394 | } |
| 395 | elseif ($this->_batchInfo['type_id'] == $batchTypes['Membership']) { |
| 396 | $this->processMembership($params); |
| 397 | } |
| 398 | |
| 399 | // update batch to close status |
| 400 | $paramValues = array( |
| 401 | 'id' => $this->_batchId, |
| 402 | // close status |
| 403 | 'status_id' => CRM_Core_OptionGroup::getValue('batch_status', 'Closed', 'name'), |
| 404 | 'total' => $params['actualBatchTotal'], |
| 405 | ); |
| 406 | |
| 407 | CRM_Batch_BAO_Batch::create($paramValues); |
| 408 | |
| 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 | /** |
| 416 | * Process contribution records. |
| 417 | * |
| 418 | * @param array $params |
| 419 | * Associated array of submitted values. |
| 420 | * |
| 421 | * |
| 422 | * @return void |
| 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. |
| 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)); |
| 435 | $priceFieldID = CRM_Price_BAO_PriceSet::getOnlyPriceFieldID($this->_priceSet); |
| 436 | $priceFieldValueID = CRM_Price_BAO_PriceSet::getOnlyPriceFieldValueID($this->_priceSet); |
| 437 | |
| 438 | if (isset($params['field'])) { |
| 439 | foreach ($params['field'] as $key => $value) { |
| 440 | // if contact is not selected we should skip the row |
| 441 | if (empty($params['primary_contact_id'][$key])) { |
| 442 | continue; |
| 443 | } |
| 444 | |
| 445 | $value['contact_id'] = CRM_Utils_Array::value($key, $params['primary_contact_id']); |
| 446 | |
| 447 | // update contact information |
| 448 | $this->updateContactInfo($value); |
| 449 | |
| 450 | //build soft credit params |
| 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]; |
| 453 | $value['soft_credit'][$key]['amount'] = CRM_Utils_Rule::cleanMoney($params['soft_credit_amount'][$key]); |
| 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 | } |
| 463 | } |
| 464 | |
| 465 | $value['custom'] = CRM_Core_BAO_CustomField::postProcess($value, |
| 466 | NULL, |
| 467 | 'Contribution' |
| 468 | ); |
| 469 | |
| 470 | foreach ($dates as $val) { |
| 471 | if (!empty($value[$val])) { |
| 472 | $value[$val] = CRM_Utils_Date::processDate($value[$val], $value[$val . '_time'], TRUE); |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | if (!empty($value['send_receipt'])) { |
| 477 | $value['receipt_date'] = date('Y-m-d His'); |
| 478 | } |
| 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', |
| 485 | |
| 486 | ); |
| 487 | foreach ($fieldTranslations as $formField => $baoField) { |
| 488 | if (isset($value[$formField])) { |
| 489 | $value[$baoField] = $value[$formField]; |
| 490 | } |
| 491 | unset($value[$formField]); |
| 492 | } |
| 493 | |
| 494 | $params['actualBatchTotal'] += $value['total_amount']; |
| 495 | $value['batch_id'] = $this->_batchId; |
| 496 | $value['skipRecentView'] = TRUE; |
| 497 | |
| 498 | // build line item params |
| 499 | $this->_priceSet['fields'][$priceFieldID]['options'][$priceFieldValueID]['amount'] = $value['total_amount']; |
| 500 | $value['price_' . $priceFieldID] = 1; |
| 501 | |
| 502 | $lineItem = array(); |
| 503 | CRM_Price_BAO_PriceSet::processAmount($this->_priceSet['fields'], $value, $lineItem[$priceSetId]); |
| 504 | |
| 505 | //unset amount level since we always use quick config price set |
| 506 | unset($value['amount_level']); |
| 507 | |
| 508 | //CRM-11529 for back office transactions |
| 509 | //when financial_type_id is passed in form, update the |
| 510 | //line items with the financial type selected in form |
| 511 | if (!empty($value['financial_type_id']) && !empty($lineItem[$priceSetId])) { |
| 512 | foreach ($lineItem[$priceSetId] as &$values) { |
| 513 | $values['financial_type_id'] = $value['financial_type_id']; |
| 514 | } |
| 515 | } |
| 516 | $value['line_item'] = $lineItem; |
| 517 | //finally call contribution create for all the magic |
| 518 | $contribution = CRM_Contribute_BAO_Contribution::create($value, CRM_Core_DAO::$_nullArray); |
| 519 | $batchTypes = CRM_Core_Pseudoconstant::get('CRM_Batch_DAO_Batch', 'type_id', array('flip' => 1), 'validate'); |
| 520 | if (!empty($this->_batchInfo['type_id']) && ($this->_batchInfo['type_id'] == $batchTypes['Pledge Payment'])) { |
| 521 | $adjustTotalAmount = FALSE; |
| 522 | if (isset($params['option_type'][$key])) { |
| 523 | if ($params['option_type'][$key] == 2) { |
| 524 | $adjustTotalAmount = TRUE; |
| 525 | } |
| 526 | } |
| 527 | $pledgeId = $params['open_pledges'][$key]; |
| 528 | if (is_numeric($pledgeId)) { |
| 529 | $result = CRM_Pledge_BAO_PledgePayment::getPledgePayments($pledgeId); |
| 530 | $pledgePaymentId = 0; |
| 531 | foreach ($result as $key => $values) { |
| 532 | if ($values['status'] != 'Completed') { |
| 533 | $pledgePaymentId = $values['id']; |
| 534 | break; |
| 535 | } |
| 536 | } |
| 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 | ); |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | //process premiums |
| 549 | if (!empty($value['product_name'])) { |
| 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. |
| 571 | if ($contribution->id && !empty($value['send_receipt'])) { |
| 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; |
| 577 | CRM_Contribute_Form_AdditionalInfo::emailReceipt($this, $value); |
| 578 | } |
| 579 | } |
| 580 | } |
| 581 | return TRUE; |
| 582 | } |
| 583 | |
| 584 | /** |
| 585 | * Process membership records. |
| 586 | * |
| 587 | * @param array $params |
| 588 | * Associated array of submitted values. |
| 589 | * |
| 590 | * |
| 591 | * @return bool |
| 592 | */ |
| 593 | private function processMembership(&$params) { |
| 594 | $dateTypes = array( |
| 595 | 'join_date' => 'joinDate', |
| 596 | 'membership_start_date' => 'startDate', |
| 597 | 'membership_end_date' => 'endDate', |
| 598 | ); |
| 599 | |
| 600 | $dates = array( |
| 601 | 'join_date', |
| 602 | 'start_date', |
| 603 | 'end_date', |
| 604 | 'reminder_date', |
| 605 | ); |
| 606 | |
| 607 | // get the price set associated with offline memebership |
| 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)); |
| 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 |
| 615 | if (empty($params['primary_contact_id'][$key])) { |
| 616 | continue; |
| 617 | } |
| 618 | |
| 619 | $value['contact_id'] = CRM_Utils_Array::value($key, $params['primary_contact_id']); |
| 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 | |
| 647 | if (!empty($value['send_receipt'])) { |
| 648 | $value['receipt_date'] = date('Y-m-d His'); |
| 649 | } |
| 650 | |
| 651 | if (!empty($value['membership_source'])) { |
| 652 | $value['source'] = $value['membership_source']; |
| 653 | } |
| 654 | |
| 655 | unset($value['membership_source']); |
| 656 | |
| 657 | //Get the membership status |
| 658 | if (!empty($value['membership_status'])) { |
| 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], |
| 676 | $key, |
| 677 | 'Membership', |
| 678 | $membershipTypeId |
| 679 | ); |
| 680 | |
| 681 | if (!empty($value['financial_type'])) { |
| 682 | $value['financial_type_id'] = $value['financial_type']; |
| 683 | } |
| 684 | |
| 685 | if (!empty($value['payment_instrument'])) { |
| 686 | $value['payment_instrument_id'] = $value['payment_instrument']; |
| 687 | } |
| 688 | |
| 689 | // handle soft credit |
| 690 | if (!empty($params['soft_credit_contact_id'][$key]) && !empty($params['soft_credit_amount'][$key])) { |
| 691 | $value['soft_credit'][$key]['contact_id'] = $params['soft_credit_contact_id'][$key]; |
| 692 | $value['soft_credit'][$key]['amount'] = CRM_Utils_Rule::cleanMoney($params['soft_credit_amount'][$key]); |
| 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 | } |
| 702 | } |
| 703 | |
| 704 | if (!empty($value['receive_date'])) { |
| 705 | $value['receive_date'] = CRM_Utils_Date::processDate($value['receive_date'], $value['receive_date_time'], TRUE); |
| 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 |
| 717 | |
| 718 | $editedFieldParams = array( |
| 719 | 'price_set_id' => $priceSetId, |
| 720 | 'name' => $value['membership_type'][0], |
| 721 | ); |
| 722 | |
| 723 | $editedResults = array(); |
| 724 | CRM_Price_BAO_PriceField::retrieve($editedFieldParams, $editedResults); |
| 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'], |
| 733 | 'membership_type_id' => $value['membership_type_id'], |
| 734 | ); |
| 735 | |
| 736 | $editedResults = array(); |
| 737 | CRM_Price_BAO_PriceFieldValue::retrieve($editedFieldParams, $editedResults); |
| 738 | $this->_priceSet['fields'][$fid]['options'][$editedResults['id']] = $priceSets['fields'][$fid]['options'][$editedResults['id']]; |
| 739 | if (!empty($value['total_amount'])) { |
| 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 | |
| 746 | $lineItem = array(); |
| 747 | CRM_Price_BAO_PriceSet::processAmount($this->_priceSet['fields'], |
| 748 | $value, $lineItem[$priceSetId] |
| 749 | ); |
| 750 | |
| 751 | //CRM-11529 for backoffice transactions |
| 752 | //when financial_type_id is passed in form, update the |
| 753 | //lineitems with the financial type selected in form |
| 754 | if (!empty($value['financial_type_id']) && !empty($lineItem[$priceSetId])) { |
| 755 | foreach ($lineItem[$priceSetId] as &$values) { |
| 756 | $values['financial_type_id'] = $value['financial_type_id']; |
| 757 | } |
| 758 | } |
| 759 | |
| 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']); |
| 768 | |
| 769 | $value['is_renew'] = FALSE; |
| 770 | if (!empty($params['member_option']) && CRM_Utils_Array::value($key, $params['member_option']) == 2) { |
| 771 | $this->_params = $params; |
| 772 | $value['is_renew'] = TRUE; |
| 773 | $membership = CRM_Member_BAO_Membership::renewMembershipFormWrapper( |
| 774 | $value['contact_id'], |
| 775 | $value['membership_type_id'], |
| 776 | FALSE, $this, NULL, NULL, |
| 777 | $value['custom'] |
| 778 | ); |
| 779 | |
| 780 | // make contribution entry |
| 781 | $contrbutionParams = array_merge($value, array('membership_id' => $membership->id)); |
| 782 | CRM_Member_BAO_Membership::recordMembershipContribution($contrbutionParams); |
| 783 | } |
| 784 | else { |
| 785 | $membership = CRM_Member_BAO_Membership::create($value, CRM_Core_DAO::$_nullArray); |
| 786 | } |
| 787 | |
| 788 | //process premiums |
| 789 | if (!empty($value['product_name'])) { |
| 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], |
| 801 | 'contribution_id' => CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipPayment', $membership->id, 'contribution_id', 'membership_id'), |
| 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. |
| 811 | if ($membership->id && !empty($value['send_receipt'])) { |
| 812 | |
| 813 | // add the domain email id |
| 814 | $domainEmail = CRM_Core_BAO_Domain::getNameAndEmail(); |
| 815 | $domainEmail = "$domainEmail[0] <$domainEmail[1]>"; |
| 816 | |
| 817 | $value['from_email_address'] = $domainEmail; |
| 818 | $value['membership_id'] = $membership->id; |
| 819 | $value['contribution_id'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipPayment', $membership->id, 'contribution_id', 'membership_id'); |
| 820 | CRM_Member_Form_Membership::emailReceipt($this, $value, $membership); |
| 821 | } |
| 822 | } |
| 823 | } |
| 824 | return TRUE; |
| 825 | } |
| 826 | |
| 827 | /** |
| 828 | * Update contact information. |
| 829 | * |
| 830 | * @param array $value |
| 831 | * Associated array of submitted values. |
| 832 | * |
| 833 | * |
| 834 | * @return void |
| 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 | } |
| 846 | |
| 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 |
| 851 | * |
| 852 | * @param array $params |
| 853 | * |
| 854 | * @return bool |
| 855 | */ |
| 856 | public function testProcessMembership($params) { |
| 857 | return $this->processMembership($params); |
| 858 | } |
| 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 | */ |
| 869 | public function testProcessContribution($params) { |
| 870 | return $this->processContribution($params); |
| 871 | } |
| 872 | |
| 873 | } |