From 5e8c8bd676f84320c8ef4a52ec40f68d312c2861 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Fri, 16 Jul 2021 19:07:03 +1200 Subject: [PATCH] Extract getCurrentMembership Note I've moved the fixmembershipStatusBeforeRenew here - it is just compensating for the possibility of the job not being run. On the Membership form we run it in preProcess but this feels like our first chance in this form --- CRM/Batch/Form/Entry.php | 54 ++++++++++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 8 deletions(-) diff --git a/CRM/Batch/Form/Entry.php b/CRM/Batch/Form/Entry.php index 99da9c98d8..5e0a7847a5 100644 --- a/CRM/Batch/Form/Entry.php +++ b/CRM/Batch/Form/Entry.php @@ -102,6 +102,11 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form { */ protected $currentRow = []; + /** + * @var array + */ + protected $currentRowExistingMembership; + /** * Get the contribution id for the current row. * @@ -131,6 +136,24 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form { return $this->currentRowMembershipID; } + /** + * Get the contact ID for the current row. + * + * @return int + */ + public function getCurrentRowContactID(): int { + return $this->currentRow['contact_id']; + } + + /** + * Get the membership type ID for the current row. + * + * @return int + */ + public function getCurrentRowMembershipTypeID(): int { + return $this->currentRow['membership_type_id']; + } + /** * Set the membership id for the current row. * @@ -751,6 +774,7 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form { $value['contact_id'] = $params['primary_contact_id'][$key]; $value = $this->standardiseRow($value); $this->currentRow = $value; + $this->currentRowExistingMembership = NULL; $this->currentRowIsRenewOption = (int) ($params['member_option'][$key] ?? 1); // update contact information @@ -1005,12 +1029,8 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form { $membershipTypeDetails = CRM_Member_BAO_MembershipType::getMembershipType($membershipTypeID); $ids = []; $isPayLater = NULL; + $currentMembership = $this->getCurrentMembership(); - // 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, - FALSE, NULL, TRUE - ); if ($currentMembership) { // Do NOT do anything. @@ -1036,9 +1056,6 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form { return CRM_Member_BAO_Membership::create($memParams); } - // 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 @@ -1232,4 +1249,25 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form { return $this->currentRowIsRenewOption === 2; } + /** + * Get any current membership for the current row contact, for the same member organization. + * + * @return array|bool + * + * @throws \CiviCRM_API3_Exception + * @throws \CRM_Core_Exception + */ + protected function getCurrentMembership() { + if (!isset($this->currentRowExistingMembership)) { + // 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 + $this->currentRowExistingMembership = CRM_Member_BAO_Membership::getContactMembership($this->getCurrentRowContactID(), $this->getCurrentRowMembershipTypeID(), + FALSE, NULL, TRUE + ); + // Check and fix the membership if it is STALE + CRM_Member_BAO_Membership::fixMembershipStatusBeforeRenew($this->currentRowExistingMembership); + } + return $this->currentRowExistingMembership; + } + } -- 2.25.1