From 5187a13c4d0e4278bb9b18f9fb327b2407766dff Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Sun, 16 May 2021 14:01:49 +1200 Subject: [PATCH] Duplicate & deprecate processMembership Function only called from one place + tests. Duplicate to where it is called & deprecated as it seems civimobile may be using this (in an unsupported way but we shouldn't actively break it since we are aware of that) --- CRM/Contribute/Form/Contribution/Confirm.php | 223 +++++++++++++++++- CRM/Member/BAO/Membership.php | 3 + .../phpunit/CRM/Member/BAO/MembershipTest.php | 4 +- 3 files changed, 227 insertions(+), 3 deletions(-) diff --git a/CRM/Contribute/Form/Contribution/Confirm.php b/CRM/Contribute/Form/Contribution/Confirm.php index 736d2715ae..abcef50666 100644 --- a/CRM/Contribute/Form/Contribution/Confirm.php +++ b/CRM/Contribute/Form/Contribution/Confirm.php @@ -1704,7 +1704,7 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr $pending = FALSE; } - [$membership, $renewalMode, $dates] = CRM_Member_BAO_Membership::processMembership( + [$membership, $renewalMode, $dates] = CRM_Contribute_Form_Contribution_Confirm::legacyProcessMembership( $contactID, $memType, $isTest, date('YmdHis'), $membershipParams['cms_contactID'] ?? NULL, $customFieldsFormatted, @@ -2886,4 +2886,225 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr ); } + /** + * Interim function for processing memberships - this is being refactored out of existence. + * + * @param int $contactID + * @param int $membershipTypeID + * @param bool $is_test + * @param string $changeToday + * @param int $modifiedID + * @param $customFieldsFormatted + * @param $numRenewTerms + * @param int $membershipID + * @param $pending + * @param int $contributionRecurID + * @param $membershipSource + * @param $isPayLater + * @param array $memParams + * @param null|CRM_Contribute_BAO_Contribution $contribution + * @param array $lineItems + * + * @return array + * @throws \CRM_Core_Exception + * @throws \CiviCRM_API3_Exception + */ + public static function legacyProcessMembership($contactID, $membershipTypeID, $is_test, $changeToday, $modifiedID, $customFieldsFormatted, $numRenewTerms, $membershipID, $pending, $contributionRecurID, $membershipSource, $isPayLater, $memParams = [], $contribution = NULL, $lineItems = []) { + $renewalMode = $updateStatusId = FALSE; + $allStatus = CRM_Member_PseudoConstant::membershipStatus(); + $format = '%Y%m%d'; + $statusFormat = '%Y-%m-%d'; + $membershipTypeDetails = CRM_Member_BAO_MembershipType::getMembershipType($membershipTypeID); + $dates = []; + $ids = []; + + // CRM-7297 - allow membership type to be be changed during renewal so long as the parent org of new membershipType + // is the same as the parent org of an existing membership of the contact + $currentMembership = CRM_Member_BAO_Membership::getContactMembership($contactID, $membershipTypeID, + $is_test, $membershipID, TRUE + ); + if ($currentMembership) { + $renewalMode = TRUE; + + // Do NOT do anything. + //1. membership with status : PENDING/CANCELLED (CRM-2395) + //2. Paylater/IPN renew. CRM-4556. + if ($pending || in_array($currentMembership['status_id'], [ + array_search('Pending', $allStatus), + // CRM-15475 + array_search('Cancelled', CRM_Member_PseudoConstant::membershipStatus(NULL, " name = 'Cancelled' ", 'name', FALSE, TRUE)), + ])) { + + $memParams = array_merge([ + 'id' => $currentMembership['id'], + 'contribution' => $contribution, + 'status_id' => $currentMembership['status_id'], + 'start_date' => $currentMembership['start_date'], + 'end_date' => $currentMembership['end_date'], + 'line_item' => $lineItems, + 'join_date' => $currentMembership['join_date'], + 'membership_type_id' => $membershipTypeID, + 'max_related' => !empty($membershipTypeDetails['max_related']) ? $membershipTypeDetails['max_related'] : NULL, + 'membership_activity_status' => ($pending || $isPayLater) ? 'Scheduled' : 'Completed', + ], $memParams); + if ($contributionRecurID) { + $memParams['contribution_recur_id'] = $contributionRecurID; + } + + $membership = CRM_Member_BAO_Membership::create($memParams); + return [$membership, $renewalMode, $dates]; + } + + // Check and fix the membership if it is STALE + CRM_Member_BAO_Membership::fixMembershipStatusBeforeRenew($currentMembership, $changeToday); + + // Now Renew the membership + if (!$currentMembership['is_current_member']) { + // membership is not CURRENT + + // CRM-7297 Membership Upsell - calculate dates based on new membership type + $dates = CRM_Member_BAO_MembershipType::getRenewalDatesForMembershipType($currentMembership['id'], + $changeToday, + $membershipTypeID, + $numRenewTerms + ); + + $currentMembership['join_date'] = CRM_Utils_Date::customFormat($currentMembership['join_date'], $format); + foreach (['start_date', 'end_date'] as $dateType) { + $currentMembership[$dateType] = $dates[$dateType] ?? NULL; + } + $currentMembership['is_test'] = $is_test; + + if (!empty($membershipSource)) { + $currentMembership['source'] = $membershipSource; + } + + if (!empty($currentMembership['id'])) { + $ids['membership'] = $currentMembership['id']; + } + $memParams = array_merge($currentMembership, $memParams); + $memParams['membership_type_id'] = $membershipTypeID; + + //set the log start date. + $memParams['log_start_date'] = CRM_Utils_Date::customFormat($dates['log_start_date'], $format); + } + else { + + // CURRENT Membership + $membership = new CRM_Member_DAO_Membership(); + $membership->id = $currentMembership['id']; + $membership->find(TRUE); + // CRM-7297 Membership Upsell - calculate dates based on new membership type + $dates = CRM_Member_BAO_MembershipType::getRenewalDatesForMembershipType($membership->id, + $changeToday, + $membershipTypeID, + $numRenewTerms + ); + + // Insert renewed dates for CURRENT membership + $memParams['join_date'] = CRM_Utils_Date::isoToMysql($membership->join_date); + $memParams['start_date'] = CRM_Utils_Date::isoToMysql($membership->start_date); + $memParams['end_date'] = $dates['end_date'] ?? NULL; + $memParams['membership_type_id'] = $membershipTypeID; + + //set the log start date. + $memParams['log_start_date'] = CRM_Utils_Date::customFormat($dates['log_start_date'], $format); + + //CRM-18067 + if (!empty($membershipSource)) { + $memParams['source'] = $membershipSource; + } + elseif (empty($membership->source)) { + $memParams['source'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', + $currentMembership['id'], + 'source' + ); + } + + if (!empty($currentMembership['id'])) { + $ids['membership'] = $currentMembership['id']; + } + $memParams['membership_activity_status'] = ($pending || $isPayLater) ? 'Scheduled' : 'Completed'; + } + } + else { + // NEW Membership + $memParams = array_merge([ + 'contact_id' => $contactID, + 'membership_type_id' => $membershipTypeID, + ], $memParams); + + if (!$pending) { + $dates = CRM_Member_BAO_MembershipType::getDatesForMembershipType($membershipTypeID, NULL, NULL, NULL, $numRenewTerms); + + foreach (['join_date', 'start_date', 'end_date'] as $dateType) { + $memParams[$dateType] = $dates[$dateType] ?? NULL; + } + + $status = CRM_Member_BAO_MembershipStatus::getMembershipStatusByDate(CRM_Utils_Date::customFormat($dates['start_date'], + $statusFormat + ), + CRM_Utils_Date::customFormat($dates['end_date'], + $statusFormat + ), + CRM_Utils_Date::customFormat($dates['join_date'], + $statusFormat + ), + 'now', + TRUE, + $membershipTypeID, + $memParams + ); + $updateStatusId = $status['id'] ?? NULL; + } + else { + // if IPN/Pay-Later set status to: PENDING + $updateStatusId = array_search('Pending', $allStatus); + } + + if (!empty($membershipSource)) { + $memParams['source'] = $membershipSource; + } + $memParams['is_test'] = $is_test; + $memParams['is_pay_later'] = $isPayLater; + } + // Putting this in an IF is precautionary as it seems likely that it would be ignored if empty, but + // perhaps shouldn't be? + if ($contributionRecurID) { + $memParams['contribution_recur_id'] = $contributionRecurID; + } + //CRM-4555 + //if we decided status here and want to skip status + //calculation in create( ); then need to pass 'skipStatusCal'. + if ($updateStatusId) { + $memParams['status_id'] = $updateStatusId; + $memParams['skipStatusCal'] = TRUE; + } + + //since we are renewing, + //make status override false. + $memParams['is_override'] = FALSE; + + //CRM-4027, create log w/ individual contact. + if ($modifiedID) { + // @todo this param is likely unused now. + $memParams['is_for_organization'] = TRUE; + } + $params['modified_id'] = $modifiedID ?? $contactID; + + $memParams['contribution'] = $contribution; + $memParams['custom'] = $customFieldsFormatted; + // Load all line items & process all in membership. Don't do in contribution. + // Relevant tests in api_v3_ContributionPageTest. + $memParams['line_item'] = $lineItems; + // @todo stop passing $ids (membership and userId may be set by this point) + $membership = CRM_Member_BAO_Membership::create($memParams, $ids); + + // not sure why this statement is here, seems quite odd :( - Lobo: 12/26/2010 + // related to: http://forum.civicrm.org/index.php/topic,11416.msg49072.html#msg49072 + $membership->find(TRUE); + + return [$membership, $renewalMode, $dates]; + } + } diff --git a/CRM/Member/BAO/Membership.php b/CRM/Member/BAO/Membership.php index 490fa993f4..f03cc9af7d 100644 --- a/CRM/Member/BAO/Membership.php +++ b/CRM/Member/BAO/Membership.php @@ -1762,6 +1762,8 @@ INNER JOIN civicrm_contact contact ON ( contact.id = membership.contact_id AND } /** + * @deprecated + * * @param int $contactID * @param int $membershipTypeID * @param bool $is_test @@ -1783,6 +1785,7 @@ INNER JOIN civicrm_contact contact ON ( contact.id = membership.contact_id AND * @throws \CiviCRM_API3_Exception */ public static function processMembership($contactID, $membershipTypeID, $is_test, $changeToday, $modifiedID, $customFieldsFormatted, $numRenewTerms, $membershipID, $pending, $contributionRecurID, $membershipSource, $isPayLater, $memParams = [], $contribution = NULL, $lineItems = []) { + CRM_Core_Error::deprecatedFunctionWarning('use the order api, BAO functions should only be called from unit tested core code.'); $renewalMode = $updateStatusId = FALSE; $allStatus = CRM_Member_PseudoConstant::membershipStatus(); $format = '%Y%m%d'; diff --git a/tests/phpunit/CRM/Member/BAO/MembershipTest.php b/tests/phpunit/CRM/Member/BAO/MembershipTest.php index dbbcaa2a83..631338a5a0 100644 --- a/tests/phpunit/CRM/Member/BAO/MembershipTest.php +++ b/tests/phpunit/CRM/Member/BAO/MembershipTest.php @@ -480,7 +480,7 @@ class CRM_Member_BAO_MembershipTest extends CiviUnitTestCase { $config->keyDisable = TRUE; $isTestMembership = 0; - [$MembershipRenew] = CRM_Member_BAO_Membership::processMembership( + [$MembershipRenew] = CRM_Contribute_Form_Contribution_Confirm::legacyProcessMembership( $contactId, $this->_membershipTypeID, $isTestMembership, @@ -549,7 +549,7 @@ class CRM_Member_BAO_MembershipTest extends CiviUnitTestCase { 'Database checked on membership log record.' ); - [$MembershipRenew] = CRM_Member_BAO_Membership::processMembership( + [$MembershipRenew] = CRM_Contribute_Form_Contribution_Confirm::legacyProcessMembership( $contactId, $this->_membershipTypeID, FALSE, -- 2.25.1