|
| 1 | +import { useState } from "react"; |
| 2 | + |
| 3 | +import type { UnitDetail, UnitProductChargeSummary } from "@squonk/account-server-client"; |
| 4 | +import { useGetUnit, useGetUnitCharges } from "@squonk/account-server-client/unit"; |
| 5 | + |
| 6 | +import { |
| 7 | + Box, |
| 8 | + Container, |
| 9 | + Divider, |
| 10 | + Paper, |
| 11 | + Table, |
| 12 | + TableBody, |
| 13 | + TableCell, |
| 14 | + TableHead, |
| 15 | + TableRow, |
| 16 | + Typography, |
| 17 | +} from "@mui/material"; |
| 18 | + |
| 19 | +import { formatCoins } from "../../utils/app/coins"; |
| 20 | +import { ChargesLinkIconButton } from "../products/ChargesLinkIconButton"; |
| 21 | +import { SelectBillingCycle } from "./SelectBillingCycle"; |
| 22 | + |
| 23 | +export interface UnitChargesProps { |
| 24 | + unitId: UnitDetail["id"]; |
| 25 | +} |
| 26 | + |
| 27 | +const productTypes: Record<UnitProductChargeSummary["product_type"], string> = { |
| 28 | + DATA_MANAGER_PROJECT_TIER_SUBSCRIPTION: "Project Subscription", |
| 29 | + DATA_MANAGER_STORAGE_SUBSCRIPTION: "Dataset Subscription", |
| 30 | +}; |
| 31 | + |
| 32 | +export const UnitCharges = ({ unitId }: UnitChargesProps) => { |
| 33 | + const [monthDelta, setMonthDelta] = useState(0); |
| 34 | + |
| 35 | + const { data: unit } = useGetUnit(unitId); |
| 36 | + const { data: charges } = useGetUnitCharges(unitId, { pbp: monthDelta }); |
| 37 | + |
| 38 | + const processingTotal = charges?.summary.charges.find( |
| 39 | + (charge) => charge.type === "PROCESSING", |
| 40 | + )?.coins; |
| 41 | + const storageTotal = charges?.summary.charges.find((charge) => charge.type === "STORAGE")?.coins; |
| 42 | + const totalCharges = charges?.summary.charges |
| 43 | + .map((charge) => charge.coins) |
| 44 | + .reduce((acc, charge) => acc + Number.parseFloat(charge), 0); |
| 45 | + |
| 46 | + return ( |
| 47 | + <Container maxWidth="md"> |
| 48 | + <Typography variant="h1">Unit Ledger</Typography> |
| 49 | + <Typography variant="subtitle2">{unit?.id}</Typography> |
| 50 | + <Typography gutterBottom component="p" variant="h5"> |
| 51 | + Charges against: {unit?.name} |
| 52 | + </Typography> |
| 53 | + <Typography gutterBottom> |
| 54 | + <strong>Billed to</strong>: unit <em>{unit?.name}</em> |
| 55 | + </Typography> |
| 56 | + <Typography gutterBottom component="h2" variant="h4"> |
| 57 | + Billing period |
| 58 | + </Typography> |
| 59 | + |
| 60 | + {unit?.billing_day && unit.created && ( |
| 61 | + <SelectBillingCycle |
| 62 | + billingDay={unit.billing_day} |
| 63 | + created={unit.created} |
| 64 | + monthDelta={monthDelta} |
| 65 | + onChange={setMonthDelta} |
| 66 | + /> |
| 67 | + )} |
| 68 | + |
| 69 | + <Typography gutterBottom sx={{ mt: 2 }} variant="h2"> |
| 70 | + Charges |
| 71 | + </Typography> |
| 72 | + <Paper> |
| 73 | + <Table size="small" sx={{ marginBottom: 2 }}> |
| 74 | + <TableHead> |
| 75 | + <TableRow> |
| 76 | + <TableCell></TableCell> |
| 77 | + <TableCell>Product Type</TableCell> |
| 78 | + <TableCell>Storage Charges</TableCell> |
| 79 | + <TableCell>Processing Charges</TableCell> |
| 80 | + <TableCell>Actions</TableCell> |
| 81 | + </TableRow> |
| 82 | + </TableHead> |
| 83 | + <TableBody> |
| 84 | + {charges?.products.map((product, index) => ( |
| 85 | + <TableRow key={product.product_id}> |
| 86 | + <TableCell>{index + 1}</TableCell> |
| 87 | + <TableCell>{productTypes[product.product_type]}</TableCell> |
| 88 | + <TableCell> |
| 89 | + {formatCoins( |
| 90 | + product.charges.find((charge) => charge.type === "STORAGE")?.coins ?? 0, |
| 91 | + )} |
| 92 | + </TableCell> |
| 93 | + <TableCell> |
| 94 | + {formatCoins( |
| 95 | + product.charges.find((charge) => charge.type === "PROCESSING")?.coins ?? 0, |
| 96 | + )} |
| 97 | + </TableCell> |
| 98 | + <TableCell> |
| 99 | + <ChargesLinkIconButton productId={product.product_id} /> |
| 100 | + </TableCell> |
| 101 | + </TableRow> |
| 102 | + ))} |
| 103 | + </TableBody> |
| 104 | + </Table> |
| 105 | + </Paper> |
| 106 | + |
| 107 | + <Divider sx={{ my: 2 }} /> |
| 108 | + <Box textAlign="right"> |
| 109 | + <Typography variant="h4">Subtotal Total</Typography> |
| 110 | + Processing: {!!processingTotal && formatCoins(processingTotal)} |
| 111 | + <br /> |
| 112 | + Storage: {!!storageTotal && formatCoins(storageTotal)} |
| 113 | + <Typography variant="h3">Total Charges</Typography> |
| 114 | + <Typography variant="subtitle1">To be paid by the unit owner</Typography> |
| 115 | + Total: {!!totalCharges && formatCoins(totalCharges)} |
| 116 | + </Box> |
| 117 | + </Container> |
| 118 | + ); |
| 119 | +}; |
0 commit comments