| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
| 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 usefusul, 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-2014 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * This class generates form components for processing a contribution |
| 38 | * |
| 39 | */ |
| 40 | class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditPayment { |
| 41 | /** |
| 42 | * the id of the contribution that we are processing |
| 43 | * |
| 44 | * @var int |
| 45 | * @public |
| 46 | */ |
| 47 | public $_id; |
| 48 | |
| 49 | /** |
| 50 | * the id of the premium that we are processing |
| 51 | * |
| 52 | * @var int |
| 53 | * @public |
| 54 | */ |
| 55 | public $_premiumID = NULL; |
| 56 | |
| 57 | /** |
| 58 | * @var CRM_Contribute_DAO_ContributionProduct |
| 59 | */ |
| 60 | public $_productDAO = NULL; |
| 61 | |
| 62 | /** |
| 63 | * the id of the note |
| 64 | * |
| 65 | * @var int |
| 66 | * @public |
| 67 | */ |
| 68 | public $_noteID; |
| 69 | |
| 70 | /** |
| 71 | * the id of the contact associated with this contribution |
| 72 | * |
| 73 | * @var int |
| 74 | * @public |
| 75 | */ |
| 76 | public $_contactID; |
| 77 | |
| 78 | /** |
| 79 | * the id of the pledge payment that we are processing |
| 80 | * |
| 81 | * @var int |
| 82 | * @public |
| 83 | */ |
| 84 | public $_ppID; |
| 85 | |
| 86 | /** |
| 87 | * the id of the pledge that we are processing |
| 88 | * |
| 89 | * @var int |
| 90 | * @public |
| 91 | */ |
| 92 | public $_pledgeID; |
| 93 | |
| 94 | /** |
| 95 | * is this contribution associated with an online |
| 96 | * financial transaction |
| 97 | * |
| 98 | * @var boolean |
| 99 | * @public |
| 100 | */ |
| 101 | public $_online = FALSE; |
| 102 | |
| 103 | /** |
| 104 | * Stores all product option |
| 105 | * |
| 106 | * @var array |
| 107 | * @public |
| 108 | */ |
| 109 | public $_options; |
| 110 | |
| 111 | /** |
| 112 | * Storage of parameters from form |
| 113 | * |
| 114 | * @var array |
| 115 | * @public |
| 116 | */ |
| 117 | public $_params; |
| 118 | |
| 119 | /** |
| 120 | * Store the contribution Type ID |
| 121 | * |
| 122 | * @var array |
| 123 | */ |
| 124 | public $_contributionType; |
| 125 | |
| 126 | /** |
| 127 | * The contribution values if an existing contribution |
| 128 | */ |
| 129 | public $_values; |
| 130 | |
| 131 | /** |
| 132 | * The pledge values if this contribution is associated with pledge |
| 133 | */ |
| 134 | public $_pledgeValues; |
| 135 | |
| 136 | public $_contributeMode = 'direct'; |
| 137 | |
| 138 | public $_context; |
| 139 | |
| 140 | /** |
| 141 | * Parameter with confusing name |
| 142 | * @todo what is it? |
| 143 | * @var string |
| 144 | */ |
| 145 | public $_compContext; |
| 146 | |
| 147 | public $_compId; |
| 148 | |
| 149 | /** |
| 150 | * Possible From email addresses |
| 151 | * @var array |
| 152 | */ |
| 153 | public $_fromEmails; |
| 154 | |
| 155 | /** |
| 156 | * ID of from email |
| 157 | * @var integer |
| 158 | */ |
| 159 | public $fromEmailId; |
| 160 | /* |
| 161 | * Store the line items if price set used. |
| 162 | */ |
| 163 | public $_lineItems; |
| 164 | |
| 165 | /** |
| 166 | * line item |
| 167 | * @todo explain why we use lineItem & lineItems |
| 168 | * @var array |
| 169 | */ |
| 170 | public $_lineItem; |
| 171 | |
| 172 | /** |
| 173 | * @var array soft credit info |
| 174 | */ |
| 175 | public $_softCreditInfo; |
| 176 | |
| 177 | protected $_formType; |
| 178 | |
| 179 | /** |
| 180 | * @todo what on earth does cdType stand for???? |
| 181 | * @var |
| 182 | */ |
| 183 | protected $_cdType; |
| 184 | public $_honoreeProfileType; |
| 185 | |
| 186 | /** |
| 187 | * logged in user's email |
| 188 | * @var string |
| 189 | */ |
| 190 | public $userEmail; |
| 191 | |
| 192 | /** |
| 193 | * Price set ID |
| 194 | * @var integer |
| 195 | */ |
| 196 | public $_priceSetId; |
| 197 | |
| 198 | |
| 199 | /** |
| 200 | * Price set as an array |
| 201 | * @var array |
| 202 | */ |
| 203 | public $_priceSet; |
| 204 | |
| 205 | /** |
| 206 | * form defaults |
| 207 | * @todo can we define this a as protected? can we define higher up the chain |
| 208 | * @var array |
| 209 | */ |
| 210 | public $_defaults; |
| 211 | |
| 212 | /** |
| 213 | * User display name |
| 214 | * |
| 215 | * @var string |
| 216 | */ |
| 217 | public $userDisplayName; |
| 218 | /** |
| 219 | * Function to set variables up before form is built |
| 220 | * |
| 221 | * @return void |
| 222 | * @access public |
| 223 | */ |
| 224 | public function preProcess() { |
| 225 | |
| 226 | //check permission for action. |
| 227 | if (!CRM_Core_Permission::checkActionPermission('CiviContribute', $this->_action)) { |
| 228 | CRM_Core_Error::fatal(ts('You do not have permission to access this page')); |
| 229 | } |
| 230 | |
| 231 | $this->_cdType = CRM_Utils_Array::value('type', $_GET); |
| 232 | |
| 233 | $this->assign('cdType', FALSE); |
| 234 | if ($this->_cdType) { |
| 235 | $this->assign('cdType', TRUE); |
| 236 | CRM_Custom_Form_CustomData::preProcess($this); |
| 237 | return; |
| 238 | } |
| 239 | |
| 240 | $this->_formType = CRM_Utils_Array::value('formType', $_GET); |
| 241 | |
| 242 | // get price set id. |
| 243 | $this->_priceSetId = CRM_Utils_Array::value('priceSetId', $_GET); |
| 244 | $this->set('priceSetId', $this->_priceSetId); |
| 245 | $this->assign('priceSetId', $this->_priceSetId); |
| 246 | |
| 247 | //get the pledge payment id |
| 248 | $this->_ppID = CRM_Utils_Request::retrieve('ppid', 'Positive', $this); |
| 249 | |
| 250 | //get the contact id |
| 251 | $this->_contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $this); |
| 252 | |
| 253 | //get the action. |
| 254 | $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'add'); |
| 255 | $this->assign('action', $this->_action); |
| 256 | |
| 257 | //get the contribution id if update |
| 258 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); |
| 259 | if (!empty($this->_id)) { |
| 260 | $this->assign('contribID', $this->_id); |
| 261 | } |
| 262 | |
| 263 | $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this); |
| 264 | $this->assign('context', $this->_context); |
| 265 | |
| 266 | $this->_compId = CRM_Utils_Request::retrieve('compId', 'Positive', $this); |
| 267 | |
| 268 | $this->_compContext = CRM_Utils_Request::retrieve('compContext', 'String', $this); |
| 269 | |
| 270 | //set the contribution mode. |
| 271 | $this->_mode = CRM_Utils_Request::retrieve('mode', 'String', $this); |
| 272 | |
| 273 | $this->assign('contributionMode', $this->_mode); |
| 274 | |
| 275 | $this->_paymentProcessor = array('billing_mode' => 1); |
| 276 | |
| 277 | $this->assign('showCheckNumber', TRUE); |
| 278 | |
| 279 | $this->_fromEmails = CRM_Core_BAO_Email::getFromEmail(); |
| 280 | $this->assignProcessors(); |
| 281 | |
| 282 | if ($this->_contactID) { |
| 283 | list($this->userDisplayName, $this->userEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactID); |
| 284 | $this->assign('displayName', $this->userDisplayName); |
| 285 | } |
| 286 | |
| 287 | // also check for billing information |
| 288 | // get the billing location type |
| 289 | $this->assignBillingType(); |
| 290 | |
| 291 | $this->_fields = array(); |
| 292 | |
| 293 | CRM_Core_Payment_Form::setPaymentFieldsByType(CRM_Utils_Array::value('payment_type', $this->_processors), $this); |
| 294 | |
| 295 | if ($this->_action & CRM_Core_Action::DELETE) { |
| 296 | return; |
| 297 | } |
| 298 | |
| 299 | if (in_array('CiviPledge', CRM_Core_Config::singleton()->enableComponents) && !$this->_formType) { |
| 300 | $this->preProcessPledge(); |
| 301 | } |
| 302 | |
| 303 | if ($this->_id) { |
| 304 | $this->showRecordLinkMesssage($this->_id); |
| 305 | } |
| 306 | $this->_values = array(); |
| 307 | |
| 308 | // current contribution id |
| 309 | if ($this->_id) { |
| 310 | $this->assignPremiumProduct($this->_id); |
| 311 | $this->buildValuesAndAssignOnline_Note_Type($this->_id, $this->_values); |
| 312 | } |
| 313 | |
| 314 | // when custom data is included in this page |
| 315 | if (!empty($_POST['hidden_custom'])) { |
| 316 | $this->applyCustomData('Contribution', CRM_Utils_Array::value('financial_type_id', $_POST), $this->_id); |
| 317 | } |
| 318 | |
| 319 | $this->_lineItems = array(); |
| 320 | if ($this->_id) { |
| 321 | if (!empty($this->_compId) && $this->_compContext == 'participant') { |
| 322 | $this->assign('compId', $this->_compId); |
| 323 | $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->_compId); |
| 324 | } |
| 325 | else { |
| 326 | $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->_id, 'contribution', 1); |
| 327 | } |
| 328 | empty($lineItem) ? NULL : $this->_lineItems[] = $lineItem; |
| 329 | } |
| 330 | |
| 331 | $this->assign('lineItem', empty($this->_lineItems) ? FALSE : $this->_lineItems); |
| 332 | |
| 333 | // Set title |
| 334 | if ($this->_contactID) { |
| 335 | $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactID); |
| 336 | |
| 337 | // Check if this is default domain contact CRM-10482 |
| 338 | if (CRM_Contact_BAO_Contact::checkDomainContact($this->_contactID)) { |
| 339 | $displayName .= ' (' . ts('default organization') . ')'; |
| 340 | } |
| 341 | |
| 342 | // omitting contactImage from title for now since the summary overlay css doesn't work outside of our crm-container |
| 343 | CRM_Utils_System::setTitle(ts('Contribution from') . ' ' . $displayName); |
| 344 | } |
| 345 | |
| 346 | if ($this->_id) { |
| 347 | CRM_Contribute_Form_SoftCredit::preprocess($this); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | function setDefaultValues() { |
| 352 | if ($this->_cdType) { |
| 353 | return CRM_Custom_Form_CustomData::setDefaultValues($this); |
| 354 | } |
| 355 | |
| 356 | $defaults = $this->_values; |
| 357 | |
| 358 | //set defaults for pledge payment. |
| 359 | if ($this->_ppID) { |
| 360 | $defaults['total_amount'] = CRM_Utils_Array::value('scheduled_amount', $this->_pledgeValues['pledgePayment']); |
| 361 | $defaults['financial_type_id'] = CRM_Utils_Array::value('financial_type_id', $this->_pledgeValues); |
| 362 | $defaults['currency'] = CRM_Utils_Array::value('currency', $this->_pledgeValues); |
| 363 | $defaults['option_type'] = 1; |
| 364 | } |
| 365 | |
| 366 | if ($this->_action & CRM_Core_Action::DELETE) { |
| 367 | return $defaults; |
| 368 | } |
| 369 | |
| 370 | // set soft credit defaults |
| 371 | CRM_Contribute_Form_SoftCredit::setDefaultValues($defaults, $this); |
| 372 | |
| 373 | if ($this->_mode) { |
| 374 | $config = CRM_Core_Config::singleton(); |
| 375 | // set default country from config if no country set |
| 376 | if (empty($defaults["billing_country_id-{$this->_bltID}"])) { |
| 377 | $defaults["billing_country_id-{$this->_bltID}"] = $config->defaultContactCountry; |
| 378 | } |
| 379 | |
| 380 | if (empty($defaults["billing_state_province_id-{$this->_bltID}"])) { |
| 381 | $defaults["billing_state_province_id-{$this->_bltID}"] = $config->defaultContactStateProvince; |
| 382 | } |
| 383 | |
| 384 | $billingDefaults = $this->getProfileDefaults('Billing', $this->_contactID); |
| 385 | $defaults = array_merge($defaults, $billingDefaults); |
| 386 | |
| 387 | // now fix all state country selectors, set correct state based on country |
| 388 | CRM_Core_BAO_Address::fixAllStateSelects($this, $defaults); |
| 389 | } |
| 390 | |
| 391 | if ($this->_id) { |
| 392 | $this->_contactID = $defaults['contact_id']; |
| 393 | } |
| 394 | |
| 395 | // Set $newCredit variable in template to control whether link to credit card mode is included |
| 396 | $this->assign('newCredit', CRM_Core_Config::isEnabledBackOfficeCreditCardPayments()); |
| 397 | |
| 398 | // fix the display of the monetary value, CRM-4038 |
| 399 | if (isset($defaults['total_amount'])) { |
| 400 | $defaults['total_amount'] = CRM_Utils_Money::format($defaults['total_amount'], NULL, '%a'); |
| 401 | } |
| 402 | |
| 403 | if (isset($defaults['non_deductible_amount'])) { |
| 404 | $defaults['non_deductible_amount'] = CRM_Utils_Money::format($defaults['non_deductible_amount'], NULL, '%a'); |
| 405 | } |
| 406 | |
| 407 | if (isset($defaults['fee_amount'])) { |
| 408 | $defaults['fee_amount'] = CRM_Utils_Money::format($defaults['fee_amount'], NULL, '%a'); |
| 409 | } |
| 410 | |
| 411 | if (isset($defaults['net_amount'])) { |
| 412 | $defaults['net_amount'] = CRM_Utils_Money::format($defaults['net_amount'], NULL, '%a'); |
| 413 | } |
| 414 | |
| 415 | if ($this->_contributionType) { |
| 416 | $defaults['financial_type_id'] = $this->_contributionType; |
| 417 | } |
| 418 | |
| 419 | if (empty($defaults['payment_instrument_id'])) { |
| 420 | $defaults['payment_instrument_id'] = key(CRM_Core_OptionGroup::values('payment_instrument', FALSE, FALSE, FALSE, 'AND is_default = 1')); |
| 421 | } |
| 422 | |
| 423 | if (!empty($defaults['is_test'])) { |
| 424 | $this->assign('is_test', TRUE); |
| 425 | } |
| 426 | |
| 427 | $this->assign('showOption', TRUE); |
| 428 | // for Premium section |
| 429 | if ($this->_premiumID) { |
| 430 | $this->assign('showOption', FALSE); |
| 431 | $options = isset($this->_options[$this->_productDAO->product_id]) ? $this->_options[$this->_productDAO->product_id] : ""; |
| 432 | if (!$options) { |
| 433 | $this->assign('showOption', TRUE); |
| 434 | } |
| 435 | $options_key = CRM_Utils_Array::key($this->_productDAO->product_option, $options); |
| 436 | if ($options_key) { |
| 437 | $defaults['product_name'] = array($this->_productDAO->product_id, trim($options_key)); |
| 438 | } |
| 439 | else { |
| 440 | $defaults['product_name'] = array($this->_productDAO->product_id); |
| 441 | } |
| 442 | if ($this->_productDAO->fulfilled_date) { |
| 443 | list($defaults['fulfilled_date']) = CRM_Utils_Date::setDateDefaults($this->_productDAO->fulfilled_date); |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | if (isset($this->userEmail)) { |
| 448 | $this->assign('email', $this->userEmail); |
| 449 | } |
| 450 | |
| 451 | if (!empty($defaults['is_pay_later'])) { |
| 452 | $this->assign('is_pay_later', TRUE); |
| 453 | } |
| 454 | $this->assign('contribution_status_id', CRM_Utils_Array::value('contribution_status_id', $defaults)); |
| 455 | |
| 456 | $dates = array('receive_date', 'receipt_date', 'cancel_date', 'thankyou_date'); |
| 457 | foreach ($dates as $key) { |
| 458 | if (!empty($defaults[$key])) { |
| 459 | list($defaults[$key], |
| 460 | $defaults[$key . '_time'] |
| 461 | ) = CRM_Utils_Date::setDateDefaults(CRM_Utils_Array::value($key, $defaults), |
| 462 | 'activityDateTime' |
| 463 | ); |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | if (!$this->_id && empty($defaults['receive_date'])) { |
| 468 | list($defaults['receive_date'], |
| 469 | $defaults['receive_date_time'] |
| 470 | ) = CRM_Utils_Date::setDateDefaults(NULL, 'activityDateTime'); |
| 471 | } |
| 472 | |
| 473 | $this->assign('receive_date', CRM_Utils_Date::processDate(CRM_Utils_Array::value('receive_date', $defaults), |
| 474 | CRM_Utils_Array::value('receive_date_time', $defaults) |
| 475 | )); |
| 476 | $currency = CRM_Utils_Array::value('currency', $defaults); |
| 477 | $this->assign('currency', $currency); |
| 478 | // Hack to get currency info to the js layer. CRM-11440 |
| 479 | CRM_Utils_Money::format(1); |
| 480 | $this->assign('currencySymbol', CRM_Utils_Array::value($currency, CRM_Utils_Money::$_currencySymbols)); |
| 481 | $this->assign('totalAmount', CRM_Utils_Array::value('total_amount', $defaults)); |
| 482 | |
| 483 | //inherit campaign from pledge. |
| 484 | if ($this->_ppID && !empty($this->_pledgeValues['campaign_id'])) { |
| 485 | $defaults['campaign_id'] = $this->_pledgeValues['campaign_id']; |
| 486 | } |
| 487 | |
| 488 | $this->_defaults = $defaults; |
| 489 | return $defaults; |
| 490 | } |
| 491 | |
| 492 | /** |
| 493 | * Function to build the form |
| 494 | * |
| 495 | * @return void |
| 496 | * @access public |
| 497 | */ |
| 498 | public function buildQuickForm() { |
| 499 | if ($this->_cdType) { |
| 500 | CRM_Custom_Form_CustomData::buildQuickForm($this); |
| 501 | return; |
| 502 | } |
| 503 | $allPanes = array(); |
| 504 | |
| 505 | // build price set form. |
| 506 | $buildPriceSet = FALSE; |
| 507 | if (empty($this->_lineItems) && |
| 508 | ($this->_priceSetId || !empty($_POST['price_set_id'])) |
| 509 | ) { |
| 510 | $buildPriceSet = TRUE; |
| 511 | $getOnlyPriceSetElements = TRUE; |
| 512 | if (!$this->_priceSetId) { |
| 513 | $this->_priceSetId = $_POST['price_set_id']; |
| 514 | $getOnlyPriceSetElements = FALSE; |
| 515 | } |
| 516 | |
| 517 | $this->set('priceSetId', $this->_priceSetId); |
| 518 | CRM_Price_BAO_PriceSet::buildPriceSet($this); |
| 519 | |
| 520 | // get only price set form elements. |
| 521 | if ($getOnlyPriceSetElements) { |
| 522 | return; |
| 523 | } |
| 524 | } |
| 525 | // use to build form during form rule. |
| 526 | $this->assign('buildPriceSet', $buildPriceSet); |
| 527 | |
| 528 | $showAdditionalInfo = FALSE; |
| 529 | |
| 530 | $defaults = $this->_values; |
| 531 | $additionalDetailFields = array( |
| 532 | 'note', |
| 533 | 'thankyou_date', |
| 534 | 'invoice_id', |
| 535 | 'non_deductible_amount', |
| 536 | 'fee_amount', |
| 537 | 'net_amount', |
| 538 | ); |
| 539 | foreach ($additionalDetailFields as $key) { |
| 540 | if (!empty($defaults[$key])) { |
| 541 | $defaults['hidden_AdditionalDetail'] = 1; |
| 542 | break; |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | if ($this->_productDAO) { |
| 547 | if ($this->_productDAO->product_id) { |
| 548 | $defaults['hidden_Premium'] = 1; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | if ($this->_noteID && |
| 553 | isset($this->_values['note']) |
| 554 | ) { |
| 555 | $defaults['hidden_AdditionalDetail'] = 1; |
| 556 | } |
| 557 | |
| 558 | $paneNames = array( |
| 559 | ts('Additional Details') => 'AdditionalDetail', |
| 560 | ); |
| 561 | |
| 562 | //Add Premium pane only if Premium is exists. |
| 563 | $dao = new CRM_Contribute_DAO_Product(); |
| 564 | $dao->is_active = 1; |
| 565 | |
| 566 | if ($dao->find(TRUE)) { |
| 567 | $paneNames[ts('Premium Information')] = 'Premium'; |
| 568 | } |
| 569 | |
| 570 | $ccPane = NULL; |
| 571 | if ($this->_mode) { |
| 572 | if (CRM_Utils_Array::value('payment_type', $this->_processors) & CRM_Core_Payment::PAYMENT_TYPE_DIRECT_DEBIT |
| 573 | ) { |
| 574 | $ccPane = array(ts('Direct Debit Information') => 'DirectDebit'); |
| 575 | } |
| 576 | else { |
| 577 | $ccPane = array(ts('Credit Card Information') => 'CreditCard'); |
| 578 | } |
| 579 | } |
| 580 | if (is_array($ccPane)) { |
| 581 | $paneNames = array_merge($ccPane, $paneNames); |
| 582 | } |
| 583 | |
| 584 | $buildRecurBlock = FALSE; |
| 585 | foreach ($paneNames as $name => $type) { |
| 586 | $urlParams = "snippet=4&formType={$type}"; |
| 587 | if ($this->_mode) { |
| 588 | $urlParams .= "&mode={$this->_mode}"; |
| 589 | } |
| 590 | |
| 591 | $open = 'false'; |
| 592 | if ($type == 'CreditCard' || |
| 593 | $type == 'DirectDebit' |
| 594 | ) { |
| 595 | $open = 'true'; |
| 596 | } |
| 597 | |
| 598 | $allPanes[$name] = array( |
| 599 | 'url' => CRM_Utils_System::url('civicrm/contact/view/contribution', $urlParams), |
| 600 | 'open' => $open, |
| 601 | 'id' => $type |
| 602 | ); |
| 603 | |
| 604 | // see if we need to include this paneName in the current form |
| 605 | if ($this->_formType == $type || !empty($_POST["hidden_{$type}"]) || |
| 606 | CRM_Utils_Array::value("hidden_{$type}", $defaults) |
| 607 | ) { |
| 608 | $showAdditionalInfo = TRUE; |
| 609 | $allPanes[$name]['open'] = 'true'; |
| 610 | } |
| 611 | |
| 612 | if ($type == 'CreditCard') { |
| 613 | $buildRecurBlock = TRUE; |
| 614 | $this->add('hidden', 'hidden_CreditCard', 1); |
| 615 | CRM_Core_Payment_Form::buildCreditCard($this, TRUE); |
| 616 | } |
| 617 | elseif ($type == 'DirectDebit') { |
| 618 | $buildRecurBlock = TRUE; |
| 619 | $this->add('hidden', 'hidden_DirectDebit', 1); |
| 620 | CRM_Core_Payment_Form::buildDirectDebit($this, TRUE); |
| 621 | } |
| 622 | else { |
| 623 | $additionalInfoFormFunction = 'build' . $type; |
| 624 | CRM_Contribute_Form_AdditionalInfo::$additionalInfoFormFunction($this); |
| 625 | } |
| 626 | } |
| 627 | if (empty($this->_recurPaymentProcessors)) { |
| 628 | $buildRecurBlock = FALSE; |
| 629 | } |
| 630 | if ($buildRecurBlock) { |
| 631 | CRM_Contribute_Form_Contribution_Main::buildRecur($this); |
| 632 | $this->setDefaults(array('is_recur' => 0)); |
| 633 | } |
| 634 | $this->assign('buildRecurBlock', $buildRecurBlock); |
| 635 | $qfKey = $this->controller->_key; |
| 636 | $this->assign('qfKey', $qfKey); |
| 637 | $this->assign('allPanes', $allPanes); |
| 638 | $this->assign('showAdditionalInfo', $showAdditionalInfo); |
| 639 | |
| 640 | if ($this->_formType) { |
| 641 | $this->assign('formType', $this->_formType); |
| 642 | return; |
| 643 | } |
| 644 | |
| 645 | $this->applyFilter('__ALL__', 'trim'); |
| 646 | |
| 647 | if ($this->_action & CRM_Core_Action::DELETE) { |
| 648 | $this->addButtons(array( |
| 649 | array( |
| 650 | 'type' => 'next', |
| 651 | 'name' => ts('Delete'), |
| 652 | 'spacing' => ' ', |
| 653 | 'isDefault' => TRUE, |
| 654 | ), |
| 655 | array( |
| 656 | 'type' => 'cancel', |
| 657 | 'name' => ts('Cancel') |
| 658 | ) |
| 659 | ) |
| 660 | ); |
| 661 | return; |
| 662 | } |
| 663 | |
| 664 | //need to assign custom data type and subtype to the template |
| 665 | $this->assign('customDataType', 'Contribution'); |
| 666 | $this->assign('customDataSubType', $this->_contributionType); |
| 667 | $this->assign('entityID', $this->_id); |
| 668 | |
| 669 | if ($this->_context == 'standalone') { |
| 670 | $this->addEntityRef('contact_id', ts('Contact'), array('create' => TRUE), TRUE); |
| 671 | } |
| 672 | |
| 673 | $attributes = CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Contribution'); |
| 674 | |
| 675 | $financialType = $this->add('select', 'financial_type_id', |
| 676 | ts('Financial Type'), |
| 677 | array('' => ts('- select -')) + CRM_Contribute_PseudoConstant::financialType(), |
| 678 | TRUE, |
| 679 | array('onChange' => "CRM.buildCustomData( 'Contribution', this.value );") |
| 680 | ); |
| 681 | $paymentInstrument = FALSE; |
| 682 | if (!$this->_mode) { |
| 683 | $paymentInstrument = $this->add('select', 'payment_instrument_id', |
| 684 | ts('Paid By'), |
| 685 | array('' => ts('- select -')) + CRM_Contribute_PseudoConstant::paymentInstrument(), |
| 686 | TRUE, array('onChange' => "return showHideByValue('payment_instrument_id','4','checkNumber','table-row','select',false);") |
| 687 | ); |
| 688 | } |
| 689 | |
| 690 | $trxnId = $this->add('text', 'trxn_id', ts('Transaction ID'), $attributes['trxn_id']); |
| 691 | |
| 692 | //add receipt for offline contribution |
| 693 | $this->addElement('checkbox', 'is_email_receipt', ts('Send Receipt?')); |
| 694 | |
| 695 | $this->add('select', 'from_email_address', ts('Receipt From'), $this->_fromEmails); |
| 696 | |
| 697 | $status = CRM_Contribute_PseudoConstant::contributionStatus(); |
| 698 | |
| 699 | // suppressing contribution statuses that are NOT relevant to pledges (CRM-5169) |
| 700 | $statusName = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); |
| 701 | if ($this->_ppID) { |
| 702 | foreach (array( |
| 703 | 'Cancelled', |
| 704 | 'Failed', |
| 705 | 'In Progress' |
| 706 | ) as $suppress) { |
| 707 | unset($status[CRM_Utils_Array::key($suppress, $statusName)]); |
| 708 | } |
| 709 | } |
| 710 | elseif ((!$this->_ppID && $this->_id) || !$this->_id) { |
| 711 | $suppressFlag = FALSE; |
| 712 | if ($this->_id) { |
| 713 | $componentDetails = CRM_Contribute_BAO_Contribution::getComponentDetails($this->_id); |
| 714 | if (CRM_Utils_Array::value('membership', $componentDetails) || CRM_Utils_Array::value('participant', $componentDetails)) { |
| 715 | $suppressFlag = TRUE; |
| 716 | } |
| 717 | } |
| 718 | if (!$suppressFlag) { |
| 719 | foreach (array( |
| 720 | 'Overdue', |
| 721 | 'In Progress' |
| 722 | ) as $suppress) { |
| 723 | unset($status[CRM_Utils_Array::key($suppress, $statusName)]); |
| 724 | } |
| 725 | } |
| 726 | else { |
| 727 | unset($status[CRM_Utils_Array::key('Overdue', $statusName)]); |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | if ($this->_id) { |
| 732 | $contributionStatus = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $this->_id, 'contribution_status_id'); |
| 733 | $name = CRM_Utils_Array::value($contributionStatus, $statusName); |
| 734 | switch ($name) { |
| 735 | case 'Completed': |
| 736 | case 'Cancelled': |
| 737 | case 'Refunded': |
| 738 | unset($status[CRM_Utils_Array::key('In Progress', $statusName)]); |
| 739 | unset($status[CRM_Utils_Array::key('Pending', $statusName)]); |
| 740 | unset($status[CRM_Utils_Array::key('Failed', $statusName)]); |
| 741 | break; |
| 742 | case 'Pending': |
| 743 | case 'In Progress': |
| 744 | unset($status[CRM_Utils_Array::key('Refunded', $statusName)]); |
| 745 | break; |
| 746 | case 'Failed': |
| 747 | foreach (array( |
| 748 | 'Pending', |
| 749 | 'Refunded', |
| 750 | 'Completed', |
| 751 | 'In Progress', |
| 752 | 'Cancelled' |
| 753 | ) as $suppress) { |
| 754 | unset($status[CRM_Utils_Array::key($suppress, $statusName)]); |
| 755 | } |
| 756 | break; |
| 757 | } |
| 758 | } |
| 759 | else { |
| 760 | unset($status[CRM_Utils_Array::key('Refunded', $statusName)]); |
| 761 | } |
| 762 | |
| 763 | $this->add('select', 'contribution_status_id', |
| 764 | ts('Contribution Status'), |
| 765 | $status, |
| 766 | FALSE |
| 767 | ); |
| 768 | |
| 769 | // add various dates |
| 770 | $this->addDateTime('receive_date', ts('Received'), FALSE, array('formatType' => 'activityDateTime')); |
| 771 | |
| 772 | if ($this->_online) { |
| 773 | $this->assign('hideCalender', TRUE); |
| 774 | } |
| 775 | $checkNumber = $this->add('text', 'check_number', ts('Check Number'), $attributes['check_number']); |
| 776 | |
| 777 | $this->addDateTime('receipt_date', ts('Receipt Date'), FALSE, array('formatType' => 'activityDateTime')); |
| 778 | $this->addDateTime('cancel_date', ts('Cancelled / Refunded Date'), FALSE, array('formatType' => 'activityDateTime')); |
| 779 | |
| 780 | $this->add('textarea', 'cancel_reason', ts('Cancellation / Refund Reason'), $attributes['cancel_reason']); |
| 781 | |
| 782 | $recurJs = NULL; |
| 783 | if ($buildRecurBlock) { |
| 784 | $recurJs = array('onChange' => "buildRecurBlock( this.value ); return false;"); |
| 785 | } |
| 786 | $element = $this->add('select', |
| 787 | 'payment_processor_id', |
| 788 | ts('Payment Processor'), |
| 789 | $this->_processors, |
| 790 | NULL, |
| 791 | $recurJs |
| 792 | ); |
| 793 | |
| 794 | if ($this->_online) { |
| 795 | $element->freeze(); |
| 796 | } |
| 797 | $totalAmount = NULL; |
| 798 | if (empty($this->_lineItems)) { |
| 799 | $buildPriceSet = FALSE; |
| 800 | $priceSets = CRM_Price_BAO_PriceSet::getAssoc(FALSE, 'CiviContribute'); |
| 801 | if (!empty($priceSets) && !$this->_ppID) { |
| 802 | $buildPriceSet = TRUE; |
| 803 | } |
| 804 | |
| 805 | // don't allow price set for contribution if it is related to participant, or if it is a pledge payment |
| 806 | // and if we already have line items for that participant. CRM-5095 |
| 807 | if ($buildPriceSet && $this->_id) { |
| 808 | $componentDetails = CRM_Contribute_BAO_Contribution::getComponentDetails($this->_id); |
| 809 | $pledgePaymentId = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment', |
| 810 | $this->_id, |
| 811 | 'id', |
| 812 | 'contribution_id' |
| 813 | ); |
| 814 | if ($pledgePaymentId) { |
| 815 | $buildPriceSet = FALSE; |
| 816 | } |
| 817 | if ($participantID = CRM_Utils_Array::value('participant', $componentDetails)) { |
| 818 | $participantLI = CRM_Price_BAO_LineItem::getLineItems($participantID); |
| 819 | if (!CRM_Utils_System::isNull($participantLI)) { |
| 820 | $buildPriceSet = FALSE; |
| 821 | } |
| 822 | } |
| 823 | } |
| 824 | |
| 825 | $hasPriceSets = FALSE; |
| 826 | if ($buildPriceSet) { |
| 827 | $hasPriceSets = TRUE; |
| 828 | $element = $this->add('select', 'price_set_id', ts('Choose price set'), |
| 829 | array( |
| 830 | '' => ts('Choose price set') |
| 831 | ) + $priceSets, |
| 832 | NULL, array('onchange' => "buildAmount( this.value );") |
| 833 | ); |
| 834 | if ($this->_online && !($this->_action & CRM_Core_Action::UPDATE)) { |
| 835 | $element->freeze(); |
| 836 | } |
| 837 | } |
| 838 | $this->assign('hasPriceSets', $hasPriceSets); |
| 839 | $currencyFreeze = FALSE; |
| 840 | if (!($this->_action & CRM_Core_Action::UPDATE)) { |
| 841 | if ($this->_online || $this->_ppID) { |
| 842 | $attributes['total_amount'] = array_merge($attributes['total_amount'], array( |
| 843 | 'READONLY' => TRUE, |
| 844 | 'style' => "background-color:#EBECE4" |
| 845 | )); |
| 846 | $optionTypes = array( |
| 847 | '1' => ts('Adjust Pledge Payment Schedule?'), |
| 848 | '2' => ts('Adjust Total Pledge Amount?'), |
| 849 | ); |
| 850 | $this->addRadio('option_type', |
| 851 | NULL, |
| 852 | $optionTypes, |
| 853 | array(), '<br/>' |
| 854 | ); |
| 855 | |
| 856 | $currencyFreeze = TRUE; |
| 857 | } |
| 858 | } |
| 859 | |
| 860 | $totalAmount = $this->addMoney('total_amount', |
| 861 | ts('Total Amount'), |
| 862 | ($hasPriceSets) ? FALSE : TRUE, |
| 863 | $attributes['total_amount'], |
| 864 | TRUE, 'currency', NULL, $currencyFreeze |
| 865 | ); |
| 866 | } |
| 867 | |
| 868 | $this->add('text', 'source', ts('Source'), CRM_Utils_Array::value('source', $attributes)); |
| 869 | |
| 870 | //CRM-7362 --add campaigns. |
| 871 | CRM_Campaign_BAO_Campaign::addCampaign($this, CRM_Utils_Array::value('campaign_id', $this->_values)); |
| 872 | |
| 873 | CRM_Contribute_Form_SoftCredit::buildQuickForm($this); |
| 874 | |
| 875 | $js = NULL; |
| 876 | if (!$this->_mode) { |
| 877 | $js = array('onclick' => "return verify( );"); |
| 878 | } |
| 879 | |
| 880 | $mailingInfo = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME, |
| 881 | 'mailing_backend' |
| 882 | ); |
| 883 | $this->assign('outBound_option', $mailingInfo['outBound_option']); |
| 884 | |
| 885 | $this->addButtons(array( |
| 886 | array( |
| 887 | 'type' => 'upload', |
| 888 | 'name' => ts('Save'), |
| 889 | 'js' => $js, |
| 890 | 'isDefault' => TRUE |
| 891 | ), |
| 892 | array( |
| 893 | 'type' => 'upload', |
| 894 | 'name' => ts('Save and New'), |
| 895 | 'js' => $js, |
| 896 | 'subName' => 'new' |
| 897 | ), |
| 898 | array( |
| 899 | 'type' => 'cancel', |
| 900 | 'name' => ts('Cancel') |
| 901 | ), |
| 902 | ) |
| 903 | ); |
| 904 | |
| 905 | // if status is Cancelled freeze Amount, Payment Instrument, Check #, Financial Type, |
| 906 | // Net and Fee Amounts are frozen in AdditionalInfo::buildAdditionalDetail |
| 907 | if ($this->_id && $this->_values['contribution_status_id'] == array_search('Cancelled', $statusName)) { |
| 908 | if ($totalAmount) { |
| 909 | $totalAmount->freeze(); |
| 910 | } |
| 911 | $checkNumber->freeze(); |
| 912 | $paymentInstrument->freeze(); |
| 913 | $trxnId->freeze(); |
| 914 | $financialType->freeze(); |
| 915 | } |
| 916 | |
| 917 | $this->addFormRule(array('CRM_Contribute_Form_Contribution', 'formRule'), $this); |
| 918 | |
| 919 | if ($this->_action & CRM_Core_Action::VIEW) { |
| 920 | $this->freeze(); |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | /** |
| 925 | * global form rule |
| 926 | * |
| 927 | * @param array $fields the input form values |
| 928 | * @param array $files the uploaded files if any |
| 929 | * @param $self |
| 930 | * |
| 931 | * @internal param array $options additional user data |
| 932 | * |
| 933 | * @return true if no errors, else array of errors |
| 934 | * @access public |
| 935 | * @static |
| 936 | */ |
| 937 | static function formRule($fields, $files, $self) { |
| 938 | $errors = array(); |
| 939 | |
| 940 | //check for Credit Card Contribution. |
| 941 | if ($self->_mode) { |
| 942 | if (empty($fields['payment_processor_id'])) { |
| 943 | $errors['payment_processor_id'] = ts('Payment Processor is a required field.'); |
| 944 | } |
| 945 | } |
| 946 | |
| 947 | // do the amount validations. |
| 948 | if (empty($fields['total_amount']) && empty($self->_lineItems)) { |
| 949 | if ($priceSetId = CRM_Utils_Array::value('price_set_id', $fields)) { |
| 950 | CRM_Price_BAO_PriceField::priceSetValidation($priceSetId, $fields, $errors); |
| 951 | } |
| 952 | } |
| 953 | |
| 954 | $softErrors = CRM_Contribute_Form_SoftCredit::formRule($fields, $errors, $self); |
| 955 | |
| 956 | // If we have a net amount or fee amount that is NOT set to 0.00, then ensure |
| 957 | // that the net_amount + the fee_amount is equal to the total amount. |
| 958 | $total_amount = NULL; |
| 959 | if(!empty($fields['total_amount']) && $fields['total_amount'] != '0.00') { |
| 960 | $total_amount = $fields['total_amount']; |
| 961 | } |
| 962 | $fee_amount = NULL; |
| 963 | if(!empty($fields['fee_amount']) && $fields['fee_amount'] != '0.00') { |
| 964 | $fee_amount = $fields['fee_amount']; |
| 965 | } |
| 966 | $net_amount = NULL; |
| 967 | if(!empty($fields['net_amount']) && $fields['net_amount'] != '0.00') { |
| 968 | $net_amount = $fields['net_amount']; |
| 969 | } |
| 970 | if ($total_amount && ($net_amount || $fee_amount)) { |
| 971 | $sum = CRM_Utils_Rule::cleanMoney($net_amount) + CRM_Utils_Rule::cleanMoney($fee_amount); |
| 972 | if (CRM_Utils_Rule::cleanMoney($total_amount) != $sum) { |
| 973 | $errors['total_amount'] = ts('The sum of fee amount and net amount must be equal to total amount'); |
| 974 | } |
| 975 | } |
| 976 | //form rule for status http://wiki.civicrm.org/confluence/display/CRM/CiviAccounts+4.3+Data+Flow |
| 977 | if ($self->_id && $self->_values['contribution_status_id'] != $fields['contribution_status_id']) { |
| 978 | CRM_Contribute_BAO_Contribution::checkStatusValidation($self->_values, $fields, $errors); |
| 979 | } |
| 980 | |
| 981 | //FIXME FOR NEW DATA FLOW http://wiki.civicrm.org/confluence/display/CRM/CiviAccounts+4.3+Data+Flow |
| 982 | if (!empty($fields['fee_amount']) && $financialType = CRM_Contribute_BAO_Contribution::validateFinancialType($fields['financial_type_id'])) { |
| 983 | $errors['financial_type_id'] = ts("Financial Account of account relationship of 'Expense Account is' is not configured for Financial Type : ") . $financialType; |
| 984 | } |
| 985 | |
| 986 | // $trxn_id must be unique CRM-13919 |
| 987 | if (!empty($fields['trxn_id'])) { |
| 988 | $queryParams = array(1 => array($fields['trxn_id'], 'String')); |
| 989 | $query = 'select count(*) from civicrm_contribution where trxn_id = %1'; |
| 990 | if ($self->_id) { |
| 991 | $queryParams[2] = array((int)$self->_id, 'Integer'); |
| 992 | $query .= ' and id !=%2'; |
| 993 | } |
| 994 | $tCnt = CRM_Core_DAO::singleValueQuery($query, $queryParams); |
| 995 | if ($tCnt) { |
| 996 | $errors['trxn_id'] = ts('Transaction ID\'s must be unique. Transaction \'%1\' already exists in your database.', array(1 => $fields['trxn_id'])); |
| 997 | } |
| 998 | } |
| 999 | |
| 1000 | $errors = array_merge($errors, $softErrors); |
| 1001 | return $errors; |
| 1002 | } |
| 1003 | |
| 1004 | /** |
| 1005 | * Function to process the form |
| 1006 | * |
| 1007 | * @access public |
| 1008 | * |
| 1009 | * @return void |
| 1010 | */ |
| 1011 | public function postProcess() { |
| 1012 | $session = CRM_Core_Session::singleton(); |
| 1013 | $sendReceipt = $pId = $contribution = FALSE; |
| 1014 | $softParams = $softIDs =array(); |
| 1015 | |
| 1016 | if ($this->_action & CRM_Core_Action::DELETE) { |
| 1017 | CRM_Contribute_BAO_Contribution::deleteContribution($this->_id); |
| 1018 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view', |
| 1019 | "reset=1&cid={$this->_contactID}&selectedChild=contribute" |
| 1020 | )); |
| 1021 | return; |
| 1022 | } |
| 1023 | |
| 1024 | // get the submitted form values. |
| 1025 | $submittedValues = $this->controller->exportValues($this->_name); |
| 1026 | if (!empty($submittedValues['price_set_id']) && $this->_action & CRM_Core_Action::UPDATE) { |
| 1027 | $line = CRM_Price_BAO_LineItem::getLineItems($this->_id, 'contribution'); |
| 1028 | $lineID = key($line); |
| 1029 | $priceSetId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', CRM_Utils_Array::value('price_field_id', $line[$lineID]), 'price_set_id'); |
| 1030 | $quickConfig = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $priceSetId, 'is_quick_config'); |
| 1031 | if ($quickConfig) { |
| 1032 | CRM_Price_BAO_LineItem::deleteLineItems($this->_id, 'civicrm_contribution'); |
| 1033 | } |
| 1034 | } |
| 1035 | |
| 1036 | // process price set and get total amount and line items. |
| 1037 | $lineItem = array(); |
| 1038 | $priceSetId = CRM_Utils_Array::value('price_set_id', $submittedValues); |
| 1039 | if (empty($priceSetId) && !$this->_id) { |
| 1040 | $this->_priceSetId = $priceSetId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', 'default_contribution_amount', 'id', 'name'); |
| 1041 | $this->_priceSet = current(CRM_Price_BAO_PriceSet::getSetDetail($priceSetId)); |
| 1042 | $fieldID = key($this->_priceSet['fields']); |
| 1043 | $fieldValueId = key($this->_priceSet['fields'][$fieldID]['options']); |
| 1044 | $this->_priceSet['fields'][$fieldID]['options'][$fieldValueId]['amount'] = $submittedValues['total_amount']; |
| 1045 | $submittedValues['price_' . $fieldID] = 1; |
| 1046 | } |
| 1047 | |
| 1048 | if ($priceSetId) { |
| 1049 | CRM_Price_BAO_PriceSet::processAmount($this->_priceSet['fields'], |
| 1050 | $submittedValues, $lineItem[$priceSetId]); |
| 1051 | |
| 1052 | $submittedValues['total_amount'] = CRM_Utils_Array::value('amount', $submittedValues); |
| 1053 | } |
| 1054 | if ($this->_id) { |
| 1055 | //CRM-10964 |
| 1056 | $pId = ($this->_compId && $this->_context == 'participant') ? $this->_compId : CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', $this->_id, 'participant_id', 'contribution_id'); |
| 1057 | } |
| 1058 | if (!$priceSetId && !empty($submittedValues['total_amount']) && $this->_id) { |
| 1059 | // 10117 update th line items for participants |
| 1060 | if ($pId) { |
| 1061 | $entityTable = 'participant'; |
| 1062 | $entityID = $pId; |
| 1063 | $participantParams = array( |
| 1064 | 'fee_amount' => $submittedValues['total_amount'], |
| 1065 | 'id' => $entityID |
| 1066 | ); |
| 1067 | CRM_Event_BAO_Participant::add($participantParams); |
| 1068 | if (empty($this->_lineItems)) { |
| 1069 | $this->_lineItems[] = CRM_Price_BAO_LineItem::getLineItems($entityID, 'participant', 1); |
| 1070 | } |
| 1071 | } |
| 1072 | else { |
| 1073 | $entityTable = 'contribution'; |
| 1074 | $entityID = $this->_id; |
| 1075 | } |
| 1076 | |
| 1077 | $lineItems = CRM_Price_BAO_LineItem::getLineItems($entityID, $entityTable); |
| 1078 | foreach (array_keys($lineItems) as $id) { |
| 1079 | $lineItems[$id]['id'] = $id; |
| 1080 | } |
| 1081 | $itemId = key($lineItems); |
| 1082 | if ($itemId && !empty($lineItems[$itemId]['price_field_id'])) { |
| 1083 | $this->_priceSetId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $lineItems[$itemId]['price_field_id'], 'price_set_id'); |
| 1084 | } |
| 1085 | |
| 1086 | if ($this->_priceSetId && CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId, 'is_quick_config')) { |
| 1087 | $lineItems[$itemId]['unit_price'] = $lineItems[$itemId]['line_total'] = CRM_Utils_Rule::cleanMoney(CRM_Utils_Array::value('total_amount', $submittedValues)); |
| 1088 | } |
| 1089 | // 10117 update th line items for participants |
| 1090 | if (!empty($lineItems[$itemId]['price_field_id'])) { |
| 1091 | $lineItem[$this->_priceSetId] = $lineItems; |
| 1092 | } |
| 1093 | } |
| 1094 | $isQuickConfig = 0; |
| 1095 | if ($this->_priceSetId && CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId, 'is_quick_config')) { |
| 1096 | $isQuickConfig = 1; |
| 1097 | } |
| 1098 | //CRM-11529 for quick config back office transactions |
| 1099 | //when financial_type_id is passed in form, update the |
| 1100 | //line items with the financial type selected in form |
| 1101 | if ($isQuickConfig && !empty($submittedValues['financial_type_id']) && CRM_Utils_Array::value($this->_priceSetId, $lineItem) |
| 1102 | ) { |
| 1103 | foreach ($lineItem[$this->_priceSetId] as &$values) { |
| 1104 | $values['financial_type_id'] = $submittedValues['financial_type_id']; |
| 1105 | } |
| 1106 | } |
| 1107 | |
| 1108 | if (!isset($submittedValues['total_amount'])) { |
| 1109 | $submittedValues['total_amount'] = CRM_Utils_Array::value('total_amount', $this->_values); |
| 1110 | } |
| 1111 | $this->assign('lineItem', !empty($lineItem) && !$isQuickConfig ? $lineItem : FALSE); |
| 1112 | |
| 1113 | if (!empty($submittedValues['pcp_made_through_id'])) { |
| 1114 | $pcp = array(); |
| 1115 | $fields = array( |
| 1116 | 'pcp_made_through_id', |
| 1117 | 'pcp_display_in_roll', |
| 1118 | 'pcp_roll_nickname', |
| 1119 | 'pcp_personal_note', |
| 1120 | ); |
| 1121 | foreach ($fields as $f) { |
| 1122 | $pcp[$f] = CRM_Utils_Array::value($f, $submittedValues); |
| 1123 | } |
| 1124 | } |
| 1125 | |
| 1126 | $isEmpty = array_keys(array_flip($submittedValues['soft_credit_contact_id'])); |
| 1127 | if ($this->_id && count($isEmpty) == 1 && key($isEmpty) == NULL) { |
| 1128 | //Delete existing soft credit records if soft credit list is empty on update |
| 1129 | CRM_Contribute_BAO_ContributionSoft::del(array('contribution_id' => $this->_id)); |
| 1130 | } |
| 1131 | else { |
| 1132 | //build soft credit params |
| 1133 | foreach ($submittedValues['soft_credit_contact_id'] as $key => $val) { |
| 1134 | if ($val && $submittedValues['soft_credit_amount'][$key]) { |
| 1135 | $softParams[$key]['contact_id'] = $val; |
| 1136 | $softParams[$key]['amount'] = CRM_Utils_Rule::cleanMoney($submittedValues['soft_credit_amount'][$key]); |
| 1137 | $softParams[$key]['soft_credit_type_id'] = $submittedValues['soft_credit_type'][$key]; |
| 1138 | if (!empty($submittedValues['soft_credit_id'][$key])) { |
| 1139 | $softIDs[] = $softParams[$key]['id'] = $submittedValues['soft_credit_id'][$key]; |
| 1140 | } |
| 1141 | } |
| 1142 | } |
| 1143 | } |
| 1144 | |
| 1145 | // set the contact, when contact is selected |
| 1146 | if (!empty($submittedValues['contact_id'])) { |
| 1147 | $this->_contactID = $submittedValues['contact_id']; |
| 1148 | } |
| 1149 | |
| 1150 | $config = CRM_Core_Config::singleton(); |
| 1151 | |
| 1152 | //Credit Card Contribution. |
| 1153 | if ($this->_mode) { |
| 1154 | $this->processCreditCard($submittedValues, $config, $session, $lineItem); |
| 1155 | } |
| 1156 | else { |
| 1157 | //Offline Contribution. |
| 1158 | $submittedValues = $this->unsetCreditCardFields($submittedValues); |
| 1159 | |
| 1160 | // get the required field value only. |
| 1161 | $formValues = $submittedValues; |
| 1162 | $params = $ids = array(); |
| 1163 | |
| 1164 | $params['contact_id'] = $this->_contactID; |
| 1165 | |
| 1166 | $params['currency'] = $this->getCurrency($submittedValues); |
| 1167 | |
| 1168 | $fields = array( |
| 1169 | 'financial_type_id', |
| 1170 | 'contribution_status_id', |
| 1171 | 'payment_instrument_id', |
| 1172 | 'cancel_reason', |
| 1173 | 'source', |
| 1174 | 'check_number', |
| 1175 | ); |
| 1176 | foreach ($fields as $f) { |
| 1177 | $params[$f] = CRM_Utils_Array::value($f, $formValues); |
| 1178 | } |
| 1179 | |
| 1180 | if (!empty($pcp)) { |
| 1181 | $params['pcp'] = $pcp; |
| 1182 | } |
| 1183 | if (!empty($softParams)) { |
| 1184 | $params['soft_credit'] = $softParams; |
| 1185 | $params['soft_credit_ids'] = $softIDs; |
| 1186 | } |
| 1187 | |
| 1188 | //if priceset is used, no need to cleanup money |
| 1189 | //CRM-5740 |
| 1190 | if ($priceSetId) { |
| 1191 | $params['skipCleanMoney'] = 1; |
| 1192 | } |
| 1193 | |
| 1194 | $dates = array( |
| 1195 | 'receive_date', |
| 1196 | 'receipt_date', |
| 1197 | 'cancel_date', |
| 1198 | ); |
| 1199 | |
| 1200 | foreach ($dates as $d) { |
| 1201 | $params[$d] = CRM_Utils_Date::processDate($formValues[$d], $formValues[$d . '_time'], TRUE); |
| 1202 | } |
| 1203 | |
| 1204 | if (!empty($formValues['is_email_receipt'])) { |
| 1205 | $params['receipt_date'] = date("Y-m-d"); |
| 1206 | } |
| 1207 | |
| 1208 | if ($params['contribution_status_id'] == CRM_Core_OptionGroup::getValue('contribution_status', 'Cancelled', 'name') |
| 1209 | || $params['contribution_status_id'] == CRM_Core_OptionGroup::getValue('contribution_status', 'Refunded', 'name') |
| 1210 | ) { |
| 1211 | if (CRM_Utils_System::isNull(CRM_Utils_Array::value('cancel_date', $params))) { |
| 1212 | $params['cancel_date'] = date('Y-m-d'); |
| 1213 | } |
| 1214 | } |
| 1215 | else { |
| 1216 | $params['cancel_date'] = $params['cancel_reason'] = 'null'; |
| 1217 | } |
| 1218 | |
| 1219 | // Set is_pay_later flag for back-office offline Pending status contributions CRM-8996 |
| 1220 | // else if contribution_status is changed to Completed is_pay_later flag is changed to 0, CRM-15041 |
| 1221 | if ($params['contribution_status_id'] == CRM_Core_OptionGroup::getValue('contribution_status', 'Pending', 'name')) { |
| 1222 | $params['is_pay_later'] = 1; |
| 1223 | } |
| 1224 | elseif ($params['contribution_status_id'] == CRM_Core_OptionGroup::getValue('contribution_status', 'Completed', 'name')) { |
| 1225 | $params['is_pay_later'] = 0; |
| 1226 | } |
| 1227 | |
| 1228 | $ids['contribution'] = $params['id'] = $this->_id; |
| 1229 | |
| 1230 | //Add Additional common information to formatted params |
| 1231 | CRM_Contribute_Form_AdditionalInfo::postProcessCommon($formValues, $params, $this); |
| 1232 | if ($pId) { |
| 1233 | $params['contribution_mode'] = 'participant'; |
| 1234 | $params['participant_id'] = $pId; |
| 1235 | $params['skipLineItem'] = 1; |
| 1236 | } |
| 1237 | $params['line_item'] = $lineItem; |
| 1238 | $params['payment_processor_id'] = $params['payment_processor'] = CRM_Utils_Array::value('id', $this->_paymentProcessor); |
| 1239 | //create contribution. |
| 1240 | if ($isQuickConfig) { |
| 1241 | $params['is_quick_config'] = 1; |
| 1242 | } |
| 1243 | |
| 1244 | // CRM-11956 |
| 1245 | // if non_deductible_amount exists i.e. Additional Details field set was opened [and staff typed something] - |
| 1246 | // if non_deductible_amount does NOT exist - then calculate it depending on: |
| 1247 | // $ContributionType->is_deductible and whether there is a product (premium). |
| 1248 | if (!isset($params['non_deductible_amount']) || (!empty($params['non_deductible_amount']))) { |
| 1249 | $contributionType = new CRM_Financial_DAO_FinancialType(); |
| 1250 | $contributionType->id = $params['financial_type_id']; |
| 1251 | if (!$contributionType->find(TRUE)) { |
| 1252 | CRM_Core_Error::fatal('Could not find a system table'); |
| 1253 | } |
| 1254 | if ($contributionType->is_deductible) { |
| 1255 | |
| 1256 | if (isset($formValues['product_name'][0])) { |
| 1257 | $selectProduct = $formValues['product_name'][0]; |
| 1258 | } |
| 1259 | // if there is a product - compare the value to the contribution amount |
| 1260 | if (isset($selectProduct)) { |
| 1261 | $productDAO = new CRM_Contribute_DAO_Product(); |
| 1262 | $productDAO->id = $selectProduct; |
| 1263 | $productDAO->find(TRUE); |
| 1264 | // product value exceeds contribution amount |
| 1265 | if ($params['total_amount'] < $productDAO->price) { |
| 1266 | $params['non_deductible_amount'] = $params['total_amount']; |
| 1267 | } |
| 1268 | // product value does NOT exceed contribution amount |
| 1269 | else { |
| 1270 | $params['non_deductible_amount'] = $productDAO->price; |
| 1271 | } |
| 1272 | } |
| 1273 | // contribution is deductible - but there is no product |
| 1274 | else { |
| 1275 | $params['non_deductible_amount'] = '0.00'; |
| 1276 | } |
| 1277 | } |
| 1278 | // contribution is NOT deductible |
| 1279 | else { |
| 1280 | $params['non_deductible_amount'] = $params['total_amount']; |
| 1281 | } |
| 1282 | } |
| 1283 | |
| 1284 | $contribution = CRM_Contribute_BAO_Contribution::create($params, $ids); |
| 1285 | |
| 1286 | // process associated membership / participant, CRM-4395 |
| 1287 | $relatedComponentStatusMsg = NULL; |
| 1288 | if ($contribution->id && $this->_action & CRM_Core_Action::UPDATE) { |
| 1289 | $relatedComponentStatusMsg = $this->updateRelatedComponent($contribution->id, |
| 1290 | $contribution->contribution_status_id, |
| 1291 | CRM_Utils_Array::value('contribution_status_id', |
| 1292 | $this->_values |
| 1293 | ) |
| 1294 | ); |
| 1295 | } |
| 1296 | |
| 1297 | //process note |
| 1298 | if ($contribution->id && isset($formValues['note'])) { |
| 1299 | CRM_Contribute_Form_AdditionalInfo::processNote($formValues, $this->_contactID, $contribution->id, $this->_noteID); |
| 1300 | } |
| 1301 | |
| 1302 | //process premium |
| 1303 | if ($contribution->id && isset($formValues['product_name'][0])) { |
| 1304 | CRM_Contribute_Form_AdditionalInfo::processPremium($formValues, $contribution->id, |
| 1305 | $this->_premiumID, $this->_options |
| 1306 | ); |
| 1307 | } |
| 1308 | |
| 1309 | //send receipt mail. |
| 1310 | if ($contribution->id && !empty($formValues['is_email_receipt'])) { |
| 1311 | $formValues['contact_id'] = $this->_contactID; |
| 1312 | $formValues['contribution_id'] = $contribution->id; |
| 1313 | |
| 1314 | $formValues += CRM_Contribute_BAO_ContributionSoft::getSoftContribution($contribution->id); |
| 1315 | |
| 1316 | // to get 'from email id' for send receipt |
| 1317 | $this->fromEmailId = $formValues['from_email_address']; |
| 1318 | $sendReceipt = CRM_Contribute_Form_AdditionalInfo::emailReceipt($this, $formValues); |
| 1319 | } |
| 1320 | |
| 1321 | $pledgePaymentId = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment', |
| 1322 | $contribution->id, |
| 1323 | 'id', |
| 1324 | 'contribution_id' |
| 1325 | ); |
| 1326 | |
| 1327 | //update pledge payment status. |
| 1328 | if ((($this->_ppID && $contribution->id) && $this->_action & CRM_Core_Action::ADD) || |
| 1329 | (($pledgePaymentId) && $this->_action & CRM_Core_Action::UPDATE) |
| 1330 | ) { |
| 1331 | |
| 1332 | if ($this->_ppID) { |
| 1333 | //store contribution id in payment record. |
| 1334 | CRM_Core_DAO::setFieldValue('CRM_Pledge_DAO_PledgePayment', $this->_ppID, 'contribution_id', $contribution->id); |
| 1335 | } |
| 1336 | else { |
| 1337 | $this->_ppID = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment', |
| 1338 | $contribution->id, |
| 1339 | 'id', |
| 1340 | 'contribution_id' |
| 1341 | ); |
| 1342 | $this->_pledgeID = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment', |
| 1343 | $contribution->id, |
| 1344 | 'pledge_id', |
| 1345 | 'contribution_id' |
| 1346 | ); |
| 1347 | } |
| 1348 | |
| 1349 | $adjustTotalAmount = FALSE; |
| 1350 | if (CRM_Utils_Array::value('option_type', $formValues) == 2) { |
| 1351 | $adjustTotalAmount = TRUE; |
| 1352 | } |
| 1353 | |
| 1354 | $updatePledgePaymentStatus = FALSE; |
| 1355 | //do only if either the status or the amount has changed |
| 1356 | if ($this->_action & CRM_Core_Action::ADD) { |
| 1357 | $updatePledgePaymentStatus = TRUE; |
| 1358 | } |
| 1359 | elseif ($this->_action & CRM_Core_Action::UPDATE && (($this->_defaults['contribution_status_id'] != $formValues['contribution_status_id']) || |
| 1360 | ($this->_defaults['total_amount'] != $formValues['total_amount'])) |
| 1361 | ) { |
| 1362 | $updatePledgePaymentStatus = TRUE; |
| 1363 | } |
| 1364 | |
| 1365 | if ($updatePledgePaymentStatus) { |
| 1366 | CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($this->_pledgeID, |
| 1367 | array($this->_ppID), |
| 1368 | $contribution->contribution_status_id, |
| 1369 | NULL, |
| 1370 | $contribution->total_amount, |
| 1371 | $adjustTotalAmount |
| 1372 | ); |
| 1373 | } |
| 1374 | } |
| 1375 | |
| 1376 | $statusMsg = ts('The contribution record has been saved.'); |
| 1377 | if (!empty($formValues['is_email_receipt']) && $sendReceipt) { |
| 1378 | $statusMsg .= ' ' . ts('A receipt has been emailed to the contributor.'); |
| 1379 | } |
| 1380 | |
| 1381 | if ($relatedComponentStatusMsg) { |
| 1382 | $statusMsg .= ' ' . $relatedComponentStatusMsg; |
| 1383 | } |
| 1384 | |
| 1385 | CRM_Core_Session::setStatus($statusMsg, ts('Saved'), 'success'); |
| 1386 | //Offline Contribution ends. |
| 1387 | } |
| 1388 | $buttonName = $this->controller->getButtonName(); |
| 1389 | if ($this->_context == 'standalone') { |
| 1390 | if ($buttonName == $this->getButtonName('upload', 'new')) { |
| 1391 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/contribute/add', |
| 1392 | 'reset=1&action=add&context=standalone' |
| 1393 | )); |
| 1394 | } |
| 1395 | else { |
| 1396 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view', |
| 1397 | "reset=1&cid={$this->_contactID}&selectedChild=contribute" |
| 1398 | )); |
| 1399 | } |
| 1400 | } |
| 1401 | elseif ($this->_context == 'contribution' && $this->_mode && $buttonName == $this->getButtonName('upload', 'new')) { |
| 1402 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view/contribution', |
| 1403 | "reset=1&action=add&context={$this->_context}&cid={$this->_contactID}&mode={$this->_mode}" |
| 1404 | )); |
| 1405 | } |
| 1406 | elseif ($buttonName == $this->getButtonName('upload', 'new')) { |
| 1407 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view/contribution', |
| 1408 | "reset=1&action=add&context={$this->_context}&cid={$this->_contactID}" |
| 1409 | )); |
| 1410 | } |
| 1411 | |
| 1412 | //store contribution ID if not yet set (on create) |
| 1413 | if ( empty($this->_id) && !empty($contribution->id) ) { |
| 1414 | $this->_id = $contribution->id; |
| 1415 | } |
| 1416 | } |
| 1417 | |
| 1418 | /** |
| 1419 | * @param $submittedValues |
| 1420 | * @param $config |
| 1421 | * @param $session CRM_Core_Session |
| 1422 | * @param $lineItem |
| 1423 | * |
| 1424 | * @throws CRM_Core_Exception |
| 1425 | */ |
| 1426 | public function processCreditCard($submittedValues, $config, $session, $lineItem) { |
| 1427 | $sendReceipt = $contribution = FALSE; |
| 1428 | $unsetParams = array( |
| 1429 | 'trxn_id', |
| 1430 | 'payment_instrument_id', |
| 1431 | 'contribution_status_id', |
| 1432 | 'cancel_date', |
| 1433 | 'cancel_reason', |
| 1434 | ); |
| 1435 | foreach ($unsetParams as $key) { |
| 1436 | if (isset($submittedValues[$key])) { |
| 1437 | unset($submittedValues[$key]); |
| 1438 | } |
| 1439 | } |
| 1440 | $isTest = ($this->_mode == 'test') ? 1 : 0; |
| 1441 | // CRM-12680 set $_lineItem if its not set |
| 1442 | if (empty($this->_lineItem) && !empty($lineItem)) { |
| 1443 | $this->_lineItem = $lineItem; |
| 1444 | } |
| 1445 | |
| 1446 | //Get the require fields value only. |
| 1447 | $params = $this->_params = $submittedValues; |
| 1448 | |
| 1449 | $this->_paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($this->_params['payment_processor_id'], |
| 1450 | $this->_mode |
| 1451 | ); |
| 1452 | |
| 1453 | //get the payment processor id as per mode. |
| 1454 | $this->_params['payment_processor'] = $params['payment_processor_id'] = |
| 1455 | $this->_params['payment_processor_id'] = $submittedValues['payment_processor_id'] = $this->_paymentProcessor['id']; |
| 1456 | |
| 1457 | $now = date('YmdHis'); |
| 1458 | $fields = array(); |
| 1459 | |
| 1460 | // we need to retrieve email address |
| 1461 | if ($this->_context == 'standalone' && !empty($submittedValues['is_email_receipt'])) { |
| 1462 | list($this->userDisplayName, |
| 1463 | $this->userEmail |
| 1464 | ) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactID); |
| 1465 | $this->assign('displayName', $this->userDisplayName); |
| 1466 | } |
| 1467 | |
| 1468 | //set email for primary location. |
| 1469 | $fields['email-Primary'] = 1; |
| 1470 | $params['email-Primary'] = $this->userEmail; |
| 1471 | |
| 1472 | // now set the values for the billing location. |
| 1473 | foreach (array_keys($this->_fields) as $name) { |
| 1474 | $fields[$name] = 1; |
| 1475 | } |
| 1476 | |
| 1477 | // also add location name to the array |
| 1478 | $params["address_name-{$this->_bltID}"] = CRM_Utils_Array::value('billing_first_name', $params) . ' ' . CRM_Utils_Array::value('billing_middle_name', $params) . ' ' . CRM_Utils_Array::value('billing_last_name', $params); |
| 1479 | $params["address_name-{$this->_bltID}"] = trim($params["address_name-{$this->_bltID}"]); |
| 1480 | $fields["address_name-{$this->_bltID}"] = 1; |
| 1481 | |
| 1482 | $ctype = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', |
| 1483 | $this->_contactID, |
| 1484 | 'contact_type' |
| 1485 | ); |
| 1486 | |
| 1487 | $nameFields = array('first_name', 'middle_name', 'last_name'); |
| 1488 | foreach ($nameFields as $name) { |
| 1489 | $fields[$name] = 1; |
| 1490 | if (array_key_exists("billing_$name", $params)) { |
| 1491 | $params[$name] = $params["billing_{$name}"]; |
| 1492 | $params['preserveDBName'] = TRUE; |
| 1493 | } |
| 1494 | } |
| 1495 | |
| 1496 | if (!empty($params['source'])) { |
| 1497 | unset($params['source']); |
| 1498 | } |
| 1499 | $contactID = CRM_Contact_BAO_Contact::createProfileContact($params, $fields, |
| 1500 | $this->_contactID, |
| 1501 | NULL, NULL, |
| 1502 | $ctype |
| 1503 | ); |
| 1504 | |
| 1505 | // add all the additional payment params we need |
| 1506 | $this->_params["state_province-{$this->_bltID}"] = $this->_params["billing_state_province-{$this->_bltID}"] = CRM_Core_PseudoConstant::stateProvinceAbbreviation($this->_params["billing_state_province_id-{$this->_bltID}"]); |
| 1507 | $this->_params["country-{$this->_bltID}"] = $this->_params["billing_country-{$this->_bltID}"] = CRM_Core_PseudoConstant::countryIsoCode($this->_params["billing_country_id-{$this->_bltID}"]); |
| 1508 | |
| 1509 | if ($this->_paymentProcessor['payment_type'] & CRM_Core_Payment::PAYMENT_TYPE_CREDIT_CARD) { |
| 1510 | $this->_params['year'] = CRM_Core_Payment_Form::getCreditCardExpirationYear($this->_params); |
| 1511 | $this->_params['month'] = CRM_Core_Payment_Form::getCreditCardExpirationMonth($this->_params); |
| 1512 | } |
| 1513 | $this->_params['ip_address'] = CRM_Utils_System::ipAddress(); |
| 1514 | $this->_params['amount'] = $this->_params['total_amount']; |
| 1515 | $this->_params['amount_level'] = 0; |
| 1516 | $this->_params['currencyID'] = CRM_Utils_Array::value('currency', |
| 1517 | $this->_params, |
| 1518 | $config->defaultCurrency |
| 1519 | ); |
| 1520 | $this->_params['payment_action'] = 'Sale'; |
| 1521 | if (!empty($this->_params['receive_date'])) { |
| 1522 | $this->_params['receive_date'] = CRM_Utils_Date::processDate($this->_params['receive_date'], $this->_params['receive_date_time']); |
| 1523 | } |
| 1524 | |
| 1525 | if (!empty($params['soft_credit_to'])) { |
| 1526 | $this->_params['soft_credit_to'] = $params['soft_credit_to']; |
| 1527 | $this->_params['pcp_made_through_id'] = $params['pcp_made_through_id']; |
| 1528 | } |
| 1529 | |
| 1530 | $this->_params['pcp_display_in_roll'] = CRM_Utils_Array::value('pcp_display_in_roll', $params); |
| 1531 | $this->_params['pcp_roll_nickname'] = CRM_Utils_Array::value('pcp_roll_nickname', $params); |
| 1532 | $this->_params['pcp_personal_note'] = CRM_Utils_Array::value('pcp_personal_note', $params); |
| 1533 | |
| 1534 | //Add common data to formatted params |
| 1535 | CRM_Contribute_Form_AdditionalInfo::postProcessCommon($params, $this->_params, $this); |
| 1536 | |
| 1537 | if (empty($this->_params['invoice_id'])) { |
| 1538 | $this->_params['invoiceID'] = md5(uniqid(rand(), TRUE)); |
| 1539 | } |
| 1540 | else { |
| 1541 | $this->_params['invoiceID'] = $this->_params['invoice_id']; |
| 1542 | } |
| 1543 | |
| 1544 | // at this point we've created a contact and stored its address etc |
| 1545 | // all the payment processors expect the name and address to be in the |
| 1546 | // so we copy stuff over to first_name etc. |
| 1547 | $paymentParams = $this->_params; |
| 1548 | $paymentParams['contactID'] = $this->_contactID; |
| 1549 | CRM_Core_Payment_Form::mapParams($this->_bltID, $this->_params, $paymentParams, TRUE); |
| 1550 | |
| 1551 | $contributionType = new CRM_Financial_DAO_FinancialType(); |
| 1552 | $contributionType->id = $params['financial_type_id']; |
| 1553 | if (!$contributionType->find(TRUE)) { |
| 1554 | CRM_Core_Error::fatal('Could not find a system table'); |
| 1555 | } |
| 1556 | |
| 1557 | // add some financial type details to the params list |
| 1558 | // if folks need to use it |
| 1559 | $paymentParams['contributionType_name'] = $this->_params['contributionType_name'] = $contributionType->name; |
| 1560 | $paymentParams['contributionPageID'] = NULL; |
| 1561 | if (!empty($this->_params['is_email_receipt'])) { |
| 1562 | $paymentParams['email'] = $this->userEmail; |
| 1563 | $paymentParams['is_email_receipt'] = 1; |
| 1564 | } |
| 1565 | else { |
| 1566 | $paymentParams['is_email_receipt'] = 0; |
| 1567 | $this->_params['is_email_receipt'] = 0; |
| 1568 | } |
| 1569 | if (!empty($this->_params['receive_date'])) { |
| 1570 | $paymentParams['receive_date'] = $this->_params['receive_date']; |
| 1571 | } |
| 1572 | |
| 1573 | // For recurring contribution, create Contribution Record first. |
| 1574 | // Contribution ID, Recurring ID and Contact ID needed |
| 1575 | // When we get a callback from the payment processor, CRM-7115 |
| 1576 | if (!empty($paymentParams['is_recur'])) { |
| 1577 | $contribution = CRM_Contribute_Form_Contribution_Confirm::processContribution($this, |
| 1578 | $this->_params, |
| 1579 | NULL, |
| 1580 | $this->_contactID, |
| 1581 | $contributionType, |
| 1582 | TRUE, |
| 1583 | FALSE, |
| 1584 | $isTest, |
| 1585 | $this->_lineItem |
| 1586 | ); |
| 1587 | $paymentParams['contributionID'] = $contribution->id; |
| 1588 | $paymentParams['contributionTypeID'] = $contribution->financial_type_id; |
| 1589 | $paymentParams['contributionPageID'] = $contribution->contribution_page_id; |
| 1590 | $paymentParams['contributionRecurID'] = $contribution->contribution_recur_id; |
| 1591 | } |
| 1592 | $result = NULL; |
| 1593 | if ($paymentParams['amount'] > 0.0) { |
| 1594 | // force a re-get of the payment processor in case the form changed it, CRM-7179 |
| 1595 | $payment = CRM_Core_Payment::singleton($this->_mode, $this->_paymentProcessor, $this, TRUE); |
| 1596 | $result = $payment->doDirectPayment($paymentParams); |
| 1597 | } |
| 1598 | |
| 1599 | if (is_a($result, 'CRM_Core_Error')) { |
| 1600 | //make sure to cleanup db for recurring case. |
| 1601 | if (!empty($paymentParams['contributionID'])) { |
| 1602 | CRM_Core_Error::debug_log_message(CRM_Core_Error::getMessages($result) . "contact id={$this->_contactID} (deleting contribution {$paymentParams['contributionID']}"); |
| 1603 | CRM_Contribute_BAO_Contribution::deleteContribution($paymentParams['contributionID']); |
| 1604 | } |
| 1605 | if (!empty($paymentParams['contributionRecurID'])) { |
| 1606 | CRM_Core_Error::debug_log_message(CRM_Core_Error::getMessages($result) . "contact id={$this->_contactID} (deleting recurring contribution {$paymentParams['contributionRecurID']}"); |
| 1607 | CRM_Contribute_BAO_ContributionRecur::deleteRecurContribution($paymentParams['contributionRecurID']); |
| 1608 | } |
| 1609 | |
| 1610 | //set the contribution mode. |
| 1611 | $urlParams = "action=add&cid={$this->_contactID}"; |
| 1612 | if ($this->_mode) { |
| 1613 | $urlParams .= "&mode={$this->_mode}"; |
| 1614 | } |
| 1615 | if (!empty($this->_ppID)) { |
| 1616 | $urlParams .= "&context=pledge&ppid={$this->_ppID}"; |
| 1617 | } |
| 1618 | CRM_Core_Error::displaySessionError($result); |
| 1619 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/view/contribution', $urlParams)); |
| 1620 | } |
| 1621 | |
| 1622 | if ($result) { |
| 1623 | $this->_params = array_merge($this->_params, $result); |
| 1624 | } |
| 1625 | |
| 1626 | $this->_params['receive_date'] = $now; |
| 1627 | |
| 1628 | if (!empty($this->_params['is_email_receipt'])) { |
| 1629 | $this->_params['receipt_date'] = $now; |
| 1630 | } |
| 1631 | else { |
| 1632 | $this->_params['receipt_date'] = CRM_Utils_Date::processDate($this->_params['receipt_date'], |
| 1633 | $params['receipt_date_time'], TRUE |
| 1634 | ); |
| 1635 | } |
| 1636 | |
| 1637 | $this->set('params', $this->_params); |
| 1638 | $this->assign('trxn_id', $result['trxn_id']); |
| 1639 | $this->assign('receive_date', $this->_params['receive_date']); |
| 1640 | |
| 1641 | // result has all the stuff we need |
| 1642 | // lets archive it to a financial transaction |
| 1643 | if ($contributionType->is_deductible) { |
| 1644 | $this->assign('is_deductible', TRUE); |
| 1645 | $this->set('is_deductible', TRUE); |
| 1646 | } |
| 1647 | |
| 1648 | // set source if not set |
| 1649 | if (empty($this->_params['source'])) { |
| 1650 | $userID = $session->get('userID'); |
| 1651 | $userSortName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $userID, |
| 1652 | 'sort_name' |
| 1653 | ); |
| 1654 | $this->_params['source'] = ts('Submit Credit Card Payment by: %1', array(1 => $userSortName)); |
| 1655 | } |
| 1656 | |
| 1657 | // build custom data getFields array |
| 1658 | $customFieldsContributionType = CRM_Core_BAO_CustomField::getFields('Contribution', FALSE, FALSE, |
| 1659 | CRM_Utils_Array::value('financial_type_id', $params) |
| 1660 | ); |
| 1661 | $customFields = CRM_Utils_Array::crmArrayMerge($customFieldsContributionType, |
| 1662 | CRM_Core_BAO_CustomField::getFields('Contribution', FALSE, FALSE, NULL, NULL, TRUE) |
| 1663 | ); |
| 1664 | $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, |
| 1665 | $customFields, |
| 1666 | $this->_id, |
| 1667 | 'Contribution' |
| 1668 | ); |
| 1669 | |
| 1670 | |
| 1671 | if (empty($paymentParams['is_recur'])) { |
| 1672 | $contribution = CRM_Contribute_Form_Contribution_Confirm::processContribution($this, |
| 1673 | $this->_params, |
| 1674 | $result, |
| 1675 | $this->_contactID, |
| 1676 | $contributionType, |
| 1677 | FALSE, FALSE, |
| 1678 | $isTest, |
| 1679 | $this->_lineItem |
| 1680 | ); |
| 1681 | } |
| 1682 | |
| 1683 | //send receipt mail. |
| 1684 | if ($contribution->id && !empty($this->_params['is_email_receipt'])) { |
| 1685 | $this->_params['trxn_id'] = CRM_Utils_Array::value('trxn_id', $result); |
| 1686 | $this->_params['contact_id'] = $this->_contactID; |
| 1687 | $this->_params['contribution_id'] = $contribution->id; |
| 1688 | $sendReceipt = CRM_Contribute_Form_AdditionalInfo::emailReceipt($this, $this->_params, TRUE); |
| 1689 | } |
| 1690 | |
| 1691 | //process the note |
| 1692 | if ($contribution->id && isset($params['note'])) { |
| 1693 | CRM_Contribute_Form_AdditionalInfo::processNote($params, $contactID, $contribution->id, NULL); |
| 1694 | } |
| 1695 | //process premium |
| 1696 | if ($contribution->id && isset($params['product_name'][0])) { |
| 1697 | CRM_Contribute_Form_AdditionalInfo::processPremium($params, $contribution->id, NULL, $this->_options); |
| 1698 | } |
| 1699 | |
| 1700 | //update pledge payment status. |
| 1701 | if ($this->_ppID && $contribution->id) { |
| 1702 | //store contribution id in payment record. |
| 1703 | CRM_Core_DAO::setFieldValue('CRM_Pledge_DAO_PledgePayment', $this->_ppID, 'contribution_id', $contribution->id); |
| 1704 | |
| 1705 | CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($this->_pledgeID, |
| 1706 | array($this->_ppID), |
| 1707 | $contribution->contribution_status_id, |
| 1708 | NULL, |
| 1709 | $contribution->total_amount |
| 1710 | ); |
| 1711 | } |
| 1712 | |
| 1713 | if ($contribution->id) { |
| 1714 | $statusMsg = ts('The contribution record has been processed.'); |
| 1715 | if (!empty($this->_params['is_email_receipt']) && $sendReceipt) { |
| 1716 | $statusMsg .= ' ' . ts('A receipt has been emailed to the contributor.'); |
| 1717 | } |
| 1718 | CRM_Core_Session::setStatus($statusMsg, ts('Complete'), 'success'); |
| 1719 | } |
| 1720 | } |
| 1721 | } |