@@ -9,20 +9,23 @@ import {
9
9
getDurationIntervalDays ,
10
10
maxDurationForPermitType ,
11
11
} from "./dateSelection" ;
12
+
12
13
import {
13
14
applyWhenNotNullable ,
14
15
getDefaultRequiredVal ,
15
16
} from "../../../common/helpers/util" ;
16
17
17
18
/**
18
- * Calculates the fee for a permit only by its duration .
19
+ * Calculates the fee for a permit.
19
20
* @param permitType Type of permit
20
21
* @param duration Number of days for duration of permit
21
- * @returns Fee to be paid for the permit duration
22
+ * @param totalDistance Total distance to travel for the route trip of the permit, if applicable
23
+ * @returns Fee to be paid for the permit
22
24
*/
23
- export const calculateFeeByDuration = (
25
+ export const calculatePermitFee = (
24
26
permitType : PermitType ,
25
27
duration : number ,
28
+ totalDistance ?: Nullable < number > ,
26
29
) => {
27
30
const maxAllowableDuration = maxDurationForPermitType ( permitType ) ;
28
31
@@ -43,6 +46,10 @@ export const calculateFeeByDuration = (
43
46
44
47
switch ( permitType ) {
45
48
// Add more conditions for other permit types if needed
49
+ case PERMIT_TYPES . MFP :
50
+ // MFP only calculate fee based on totalDistance
51
+ // minimum $20 no matter what, $0.11 per km
52
+ return Math . max ( 20 , 0.11 * getDefaultRequiredVal ( 0 , totalDistance ) ) ;
46
53
case PERMIT_TYPES . STOS :
47
54
// STOS have constant fee of $15 (regardless of duration)
48
55
return 15 ;
@@ -61,20 +68,22 @@ export const calculateFeeByDuration = (
61
68
* @param feeSummary fee summary field for a permit (if exists)
62
69
* @param duration duration field for a permit (if exists)
63
70
* @param permitType type of permit (if exists)
71
+ * @param totalDistance total distance to travel for the route trip of a permit (if applicable)
64
72
* @returns display text for the fee summary (currency amount to 2 decimal places)
65
73
*/
66
74
export const feeSummaryDisplayText = (
67
75
feeSummary ?: Nullable < string > ,
68
76
duration ?: Nullable < number > ,
69
77
permitType ?: Nullable < PermitType > ,
78
+ totalDistance ?: Nullable < number > ,
70
79
) => {
71
80
const feeFromSummary = applyWhenNotNullable (
72
81
( numericStr ) => Number ( numericStr ) . toFixed ( 2 ) ,
73
82
feeSummary ,
74
83
) ;
75
84
const feeFromDuration =
76
85
duration && permitType
77
- ? calculateFeeByDuration ( permitType , duration ) . toFixed ( 2 )
86
+ ? calculatePermitFee ( permitType , duration , totalDistance ) . toFixed ( 2 )
78
87
: null ;
79
88
80
89
const fee = getDefaultRequiredVal ( "0.00" , feeFromSummary , feeFromDuration ) ;
@@ -113,25 +122,29 @@ export const calculateNetAmount = (permitHistory: PermitHistory[]) => {
113
122
} ;
114
123
115
124
/**
116
- * Calculates the amount that needs to be refunded (or paid if amount is negative) for a permit given a new duration period .
125
+ * Calculates the amount that needs to be refunded (or paid if amount is negative) for a permit.
117
126
* @param permitHistory List of history objects that make up the history of a permit and its transactions
118
127
* @param currDuration Current (updated) duration of the permit
119
- * @param currPermitType Permit type of current permit to refund
128
+ * @param currPermitType Permit type of current permit to refund for
129
+ * @param updatedTotalDistance Updated total distance of the route trip for the permit (if applicable)
120
130
* @returns Amount that needs to be refunded, or if negative then the amount that still needs to be paid
121
131
*/
122
132
export const calculateAmountToRefund = (
123
133
permitHistory : PermitHistory [ ] ,
124
134
currDuration : number ,
125
135
currPermitType : PermitType ,
136
+ updatedTotalDistance ?: Nullable < number > ,
126
137
) => {
127
138
const netPaid = calculateNetAmount ( permitHistory ) ;
128
139
if ( isZeroAmount ( netPaid ) ) return 0 ; // If total paid is $0 (eg. no-fee permits), then refund nothing
129
140
130
- const feeForCurrDuration = calculateFeeByDuration (
141
+ const updatedFee = calculatePermitFee (
131
142
currPermitType ,
132
143
currDuration ,
144
+ updatedTotalDistance ,
133
145
) ;
134
- return netPaid - feeForCurrDuration ;
146
+
147
+ return netPaid - updatedFee ;
135
148
} ;
136
149
137
150
/**
0 commit comments