/**
* View details of a recurring contribution.
*/
public function view()
{
$recur = new CRM_Contribute_DAO_ContributionRecur();
$recur->id = $this->_id;
if ($recur->find(TRUE)) {
$values = array();
CRM_Core_DAO::storeValues($recur, $values);
// if there is a payment processor ID, get the name of the payment processor
if (!empty($values['payment_processor_id'])) {
$values['payment_processor'] = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_PaymentProcessor', $values['payment_processor_id'], 'name');
}
$idFields = array('contribution_status_id', 'campaign_id');
if (CRM_Contribute_BAO_ContributionRecur::supportsFinancialTypeChange($values['id'])) {
$idFields[] = 'financial_type_id';
}
foreach ($idFields as $idField) {
if (!empty($values[$idField])) {
$values[substr($idField, 0, -3)] = CRM_Core_PseudoConstant::getLabel('CRM_Contribute_BAO_ContributionRecur', $idField, $values[$idField]);
}
}
// Get financial type name
if (!empty($values['financial_type_id'])) {
$values['financial_type_name'] = CRM_Core_PseudoConstant::getLabel('CRM_Contribute_BAO_Contribution', 'financial_type_id', $values['financial_type_id']);
}
// Get Paid By label
if (!empty($values['payment_instrument_id'])) {
$values['payment_instrument'] = CRM_Core_OptionGroup::getLabel('payment_instrument', $values['payment_instrument_id']);
}
$this->assign('recur', $values);
$this->assign('customDataType', 'ContributionRecur');
$groupTree = CRM_Core_BAO_CustomGroup::getTree('ContributionRecur', $this, $this->_id);
CRM_Core_BAO_CustomGroup::buildCustomDataView($this, $groupTree);
}
}
/**
* takes an associative array and creates a contribution object
*
* the function extract all the params it needs to initialize the create a
* contribution object. the params array could contain additional unused name/value
* pairs
*
* @param array $params (reference ) an assoc array of name/value pairs
* @param array $ids the array that holds all the db ids
*
* @return object CRM_Contribute_BAO_Contribution object
* @access public
* @static
*/
static function add(&$params, &$ids)
{
$duplicates = array();
if (self::checkDuplicate($params, $duplicates)) {
$error =& CRM_Core_Error::singleton();
$d = implode(', ', $duplicates);
$error->push(CRM_Core_Error::DUPLICATE_CONTRIBUTION, 'Fatal', array($d), "Found matching contribution(s): {$d}");
return $error;
}
$recurring = new CRM_Contribute_BAO_ContributionRecur();
$recurring->copyValues($params);
$recurring->id = CRM_Utils_Array::value('contribution', $ids);
// set currency for CRM-1496
if (!isset($recurring->currency)) {
$config =& CRM_Core_Config::singleton();
$recurring->currency = $config->defaultCurrency;
}
return $recurring->save();
}
/**
* View details of a recurring contribution.
*/
public function view()
{
$recur = new CRM_Contribute_DAO_ContributionRecur();
$recur->id = $this->_id;
if ($recur->find(TRUE)) {
$values = array();
CRM_Core_DAO::storeValues($recur, $values);
// if there is a payment processor ID, get the name of the payment processor
if (!empty($values['payment_processor_id'])) {
$values['payment_processor'] = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_PaymentProcessor', $values['payment_processor_id'], 'name');
}
$idFields = array('contribution_status_id', 'campaign_id');
if (CRM_Contribute_BAO_ContributionRecur::supportsFinancialTypeChange($values['id'])) {
$idFields[] = 'financial_type_id';
}
foreach ($idFields as $idField) {
if (!empty($values[$idField])) {
$values[substr($idField, 0, -3)] = CRM_Core_PseudoConstant::getLabel('CRM_Contribute_BAO_ContributionRecur', $idField, $values[$idField]);
}
}
$this->assign('recur', $values);
}
}
/**
* In case user cancel recurring contribution,
* When we get the control back from payment gate way
* lets delete the recurring and related contribution.
*
**/
public function cancelRecurring()
{
$isCancel = CRM_Utils_Request::retrieve('cancel', 'Boolean', CRM_Core_DAO::$_nullObject);
if ($isCancel) {
$isRecur = CRM_Utils_Request::retrieve('isRecur', 'Boolean', CRM_Core_DAO::$_nullObject);
$recurId = CRM_Utils_Request::retrieve('recurId', 'Positive', CRM_Core_DAO::$_nullObject);
//clean db for recurring contribution.
if ($isRecur && $recurId) {
CRM_Contribute_BAO_ContributionRecur::deleteRecurContribution($recurId);
}
$contribId = CRM_Utils_Request::retrieve('contribId', 'Positive', CRM_Core_DAO::$_nullObject);
if ($contribId) {
CRM_Contribute_BAO_Contribution::deleteContribution($contribId);
}
}
}
//.........这里部分代码省略.........
}
}
if (!empty($priceFieldIDS)) {
$ids = implode(',', $priceFieldIDS);
$priceFieldIDS['id'] = $fields['priceSetId'];
$self->set('memberPriceFieldIDS', $priceFieldIDS);
$count = CRM_Price_BAO_PriceSet::getMembershipCount($ids);
foreach ($count as $id => $occurrence) {
if ($occurrence > 1) {
$errors['_qf_default'] = ts('You have selected multiple memberships for the same organization or entity. Please review your selections and choose only one membership per entity. Contact the site administrator if you need assistance.');
}
}
}
if (empty($priceFieldMemTypes) && $self->_membershipBlock['is_required'] == 1) {
$errors['_qf_default'] = ts('Please select at least one membership option.');
}
}
CRM_Price_BAO_PriceSet::processAmount($self->_values['fee'], $fields, $lineItem);
if ($fields['amount'] < 0) {
$errors['_qf_default'] = ts('Contribution can not be less than zero. Please select the options accordingly');
}
$amount = $fields['amount'];
}
if (isset($fields['selectProduct']) && $fields['selectProduct'] != 'no_thanks') {
$productDAO = new CRM_Contribute_DAO_Product();
$productDAO->id = $fields['selectProduct'];
$productDAO->find(TRUE);
$min_amount = $productDAO->min_contribution;
if ($amount < $min_amount) {
$errors['selectProduct'] = ts('The premium you have selected requires a minimum contribution of %1', array(1 => CRM_Utils_Money::format($min_amount)));
CRM_Core_Session::setStatus($errors['selectProduct']);
}
}
//CRM-16285 - Function to handle validation errors on form, for recurring contribution field.
CRM_Contribute_BAO_ContributionRecur::validateRecurContribution($fields, $files, $self, $errors);
if (!empty($fields['is_recur']) && CRM_Utils_Array::value('payment_processor_id', $fields) == 0) {
$errors['_qf_default'] = ts('You cannot set up a recurring contribution if you are not paying online by credit card.');
}
// validate PCP fields - if not anonymous, we need a nick name value
if ($self->_pcpId && !empty($fields['pcp_display_in_roll']) && CRM_Utils_Array::value('pcp_is_anonymous', $fields) == 0 && CRM_Utils_Array::value('pcp_roll_nickname', $fields) == '') {
$errors['pcp_roll_nickname'] = ts('Please enter a name to include in the Honor Roll, or select \'contribute anonymously\'.');
}
// return if this is express mode
$config = CRM_Core_Config::singleton();
if ($self->_paymentProcessor && $self->_paymentProcessor['billing_mode'] & CRM_Core_Payment::BILLING_MODE_BUTTON) {
if (!empty($fields[$self->_expressButtonName . '_x']) || !empty($fields[$self->_expressButtonName . '_y']) || CRM_Utils_Array::value($self->_expressButtonName, $fields)) {
return $errors;
}
}
//validate the pledge fields.
if (!empty($self->_values['pledge_block_id'])) {
//validation for pledge payment.
if (!empty($self->_values['pledge_id'])) {
if (empty($fields['pledge_amount'])) {
$errors['pledge_amount'] = ts('At least one payment option needs to be checked.');
}
} elseif (!empty($fields['is_pledge'])) {
if (CRM_Utils_Rule::positiveInteger(CRM_Utils_Array::value('pledge_installments', $fields)) == FALSE) {
$errors['pledge_installments'] = ts('Please enter a valid number of pledge installments.');
} else {
if (CRM_Utils_Array::value('pledge_installments', $fields) == NULL) {
$errors['pledge_installments'] = ts('Pledge Installments is required field.');
} elseif (CRM_Utils_Array::value('pledge_installments', $fields) == 1) {
$errors['pledge_installments'] = ts('Pledges consist of multiple scheduled payments. Select one-time contribution if you want to make your gift in a single payment.');
} elseif (CRM_Utils_Array::value('pledge_installments', $fields) == 0) {
$errors['pledge_installments'] = ts('Pledge Installments field must be > 1.');
}
}
//validation for Pledge Frequency Interval.
if (CRM_Utils_Rule::positiveInteger(CRM_Utils_Array::value('pledge_frequency_interval', $fields)) == FALSE) {
$errors['pledge_frequency_interval'] = ts('Please enter a valid Pledge Frequency Interval.');
} else {
if (CRM_Utils_Array::value('pledge_frequency_interval', $fields) == NULL) {
$errors['pledge_frequency_interval'] = ts('Pledge Frequency Interval. is required field.');
} elseif (CRM_Utils_Array::value('pledge_frequency_interval', $fields) == 0) {
$errors['pledge_frequency_interval'] = ts('Pledge frequency interval field must be > 0');
}
}
}
}
// if the user has chosen a free membership or the amount is less than zero
// i.e. we don't need to validate payment related fields or profiles.
if ((double) $amount <= 0.0) {
return $errors;
}
if (CRM_Utils_Array::value('payment_processor_id', $fields) == NULL) {
$errors['payment_processor_id'] = ts('Payment Method is a required field.');
} else {
CRM_Core_Payment_Form::validatePaymentInstrument($fields['payment_processor_id'], $fields, $errors, !$self->_isBillingAddressRequiredForPayLater ? NULL : 'billing');
}
foreach (CRM_Contact_BAO_Contact::$_greetingTypes as $greeting) {
if ($greetingType = CRM_Utils_Array::value($greeting, $fields)) {
$customizedValue = CRM_Core_OptionGroup::getValue($greeting, 'Customized', 'name');
if ($customizedValue == $greetingType && empty($fielse[$greeting . '_custom'])) {
$errors[$greeting . '_custom'] = ts('Custom %1 is a required field if %1 is of type Customized.', array(1 => ucwords(str_replace('_', " ", $greeting))));
}
}
}
return empty($errors) ? TRUE : $errors;
}
/**
* Global form rule.
*
* @param array $fields
* The input form values.
* @param array $files
* The uploaded files if any.
* @param $self
*
* @return bool|array
* true if no errors, else array of errors
*/
public static function formRule($fields, $files, $self)
{
$errors = array();
// Check for Credit Card Contribution.
if ($self->_mode) {
if (empty($fields['payment_processor_id'])) {
$errors['payment_processor_id'] = ts('Payment Processor is a required field.');
} else {
// validate payment instrument (e.g. credit card number)
CRM_Core_Payment_Form::validatePaymentInstrument($fields['payment_processor_id'], $fields, $errors, NULL);
}
}
// Do the amount validations.
if (empty($fields['total_amount']) && empty($self->_lineItems)) {
if ($priceSetId = CRM_Utils_Array::value('price_set_id', $fields)) {
CRM_Price_BAO_PriceField::priceSetValidation($priceSetId, $fields, $errors);
}
}
$softErrors = CRM_Contribute_Form_SoftCredit::formRule($fields, $errors, $self);
if (!empty($fields['total_amount']) && (!empty($fields['net_amount']) || !empty($fields['fee_amount']))) {
$sum = CRM_Utils_Rule::cleanMoney($fields['net_amount']) + CRM_Utils_Rule::cleanMoney($fields['fee_amount']);
// For taxable contribution we need to deduct taxable amount from
// (net amount + fee amount) before comparing it with total amount
if (!empty($self->_values['tax_amount'])) {
$componentDetails = CRM_Contribute_BAO_Contribution::getComponentDetails($self->_id);
if (!(CRM_Utils_Array::value('membership', $componentDetails) || CRM_Utils_Array::value('participant', $componentDetails))) {
$sum = CRM_Utils_Money::format($sum - $self->_values['tax_amount'], NULL, '%a');
}
}
if (CRM_Utils_Rule::cleanMoney($fields['total_amount']) != $sum) {
$errors['total_amount'] = ts('The sum of fee amount and net amount must be equal to total amount');
}
}
//CRM-16285 - Function to handle validation errors on form, for recurring contribution field.
CRM_Contribute_BAO_ContributionRecur::validateRecurContribution($fields, $files, $self, $errors);
// Form rule for status http://wiki.civicrm.org/confluence/display/CRM/CiviAccounts+4.3+Data+Flow
if ($self->_action & CRM_Core_Action::UPDATE && $self->_id && $self->_values['contribution_status_id'] != $fields['contribution_status_id']) {
CRM_Contribute_BAO_Contribution::checkStatusValidation($self->_values, $fields, $errors);
}
// CRM-16015, add form-rule to restrict change of financial type if using price field of different financial type
if ($self->_action & CRM_Core_Action::UPDATE && $self->_id && $self->_values['financial_type_id'] != $fields['financial_type_id']) {
CRM_Contribute_BAO_Contribution::checkFinancialTypeChange(NULL, $self->_id, $errors);
}
//FIXME FOR NEW DATA FLOW http://wiki.civicrm.org/confluence/display/CRM/CiviAccounts+4.3+Data+Flow
if (!empty($fields['fee_amount']) && !empty($fields['financial_type_id']) && ($financialType = CRM_Contribute_BAO_Contribution::validateFinancialType($fields['financial_type_id']))) {
$errors['financial_type_id'] = ts("Financial Account of account relationship of 'Expense Account is' is not configured for Financial Type : ") . $financialType;
}
// $trxn_id must be unique CRM-13919
if (!empty($fields['trxn_id'])) {
$queryParams = array(1 => array($fields['trxn_id'], 'String'));
$query = 'select count(*) from civicrm_contribution where trxn_id = %1';
if ($self->_id) {
$queryParams[2] = array((int) $self->_id, 'Integer');
$query .= ' and id !=%2';
}
$tCnt = CRM_Core_DAO::singleValueQuery($query, $queryParams);
if ($tCnt) {
$errors['trxn_id'] = ts('Transaction ID\'s must be unique. Transaction \'%1\' already exists in your database.', array(1 => $fields['trxn_id']));
}
}
$errors = array_merge($errors, $softErrors);
return $errors;
}
//.........这里部分代码省略.........
*/
CRM_Member_BAO_Membership::fixMembershipStatusBeforeRenew($currentMembership, $changeDate);
// @todo - we should pass membership_type_id instead of null here but not
// adding as not sure of testing
$dates = CRM_Member_BAO_MembershipType::getRenewalDatesForMembershipType($membershipParams['id'], $changeDate, NULL, $membershipParams['num_terms']);
$dates['join_date'] = $currentMembership['join_date'];
}
//get the status for membership.
$calcStatus = CRM_Member_BAO_MembershipStatus::getMembershipStatusByDate($dates['start_date'], $dates['end_date'], $dates['join_date'], 'today', TRUE, $membershipParams['membership_type_id'], $membershipParams);
$membershipParams['status_id'] = CRM_Utils_Array::value('id', $calcStatus, 'New');
//we might be renewing membership,
//so make status override false.
$membershipParams['is_override'] = FALSE;
civicrm_api3('Membership', 'create', $membershipParams);
//update related Memberships.
CRM_Member_BAO_Membership::updateRelatedMemberships($membership->id, $membershipParams);
}
}
}
} else {
if (empty($input['IAmAHorribleNastyBeyondExcusableHackInTheCRMEventFORMTaskClassThatNeedsToBERemoved'])) {
$eventDetail = civicrm_api3('Event', 'getsingle', array('id' => $objects['event']->id));
$contributionParams['source'] = ts('Online Event Registration') . ': ' . $eventDetail['title'];
if ($eventDetail['is_email_confirm']) {
// @todo this should be set by the function that sends the mail after sending.
$contributionParams['receipt_date'] = $changeDate;
}
$participantParams['id'] = $participant->id;
$participantParams['status_id'] = 'Registered';
civicrm_api3('Participant', 'create', $participantParams);
}
}
$contributionParams['id'] = $contribution->id;
civicrm_api3('Contribution', 'create', $contributionParams);
// Add new soft credit against current $contribution.
if (CRM_Utils_Array::value('contributionRecur', $objects) && $objects['contributionRecur']->id) {
CRM_Contribute_BAO_ContributionRecur::addrecurSoftCredit($objects['contributionRecur']->id, $contribution->id);
}
$paymentProcessorId = '';
if (isset($objects['paymentProcessor'])) {
if (is_array($objects['paymentProcessor'])) {
$paymentProcessorId = $objects['paymentProcessor']['id'];
} else {
$paymentProcessorId = $objects['paymentProcessor']->id;
}
}
$contributionStatuses = CRM_Core_PseudoConstant::get('CRM_Contribute_DAO_Contribution', 'contribution_status_id', array('labelColumn' => 'name', 'flip' => 1));
if (empty($input['prevContribution']) && $paymentProcessorId || !$input['prevContribution']->is_pay_later && $input['prevContribution']->contribution_status_id == $contributionStatuses['Pending']) {
$input['payment_processor'] = $paymentProcessorId;
}
$input['contribution_status_id'] = $contributionStatuses['Completed'];
$input['total_amount'] = $input['amount'];
$input['contribution'] = $contribution;
$input['financial_type_id'] = $contribution->financial_type_id;
if (!empty($contribution->_relatedObjects['participant'])) {
$input['contribution_mode'] = 'participant';
$input['participant_id'] = $contribution->_relatedObjects['participant']->id;
$input['skipLineItem'] = 1;
} elseif (!empty($contribution->_relatedObjects['membership'])) {
$input['skipLineItem'] = TRUE;
$input['contribution_mode'] = 'membership';
}
//@todo writing a unit test I was unable to create a scenario where this line did not fatal on second
// and subsequent payments. In this case the line items are created at
// CRM_Contribute_BAO_ContributionRecur::addRecurLineItems
// and since the contribution is saved prior to this line there is always a contribution-id,
// however there is never a prevContribution (which appears to mean original contribution not previous
// contribution - or preUpdateContributionObject most accurately)
// so, this is always called & only appears to succeed when prevContribution exists - which appears
// to mean "are we updating an exisitng pending contribution"
//I was able to make the unit test complete as fataling here doesn't prevent
// the contribution being created - but activities would not be created or emails sent
CRM_Contribute_BAO_Contribution::recordFinancialAccounts($input, NULL);
CRM_Core_Error::debug_log_message("Contribution record updated successfully");
$transaction->commit();
CRM_Contribute_BAO_ContributionRecur::updateRecurLinkedPledge($contribution);
// create an activity record
if ($input['component'] == 'contribute') {
//CRM-4027
$targetContactID = NULL;
if (!empty($ids['related_contact'])) {
$targetContactID = $contribution->contact_id;
$contribution->contact_id = $ids['related_contact'];
}
CRM_Activity_BAO_Activity::addActivity($contribution, NULL, $targetContactID);
// event
} else {
CRM_Activity_BAO_Activity::addActivity($participant);
}
// CRM-9132 legacy behaviour was that receipts were sent out in all instances. Still sending
// when array_key 'is_email_receipt doesn't exist in case some instances where is needs setting haven't been set
if (!array_key_exists('is_email_receipt', $values) || $values['is_email_receipt'] == 1) {
self::sendMail($input, $ids, $objects['contribution'], $values, $recur, FALSE);
CRM_Core_Error::debug_log_message("Receipt sent");
}
CRM_Core_Error::debug_log_message("Success: Database updated");
if ($isRecurring) {
CRM_Contribute_BAO_ContributionRecur::sendRecurringStartOrEndNotification($ids, $recur, $isFirstOrLastRecurringPayment);
}
}
static function processAPIContribution($params)
{
if (empty($params) || array_key_exists('error', $params)) {
return false;
}
// add contact using dedupe rule
require_once 'CRM/Dedupe/Finder.php';
$dedupeParams = CRM_Dedupe_Finder::formatParams($params, 'Individual');
$dedupeParams['check_permission'] = false;
$dupeIds = CRM_Dedupe_Finder::dupesByParams($dedupeParams, 'Individual');
// if we find more than one contact, use the first one
if (CRM_Utils_Array::value(0, $dupeIds)) {
$params['contact_id'] = $dupeIds[0];
}
require_once 'CRM/Contact/BAO/Contact.php';
$contact = CRM_Contact_BAO_Contact::create($params);
if (!$contact->id) {
return false;
}
// only pass transaction params to contribution::create, if available
if (array_key_exists('transaction', $params)) {
$params = $params['transaction'];
$params['contact_id'] = $contact->id;
}
// handle contribution custom data
$customFields = CRM_Core_BAO_CustomField::getFields('Contribution', false, false, CRM_Utils_Array::value('contribution_type_id', $params));
$params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, $customFields, CRM_Utils_Array::value('id', $params, null), 'Contribution');
// create contribution
// if this is a recurring contribution then process it first
if ($params['trxn_type'] == 'subscrpayment') {
// see if a recurring record already exists
require_once 'CRM/Contribute/BAO/ContributionRecur.php';
$recurring = new CRM_Contribute_BAO_ContributionRecur();
$recurring->processor_id = $params['processor_id'];
if (!$recurring->find(true)) {
$recurring = new CRM_Contribute_BAO_ContributionRecur();
$recurring->invoice_id = $params['invoice_id'];
$recurring->find(true);
}
// This is the same thing the CiviCRM IPN handler does to handle
// subsequent recurring payments to avoid duplicate contribution
// errors due to invoice ID. See:
// ./CRM/Core/Payment/PayPalIPN.php:200
if ($recurring->id) {
$params['invoice_id'] = md5(uniqid(rand(), true));
}
$recurring->copyValues($params);
$recurring->save();
if (is_a($recurring, 'CRM_Core_Error')) {
return false;
} else {
$params['contribution_recur_id'] = $recurring->id;
}
}
require_once 'CRM/Contribute/BAO/Contribution.php';
$contribution =& CRM_Contribute_BAO_Contribution::create($params, CRM_Core_DAO::$_nullArray);
if (!$contribution->id) {
return false;
}
return true;
}
/**
* Test cancellation works per CRM-14986.
*
* We are checking for absence of error.
*/
public function testCancelRecur()
{
$contributionRecur = $this->callAPISuccess('contribution_recur', 'create', $this->_params);
CRM_Contribute_BAO_ContributionRecur::cancelRecurContribution($contributionRecur['id'], CRM_Core_DAO::$_nullObject);
}
请发表评论