Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BTHAB-293: Allow extension to update membership minimum #508

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CRM/MembershipExtras/API/PaymentSchedule/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ protected function getInstalments(array $membershipTypes, array $nonMembershipPr
$endDate = !empty($this->params['end_date']) ? new DateTime($this->params['end_date']) : NULL;
$membershipInstalmentsSchedule = new CRM_MembershipExtras_Service_MembershipInstalmentsSchedule(
$membershipTypes,
$this->params['schedule']
$this->params['schedule'],
$this->params['contact_id'] ?? NULL
);

if (!empty($nonMembershipPriceFieldValues)) {
Expand Down
4 changes: 2 additions & 2 deletions CRM/MembershipExtras/API/PaymentSchedule/PriceValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ private function processPriceFieldValues() {
$this->membershipTypes[] = $membershipType;
}

if (empty($membershipTypes)) {
new API_Exception(ts('At least one price field items must be of type membership'));
if (empty($this->membershipTypes)) {
throw new API_Exception(ts('At least one price field items must be of type membership'));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

use Civi\Core\Event\GenericHookEvent;

/**
* Class CRM_MembershipExtras_Hook_CustomDispatch_CalculateMembershipMinimumFee
*/
class CRM_MembershipExtras_Hook_CustomDispatch_CalculateMembershipMinimumFee {

const NAME = 'me.membership.calculate_minimum_fee';

/**
* Membership Types.
*
* @var array
*/
private array $membershipTypes;

/**
* Contact ID.
*
* @var int
*/
private $contactID;

/**
* CRM_MembershipExtras_Hook_CustomDispatch_CalculateMembershipMinimumFee constructor.
*
* @param array $membershipTypes
* @param int $contactID
*/
public function __construct(&$membershipTypes, $contactID) {
$this->membershipTypes =& $membershipTypes;
$this->contactID = $contactID;
}

/**
* Dispatches event.
*/
public function dispatch() {
$event = GenericHookEvent::create(['contactID' => $this->contactID, 'membershipTypes' => &$this->membershipTypes]);
Civi::dispatcher()->dispatch(self::NAME, $event);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use CRM_MembershipExtras_Service_MembershipInstalmentAmountCalculator as InstalmentAmountCalculator;
use CRM_MembershipExtras_Service_MembershipTypeDurationCalculator as MembershipTypeDurationCalculator;
use CRM_MembershipExtras_Service_MembershipTypeDatesCalculator as MembershipTypeDatesCalculator;
use CRM_MembershipExtras_Hook_CustomDispatch_CalculateMembershipMinimumFee as CalculateMembershipMinimumFeeHook;
use CRM_MembershipExtras_Service_MembershipPeriodType_RollingPeriodTypeCalculator as RollingPeriodTypeCalculator;

class CRM_MembershipExtras_Hook_Pre_MembershipPaymentPlanProcessor_AbstractProcessor {
Expand Down Expand Up @@ -86,6 +87,7 @@ protected function assignInstalmentDetails() {
}

protected function getInstalmentAmountCalculator(array $membershipTypes, $periodType = 'rolling') {
(new CalculateMembershipMinimumFeeHook($membershipTypes, $this->params['contact_id']))->dispatch();
if ($periodType == 'fixed') {
$calculator = new FixedPeriodTypeCalculator($membershipTypes);
$calculator->setStartDate(new DateTime($this->getMembership()['start_date']));
Expand Down
12 changes: 8 additions & 4 deletions CRM/MembershipExtras/Job/OfflineAutoRenewal/PaymentPlan.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use CRM_MembershipExtras_Service_MembershipEndDateCalculator as MembershipEndDateCalculator;
use CRM_MembershipExtras_SettingsManager as SettingsManager;
use CRM_MembershipExtras_Hook_CustomDispatch_PostOfflineAutoRenewal as PostOfflineAutoRenewalDispatcher;
use CRM_MembershipExtras_Hook_CustomDispatch_CalculateMembershipMinimumFee as CalculateMembershipMinimumFeeHook;

/**
* Renews a payment plan.
Expand Down Expand Up @@ -416,11 +417,14 @@ private function getMembershipMinimumFeeFromLineItem($lineItem, $priceFieldValue
$membershipTypeID = $priceFieldValue['membership_type_id'];
}

$membershipType = civicrm_api3('MembershipType', 'getsingle', [
'id' => $membershipTypeID,
]);
/**
* @var CRM_Member_DAO_MembershipType[]
*/
$membershipTypes = [CRM_Member_BAO_MembershipType::findById($membershipTypeID)];
$contactId = $this->currentRecurringContribution['contact_id'];
(new CalculateMembershipMinimumFeeHook($membershipTypes, $contactId ?? NULL))->dispatch();

return $membershipType['minimum_fee'];
return $membershipTypes[0]->minimum_fee;
}

/**
Expand Down
1 change: 1 addition & 0 deletions CRM/MembershipExtras/Page/InstalmentSchedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ private function assignInstalments() {
$params['payment_method'] = CRM_Utils_Request::retrieve('payment_method', 'Int');
$params['start_date'] = CRM_Utils_Request::retrieve('start_date', 'String');
$params['join_date'] = CRM_Utils_Request::retrieve('join_date', 'String');
$params['contact_id'] = CRM_Utils_Request::retrieve('contact_id', 'Int');

try {
$result = civicrm_api3('PaymentSchedule', $action, $params);
Expand Down
11 changes: 10 additions & 1 deletion CRM/MembershipExtras/Service/MembershipInstalmentsSchedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use CRM_MembershipExtras_Service_MembershipInstalmentAmountCalculator as InstalmentAmountCalculator;
use CRM_MembershipExtras_Service_MembershipPeriodType_FixedPeriodTypeCalculator as FixedPeriodTypeCalculator;
use CRM_MembershipExtras_Service_MembershipPeriodType_RollingPeriodTypeCalculator as RollingPeriodCalculator;
use CRM_MembershipExtras_Hook_CustomDispatch_CalculateMembershipMinimumFee as CalculateMembershipMinimumFeeHook;
use CRM_MembershipExtras_Hook_CustomDispatch_CalculateContributionReceiveDate as CalculateContributionReceiveDateDispatcher;
use CRM_MembershipExtras_Helper_InstalmentSchedule as InstalmentScheduleHelper;

Expand Down Expand Up @@ -70,19 +71,26 @@ class CRM_MembershipExtras_Service_MembershipInstalmentsSchedule {
*/
private $installmentReceiveDateCalculator;

/**
* @var int|null
*/
private $contactID;

/**
* CRM_MembershipExtras_Service_MembershipTypeInstalment constructor.
*
* @param array $membershipTypes
* @param string $schedule
* @param int|null $contactID
*
* @throws CRM_MembershipExtras_Exception_InvalidMembershipTypeInstalment
*/
public function __construct(array $membershipTypes, string $schedule) {
public function __construct(array $membershipTypes, string $schedule, int|null $contactID = NULL) {
$this->membershipInstalmentTaxAmountCalculator = new CRM_MembershipExtras_Service_MembershipInstalmentTaxAmountCalculator();
$this->installmentReceiveDateCalculator = new CRM_MembershipExtras_Service_InstalmentReceiveDateCalculator();
$this->membershipTypes = $membershipTypes;
$this->schedule = $schedule;
$this->contactID = $contactID;
$this->validateMembershipTypeForInstalment();
}

Expand Down Expand Up @@ -208,6 +216,7 @@ private function calculateInstalmentAmount() {
*
*/
private function getInstalmentAmountCalculator() {
(new CalculateMembershipMinimumFeeHook($this->membershipTypes, $this->contactID))->dispatch();
if ($this->membershipTypes[0]->period_type == 'fixed') {
$fixedPeriodTypCalculator = new FixedPeriodTypeCalculator($this->membershipTypes);
$fixedPeriodTypCalculator->setStartDate($this->startDate);
Expand Down
8 changes: 7 additions & 1 deletion js/paymentPlanToggler.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ function paymentPlanToggler(togglerValue, currencySymbol) {
preventPaymentTabLinkAction();
toggleNumOfTermsField();
preventMultiFormSubmission();
window.isPaymentPlanTabActive = isPaymentPlanTabActive;
window.isPriceSetSelected = isPriceSetSelected;
window.getSelectedPriceFieldValues = getSelectedPriceFieldValues;
});

/**
Expand Down Expand Up @@ -108,7 +111,7 @@ function paymentPlanToggler(togglerValue, currencySymbol) {
assignFirstContributionReceiveDate();
});

$('#payment_plan_schedule, #payment_instrument_id, #start_date, #end_date').change(() => {
$('#payment_plan_schedule, #payment_instrument_id, #start_date, #end_date, #contact_id').change(() => {
if (!isPaymentPlanTabActive()) {
return;
}
Expand Down Expand Up @@ -172,11 +175,14 @@ function paymentPlanToggler(togglerValue, currencySymbol) {
*/
function generateInstalmentSchedule(isPriceSet, startDate) {
let schedule = $('#payment_plan_schedule').val();
const searchParams = new URLSearchParams(window.location.search);
const cid = $('#contact_id').val() ?? searchParams.get('cid');
let params = {
schedule: schedule,
start_date: startDate,
join_date: $('#join_date').val(),
payment_method: $('#payment_instrument_id').val(),
contact_id: cid,
};
if (isPriceSet) {
let selectedPriceFieldValues = getSelectedPriceFieldValues();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ private function mockParams($membershipPeriodType, $membershipTypeDuration, $mem
'contribution_id' => $contribution['id'],
'entity_table' => 'civicrm_membership',
'entity_id' => $this->membership['id'],
'contact_id' => $contact['id'],
];
}

Expand Down
Loading