/** * Added for CRM-12695 * Based on the contactID provided * add/update membership(s) to related contacts * * @param int $contactID */ public static function addMembershipToRealtedContacts($contactID) { $dao = new CRM_Member_DAO_Membership(); $dao->contact_id = $contactID; $dao->is_test = 0; $dao->find(); //checks membership of contact itself while ($dao->fetch()) { $relationshipTypeId = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $dao->membership_type_id, 'relationship_type_id', 'id'); if ($relationshipTypeId) { $membershipParams = array('id' => $dao->id, 'contact_id' => $dao->contact_id, 'membership_type_id' => $dao->membership_type_id, 'join_date' => CRM_Utils_Date::isoToMysql($dao->join_date), 'start_date' => CRM_Utils_Date::isoToMysql($dao->start_date), 'end_date' => CRM_Utils_Date::isoToMysql($dao->end_date), 'source' => $dao->source, 'status_id' => $dao->status_id); // create/update membership(s) for related contact(s) CRM_Member_BAO_Membership::createRelatedMemberships($membershipParams, $dao); } // end of if relationshipTypeId } }
/** * Complete an order. * * Do not call this directly - use the contribution.completetransaction api as this function is being refactored. * * Currently overloaded to complete a transaction & repeat a transaction - fix! * * Moving it out of the BaseIPN class is just the first step. * * @param array $input * @param array $ids * @param array $objects * @param CRM_Core_Transaction $transaction * @param int $recur * @param CRM_Contribute_BAO_Contribution $contribution * @param bool $isRecurring * Duplication of param needs review. Only used by AuthorizeNetIPN * @param int $isFirstOrLastRecurringPayment * Deprecated param only used by AuthorizeNetIPN. */ public static function completeOrder(&$input, &$ids, $objects, $transaction, $recur, $contribution, $isRecurring, $isFirstOrLastRecurringPayment) { $primaryContributionID = isset($contribution->id) ? $contribution->id : $objects['first_contribution']->id; // The previous details are used when calculating line items so keep it before any code that 'does something' if (!empty($contribution->id)) { $input['prevContribution'] = CRM_Contribute_BAO_Contribution::getValues(array('id' => $contribution->id), CRM_Core_DAO::$_nullArray, CRM_Core_DAO::$_nullArray); } $inputContributionWhiteList = array('fee_amount', 'net_amount', 'trxn_id', 'check_number', 'payment_instrument_id', 'is_test', 'campaign_id', 'receive_date'); if (self::isSingleLineItem($primaryContributionID)) { $inputContributionWhiteList[] = 'financial_type_id'; } $participant = CRM_Utils_Array::value('participant', $objects); $memberships = CRM_Utils_Array::value('membership', $objects); $recurContrib = CRM_Utils_Array::value('contributionRecur', $objects); $event = CRM_Utils_Array::value('event', $objects); $contributionParams = array_merge(array('contribution_status_id' => 'Completed', 'source' => self::getRecurringContributionDescription($contribution, $event)), array_intersect_key($input, array_fill_keys($inputContributionWhiteList, 1))); if (!empty($recurContrib->id)) { $contributionParams['contribution_recur_id'] = $recurContrib->id; } $changeDate = CRM_Utils_Array::value('trxn_date', $input, date('YmdHis')); if (empty($contributionParams['receive_date']) && $changeDate) { $contributionParams['receive_date'] = $changeDate; } self::repeatTransaction($contribution, $input, $contributionParams); $contributionParams['financial_type_id'] = $contribution->financial_type_id; if (is_numeric($memberships)) { $memberships = array($objects['membership']); } $values = array(); if (isset($input['is_email_receipt'])) { $values['is_email_receipt'] = $input['is_email_receipt']; } if ($input['component'] == 'contribute') { if ($contribution->contribution_page_id) { // Figure out what we gain from this. CRM_Contribute_BAO_ContributionPage::setValues($contribution->contribution_page_id, $values); } elseif ($recurContrib && $recurContrib->id) { $values['amount'] = $recurContrib->amount; $values['financial_type_id'] = $objects['contributionType']->id; $values['title'] = $source = ts('Offline Recurring Contribution'); $domainValues = CRM_Core_BAO_Domain::getNameAndEmail(); $values['receipt_from_name'] = $domainValues[0]; $values['receipt_from_email'] = $domainValues[1]; } if ($recurContrib && $recurContrib->id && !isset($input['is_email_receipt'])) { //CRM-13273 - is_email_receipt setting on recurring contribution should take precedence over contribution page setting // but CRM-16124 if $input['is_email_receipt'] is set then that should not be overridden. $values['is_email_receipt'] = $recurContrib->is_email_receipt; } if (!empty($values['is_email_receipt'])) { $contributionParams['receipt_date'] = $changeDate; } if (!empty($memberships)) { foreach ($memberships as $membershipTypeIdKey => $membership) { if ($membership) { $membershipParams = array('id' => $membership->id, 'contact_id' => $membership->contact_id, 'is_test' => $membership->is_test, 'membership_type_id' => $membership->membership_type_id); $currentMembership = CRM_Member_BAO_Membership::getContactMembership($membershipParams['contact_id'], $membershipParams['membership_type_id'], $membershipParams['is_test'], $membershipParams['id']); // CRM-8141 update the membership type with the value recorded in log when membership created/renewed // this picks up membership type changes during renewals $sql = "\nSELECT membership_type_id\nFROM civicrm_membership_log\nWHERE membership_id={$membershipParams['id']}\nORDER BY id DESC\nLIMIT 1;"; $dao = CRM_Core_DAO::executeQuery($sql); if ($dao->fetch()) { if (!empty($dao->membership_type_id)) { $membershipParams['membership_type_id'] = $dao->membership_type_id; } } $dao->free(); $membershipParams['num_terms'] = $contribution->getNumTermsByContributionAndMembershipType($membershipParams['membership_type_id'], $primaryContributionID); $dates = array_fill_keys(array('join_date', 'start_date', 'end_date'), NULL); if ($currentMembership) { /* * Fixed FOR CRM-4433 * In BAO/Membership.php(renewMembership function), we skip the extend membership date and status * when Contribution mode is notify and membership is for renewal ) */ 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; //CRM-17723 - reset static $relatedContactIds array() $var = TRUE; CRM_Member_BAO_Membership::createRelatedMemberships($var, $var, TRUE); civicrm_api3('Membership', 'create', $membershipParams); } } } } else { if (empty($input['IAmAHorribleNastyBeyondExcusableHackInTheCRMEventFORMTaskClassThatNeedsToBERemoved'])) { if ($event->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); } }
/** * Clean up financial entities after financial tests (so we remember to get all the tables :-)) */ public function quickCleanUpFinancialEntities() { $tablesToTruncate = array('civicrm_activity', 'civicrm_activity_contact', 'civicrm_contribution', 'civicrm_contribution_soft', 'civicrm_contribution_product', 'civicrm_financial_trxn', 'civicrm_financial_item', 'civicrm_contribution_recur', 'civicrm_line_item', 'civicrm_contribution_page', 'civicrm_payment_processor', 'civicrm_entity_financial_trxn', 'civicrm_membership', 'civicrm_membership_type', 'civicrm_membership_payment', 'civicrm_membership_log', 'civicrm_membership_block', 'civicrm_event', 'civicrm_participant', 'civicrm_participant_payment', 'civicrm_pledge', 'civicrm_pledge_payment', 'civicrm_price_set_entity', 'civicrm_price_field_value', 'civicrm_price_field'); $this->quickCleanup($tablesToTruncate); CRM_Core_DAO::executeQuery("DELETE FROM civicrm_membership_status WHERE name NOT IN('New', 'Current', 'Grace', 'Expired', 'Pending', 'Cancelled', 'Deceased')"); $this->restoreDefaultPriceSetConfig(); $var = TRUE; CRM_Member_BAO_Membership::createRelatedMemberships($var, $var, TRUE); System::singleton()->flushProcessors(); }