import { useEffect, useState } from "react"; import { Button, Column, Grid, InlineLoading, InlineNotification, NumberInput, Table, TableBody, TableCell, TableContainer, TableHead, TableHeader, TableRow, Tag, TextInput, } from "@carbon/react"; import { confirm } from "@tauri-apps/plugin-dialog"; import { api } from "../lib/api"; import type { InvoiceSeries } from "../lib/types"; export default function SeriesSettings() { const [active, setActive] = useState(null); const [all, setAll] = useState([]); const [loading, setLoading] = useState(true); const [prefix, setPrefix] = useState(`INV/${new Date().getFullYear()}-`); const [padding, setPadding] = useState(3); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); const [notice, setNotice] = useState(null); const load = async () => { try { const [current, list] = await Promise.all([api.getActiveSeries(), api.listSeries()]); setActive(current); setAll(list); if (current) { setPrefix(current.prefix); setPadding(current.padding); } setError(null); } catch (e) { setError(String(e)); } finally { setLoading(false); } }; useEffect(() => { void load(); }, []); const startNew = async () => { const ok = await confirm( `Start a new series from ${prefix}${String(1).padStart(padding, "0")}? ` + "The current series will be retired and the counter resets to 1.", { title: "Start new series", kind: "warning" }, ); if (!ok) return; setBusy(true); setError(null); setNotice(null); try { const created = await api.startNewSeries(prefix, padding); setNotice(`New series started. Next invoice: ${created.nextInvoiceNumber}`); await load(); } catch (e) { setError(String(e)); } finally { setBusy(false); } }; if (loading) { return (
); } return (

Invoice series

The active series drives the automatic invoice numbers. Start a new series to reset the counter.

{error ? ( ) : null} {notice ? ( setNotice(null)} /> ) : null} setPrefix(e.target.value)} /> setPadding(Number(value) || 3)} /> Prefix Next number Digits Status Created {all.map((series) => ( {series.prefix} {series.nextInvoiceNumber} {series.padding} {series.isActive ? Active : Retired} {new Date(series.createdAt).toLocaleDateString("en-GB")} ))}
); }