import { useEffect, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { Alert, Badge, Button, Center, Group, Loader, Modal, NumberInput, Paper, Stack, Switch, Text, TextInput, ThemeIcon, Title, Tooltip, } from '@mantine/core'; import { IconAlertTriangle, IconCreditCard, IconEdit, IconInfoCircle, IconLock, } from '@tabler/icons-react'; import { notify, ModalFooter, AdvancedTable, useServerTable, type AdvancedColumn, } from '@ema-platform/ui'; import { extractErrorMessage, useLocalized, useGetLicenseTypesQuery, useGetPaymentCapabilitiesQuery, useUpdateLicenseFeesMutation, } from '@ema-platform/api'; import type { LicenseType } from '@ema-platform/api'; /** * Licence fee configuration. * * The amounts live on the licence type itself, which is what the workflow * reads when it raises a payment — so what is edited here is the same value * the applicant is charged, not a parallel copy of it. * * Saving is guarded server-side by `can:update:license-type`; an officer * without that permission can read the figures but the save is refused. */ function feeText(amount: string | number | null, currency: string): string { if (amount === null || amount === '') return '—'; const value = Number(amount); if (!Number.isFinite(value)) return '—'; return `${value.toLocaleString('en-US')} ${currency}`; } export function PaymentConfigPage() { const { t } = useTranslation(); const localized = useLocalized(); const { data, isLoading, isFetching, error, refetch } = useGetLicenseTypesQuery(); const { data: capabilities } = useGetPaymentCapabilitiesQuery(); const [editing, setEditing] = useState(null); const { setPageIndex, pageSize, setPageSize, paginate } = useServerTable(); if (isLoading) { return (
); } if (error) { return ( } title={t('paymentConfig.loadError', 'Could not load licence types')} > {extractErrorMessage(error)} ); } const types = [...(data?.items ?? [])].sort( (a, b) => (a.sortOrder ?? 0) - (b.sortOrder ?? 0), ); const page = paginate(types); const columns: AdvancedColumn[] = [ { header: t('paymentConfig.columns.type', 'Licence type'), cell: ({ row }) => ( <> {localized(row.original.name)} {row.original.key} ), }, { header: t('paymentConfig.columns.newApplication', 'New application'), cell: ({ row }) => ( {feeText(row.original.feeNewApplication, row.original.feeCurrency)} ), }, { header: t('paymentConfig.columns.renewal', 'Renewal'), cell: ({ row }) => { const type = row.original; if (type.feeNewApplication === null) { // No charge at all, so "same as new" would be noise. return ( ); } if (type.feeRenewal === null) { return ( {feeText(type.feeNewApplication, type.feeCurrency)}{' '} {t('paymentConfig.renewalSameAsNew', '(same as new)')} ); } return ( {feeText(type.feeRenewal, type.feeCurrency)} ); }, }, { header: t('paymentConfig.columns.charged', 'Charged?'), cell: ({ row }) => row.original.issuesCertificate ? ( {t('paymentConfig.chargedOnApproval', 'On approval')} ) : ( {t('paymentConfig.chargedNotCharged', 'Not charged')} ), }, { header: '', size: 90, align: 'right', cell: ({ row }) => ( ), }, ]; return (
{t('paymentConfig.title', 'Payment configuration')} {t( 'paymentConfig.subtitle', 'What each licence costs. Applicants are charged after approval, and the amount is fixed onto the application at that moment.', )}
} > {t( 'paymentConfig.changeNotice', 'A change applies to applications approved from now on. Anything already approved keeps the amount it was quoted, so an edit here can never alter what an applicant has already been asked to pay.', )} setEditing(null)} />
); } /** * How payments are actually collected. Read-only on purpose: these come from * the payment service's environment rather than the database, so rendering * them as editable fields would be a lie. */ function GatewayPanel({ bypassEnabled }: { bypassEnabled?: boolean }) { const { t } = useTranslation(); return ( {t('paymentConfig.gateway.title', 'Payment gateway')} {t( 'paymentConfig.gateway.subtitle', 'Set by the payment service environment — not editable here.', )} {t('paymentConfig.gateway.provider', 'Telebirr')} {bypassEnabled ? ( {t('paymentConfig.gateway.bypassEnabled', 'Test bypass enabled')} ) : ( {t('paymentConfig.gateway.bypassOff', 'Test bypass off')} )} ); } function FeeEditModal({ licenseType, onClose, }: { licenseType: LicenseType | null; onClose: () => void; }) { const { t } = useTranslation(); const localized = useLocalized(); const [updateFees, { isLoading }] = useUpdateLicenseFeesMutation(); const [newFee, setNewFee] = useState(''); const [renewalFee, setRenewalFee] = useState(''); const [currency, setCurrency] = useState('ETB'); // Held separately from an empty amount: "carries no fee" and "same as new" // are real configuration states, not blank fields. const [chargeable, setChargeable] = useState(true); const [sameAsNew, setSameAsNew] = useState(true); useEffect(() => { if (!licenseType) return; setChargeable(licenseType.feeNewApplication !== null); setNewFee( licenseType.feeNewApplication === null ? '' : Number(licenseType.feeNewApplication), ); setSameAsNew(licenseType.feeRenewal === null); setRenewalFee( licenseType.feeRenewal === null ? '' : Number(licenseType.feeRenewal), ); setCurrency(licenseType.feeCurrency || 'ETB'); }, [licenseType]); async function save() { if (!licenseType) return; if (chargeable && newFee === '') { notify.error( t( 'paymentConfig.modal.missingNewFee', 'Enter a new-application fee, or turn off "carries a fee".', ), ); return; } if (chargeable && !sameAsNew && renewalFee === '') { notify.error( t( 'paymentConfig.modal.missingRenewalFee', 'Enter a renewal fee, or charge renewal at the same rate.', ), ); return; } try { await updateFees({ id: licenseType.id, feeNewApplication: chargeable ? Number(newFee) : null, feeRenewal: chargeable && !sameAsNew ? Number(renewalFee) : null, feeCurrency: currency.trim() || 'ETB', }).unwrap(); notify.success( t('paymentConfig.modal.updated', { type: localized(licenseType.name), defaultValue: '{{type}} fees updated.', }), ); onClose(); } catch (err) { notify.error( extractErrorMessage(err, t('paymentConfig.modal.saveFailed', 'Could not save the fees')), ); } } return ( {licenseType && ( {!licenseType.issuesCertificate && ( } > {t( 'paymentConfig.modal.noPaymentStage', 'This licence type concludes with an EMA decision and never reaches a payment stage, so a fee set here stays unused until that changes.', )} )} setChargeable(e.currentTarget.checked)} label={t('paymentConfig.modal.chargeableLabel', 'This licence carries a fee')} description={t( 'paymentConfig.modal.chargeableDescription', 'Turn off for licence types applicants are never charged for.', )} /> {chargeable && ( <> setNewFee(v === '' ? '' : Number(v))} min={0} max={9_999_999_999} thousandSeparator="," allowNegative={false} decimalScale={2} /> setSameAsNew(e.currentTarget.checked)} label={t('paymentConfig.modal.sameRateLabel', 'Charge renewal at the same rate')} description={t( 'paymentConfig.modal.sameRateDescription', 'Turn off to set a separate renewal fee.', )} /> {!sameAsNew && ( setRenewalFee(v === '' ? '' : Number(v))} min={0} max={9_999_999_999} thousandSeparator="," allowNegative={false} decimalScale={2} /> )} setCurrency(e.currentTarget.value.toUpperCase()) } maxLength={8} /> )} )} ); } export default PaymentConfigPage;