Skip to content

Commit be8f6ed

Browse files
committed
fix(charges-page): fix billing periods calculation
1 parent f564e04 commit be8f6ed

File tree

6 files changed

+28
-18
lines changed

6 files changed

+28
-18
lines changed

components/finance/ProductCharges.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
import { filesize } from "filesize";
2121

2222
import { toLocalTimeString } from "../../utils/app/datetime";
23+
import { formatOrdinals } from "../../utils/app/ordinals";
2324
import { getBillingPeriods } from "../../utils/app/products";
2425
import { CenterLoader } from "../CenterLoader";
2526

@@ -57,9 +58,9 @@ export const ProductCharges = ({ productId }: ProductChargesProps) => {
5758
<em>{productData.product.organisation.name}</em> organisation{" "}
5859
</Typography>
5960
<Typography gutterBottom component="h2" variant="h4">
60-
Billing period
61+
Billing period (Billed on the {formatOrdinals(productData.product.unit.billing_day)} of the
62+
month)
6163
</Typography>
62-
6364
<Select
6465
id="select-billing-cycle"
6566
size="small"
@@ -72,7 +73,6 @@ export const ProductCharges = ({ productId }: ProductChargesProps) => {
7273
<MenuItem key={d1} value={i}>{`${d1}${d2}`}</MenuItem>
7374
))}
7475
</Select>
75-
7676
<Typography gutterBottom sx={{ mt: 2 }} variant="h2">
7777
Charges
7878
</Typography>

components/results/DateTimeListItem.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { ListItem, ListItemText, useMediaQuery, useTheme } from "@mui/material";
22
import dayjs from "dayjs";
33
import utc from "dayjs/plugin/utc";
44

5-
dayjs.extend(utc);
5+
import { DATE_FORMAT, TIME_FORMAT } from "../../constants/datetimes";
66

7-
import { DATE_FORMAT, TIME_FORMAT } from "../../utils/app/datetime";
7+
dayjs.extend(utc);
88

99
export interface DateTimeListItemProps {
1010
/**

constants/datetimes.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const DATE_FORMAT = "DD/MM/YY";
2+
export const TIME_FORMAT = "HH:mm:ss";

utils/app/datetime.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import dayjs from "dayjs";
22
import utc from "dayjs/plugin/utc";
33

4-
dayjs.extend(utc);
4+
import { DATE_FORMAT, TIME_FORMAT } from "../../constants/datetimes";
55

6-
export const DATE_FORMAT = "DD/MM/YY";
7-
export const TIME_FORMAT = "HH:mm:ss";
6+
dayjs.extend(utc);
87

98
export const toLocalTimeString = (
109
utcTimestamp: string,

utils/app/ordinals.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const pr = new Intl.PluralRules("en-US", { type: "ordinal" });
2+
3+
const suffixes = new Map([
4+
["one", "st"],
5+
["two", "nd"],
6+
["few", "rd"],
7+
["other", "th"],
8+
]);
9+
10+
export const formatOrdinals = (n: number) => {
11+
const rule = pr.select(n);
12+
const suffix = suffixes.get(rule);
13+
return `${n}${suffix}`;
14+
};

utils/app/products.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import type { Dayjs } from "dayjs";
22
import dayjs from "dayjs";
33
import utc from "dayjs/plugin/utc";
44

5+
import { DATE_FORMAT } from "../../constants/datetimes";
6+
57
dayjs.extend(utc);
68

79
/**
@@ -27,14 +29,7 @@ export const getBillingDay = () => {
2729
export const getBillingPeriods = (billingDay: number, created: string) => {
2830
const createdDay = dayjs.utc(created);
2931

30-
let firstBillingDay: Dayjs;
31-
if (billingDay > createdDay.day()) {
32-
// Current moths billing day hasn't happened yet
33-
firstBillingDay = createdDay.set("day", billingDay);
34-
} else {
35-
// Current months billing day has already passed
36-
firstBillingDay = createdDay.set("day", billingDay).add(1, "month");
37-
}
32+
const firstBillingDay = createdDay.set("date", billingDay);
3833

3934
let date = firstBillingDay;
4035
const dates: Dayjs[] = [];
@@ -45,7 +40,7 @@ export const getBillingPeriods = (billingDay: number, created: string) => {
4540

4641
return dates.map((d, index) => [
4742
index - dates.length + 1,
48-
d.local().format("YYYY-MM-DD"),
49-
d.local().add(1, "month").subtract(1, "day").format("YYYY-MM-DD"),
43+
d.local().format(DATE_FORMAT),
44+
d.local().add(1, "month").format(DATE_FORMAT),
5045
]);
5146
};

0 commit comments

Comments
 (0)