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:
@@ -37,12 +37,7 @@ export interface IssuedOrderInput {
|
||||
[key: string]: unknown;
|
||||
}
|
||||
|
||||
interface ListIssuedOrdersParams {
|
||||
page: number;
|
||||
limit: number;
|
||||
skip: number;
|
||||
sort: string;
|
||||
order: string;
|
||||
interface IssuedOrderFilterParams {
|
||||
search?: string;
|
||||
status?: string;
|
||||
customer_id?: number;
|
||||
@@ -50,6 +45,46 @@ interface ListIssuedOrdersParams {
|
||||
year?: number;
|
||||
}
|
||||
|
||||
interface ListIssuedOrdersParams extends IssuedOrderFilterParams {
|
||||
page: number;
|
||||
limit: number;
|
||||
skip: number;
|
||||
sort: string;
|
||||
order: string;
|
||||
}
|
||||
|
||||
export interface CurrencyAmount {
|
||||
amount: number;
|
||||
currency: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shared `where` builder for the issued-order list and the per-currency stats
|
||||
* aggregation, so both stay in sync. month/year filter on order_date (matching
|
||||
* the list).
|
||||
*/
|
||||
function buildIssuedOrderWhere(
|
||||
params: IssuedOrderFilterParams,
|
||||
): Record<string, unknown> {
|
||||
const { search, status, customer_id, month, year } = params;
|
||||
const where: Record<string, unknown> = {};
|
||||
if (status) where.status = status;
|
||||
if (customer_id) where.customer_id = customer_id;
|
||||
if (search) {
|
||||
where.OR = [
|
||||
{ po_number: { contains: search } },
|
||||
{ customers: { name: { contains: search } } },
|
||||
{ customers: { company_id: { contains: search } } },
|
||||
];
|
||||
}
|
||||
if (month && year) {
|
||||
const from = new Date(year, month - 1, 1);
|
||||
const to = new Date(year, month, 1);
|
||||
where.order_date = { gte: from, lt: to };
|
||||
}
|
||||
return where;
|
||||
}
|
||||
|
||||
const VALID_TRANSITIONS: Record<string, string[]> = {
|
||||
draft: ["sent", "cancelled"],
|
||||
sent: ["confirmed", "cancelled"],
|
||||
@@ -95,35 +130,10 @@ export function computeIssuedOrderTotals(
|
||||
}
|
||||
|
||||
export async function listIssuedOrders(params: ListIssuedOrdersParams) {
|
||||
const {
|
||||
page,
|
||||
limit,
|
||||
skip,
|
||||
sort,
|
||||
order,
|
||||
search,
|
||||
status,
|
||||
customer_id,
|
||||
month,
|
||||
year,
|
||||
} = params;
|
||||
const { page, limit, skip, sort, order } = params;
|
||||
const sortField = ALLOWED_SORT_FIELDS.includes(sort) ? sort : "id";
|
||||
|
||||
const where: Record<string, unknown> = {};
|
||||
if (status) where.status = status;
|
||||
if (customer_id) where.customer_id = customer_id;
|
||||
if (search) {
|
||||
where.OR = [
|
||||
{ po_number: { contains: search } },
|
||||
{ customers: { name: { contains: search } } },
|
||||
{ customers: { company_id: { contains: search } } },
|
||||
];
|
||||
}
|
||||
if (month && year) {
|
||||
const from = new Date(year, month - 1, 1);
|
||||
const to = new Date(year, month, 1);
|
||||
where.order_date = { gte: from, lt: to };
|
||||
}
|
||||
const where = buildIssuedOrderWhere(params);
|
||||
|
||||
// Normalize the direction so an unexpected value can't reach Prisma at runtime.
|
||||
const dir: "asc" | "desc" = order === "asc" ? "asc" : "desc";
|
||||
@@ -165,6 +175,44 @@ export async function listIssuedOrders(params: ListIssuedOrdersParams) {
|
||||
return { data: enriched, total, page, limit };
|
||||
}
|
||||
|
||||
/**
|
||||
* Sum issued-order TOTAL (incl. VAT) per currency across the WHOLE filtered set
|
||||
* (not a single page). Reuses `buildIssuedOrderWhere` so filters track the list,
|
||||
* and `computeIssuedOrderTotals` so per-order math matches the list/detail.
|
||||
* Currency defaults to "CZK" when the column is null. Returns one entry per
|
||||
* currency, rounded to 2dp, zero/empty totals dropped.
|
||||
*/
|
||||
export async function getIssuedOrderTotals(
|
||||
params: IssuedOrderFilterParams,
|
||||
): Promise<{ totals: CurrencyAmount[] }> {
|
||||
const where = buildIssuedOrderWhere(params);
|
||||
|
||||
const rows = await prisma.issued_orders.findMany({
|
||||
where,
|
||||
include: { issued_order_items: true },
|
||||
});
|
||||
|
||||
const byCurrency: Record<string, number> = {};
|
||||
for (const o of rows) {
|
||||
const { total } = computeIssuedOrderTotals(
|
||||
o.issued_order_items,
|
||||
o.apply_vat,
|
||||
o.vat_rate,
|
||||
);
|
||||
const cur = o.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 getIssuedOrder(id: number) {
|
||||
const order = await prisma.issued_orders.findUnique({
|
||||
where: { id },
|
||||
|
||||
Reference in New Issue
Block a user