feat(orders): PDF footer = issuer name+email, 'Zobrazit objednávku' button, per-currency list totals

- Issued-order PDF: drop the Vystavil/Schválil signature row; footer now shows
  'Vystavil: <name>' + 'E-mail: <email>' from the logged-in user (authData).
- IssuedOrderDetail PDF button 'Export PDF' -> 'Zobrazit objednávku' (already
  opens inline like invoices' 'Zobrazit fakturu').
- New /orders/stats + /issued-orders/stats endpoints sum order total (incl VAT)
  per currency across the active filter; rendered as 'Celkem: …' beneath both
  the received and issued lists. formatMultiCurrency lifted to a shared util.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
BOHA
2026-06-09 21:50:05 +02:00
parent 78cd7cf2d8
commit 5462fcf944
14 changed files with 627 additions and 84 deletions

View File

@@ -102,12 +102,7 @@ function enrichOrder(o: any) {
};
}
interface ListOrdersParams {
page: number;
limit: number;
skip: number;
sort: string;
order: "asc" | "desc";
interface OrderFilterParams {
status?: string;
customer_id?: number;
search?: string;
@@ -115,15 +110,24 @@ interface ListOrdersParams {
year?: number;
}
export async function listOrders(params: ListOrdersParams) {
const { page, limit, skip, order, search, month, year } = params;
const sortField = ORDER_ALLOWED_SORT_FIELDS.includes(params.sort)
? params.sort
: "id";
interface ListOrdersParams extends OrderFilterParams {
page: number;
limit: number;
skip: number;
sort: string;
order: "asc" | "desc";
}
/**
* Shared `where` builder for the orders list and the per-currency stats
* aggregation, so both stay in sync (any new filter applies to both). month/year
* filters on created_at (matching the list).
*/
function buildOrderWhere(params: OrderFilterParams): Record<string, unknown> {
const { status, customer_id, search, month, year } = params;
const where: Record<string, unknown> = {};
if (params.status) where.status = params.status;
if (params.customer_id) where.customer_id = params.customer_id;
if (status) where.status = status;
if (customer_id) where.customer_id = customer_id;
if (search) {
where.OR = [
{ order_number: { contains: search } },
@@ -136,6 +140,16 @@ export async function listOrders(params: ListOrdersParams) {
const to = new Date(year, month, 1);
where.created_at = { gte: from, lt: to };
}
return where;
}
export async function listOrders(params: ListOrdersParams) {
const { page, limit, skip, order } = params;
const sortField = ORDER_ALLOWED_SORT_FIELDS.includes(params.sort)
? params.sort
: "id";
const where = buildOrderWhere(params);
const [orders, total] = await Promise.all([
prisma.orders.findMany({
@@ -163,6 +177,54 @@ export async function listOrders(params: ListOrdersParams) {
return { data: enriched, total, page, limit };
}
export interface CurrencyAmount {
amount: number;
currency: string;
}
/**
* Sum order TOTAL (incl. VAT) per currency across the WHOLE filtered set (not a
* single page). Reuses `buildOrderWhere` so the filters track the list, and
* `enrichOrder` so the per-order math matches the list/detail exactly. Returns
* one entry per currency, rounded to 2dp, zero/empty totals dropped.
*/
export async function getOrderTotals(
params: OrderFilterParams,
): Promise<{ totals: CurrencyAmount[] }> {
const where = buildOrderWhere(params);
const orders = await prisma.orders.findMany({
where,
include: {
customers: { select: { id: true, name: true } },
order_items: { orderBy: { position: "asc" } },
order_sections: { orderBy: { position: "asc" } },
quotations: { select: { quotation_number: true, project_code: true } },
invoices: {
select: { id: true, invoice_number: true },
take: 1,
orderBy: { id: "desc" },
},
},
});
const byCurrency: Record<string, number> = {};
for (const o of orders) {
const { total, currency } = enrichOrder(o);
const cur = currency || "CZK";
byCurrency[cur] = (byCurrency[cur] || 0) + (Number(total) || 0);
}
const totals: CurrencyAmount[] = Object.entries(byCurrency)
.map(([currency, amount]) => ({
currency,
amount: Math.round(amount * 100) / 100,
}))
.filter((t) => t.amount > 0);
return { totals };
}
export async function getOrder(id: number) {
const order = await prisma.orders.findUnique({
where: { id },