import * as React from 'react'; import { __ } from '@wordpress/i18n'; import type { BookingApi } from '../booking-ui/api'; import { money } from '../booking-ui/pricing'; import { formatDateTime } from '../booking-ui/dateTime'; import { Amounts, OutstandingTable, type OutstandingBalance } from '../booking-ui/receivables'; import { CalendarBookingPanel } from '../booking-ui/CalendarBookingPanel'; interface Debtor { customer_type: string; customer_id: number; customer_name: string; currency: string; outstanding_amount: number; item_count: number; oldest_debt: string; relevant_booking: string; booking_id: number; } function typeLabel(type: string) { return type === 'member' ? __('Club member', 'abece-booking') : type === 'registered_non_member' ? __('Registered non-member', 'abece-booking') : __('External / phone booking', 'abece-booking'); } export function ReceivablesApp({ api }: { api: BookingApi; }) { const [search, setSearch] = React.useState(''), [currency, setCurrency] = React.useState(''), [type, setType] = React.useState(''), [sort, setSort] = React.useState('oldest'), [page, setPage] = React.useState(1), [revision, setRevision] = React.useState(0); const [list, setList] = React.useState<{ items: Debtor[]; total: number; timezone: string; } | null>(null), [customer, setCustomer] = React.useState(null), [details, setDetails] = React.useState(null), [detailPage, setDetailPage] = React.useState(1), [booking, setBooking] = React.useState(null), [error, setError] = React.useState(''); React.useEffect(() => { let live = true; setError(''); setList(null); const timer = setTimeout(() => api.request>('admin/receivables', 'GET', undefined, { search, sort, page: String(page), ...(currency ? { currency } : {}), ...(type ? { customer_type: type } : {}) }).then(d => { if (live) setList(d); }).catch(e => { if (live) setError(e.message); }), 200); return () => { live = false; clearTimeout(timer); }; }, [api, search, currency, type, sort, page, revision]); React.useEffect(() => { let live = true; setDetails(null); if (customer) api.request(`admin/receivables/${customer.customer_type}/${customer.customer_id}`, 'GET', undefined, { page: String(detailPage) }).then(d => { if (live) setDetails(d); }).catch(e => { if (live) setError(e.message); }); return () => { live = false; }; }, [api, customer, detailPage, revision]); return

{__('Outstanding amounts', 'abece-booking')}

{__('Payment settings', 'abece-booking')}
{error &&

{error}

}{!list && !error &&

{__('Loading…', 'abece-booking')}

}{list && <>
{[__('Customer', 'abece-booking'), __('Customer type', 'abece-booking'), __('Outstanding', 'abece-booking'), __('Currency', 'abece-booking'), __('Unpaid items', 'abece-booking'), __('Oldest debt', 'abece-booking'), __('Relevant booking', 'abece-booking'), __('Actions', 'abece-booking')].map(t => )}{list.items.map(d => )}
{t}
{d.customer_name}{typeLabel(d.customer_type)}{money(d.outstanding_amount, d.currency)}{d.currency}{d.item_count}{formatDateTime(d.oldest_debt, list.timezone)}{d.relevant_booking}
{!list.items.length &&

{__('No outstanding payments.', 'abece-booking')}

} {page} }{customer &&

{customer.customer_name}

{details ? <> {detailPage} :

{__('Loading…', 'abece-booking')}

}
}{booking && setBooking(null)} onChanged={() => setRevision(n => n + 1)}/>}
; }