| 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 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-2013 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * This class generates form components for processing a contribution |
| 38 | * |
| 39 | */ |
| 40 | class CRM_Contribute_Form_AbstractEditPayment extends CRM_Core_Form { |
| 41 | public $_mode; |
| 42 | |
| 43 | public $_action; |
| 44 | |
| 45 | public $_bltID; |
| 46 | |
| 47 | public $_fields; |
| 48 | |
| 49 | public $_paymentProcessor; |
| 50 | public $_recurPaymentProcessors; |
| 51 | |
| 52 | public $_processors; |
| 53 | |
| 54 | /** |
| 55 | * the id of the contribution that we are proceessing |
| 56 | * |
| 57 | * @var int |
| 58 | * @public |
| 59 | */ |
| 60 | public $_id; |
| 61 | |
| 62 | /** |
| 63 | * the id of the premium that we are proceessing |
| 64 | * |
| 65 | * @var int |
| 66 | * @public |
| 67 | */ |
| 68 | public $_premiumID = NULL; |
| 69 | public $_productDAO = NULL; |
| 70 | |
| 71 | /** |
| 72 | * the id of the note |
| 73 | * |
| 74 | * @var int |
| 75 | * @public |
| 76 | */ |
| 77 | public $_noteID; |
| 78 | |
| 79 | /** |
| 80 | * the id of the contact associated with this contribution |
| 81 | * |
| 82 | * @var int |
| 83 | * @public |
| 84 | */ |
| 85 | public $_contactID; |
| 86 | |
| 87 | /** |
| 88 | * the id of the pledge payment that we are processing |
| 89 | * |
| 90 | * @var int |
| 91 | * @public |
| 92 | */ |
| 93 | public $_ppID; |
| 94 | |
| 95 | /** |
| 96 | * the id of the pledge that we are processing |
| 97 | * |
| 98 | * @var int |
| 99 | * @public |
| 100 | */ |
| 101 | public $_pledgeID; |
| 102 | |
| 103 | /** |
| 104 | * is this contribution associated with an online |
| 105 | * financial transaction |
| 106 | * |
| 107 | * @var boolean |
| 108 | * @public |
| 109 | */ |
| 110 | public $_online = FALSE; |
| 111 | |
| 112 | /** |
| 113 | * Stores all product option |
| 114 | * |
| 115 | * @var array |
| 116 | * @public |
| 117 | */ |
| 118 | public $_options; |
| 119 | |
| 120 | /** |
| 121 | * stores the honor id |
| 122 | * |
| 123 | * @var int |
| 124 | * @public |
| 125 | */ |
| 126 | public $_honorID = NULL; |
| 127 | |
| 128 | /** |
| 129 | * Store the contribution Type ID |
| 130 | * |
| 131 | * @var array |
| 132 | */ |
| 133 | public $_contributionType; |
| 134 | |
| 135 | /** |
| 136 | * The contribution values if an existing contribution |
| 137 | */ |
| 138 | public $_values; |
| 139 | |
| 140 | /** |
| 141 | * The pledge values if this contribution is associated with pledge |
| 142 | */ |
| 143 | public $_pledgeValues; |
| 144 | |
| 145 | public $_contributeMode = 'direct'; |
| 146 | |
| 147 | public $_context; |
| 148 | |
| 149 | public $_compId; |
| 150 | |
| 151 | /* |
| 152 | * Store the line items if price set used. |
| 153 | */ |
| 154 | public $_lineItems; |
| 155 | |
| 156 | protected $_formType; |
| 157 | protected $_cdType; |
| 158 | |
| 159 | public function buildValuesAndAssignOnline_Note_Type($id, &$values) { |
| 160 | $ids = array(); |
| 161 | $params = array('id' => $id); |
| 162 | CRM_Contribute_BAO_Contribution::getValues($params, $values, $ids); |
| 163 | |
| 164 | //Check if this is an online transaction (financial_trxn.payment_processor_id NOT NULL) |
| 165 | $this->_online = FALSE; |
| 166 | $fids = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($id); |
| 167 | if (CRM_Utils_Array::value('financialTrxnId', $fids)) { |
| 168 | $this->_online = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialTrxn', $fids['financialTrxnId'], 'payment_processor_id'); |
| 169 | } |
| 170 | |
| 171 | // Also don't allow user to update some fields for recurring contributions. |
| 172 | if (!$this->_online) { |
| 173 | $this->_online = CRM_Utils_Array::value('contribution_recur_id', $values); |
| 174 | } |
| 175 | |
| 176 | $this->assign('isOnline', $this->_online ? TRUE : FALSE); |
| 177 | |
| 178 | //unset the honor type id:when delete the honor_contact_id |
| 179 | //and edit the contribution, honoree infomation pane open |
| 180 | //since honor_type_id is present |
| 181 | if (!CRM_Utils_Array::value('honor_contact_id', $values)) { |
| 182 | unset($values['honor_type_id']); |
| 183 | } |
| 184 | //to get note id |
| 185 | $daoNote = new CRM_Core_BAO_Note(); |
| 186 | $daoNote->entity_table = 'civicrm_contribution'; |
| 187 | $daoNote->entity_id = $id; |
| 188 | if ($daoNote->find(TRUE)) { |
| 189 | $this->_noteID = $daoNote->id; |
| 190 | $values['note'] = $daoNote->note; |
| 191 | } |
| 192 | $this->_contributionType = $values['financial_type_id']; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * @param string $type eg 'Contribution' |
| 197 | * @param string $subType |
| 198 | * @param int $entityId |
| 199 | */ |
| 200 | public function applyCustomData($type, $subType, $entityId) { |
| 201 | $this->set('type', $type); |
| 202 | $this->set('subType', $subType); |
| 203 | $this->set('entityId', $entityId); |
| 204 | |
| 205 | CRM_Custom_Form_CustomData::preProcess($this, NULL, $subType, 1, $type, $entityId); |
| 206 | CRM_Custom_Form_CustomData::buildQuickForm($this); |
| 207 | CRM_Custom_Form_CustomData::setDefaultValues($this); |
| 208 | } |
| 209 | |
| 210 | public function assignPremiumProduct($id) { //to get Premium id |
| 211 | $sql = " |
| 212 | SELECT * |
| 213 | FROM civicrm_contribution_product |
| 214 | WHERE contribution_id = {$id} |
| 215 | "; |
| 216 | $dao = CRM_Core_DAO::executeQuery($sql, |
| 217 | CRM_Core_DAO::$_nullArray |
| 218 | ); |
| 219 | if ($dao->fetch()) { |
| 220 | $this->_premiumID = $dao->id; |
| 221 | $this->_productDAO = $dao; |
| 222 | } |
| 223 | $dao->free(); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * This function process contribution related objects. |
| 228 | */ |
| 229 | protected function updateRelatedComponent($contributionId, $statusId, $previousStatusId = NULL) { |
| 230 | $statusMsg = NULL; |
| 231 | if (!$contributionId || !$statusId) { |
| 232 | return $statusMsg; |
| 233 | } |
| 234 | |
| 235 | $params = array( |
| 236 | 'contribution_id' => $contributionId, |
| 237 | 'contribution_status_id' => $statusId, |
| 238 | 'previous_contribution_status_id' => $previousStatusId, |
| 239 | ); |
| 240 | |
| 241 | $updateResult = CRM_Contribute_BAO_Contribution::transitionComponents($params); |
| 242 | |
| 243 | if (!is_array($updateResult) || |
| 244 | !($updatedComponents = CRM_Utils_Array::value('updatedComponents', $updateResult)) || |
| 245 | !is_array($updatedComponents) || |
| 246 | empty($updatedComponents) |
| 247 | ) { |
| 248 | return $statusMsg; |
| 249 | } |
| 250 | |
| 251 | // get the user display name. |
| 252 | $sql = " |
| 253 | SELECT display_name as displayName |
| 254 | FROM civicrm_contact |
| 255 | LEFT JOIN civicrm_contribution on (civicrm_contribution.contact_id = civicrm_contact.id ) |
| 256 | WHERE civicrm_contribution.id = {$contributionId}"; |
| 257 | $userDisplayName = CRM_Core_DAO::singleValueQuery($sql); |
| 258 | |
| 259 | // get the status message for user. |
| 260 | foreach ($updatedComponents as $componentName => $updatedStatusId) { |
| 261 | |
| 262 | if ($componentName == 'CiviMember') { |
| 263 | $updatedStatusName = CRM_Utils_Array::value($updatedStatusId, |
| 264 | CRM_Member_PseudoConstant::membershipStatus() |
| 265 | ); |
| 266 | if ($updatedStatusName == 'Cancelled') { |
| 267 | $statusMsg .= "<br />" . ts("Membership for %1 has been Cancelled.", array(1 => $userDisplayName)); |
| 268 | } |
| 269 | elseif ($updatedStatusName == 'Expired') { |
| 270 | $statusMsg .= "<br />" . ts("Membership for %1 has been Expired.", array(1 => $userDisplayName)); |
| 271 | } |
| 272 | elseif ($endDate = CRM_Utils_Array::value('membership_end_date', $updateResult)) { |
| 273 | $statusMsg .= "<br />" . ts("Membership for %1 has been updated. The membership End Date is %2.", |
| 274 | array( |
| 275 | 1 => $userDisplayName, |
| 276 | 2 => $endDate |
| 277 | ) |
| 278 | ); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | if ($componentName == 'CiviEvent') { |
| 283 | $updatedStatusName = CRM_Utils_Array::value($updatedStatusId, |
| 284 | CRM_Event_PseudoConstant::participantStatus() |
| 285 | ); |
| 286 | if ($updatedStatusName == 'Cancelled') { |
| 287 | $statusMsg .= "<br />" . ts("Event Registration for %1 has been Cancelled.", array(1 => $userDisplayName)); |
| 288 | } |
| 289 | elseif ($updatedStatusName == 'Registered') { |
| 290 | $statusMsg .= "<br />" . ts("Event Registration for %1 has been updated.", array(1 => $userDisplayName)); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | if ($componentName == 'CiviPledge') { |
| 295 | $updatedStatusName = CRM_Utils_Array::value($updatedStatusId, |
| 296 | CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name') |
| 297 | ); |
| 298 | if ($updatedStatusName == 'Cancelled') { |
| 299 | $statusMsg .= "<br />" . ts("Pledge Payment for %1 has been Cancelled.", array(1 => $userDisplayName)); |
| 300 | } |
| 301 | elseif ($updatedStatusName == 'Failed') { |
| 302 | $statusMsg .= "<br />" . ts("Pledge Payment for %1 has been Failed.", array(1 => $userDisplayName)); |
| 303 | } |
| 304 | elseif ($updatedStatusName == 'Completed') { |
| 305 | $statusMsg .= "<br />" . ts("Pledge Payment for %1 has been updated.", array(1 => $userDisplayName)); |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | return $statusMsg; |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * @return array (0 => array(int $ppId => string $label), 1 => array(...payproc details...)) |
| 315 | */ |
| 316 | public function getValidProcessorsAndAssignFutureStartDate() { |
| 317 | $validProcessors = array(); |
| 318 | $processors = CRM_Core_PseudoConstant::paymentProcessor(FALSE, FALSE, "billing_mode IN ( 1, 3 )"); |
| 319 | |
| 320 | foreach ($processors as $ppID => $label) { |
| 321 | $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($ppID, $this->_mode); |
| 322 | // at this stage only Authorize.net has been tested to support future start dates so if it's enabled let the template know |
| 323 | // to show receive date |
| 324 | $processorsSupportingFutureStartDate = array('AuthNet'); |
| 325 | if (in_array($paymentProcessor['payment_processor_type'], $processorsSupportingFutureStartDate)) { |
| 326 | $this->assign('processorSupportsFutureStartDate', TRUE); |
| 327 | } |
| 328 | if ($paymentProcessor['payment_processor_type'] == 'PayPal' && !$paymentProcessor['user_name']) { |
| 329 | continue; |
| 330 | } |
| 331 | elseif ($paymentProcessor['payment_processor_type'] == 'Dummy' && $this->_mode == 'live') { |
| 332 | continue; |
| 333 | } |
| 334 | else { |
| 335 | $paymentObject = CRM_Core_Payment::singleton($this->_mode, $paymentProcessor, $this); |
| 336 | $error = $paymentObject->checkConfig(); |
| 337 | if (empty($error)) { |
| 338 | $validProcessors[$ppID] = $label; |
| 339 | } |
| 340 | $paymentObject = NULL; |
| 341 | } |
| 342 | } |
| 343 | if (empty($validProcessors)) { |
| 344 | CRM_Core_Error::fatal(ts('You will need to configure the %1 settings for your Payment Processor before you can submit credit card transactions.', array(1 => $this->_mode))); |
| 345 | } |
| 346 | else { |
| 347 | return array($validProcessors, $paymentProcessor); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * Assign billing type id to bltID |
| 353 | * |
| 354 | * @return void |
| 355 | */ |
| 356 | public function assignBillingType() { |
| 357 | $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'); |
| 358 | $this->_bltID = array_search('Billing', $locationTypes); |
| 359 | if (!$this->_bltID) { |
| 360 | CRM_Core_Error::fatal(ts('Please set a location type of %1', array(1 => 'Billing'))); |
| 361 | } |
| 362 | $this->set('bltID', $this->_bltID); |
| 363 | $this->assign('bltID', $this->_bltID); |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * Assign $this->processors, $this->recurPaymentProcessors, and related Smarty variables |
| 368 | */ |
| 369 | public function assignProcessors() { |
| 370 | //ensure that processor has a valid config |
| 371 | //only valid processors get display to user |
| 372 | if ($this->_mode) { |
| 373 | list($this->_processors, $paymentProcessor) = $this->getValidProcessorsAndAssignFutureStartDate(); |
| 374 | |
| 375 | //get the valid recurring processors. |
| 376 | $recurring = CRM_Core_PseudoConstant::paymentProcessor(FALSE, FALSE, 'is_recur = 1'); |
| 377 | $this->_recurPaymentProcessors = array_intersect_assoc($this->_processors, $recurring); |
| 378 | } |
| 379 | $this->assign('recurringPaymentProcessorIds', |
| 380 | empty($this->_recurPaymentProcessors) ? '' : implode(',', array_keys($this->_recurPaymentProcessors)) |
| 381 | ); |
| 382 | |
| 383 | // this required to show billing block |
| 384 | $this->assign_by_ref('paymentProcessor', $paymentProcessor); |
| 385 | $this->assign('hidePayPalExpress', TRUE); |
| 386 | } |
| 387 | |
| 388 | public function getCurrency($submittedValues) { // get current currency from DB or use default currency |
| 389 | $config = CRM_Core_Config::singleton(); |
| 390 | |
| 391 | $currentCurrency = CRM_Utils_Array::value('currency', |
| 392 | $this->_values, |
| 393 | $config->defaultCurrency |
| 394 | ); |
| 395 | |
| 396 | // use submitted currency if present else use current currency |
| 397 | $result = CRM_Utils_Array::value('currency', |
| 398 | $submittedValues, |
| 399 | $currentCurrency |
| 400 | ); |
| 401 | return $result; |
| 402 | } |
| 403 | |
| 404 | public function getFinancialAccounts($financialTypeId) { |
| 405 | $financialAccounts = array(); |
| 406 | CRM_Core_PseudoConstant::populate($financialAccounts, |
| 407 | 'CRM_Financial_DAO_EntityFinancialAccount', |
| 408 | $all = TRUE, |
| 409 | $retrieve = 'financial_account_id', |
| 410 | $filter = NULL, |
| 411 | " entity_id = {$financialTypeId} ", NULL, 'account_relationship'); |
| 412 | return $financialAccounts; |
| 413 | } |
| 414 | |
| 415 | public function getFinancialAccount($financialTypeId, $relationTypeId) { |
| 416 | $financialAccounts = $this->getFinancialAccounts($financialTypeId); |
| 417 | return CRM_Utils_Array::value($relationTypeId, $financialAccounts); |
| 418 | } |
| 419 | |
| 420 | public function preProcessPledge() { |
| 421 | //get the payment values associated with given pledge payment id OR check for payments due. |
| 422 | $this->_pledgeValues = array(); |
| 423 | if ($this->_ppID) { |
| 424 | $payParams = array('id' => $this->_ppID); |
| 425 | |
| 426 | CRM_Pledge_BAO_PledgePayment::retrieve($payParams, $this->_pledgeValues['pledgePayment']); |
| 427 | $this->_pledgeID = CRM_Utils_Array::value('pledge_id', $this->_pledgeValues['pledgePayment']); |
| 428 | $paymentStatusID = CRM_Utils_Array::value('status_id', $this->_pledgeValues['pledgePayment']); |
| 429 | $this->_id = CRM_Utils_Array::value('contribution_id', $this->_pledgeValues['pledgePayment']); |
| 430 | |
| 431 | //get all status |
| 432 | $allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); |
| 433 | if (!($paymentStatusID == array_search('Pending', $allStatus) || $paymentStatusID == array_search('Overdue', $allStatus))) { |
| 434 | CRM_Core_Error::fatal(ts("Pledge payment status should be 'Pending' or 'Overdue'.")); |
| 435 | } |
| 436 | |
| 437 | //get the pledge values associated with given pledge payment. |
| 438 | |
| 439 | $ids = array(); |
| 440 | $pledgeParams = array('id' => $this->_pledgeID); |
| 441 | CRM_Pledge_BAO_Pledge::getValues($pledgeParams, $this->_pledgeValues, $ids); |
| 442 | $this->assign('ppID', $this->_ppID); |
| 443 | } |
| 444 | else { |
| 445 | // Not making a pledge payment, so if adding a new contribution we should check if pledge payment(s) are due for this contact so we can alert the user. CRM-5206 |
| 446 | if (isset($this->_contactID)) { |
| 447 | $contactPledges = CRM_Pledge_BAO_Pledge::getContactPledges($this->_contactID); |
| 448 | |
| 449 | if (!empty($contactPledges)) { |
| 450 | $payments = $paymentsDue = NULL; |
| 451 | $multipleDue = FALSE; |
| 452 | foreach ($contactPledges as $key => $pledgeId) { |
| 453 | $payments = CRM_Pledge_BAO_PledgePayment::getOldestPledgePayment($pledgeId); |
| 454 | if ($payments) { |
| 455 | if ($paymentsDue) { |
| 456 | $multipleDue = TRUE; |
| 457 | break; |
| 458 | } |
| 459 | else { |
| 460 | $paymentsDue = $payments; |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | if ($multipleDue) { |
| 465 | // Show link to pledge tab since more than one pledge has a payment due |
| 466 | $pledgeTab = CRM_Utils_System::url('civicrm/contact/view', |
| 467 | "reset=1&force=1&cid={$this->_contactID}&selectedChild=pledge" |
| 468 | ); |
| 469 | CRM_Core_Session::setStatus(ts('This contact has pending or overdue pledge payments. <a href="%1">Click here to view their Pledges tab</a> and verify whether this contribution should be applied as a pledge payment.', array(1 => $pledgeTab)), ts('Notice'), 'alert'); |
| 470 | } |
| 471 | elseif ($paymentsDue) { |
| 472 | // Show user link to oldest Pending or Overdue pledge payment |
| 473 | $ppAmountDue = CRM_Utils_Money::format($payments['amount'], $payments['currency']); |
| 474 | $ppSchedDate = CRM_Utils_Date::customFormat(CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment', $payments['id'], 'scheduled_date')); |
| 475 | if ($this->_mode) { |
| 476 | $ppUrl = CRM_Utils_System::url('civicrm/contact/view/contribution', |
| 477 | "reset=1&action=add&cid={$this->_contactID}&ppid={$payments['id']}&context=pledge&mode=live" |
| 478 | ); |
| 479 | } |
| 480 | else { |
| 481 | $ppUrl = CRM_Utils_System::url('civicrm/contact/view/contribution', |
| 482 | "reset=1&action=add&cid={$this->_contactID}&ppid={$payments['id']}&context=pledge" |
| 483 | ); |
| 484 | } |
| 485 | CRM_Core_Session::setStatus(ts('This contact has a pending or overdue pledge payment of %2 which is scheduled for %3. <a href="%1">Click here to enter a pledge payment</a>.', array( |
| 486 | 1 => $ppUrl, |
| 487 | 2 => $ppAmountDue, |
| 488 | 3 => $ppSchedDate |
| 489 | )), ts('Notice'), 'alert'); |
| 490 | } |
| 491 | } |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | public function unsetCreditCardFields($submittedValues) { |
| 497 | //Offline Contribution. |
| 498 | $unsetParams = array( |
| 499 | 'payment_processor_id', |
| 500 | "email-{$this->_bltID}", |
| 501 | 'hidden_buildCreditCard', |
| 502 | 'hidden_buildDirectDebit', |
| 503 | 'billing_first_name', |
| 504 | 'billing_middle_name', |
| 505 | 'billing_last_name', |
| 506 | 'street_address-5', |
| 507 | "city-{$this->_bltID}", |
| 508 | "state_province_id-{$this->_bltID}", |
| 509 | "postal_code-{$this->_bltID}", |
| 510 | "country_id-{$this->_bltID}", |
| 511 | 'credit_card_number', |
| 512 | 'cvv2', |
| 513 | 'credit_card_exp_date', |
| 514 | 'credit_card_type', |
| 515 | ); |
| 516 | foreach ($unsetParams as $key) { |
| 517 | if (isset($submittedValues[$key])) { |
| 518 | unset($submittedValues[$key]); |
| 519 | } |
| 520 | } |
| 521 | return $submittedValues; |
| 522 | } |
| 523 | |
| 524 | } |