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