feat(offers,invoices): per-currency 'Celkem' totals beneath the lists (consistent with orders)

Mirrors the orders totals exactly: /stats (offers) + /list-totals (issued +
received invoices) endpoints sum each doc's total per currency across the active
filter (reusing extracted where-builders so they stay in sync with the lists),
rendered as the identical 'Celkem: …' block via the shared formatMultiCurrency.
Existing invoice/received KPI stats untouched.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
BOHA
2026-06-09 22:12:24 +02:00
parent 1e4dd1fbcc
commit 97101c4db7
11 changed files with 760 additions and 66 deletions

View File

@@ -23,6 +23,7 @@ import useTableSort from "../hooks/useTableSort";
import {
invoiceListOptions,
invoiceStatsOptions,
invoiceTotalsOptions,
type Invoice,
type CurrencyAmount,
} from "../lib/queries/invoices";
@@ -283,6 +284,18 @@ export default function Invoices() {
}),
);
// Per-currency total over the WHOLE filtered set of the ISSUED list (not one
// page). Uses the SAME filters as the issued list query so the summary
// matches what's shown. Separate from the KPI `statsQuery` above.
const listTotalsQuery = useQuery(
invoiceTotalsOptions({
search,
status: statusFilter || undefined,
month: statsMonth,
year: statsYear,
}),
);
// Mark first load done in an effect (not during render) to avoid a
// render-phase ref mutation.
useEffect(() => {
@@ -755,6 +768,27 @@ export default function Invoices() {
)
}
/>
{(listTotalsQuery.data?.length ?? 0) > 0 && (
<Box
sx={{
display: "flex",
justifyContent: "flex-end",
mt: 1.5,
px: 1,
gap: 1,
color: "text.secondary",
fontSize: "0.9rem",
}}
>
<span>Celkem:</span>
<Box
component="span"
sx={{ fontWeight: 700, color: "text.primary" }}
>
{formatMultiCurrency(listTotalsQuery.data ?? [])}
</Box>
</Box>
)}
<Pagination
page={page}
pageCount={pagination?.total_pages ?? 1}

View File

@@ -13,12 +13,21 @@ import { useAuth } from "../context/AuthContext";
import Forbidden from "../components/Forbidden";
import apiFetch from "../utils/api";
import { formatCurrency, formatDate, czechPlural } from "../utils/formatters";
import {
formatCurrency,
formatDate,
formatMultiCurrency,
czechPlural,
} from "../utils/formatters";
import useTableSort from "../hooks/useTableSort";
import useDebounce from "../hooks/useDebounce";
import { useQuery, useQueryClient } from "@tanstack/react-query";
import { usePaginatedQuery } from "../hooks/usePaginatedQuery";
import { offerListOptions, offerCustomersOptions } from "../lib/queries/offers";
import {
offerListOptions,
offerStatsOptions,
offerCustomersOptions,
} from "../lib/queries/offers";
import { useApiMutation } from "../lib/queries/mutations";
import {
Button,
@@ -370,6 +379,16 @@ export default function Offers() {
}),
);
// Per-currency total over the WHOLE filtered set (not one page). Uses the
// SAME filters as the list query so the summary matches what's shown.
const statsQuery = useQuery(
offerStatsOptions({
search: debouncedSearch,
status: statusFilter || undefined,
customer_id: customerFilter || undefined,
}),
);
// Mark first load done in an effect (not during render) to avoid a
// render-phase ref mutation.
useEffect(() => {
@@ -875,6 +894,27 @@ export default function Offers() {
)
}
/>
{(statsQuery.data?.length ?? 0) > 0 && (
<Box
sx={{
display: "flex",
justifyContent: "flex-end",
mt: 1.5,
px: 1,
gap: 1,
color: "text.secondary",
fontSize: "0.9rem",
}}
>
<span>Celkem:</span>
<Box
component="span"
sx={{ fontWeight: 700, color: "text.primary" }}
>
{formatMultiCurrency(statsQuery.data ?? [])}
</Box>
</Box>
)}
<Pagination
page={page}
pageCount={pagination?.total_pages ?? 1}

View File

@@ -25,6 +25,7 @@ import { supplierListOptions } from "../lib/queries/common";
import {
receivedInvoiceListOptions,
receivedInvoiceStatsOptions,
receivedInvoiceTotalsOptions,
type ReceivedInvoice,
type CurrencyAmount,
} from "../lib/queries/invoices";
@@ -277,6 +278,18 @@ export default function ReceivedInvoices({
receivedInvoiceStatsOptions(statsMonth, statsYear),
);
// Per-currency total over the WHOLE filtered set (not one page). Uses the
// SAME filters as the list query (month/year/search) so the summary matches
// what's shown. `amount` is GROSS (VAT-inclusive). Separate from the KPI
// `statsQuery` above.
const listTotalsQuery = useQuery(
receivedInvoiceTotalsOptions({
month: statsMonth,
year: statsYear,
search,
}),
);
// Derive list data from query (paginatedJsonQuery returns { data, pagination })
const invoices = listQuery.data?.data ?? [];
@@ -841,6 +854,27 @@ export default function ReceivedInvoices({
}
/>
)}
{(listTotalsQuery.data?.length ?? 0) > 0 && (
<Box
sx={{
display: "flex",
justifyContent: "flex-end",
mt: 1.5,
px: 1,
gap: 1,
color: "text.secondary",
fontSize: "0.9rem",
}}
>
<span>Celkem:</span>
<Box
component="span"
sx={{ fontWeight: 700, color: "text.primary" }}
>
{formatMultiCurrency(listTotalsQuery.data ?? [])}
</Box>
</Box>
)}
</Card>
</motion.div>