Skip to content

Commit

Permalink
Merge pull request civicrm#29730 from eileenmcnaughton/membership_sunil
Browse files Browse the repository at this point in the history
dev/core#3752 Fix issue with price set membership terms
  • Loading branch information
eileenmcnaughton authored Apr 1, 2024
2 parents 0d8c732 + b84373b commit 29ae69c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
6 changes: 4 additions & 2 deletions CRM/Member/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ class CRM_Member_Form extends CRM_Contribute_Form_AbstractEditPayment {
* Price set ID configured for the form.
*
* @var int
*
* @deprecated use getPriceSetID()
*/
public $_priceSetId;

Expand Down Expand Up @@ -602,8 +604,8 @@ protected function setPriceSetParameters(array $formValues): array {
*
* @return bool
*/
private function isQuickConfig(): bool {
return $this->_priceSetId && CRM_Price_BAO_PriceSet::isQuickConfig($this->_priceSetId);
protected function isQuickConfig(): bool {
return $this->getPriceSetID() && CRM_Price_BAO_PriceSet::isQuickConfig($this->getPriceSetID());
}

/**
Expand Down
25 changes: 21 additions & 4 deletions CRM/Member/Form/Membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,14 @@ public function preProcess() {

// get price set id.
$this->_priceSetId = $_GET['priceSetId'] ?? NULL;
$this->set('priceSetId', $this->_priceSetId);
$this->assign('priceSetId', $this->_priceSetId);
// This is assigned only for the purposes of displaying the price block
// INSTEAD of the edit form. ie when the form is being overloaded
// via ajax (which is a long-standing anti-pattern - in
// some cases we have moved that overload behaviour
// to a separate form but note that it is necessary to
// add the fields to QuickForm when the form
// has been submitted so they appear in submittedValues().
$this->assign('priceSetId', $_GET['priceSetId'] ?? NULL);

if ($this->_action & CRM_Core_Action::DELETE) {
$contributionID = CRM_Member_BAO_Membership::getMembershipContributionId($this->_id);
Expand Down Expand Up @@ -1763,9 +1769,20 @@ protected function getMembershipParameters(): array {
$membershipTypeValues[$memType]['max_related'] = $this->getSubmittedValue('max_related');
}
}

// Really we don't need to do all this unless one of the join dates
// has been left empty by the submitter - ie in an ADD scenario but not really
// valid when editing & it would possibly not get the number of terms right
// so ideally the fields would be required on edit & the below would only
// be called on ADD
foreach ($this->order->getMembershipLineItems() as $membershipLineItem) {
$memTypeNumTerms = $this->getSubmittedValue('num_terms') ?: $membershipLineItem['membership_num_terms'];
if ($this->getAction() === CRM_Core_Action::ADD && $this->isQuickConfig()) {
$memTypeNumTerms = $this->getSubmittedValue('num_terms');
}
else {
// The submitted value is hidden when a price set is selected so
// although it is present it should be ignored.
$memTypeNumTerms = $membershipLineItem['membership_num_terms'];
}
$calcDates = CRM_Member_BAO_MembershipType::getDatesForMembershipType(
$membershipLineItem['membership_type_id'],
$this->getSubmittedValue('join_date'),
Expand Down
11 changes: 4 additions & 7 deletions tests/phpunit/CRM/Member/Form/MembershipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1337,6 +1337,7 @@ public function buildAmountMembershipDiscount($pageType, &$form, &$amount) {
* @param array $formValues
*
* @return \CRM_Member_Form_Membership
* @throws \CRM_Core_Exception
*/
protected function getForm(array $formValues = []): CRM_Member_Form_Membership {
if (isset($_REQUEST['cid'])) {
Expand Down Expand Up @@ -1583,12 +1584,10 @@ private function getExpectedEmailStrings(): array {
/**
* Test that membership end_date is correct for multiple terms for pending contribution
*
* @throws \CRM_Core_Exception
* @throws \Exception
*/
public function testCreatePendingWithMultipleTerms(): void {
CRM_Core_Session::singleton()->getStatus(TRUE);
$mailUtil = new CiviMailUtils($this, TRUE);
$this->createLoggedInUser();
$membershipTypeAnnualRolling = $this->callAPISuccess('membership_type', 'create', [
'domain_id' => 1,
Expand All @@ -1603,7 +1602,7 @@ public function testCreatePendingWithMultipleTerms(): void {
'financial_type_id' => 2,
]);
$params = [
'cid' => $this->ids['Contact']['individual_0'],
'contact_id' => $this->ids['Contact']['individual_0'],
'join_date' => date('Y-m-d'),
'start_date' => '',
'end_date' => '',
Expand All @@ -1620,10 +1619,8 @@ public function testCreatePendingWithMultipleTerms(): void {
'from_email_address' => '"Demonstrators Anonymous" <[email protected]>',
'receipt_text' => '',
];
$form = $this->getForm();
$form->preProcess();
$form->_contactID = $this->ids['Contact']['individual_0'];
$form->testSubmit($params);
$form = $this->getTestForm('CRM_Member_Form_Membership', $params);
$form->processForm();
$membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->ids['Contact']['individual_0']]);
// Check if Membership is set to Pending.
$this->assertEquals(CRM_Core_PseudoConstant::getKey('CRM_Member_BAO_Membership', 'status_id', 'Pending'), $membership['status_id']);
Expand Down
1 change: 1 addition & 0 deletions tests/phpunit/CiviTest/CiviUnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1725,6 +1725,7 @@ public function quickCleanUpFinancialEntities(): void {
*/
public function restoreDefaultPriceSetConfig(): void {
CRM_Core_DAO::executeQuery("DELETE FROM civicrm_price_set WHERE name NOT IN('default_contribution_amount', 'default_membership_type_amount')");
CRM_Core_DAO::executeQuery('UPDATE civicrm_price_set SET is_quick_config = 1, is_active = 1');
CRM_Core_DAO::executeQuery("UPDATE civicrm_price_set SET id = 1 WHERE name ='default_contribution_amount'");
CRM_Core_DAO::executeQuery("INSERT INTO `civicrm_price_field` (`id`, `price_set_id`, `name`, `label`, `html_type`, `is_enter_qty`, `help_pre`, `help_post`, `weight`, `is_display_amounts`, `options_per_line`, `is_active`, `is_required`, `active_on`, `expire_on`, `javascript`, `visibility_id`) VALUES (1, 1, 'contribution_amount', 'Contribution Amount', 'Text', 0, NULL, NULL, 1, 1, 1, 1, 1, NULL, NULL, NULL, 1)");
CRM_Core_DAO::executeQuery("INSERT INTO `civicrm_price_field_value` (`id`, `price_field_id`, `name`, `label`, `description`, `amount`, `count`, `max_value`, `weight`, `membership_type_id`, `membership_num_terms`, `is_default`, `is_active`, `financial_type_id`, `non_deductible_amount`) VALUES (1, 1, 'contribution_amount', 'Contribution Amount', NULL, '1', NULL, NULL, 1, NULL, NULL, 0, 1, 1, 0.00)");
Expand Down

0 comments on commit 29ae69c

Please sign in to comment.