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

ORV2-3211 - BVT: Void not refunding the total value #1740

Merged
merged 2 commits into from
Jan 11, 2025
Merged
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
57 changes: 34 additions & 23 deletions frontend/src/features/permits/helpers/feeSummary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import { TRANSACTION_TYPES, TransactionType } from "../types/payment";
import { Permit } from "../types/permit";
import { isValidTransaction } from "./payment";
import { Nullable } from "../../../common/types/common";
import { PERMIT_STATES, daysLeftBeforeExpiry, getPermitState } from "./permitState";
import { PERMIT_STATES, getPermitState } from "./permitState";
import { PERMIT_TYPES, PermitType } from "../types/PermitType";
import { getDurationIntervalDays, maxDurationForPermitType } from "./dateSelection";
import {
getDurationIntervalDays,
maxDurationForPermitType,
} from "./dateSelection";
import {
applyWhenNotNullable,
getDefaultRequiredVal,
Expand All @@ -17,19 +20,27 @@ import {
* @param duration Number of days for duration of permit
* @returns Fee to be paid for the permit duration
*/
export const calculateFeeByDuration = (permitType: PermitType, duration: number) => {
export const calculateFeeByDuration = (
permitType: PermitType,
duration: number,
) => {
const maxAllowableDuration = maxDurationForPermitType(permitType);

// Make sure that duration is between 0 and max allowable duration (for given permit type)
const safeDuration = duration < 0
? 0
: (duration > maxAllowableDuration) ? maxAllowableDuration : duration;

// Make sure that duration is between 0 and max allowable duration (for given permit type)
const safeDuration =
duration < 0
? 0
: duration > maxAllowableDuration
? maxAllowableDuration
: duration;

const intervalDays = getDurationIntervalDays(permitType);

const intervalPeriodsToPay = safeDuration > 360
? Math.ceil(360 / intervalDays) : Math.ceil(safeDuration / intervalDays);

const intervalPeriodsToPay =
safeDuration > 360
? Math.ceil(360 / intervalDays)
: Math.ceil(safeDuration / intervalDays);

switch (permitType) {
// Add more conditions for other permit types if needed
case PERMIT_TYPES.STOS:
Expand Down Expand Up @@ -61,9 +72,11 @@ export const feeSummaryDisplayText = (
(numericStr) => Number(numericStr).toFixed(2),
feeSummary,
);
const feeFromDuration = duration && permitType ?
calculateFeeByDuration(permitType, duration).toFixed(2) : null;

const feeFromDuration =
duration && permitType
? calculateFeeByDuration(permitType, duration).toFixed(2)
: null;

const fee = getDefaultRequiredVal("0.00", feeFromSummary, feeFromDuration);
const numericFee = Number(fee);
return numericFee >= 0 ? `$${fee}` : `-$${(numericFee * -1).toFixed(2)}`;
Expand Down Expand Up @@ -114,7 +127,10 @@ export const calculateAmountToRefund = (
const netPaid = calculateNetAmount(permitHistory);
if (isZeroAmount(netPaid)) return 0; // If total paid is $0 (eg. no-fee permits), then refund nothing

const feeForCurrDuration = calculateFeeByDuration(currPermitType, currDuration);
const feeForCurrDuration = calculateFeeByDuration(
currPermitType,
currDuration,
);
return netPaid - feeForCurrDuration;
};

Expand Down Expand Up @@ -142,13 +158,8 @@ export const calculateAmountForVoid = (
return 0;
}

const netAmountPaid = calculateNetAmount(transactionHistory);
if (isZeroAmount(netAmountPaid)) return 0; // If existing net paid is $0 (eg. no-fee permits), then refund nothing
const netPaid = calculateNetAmount(transactionHistory);
if (isZeroAmount(netPaid)) return 0; // If existing net paid is $0 (eg. no-fee permits), then refund nothing

const daysLeft = daysLeftBeforeExpiry(permit);
const intervalDays = getDurationIntervalDays(permit.permitType);
return calculateFeeByDuration(
permit.permitType,
Math.floor(daysLeft / intervalDays) * intervalDays,
);
return netPaid;
};
Loading