Compare commits
24 Commits
8ee5a443ef
...
v2.4.5
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
575c07aac8 | ||
|
|
4bf245d35c | ||
|
|
79d026e756 | ||
|
|
40a859f5e1 | ||
|
|
ffae1a4e74 | ||
|
|
5c9ebddf50 | ||
|
|
f268907848 | ||
|
|
dd8c699420 | ||
|
|
268a947c85 | ||
|
|
75dc97516e | ||
|
|
d1533ffc4d | ||
|
|
1c1b9dca17 | ||
|
|
7b20c47937 | ||
|
|
7398cad466 | ||
|
|
396a1d37ec | ||
|
|
ca1f07671f | ||
|
|
638264fc7c | ||
|
|
f68a4dafc4 | ||
|
|
700fb47bbc | ||
|
|
c2746d78c9 | ||
|
|
4e534471d9 | ||
|
|
975a555af5 | ||
|
|
6a22195c7d | ||
|
|
74ce24e3fa |
755
CLAUDE.md
755
CLAUDE.md
@@ -1,25 +1,26 @@
|
|||||||
# CLAUDE.md — boha-app-ts
|
# CLAUDE.md — boha-app-ts
|
||||||
|
|
||||||
Business management system for a Czech company, rewritten from PHP to TypeScript/Node.js.
|
Business management system for a Czech company, rewritten from PHP to TypeScript/Node.js.
|
||||||
Handles attendance, invoicing, leave/trips, projects, vehicles, and HR operations.
|
Handles attendance, invoicing, offers/orders, leave/trips, projects, warehouse, vehicles, and HR operations.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Tech Stack
|
## Tech Stack
|
||||||
|
|
||||||
| Layer | Technology |
|
| Layer | Technology |
|
||||||
| -------------- | ---------------------------------------------------------------------------------------------------------------- |
|
| -------------- | ----------------------------------------------------------------------------------------------------- |
|
||||||
| Runtime | Node.js, TypeScript 5.9.3 (strict) |
|
| Runtime | Node.js, TypeScript (strict, all tsconfigs) |
|
||||||
| HTTP Framework | Fastify 5.8.2 |
|
| HTTP Framework | Fastify 5 |
|
||||||
| ORM | Prisma 7.8.0 (Rust-free client + @prisma/adapter-mariadb driver adapter; datasource in prisma.config.ts) → MySQL |
|
| ORM | Prisma 7 (Rust-free client + @prisma/adapter-mariadb; datasource lives in `prisma.config.ts`) → MySQL |
|
||||||
| Auth | JWT (HS256, 15 min) + TOTP 2FA (RFC 6238, otpauth) + bcryptjs |
|
| Auth | JWT (HS256, 15 min) + TOTP 2FA (RFC 6238, otpauth) + bcryptjs |
|
||||||
| Validation | Zod 4.3.6 |
|
| Validation | Zod 4 |
|
||||||
| Frontend | React 19.2.7 + Vite 8.0.0 + Material UI v7 (Emotion) |
|
| Frontend | React 19 + Vite + Material UI v7 (Emotion) + React Query |
|
||||||
| Testing | Vitest 4.1.0 + Supertest |
|
| Testing | Vitest + Supertest against a real `app_test` DB |
|
||||||
| PDF | Puppeteer 24.x |
|
| PDF | Puppeteer (stay on 24.x — v25 is ESM-only, conflicts with the CJS server build) |
|
||||||
| Email | nodemailer 8.x |
|
| Email / Cron | nodemailer / node-cron |
|
||||||
| Cron | node-cron 4.x |
|
| AI | @anthropic-ai/sdk → claude-sonnet-4-6 ("Odin" assistant) |
|
||||||
| AI | @anthropic-ai/sdk 0.102 — Claude Sonnet 4.6 ("Odin" assistant) |
|
|
||||||
|
(Exact versions live in `package.json` — don't trust any pinned here.)
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -27,34 +28,28 @@ Handles attendance, invoicing, leave/trips, projects, vehicles, and HR operation
|
|||||||
|
|
||||||
```
|
```
|
||||||
src/
|
src/
|
||||||
├── server.ts # Fastify server entry point — plugins, routes, error handler
|
├── server.ts # Fastify entry — plugins, routes, error handler
|
||||||
├── routes/admin/ # HTTP route handlers (one file per entity)
|
├── routes/admin/ # HTTP route handlers (one file per entity)
|
||||||
├── services/ # Business logic (no classes, exported functions, uses Prisma directly)
|
├── services/ # Business logic (no classes, exported functions, own Prisma)
|
||||||
├── schemas/ # Zod validation schemas (one file per entity)
|
├── schemas/ # Zod validation schemas (one file per entity; shared coercers in common.ts)
|
||||||
├── middleware/ # auth.ts (requireAuth, requirePermission, optionalAuth)
|
├── middleware/ # auth.ts (requireAuth/requirePermission), security.ts (CSP, HSTS)
|
||||||
│ # security.ts (CSP, HSTS, security headers)
|
├── utils/ # totp, email, audit, date, pdf-shared, content-disposition, html-to-pdf, …
|
||||||
├── utils/ # totp.ts, pdf.ts, email.ts, audit.ts, formatters, etc.
|
├── config/ # env.ts (config singleton, TZ + Date.toJSON override)
|
||||||
├── config/ # env.ts (config singleton, Date.toJSON override)
|
├── types/ # AuthData, JwtPayload, ApiResponse, Prisma re-exports
|
||||||
├── types/ # index.ts (AuthData, JwtPayload, ApiResponse, re-exports from Prisma)
|
├── admin/ # React 19 + MUI v7 frontend
|
||||||
├── admin/ # React 18 + Material UI frontend (~114 .tsx files)
|
|
||||||
│ ├── AdminApp.tsx # Router + lazy-loaded pages
|
│ ├── AdminApp.tsx # Router + lazy-loaded pages
|
||||||
│ ├── theme.ts # MUI theme — light/dark color schemes, tokens, component defaults
|
│ ├── theme.ts / GlobalStyles.tsx
|
||||||
│ ├── GlobalStyles.tsx # App-wide global styles via MUI <GlobalStyles> (reset, typography, utilities)
|
│ ├── ui/ # MUI component kit — pages import from here
|
||||||
│ ├── ui/ # MUI component kit (AppShell, Button, DataTable, Modal, …) — pages import from here
|
│ ├── components/ # Non-page components incl. document/ (shared doc editors), odin/, warehouse/
|
||||||
│ ├── context/ # AuthContext, AlertContext (ThemeContext lives in src/context/)
|
│ ├── context/ # AuthContext, AlertContext (ThemeContext lives in src/context/)
|
||||||
│ ├── components/ # Non-page components: RichEditor (Quill), PlanGrid, file manager, dashboard/ + warehouse/ widgets, odin/ (AI chat)
|
|
||||||
│ ├── pages/ # One file per page/feature
|
│ ├── pages/ # One file per page/feature
|
||||||
│ ├── lib/ # React Query options & mutations (queries/) + shared label maps
|
│ ├── lib/queries/ # React Query options + useApiMutation
|
||||||
│ ├── hooks/ # usePaginatedQuery, useTableSort, useDebounce, useReducedMotion, …
|
│ ├── hooks/ # usePaginatedQuery, useDocumentLock, useUnsavedChangesGuard, useDocumentPdf, …
|
||||||
│ └── utils/ # api.ts (fetch wrapper with token refresh), formatters, helpers
|
│ └── utils/ # api.ts (apiFetch with token refresh), formatters
|
||||||
└── __tests__/ # Vitest tests (17 files: auth, numbering, warehouse, plan, invoices, ai/Odin, received-invoices VAT, …)
|
└── __tests__/ # Vitest suites (server-side only; real app_test DB)
|
||||||
|
|
||||||
prisma/
|
prisma/ # schema.prisma (snake_case columns) + migrations/
|
||||||
├── schema.prisma # 52 models, MySQL, snake_case columns
|
dist/ dist-client/ # Compiled server (CommonJS) / built frontend (Vite)
|
||||||
└── migrations/ # Applied migrations
|
|
||||||
|
|
||||||
dist/ # Compiled server (CommonJS, ES2022)
|
|
||||||
dist-client/ # Built frontend (Vite, ES2020)
|
|
||||||
```
|
```
|
||||||
|
|
||||||
---
|
---
|
||||||
@@ -62,502 +57,364 @@ dist-client/ # Built frontend (Vite, ES2020)
|
|||||||
## Commands
|
## Commands
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# Development
|
npm run dev:server # tsx watch src/server.ts (frontend: npm run dev:client)
|
||||||
npm run dev # Starts server in watch mode (manage frontend separately)
|
npm run build # server + client
|
||||||
npm run dev:server # tsx watch src/server.ts
|
npm test # vitest run — against app_test via .env.test
|
||||||
npm run dev:client # Vite dev server
|
npm run typecheck # tsc -b --noEmit (NOT -p tsconfig.json — that solution file checks nothing)
|
||||||
|
npm run lint # eslint . — react-hooks/rules-of-hooks is an ERROR; keep 0 errors
|
||||||
# Build
|
|
||||||
npm run build # Build server + client
|
|
||||||
npm run build:server # tsc -p tsconfig.server.json → dist/
|
|
||||||
npm run build:client # vite build → dist-client/
|
|
||||||
|
|
||||||
# Run (production)
|
|
||||||
npm start # node dist/server.js
|
|
||||||
|
|
||||||
# Tests
|
|
||||||
npm test # vitest run (single pass) — runs against the app_test DB via .env.test
|
|
||||||
npm run test:watch # vitest watch
|
|
||||||
|
|
||||||
# Quality gates
|
|
||||||
npm run typecheck # tsc -b --noEmit (also type-checks the tests via tsconfig.test.json)
|
|
||||||
npm run lint # eslint . — react-hooks/rules-of-hooks is an ERROR; keep at 0 errors
|
|
||||||
npm run format # prettier --write .
|
npm run format # prettier --write .
|
||||||
|
npx prisma generate # after every schema change (client is not committed)
|
||||||
# Database
|
npx prisma studio # DB browser
|
||||||
npx prisma migrate dev # Create migration from schema changes + apply to dev
|
|
||||||
npx prisma migrate dev --name <descriptive_name> # Named migration
|
|
||||||
npx prisma migrate deploy # Apply pending migrations to production
|
|
||||||
npx prisma generate # Regenerate Prisma client after schema changes
|
|
||||||
npx prisma studio # DB browser GUI
|
|
||||||
npx prisma db seed # (Re)seed the dev database
|
|
||||||
npx prisma migrate diff --from-url <url> --to-schema-datamodel prisma/schema.prisma --script # Preview SQL before prod deploy
|
|
||||||
npx prisma migrate resolve --applied <migration> # Mark migration as applied without running SQL
|
|
||||||
```
|
```
|
||||||
|
|
||||||
**Do not start the dev server.** The user manages it separately.
|
**Do not start the dev server.** The user manages it separately.
|
||||||
|
|
||||||
**Before running `prisma migrate dev` or `prisma db push`, ask the user to stop their dev server and wait for confirmation.** Migrations can conflict with an active database connection from the running server.
|
**Before any migration, ask the user to stop their dev server and wait for
|
||||||
|
confirmation** (the running server holds DB connections).
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Environment Variables
|
## Environment Variables
|
||||||
|
|
||||||
Required:
|
Required: `DATABASE_URL`, `JWT_SECRET`, `TOTP_ENCRYPTION_KEY` (64-char hex).
|
||||||
|
|
||||||
```
|
Optional (defaults in `src/config/env.ts`): `PORT` (prod 3001; dev default 3050
|
||||||
DATABASE_URL=mysql://user:pass@host:3306/dbname
|
hardcoded), `HOST`, `APP_ENV=local|production` (controls CSP/CORS/HSTS),
|
||||||
JWT_SECRET=<64-char hex string>
|
`ACCESS_TOKEN_EXPIRY`, `REFRESH_TOKEN_SESSION_EXPIRY`,
|
||||||
TOTP_ENCRYPTION_KEY=<64-char hex string>
|
`REFRESH_TOKEN_REMEMBER_EXPIRY`, `NAS_PATH` (project files),
|
||||||
```
|
`NAS_INVOICES` + `NAS_ORDERS` (document PDF archives — split trees),
|
||||||
|
`MAX_UPLOAD_SIZE`, `CONTACT_EMAIL_TO/FROM`, `SMTP_FROM`, `LEAVE_NOTIFY_EMAIL`,
|
||||||
|
`APP_URL`, `CORS_ORIGINS`, `ANTHROPIC_API_KEY` (Odin self-hides without it).
|
||||||
|
|
||||||
Optional (with defaults):
|
`.env` for dev, `.env.test` for tests (both gitignored — **never edit env
|
||||||
|
files; the user manages them**).
|
||||||
```
|
|
||||||
PORT=3001 # Production port (dev default: 3050, hardcoded in server.ts)
|
|
||||||
HOST=127.0.0.1
|
|
||||||
APP_ENV=local|production # Default: local. Controls CSP, CORS, HSTS
|
|
||||||
ACCESS_TOKEN_EXPIRY=900 # 15 minutes
|
|
||||||
REFRESH_TOKEN_SESSION_EXPIRY=3600 # 1 hour
|
|
||||||
REFRESH_TOKEN_REMEMBER_EXPIRY=2592000 # 30 days
|
|
||||||
NAS_PATH=Z:/02_PROJEKTY # Network share for project files
|
|
||||||
MAX_UPLOAD_SIZE=52428800 # 50MB
|
|
||||||
CONTACT_EMAIL_TO=
|
|
||||||
CONTACT_EMAIL_FROM=
|
|
||||||
SMTP_FROM=
|
|
||||||
LEAVE_NOTIFY_EMAIL=
|
|
||||||
APP_URL= # Used in email links
|
|
||||||
CORS_ORIGINS= # Comma-separated, production only
|
|
||||||
```
|
|
||||||
|
|
||||||
Use `.env` for dev, `.env.test` for tests.
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Architecture & Key Patterns
|
## Architecture & Key Patterns
|
||||||
|
|
||||||
### Request Flow
|
### Request flow
|
||||||
|
|
||||||
```
|
```
|
||||||
Request → CORS → Cookie → Rate-limit → Security headers
|
Request → CORS → Cookie → Rate-limit → Security headers
|
||||||
→ requirePermission() or requireAuth()
|
→ requirePermission() / requireAnyPermission()
|
||||||
→ Zod schema validation (parseBody helper)
|
→ parseBody(Schema) / parseId(param)
|
||||||
→ Route handler
|
→ Route handler → Service function → Prisma
|
||||||
→ Service function
|
→ success(reply, data) | error(reply, czechMessage, status)
|
||||||
→ Prisma
|
|
||||||
→ success(reply, data) or error(reply, message, status)
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Response Format
|
### Response format
|
||||||
|
|
||||||
All responses use this shape:
|
`{ success: true, data, message?, pagination? }` on success,
|
||||||
|
`{ success: false, error }` on failure. Always the `success()`/`error()`/
|
||||||
|
paginated helpers — never raw `reply.send()`.
|
||||||
|
|
||||||
```typescript
|
### Services
|
||||||
// Success
|
|
||||||
{ success: true, data: T, message?: string, pagination?: {...} }
|
|
||||||
|
|
||||||
// Error
|
Plain exported async functions; services own Prisma, routes stay thin.
|
||||||
{ success: false, error: string }
|
Preferred error shape: return **token literals** (`"not_found"`,
|
||||||
```
|
`"invalid_transition"`, `"supplier_not_found"`, …) and let the ROUTE map
|
||||||
|
tokens to Czech messages + HTTP codes (the offers/issued-orders modules are
|
||||||
Use the `success()` and `error()` helpers in routes — never write raw `reply.send()`.
|
the reference). Legacy services return `{ error: czech, status }` — fine to
|
||||||
|
keep until touched; never the discriminated-union style. Respect soft-delete
|
||||||
### Service Pattern
|
(`is_deleted: false`) in reads where the model has it.
|
||||||
|
|
||||||
Services are plain exported async functions, no classes:
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
// src/services/foo.service.ts
|
|
||||||
export async function getFoo(id: number) {
|
|
||||||
const result = await prisma.foo.findUnique({ where: { id } });
|
|
||||||
if (!result) return { error: "Not found", status: 404 };
|
|
||||||
return { data: result };
|
|
||||||
}
|
|
||||||
|
|
||||||
// src/routes/admin/foo.ts
|
|
||||||
const result = await getFoo(id);
|
|
||||||
if ("error" in result) return error(reply, result.error, result.status ?? 400);
|
|
||||||
return success(reply, result.data);
|
|
||||||
```
|
|
||||||
|
|
||||||
### Error Handling
|
|
||||||
|
|
||||||
- Routes map service errors to HTTP responses using the pattern above.
|
|
||||||
- Global error handler in `server.ts` catches all unhandled exceptions; returns 500 with Czech message.
|
|
||||||
- **Never silently swallow errors.** Even if a failure is non-fatal, log it: `app.log.error(e, 'context')`.
|
|
||||||
- Error messages are in Czech (this is intentional — user-facing messages, Czech company).
|
|
||||||
|
|
||||||
### Permissions
|
### Permissions
|
||||||
|
|
||||||
```typescript
|
`requirePermission("entity.action")` / `requireAnyPermission(…)`. The admin
|
||||||
// Route-level guard
|
role bypasses checks (shortcut: `authData.roleName === "admin"` —
|
||||||
fastify.addHook("preHandler", requirePermission("invoices.view"));
|
⚠️ `AuthData` has **`roleName`**, not `role`; don't type `authData` as `any`).
|
||||||
// or multiple
|
GET list/detail endpoints need a permission guard too, NOT bare `requireAuth`
|
||||||
fastify.addHook(
|
(bare-auth reads leak data to any logged-in user). Frontend rule: a _view_
|
||||||
"preHandler",
|
permission opens pages read-only; an _edit_ permission unlocks fields and
|
||||||
requirePermission("invoices.view", "invoices.edit"),
|
save buttons.
|
||||||
);
|
|
||||||
|
|
||||||
// Admin role bypasses all permission checks
|
### Audit
|
||||||
// Permissions follow the pattern: "entity.action" (e.g., "users.create", "invoices.delete")
|
|
||||||
```
|
|
||||||
|
|
||||||
### Audit Logging
|
`logAudit()` on every create/update/delete (and security-relevant actions)
|
||||||
|
with `oldValues`/`newValues`. Use a `"(koncept)"`-style fallback in
|
||||||
Call `logAudit()` from `src/utils/audit.ts` whenever data is created/updated/deleted.
|
descriptions for unnumbered drafts. Audit failures are non-fatal.
|
||||||
Pass `oldData` and `newData` so the diff is stored. Audit failures are non-fatal.
|
|
||||||
|
|
||||||
### Validation
|
|
||||||
|
|
||||||
Use Zod schemas from `src/schemas/`. All route bodies must be validated:
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
const body = parseBody(FooSchema, request.body);
|
|
||||||
if ("error" in body) return error(reply, body.error, 400);
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Date & Timezone Handling (Critical Gotcha)
|
## Dates & Timezones (Critical — three rules)
|
||||||
|
|
||||||
`src/config/env.ts` sets `process.env.TZ = 'Europe/Prague'` and overrides
|
1. **Global override:** `src/config/env.ts` sets `TZ=Europe/Prague` and
|
||||||
`Date.prototype.toJSON()` to return local time (not UTC). This means:
|
monkey-patches `Date.prototype.toJSON()` to emit LOCAL time (PHP-migration
|
||||||
|
parity). All API date strings are local. Never assume UTC; never manually
|
||||||
|
offset. Do not remove the override.
|
||||||
|
2. **`@db.Date` columns — Prisma truncates filter Dates to the UTC date
|
||||||
|
part.** A local-midnight boundary (`new Date(y, m, d)` = 22:00/23:00 UTC of
|
||||||
|
the _previous_ day) silently shifts the queried window a day back — this was
|
||||||
|
a 14-site production bug class. Build `@db.Date` filter boundaries and
|
||||||
|
"today" writes as UTC-midnight instants of the LOCAL calendar day:
|
||||||
|
`new Date(Date.UTC(y, m, d))` or `utcMidnightOfLocalDay()` from
|
||||||
|
`src/utils/date.ts`; use half-open `gte`/`lt` ranges; never write a bare
|
||||||
|
`new Date()` into a `@db.Date` column (stores yesterday between 00:00–02:00
|
||||||
|
Prague).
|
||||||
|
3. **Two deliberate write regimes — don't "fix" either:** the plan module
|
||||||
|
does date-only math in UTC; attendance/leave writes `@db.Date` at **local
|
||||||
|
noon** (`new Date(y, m, d, 12, 0, 0)`). Frontend "today" comes from
|
||||||
|
`localDateStr` (server) / `normalizeDateStr`/`todayLocalStr` (client) —
|
||||||
|
**never** `new Date().toISOString().split("T")[0]` (UTC date, a day early
|
||||||
|
in the Prague evening).
|
||||||
|
|
||||||
- `JSON.stringify(new Date())` returns local Czech time, not UTC.
|
---
|
||||||
- All API responses with Date fields will contain local time strings.
|
|
||||||
- Prisma stores dates as UTC internally, but they read back as local due to the TZ setting.
|
## Document Modules (offers · orders · issued orders · invoices)
|
||||||
- **Never assume UTC** when working with Date objects in this codebase.
|
|
||||||
- When writing new date comparisons or DB queries, use `new Date()` (already local) — do not manually offset.
|
**Business rules (user/accountant decisions — do not revert):**
|
||||||
- The override exists for PHP migration compatibility and Czech date display.
|
|
||||||
|
- **VAT exists ONLY on invoices and received invoices.** Offers, order
|
||||||
|
confirmations and issued orders are NOT tax documents: net prices only, one
|
||||||
|
total labeled "Celkem bez DPH" / "Total excl. VAT", no VAT columns, no
|
||||||
|
explanatory VAT notice. Their VAT DB columns were dropped. Invoices created
|
||||||
|
from an order prefill the company default VAT rate.
|
||||||
|
- **Issued orders (objednávky vydané) take suppliers** (`sklad_suppliers`,
|
||||||
|
picker lookup at `GET /issued-orders/suppliers` under orders._ perms);
|
||||||
|
offers take customers. Issued orders share the `orders._` permission family
|
||||||
|
(deliberate).
|
||||||
|
- **PDF families:** the offer PDF keeps its own monochrome customer-facing
|
||||||
|
design; invoices + issued orders share the red-accent (#de3a3a) family.
|
||||||
|
- **Free-form PO content lives in the rich-text sections** ("Obsah" on issued
|
||||||
|
orders, "Rozsah projektu" on offers — CZ/EN titles, Quill content). Issued
|
||||||
|
orders deliberately have NO scope-template picker, NO item templates, NO
|
||||||
|
duplicate action.
|
||||||
|
|
||||||
|
**Platform conventions:**
|
||||||
|
|
||||||
|
- **Deferred numbering:** drafts carry a NULL document number
|
||||||
|
(nullable-unique column); the sequence number is consumed in-transaction on
|
||||||
|
finalize (draft→active / draft→sent) via `numbering.service.ts`, and
|
||||||
|
released-if-highest on delete. Never hardcode numbering; previews use the
|
||||||
|
collision-advancing helpers.
|
||||||
|
- **Status machines:** `VALID_TRANSITIONS` maps in the services; detail
|
||||||
|
responses return `valid_transitions` and the UI renders transition buttons
|
||||||
|
from it. Field edits outside the editable states (offers: draft/active;
|
||||||
|
issued: draft/sent) are rejected with an explicit Czech 400 — status-only
|
||||||
|
payloads still pass.
|
||||||
|
- **Edit locking:** lock/heartbeat/unlock route trio on both offers and
|
||||||
|
issued orders (30 s server TTL = 3 missed 10 s client heartbeats); detail
|
||||||
|
enriches `locked_by {user_id, username, full_name}` only when fresh and
|
||||||
|
held by another user; client side is the shared `useDocumentLock` hook +
|
||||||
|
`LockBanner`.
|
||||||
|
- **PDF serving:** `GET …/:id/file` serves the archived NAS copy and falls
|
||||||
|
back to a live render (re-archiving it) on a miss; drafts 404 (no number),
|
||||||
|
so the UI hides PDF buttons on drafts. Numbered-document archives write to
|
||||||
|
a deterministic path and **overwrite in place** — never uniquePath `_N`
|
||||||
|
suffixes. The issued-order PDF gets language from the document's `language`
|
||||||
|
column and carries a Puppeteer `footerTemplate` footer on every page
|
||||||
|
("Vystavil" left, "Strana X z Y"/"Page X of Y" centered) — Chromium has no
|
||||||
|
CSS margin-box footers; use `htmlToPdf(html, { footerTemplate })`.
|
||||||
|
- **Shared modules — extend, never fork local copies:**
|
||||||
|
`src/admin/components/document/` (DocumentItemsEditor, SectionsEditor,
|
||||||
|
LockBanner), hooks `useDocumentLock` / `useUnsavedChangesGuard` /
|
||||||
|
`useDocumentPdf` (+ list variant), `src/admin/lib/documentStatus.ts`,
|
||||||
|
CustomerPicker/SupplierPicker, `src/utils/pdf-shared.ts` (escapeHtml,
|
||||||
|
strict cleanQuillHtml, formatNum NBSP, formatCurrency, formatDate),
|
||||||
|
`src/utils/content-disposition.ts` (RFC 5987 — raw filenames with
|
||||||
|
diacritics in headers make Node throw 500s).
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## TOTP / 2FA
|
## TOTP / 2FA
|
||||||
|
|
||||||
- Secret stored AES-256-GCM encrypted in `users.totp_secret`.
|
Secret AES-256-GCM encrypted in `users.totp_secret` (PHP-legacy base64 and TS
|
||||||
- Supports two encoding formats: PHP legacy (base64 iv+cipher+tag) and TS (hex).
|
hex formats both supported); encrypted backup codes with their own
|
||||||
- Backup codes stored as encrypted JSON array in `users.totp_backup_codes`.
|
`/totp/backup-verify` login endpoint. `company_settings.require_2fa` forces
|
||||||
- When `company_settings.require_2fa = true`, all users must enroll before accessing the app.
|
enrollment. Login flow: password → (if enrolled) single-use 5-min
|
||||||
- Login flow: password → if 2FA enabled → issue `loginToken` (5 min, single-use) → TOTP verify → issue access + refresh tokens.
|
`loginToken` → TOTP/backup verify → access + refresh tokens. The enrollment
|
||||||
|
QR is generated locally with the `qrcode` package (never an external QR URL —
|
||||||
|
CSP blocks it and it would leak the secret).
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Testing
|
## Testing
|
||||||
|
|
||||||
Tests live in `src/__tests__/`. They use Vitest + Supertest against a **real, throwaway test database** (`app_test`).
|
- The suite MUTATES a real MySQL DB → it runs against **`app_test`** (never
|
||||||
|
dev `app`), configured in `.env.test`; `src/__tests__/setup.ts` hard-throws
|
||||||
- **Isolated test DB (`app_test`):** the suite MUTATES a real MySQL DB, so it runs against `app_test` (NOT dev `app`), configured in **`.env.test`** (gitignored). `src/__tests__/setup.ts` **hard-throws** if `DATABASE_URL` doesn't name a `*test*` database — a stray URL can never corrupt dev/prod data. To (re)create it: `CREATE DATABASE app_test;` → copy `.env` to `.env.test` with the DB name swapped + `ANTHROPIC_API_KEY=` blanked → `DATABASE_URL=<app_test-url> npx prisma migrate deploy` → `DATABASE_URL=<app_test-url> npx tsx prisma/seed.ts`.
|
unless `DATABASE_URL` names a `*test*` database.
|
||||||
- The suite spans 18 files / 247 tests — auth (incl. happy-path login/refresh-rotation), numbering, warehouse (incl. FIFO oldest-first), plan, invoices, exchange-rates, schema coercion, NAS file manager, env, manual-create, AI/Odin, received-invoices VAT. Server-side only (no component tests yet).
|
- **After any schema change, apply migrations to the test DB too** or the
|
||||||
- Tests are now **type-checked** by `tsc -b` (via `tsconfig.test.json`) — keep them compiling.
|
suite fails: set `DATABASE_URL` to the app_test URL and run
|
||||||
- Use `buildApp()` helper to spin up the Fastify instance for tests.
|
`npx prisma migrate deploy`.
|
||||||
- Tests use `vitest.config.ts` with `environment: 'node'` and 15s timeout; `fileParallelism: false` (serialized) to avoid `number_sequences` deadlocks.
|
- To (re)create it: `CREATE DATABASE app_test;` → copy `.env` to `.env.test`
|
||||||
- **Do not mock Prisma** — tests hit a real database to catch schema/query bugs.
|
with the DB name swapped + `ANTHROPIC_API_KEY=` blanked → migrate deploy →
|
||||||
|
`npx tsx prisma/seed.ts`.
|
||||||
When adding new features, add tests in `src/__tests__/`. Name test files `<feature>.test.ts`.
|
- **Do not mock Prisma** — real DB catches schema/query bugs. Mock only true
|
||||||
|
externals (Puppeteer via `vi.mock("../utils/html-to-pdf")`, NAS reads via
|
||||||
|
`vi.spyOn`). Files run serialized (`fileParallelism: false`) to avoid
|
||||||
|
sequence deadlocks. Tests are type-checked by `tsc -b`.
|
||||||
|
- Fixture hygiene: far-future dates (year 2098) or unique prefixes + full
|
||||||
|
cleanup; FK-safe deletion order. New features get tests in
|
||||||
|
`src/__tests__/<feature>.test.ts`.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Frontend Conventions
|
## Frontend Conventions
|
||||||
|
|
||||||
- Pages are lazy-loaded via `React.lazy()` in `AdminApp.tsx`.
|
- Pages lazy-load via `React.lazy()` in `AdminApp.tsx`; auth via `useAuth()`,
|
||||||
- Auth state lives in `AuthContext`; use `useAuth()` hook to access it.
|
toasts via `useAlert()`.
|
||||||
- Alerts/toasts use `AlertContext`; use `useAlert()` to show them.
|
- **React Query for all fetching** (query options live in
|
||||||
- Data fetching uses **React Query** (query options + mutations in `src/admin/lib/queries/`); `src/admin/utils/api.ts` (`apiFetch`) handles token refresh automatically — dedupes concurrent refreshes, and token responses carry `expires_in` so the client refreshes BEFORE expiry (no reactive 401 churn).
|
`src/admin/lib/queries/`; no fetch-in-useEffect; prefer deriving state over
|
||||||
- Custom hooks: `usePaginatedQuery`, `useTableSort`, `useDebounce`, `useModalLock`, `useReducedMotion`.
|
effects). Mutations via `useApiMutation` (opt-in `envelope` mode passes the
|
||||||
- Styling: **Material UI v7** (Emotion) — theme in `src/admin/theme.ts`, `sx`/`styled()` in components, app-wide rules in `GlobalStyles.tsx`. **No hand-written `.css` files, no Tailwind.** Use `theme.vars` for colours so light/dark resolve automatically.
|
server message through). `apiFetch` refreshes tokens proactively.
|
||||||
|
- **Rules of Hooks:** every hook runs BEFORE any early return — the
|
||||||
|
`<Forbidden/>` guard goes after all hooks. Lint enforces this as an error.
|
||||||
|
- **Invalidation:** broad domain keys (`["offers"]`, not `["offers","list"]`
|
||||||
|
— prefix matching covers sub-queries), and a mutation invalidates every
|
||||||
|
domain that embeds its data (user CRUD → trips+attendance; vehicle → trips;
|
||||||
|
invoice → orders; attendance/leave → **`["dashboard"]`**). The dashboard
|
||||||
|
also uses `refetchOnMount: "always"` as a backstop. Raw-`apiFetch` writes
|
||||||
|
still must invalidate their domains.
|
||||||
|
- Single sources of truth: status chips/labels in `lib/documentStatus.ts`;
|
||||||
|
audit entity labels in `lib/entityTypeLabels.ts`; plan categories from the
|
||||||
|
DB. Don't duplicate label maps.
|
||||||
|
|
||||||
### Query Invalidation Convention
|
### Styling (MUI v7 — no custom .css, no Tailwind)
|
||||||
|
|
||||||
Mutations must invalidate the **full domain** of any entity they touch. Prefer broad invalidation:
|
- Theme in `src/admin/theme.ts` (cssVariables, light+dark schemes); global
|
||||||
|
rules in `GlobalStyles.tsx`; pages compose the kit in `src/admin/ui/` —
|
||||||
- `["users"]` over `["users", "list"]`
|
reach for raw `@mui/material` only for one-off layout/infra.
|
||||||
- `["trips"]` over `["trips", "vehicles"]`
|
- Colours via **`theme.vars!.palette.*`**; alpha via channel tokens
|
||||||
|
(`rgba(${theme.vars!.palette.primary.mainChannel} / 0.12)`); per-scheme
|
||||||
Reason: React Query's `invalidateQueries` uses prefix matching, so `["trips"]` matches all
|
one-offs via `theme.applyStyles("dark", …)`. No hardcoded hex.
|
||||||
`["trips", ...]` sub-queries. This means new queries are automatically invalidated without
|
- Don't regress: status row tints are channel-alpha washes (never `.light`
|
||||||
updating every mutation handler. Inactive queries are only marked stale, not refetched, so
|
fills — invisible text in dark mode); icon badges are solid `X.main` +
|
||||||
the performance cost is minimal. Optimize to targeted keys only when profiling shows a problem.
|
white glyph; the theme persists ONLY under MUI's `mui-mode` localStorage
|
||||||
|
key (ThemeContext owns `<html data-theme>`); dialogs lock `<html>` via
|
||||||
When entity A embeds/references entity B, A's mutation handlers invalidate B's domain:
|
`useDialogScrollLock`; Modal/ConfirmDialog freeze content through the close
|
||||||
|
fade; ConfirmDialogs stay open with `loading` during their request.
|
||||||
- User CRUD invalidates `["trips"]` and `["attendance"]` (both embed user data)
|
- When refactoring pages, **preserve ALL data logic verbatim** (hooks,
|
||||||
- Vehicle CRUD invalidates `["trips"]` (trips reference vehicles)
|
mutations, invalidate arrays, validation, permissions).
|
||||||
- Invoice CRUD invalidates `["orders"]` (orders reference invoices)
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Frontend — Styling & MUI (migration complete, shipped in v2.0.0)
|
## AI Assistant — "Odin"
|
||||||
|
|
||||||
The admin frontend is **fully on Material UI v7** (Emotion). The old ~7,100 lines of hand-written CSS are gone — **there are no custom `.css` files in `src/admin`**; the only stylesheets imported anywhere are the two third-party libs (`react-quill-new/dist/quill.snow.css`, `leaflet/dist/leaflet.css`). Look-and-feel is "Soft-SaaS shell + dense tables", dark/light preserved. Full history/decisions/gotchas live in agent memory (`project_mui_migration.md`); the original spec/plans are under `docs/superpowers/`.
|
Admin-only (`ai.use` permission) Claude assistant at `/odin`. **Phase-1 scope
|
||||||
|
is invoice import ONLY** — general chat is guarded off in `OdinChat.submit`
|
||||||
**Where styling lives**
|
(text-only messages get a canned reply, no API call). Backend
|
||||||
|
`src/services/ai.service.ts` + `src/routes/admin/ai.ts`; per-call cost ledger
|
||||||
- **Theme — `src/admin/theme.ts`:** `cssVariables` with `colorSchemeSelector: "[data-theme='%s']"`, light + dark `colorSchemes`, tokens, component defaults. Use **`theme.vars!.palette.*`** (the `vars` field is typed optional → `!`) so colours resolve per scheme; for alpha use the channel tokens — `rgba(${theme.vars!.palette.primary.mainChannel} / 0.12)`; for per-scheme one-offs use `theme.applyStyles("dark", { … })`.
|
in `ai_usage` with a monthly budget cap (402 when exceeded). Invoice flow:
|
||||||
- **Global rules — `src/admin/GlobalStyles.tsx`:** reset, typography, scrollbar, `::selection`, view-transition timing, and the utility classes (`.text-*`, `.flex-*`, `.mb-*`, …), all theme-aware. (The pre-React bootstrap spinner is inlined in `src/App.tsx` because it mounts before MUI.)
|
PDF → `extract-invoices` (structured output) → review cards → existing
|
||||||
- **Component kit — `src/admin/ui/`:** AppShell, Button, Card, TextField, Select (string-based — convert ids at the boundary), DateField/MonthField/TimeField (date-fns v4, cs), Modal, ConfirmDialog (optional `children` + content freeze), DataTable (sortable + mobile card layout + `rowSx`/`rowDanger`/`rowInactive`), Pagination, Tabs/TabPanel, StatusChip, CheckboxField/SwitchField, Field, Alert, PageHeader, FilterBar, StatCard, ProgressBar, FileUpload, EmptyState, LoadingState, ThemeToggle, PageEnter (staggered page entrance), RichEditorRoot/RichTextView (Quill). Dev-only `/ui-kit` showcase route.
|
`POST /received-invoices`. **`received_invoices.amount` is GROSS
|
||||||
|
(VAT-inclusive)** — VAT is back-calculated (`vatFromGross`). Phase-2 design
|
||||||
**Building / editing pages**
|
notes live in `docs/superpowers/specs/`.
|
||||||
|
|
||||||
- Pages import from the **kit** (`src/admin/ui/`); reach for `@mui/material` (`styled`/`sx`) directly only for one-off layout or infra (theme, GlobalStyles, the Quill/Leaflet wrappers).
|
|
||||||
- Wrap a page's top-level sections in `<PageEnter>`. Filters go in `<FilterBar>` with **bare** controls (no `<Field>` label) at the standard widths.
|
|
||||||
- When refactoring, **preserve ALL data logic verbatim** (hooks, mutations, `invalidate` arrays, validation, permissions); change only presentation.
|
|
||||||
|
|
||||||
**Conventions learned — don't regress**
|
|
||||||
|
|
||||||
- **Status row tints** = subtle channel-alpha washes (`rgba(var(--mui-palette-X-mainChannel) / 0.12)`), NEVER a solid `.light` fill: `.light` is a light colour in BOTH schemes, so white dark-mode text on it is invisible. Voided/disabled rows = `opacity` + muted text.
|
|
||||||
- **Icon badges** = solid `X.main` tile + **white** glyph (not an `X.light` tile + `X.main` glyph — that's low-contrast).
|
|
||||||
- **Theme is single-source:** `src/context/ThemeContext.tsx` owns the `<html data-theme>` attribute + the View-Transitions cross-fade, and persists under MUI's **`mui-mode`** localStorage key (the same key MUI reads on mount). Do NOT add a second theme key — that desyncs page vs toggle on refresh.
|
|
||||||
- **Dialogs** lock `<html>` via `useDialogScrollLock` (MUI only locks `<body>`, and `html{overflow-x:hidden}` makes `<html>` the scroller); Modal/ConfirmDialog freeze title/label/loading through the close fade so nothing flashes. Login renders OUTSIDE AppShell.
|
|
||||||
|
|
||||||
**Gates every change:** `npx tsc -b --noEmit` (NOT `-p tsconfig.json` — a vacuous solution file that checks nothing), `npm run build`, `npx vitest run`, `npm run lint`. The solution `tsconfig.json` now references `tsconfig.test.json` too, so `tsc -b` **type-checks the test files** (previously skipped). ESLint (flat config in `eslint.config.mjs`) enforces `react-hooks/rules-of-hooks` as an **error** — that rule catches the "hook after an early `return`" bug class; keep `npm run lint` at zero errors (warnings are advisory). Prettier config is `.prettierrc.json` (`npm run format`).
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## AI Assistant — "Odin" (shipped v2.1.5)
|
## Database & Migrations
|
||||||
|
|
||||||
Admins-only Claude assistant on the `/odin` page (sidebar nav item "Odin"). **Phase-1 scope is invoice import ONLY** — general chat is guarded off in `OdinChat.submit` to avoid spending API credits: a text-only message gets a canned reply and makes NO AI call. Removing that one guard re-enables the `/chat` path for a later "general assistant" phase.
|
Conventions: snake_case columns; `created_at`/`updated_at` timestamps;
|
||||||
|
soft-delete via `is_deleted` where present; `number_sequences` owns all
|
||||||
|
document numbering.
|
||||||
|
|
||||||
- **SDK / model:** `@anthropic-ai/sdk` → `claude-sonnet-4-6`. Server-side only — `ANTHROPIC_API_KEY` in `.env` (already set on prod; **don't touch env, the user manages it**). The whole feature self-hides when the key is absent (`/ai/usage` returns `configured:false`).
|
**Golden rules:**
|
||||||
- **Backend:** `src/services/ai.service.ts` (chat, `extractInvoice` [PDF→structured-output JSON], cost/budget tracking, conversation CRUD with server-side auto-title), `src/routes/admin/ai.ts` (`/api/admin/ai/*`: usage, budget, chat, extract-invoices, conversations[/:id/messages]), `src/schemas/ai.schema.ts`. Every route `requirePermission("ai.use")` (granted to **admin only** via migration).
|
|
||||||
- **Data:** `ai_usage` (per-call token-cost ledger), `ai_conversations` + `ai_chat_messages` (per-user, FK cascade, auto-title from first message), `company_settings.ai_monthly_budget_usd` (default $50; `assertBudgetAvailable` → 402 at the cap).
|
|
||||||
- **Frontend:** `src/admin/pages/Odin.tsx` → `src/admin/components/odin/`: `OdinChat` (orchestrator + state + the proven submit/extract/save logic), `OdinSidebar` (conversation list; inline ≥md, slide-in Drawer below md), `OdinThread` (messages, framer-motion entrance, Newsreader serif greeting hero), `OdinComposer` (claude-style rounded multiline pill, attach/send icons), `InvoiceReviewCard`, `OdinMark` (animated brand mark — CSS keyframes via `styled`, not inline style; opacity-glow fallback under reduced-motion), `types.ts`. Queries in `src/admin/lib/queries/ai.ts`. `Fraunces`→`Newsreader` font added to `index.html`.
|
|
||||||
- **Invoice flow:** attach PDF(s) → `extract-invoices` → editable review cards → save to the EXISTING `POST /received-invoices`. **`received_invoices.amount` is GROSS (VAT-inclusive)** — VAT is back-calculated via `vatFromGross` in `received-invoices.ts` (`amount * rate/(100+rate)`), used by both the manual form and the AI import. The save button is gated on `invoices.create`.
|
|
||||||
- **Phase 2 (general assistant — NOT built):** forward-looking design notes in `docs/superpowers/specs/2026-06-08-odin-phase2-general-assistant-notes.md` (tool-use over services, structured message-block storage, per-action authorization). Phase-1 spec/plan also under `docs/superpowers/`.
|
|
||||||
|
|
||||||
---
|
- **NEVER `prisma db push`** — it bypasses migration history and causes drift.
|
||||||
|
- **Every DB change is a tracked migration** — schema, permission/seed rows,
|
||||||
|
backfills, one-shot fixes. A migration's `migration.sql` may contain plain
|
||||||
|
`INSERT/UPDATE/DELETE`. No raw SQL against production, ever; `prisma db
|
||||||
|
seed` is dev-only convenience (`prisma/seed.ts` refuses to run with
|
||||||
|
`APP_ENV=production`) — prod baselines belong in migrations.
|
||||||
|
|
||||||
## Database Conventions
|
**Making a schema change (this shell is non-interactive — `prisma migrate
|
||||||
|
dev` will refuse to run; use this recipe):**
|
||||||
- All models use `snake_case` column names; Prisma maps to camelCase in TypeScript.
|
|
||||||
- Soft-delete via `is_deleted` boolean (not all tables, check schema).
|
|
||||||
- Timestamps: `created_at`, `updated_at` (auto-managed by Prisma).
|
|
||||||
- Number sequences (`number_sequences` table) manage invoice/quotation numbering — never hardcode numbering logic.
|
|
||||||
- All significant tables have audit log entries. Check `audit_logs` model for the schema.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Database Migrations (Critical Workflow)
|
|
||||||
|
|
||||||
**Golden rule: NEVER use `prisma db push`.** It bypasses migration history and causes drift
|
|
||||||
between the schema and migration tracking. Always use `prisma migrate dev` for every schema change.
|
|
||||||
|
|
||||||
**Every database change or manipulation must be a tracked Prisma migration.** This includes:
|
|
||||||
|
|
||||||
- Schema changes (tables, columns, indexes, constraints) — via `prisma migrate dev`
|
|
||||||
- Permission/role/seed data changes — via a migration that does the INSERTs/DELETEs explicitly
|
|
||||||
- Lookup data, default settings, or any other row-level baseline that production needs
|
|
||||||
- Backfills, data fixes, and one-shot corrections that should be in sync across environments
|
|
||||||
|
|
||||||
**What is NOT acceptable:**
|
|
||||||
|
|
||||||
- Running raw SQL against production (`mysql -e "..."`, `npx prisma db execute`, `pm2 exec`)
|
|
||||||
- `npx prisma db seed` as the only way to populate data (seed is dev-only convenience; prod must run the same SQL via a migration)
|
|
||||||
- "I'll fix it in the seed file" or "I'll add a row manually" without a migration to back it
|
|
||||||
|
|
||||||
The reason: every change to the production database must be reviewable, reversible, and reproducible from a fresh checkout. External SQL commands and seed-only changes cause drift — prod and dev diverge, rollback gets harder with every manual change, and the next deploy surprises us.
|
|
||||||
|
|
||||||
If you need to insert/update/seed data on production, write a migration. The migration's `migration.sql` is a normal SQL file — `INSERT INTO ...`, `UPDATE ...`, `DELETE ...` are all valid. Prisma will run it via `prisma migrate deploy` like any other migration.
|
|
||||||
|
|
||||||
### Making schema changes
|
|
||||||
|
|
||||||
```
|
|
||||||
1. Edit prisma/schema.prisma
|
|
||||||
2. npx prisma migrate dev --name descriptive_name
|
|
||||||
→ This creates a migration in prisma/migrations/ AND applies it to dev DB
|
|
||||||
3. npx prisma generate
|
|
||||||
4. Commit BOTH schema.prisma AND the new migration folder
|
|
||||||
git add prisma/schema.prisma prisma/migrations/
|
|
||||||
```
|
|
||||||
|
|
||||||
### Verifying before production deploy
|
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# Preview what SQL will run on production (no changes applied)
|
# 1. Edit prisma/schema.prisma (ask the user to stop their dev server first!)
|
||||||
npx prisma migrate diff \
|
# 2. Generate the migration SQL into a new folder:
|
||||||
--from-url "mysql://user:pass@prod:3306/app" \
|
mkdir prisma/migrations/<yyyyMMddHHmmss>_<name>
|
||||||
--to-schema-datamodel prisma/schema.prisma \
|
npx prisma migrate diff --from-config-datasource --to-schema prisma/schema.prisma --script \
|
||||||
--script
|
> prisma/migrations/<...>/migration.sql # strip the BOM if written via PowerShell Out-File!
|
||||||
|
# 3. Review the SQL, then apply + regenerate:
|
||||||
# Empty output = no diff. If it shows SQL, review it before deploying.
|
npx prisma migrate deploy
|
||||||
|
npx prisma generate
|
||||||
|
# 4. Apply to the test DB as well (suite breaks otherwise):
|
||||||
|
# DATABASE_URL=<app_test url> npx prisma migrate deploy
|
||||||
|
# 5. Commit schema.prisma AND the migration folder together.
|
||||||
```
|
```
|
||||||
|
|
||||||
### Deploying migrations to production
|
Deployment, drift checks, hotfix-migration shipping and baselining:
|
||||||
|
see **`docs/release.md`**.
|
||||||
The release process runs `prisma migrate deploy` on production.
|
|
||||||
This applies only pending migrations — safe, idempotent.
|
|
||||||
|
|
||||||
### If migrations get out of sync (drift)
|
|
||||||
|
|
||||||
```bash
|
|
||||||
# Diff production DB against local schema to find drift
|
|
||||||
npx prisma migrate diff \
|
|
||||||
--from-url "mysql://prod" \
|
|
||||||
--to-schema-datamodel prisma/schema.prisma \
|
|
||||||
--script
|
|
||||||
|
|
||||||
# If drift is safe (CREATE only, no DROPs): apply with db push ONCE, then baseline
|
|
||||||
# If drift includes DROPs: investigate before touching production
|
|
||||||
```
|
|
||||||
|
|
||||||
### Baselinining a database that has no migrations
|
|
||||||
|
|
||||||
If production was synced with `db push` and has no `_prisma_migrations` table:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
# 1. Create initial migration locally
|
|
||||||
npx prisma migrate dev --name init
|
|
||||||
|
|
||||||
# 2. Copy to production and mark as applied (no SQL runs)
|
|
||||||
scp -r prisma/migrations user@prod:/var/www/app-ts/prisma/
|
|
||||||
ssh user@prod
|
|
||||||
cd /var/www/app-ts
|
|
||||||
npx prisma migrate resolve --applied <migration_name>
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Conventions (enforced — verified by the 2026-06-06 audit)
|
## Enforced Conventions (single source — merged 2026-06 audits)
|
||||||
|
|
||||||
These are the unified rules across the codebase. Follow them for new code; the
|
|
||||||
audit found and fixed the deviations. Full report: `docs/codebase-audit-2026-06-06.md`.
|
|
||||||
|
|
||||||
**Routes**
|
**Routes**
|
||||||
|
|
||||||
- **Responses:** always `success(reply, data[, status, message])` / `error(reply, msg, status)` / the paginated helper. Never raw `reply.send({ success: true, ... })`. (Some legacy files still do — convert when you touch them.)
|
- `success()`/`error()`/paginated helpers only; `parseId` for numeric params
|
||||||
- **Route ids:** parse numeric params with `parseId((request.params as any).id, reply)` then `if (id === null) return;`. Do not use raw `parseInt` (it yields `NaN`, not a 400).
|
(`if (id === null) return;`); `parseBody` for every body; permission guards
|
||||||
- **Bodies:** validate every body with `parseBody(Schema, request.body)` → `if ("error" in body) return error(reply, body.error, 400)`.
|
on reads AND writes; `logAudit` with old/new values on every mutation.
|
||||||
- **Permissions:** guard with `requirePermission` / `requireAnyPermission`. The admin shortcut is `authData.roleName === "admin"`. ⚠️ `AuthData` has **`roleName`**, not `role` (`role` only exists on `JwtPayload`). Don't type `authData` as `any` — it hides exactly this bug.
|
- Role-management writes are admin-only and re-checked (no creating or
|
||||||
- **Audit:** call `logAudit` on every create/update/delete (and on security-relevant actions like session/token termination) with `oldValues`/`newValues`.
|
rewriting the `admin` role).
|
||||||
|
|
||||||
**Services**
|
|
||||||
|
|
||||||
- Plain exported async functions; return `{ data }` or `{ error, status }` (preferred over discriminated unions). Services own Prisma; routes stay thin.
|
|
||||||
- Respect soft-delete (`is_deleted: false`) in reads where the model has it.
|
|
||||||
|
|
||||||
**Schemas (Zod 4)**
|
**Schemas (Zod 4)**
|
||||||
|
|
||||||
- Use Zod 4 idioms: `z.strictObject({...})` / `z.looseObject({...})` (not deprecated `.strict()` / `.passthrough()`).
|
- `z.strictObject`/`z.looseObject` (not deprecated `.strict()`/`.passthrough()`).
|
||||||
- Shared coercion helpers (number-from-form, nullable-number, boolean-from-form) belong in `src/schemas/common.ts` — don't copy-paste the `z.union([z.number(), z.string()]).transform(Number)` idiom (it's duplicated ~150× today; consolidating is a tracked follow-up). New number coercions should guard against `NaN`.
|
- **Shared coercion helpers in `src/schemas/common.ts` are MANDATORY** for
|
||||||
- User-facing messages in **Czech**; identifiers/keys in English.
|
form-coerced fields: `numberFromForm`, `numberInRange`,
|
||||||
|
`nonNegativeNumberFromForm`, `positiveNumberFromForm`,
|
||||||
|
`intIdFromForm`/`nullableIntIdFromForm`, `nonNegativeIntFromForm`,
|
||||||
|
`booleanFromForm`, `isoDateString`, `dateTimeString`/`nullableDateTimeString`,
|
||||||
|
`timeString`, `emailOrEmpty`, `DocumentSectionSchema`. NEVER the raw
|
||||||
|
`z.union([z.number(), z.string()]).transform(Number)` idiom (silent NaN —
|
||||||
|
the single biggest historical bug class). FK ids → intIdFromForm;
|
||||||
|
quantities/prices → nonNegative/positive; bounded → numberInRange.
|
||||||
|
- The date/time helpers are deliberately **lenient** (strip trailing time
|
||||||
|
components) because edit forms re-submit `toJSON`-serialized values.
|
||||||
|
Optional emails: `emailOrEmpty.nullish()` (a bare `.email()` 400s the form).
|
||||||
|
- Align `max()` caps with the DB column widths (an over-cap string 500s at
|
||||||
|
Prisma instead of 400ing at Zod). Update schemas derive via
|
||||||
|
`.partial()` (+ `.omit()` for immutable fields like document numbers) —
|
||||||
|
and therefore must NOT carry top-level `.default()`s (Zod 4 `.partial()`
|
||||||
|
still injects field defaults into partial payloads).
|
||||||
|
- User-facing messages in **Czech**; identifiers/tokens in English.
|
||||||
|
|
||||||
**Frontend**
|
**Determinism & concurrency**
|
||||||
|
|
||||||
- **Query invalidation:** invalidate the **broad domain key** (`["offers"]`, not `["offers","list"]`). Prefix-matching covers sub-queries.
|
- Timestamp/date-only sorts ALWAYS get an `{ id }` tiebreak (second-precision
|
||||||
- **Single source of truth for shared maps:** audit `entity_type` → Czech label lives in `src/admin/lib/entityTypeLabels.ts` and MUST be keyed to the server's `EntityType` values (add the new key there when you add an entity type). Plan categories come from the DB via `lib/queries/plan.ts`. Don't duplicate label maps across files or across server/client.
|
columns make single-key sorts non-deterministic).
|
||||||
- Prefer deriving state over `useEffect`; use React Query for fetching, not effects.
|
- Read-modify-write of shared rows (stock, balances, sequences) runs inside
|
||||||
|
`prisma.$transaction` with `SELECT … FOR UPDATE` via **`tx.$queryRaw`**
|
||||||
|
(not `$executeRaw`), locking in global ascending-id order. Multi-statement
|
||||||
|
writes (header + items + sections) belong in ONE transaction.
|
||||||
|
- Uniqueness checks go INSIDE the create transaction (pass the `tx` client to
|
||||||
|
the checker) and catch `P2002` → 409 as backstop.
|
||||||
|
|
||||||
**Dates (two deliberate regimes — don't "fix" either)**
|
**Errors**
|
||||||
|
|
||||||
- **Plan module** does all date-only math in **UTC** (`setUTCDate`, `toISOString().slice(0,10)`) because its columns are `@db.Date` (UTC-midnight). Correct and stable.
|
- Never silently swallow — every catch logs (`console.*` in services; there
|
||||||
- **Attendance/leave** writes `@db.Date` at **local noon** (`new Date(y, m, d, 12, 0, 0)`).
|
is no request-scoped logger there). The only allowed silent catch is an
|
||||||
- **Frontend "today" / date-string round-trips:** use `utils/date.ts` `localDateStr` (server) / `normalizeDateStr` (client). **Never** use `new Date().toISOString().split("T")[0]` for "today" — it's the UTC date and is a day early during the late-evening Prague window.
|
_expected_ condition (e.g. ENOENT-as-existence-check) **with a comment**.
|
||||||
|
- Prefer explicit Czech 4xx over silently ignoring submitted fields.
|
||||||
## Conventions (enforced — added by the 2026-06-09 full audit)
|
|
||||||
|
|
||||||
The 2026-06-09 file-by-file audit traced most bugs to a handful of patterns. These are now rules — `npm run lint` + `tsc -b` (which now type-checks tests) catch some automatically.
|
|
||||||
|
|
||||||
**Zod validation — shared coercion helpers are MANDATORY**
|
|
||||||
|
|
||||||
- All numeric/boolean/date/email form fields MUST use the helpers in **`src/schemas/common.ts`**: `numberFromForm`, `numberInRange(min,max)`, `nonNegativeNumberFromForm`, `positiveNumberFromForm`, `intIdFromForm`, `nullableNumberFromForm`/`nullableIntIdFromForm`, `booleanFromForm`, `isoDateString`, `timeString`, `emailOrEmpty`.
|
|
||||||
- **NEVER** the raw `z.union([z.number(), z.string()]).transform(Number)` idiom — it silently yields `NaN` that flows into Prisma/business math (this was the single biggest bug class). FK ids → `intIdFromForm`/`nullableIntIdFromForm`; quantities/prices → `nonNegative`/`positive`; bounded values (VAT `0–100`, month `1–12`) → `numberInRange`.
|
|
||||||
- `isoDateString`/`timeString` are **lenient** (they strip a trailing time/seconds component) because an edit form re-submits a `@db.Date` that the `toJSON` override serialised as `"YYYY-MM-DDT00:00:00"`. Use them for date/time fields, not a bare regex.
|
|
||||||
- An optional email a form may submit as `""` → **`emailOrEmpty.nullish()`** (a bare `.email()` 400s the whole form, blocking unrelated saves).
|
|
||||||
|
|
||||||
**Deterministic ordering — always tiebreak a timestamp sort with `id`**
|
|
||||||
|
|
||||||
- `created_at`/`received_at` are **second-precision** (`@db.Timestamp(0)`/`DateTime(0)`), so `orderBy: { created_at: "desc" }` alone is non-deterministic for same-second rows. ALWAYS add `{ id: "desc" }` (or `asc`) as the secondary sort. (Bit `plan.service.resolveCell`/`resolveGrid` and warehouse FIFO `selectFifoBatches`.)
|
|
||||||
|
|
||||||
**Concurrency — locking discipline for stock / balance / sequence mutations**
|
|
||||||
|
|
||||||
- Any service op that read-modify-writes shared rows (stock, batches, reservations, balances, number sequences) MUST: run inside a `prisma.$transaction`; take the row lock with `SELECT … FOR UPDATE` via **`tx.$queryRaw`** (NOT `$executeRaw` — it does not reliably hold the lock); and lock rows in one global **ascending-id order** (parent → items → batches) so concurrent paths can't deadlock. See `warehouse.service.ts` `lockParentRow`/`lockRowsForUpdate` and `attendance.service.ts` `lockUserRow`.
|
|
||||||
- **Uniqueness checks** (username/email/document number) go INSIDE the create transaction (re-check immediately before insert) and catch `P2002` → 409. A pre-transaction check is a TOCTOU race that surfaces as a 500.
|
|
||||||
|
|
||||||
**Permissions — guard reads, not just writes**
|
|
||||||
|
|
||||||
- GET list/detail endpoints need a `requirePermission`/`requireAnyPermission` guard, NOT bare `requireAuth` (bare-auth reads leak data to any logged-in user). Role-management writes are admin-only and re-checked (no privilege escalation; no creating/rewriting the `admin` role).
|
|
||||||
|
|
||||||
**Frontend — Rules of Hooks (lint-enforced) + no UTC-today**
|
|
||||||
|
|
||||||
- ALL hooks (`useState`, `useApiMutation`, `useMemo`, …) run BEFORE any early `return` (the `<Forbidden/>`/`<Navigate/>` permission guard goes AFTER every hook). `eslint.config.mjs` sets `react-hooks/rules-of-hooks` to **error** — `npm run lint` must stay at 0 errors. This was a systemic crash bug across ~14 pages.
|
|
||||||
- Mutations invalidate the **broad domain key**; a mutation that writes via raw `apiFetch` must still `invalidateQueries` the domain it touched (several dashboard widgets were missing this).
|
|
||||||
|
|
||||||
**Safety**
|
|
||||||
|
|
||||||
- `prisma/seed.ts` **refuses to run when `APP_ENV=production`** (it wipes/reseeds permissions). Never seed prod.
|
|
||||||
- Every catch logs (never silently swallow) — the NAS managers were the worst offenders.
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Known Issues & Gotchas
|
## Known Gotchas
|
||||||
|
|
||||||
1. **Date.prototype.toJSON override** — global monkey-patch in `src/config/env.ts`. Side-effects on third-party libraries that serialize dates. Do not remove without migrating all date serialization.
|
1. **CJS/ESM:** the server compiles to CommonJS; Vitest runs ESM
|
||||||
|
(`vitest.config.ts` bridges it). Beware ESM-only dependencies.
|
||||||
2. **CJS/ESM mismatch in tests** — Server compiles to CommonJS (`tsconfig.server.json`), but Vitest runs in ESM by default. The `vitest.config.ts` resolves this, but be careful when adding dependencies that only support ESM.
|
2. **Rich text / PDF security:** every rich-text/HTML field gets server-side
|
||||||
|
DOMPurify + the strict `cleanQuillHtml` from `src/utils/pdf-shared.ts` on
|
||||||
3. **Mixed error patterns** — Some services return `{ error, status }`, others return discriminated unions `{ type: 'success' | 'error' }`. Prefer `{ error, status }` for consistency with existing routes.
|
the PDF path AND sanitization before any `dangerouslySetInnerHTML`. Never
|
||||||
|
pass unsanitized user data into Puppeteer templates; string-built HTML
|
||||||
4. **Never swallow errors** — log at minimum; never use empty `catch` blocks. The service layer logs via `console.*` (there is no request-scoped pino logger in services). The NAS managers' previously-silent filesystem catches are now logged. One exception: a `catch` that handles an _expected_ condition (e.g. `ENOENT` used as an existence check) may stay silent **only with a comment** saying so.
|
(print views, PDF templates) must `escapeHtml` every interpolation.
|
||||||
|
3. **NAS paths** may be unmounted in dev (`Z:`) — NAS features must degrade
|
||||||
5. **HTML sanitization is in place — keep applying it** — Rich-text fields (invoice notes, quotation scope, order notes) ARE sanitized: server-side DOMPurify (jsdom) plus a `cleanQuillHtml` regex pass at all three PDF routes, and the frontend sanitizes before every `dangerouslySetInnerHTML` (InvoiceDetail, OrderDetail). (The old "sanitization gap" note was stale — verified by the 2026-06-06 audit.) When you add ANY new rich-text/HTML field, apply the same sanitization on BOTH the PDF path and the render path.
|
with logged errors, and tests stub NAS reads.
|
||||||
|
4. **Prisma client**: regenerate after every schema change; it is not
|
||||||
6. **Puppeteer PDF generation** — Runs a headless browser. Input to the HTML template must be sanitized. Do not pass unsanitized user data into PDF templates.
|
committed. (On prod this is a mandatory deploy step — see docs/release.md.)
|
||||||
|
5. **No CSRF tokens** by design (SameSite=Strict + CORS) — do not weaken CORS.
|
||||||
7. **NAS_PATH file access** — Project file uploads write to a network share path. In dev, this path may not be mounted. Features using `NAS_PATH` will fail gracefully (or not) if the path is unavailable.
|
6. **Czech locale hardcoded** in messages/month names — intentional.
|
||||||
|
7. **Cross-login caches:** React Query keys are user-agnostic; the query
|
||||||
8. **Prisma client regeneration** — After any schema change and migration, run `npx prisma generate`. The generated client is not committed to git.
|
cache is cleared on login/logout in AuthContext — keep it that way.
|
||||||
|
|
||||||
9. **No CSRF tokens** — CSRF protection relies on `SameSite=Strict` cookies + CORS. Do not weaken CORS configuration.
|
|
||||||
|
|
||||||
10. **Czech locale hardcoded** — Error messages, month names, and some business logic strings are Czech. This is intentional.
|
|
||||||
|
|
||||||
11. **Seed file is dev-only** — `prisma/seed.ts` is for local dev convenience. It is **not** the production data source. Any data the production database must have (permissions, default roles, baseline rows) belongs in a migration's `migration.sql`, not in the seed file. `npx prisma db seed` must never be run against production.
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Release Process
|
## Release
|
||||||
|
|
||||||
1. Bump version in `package.json`
|
Process, deployment commands, hotfix-migration shipping and verification
|
||||||
2. `npm run build`
|
checklist: **`docs/release.md`**. Run the full test suite before tagging; the
|
||||||
3. Commit and tag (`git tag -a vX.Y.Z`)
|
user picks version numbers. **Never push to production or restart services
|
||||||
4. Push to Gitea (`git push origin master && git push origin vX.Y.Z`)
|
without explicit confirmation.**
|
||||||
5. Create tarball: `tar -czf app-ts-X.Y.Z.tar.gz dist dist-client prisma package.json package-lock.json scripts`
|
|
||||||
6. Deploy via SSH to production server (`boha_admin@192.168.50.100`):
|
|
||||||
- Path: `/var/www/app-ts`
|
|
||||||
- Remove old files: `rm -rf dist dist-client prisma scripts package.json package-lock.json`
|
|
||||||
- Copy tarball to server: `scp app-ts-X.Y.Z.tar.gz boha_admin@192.168.50.100:/tmp/`
|
|
||||||
- Extract tarball: `tar -xzf /tmp/app-ts-X.Y.Z.tar.gz`
|
|
||||||
- Install dependencies: `npm install --omit=dev`
|
|
||||||
- Apply Prisma migrations: `npx prisma migrate deploy`
|
|
||||||
- Restart: `pm2 restart app-ts --update-env`
|
|
||||||
|
|
||||||
### Hotfixing a migration that was added after the tarball was built
|
|
||||||
|
|
||||||
If you write a new `prisma/migrations/<timestamp>_*` folder **after** the
|
|
||||||
release tarball has already been built and shipped, that migration is
|
|
||||||
NOT in the tarball and `prisma migrate deploy` on prod will report
|
|
||||||
"No pending migrations". The release tarball re-build re-runs the whole
|
|
||||||
release, but for a one-off hotfix you can ship just the new migration
|
|
||||||
folder:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
# Local
|
|
||||||
cd prisma/migrations
|
|
||||||
tar -czf /tmp/warehouse_perms_migration.tar.gz <timestamp>_<name>/
|
|
||||||
scp /tmp/warehouse_perms_migration.tar.gz boha_admin@192.168.50.100:/tmp/
|
|
||||||
|
|
||||||
# On prod
|
|
||||||
ssh boha_admin@192.168.50.100
|
|
||||||
cd /var/www/app-ts/prisma/migrations
|
|
||||||
sudo -u boha_admin tar -xzf /tmp/warehouse_perms_migration.tar.gz
|
|
||||||
cd /var/www/app-ts
|
|
||||||
npx prisma migrate deploy
|
|
||||||
pm2 restart app-ts --update-env
|
|
||||||
```
|
|
||||||
|
|
||||||
The migration is still tracked in git and applied via `prisma migrate
|
|
||||||
deploy` — the only thing the tarball is doing is delivering the
|
|
||||||
`migration.sql` to prod, which is the same mechanism the main release
|
|
||||||
uses. This is preferred over running raw SQL on prod (which the policy
|
|
||||||
in "Database Migrations" forbids).
|
|
||||||
|
|
||||||
Do not push directly to production or restart services without confirmation.
|
|
||||||
|
|||||||
96
docs/release.md
Normal file
96
docs/release.md
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
# Release & deployment runbook — boha-app-ts
|
||||||
|
|
||||||
|
Referenced from CLAUDE.md. **Never deploy to production or restart services
|
||||||
|
without explicit user confirmation.**
|
||||||
|
|
||||||
|
## Standard release
|
||||||
|
|
||||||
|
1. Run the full gates locally: `npx vitest run` (all green), `npx tsc -b --noEmit`,
|
||||||
|
`npm run lint` (0 errors).
|
||||||
|
2. Bump version: `npm version X.Y.Z --no-git-tag-version` (the user picks the number).
|
||||||
|
3. `npm run build`
|
||||||
|
4. Commit and tag: `git tag -a vX.Y.Z`
|
||||||
|
5. Push to Gitea: `git push origin master && git push origin vX.Y.Z`
|
||||||
|
6. Create tarball:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
tar -czf app-ts-X.Y.Z.tar.gz dist dist-client prisma prisma.config.ts package.json package-lock.json scripts
|
||||||
|
```
|
||||||
|
|
||||||
|
⚠️ `prisma.config.ts` is REQUIRED — Prisma 7 keeps the datasource URL there;
|
||||||
|
without it, `prisma generate`/`migrate deploy` on prod have no datasource.
|
||||||
|
|
||||||
|
7. Deploy via SSH to production (`boha_admin@192.168.50.100`, path `/var/www/app-ts`):
|
||||||
|
|
||||||
|
```bash
|
||||||
|
scp app-ts-X.Y.Z.tar.gz boha_admin@192.168.50.100:/tmp/
|
||||||
|
ssh boha_admin@192.168.50.100
|
||||||
|
cd /var/www/app-ts
|
||||||
|
rm -rf dist dist-client prisma prisma.config.ts scripts package.json package-lock.json
|
||||||
|
tar -xzf /tmp/app-ts-X.Y.Z.tar.gz
|
||||||
|
npm install --omit=dev
|
||||||
|
npx prisma generate # MANDATORY — see below
|
||||||
|
npx prisma migrate deploy
|
||||||
|
pm2 restart app-ts --update-env
|
||||||
|
```
|
||||||
|
|
||||||
|
**`npx prisma generate` is mandatory.** `npm install` skips client
|
||||||
|
regeneration when dependencies didn't change, leaving a stale client that
|
||||||
|
still selects dropped/renamed columns → P2022 500s in prod (this caused
|
||||||
|
the v2.4.0 incident: every issued-orders query failed until generate ran).
|
||||||
|
|
||||||
|
8. Verify: `pm2 list` (status online, correct version, restart counter not
|
||||||
|
climbing), `npx prisma migrate status` ("up to date"),
|
||||||
|
`curl -s -o /dev/null -w '%{http_code}' http://127.0.0.1:3001/` → 200,
|
||||||
|
and grep logs for errors from the CURRENT pid only (the out log keeps old
|
||||||
|
errors from prior pids).
|
||||||
|
9. Clean up tarballs locally and in `/tmp/` on the server.
|
||||||
|
|
||||||
|
Risky releases (framework jumps, FK/constraint-altering migrations) get a
|
||||||
|
read-only pre-flight first: `prisma migrate status` on prod, verify FK
|
||||||
|
constraint names the migration DROPs, check no data violates new
|
||||||
|
UNIQUE/RESTRICT constraints — and confirm with the user before the
|
||||||
|
irreversible steps. The prod DB is backed up by a cron job (no manual
|
||||||
|
mysqldump needed).
|
||||||
|
|
||||||
|
## Hotfixing a migration added after the tarball was built
|
||||||
|
|
||||||
|
A migration folder created after the release tarball shipped is not on prod,
|
||||||
|
so `prisma migrate deploy` reports "No pending migrations". Ship just the
|
||||||
|
migration folder (still tracked in git — this is NOT raw SQL on prod):
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Local
|
||||||
|
cd prisma/migrations
|
||||||
|
tar -czf /tmp/<name>_migration.tar.gz <timestamp>_<name>/
|
||||||
|
scp /tmp/<name>_migration.tar.gz boha_admin@192.168.50.100:/tmp/
|
||||||
|
|
||||||
|
# On prod
|
||||||
|
ssh boha_admin@192.168.50.100
|
||||||
|
cd /var/www/app-ts/prisma/migrations
|
||||||
|
tar -xzf /tmp/<name>_migration.tar.gz
|
||||||
|
cd /var/www/app-ts
|
||||||
|
npx prisma migrate deploy
|
||||||
|
pm2 restart app-ts --update-env
|
||||||
|
```
|
||||||
|
|
||||||
|
## Drift check / preview before deploy
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Preview what SQL would bring the dev DB (per prisma.config.ts) to the local schema
|
||||||
|
npx prisma migrate diff --from-config-datasource --to-schema prisma/schema.prisma --script
|
||||||
|
```
|
||||||
|
|
||||||
|
Empty output = no diff. (Prisma 7 removed `--from-url`; diffs against another
|
||||||
|
DB need its URL in a config/env override.) If drift includes DROPs,
|
||||||
|
investigate before touching production.
|
||||||
|
|
||||||
|
## Baselining a database that has no migrations
|
||||||
|
|
||||||
|
If a database was synced without migration history (no `_prisma_migrations`
|
||||||
|
table): create the initial migration locally, copy `prisma/migrations` to the
|
||||||
|
server, then mark it applied without running SQL:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npx prisma migrate resolve --applied <migration_name>
|
||||||
|
```
|
||||||
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "app-ts",
|
"name": "app-ts",
|
||||||
"version": "2.3.0",
|
"version": "2.4.5",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "app-ts",
|
"name": "app-ts",
|
||||||
"version": "2.3.0",
|
"version": "2.4.5",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@anthropic-ai/sdk": "^0.102.0",
|
"@anthropic-ai/sdk": "^0.102.0",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "app-ts",
|
"name": "app-ts",
|
||||||
"version": "2.3.0",
|
"version": "2.4.5",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "dist/server.js",
|
"main": "dist/server.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
@@ -14,7 +14,6 @@
|
|||||||
"preview": "vite preview",
|
"preview": "vite preview",
|
||||||
"db:generate": "prisma generate",
|
"db:generate": "prisma generate",
|
||||||
"db:pull": "prisma db pull",
|
"db:pull": "prisma db pull",
|
||||||
"db:push": "prisma db push",
|
|
||||||
"db:studio": "prisma studio",
|
"db:studio": "prisma studio",
|
||||||
"test": "vitest run",
|
"test": "vitest run",
|
||||||
"test:watch": "vitest",
|
"test:watch": "vitest",
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
-- DropForeignKey
|
||||||
|
ALTER TABLE `issued_orders` DROP FOREIGN KEY `issued_orders_ibfk_1`;
|
||||||
|
|
||||||
|
-- DropIndex
|
||||||
|
DROP INDEX `issued_orders_customer_id` ON `issued_orders`;
|
||||||
|
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE `issued_orders` DROP COLUMN `customer_id`,
|
||||||
|
ADD COLUMN `supplier_id` INTEGER NULL;
|
||||||
|
|
||||||
|
-- CreateIndex
|
||||||
|
CREATE INDEX `issued_orders_supplier_id` ON `issued_orders`(`supplier_id`);
|
||||||
|
|
||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE `issued_orders` ADD CONSTRAINT `issued_orders_supplier_fk` FOREIGN KEY (`supplier_id`) REFERENCES `sklad_suppliers`(`id`) ON DELETE RESTRICT ON UPDATE NO ACTION;
|
||||||
|
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE `issued_orders` ADD COLUMN `order_text` VARCHAR(500) NULL;
|
||||||
|
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE `issued_order_items` DROP COLUMN `vat_rate`;
|
||||||
|
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE `issued_orders` DROP COLUMN `apply_vat`,
|
||||||
|
DROP COLUMN `vat_rate`;
|
||||||
|
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE `orders` DROP COLUMN `apply_vat`,
|
||||||
|
DROP COLUMN `vat_rate`;
|
||||||
|
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE `quotations` DROP COLUMN `apply_vat`,
|
||||||
|
DROP COLUMN `vat_rate`;
|
||||||
|
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE `issued_orders` ADD COLUMN `locked_at` DATETIME(0) NULL,
|
||||||
|
ADD COLUMN `locked_by` INTEGER NULL;
|
||||||
|
|
||||||
|
-- CreateTable
|
||||||
|
CREATE TABLE `issued_order_sections` (
|
||||||
|
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||||
|
`issued_order_id` INTEGER NOT NULL,
|
||||||
|
`position` INTEGER NULL DEFAULT 0,
|
||||||
|
`title` VARCHAR(500) NULL,
|
||||||
|
`title_cz` VARCHAR(500) NULL,
|
||||||
|
`content` TEXT NULL,
|
||||||
|
`modified_at` DATETIME(0) NULL,
|
||||||
|
|
||||||
|
INDEX `issued_order_sections_order_id`(`issued_order_id`),
|
||||||
|
PRIMARY KEY (`id`)
|
||||||
|
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||||
|
|
||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE `issued_order_sections` ADD CONSTRAINT `issued_order_sections_fk` FOREIGN KEY (`issued_order_id`) REFERENCES `issued_orders`(`id`) ON DELETE CASCADE ON UPDATE NO ACTION;
|
||||||
|
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE `issued_orders` DROP COLUMN `delivery_terms`,
|
||||||
|
DROP COLUMN `issued_by`,
|
||||||
|
DROP COLUMN `notes`,
|
||||||
|
DROP COLUMN `payment_terms`;
|
||||||
|
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
-- Invoices: rich-text "Obsah" sections (mirrors issued_order_sections) replace
|
||||||
|
-- the printed notes block; notes become internal-only. Existing printed notes
|
||||||
|
-- are preserved by merging them into internal_notes before the column drop
|
||||||
|
-- (both are Quill HTML — concatenation is valid markup).
|
||||||
|
|
||||||
|
-- Preserve data: move printed notes into internal_notes
|
||||||
|
UPDATE `invoices`
|
||||||
|
SET `internal_notes` = CASE
|
||||||
|
WHEN `internal_notes` IS NULL OR `internal_notes` = '' THEN `notes`
|
||||||
|
ELSE CONCAT(`internal_notes`, `notes`)
|
||||||
|
END
|
||||||
|
WHERE `notes` IS NOT NULL AND `notes` <> '';
|
||||||
|
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE `invoices` DROP COLUMN `notes`;
|
||||||
|
|
||||||
|
-- CreateTable
|
||||||
|
CREATE TABLE `invoice_sections` (
|
||||||
|
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||||
|
`invoice_id` INTEGER NOT NULL,
|
||||||
|
`position` INTEGER NULL DEFAULT 0,
|
||||||
|
`title` VARCHAR(500) NULL,
|
||||||
|
`title_cz` VARCHAR(500) NULL,
|
||||||
|
`content` TEXT NULL,
|
||||||
|
`modified_at` DATETIME(0) NULL,
|
||||||
|
|
||||||
|
INDEX `invoice_sections_invoice_id`(`invoice_id`),
|
||||||
|
PRIMARY KEY (`id`)
|
||||||
|
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||||
|
|
||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE `invoice_sections` ADD CONSTRAINT `invoice_sections_fk` FOREIGN KEY (`invoice_id`) REFERENCES `invoices`(`id`) ON DELETE CASCADE ON UPDATE NO ACTION;
|
||||||
@@ -7,29 +7,29 @@ datasource db {
|
|||||||
}
|
}
|
||||||
|
|
||||||
model attendance {
|
model attendance {
|
||||||
id Int @id @default(autoincrement())
|
id Int @id @default(autoincrement())
|
||||||
user_id Int
|
user_id Int
|
||||||
shift_date DateTime @db.Date
|
shift_date DateTime @db.Date
|
||||||
arrival_time DateTime? @db.DateTime(0)
|
arrival_time DateTime? @db.DateTime(0)
|
||||||
arrival_lat Decimal? @db.Decimal(10, 8)
|
arrival_lat Decimal? @db.Decimal(10, 8)
|
||||||
arrival_lng Decimal? @db.Decimal(11, 8)
|
arrival_lng Decimal? @db.Decimal(11, 8)
|
||||||
arrival_accuracy Decimal? @db.Decimal(10, 2)
|
arrival_accuracy Decimal? @db.Decimal(10, 2)
|
||||||
arrival_address String? @db.VarChar(500)
|
arrival_address String? @db.VarChar(500)
|
||||||
break_start DateTime? @db.DateTime(0)
|
break_start DateTime? @db.DateTime(0)
|
||||||
break_end DateTime? @db.DateTime(0)
|
break_end DateTime? @db.DateTime(0)
|
||||||
departure_time DateTime? @db.DateTime(0)
|
departure_time DateTime? @db.DateTime(0)
|
||||||
departure_lat Decimal? @db.Decimal(10, 8)
|
departure_lat Decimal? @db.Decimal(10, 8)
|
||||||
departure_lng Decimal? @db.Decimal(11, 8)
|
departure_lng Decimal? @db.Decimal(11, 8)
|
||||||
departure_accuracy Decimal? @db.Decimal(10, 2)
|
departure_accuracy Decimal? @db.Decimal(10, 2)
|
||||||
departure_address String? @db.VarChar(500)
|
departure_address String? @db.VarChar(500)
|
||||||
notes String? @db.Text
|
notes String? @db.Text
|
||||||
project_id Int?
|
project_id Int?
|
||||||
leave_type attendance_leave_type? @default(work)
|
leave_type attendance_leave_type? @default(work)
|
||||||
leave_hours Decimal? @db.Decimal(4, 2)
|
leave_hours Decimal? @db.Decimal(4, 2)
|
||||||
created_at DateTime? @default(now()) @db.Timestamp(0)
|
created_at DateTime? @default(now()) @db.Timestamp(0)
|
||||||
updated_at DateTime? @default(now()) @db.Timestamp(0)
|
updated_at DateTime? @default(now()) @db.Timestamp(0)
|
||||||
users users @relation(fields: [user_id], references: [id], onDelete: Cascade, onUpdate: NoAction, map: "attendance_ibfk_1")
|
users users @relation(fields: [user_id], references: [id], onDelete: Cascade, onUpdate: NoAction, map: "attendance_ibfk_1")
|
||||||
attendance_project_logs attendance_project_logs[]
|
attendance_project_logs attendance_project_logs[]
|
||||||
|
|
||||||
@@index([user_id, shift_date], map: "idx_attendance_user_date")
|
@@index([user_id, shift_date], map: "idx_attendance_user_date")
|
||||||
@@index([user_id, departure_time], map: "idx_attendance_user_departure")
|
@@index([user_id, departure_time], map: "idx_attendance_user_departure")
|
||||||
@@ -127,54 +127,54 @@ model bank_accounts {
|
|||||||
}
|
}
|
||||||
|
|
||||||
model company_settings {
|
model company_settings {
|
||||||
id Int @id @default(autoincrement())
|
id Int @id @default(autoincrement())
|
||||||
company_name String? @db.VarChar(255)
|
company_name String? @db.VarChar(255)
|
||||||
street String? @db.VarChar(255)
|
street String? @db.VarChar(255)
|
||||||
city String? @db.VarChar(255)
|
city String? @db.VarChar(255)
|
||||||
postal_code String? @db.VarChar(20)
|
postal_code String? @db.VarChar(20)
|
||||||
country String? @db.VarChar(100)
|
country String? @db.VarChar(100)
|
||||||
company_id String? @db.VarChar(50)
|
company_id String? @db.VarChar(50)
|
||||||
vat_id String? @db.VarChar(50)
|
vat_id String? @db.VarChar(50)
|
||||||
custom_fields String? @db.LongText
|
custom_fields String? @db.LongText
|
||||||
logo_data Bytes?
|
logo_data Bytes?
|
||||||
logo_data_dark Bytes? @db.MediumBlob
|
logo_data_dark Bytes? @db.MediumBlob
|
||||||
quotation_prefix String? @db.VarChar(20)
|
quotation_prefix String? @db.VarChar(20)
|
||||||
default_currency String? @default("CZK") @db.VarChar(10)
|
default_currency String? @default("CZK") @db.VarChar(10)
|
||||||
default_vat_rate Decimal? @default(21.00) @db.Decimal(5, 2)
|
default_vat_rate Decimal? @default(21.00) @db.Decimal(5, 2)
|
||||||
uuid String? @unique @db.VarChar(36)
|
uuid String? @unique @db.VarChar(36)
|
||||||
modified_at DateTime? @db.DateTime(0)
|
modified_at DateTime? @db.DateTime(0)
|
||||||
is_deleted Boolean? @default(false)
|
is_deleted Boolean? @default(false)
|
||||||
sync_version Int? @default(0)
|
sync_version Int? @default(0)
|
||||||
order_type_code String? @db.VarChar(10)
|
order_type_code String? @db.VarChar(10)
|
||||||
invoice_type_code String? @db.VarChar(10)
|
invoice_type_code String? @db.VarChar(10)
|
||||||
require_2fa Boolean @default(false)
|
require_2fa Boolean @default(false)
|
||||||
break_threshold_hours Decimal? @default(6.00) @db.Decimal(4, 2)
|
break_threshold_hours Decimal? @default(6.00) @db.Decimal(4, 2)
|
||||||
break_duration_short Int? @default(15)
|
break_duration_short Int? @default(15)
|
||||||
break_duration_long Int? @default(30)
|
break_duration_long Int? @default(30)
|
||||||
clock_rounding_minutes Int? @default(15)
|
clock_rounding_minutes Int? @default(15)
|
||||||
invoice_alert_email String? @db.VarChar(255)
|
invoice_alert_email String? @db.VarChar(255)
|
||||||
leave_notify_email String? @db.VarChar(255)
|
leave_notify_email String? @db.VarChar(255)
|
||||||
max_login_attempts Int? @default(5)
|
max_login_attempts Int? @default(5)
|
||||||
lockout_minutes Int? @default(15)
|
lockout_minutes Int? @default(15)
|
||||||
max_requests_per_minute Int? @default(300)
|
max_requests_per_minute Int? @default(300)
|
||||||
available_vat_rates String? @db.LongText
|
available_vat_rates String? @db.LongText
|
||||||
available_currencies String? @db.LongText
|
available_currencies String? @db.LongText
|
||||||
smtp_from String? @db.VarChar(255)
|
smtp_from String? @db.VarChar(255)
|
||||||
smtp_from_name String? @db.VarChar(255)
|
smtp_from_name String? @db.VarChar(255)
|
||||||
offer_number_pattern String? @db.VarChar(100)
|
offer_number_pattern String? @db.VarChar(100)
|
||||||
order_number_pattern String? @db.VarChar(100)
|
order_number_pattern String? @db.VarChar(100)
|
||||||
invoice_number_pattern String? @db.VarChar(100)
|
invoice_number_pattern String? @db.VarChar(100)
|
||||||
issued_order_number_pattern String? @db.VarChar(100)
|
issued_order_number_pattern String? @db.VarChar(100)
|
||||||
issued_order_type_code String? @db.VarChar(10)
|
issued_order_type_code String? @db.VarChar(10)
|
||||||
project_number_pattern String? @db.VarChar(100)
|
project_number_pattern String? @db.VarChar(100)
|
||||||
project_type_code String? @db.VarChar(10)
|
project_type_code String? @db.VarChar(10)
|
||||||
warehouse_receipt_prefix String? @db.VarChar(20)
|
warehouse_receipt_prefix String? @db.VarChar(20)
|
||||||
warehouse_receipt_number_pattern String? @db.VarChar(100)
|
warehouse_receipt_number_pattern String? @db.VarChar(100)
|
||||||
warehouse_issue_prefix String? @db.VarChar(20)
|
warehouse_issue_prefix String? @db.VarChar(20)
|
||||||
warehouse_issue_number_pattern String? @db.VarChar(100)
|
warehouse_issue_number_pattern String? @db.VarChar(100)
|
||||||
warehouse_inventory_prefix String? @db.VarChar(20)
|
warehouse_inventory_prefix String? @db.VarChar(20)
|
||||||
warehouse_inventory_number_pattern String? @db.VarChar(100)
|
warehouse_inventory_number_pattern String? @db.VarChar(100)
|
||||||
ai_monthly_budget_usd Decimal? @default(50.00) @db.Decimal(10, 2)
|
ai_monthly_budget_usd Decimal? @default(50.00) @db.Decimal(10, 2)
|
||||||
}
|
}
|
||||||
|
|
||||||
model customers {
|
model customers {
|
||||||
@@ -193,55 +193,54 @@ model customers {
|
|||||||
sync_version Int? @default(0)
|
sync_version Int? @default(0)
|
||||||
invoices invoices[]
|
invoices invoices[]
|
||||||
orders orders[]
|
orders orders[]
|
||||||
issued_orders issued_orders[]
|
|
||||||
projects projects[]
|
projects projects[]
|
||||||
quotations quotations[]
|
quotations quotations[]
|
||||||
}
|
}
|
||||||
|
|
||||||
model invoice_items {
|
model invoice_items {
|
||||||
id Int @id @default(autoincrement())
|
id Int @id @default(autoincrement())
|
||||||
invoice_id Int
|
invoice_id Int
|
||||||
description String? @db.VarChar(500)
|
description String? @db.VarChar(500)
|
||||||
item_description String? @db.Text
|
item_description String? @db.Text
|
||||||
quantity Decimal? @default(1.000) @db.Decimal(12, 3)
|
quantity Decimal? @default(1.000) @db.Decimal(12, 3)
|
||||||
unit String? @db.VarChar(20)
|
unit String? @db.VarChar(20)
|
||||||
unit_price Decimal? @default(0.00) @db.Decimal(12, 2)
|
unit_price Decimal? @default(0.00) @db.Decimal(12, 2)
|
||||||
vat_rate Decimal? @default(21.00) @db.Decimal(5, 2)
|
vat_rate Decimal? @default(21.00) @db.Decimal(5, 2)
|
||||||
position Int? @default(0)
|
position Int? @default(0)
|
||||||
invoices invoices @relation(fields: [invoice_id], references: [id], onDelete: Cascade, onUpdate: NoAction, map: "invoice_items_ibfk_1")
|
invoices invoices @relation(fields: [invoice_id], references: [id], onDelete: Cascade, onUpdate: NoAction, map: "invoice_items_ibfk_1")
|
||||||
|
|
||||||
@@index([invoice_id], map: "invoice_id")
|
@@index([invoice_id], map: "invoice_id")
|
||||||
}
|
}
|
||||||
|
|
||||||
model invoices {
|
model invoices {
|
||||||
id Int @id @default(autoincrement())
|
id Int @id @default(autoincrement())
|
||||||
invoice_number String? @unique(map: "idx_invoices_number_unique") @db.VarChar(50)
|
invoice_number String? @unique(map: "idx_invoices_number_unique") @db.VarChar(50)
|
||||||
order_id Int?
|
order_id Int?
|
||||||
customer_id Int?
|
customer_id Int?
|
||||||
status String? @default("issued") @db.VarChar(30)
|
status String? @default("issued") @db.VarChar(30)
|
||||||
currency String? @default("CZK") @db.VarChar(10)
|
currency String? @default("CZK") @db.VarChar(10)
|
||||||
vat_rate Decimal? @default(21.00) @db.Decimal(5, 2)
|
vat_rate Decimal? @default(21.00) @db.Decimal(5, 2)
|
||||||
apply_vat Boolean? @default(true)
|
apply_vat Boolean? @default(true)
|
||||||
payment_method String? @db.VarChar(50)
|
payment_method String? @db.VarChar(50)
|
||||||
constant_symbol String? @db.VarChar(20)
|
constant_symbol String? @db.VarChar(20)
|
||||||
bank_name String? @db.VarChar(255)
|
bank_name String? @db.VarChar(255)
|
||||||
bank_swift String? @db.VarChar(20)
|
bank_swift String? @db.VarChar(20)
|
||||||
bank_iban String? @db.VarChar(50)
|
bank_iban String? @db.VarChar(50)
|
||||||
bank_account String? @db.VarChar(50)
|
bank_account String? @db.VarChar(50)
|
||||||
issue_date DateTime? @db.Date
|
issue_date DateTime? @db.Date
|
||||||
due_date DateTime? @db.Date
|
due_date DateTime? @db.Date
|
||||||
tax_date DateTime? @db.Date
|
tax_date DateTime? @db.Date
|
||||||
paid_date DateTime? @db.Date
|
paid_date DateTime? @db.Date
|
||||||
issued_by String? @db.VarChar(255)
|
issued_by String? @db.VarChar(255)
|
||||||
billing_text String? @db.VarChar(500)
|
billing_text String? @db.VarChar(500)
|
||||||
language String? @default("cs") @db.VarChar(5)
|
language String? @default("cs") @db.VarChar(5)
|
||||||
notes String? @db.Text
|
internal_notes String? @db.Text
|
||||||
internal_notes String? @db.Text
|
created_at DateTime? @default(now()) @db.DateTime(0)
|
||||||
created_at DateTime? @default(now()) @db.DateTime(0)
|
modified_at DateTime? @db.DateTime(0)
|
||||||
modified_at DateTime? @db.DateTime(0)
|
invoice_items invoice_items[]
|
||||||
invoice_items invoice_items[]
|
invoice_sections invoice_sections[]
|
||||||
orders orders? @relation(fields: [order_id], references: [id], onDelete: Restrict, onUpdate: NoAction, map: "invoices_ibfk_1")
|
orders orders? @relation(fields: [order_id], references: [id], onDelete: Restrict, onUpdate: NoAction, map: "invoices_ibfk_1")
|
||||||
customers customers? @relation(fields: [customer_id], references: [id], onDelete: Restrict, onUpdate: NoAction, map: "invoices_ibfk_2")
|
customers customers? @relation(fields: [customer_id], references: [id], onDelete: Restrict, onUpdate: NoAction, map: "invoices_ibfk_2")
|
||||||
|
|
||||||
@@index([customer_id], map: "customer_id")
|
@@index([customer_id], map: "customer_id")
|
||||||
@@index([due_date], map: "idx_invoices_due_date")
|
@@index([due_date], map: "idx_invoices_due_date")
|
||||||
@@ -250,6 +249,19 @@ model invoices {
|
|||||||
@@index([order_id], map: "order_id")
|
@@index([order_id], map: "order_id")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
model invoice_sections {
|
||||||
|
id Int @id @default(autoincrement())
|
||||||
|
invoice_id Int
|
||||||
|
position Int? @default(0)
|
||||||
|
title String? @db.VarChar(500)
|
||||||
|
title_cz String? @db.VarChar(500)
|
||||||
|
content String? @db.Text
|
||||||
|
modified_at DateTime? @db.DateTime(0)
|
||||||
|
invoices invoices @relation(fields: [invoice_id], references: [id], onDelete: Cascade, onUpdate: NoAction, map: "invoice_sections_fk")
|
||||||
|
|
||||||
|
@@index([invoice_id], map: "invoice_sections_invoice_id")
|
||||||
|
}
|
||||||
|
|
||||||
model item_templates {
|
model item_templates {
|
||||||
id Int @id @default(autoincrement())
|
id Int @id @default(autoincrement())
|
||||||
name String? @db.VarChar(255)
|
name String? @db.VarChar(255)
|
||||||
@@ -348,8 +360,6 @@ model orders {
|
|||||||
status String? @default("prijata") @db.VarChar(30)
|
status String? @default("prijata") @db.VarChar(30)
|
||||||
currency String? @default("CZK") @db.VarChar(10)
|
currency String? @default("CZK") @db.VarChar(10)
|
||||||
language String? @default("cs") @db.VarChar(5)
|
language String? @default("cs") @db.VarChar(5)
|
||||||
vat_rate Decimal? @default(21.00) @db.Decimal(5, 2)
|
|
||||||
apply_vat Boolean? @default(true)
|
|
||||||
exchange_rate Decimal? @default(1.0000) @db.Decimal(10, 4)
|
exchange_rate Decimal? @default(1.0000) @db.Decimal(10, 4)
|
||||||
scope_title String? @db.VarChar(500)
|
scope_title String? @db.VarChar(500)
|
||||||
scope_description String? @db.Text
|
scope_description String? @db.Text
|
||||||
@@ -368,31 +378,45 @@ model orders {
|
|||||||
}
|
}
|
||||||
|
|
||||||
model issued_orders {
|
model issued_orders {
|
||||||
id Int @id @default(autoincrement())
|
id Int @id @default(autoincrement())
|
||||||
po_number String? @unique(map: "idx_issued_orders_number_unique") @db.VarChar(50)
|
po_number String? @unique(map: "idx_issued_orders_number_unique") @db.VarChar(50)
|
||||||
customer_id Int?
|
supplier_id Int?
|
||||||
status issued_orders_status @default(draft)
|
status issued_orders_status @default(draft)
|
||||||
currency String? @default("CZK") @db.VarChar(10)
|
currency String? @default("CZK") @db.VarChar(10)
|
||||||
vat_rate Decimal? @default(21.00) @db.Decimal(5, 2)
|
exchange_rate Decimal? @default(1.0000) @db.Decimal(10, 4)
|
||||||
apply_vat Boolean? @default(true)
|
order_date DateTime? @db.Date
|
||||||
exchange_rate Decimal? @default(1.0000) @db.Decimal(10, 4)
|
delivery_date DateTime? @db.Date
|
||||||
order_date DateTime? @db.Date
|
language String? @default("cs") @db.VarChar(5)
|
||||||
delivery_date DateTime? @db.Date
|
order_text String? @db.VarChar(500)
|
||||||
language String? @default("cs") @db.VarChar(5)
|
internal_notes String? @db.Text
|
||||||
delivery_terms String? @db.VarChar(500)
|
locked_by Int?
|
||||||
payment_terms String? @db.VarChar(500)
|
locked_at DateTime? @db.DateTime(0)
|
||||||
issued_by String? @db.VarChar(255)
|
created_at DateTime? @default(now()) @db.DateTime(0)
|
||||||
notes String? @db.Text
|
modified_at DateTime? @db.DateTime(0)
|
||||||
internal_notes String? @db.Text
|
issued_order_items issued_order_items[]
|
||||||
created_at DateTime? @default(now()) @db.DateTime(0)
|
issued_order_sections issued_order_sections[]
|
||||||
modified_at DateTime? @db.DateTime(0)
|
suppliers sklad_suppliers? @relation(fields: [supplier_id], references: [id], onDelete: Restrict, onUpdate: NoAction, map: "issued_orders_supplier_fk")
|
||||||
issued_order_items issued_order_items[]
|
|
||||||
customers customers? @relation(fields: [customer_id], references: [id], onDelete: Restrict, onUpdate: NoAction, map: "issued_orders_ibfk_1")
|
|
||||||
|
|
||||||
@@index([customer_id], map: "issued_orders_customer_id")
|
@@index([supplier_id], map: "issued_orders_supplier_id")
|
||||||
@@index([status, order_date], map: "idx_issued_orders_status_date")
|
@@index([status, order_date], map: "idx_issued_orders_status_date")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Mirrors scope_sections (offers) 1:1 — free-form bilingual rich-text sections
|
||||||
|
// rendered on their own PDF page. Kept as a separate table (not shared) so the
|
||||||
|
// FK cascade and per-document ordering stay simple.
|
||||||
|
model issued_order_sections {
|
||||||
|
id Int @id @default(autoincrement())
|
||||||
|
issued_order_id Int
|
||||||
|
position Int? @default(0)
|
||||||
|
title String? @db.VarChar(500)
|
||||||
|
title_cz String? @db.VarChar(500)
|
||||||
|
content String? @db.Text
|
||||||
|
modified_at DateTime? @db.DateTime(0)
|
||||||
|
issued_orders issued_orders @relation(fields: [issued_order_id], references: [id], onDelete: Cascade, onUpdate: NoAction, map: "issued_order_sections_fk")
|
||||||
|
|
||||||
|
@@index([issued_order_id], map: "issued_order_sections_order_id")
|
||||||
|
}
|
||||||
|
|
||||||
model issued_order_items {
|
model issued_order_items {
|
||||||
id Int @id @default(autoincrement())
|
id Int @id @default(autoincrement())
|
||||||
issued_order_id Int
|
issued_order_id Int
|
||||||
@@ -401,7 +425,6 @@ model issued_order_items {
|
|||||||
quantity Decimal? @default(1.000) @db.Decimal(12, 3)
|
quantity Decimal? @default(1.000) @db.Decimal(12, 3)
|
||||||
unit String? @db.VarChar(20)
|
unit String? @db.VarChar(20)
|
||||||
unit_price Decimal? @default(0.00) @db.Decimal(12, 2)
|
unit_price Decimal? @default(0.00) @db.Decimal(12, 2)
|
||||||
vat_rate Decimal? @default(21.00) @db.Decimal(5, 2)
|
|
||||||
position Int? @default(0)
|
position Int? @default(0)
|
||||||
issued_orders issued_orders @relation(fields: [issued_order_id], references: [id], onDelete: Cascade, onUpdate: NoAction, map: "issued_order_items_ibfk_1")
|
issued_orders issued_orders @relation(fields: [issued_order_id], references: [id], onDelete: Cascade, onUpdate: NoAction, map: "issued_order_items_ibfk_1")
|
||||||
|
|
||||||
@@ -439,29 +462,29 @@ model project_notes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
model projects {
|
model projects {
|
||||||
id Int @id @default(autoincrement())
|
id Int @id @default(autoincrement())
|
||||||
project_number String? @unique @db.VarChar(50)
|
project_number String? @unique @db.VarChar(50)
|
||||||
name String? @db.VarChar(255)
|
name String? @db.VarChar(255)
|
||||||
customer_id Int?
|
customer_id Int?
|
||||||
responsible_user_id Int?
|
responsible_user_id Int?
|
||||||
quotation_id Int?
|
quotation_id Int?
|
||||||
order_id Int?
|
order_id Int?
|
||||||
status String? @default("aktivni") @db.VarChar(30)
|
status String? @default("aktivni") @db.VarChar(30)
|
||||||
start_date DateTime? @db.Date
|
start_date DateTime? @db.Date
|
||||||
end_date DateTime? @db.Date
|
end_date DateTime? @db.Date
|
||||||
notes String? @db.Text
|
notes String? @db.Text
|
||||||
created_at DateTime? @default(now()) @db.DateTime(0)
|
created_at DateTime? @default(now()) @db.DateTime(0)
|
||||||
modified_at DateTime? @db.DateTime(0)
|
modified_at DateTime? @db.DateTime(0)
|
||||||
attendance_project_logs attendance_project_logs[]
|
attendance_project_logs attendance_project_logs[]
|
||||||
project_notes project_notes[]
|
project_notes project_notes[]
|
||||||
sklad_issues sklad_issues[]
|
sklad_issues sklad_issues[]
|
||||||
sklad_reservations sklad_reservations[]
|
sklad_reservations sklad_reservations[]
|
||||||
work_plan_entries work_plan_entries[]
|
work_plan_entries work_plan_entries[]
|
||||||
work_plan_overrides work_plan_overrides[]
|
work_plan_overrides work_plan_overrides[]
|
||||||
users users? @relation(fields: [responsible_user_id], references: [id], onUpdate: NoAction, map: "fk_projects_responsible_user")
|
users users? @relation(fields: [responsible_user_id], references: [id], onUpdate: NoAction, map: "fk_projects_responsible_user")
|
||||||
customers customers? @relation(fields: [customer_id], references: [id], onDelete: Restrict, onUpdate: NoAction, map: "projects_ibfk_1")
|
customers customers? @relation(fields: [customer_id], references: [id], onDelete: Restrict, onUpdate: NoAction, map: "projects_ibfk_1")
|
||||||
quotations quotations? @relation(fields: [quotation_id], references: [id], onDelete: Restrict, onUpdate: NoAction, map: "projects_ibfk_2")
|
quotations quotations? @relation(fields: [quotation_id], references: [id], onDelete: Restrict, onUpdate: NoAction, map: "projects_ibfk_2")
|
||||||
orders orders? @relation(fields: [order_id], references: [id], onDelete: Restrict, onUpdate: NoAction, map: "projects_ibfk_3")
|
orders orders? @relation(fields: [order_id], references: [id], onDelete: Restrict, onUpdate: NoAction, map: "projects_ibfk_3")
|
||||||
|
|
||||||
@@index([customer_id], map: "customer_id")
|
@@index([customer_id], map: "customer_id")
|
||||||
@@index([responsible_user_id], map: "fk_projects_responsible_user")
|
@@index([responsible_user_id], map: "fk_projects_responsible_user")
|
||||||
@@ -486,28 +509,26 @@ model quotation_items {
|
|||||||
}
|
}
|
||||||
|
|
||||||
model quotations {
|
model quotations {
|
||||||
id Int @id @default(autoincrement())
|
id Int @id @default(autoincrement())
|
||||||
quotation_number String? @unique @db.VarChar(50)
|
quotation_number String? @unique @db.VarChar(50)
|
||||||
project_code String? @db.VarChar(100)
|
project_code String? @db.VarChar(100)
|
||||||
customer_id Int?
|
customer_id Int?
|
||||||
created_at DateTime? @default(now()) @db.DateTime(0)
|
created_at DateTime? @default(now()) @db.DateTime(0)
|
||||||
valid_until DateTime? @db.Date
|
valid_until DateTime? @db.Date
|
||||||
currency String? @default("CZK") @db.VarChar(10)
|
currency String? @default("CZK") @db.VarChar(10)
|
||||||
language String? @default("cs") @db.VarChar(5)
|
language String? @default("cs") @db.VarChar(5)
|
||||||
vat_rate Decimal? @default(21.00) @db.Decimal(5, 2)
|
order_id Int?
|
||||||
apply_vat Boolean? @default(true)
|
status String @default("active") @db.VarChar(20)
|
||||||
order_id Int?
|
scope_title String? @db.VarChar(500)
|
||||||
status String @default("active") @db.VarChar(20)
|
scope_description String? @db.Text
|
||||||
scope_title String? @db.VarChar(500)
|
locked_by Int?
|
||||||
scope_description String? @db.Text
|
locked_at DateTime? @db.DateTime(0)
|
||||||
locked_by Int?
|
modified_at DateTime? @db.DateTime(0)
|
||||||
locked_at DateTime? @db.DateTime(0)
|
orders orders[]
|
||||||
modified_at DateTime? @db.DateTime(0)
|
projects projects[]
|
||||||
orders orders[]
|
quotation_items quotation_items[]
|
||||||
projects projects[]
|
customers customers? @relation(fields: [customer_id], references: [id], onDelete: Restrict, onUpdate: NoAction, map: "quotations_ibfk_1")
|
||||||
quotation_items quotation_items[]
|
scope_sections scope_sections[]
|
||||||
customers customers? @relation(fields: [customer_id], references: [id], onDelete: Restrict, onUpdate: NoAction, map: "quotations_ibfk_1")
|
|
||||||
scope_sections scope_sections[]
|
|
||||||
|
|
||||||
@@index([customer_id], map: "customer_id")
|
@@index([customer_id], map: "customer_id")
|
||||||
@@index([quotation_number], map: "idx_quotations_number")
|
@@index([quotation_number], map: "idx_quotations_number")
|
||||||
@@ -581,14 +602,14 @@ model roles {
|
|||||||
}
|
}
|
||||||
|
|
||||||
model scope_sections {
|
model scope_sections {
|
||||||
id Int @id @default(autoincrement())
|
id Int @id @default(autoincrement())
|
||||||
quotation_id Int
|
quotation_id Int
|
||||||
position Int? @default(0)
|
position Int? @default(0)
|
||||||
title String? @db.VarChar(500)
|
title String? @db.VarChar(500)
|
||||||
title_cz String? @db.VarChar(500)
|
title_cz String? @db.VarChar(500)
|
||||||
content String? @db.Text
|
content String? @db.Text
|
||||||
modified_at DateTime? @db.DateTime(0)
|
modified_at DateTime? @db.DateTime(0)
|
||||||
quotations quotations @relation(fields: [quotation_id], references: [id], onDelete: Cascade, onUpdate: NoAction, map: "scope_sections_ibfk_1")
|
quotations quotations @relation(fields: [quotation_id], references: [id], onDelete: Cascade, onUpdate: NoAction, map: "scope_sections_ibfk_1")
|
||||||
|
|
||||||
@@index([quotation_id], map: "quotation_id")
|
@@index([quotation_id], map: "quotation_id")
|
||||||
}
|
}
|
||||||
@@ -657,38 +678,38 @@ model trips {
|
|||||||
}
|
}
|
||||||
|
|
||||||
model users {
|
model users {
|
||||||
id Int @id @default(autoincrement())
|
id Int @id @default(autoincrement())
|
||||||
username String @unique(map: "username") @db.VarChar(50)
|
username String @unique(map: "username") @db.VarChar(50)
|
||||||
email String @unique(map: "email") @db.VarChar(255)
|
email String @unique(map: "email") @db.VarChar(255)
|
||||||
password_hash String @db.VarChar(255)
|
password_hash String @db.VarChar(255)
|
||||||
first_name String @db.VarChar(50)
|
first_name String @db.VarChar(50)
|
||||||
last_name String @db.VarChar(50)
|
last_name String @db.VarChar(50)
|
||||||
role_id Int?
|
role_id Int?
|
||||||
is_active Boolean? @default(true)
|
is_active Boolean? @default(true)
|
||||||
last_login DateTime? @db.Timestamp(0)
|
last_login DateTime? @db.Timestamp(0)
|
||||||
failed_login_attempts Int? @default(0)
|
failed_login_attempts Int? @default(0)
|
||||||
locked_until DateTime? @db.Timestamp(0)
|
locked_until DateTime? @db.Timestamp(0)
|
||||||
password_changed_at DateTime? @default(now()) @db.Timestamp(0)
|
password_changed_at DateTime? @default(now()) @db.Timestamp(0)
|
||||||
created_at DateTime? @default(now()) @db.Timestamp(0)
|
created_at DateTime? @default(now()) @db.Timestamp(0)
|
||||||
updated_at DateTime? @default(now()) @db.Timestamp(0)
|
updated_at DateTime? @default(now()) @db.Timestamp(0)
|
||||||
totp_secret String? @db.VarChar(255)
|
totp_secret String? @db.VarChar(255)
|
||||||
totp_enabled Boolean @default(false)
|
totp_enabled Boolean @default(false)
|
||||||
totp_backup_codes String? @db.Text
|
totp_backup_codes String? @db.Text
|
||||||
totp_last_used_counter Int?
|
totp_last_used_counter Int?
|
||||||
attendance attendance[]
|
attendance attendance[]
|
||||||
leave_balances leave_balances[]
|
leave_balances leave_balances[]
|
||||||
leave_requests_leave_requests_user_idTousers leave_requests[] @relation("leave_requests_user_idTousers")
|
leave_requests_leave_requests_user_idTousers leave_requests[] @relation("leave_requests_user_idTousers")
|
||||||
leave_requests_leave_requests_reviewer_idTousers leave_requests[] @relation("leave_requests_reviewer_idTousers")
|
leave_requests_leave_requests_reviewer_idTousers leave_requests[] @relation("leave_requests_reviewer_idTousers")
|
||||||
projects projects[]
|
projects projects[]
|
||||||
trips trips[]
|
trips trips[]
|
||||||
sklad_receipts_received sklad_receipts[] @relation("SkladReceivedBy")
|
sklad_receipts_received sklad_receipts[] @relation("SkladReceivedBy")
|
||||||
sklad_issues_issued sklad_issues[] @relation("SkladIssuedBy")
|
sklad_issues_issued sklad_issues[] @relation("SkladIssuedBy")
|
||||||
sklad_reservations sklad_reservations[] @relation("SkladReservedBy")
|
sklad_reservations sklad_reservations[] @relation("SkladReservedBy")
|
||||||
work_plan_entries work_plan_entries[] @relation("work_plan_entries_creator")
|
work_plan_entries work_plan_entries[] @relation("work_plan_entries_creator")
|
||||||
work_plan_entries_owned work_plan_entries[]
|
work_plan_entries_owned work_plan_entries[]
|
||||||
work_plan_overrides work_plan_overrides[] @relation("work_plan_overrides_creator")
|
work_plan_overrides work_plan_overrides[] @relation("work_plan_overrides_creator")
|
||||||
work_plan_overrides_owned work_plan_overrides[]
|
work_plan_overrides_owned work_plan_overrides[]
|
||||||
roles roles? @relation(fields: [role_id], references: [id], onUpdate: NoAction, map: "users_ibfk_1")
|
roles roles? @relation(fields: [role_id], references: [id], onUpdate: NoAction, map: "users_ibfk_1")
|
||||||
|
|
||||||
@@index([is_active], map: "idx_users_is_active")
|
@@index([is_active], map: "idx_users_is_active")
|
||||||
@@index([role_id], map: "idx_users_role_id")
|
@@index([role_id], map: "idx_users_role_id")
|
||||||
@@ -787,7 +808,7 @@ model sklad_suppliers {
|
|||||||
name String @db.VarChar(255)
|
name String @db.VarChar(255)
|
||||||
ico String? @db.VarChar(20)
|
ico String? @db.VarChar(20)
|
||||||
dic String? @db.VarChar(20)
|
dic String? @db.VarChar(20)
|
||||||
contact_person String? @db.VarChar(255)
|
contact_person String? @db.VarChar(255)
|
||||||
email String? @db.VarChar(255)
|
email String? @db.VarChar(255)
|
||||||
phone String? @db.VarChar(50)
|
phone String? @db.VarChar(50)
|
||||||
address String? @db.Text
|
address String? @db.Text
|
||||||
@@ -796,7 +817,8 @@ model sklad_suppliers {
|
|||||||
created_at DateTime? @default(now()) @db.DateTime(0)
|
created_at DateTime? @default(now()) @db.DateTime(0)
|
||||||
modified_at DateTime? @db.DateTime(0)
|
modified_at DateTime? @db.DateTime(0)
|
||||||
|
|
||||||
receipts sklad_receipts[]
|
receipts sklad_receipts[]
|
||||||
|
issued_orders issued_orders[]
|
||||||
|
|
||||||
@@map("sklad_suppliers")
|
@@map("sklad_suppliers")
|
||||||
}
|
}
|
||||||
@@ -810,7 +832,7 @@ model sklad_locations {
|
|||||||
created_at DateTime? @default(now()) @db.DateTime(0)
|
created_at DateTime? @default(now()) @db.DateTime(0)
|
||||||
modified_at DateTime? @db.DateTime(0)
|
modified_at DateTime? @db.DateTime(0)
|
||||||
|
|
||||||
items sklad_item_locations[]
|
items sklad_item_locations[]
|
||||||
receipt_lines sklad_receipt_lines[]
|
receipt_lines sklad_receipt_lines[]
|
||||||
issue_lines sklad_issue_lines[]
|
issue_lines sklad_issue_lines[]
|
||||||
inventory_lines sklad_inventory_lines[]
|
inventory_lines sklad_inventory_lines[]
|
||||||
@@ -819,19 +841,19 @@ model sklad_locations {
|
|||||||
}
|
}
|
||||||
|
|
||||||
model sklad_items {
|
model sklad_items {
|
||||||
id Int @id @default(autoincrement())
|
id Int @id @default(autoincrement())
|
||||||
item_number String? @unique @db.VarChar(50)
|
item_number String? @unique @db.VarChar(50)
|
||||||
name String @db.VarChar(255)
|
name String @db.VarChar(255)
|
||||||
description String? @db.Text
|
description String? @db.Text
|
||||||
category_id Int?
|
category_id Int?
|
||||||
unit String @db.VarChar(20)
|
unit String @db.VarChar(20)
|
||||||
min_quantity Decimal? @db.Decimal(12, 3)
|
min_quantity Decimal? @db.Decimal(12, 3)
|
||||||
is_active Boolean @default(true)
|
is_active Boolean @default(true)
|
||||||
notes String? @db.Text
|
notes String? @db.Text
|
||||||
created_at DateTime? @default(now()) @db.DateTime(0)
|
created_at DateTime? @default(now()) @db.DateTime(0)
|
||||||
modified_at DateTime? @db.DateTime(0)
|
modified_at DateTime? @db.DateTime(0)
|
||||||
|
|
||||||
category sklad_categories? @relation(fields: [category_id], references: [id], onDelete: SetNull)
|
category sklad_categories? @relation(fields: [category_id], references: [id], onDelete: SetNull)
|
||||||
batches sklad_batches[]
|
batches sklad_batches[]
|
||||||
item_locations sklad_item_locations[]
|
item_locations sklad_item_locations[]
|
||||||
receipt_lines sklad_receipt_lines[]
|
receipt_lines sklad_receipt_lines[]
|
||||||
@@ -846,50 +868,50 @@ model sklad_items {
|
|||||||
model sklad_batches {
|
model sklad_batches {
|
||||||
id Int @id @default(autoincrement())
|
id Int @id @default(autoincrement())
|
||||||
item_id Int
|
item_id Int
|
||||||
receipt_line_id Int @unique
|
receipt_line_id Int @unique
|
||||||
quantity Decimal @db.Decimal(12, 3)
|
quantity Decimal @db.Decimal(12, 3)
|
||||||
original_qty Decimal @db.Decimal(12, 3)
|
original_qty Decimal @db.Decimal(12, 3)
|
||||||
unit_price Decimal @db.Decimal(12, 2)
|
unit_price Decimal @db.Decimal(12, 2)
|
||||||
received_at DateTime? @db.DateTime(0)
|
received_at DateTime? @db.DateTime(0)
|
||||||
is_consumed Boolean @default(false)
|
is_consumed Boolean @default(false)
|
||||||
|
|
||||||
item sklad_items @relation(fields: [item_id], references: [id], onDelete: Restrict)
|
item sklad_items @relation(fields: [item_id], references: [id], onDelete: Restrict)
|
||||||
receipt_line sklad_receipt_lines @relation(fields: [receipt_line_id], references: [id], onDelete: Restrict)
|
receipt_line sklad_receipt_lines @relation(fields: [receipt_line_id], references: [id], onDelete: Restrict)
|
||||||
issue_lines sklad_issue_lines[]
|
issue_lines sklad_issue_lines[]
|
||||||
|
|
||||||
@@index([item_id, is_consumed, received_at], map: "sklad_batches_fifo")
|
@@index([item_id, is_consumed, received_at], map: "sklad_batches_fifo")
|
||||||
@@map("sklad_batches")
|
@@map("sklad_batches")
|
||||||
}
|
}
|
||||||
|
|
||||||
model sklad_item_locations {
|
model sklad_item_locations {
|
||||||
id Int @id @default(autoincrement())
|
id Int @id @default(autoincrement())
|
||||||
item_id Int
|
item_id Int
|
||||||
location_id Int
|
location_id Int
|
||||||
quantity Decimal @default(0) @db.Decimal(12, 3)
|
quantity Decimal @default(0) @db.Decimal(12, 3)
|
||||||
|
|
||||||
item sklad_items @relation(fields: [item_id], references: [id], onDelete: Restrict)
|
item sklad_items @relation(fields: [item_id], references: [id], onDelete: Restrict)
|
||||||
location sklad_locations @relation(fields: [location_id], references: [id], onDelete: Restrict)
|
location sklad_locations @relation(fields: [location_id], references: [id], onDelete: Restrict)
|
||||||
|
|
||||||
@@unique([item_id, location_id], map: "sklad_item_locations_unique")
|
@@unique([item_id, location_id], map: "sklad_item_locations_unique")
|
||||||
@@map("sklad_item_locations")
|
@@map("sklad_item_locations")
|
||||||
}
|
}
|
||||||
|
|
||||||
model sklad_receipts {
|
model sklad_receipts {
|
||||||
id Int @id @default(autoincrement())
|
id Int @id @default(autoincrement())
|
||||||
receipt_number String? @unique @db.VarChar(50)
|
receipt_number String? @unique @db.VarChar(50)
|
||||||
supplier_id Int?
|
supplier_id Int?
|
||||||
delivery_note_number String? @db.VarChar(100)
|
delivery_note_number String? @db.VarChar(100)
|
||||||
delivery_note_date DateTime? @db.DateTime(0)
|
delivery_note_date DateTime? @db.DateTime(0)
|
||||||
received_by Int?
|
received_by Int?
|
||||||
notes String? @db.Text
|
notes String? @db.Text
|
||||||
status sklad_receipt_status @default(DRAFT)
|
status sklad_receipt_status @default(DRAFT)
|
||||||
created_at DateTime? @default(now()) @db.DateTime(0)
|
created_at DateTime? @default(now()) @db.DateTime(0)
|
||||||
modified_at DateTime? @db.DateTime(0)
|
modified_at DateTime? @db.DateTime(0)
|
||||||
|
|
||||||
supplier sklad_suppliers? @relation(fields: [supplier_id], references: [id], onDelete: SetNull)
|
supplier sklad_suppliers? @relation(fields: [supplier_id], references: [id], onDelete: SetNull)
|
||||||
received_by_user users? @relation("SkladReceivedBy", fields: [received_by], references: [id], onDelete: SetNull)
|
received_by_user users? @relation("SkladReceivedBy", fields: [received_by], references: [id], onDelete: SetNull)
|
||||||
items sklad_receipt_lines[]
|
items sklad_receipt_lines[]
|
||||||
attachments sklad_receipt_attachments[]
|
attachments sklad_receipt_attachments[]
|
||||||
|
|
||||||
@@index([supplier_id], map: "sklad_receipts_supplier_id")
|
@@index([supplier_id], map: "sklad_receipts_supplier_id")
|
||||||
@@index([received_by], map: "sklad_receipts_received_by")
|
@@index([received_by], map: "sklad_receipts_received_by")
|
||||||
@@ -897,50 +919,50 @@ model sklad_receipts {
|
|||||||
}
|
}
|
||||||
|
|
||||||
model sklad_receipt_lines {
|
model sklad_receipt_lines {
|
||||||
id Int @id @default(autoincrement())
|
id Int @id @default(autoincrement())
|
||||||
receipt_id Int
|
receipt_id Int
|
||||||
item_id Int
|
item_id Int
|
||||||
quantity Decimal @db.Decimal(12, 3)
|
quantity Decimal @db.Decimal(12, 3)
|
||||||
unit_price Decimal @db.Decimal(12, 2)
|
unit_price Decimal @db.Decimal(12, 2)
|
||||||
location_id Int?
|
location_id Int?
|
||||||
notes String? @db.VarChar(255)
|
notes String? @db.VarChar(255)
|
||||||
|
|
||||||
receipt sklad_receipts @relation(fields: [receipt_id], references: [id], onDelete: Cascade)
|
receipt sklad_receipts @relation(fields: [receipt_id], references: [id], onDelete: Cascade)
|
||||||
item sklad_items @relation(fields: [item_id], references: [id], onDelete: Restrict)
|
item sklad_items @relation(fields: [item_id], references: [id], onDelete: Restrict)
|
||||||
location sklad_locations? @relation(fields: [location_id], references: [id], onDelete: Restrict)
|
location sklad_locations? @relation(fields: [location_id], references: [id], onDelete: Restrict)
|
||||||
batch sklad_batches?
|
batch sklad_batches?
|
||||||
|
|
||||||
@@index([receipt_id], map: "sklad_receipt_lines_receipt_id")
|
@@index([receipt_id], map: "sklad_receipt_lines_receipt_id")
|
||||||
@@map("sklad_receipt_lines")
|
@@map("sklad_receipt_lines")
|
||||||
}
|
}
|
||||||
|
|
||||||
model sklad_receipt_attachments {
|
model sklad_receipt_attachments {
|
||||||
id Int @id @default(autoincrement())
|
id Int @id @default(autoincrement())
|
||||||
receipt_id Int
|
receipt_id Int
|
||||||
file_name String @db.VarChar(255)
|
file_name String @db.VarChar(255)
|
||||||
file_mime String @db.VarChar(100)
|
file_mime String @db.VarChar(100)
|
||||||
file_size Int
|
file_size Int
|
||||||
file_path String @db.VarChar(500)
|
file_path String @db.VarChar(500)
|
||||||
created_at DateTime? @default(now()) @db.DateTime(0)
|
created_at DateTime? @default(now()) @db.DateTime(0)
|
||||||
|
|
||||||
receipt sklad_receipts @relation(fields: [receipt_id], references: [id], onDelete: Cascade)
|
receipt sklad_receipts @relation(fields: [receipt_id], references: [id], onDelete: Cascade)
|
||||||
|
|
||||||
@@map("sklad_receipt_attachments")
|
@@map("sklad_receipt_attachments")
|
||||||
}
|
}
|
||||||
|
|
||||||
model sklad_issues {
|
model sklad_issues {
|
||||||
id Int @id @default(autoincrement())
|
id Int @id @default(autoincrement())
|
||||||
issue_number String? @unique @db.VarChar(50)
|
issue_number String? @unique @db.VarChar(50)
|
||||||
project_id Int
|
project_id Int
|
||||||
issued_by Int?
|
issued_by Int?
|
||||||
notes String? @db.Text
|
notes String? @db.Text
|
||||||
status sklad_issue_status @default(DRAFT)
|
status sklad_issue_status @default(DRAFT)
|
||||||
created_at DateTime? @default(now()) @db.DateTime(0)
|
created_at DateTime? @default(now()) @db.DateTime(0)
|
||||||
modified_at DateTime? @db.DateTime(0)
|
modified_at DateTime? @db.DateTime(0)
|
||||||
|
|
||||||
project projects @relation(fields: [project_id], references: [id], onDelete: Restrict)
|
project projects @relation(fields: [project_id], references: [id], onDelete: Restrict)
|
||||||
issued_by_user users? @relation("SkladIssuedBy", fields: [issued_by], references: [id], onDelete: SetNull)
|
issued_by_user users? @relation("SkladIssuedBy", fields: [issued_by], references: [id], onDelete: SetNull)
|
||||||
items sklad_issue_lines[]
|
items sklad_issue_lines[]
|
||||||
|
|
||||||
@@index([project_id], map: "sklad_issues_project_id")
|
@@index([project_id], map: "sklad_issues_project_id")
|
||||||
@@index([issued_by], map: "sklad_issues_issued_by")
|
@@index([issued_by], map: "sklad_issues_issued_by")
|
||||||
@@ -948,72 +970,72 @@ model sklad_issues {
|
|||||||
}
|
}
|
||||||
|
|
||||||
model sklad_issue_lines {
|
model sklad_issue_lines {
|
||||||
id Int @id @default(autoincrement())
|
id Int @id @default(autoincrement())
|
||||||
issue_id Int
|
issue_id Int
|
||||||
item_id Int
|
item_id Int
|
||||||
batch_id Int
|
batch_id Int
|
||||||
quantity Decimal @db.Decimal(12, 3)
|
quantity Decimal @db.Decimal(12, 3)
|
||||||
location_id Int?
|
location_id Int?
|
||||||
reservation_id Int?
|
reservation_id Int?
|
||||||
notes String? @db.VarChar(255)
|
notes String? @db.VarChar(255)
|
||||||
|
|
||||||
issue sklad_issues @relation(fields: [issue_id], references: [id], onDelete: Cascade)
|
issue sklad_issues @relation(fields: [issue_id], references: [id], onDelete: Cascade)
|
||||||
item sklad_items @relation(fields: [item_id], references: [id], onDelete: Restrict)
|
item sklad_items @relation(fields: [item_id], references: [id], onDelete: Restrict)
|
||||||
batch sklad_batches @relation(fields: [batch_id], references: [id], onDelete: Restrict)
|
batch sklad_batches @relation(fields: [batch_id], references: [id], onDelete: Restrict)
|
||||||
location sklad_locations? @relation(fields: [location_id], references: [id], onDelete: Restrict)
|
location sklad_locations? @relation(fields: [location_id], references: [id], onDelete: Restrict)
|
||||||
reservation sklad_reservations? @relation(fields: [reservation_id], references: [id], onDelete: SetNull)
|
reservation sklad_reservations? @relation(fields: [reservation_id], references: [id], onDelete: SetNull)
|
||||||
|
|
||||||
@@index([issue_id], map: "sklad_issue_lines_issue_id")
|
@@index([issue_id], map: "sklad_issue_lines_issue_id")
|
||||||
@@map("sklad_issue_lines")
|
@@map("sklad_issue_lines")
|
||||||
}
|
}
|
||||||
|
|
||||||
model sklad_reservations {
|
model sklad_reservations {
|
||||||
id Int @id @default(autoincrement())
|
id Int @id @default(autoincrement())
|
||||||
item_id Int
|
item_id Int
|
||||||
project_id Int
|
project_id Int
|
||||||
quantity Decimal @db.Decimal(12, 3)
|
quantity Decimal @db.Decimal(12, 3)
|
||||||
remaining_qty Decimal @db.Decimal(12, 3)
|
remaining_qty Decimal @db.Decimal(12, 3)
|
||||||
reserved_by Int?
|
reserved_by Int?
|
||||||
notes String? @db.Text
|
notes String? @db.Text
|
||||||
status sklad_reservation_status @default(ACTIVE)
|
status sklad_reservation_status @default(ACTIVE)
|
||||||
created_at DateTime? @default(now()) @db.DateTime(0)
|
created_at DateTime? @default(now()) @db.DateTime(0)
|
||||||
modified_at DateTime? @db.DateTime(0)
|
modified_at DateTime? @db.DateTime(0)
|
||||||
|
|
||||||
item sklad_items @relation(fields: [item_id], references: [id], onDelete: Restrict)
|
item sklad_items @relation(fields: [item_id], references: [id], onDelete: Restrict)
|
||||||
project projects @relation(fields: [project_id], references: [id], onDelete: Restrict)
|
project projects @relation(fields: [project_id], references: [id], onDelete: Restrict)
|
||||||
reserved_by_user users? @relation("SkladReservedBy", fields: [reserved_by], references: [id], onDelete: SetNull)
|
reserved_by_user users? @relation("SkladReservedBy", fields: [reserved_by], references: [id], onDelete: SetNull)
|
||||||
issue_lines sklad_issue_lines[]
|
issue_lines sklad_issue_lines[]
|
||||||
|
|
||||||
@@index([item_id, status], map: "sklad_reservations_item_status")
|
@@index([item_id, status], map: "sklad_reservations_item_status")
|
||||||
@@map("sklad_reservations")
|
@@map("sklad_reservations")
|
||||||
}
|
}
|
||||||
|
|
||||||
model sklad_inventory_sessions {
|
model sklad_inventory_sessions {
|
||||||
id Int @id @default(autoincrement())
|
id Int @id @default(autoincrement())
|
||||||
session_number String? @unique @db.VarChar(50)
|
session_number String? @unique @db.VarChar(50)
|
||||||
notes String? @db.Text
|
notes String? @db.Text
|
||||||
status sklad_inventory_status @default(DRAFT)
|
status sklad_inventory_status @default(DRAFT)
|
||||||
created_at DateTime? @default(now()) @db.DateTime(0)
|
created_at DateTime? @default(now()) @db.DateTime(0)
|
||||||
modified_at DateTime? @db.DateTime(0)
|
modified_at DateTime? @db.DateTime(0)
|
||||||
|
|
||||||
items sklad_inventory_lines[]
|
items sklad_inventory_lines[]
|
||||||
|
|
||||||
@@map("sklad_inventory_sessions")
|
@@map("sklad_inventory_sessions")
|
||||||
}
|
}
|
||||||
|
|
||||||
model sklad_inventory_lines {
|
model sklad_inventory_lines {
|
||||||
id Int @id @default(autoincrement())
|
id Int @id @default(autoincrement())
|
||||||
session_id Int
|
session_id Int
|
||||||
item_id Int
|
item_id Int
|
||||||
location_id Int?
|
location_id Int?
|
||||||
system_qty Decimal @db.Decimal(12, 3)
|
system_qty Decimal @db.Decimal(12, 3)
|
||||||
actual_qty Decimal @db.Decimal(12, 3)
|
actual_qty Decimal @db.Decimal(12, 3)
|
||||||
difference Decimal @db.Decimal(12, 3)
|
difference Decimal @db.Decimal(12, 3)
|
||||||
notes String? @db.VarChar(255)
|
notes String? @db.VarChar(255)
|
||||||
|
|
||||||
session sklad_inventory_sessions @relation(fields: [session_id], references: [id], onDelete: Cascade)
|
session sklad_inventory_sessions @relation(fields: [session_id], references: [id], onDelete: Cascade)
|
||||||
item sklad_items @relation(fields: [item_id], references: [id], onDelete: Restrict)
|
item sklad_items @relation(fields: [item_id], references: [id], onDelete: Restrict)
|
||||||
location sklad_locations? @relation(fields: [location_id], references: [id], onDelete: Restrict)
|
location sklad_locations? @relation(fields: [location_id], references: [id], onDelete: Restrict)
|
||||||
|
|
||||||
@@index([session_id], map: "sklad_inventory_lines_session_id")
|
@@index([session_id], map: "sklad_inventory_lines_session_id")
|
||||||
@@map("sklad_inventory_lines")
|
@@map("sklad_inventory_lines")
|
||||||
@@ -1031,20 +1053,20 @@ model plan_categories {
|
|||||||
}
|
}
|
||||||
|
|
||||||
model work_plan_entries {
|
model work_plan_entries {
|
||||||
id Int @id @default(autoincrement())
|
id Int @id @default(autoincrement())
|
||||||
user_id Int
|
user_id Int
|
||||||
date_from DateTime @db.Date
|
date_from DateTime @db.Date
|
||||||
date_to DateTime @db.Date
|
date_to DateTime @db.Date
|
||||||
project_id Int?
|
project_id Int?
|
||||||
category String @default("work") @db.VarChar(50)
|
category String @default("work") @db.VarChar(50)
|
||||||
note String @db.VarChar(500)
|
note String @db.VarChar(500)
|
||||||
created_by Int
|
created_by Int
|
||||||
created_at DateTime? @default(now()) @db.Timestamp(0)
|
created_at DateTime? @default(now()) @db.Timestamp(0)
|
||||||
updated_at DateTime? @default(now()) @db.Timestamp(0)
|
updated_at DateTime? @default(now()) @db.Timestamp(0)
|
||||||
is_deleted Boolean? @default(false)
|
is_deleted Boolean? @default(false)
|
||||||
users users @relation(fields: [user_id], references: [id], onDelete: Cascade, onUpdate: NoAction)
|
users users @relation(fields: [user_id], references: [id], onDelete: Cascade, onUpdate: NoAction)
|
||||||
projects projects? @relation(fields: [project_id], references: [id], onDelete: SetNull, onUpdate: NoAction)
|
projects projects? @relation(fields: [project_id], references: [id], onDelete: SetNull, onUpdate: NoAction)
|
||||||
creator users @relation("work_plan_entries_creator", fields: [created_by], references: [id], onDelete: Restrict, onUpdate: NoAction)
|
creator users @relation("work_plan_entries_creator", fields: [created_by], references: [id], onDelete: Restrict, onUpdate: NoAction)
|
||||||
|
|
||||||
@@index([user_id, date_from], map: "idx_wpe_user_from")
|
@@index([user_id, date_from], map: "idx_wpe_user_from")
|
||||||
@@index([user_id, date_to], map: "idx_wpe_user_to")
|
@@index([user_id, date_to], map: "idx_wpe_user_to")
|
||||||
@@ -1053,19 +1075,19 @@ model work_plan_entries {
|
|||||||
}
|
}
|
||||||
|
|
||||||
model work_plan_overrides {
|
model work_plan_overrides {
|
||||||
id Int @id @default(autoincrement())
|
id Int @id @default(autoincrement())
|
||||||
user_id Int
|
user_id Int
|
||||||
shift_date DateTime @db.Date
|
shift_date DateTime @db.Date
|
||||||
project_id Int?
|
project_id Int?
|
||||||
category String @db.VarChar(50)
|
category String @db.VarChar(50)
|
||||||
note String @db.VarChar(500)
|
note String @db.VarChar(500)
|
||||||
created_by Int
|
created_by Int
|
||||||
created_at DateTime? @default(now()) @db.Timestamp(0)
|
created_at DateTime? @default(now()) @db.Timestamp(0)
|
||||||
updated_at DateTime? @default(now()) @db.Timestamp(0)
|
updated_at DateTime? @default(now()) @db.Timestamp(0)
|
||||||
is_deleted Boolean? @default(false)
|
is_deleted Boolean? @default(false)
|
||||||
users users @relation(fields: [user_id], references: [id], onDelete: Cascade, onUpdate: NoAction)
|
users users @relation(fields: [user_id], references: [id], onDelete: Cascade, onUpdate: NoAction)
|
||||||
projects projects? @relation(fields: [project_id], references: [id], onDelete: SetNull, onUpdate: NoAction)
|
projects projects? @relation(fields: [project_id], references: [id], onDelete: SetNull, onUpdate: NoAction)
|
||||||
creator users @relation("work_plan_overrides_creator", fields: [created_by], references: [id], onDelete: Restrict, onUpdate: NoAction)
|
creator users @relation("work_plan_overrides_creator", fields: [created_by], references: [id], onDelete: Restrict, onUpdate: NoAction)
|
||||||
|
|
||||||
@@index([user_id, shift_date], map: "idx_wpo_user_date")
|
@@index([user_id, shift_date], map: "idx_wpo_user_date")
|
||||||
@@index([shift_date], map: "idx_wpo_date")
|
@@index([shift_date], map: "idx_wpo_date")
|
||||||
|
|||||||
@@ -258,6 +258,106 @@ describe("GET /api/admin/attendance (complete month, no truncation)", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("GET /api/admin/attendance (month window boundaries, @db.Date)", () => {
|
||||||
|
it("includes the month's own LAST day and excludes the neighbor month's first day", async () => {
|
||||||
|
// shift_date is @db.Date — Prisma compares the filter Dates by their UTC
|
||||||
|
// date part. The old LOCAL-midnight month bounds (22:00/23:00 UTC of the
|
||||||
|
// previous day) shifted the whole window a day back: the June list
|
||||||
|
// included May 31 and DROPPED June 30. Fixture rows sit exactly on the
|
||||||
|
// 2098-06 / 2098-07 boundary.
|
||||||
|
const juneLast = await prisma.attendance.create({
|
||||||
|
data: {
|
||||||
|
user_id: adminUserId,
|
||||||
|
shift_date: new Date(2098, 5, 30, 12, 0, 0), // 2098-06-30, local noon
|
||||||
|
leave_type: "work",
|
||||||
|
arrival_time: new Date(2098, 5, 30, 8, 0, 0),
|
||||||
|
departure_time: new Date(2098, 5, 30, 16, 0, 0),
|
||||||
|
notes: "attendance_wire_test june-last-day",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const julyFirst = await prisma.attendance.create({
|
||||||
|
data: {
|
||||||
|
user_id: adminUserId,
|
||||||
|
shift_date: new Date(2098, 6, 1, 12, 0, 0), // 2098-07-01, local noon
|
||||||
|
leave_type: "work",
|
||||||
|
arrival_time: new Date(2098, 6, 1, 8, 0, 0),
|
||||||
|
departure_time: new Date(2098, 6, 1, 16, 0, 0),
|
||||||
|
notes: "attendance_wire_test july-first-day",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const june = await app.inject({
|
||||||
|
method: "GET",
|
||||||
|
url: "/api/admin/attendance?year=2098&month=6&limit=100",
|
||||||
|
headers: { Authorization: `Bearer ${adminToken}` },
|
||||||
|
});
|
||||||
|
expect(june.statusCode).toBe(200);
|
||||||
|
const juneIds = june.json().data.map((r: { id: number }) => r.id);
|
||||||
|
expect(juneIds).toContain(juneLast.id);
|
||||||
|
expect(juneIds).not.toContain(julyFirst.id);
|
||||||
|
|
||||||
|
const july = await app.inject({
|
||||||
|
method: "GET",
|
||||||
|
url: "/api/admin/attendance?year=2098&month=7&limit=100",
|
||||||
|
headers: { Authorization: `Bearer ${adminToken}` },
|
||||||
|
});
|
||||||
|
expect(july.statusCode).toBe(200);
|
||||||
|
const julyIds = july.json().data.map((r: { id: number }) => r.id);
|
||||||
|
expect(julyIds).toContain(julyFirst.id);
|
||||||
|
expect(julyIds).not.toContain(juneLast.id);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("POST /api/admin/attendance (same-day duplicate window, @db.Date)", () => {
|
||||||
|
it("rejects a second leave record on the SAME day", async () => {
|
||||||
|
const first = await authPost("/api/admin/attendance", adminToken, {
|
||||||
|
user_id: adminUserId,
|
||||||
|
shift_date: "2098-08-10",
|
||||||
|
leave_type: "vacation",
|
||||||
|
leave_hours: 8,
|
||||||
|
notes: "attendance_wire_test dup-leave-first",
|
||||||
|
});
|
||||||
|
expect(first.statusCode).toBe(201);
|
||||||
|
|
||||||
|
// Old bug: the duplicate/overlap window was built from LOCAL midnights of
|
||||||
|
// the UTC-midnight shiftDate, so the validation queried ONLY the previous
|
||||||
|
// day — a same-day duplicate leave sailed through undetected.
|
||||||
|
const second = await authPost("/api/admin/attendance", adminToken, {
|
||||||
|
user_id: adminUserId,
|
||||||
|
shift_date: "2098-08-10",
|
||||||
|
leave_type: "sick",
|
||||||
|
leave_hours: 8,
|
||||||
|
notes: "attendance_wire_test dup-leave-second",
|
||||||
|
});
|
||||||
|
expect(second.statusCode).toBe(400);
|
||||||
|
expect(second.json().success).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does NOT falsely reject a leave on the day AFTER an existing record", async () => {
|
||||||
|
const first = await authPost("/api/admin/attendance", adminToken, {
|
||||||
|
user_id: adminUserId,
|
||||||
|
shift_date: "2098-08-10",
|
||||||
|
leave_type: "vacation",
|
||||||
|
leave_hours: 8,
|
||||||
|
notes: "attendance_wire_test neighbor-day-first",
|
||||||
|
});
|
||||||
|
expect(first.statusCode).toBe(201);
|
||||||
|
|
||||||
|
// Old bug (the other half): creating a record for the NEXT day queried
|
||||||
|
// the window [Aug 10, Aug 11) — i.e. yesterday's records — and bounced
|
||||||
|
// with a false "záznam o nepřítomnosti již existuje".
|
||||||
|
const nextDay = await authPost("/api/admin/attendance", adminToken, {
|
||||||
|
user_id: adminUserId,
|
||||||
|
shift_date: "2098-08-11",
|
||||||
|
leave_type: "vacation",
|
||||||
|
leave_hours: 8,
|
||||||
|
notes: "attendance_wire_test neighbor-day-next",
|
||||||
|
});
|
||||||
|
expect(nextDay.statusCode).toBe(201);
|
||||||
|
expect(nextDay.json().success).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe("PUT /api/admin/attendance/:id (combined datetimes)", () => {
|
describe("PUT /api/admin/attendance/:id (combined datetimes)", () => {
|
||||||
it("updates a record with combined datetimes (incl. overnight departure)", async () => {
|
it("updates a record with combined datetimes (incl. overnight departure)", async () => {
|
||||||
const created = await prisma.attendance.create({
|
const created = await prisma.attendance.create({
|
||||||
|
|||||||
100
src/__tests__/dashboard.test.ts
Normal file
100
src/__tests__/dashboard.test.ts
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
import { describe, it, expect, beforeAll, afterAll } from "vitest";
|
||||||
|
import Fastify from "fastify";
|
||||||
|
import jwt from "jsonwebtoken";
|
||||||
|
import prisma from "../config/database";
|
||||||
|
import { config } from "../config/env";
|
||||||
|
import dashboardRoutes from "../routes/admin/dashboard";
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Regression test for the "Docházka dnes" / "Přítomní dnes" dashboard window.
|
||||||
|
//
|
||||||
|
// attendance.shift_date is @db.Date and Prisma truncates a filter Date to its
|
||||||
|
// UTC date part. The dashboard used local-midnight boundaries
|
||||||
|
// (new Date(y, m, d) = 22:00/23:00 UTC of the PREVIOUS day under
|
||||||
|
// TZ=Europe/Prague), which truncated to [yesterday, today) — today's punches
|
||||||
|
// matched zero rows and everyone showed as absent. The boundaries must be
|
||||||
|
// UTC-midnight instants of the LOCAL calendar day.
|
||||||
|
//
|
||||||
|
// Unlike the other suites this one has to use the REAL current date (the
|
||||||
|
// route computes "today" internally), so fixtures are tracked by id and
|
||||||
|
// deleted in afterAll.
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
let app: Awaited<ReturnType<typeof buildApp>>;
|
||||||
|
let adminUserId: number;
|
||||||
|
let adminToken: string;
|
||||||
|
const createdIds: number[] = [];
|
||||||
|
|
||||||
|
async function buildApp() {
|
||||||
|
const a = Fastify({ logger: false });
|
||||||
|
await a.register(dashboardRoutes, { prefix: "/api/admin/dashboard" });
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
app = await buildApp();
|
||||||
|
|
||||||
|
const admin = await prisma.users.findFirst({
|
||||||
|
where: { roles: { name: "admin" } },
|
||||||
|
include: { roles: true },
|
||||||
|
});
|
||||||
|
if (!admin) throw new Error("Test setup: admin user not found");
|
||||||
|
adminUserId = admin.id;
|
||||||
|
adminToken = jwt.sign(
|
||||||
|
{ sub: admin.id, username: admin.username, role: "admin" },
|
||||||
|
config.jwt.secret,
|
||||||
|
{ expiresIn: "15m" },
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
if (createdIds.length) {
|
||||||
|
await prisma.attendance.deleteMany({ where: { id: { in: createdIds } } });
|
||||||
|
}
|
||||||
|
if (app) await app.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("GET /api/admin/dashboard — today's attendance window", () => {
|
||||||
|
it("counts a punched-in user as present (local-noon @db.Date row)", async () => {
|
||||||
|
const now = new Date();
|
||||||
|
// The attendance regime stores shift_date at LOCAL NOON (so the UTC date
|
||||||
|
// part equals the Prague calendar date) — same as punchAction does.
|
||||||
|
const row = await prisma.attendance.create({
|
||||||
|
data: {
|
||||||
|
user_id: adminUserId,
|
||||||
|
shift_date: new Date(
|
||||||
|
now.getFullYear(),
|
||||||
|
now.getMonth(),
|
||||||
|
now.getDate(),
|
||||||
|
12,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
),
|
||||||
|
leave_type: "work",
|
||||||
|
arrival_time: now,
|
||||||
|
departure_time: null,
|
||||||
|
notes: "dashboard_window_test",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
createdIds.push(row.id);
|
||||||
|
|
||||||
|
const res = await app.inject({
|
||||||
|
method: "GET",
|
||||||
|
url: "/api/admin/dashboard",
|
||||||
|
headers: { Authorization: `Bearer ${adminToken}` },
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(res.statusCode).toBe(200);
|
||||||
|
const body = res.json();
|
||||||
|
expect(body.success).toBe(true);
|
||||||
|
|
||||||
|
const attendance = body.data.attendance;
|
||||||
|
expect(attendance).toBeTruthy();
|
||||||
|
const me = attendance.users.find(
|
||||||
|
(u: { user_id: number }) => u.user_id === adminUserId,
|
||||||
|
);
|
||||||
|
expect(me).toBeTruthy();
|
||||||
|
expect(me.status).toBe("in");
|
||||||
|
expect(attendance.present_today).toBeGreaterThanOrEqual(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,5 +1,8 @@
|
|||||||
import { describe, it, expect, afterEach } from "vitest";
|
import { describe, it, expect, afterEach, beforeAll, afterAll } from "vitest";
|
||||||
|
import Fastify from "fastify";
|
||||||
|
import jwt from "jsonwebtoken";
|
||||||
import prisma from "../config/database";
|
import prisma from "../config/database";
|
||||||
|
import { config } from "../config/env";
|
||||||
import {
|
import {
|
||||||
previewIssuedOrderNumber,
|
previewIssuedOrderNumber,
|
||||||
previewInvoiceNumber,
|
previewInvoiceNumber,
|
||||||
@@ -19,23 +22,36 @@ import {
|
|||||||
createOffer,
|
createOffer,
|
||||||
updateOffer,
|
updateOffer,
|
||||||
deleteOffer,
|
deleteOffer,
|
||||||
|
invalidateOffer,
|
||||||
|
getOffer,
|
||||||
|
listOffers,
|
||||||
} from "../services/offers.service";
|
} from "../services/offers.service";
|
||||||
|
import quotationsRoutes from "../routes/admin/quotations";
|
||||||
|
|
||||||
// Track every row we create so the suite leaves the (real) app_test DB clean.
|
// Track every row we create so the suite leaves the (real) app_test DB clean.
|
||||||
const issuedOrderIds: number[] = [];
|
const issuedOrderIds: number[] = [];
|
||||||
const invoiceIds: number[] = [];
|
const invoiceIds: number[] = [];
|
||||||
const offerIds: number[] = [];
|
const offerIds: number[] = [];
|
||||||
|
const customerIds: number[] = [];
|
||||||
|
const linkedOrderIds: number[] = [];
|
||||||
|
|
||||||
afterEach(async () => {
|
afterEach(async () => {
|
||||||
for (const id of issuedOrderIds)
|
for (const id of issuedOrderIds)
|
||||||
await prisma.issued_orders.deleteMany({ where: { id } });
|
await prisma.issued_orders.deleteMany({ where: { id } });
|
||||||
for (const id of invoiceIds)
|
for (const id of invoiceIds)
|
||||||
await prisma.invoices.deleteMany({ where: { id } });
|
await prisma.invoices.deleteMany({ where: { id } });
|
||||||
|
// Linked orders FIRST — orders.quotation_id is a Restrict FK on quotations.
|
||||||
|
for (const id of linkedOrderIds)
|
||||||
|
await prisma.orders.deleteMany({ where: { id } });
|
||||||
for (const id of offerIds)
|
for (const id of offerIds)
|
||||||
await prisma.quotations.deleteMany({ where: { id } });
|
await prisma.quotations.deleteMany({ where: { id } });
|
||||||
|
for (const id of customerIds)
|
||||||
|
await prisma.customers.deleteMany({ where: { id } });
|
||||||
issuedOrderIds.length = 0;
|
issuedOrderIds.length = 0;
|
||||||
invoiceIds.length = 0;
|
invoiceIds.length = 0;
|
||||||
|
linkedOrderIds.length = 0;
|
||||||
offerIds.length = 0;
|
offerIds.length = 0;
|
||||||
|
customerIds.length = 0;
|
||||||
// Reset every sequence this suite may have touched so preview values are
|
// Reset every sequence this suite may have touched so preview values are
|
||||||
// stable across tests and we don't leak consumed numbers into other files.
|
// stable across tests and we don't leak consumed numbers into other files.
|
||||||
await prisma.number_sequences.deleteMany({
|
await prisma.number_sequences.deleteMany({
|
||||||
@@ -43,6 +59,14 @@ afterEach(async () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/** createOffer + narrow the token union + track cleanup. */
|
||||||
|
async function mkOffer(body: Record<string, unknown> = {}) {
|
||||||
|
const res = await createOffer(body);
|
||||||
|
if ("error" in res) throw new Error(`createOffer failed: ${res.error}`);
|
||||||
|
offerIds.push(res.id);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
/* -------------------------------------------------------------------------- */
|
/* -------------------------------------------------------------------------- */
|
||||||
/* Issued orders */
|
/* Issued orders */
|
||||||
/* -------------------------------------------------------------------------- */
|
/* -------------------------------------------------------------------------- */
|
||||||
@@ -51,6 +75,8 @@ describe("issued-order drafts — deferred numbering", () => {
|
|||||||
it("a draft has no number and does NOT consume the sequence", async () => {
|
it("a draft has no number and does NOT consume the sequence", async () => {
|
||||||
const before = (await previewIssuedOrderNumber()).number;
|
const before = (await previewIssuedOrderNumber()).number;
|
||||||
const draft = await createIssuedOrder({}); // defaults to draft
|
const draft = await createIssuedOrder({}); // defaults to draft
|
||||||
|
if ("error" in draft)
|
||||||
|
throw new Error(`createIssuedOrder failed: ${draft.error}`);
|
||||||
issuedOrderIds.push(draft.id);
|
issuedOrderIds.push(draft.id);
|
||||||
expect(draft.status).toBe("draft");
|
expect(draft.status).toBe("draft");
|
||||||
expect(draft.po_number).toBeNull();
|
expect(draft.po_number).toBeNull();
|
||||||
@@ -62,6 +88,8 @@ describe("issued-order drafts — deferred numbering", () => {
|
|||||||
it("finalizing a draft (draft -> sent) assigns the next sequence number", async () => {
|
it("finalizing a draft (draft -> sent) assigns the next sequence number", async () => {
|
||||||
const expected = (await previewIssuedOrderNumber()).number;
|
const expected = (await previewIssuedOrderNumber()).number;
|
||||||
const draft = await createIssuedOrder({});
|
const draft = await createIssuedOrder({});
|
||||||
|
if ("error" in draft)
|
||||||
|
throw new Error(`createIssuedOrder failed: ${draft.error}`);
|
||||||
issuedOrderIds.push(draft.id);
|
issuedOrderIds.push(draft.id);
|
||||||
|
|
||||||
const res = await updateIssuedOrder(draft.id, { status: "sent" });
|
const res = await updateIssuedOrder(draft.id, { status: "sent" });
|
||||||
@@ -77,6 +105,8 @@ describe("issued-order drafts — deferred numbering", () => {
|
|||||||
|
|
||||||
it("finalizing is idempotent — re-finalizing does not re-number", async () => {
|
it("finalizing is idempotent — re-finalizing does not re-number", async () => {
|
||||||
const draft = await createIssuedOrder({});
|
const draft = await createIssuedOrder({});
|
||||||
|
if ("error" in draft)
|
||||||
|
throw new Error(`createIssuedOrder failed: ${draft.error}`);
|
||||||
issuedOrderIds.push(draft.id);
|
issuedOrderIds.push(draft.id);
|
||||||
|
|
||||||
await updateIssuedOrder(draft.id, { status: "sent" });
|
await updateIssuedOrder(draft.id, { status: "sent" });
|
||||||
@@ -104,6 +134,8 @@ describe("issued-order drafts — deferred numbering", () => {
|
|||||||
it("two drafts coexist with null numbers (no unique violation)", async () => {
|
it("two drafts coexist with null numbers (no unique violation)", async () => {
|
||||||
const a = await createIssuedOrder({});
|
const a = await createIssuedOrder({});
|
||||||
const b = await createIssuedOrder({});
|
const b = await createIssuedOrder({});
|
||||||
|
if ("error" in a) throw new Error(`createIssuedOrder failed: ${a.error}`);
|
||||||
|
if ("error" in b) throw new Error(`createIssuedOrder failed: ${b.error}`);
|
||||||
issuedOrderIds.push(a.id, b.id);
|
issuedOrderIds.push(a.id, b.id);
|
||||||
expect(a.po_number).toBeNull();
|
expect(a.po_number).toBeNull();
|
||||||
expect(b.po_number).toBeNull();
|
expect(b.po_number).toBeNull();
|
||||||
@@ -112,6 +144,8 @@ describe("issued-order drafts — deferred numbering", () => {
|
|||||||
it("deleting a draft releases nothing (no number was consumed)", async () => {
|
it("deleting a draft releases nothing (no number was consumed)", async () => {
|
||||||
const before = (await previewIssuedOrderNumber()).number;
|
const before = (await previewIssuedOrderNumber()).number;
|
||||||
const draft = await createIssuedOrder({});
|
const draft = await createIssuedOrder({});
|
||||||
|
if ("error" in draft)
|
||||||
|
throw new Error(`createIssuedOrder failed: ${draft.error}`);
|
||||||
await deleteIssuedOrder(draft.id);
|
await deleteIssuedOrder(draft.id);
|
||||||
const after = (await previewIssuedOrderNumber()).number;
|
const after = (await previewIssuedOrderNumber()).number;
|
||||||
expect(after).toBe(before);
|
expect(after).toBe(before);
|
||||||
@@ -273,3 +307,282 @@ describe("offer drafts — deferred numbering", () => {
|
|||||||
expect(after).toBe(before);
|
expect(after).toBe(before);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/* -------------------------------------------------------------------------- */
|
||||||
|
/* Offers — status transition table (service) */
|
||||||
|
/* -------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
describe("offer status transitions (service)", () => {
|
||||||
|
it("rejects draft -> ordered so a numberless 'ordered' offer can never exist", async () => {
|
||||||
|
const draft = await mkOffer({});
|
||||||
|
const res = await updateOffer(draft.id, { status: "ordered" });
|
||||||
|
expect("error" in res && res.error).toBe("invalid_transition");
|
||||||
|
const row = await prisma.quotations.findUnique({ where: { id: draft.id } });
|
||||||
|
expect(row!.status).toBe("draft");
|
||||||
|
expect(row!.quotation_number).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("allows active -> invalidated via update", async () => {
|
||||||
|
const offer = await mkOffer({ status: "active" });
|
||||||
|
const res = await updateOffer(offer.id, { status: "invalidated" });
|
||||||
|
expect("error" in res).toBe(false);
|
||||||
|
const row = await prisma.quotations.findUnique({ where: { id: offer.id } });
|
||||||
|
expect(row!.status).toBe("invalidated");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("invalidateOffer respects the table: draft rejected, ordered ok, terminal stays terminal", async () => {
|
||||||
|
const draft = await mkOffer({});
|
||||||
|
const rejected = await invalidateOffer(draft.id);
|
||||||
|
expect("error" in rejected && rejected.error).toBe("invalid_transition");
|
||||||
|
|
||||||
|
const ordered = await mkOffer({ status: "ordered" });
|
||||||
|
const ok = await invalidateOffer(ordered.id);
|
||||||
|
expect("error" in ok).toBe(false);
|
||||||
|
|
||||||
|
// invalidated is terminal — a second invalidate is rejected too.
|
||||||
|
const again = await invalidateOffer(ordered.id);
|
||||||
|
expect("error" in again && again.error).toBe("invalid_transition");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("getOffer exposes valid_transitions computed from the current status", async () => {
|
||||||
|
const draft = await mkOffer({});
|
||||||
|
expect((await getOffer(draft.id))?.valid_transitions).toEqual(["active"]);
|
||||||
|
|
||||||
|
const active = await mkOffer({ status: "active" });
|
||||||
|
expect((await getOffer(active.id))?.valid_transitions).toEqual([
|
||||||
|
"ordered",
|
||||||
|
"invalidated",
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
/* -------------------------------------------------------------------------- */
|
||||||
|
/* Offers — editable-state guard (service) */
|
||||||
|
/* -------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
describe("offer editable-state guard (service)", () => {
|
||||||
|
it("explicitly rejects a field edit on an ordered offer (not silently dropped)", async () => {
|
||||||
|
const offer = await mkOffer({ status: "ordered", project_code: "PŮVODNÍ" });
|
||||||
|
const res = await updateOffer(offer.id, { project_code: "ZMĚNA" });
|
||||||
|
expect("error" in res && res.error).toBe("not_editable");
|
||||||
|
const row = await prisma.quotations.findUnique({ where: { id: offer.id } });
|
||||||
|
expect(row!.project_code).toBe("PŮVODNÍ");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rejects a field edit on an invalidated offer", async () => {
|
||||||
|
const offer = await mkOffer({ status: "invalidated" });
|
||||||
|
const res = await updateOffer(offer.id, { scope_title: "X" });
|
||||||
|
expect("error" in res && res.error).toBe("not_editable");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("still allows the status-only ordered -> invalidated payload", async () => {
|
||||||
|
const offer = await mkOffer({ status: "ordered" });
|
||||||
|
const res = await updateOffer(offer.id, { status: "invalidated" });
|
||||||
|
expect("error" in res).toBe(false);
|
||||||
|
const row = await prisma.quotations.findUnique({ where: { id: offer.id } });
|
||||||
|
expect(row!.status).toBe("invalidated");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
/* -------------------------------------------------------------------------- */
|
||||||
|
/* Offers — customer FK handling (service) */
|
||||||
|
/* -------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
describe("offer customer FK handling (service)", () => {
|
||||||
|
async function mkCustomer() {
|
||||||
|
const c = await prisma.customers.create({
|
||||||
|
data: { name: "drafts_test_zákazník" },
|
||||||
|
});
|
||||||
|
customerIds.push(c.id);
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
it("create rejects a nonexistent customer with a clean token (no P2003 500)", async () => {
|
||||||
|
const res = await createOffer({ customer_id: 99999999 });
|
||||||
|
expect("error" in res && res.error).toBe("customer_not_found");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("update rejects a nonexistent customer with a clean token", async () => {
|
||||||
|
const offer = await mkOffer({});
|
||||||
|
const res = await updateOffer(offer.id, { customer_id: 99999999 });
|
||||||
|
expect("error" in res && res.error).toBe("customer_not_found");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("explicit null CLEARS the customer (the old Number(null) -> 0 bug)", async () => {
|
||||||
|
const customer = await mkCustomer();
|
||||||
|
const offer = await mkOffer({ customer_id: customer.id });
|
||||||
|
expect(offer.customer_id).toBe(customer.id);
|
||||||
|
|
||||||
|
const res = await updateOffer(offer.id, { customer_id: null });
|
||||||
|
expect("error" in res).toBe(false);
|
||||||
|
const row = await prisma.quotations.findUnique({ where: { id: offer.id } });
|
||||||
|
expect(row!.customer_id).toBeNull();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
/* -------------------------------------------------------------------------- */
|
||||||
|
/* Offers — deterministic list ordering */
|
||||||
|
/* -------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
describe("listOffers deterministic ordering", () => {
|
||||||
|
it("tiebreaks same-created_at rows by id (stable in both directions)", async () => {
|
||||||
|
const MARK = `TIEBREAK-${Date.now()}`;
|
||||||
|
const ids: number[] = [];
|
||||||
|
for (let i = 0; i < 3; i++) {
|
||||||
|
const o = await mkOffer({ project_code: MARK });
|
||||||
|
ids.push(o.id);
|
||||||
|
}
|
||||||
|
// created_at is second-precision; pin all three to the SAME timestamp so
|
||||||
|
// only the id tiebreak can order them.
|
||||||
|
await prisma.quotations.updateMany({
|
||||||
|
where: { id: { in: ids } },
|
||||||
|
data: { created_at: new Date("2026-01-15T10:00:00") },
|
||||||
|
});
|
||||||
|
|
||||||
|
const base = { page: 1, limit: 10, skip: 0, search: MARK };
|
||||||
|
const desc = await listOffers({
|
||||||
|
...base,
|
||||||
|
sort: "created_at",
|
||||||
|
order: "desc",
|
||||||
|
});
|
||||||
|
expect(desc.data.map((o: { id: number }) => o.id)).toEqual(
|
||||||
|
[...ids].sort((a, b) => b - a),
|
||||||
|
);
|
||||||
|
|
||||||
|
const asc = await listOffers({ ...base, sort: "created_at", order: "asc" });
|
||||||
|
expect(asc.data.map((o: { id: number }) => o.id)).toEqual(
|
||||||
|
[...ids].sort((a, b) => a - b),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
/* -------------------------------------------------------------------------- */
|
||||||
|
/* Offers — route-level hardening (Czech messages + status codes) */
|
||||||
|
/* -------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
let app: ReturnType<typeof Fastify> | null = null;
|
||||||
|
let adminToken = "";
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
app = Fastify({ logger: false });
|
||||||
|
await app.register(quotationsRoutes, { prefix: "/api/admin/offers" });
|
||||||
|
|
||||||
|
const admin = await prisma.users.findFirst({
|
||||||
|
where: { roles: { name: "admin" } },
|
||||||
|
});
|
||||||
|
if (!admin) throw new Error("Test setup: admin user not found");
|
||||||
|
adminToken = jwt.sign(
|
||||||
|
{ sub: admin.id, username: admin.username, role: "admin" },
|
||||||
|
config.jwt.secret,
|
||||||
|
{ expiresIn: "15m" },
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
if (app) await app.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("offers routes — hardening (HTTP)", () => {
|
||||||
|
const auth = () => ({ Authorization: `Bearer ${adminToken}` });
|
||||||
|
|
||||||
|
it("PUT rejects a field edit on an ordered offer with the explicit Czech 400", async () => {
|
||||||
|
const offer = await mkOffer({ status: "ordered" });
|
||||||
|
const res = await app!.inject({
|
||||||
|
method: "PUT",
|
||||||
|
url: `/api/admin/offers/${offer.id}`,
|
||||||
|
headers: auth(),
|
||||||
|
payload: { project_code: "ZMĚNA" },
|
||||||
|
});
|
||||||
|
expect(res.statusCode).toBe(400);
|
||||||
|
expect(res.json().error).toBe("Nabídku v tomto stavu nelze upravovat");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("PUT rejects draft -> ordered with the transition message", async () => {
|
||||||
|
const draft = await mkOffer({});
|
||||||
|
const res = await app!.inject({
|
||||||
|
method: "PUT",
|
||||||
|
url: `/api/admin/offers/${draft.id}`,
|
||||||
|
headers: auth(),
|
||||||
|
payload: { status: "ordered" },
|
||||||
|
});
|
||||||
|
expect(res.statusCode).toBe(400);
|
||||||
|
expect(res.json().error).toBe(
|
||||||
|
'Neplatný přechod stavu z "draft" na "ordered"',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("POST 400s a 501-char item description (schema cap, not a DB 500)", async () => {
|
||||||
|
const res = await app!.inject({
|
||||||
|
method: "POST",
|
||||||
|
url: "/api/admin/offers",
|
||||||
|
headers: auth(),
|
||||||
|
payload: { items: [{ description: "x".repeat(501) }] },
|
||||||
|
});
|
||||||
|
expect(res.statusCode).toBe(400);
|
||||||
|
expect(res.json().success).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("POST 400s a nonexistent customer with 'Zákazník nenalezen'", async () => {
|
||||||
|
const res = await app!.inject({
|
||||||
|
method: "POST",
|
||||||
|
url: "/api/admin/offers",
|
||||||
|
headers: auth(),
|
||||||
|
payload: { customer_id: 99999999 },
|
||||||
|
});
|
||||||
|
expect(res.statusCode).toBe(400);
|
||||||
|
expect(res.json().error).toBe("Zákazník nenalezen");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("DELETE returns a Czech 409 when the offer is linked to an order (real Restrict FK)", async () => {
|
||||||
|
const offer = await mkOffer({ status: "active" });
|
||||||
|
const order = await prisma.orders.create({
|
||||||
|
data: {
|
||||||
|
order_number: `DRAFTSLINK-${Date.now()}`,
|
||||||
|
quotation_id: offer.id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
linkedOrderIds.push(order.id);
|
||||||
|
|
||||||
|
const res = await app!.inject({
|
||||||
|
method: "DELETE",
|
||||||
|
url: `/api/admin/offers/${offer.id}`,
|
||||||
|
headers: auth(),
|
||||||
|
});
|
||||||
|
expect(res.statusCode).toBe(409);
|
||||||
|
expect(res.json().error).toBe(
|
||||||
|
"Nabídku nelze smazat, je navázána na objednávku nebo projekt",
|
||||||
|
);
|
||||||
|
// The offer survived the rejected delete.
|
||||||
|
expect(
|
||||||
|
await prisma.quotations.findUnique({ where: { id: offer.id } }),
|
||||||
|
).not.toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("PUT writes an audit row carrying oldValues and newValues ('koncept' fallback)", async () => {
|
||||||
|
const draft = await mkOffer({ project_code: "PŘED" });
|
||||||
|
const res = await app!.inject({
|
||||||
|
method: "PUT",
|
||||||
|
url: `/api/admin/offers/${draft.id}`,
|
||||||
|
headers: auth(),
|
||||||
|
payload: { project_code: "PO" },
|
||||||
|
});
|
||||||
|
expect(res.statusCode).toBe(200);
|
||||||
|
|
||||||
|
const audit = await prisma.audit_logs.findFirst({
|
||||||
|
where: {
|
||||||
|
entity_type: "quotation",
|
||||||
|
entity_id: draft.id,
|
||||||
|
action: "update",
|
||||||
|
},
|
||||||
|
orderBy: { id: "desc" },
|
||||||
|
});
|
||||||
|
expect(audit).not.toBeNull();
|
||||||
|
expect(audit!.old_values).toBeTruthy();
|
||||||
|
expect(audit!.new_values).toBeTruthy();
|
||||||
|
expect(JSON.parse(audit!.old_values!).project_code).toBe("PŘED");
|
||||||
|
expect(JSON.parse(audit!.new_values!).project_code).toBe("PO");
|
||||||
|
// Unnumbered drafts read "koncept", never "null".
|
||||||
|
expect(audit!.description).toContain("koncept");
|
||||||
|
expect(audit!.description).not.toContain("null");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
50
src/__tests__/invoice-alerts.test.ts
Normal file
50
src/__tests__/invoice-alerts.test.ts
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
import { describe, it, expect } from "vitest";
|
||||||
|
import { computeAlertWindow } from "../services/invoice-alerts";
|
||||||
|
import { utcMidnightOfLocalDay } from "../utils/date";
|
||||||
|
|
||||||
|
// Pure tests — no DB rows are touched. due_date is @db.Date: Prisma truncates
|
||||||
|
// a Date used in a WHERE filter to its UTC date part, so the alert window
|
||||||
|
// boundaries must be UTC midnights of the LOCAL calendar day. The old code
|
||||||
|
// used local midnights (= 22:00/23:00 UTC of the PREVIOUS day), which shifted
|
||||||
|
// the [today, today+3] due_date window a day early — the "splatnost za 3 dny"
|
||||||
|
// advance alert (due == today+3) could then never fire. All assertions below
|
||||||
|
// are timezone-independent (inputs are built from local components).
|
||||||
|
|
||||||
|
describe("utcMidnightOfLocalDay", () => {
|
||||||
|
it("preserves the LOCAL calendar day shortly after local midnight (the night window)", () => {
|
||||||
|
// 00:30 local on 2098-07-01 — in Prague (UTC+2) this instant is
|
||||||
|
// 2098-06-30T22:30Z, i.e. its UTC date part is the PREVIOUS day, which is
|
||||||
|
// exactly what Prisma would truncate a bare Date to.
|
||||||
|
const justAfterMidnight = new Date(2098, 6, 1, 0, 30, 0);
|
||||||
|
expect(utcMidnightOfLocalDay(justAfterMidnight).getTime()).toBe(
|
||||||
|
Date.UTC(2098, 6, 1),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns UTC midnight of the local day for a mid-day instant", () => {
|
||||||
|
const noon = new Date(2098, 0, 15, 12, 0, 0);
|
||||||
|
expect(utcMidnightOfLocalDay(noon).getTime()).toBe(Date.UTC(2098, 0, 15));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("computeAlertWindow", () => {
|
||||||
|
it("builds [today, today+3] as UTC midnights of the LOCAL day, with matching strings", () => {
|
||||||
|
// 00:30 local — the window where the old local-midnight computation
|
||||||
|
// produced yesterday's UTC date and the whole window slid a day early.
|
||||||
|
const now = new Date(2098, 6, 1, 0, 30, 0);
|
||||||
|
const w = computeAlertWindow(now);
|
||||||
|
|
||||||
|
expect(w.today.getTime()).toBe(Date.UTC(2098, 6, 1));
|
||||||
|
expect(w.in3days.getTime()).toBe(Date.UTC(2098, 6, 4));
|
||||||
|
expect(w.todayStr).toBe("2098-07-01");
|
||||||
|
expect(w.in3daysStr).toBe("2098-07-04");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rolls the +3-day bound over a month boundary", () => {
|
||||||
|
const now = new Date(2098, 0, 30, 8, 0, 0); // 2098-01-30
|
||||||
|
const w = computeAlertWindow(now);
|
||||||
|
|
||||||
|
expect(w.in3days.getTime()).toBe(Date.UTC(2098, 1, 2)); // 2098-02-02
|
||||||
|
expect(w.in3daysStr).toBe("2098-02-02");
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,12 +1,18 @@
|
|||||||
import { describe, it, expect, afterEach } from "vitest";
|
import { describe, it, expect, afterEach, beforeEach } from "vitest";
|
||||||
import { Prisma } from "@prisma/client";
|
import { Prisma } from "@prisma/client";
|
||||||
import prisma from "../config/database";
|
import prisma from "../config/database";
|
||||||
import {
|
import {
|
||||||
invoiceTotalWithVat,
|
invoiceTotalWithVat,
|
||||||
createInvoice,
|
createInvoice,
|
||||||
updateInvoice,
|
updateInvoice,
|
||||||
|
getInvoice,
|
||||||
|
listInvoices,
|
||||||
|
getInvoiceListTotals,
|
||||||
} from "../services/invoices.service";
|
} from "../services/invoices.service";
|
||||||
import { UpdateInvoiceSchema } from "../schemas/invoices.schema";
|
import {
|
||||||
|
CreateInvoiceSchema,
|
||||||
|
UpdateInvoiceSchema,
|
||||||
|
} from "../schemas/invoices.schema";
|
||||||
|
|
||||||
// `invoiceTotalWithVat` receives a Prisma row whose money columns are real
|
// `invoiceTotalWithVat` receives a Prisma row whose money columns are real
|
||||||
// `Prisma.Decimal` instances (the model maps `@db.Decimal` → Decimal). Using
|
// `Prisma.Decimal` instances (the model maps `@db.Decimal` → Decimal). Using
|
||||||
@@ -166,7 +172,10 @@ describe("updateInvoice — billing_text round-trips through UpdateInvoiceSchema
|
|||||||
invoiceIds.push(draft.id);
|
invoiceIds.push(draft.id);
|
||||||
|
|
||||||
// Absent from the body -> updateInvoice's `!== undefined` guard skips it.
|
// Absent from the body -> updateInvoice's `!== undefined` guard skips it.
|
||||||
await updateInvoice(draft.id, UpdateInvoiceSchema.parse({ notes: "x" }));
|
await updateInvoice(
|
||||||
|
draft.id,
|
||||||
|
UpdateInvoiceSchema.parse({ internal_notes: "x" }),
|
||||||
|
);
|
||||||
let row = await prisma.invoices.findUnique({ where: { id: draft.id } });
|
let row = await prisma.invoices.findUnique({ where: { id: draft.id } });
|
||||||
expect(row?.billing_text).toBe("Text na PDF");
|
expect(row?.billing_text).toBe("Text na PDF");
|
||||||
|
|
||||||
@@ -179,3 +188,171 @@ describe("updateInvoice — billing_text round-trips through UpdateInvoiceSchema
|
|||||||
expect(row?.billing_text).toBeNull();
|
expect(row?.billing_text).toBeNull();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/* -------------------------------------------------------------------------- */
|
||||||
|
/* "Obsah" sections + internal-only notes (mirrors issued orders) */
|
||||||
|
/* -------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
describe("invoice sections round-trip; dropped `notes` is stripped", () => {
|
||||||
|
const invoiceIds: number[] = [];
|
||||||
|
|
||||||
|
afterEach(async () => {
|
||||||
|
for (const id of invoiceIds)
|
||||||
|
await prisma.invoices.deleteMany({ where: { id } });
|
||||||
|
invoiceIds.length = 0;
|
||||||
|
});
|
||||||
|
|
||||||
|
it("creates, returns and replaces CZ/EN sections in position order", async () => {
|
||||||
|
const parsed = CreateInvoiceSchema.parse({
|
||||||
|
billing_text: "Sekce test",
|
||||||
|
sections: [
|
||||||
|
{ title: "Scope", title_cz: "Rozsah", content: "<p>obsah 1</p>" },
|
||||||
|
{ title: "", title_cz: "Podmínky", content: "<p>obsah 2</p>" },
|
||||||
|
],
|
||||||
|
});
|
||||||
|
const draft = await createInvoice(parsed);
|
||||||
|
invoiceIds.push(draft.id);
|
||||||
|
|
||||||
|
const detail = await getInvoice(draft.id);
|
||||||
|
expect(detail?.sections?.length).toBe(2);
|
||||||
|
expect(detail?.sections?.[0].title_cz).toBe("Rozsah");
|
||||||
|
expect(detail?.sections?.[1].title_cz).toBe("Podmínky");
|
||||||
|
expect(detail?.sections?.[0].position).toBe(0);
|
||||||
|
expect(detail?.sections?.[1].position).toBe(1);
|
||||||
|
|
||||||
|
// Full-replace on update (same model as items).
|
||||||
|
const upd = UpdateInvoiceSchema.parse({
|
||||||
|
sections: [
|
||||||
|
{ title: "Only", title_cz: "Jediná", content: "<p>nový obsah</p>" },
|
||||||
|
],
|
||||||
|
});
|
||||||
|
const res = await updateInvoice(draft.id, upd);
|
||||||
|
expect("error" in res).toBe(false);
|
||||||
|
|
||||||
|
const after = await getInvoice(draft.id);
|
||||||
|
expect(after?.sections?.length).toBe(1);
|
||||||
|
expect(after?.sections?.[0].title_cz).toBe("Jediná");
|
||||||
|
expect(after?.sections?.[0].content).toBe("<p>nový obsah</p>");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("sections survive an items-only update untouched (absent = keep)", async () => {
|
||||||
|
const draft = await createInvoice(
|
||||||
|
CreateInvoiceSchema.parse({
|
||||||
|
sections: [{ title: "", title_cz: "Drží", content: "<p>x</p>" }],
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
invoiceIds.push(draft.id);
|
||||||
|
|
||||||
|
await updateInvoice(
|
||||||
|
draft.id,
|
||||||
|
UpdateInvoiceSchema.parse({
|
||||||
|
items: [{ description: "Práce", quantity: 1, unit_price: 100 }],
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
const after = await getInvoice(draft.id);
|
||||||
|
expect(after?.sections?.length).toBe(1);
|
||||||
|
expect(after?.sections?.[0].title_cz).toBe("Drží");
|
||||||
|
expect(after?.items?.length).toBe(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("silently strips the dropped `notes` key from legacy payloads", async () => {
|
||||||
|
// The printed-notes column was dropped (notes are internal-only now) —
|
||||||
|
// a legacy client still sending `notes` must not 500 or write anything.
|
||||||
|
const parsedCreate = CreateInvoiceSchema.parse({
|
||||||
|
notes: "<p>stará poznámka</p>",
|
||||||
|
internal_notes: "<p>interní</p>",
|
||||||
|
});
|
||||||
|
expect("notes" in parsedCreate).toBe(false);
|
||||||
|
|
||||||
|
const draft = await createInvoice(parsedCreate);
|
||||||
|
invoiceIds.push(draft.id);
|
||||||
|
const row = await prisma.invoices.findUnique({ where: { id: draft.id } });
|
||||||
|
expect(row?.internal_notes).toBe("<p>interní</p>");
|
||||||
|
|
||||||
|
const parsedUpdate = UpdateInvoiceSchema.parse({ notes: "x" });
|
||||||
|
expect("notes" in parsedUpdate).toBe(false);
|
||||||
|
const res = await updateInvoice(draft.id, parsedUpdate);
|
||||||
|
expect("error" in res).toBe(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
/* -------------------------------------------------------------------------- */
|
||||||
|
/* Month filter boundaries — issue_date is @db.Date */
|
||||||
|
/* -------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
// issue_date is @db.Date: Prisma compares a Date filter by its UTC date part.
|
||||||
|
// The old LOCAL-midnight month bounds (= 22:00/23:00 UTC of the previous day)
|
||||||
|
// shifted the window a day back — the list/totals included the previous
|
||||||
|
// month's last day and DROPPED invoices issued on the selected month's LAST
|
||||||
|
// day. buildInvoiceWhere is shared by listInvoices AND getInvoiceListTotals,
|
||||||
|
// so both are exercised. Fixtures use the test-only "TST" currency + far-future
|
||||||
|
// 2098 dates so totals can be asserted exactly without colliding with other
|
||||||
|
// suites' rows.
|
||||||
|
|
||||||
|
describe("listInvoices / getInvoiceListTotals — month filter (@db.Date boundaries)", () => {
|
||||||
|
const cleanup = async () => {
|
||||||
|
// invoice_items cascade on invoice delete.
|
||||||
|
await prisma.invoices.deleteMany({ where: { currency: "TST" } });
|
||||||
|
};
|
||||||
|
|
||||||
|
beforeEach(cleanup);
|
||||||
|
afterEach(cleanup);
|
||||||
|
|
||||||
|
const listParams = {
|
||||||
|
page: 1,
|
||||||
|
limit: 100,
|
||||||
|
skip: 0,
|
||||||
|
sort: "id",
|
||||||
|
order: "asc" as const,
|
||||||
|
search: "",
|
||||||
|
};
|
||||||
|
|
||||||
|
async function createBoundaryFixtures() {
|
||||||
|
// Drafts: no invoice number is consumed, but buildInvoiceWhere has no
|
||||||
|
// status filter so they appear in the list/totals like any invoice.
|
||||||
|
const lastDayJan = await createInvoice({
|
||||||
|
status: "draft",
|
||||||
|
issue_date: "2098-01-31",
|
||||||
|
currency: "TST",
|
||||||
|
vat_rate: 21,
|
||||||
|
items: [{ quantity: 1, unit_price: 100, vat_rate: 21 }], // total 121
|
||||||
|
});
|
||||||
|
const firstDayFeb = await createInvoice({
|
||||||
|
status: "draft",
|
||||||
|
issue_date: "2098-02-01",
|
||||||
|
currency: "TST",
|
||||||
|
vat_rate: 21,
|
||||||
|
items: [{ quantity: 1, unit_price: 200, vat_rate: 21 }], // total 242
|
||||||
|
});
|
||||||
|
return { lastDayJan, firstDayFeb };
|
||||||
|
}
|
||||||
|
|
||||||
|
it("a month's list contains its own LAST day and not the neighbor month's first day", async () => {
|
||||||
|
const { lastDayJan, firstDayFeb } = await createBoundaryFixtures();
|
||||||
|
|
||||||
|
const jan = await listInvoices({ ...listParams, month: 1, year: 2098 });
|
||||||
|
const janIds = jan.data.map((i) => i.id);
|
||||||
|
expect(janIds).toContain(lastDayJan.id);
|
||||||
|
expect(janIds).not.toContain(firstDayFeb.id);
|
||||||
|
|
||||||
|
const feb = await listInvoices({ ...listParams, month: 2, year: 2098 });
|
||||||
|
const febIds = feb.data.map((i) => i.id);
|
||||||
|
expect(febIds).toContain(firstDayFeb.id);
|
||||||
|
expect(febIds).not.toContain(lastDayJan.id);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("per-currency totals include the month's last day exactly once", async () => {
|
||||||
|
await createBoundaryFixtures();
|
||||||
|
|
||||||
|
const jan = await getInvoiceListTotals({ month: 1, year: 2098 });
|
||||||
|
const janTst = jan.totals.find((t) => t.currency === "TST");
|
||||||
|
// 100 + 21 % VAT — proves the Jan 31 invoice is counted and the
|
||||||
|
// Feb 1 invoice (242) is NOT pulled into January.
|
||||||
|
expect(janTst?.amount).toBe(121);
|
||||||
|
|
||||||
|
const feb = await getInvoiceListTotals({ month: 2, year: 2098 });
|
||||||
|
const febTst = feb.totals.find((t) => t.currency === "TST");
|
||||||
|
expect(febTst?.amount).toBe(242);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -47,8 +47,6 @@ const baseOrder = {
|
|||||||
status: "prijata" as const,
|
status: "prijata" as const,
|
||||||
currency: "CZK",
|
currency: "CZK",
|
||||||
language: "cs",
|
language: "cs",
|
||||||
vat_rate: 21,
|
|
||||||
apply_vat: true,
|
|
||||||
exchange_rate: 1,
|
exchange_rate: 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -117,8 +115,6 @@ describe("createOrderFromQuotation (auto-project gets its OWN number)", () => {
|
|||||||
status: "active",
|
status: "active",
|
||||||
currency: "CZK",
|
currency: "CZK",
|
||||||
language: "cs",
|
language: "cs",
|
||||||
vat_rate: 21,
|
|
||||||
apply_vat: true,
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
createdQuotationIds.push(quotation.id);
|
createdQuotationIds.push(quotation.id);
|
||||||
@@ -146,6 +142,84 @@ describe("createOrderFromQuotation (auto-project gets its OWN number)", () => {
|
|||||||
expect(project?.project_number).not.toBe(order?.order_number);
|
expect(project?.project_number).not.toBe(order?.order_number);
|
||||||
expect(project?.order_id).toBe(orderId);
|
expect(project?.order_id).toBe(orderId);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("rejects a DRAFT offer (a numberless 'ordered' offer must never exist)", async () => {
|
||||||
|
const draft = await prisma.quotations.create({
|
||||||
|
data: { status: "draft", currency: "CZK", language: "cs" },
|
||||||
|
});
|
||||||
|
createdQuotationIds.push(draft.id);
|
||||||
|
|
||||||
|
const res = await createOrderFromQuotation({ quotationId: draft.id });
|
||||||
|
expect("error" in res).toBe(true);
|
||||||
|
if ("error" in res) {
|
||||||
|
expect(res.status).toBe(400);
|
||||||
|
expect(res.error).toContain('ve stavu "draft"');
|
||||||
|
}
|
||||||
|
// The offer is untouched — still a numberless draft.
|
||||||
|
const row = await prisma.quotations.findUnique({ where: { id: draft.id } });
|
||||||
|
expect(row!.status).toBe("draft");
|
||||||
|
expect(row!.quotation_number).toBeNull();
|
||||||
|
expect(row!.order_id).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rejects an INVALIDATED offer (terminal status — no ordered transition)", async () => {
|
||||||
|
const invalidated = await prisma.quotations.create({
|
||||||
|
data: {
|
||||||
|
quotation_number: `Q-INVAL-${Date.now()}`,
|
||||||
|
status: "invalidated",
|
||||||
|
currency: "CZK",
|
||||||
|
language: "cs",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
createdQuotationIds.push(invalidated.id);
|
||||||
|
|
||||||
|
const res = await createOrderFromQuotation({
|
||||||
|
quotationId: invalidated.id,
|
||||||
|
});
|
||||||
|
expect("error" in res).toBe(true);
|
||||||
|
if ("error" in res) {
|
||||||
|
expect(res.status).toBe(400);
|
||||||
|
expect(res.error).toContain('ve stavu "invalidated"');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("deleteOrder reverts the source offer (stuck-ordered regression)", () => {
|
||||||
|
it("returns the linked quotation to active with order_id cleared", async () => {
|
||||||
|
const quotation = await prisma.quotations.create({
|
||||||
|
data: {
|
||||||
|
quotation_number: `Q-REVERT-${Date.now()}`,
|
||||||
|
status: "active",
|
||||||
|
currency: "CZK",
|
||||||
|
language: "cs",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
createdQuotationIds.push(quotation.id);
|
||||||
|
|
||||||
|
const res = await createOrderFromQuotation({ quotationId: quotation.id });
|
||||||
|
if (!("data" in res) || !res.data) failResult(res);
|
||||||
|
const project = await prisma.projects.findFirst({
|
||||||
|
where: { order_id: res.data.order_id },
|
||||||
|
});
|
||||||
|
if (project) createdProjectIds.push(project.id);
|
||||||
|
|
||||||
|
const afterCreate = await prisma.quotations.findUnique({
|
||||||
|
where: { id: quotation.id },
|
||||||
|
});
|
||||||
|
expect(afterCreate!.status).toBe("ordered");
|
||||||
|
expect(afterCreate!.order_id).toBe(res.data.order_id);
|
||||||
|
|
||||||
|
const del = await deleteOrder(res.data.order_id);
|
||||||
|
if ("error" in del) failResult(del);
|
||||||
|
|
||||||
|
// The offer must be orderable again — `ordered` with no order_id is a
|
||||||
|
// dead end (the transition table only allows ordered -> invalidated).
|
||||||
|
const afterDelete = await prisma.quotations.findUnique({
|
||||||
|
where: { id: quotation.id },
|
||||||
|
});
|
||||||
|
expect(afterDelete!.order_id).toBeNull();
|
||||||
|
expect(afterDelete!.status).toBe("active");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("deleteOrder releases the project number (release regression)", () => {
|
describe("deleteOrder releases the project number (release regression)", () => {
|
||||||
|
|||||||
@@ -53,4 +53,29 @@ describe("NasDocumentManager issued-PDF round-trip", () => {
|
|||||||
blank.saveIssuedPdf("25P0003", 2026, 6, Buffer.from("x")),
|
blank.saveIssuedPdf("25P0003", 2026, 6, Buffer.from("x")),
|
||||||
).toBeNull();
|
).toBeNull();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("re-saving the same number OVERWRITES in place — never a _1 duplicate", () => {
|
||||||
|
const first = nas.saveIssuedPdf("25P0004", 2026, 6, Buffer.from("v1"));
|
||||||
|
const second = nas.saveIssuedPdf("25P0004", 2026, 6, Buffer.from("v2"));
|
||||||
|
|
||||||
|
// Same canonical path both times, newest content wins.
|
||||||
|
expect(second).toBe(first);
|
||||||
|
const dir = path.join(tmpBase, "Vydané", "2026", "06");
|
||||||
|
expect(fs.readdirSync(dir)).toEqual(["25P0004.pdf"]);
|
||||||
|
expect(nas.readIssued(second!)!.data.toString()).toBe("v2");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("cleanIssued also sweeps stale _N duplicates left by the old racy save", () => {
|
||||||
|
nas.saveIssuedPdf("25P0005", 2026, 6, Buffer.from("canonical"));
|
||||||
|
// Simulate duplicates the pre-fix uniquePath save created during races.
|
||||||
|
const dir = path.join(tmpBase, "Vydané", "2026", "06");
|
||||||
|
fs.writeFileSync(path.join(dir, "25P0005_1.pdf"), "stale");
|
||||||
|
fs.writeFileSync(path.join(dir, "25P0005_2.pdf"), "stale");
|
||||||
|
// A DIFFERENT number must not be touched by the suffix match.
|
||||||
|
nas.saveIssuedPdf("25P0006", 2026, 6, Buffer.from("other"));
|
||||||
|
|
||||||
|
nas.cleanIssued("25P0005");
|
||||||
|
|
||||||
|
expect(fs.readdirSync(dir).sort()).toEqual(["25P0006.pdf"]);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -54,18 +54,16 @@ function amountFor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
describe("getOfferTotals per-currency aggregation", () => {
|
describe("getOfferTotals per-currency aggregation", () => {
|
||||||
it("sums offer TOTAL incl. VAT per currency over the full filtered set", async () => {
|
it("sums offer NET total per currency over the full filtered set", async () => {
|
||||||
// Two CZK offers + one EUR. 21% VAT applied on the whole subtotal
|
// Two CZK offers + one EUR. NET only (enrichQuotation math — offers are
|
||||||
// (enrichQuotation math).
|
// not tax documents, no VAT anywhere).
|
||||||
// CZK #1: 2 x 1000 = 2000 net -> +21% = 2420
|
// CZK #1: 2 x 1000 = 2000
|
||||||
// CZK #2: 1 x 500 = 500 net -> +21% = 605 => CZK total 3025
|
// CZK #2: 1 x 500 = 500 => CZK total 2500
|
||||||
// EUR : 3 x 100 = 300 net -> +21% = 363 => EUR total 363
|
// EUR : 3 x 100 = 300 => EUR total 300
|
||||||
const mk = async (currency: string, qty: number, price: number) => {
|
const mk = async (currency: string, qty: number, price: number) => {
|
||||||
const res = await createOffer({
|
const res = await createOffer({
|
||||||
status: "draft", // draft so no offer number is consumed
|
status: "draft", // draft so no offer number is consumed
|
||||||
currency,
|
currency,
|
||||||
vat_rate: 21,
|
|
||||||
apply_vat: true,
|
|
||||||
project_code: OFFER_MARKER,
|
project_code: OFFER_MARKER,
|
||||||
items: [{ description: "X", quantity: qty, unit_price: price }],
|
items: [{ description: "X", quantity: qty, unit_price: price }],
|
||||||
});
|
});
|
||||||
@@ -79,8 +77,8 @@ describe("getOfferTotals per-currency aggregation", () => {
|
|||||||
await mk("EUR", 3, 100);
|
await mk("EUR", 3, 100);
|
||||||
|
|
||||||
const { totals } = await getOfferTotals({ search: OFFER_MARKER });
|
const { totals } = await getOfferTotals({ search: OFFER_MARKER });
|
||||||
expect(amountFor(totals, "CZK")).toBe(3025);
|
expect(amountFor(totals, "CZK")).toBe(2500);
|
||||||
expect(amountFor(totals, "EUR")).toBe(363);
|
expect(amountFor(totals, "EUR")).toBe(300);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("respects the where: a status filter narrows the result", async () => {
|
it("respects the where: a status filter narrows the result", async () => {
|
||||||
@@ -88,8 +86,6 @@ describe("getOfferTotals per-currency aggregation", () => {
|
|||||||
const res = await createOffer({
|
const res = await createOffer({
|
||||||
status,
|
status,
|
||||||
currency: "CZK",
|
currency: "CZK",
|
||||||
vat_rate: 21,
|
|
||||||
apply_vat: true,
|
|
||||||
project_code: OFFER_MARKER,
|
project_code: OFFER_MARKER,
|
||||||
items: [{ description: "X", quantity: 1, unit_price: 1000 }],
|
items: [{ description: "X", quantity: 1, unit_price: 1000 }],
|
||||||
});
|
});
|
||||||
@@ -103,23 +99,23 @@ describe("getOfferTotals per-currency aggregation", () => {
|
|||||||
await mk("draft");
|
await mk("draft");
|
||||||
await mk("invalidated");
|
await mk("invalidated");
|
||||||
|
|
||||||
// Marker only: all three count -> 3 x 1210 = 3630.
|
// Marker only: all three count -> 3 x 1000 = 3000 (net).
|
||||||
const all = await getOfferTotals({ search: OFFER_MARKER });
|
const all = await getOfferTotals({ search: OFFER_MARKER });
|
||||||
expect(amountFor(all.totals, "CZK")).toBe(3630);
|
expect(amountFor(all.totals, "CZK")).toBe(3000);
|
||||||
|
|
||||||
// Status filter narrows to the two drafts -> 2 x 1210 = 2420.
|
// Status filter narrows to the two drafts -> 2 x 1000 = 2000.
|
||||||
const onlyDraft = await getOfferTotals({
|
const onlyDraft = await getOfferTotals({
|
||||||
search: OFFER_MARKER,
|
search: OFFER_MARKER,
|
||||||
status: "draft",
|
status: "draft",
|
||||||
});
|
});
|
||||||
expect(amountFor(onlyDraft.totals, "CZK")).toBe(2420);
|
expect(amountFor(onlyDraft.totals, "CZK")).toBe(2000);
|
||||||
|
|
||||||
// Invalidated alone -> 1210.
|
// Invalidated alone -> 1000.
|
||||||
const onlyInvalid = await getOfferTotals({
|
const onlyInvalid = await getOfferTotals({
|
||||||
search: OFFER_MARKER,
|
search: OFFER_MARKER,
|
||||||
status: "invalidated",
|
status: "invalidated",
|
||||||
});
|
});
|
||||||
expect(amountFor(onlyInvalid.totals, "CZK")).toBe(1210);
|
expect(amountFor(onlyInvalid.totals, "CZK")).toBe(1000);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -8,8 +8,9 @@ import {
|
|||||||
|
|
||||||
// Per-currency total aggregation for both order lists. These hit the real
|
// Per-currency total aggregation for both order lists. These hit the real
|
||||||
// `app_test` DB via the service layer (suite convention) and prove the /stats
|
// `app_test` DB via the service layer (suite convention) and prove the /stats
|
||||||
// endpoints sum order TOTAL (incl. VAT) per currency across the WHOLE filtered
|
// endpoints sum order NET total per currency across the WHOLE filtered set
|
||||||
// set, and that the where (month/year + status) is respected.
|
// (orders are not tax documents — no VAT anywhere), and that the where
|
||||||
|
// (month/year + status) is respected.
|
||||||
//
|
//
|
||||||
// A far-future month/year is used so seeded/other-test rows can't fall in the
|
// A far-future month/year is used so seeded/other-test rows can't fall in the
|
||||||
// window and skew the deterministic sums (mirrors drafts-aggregation.test.ts).
|
// window and skew the deterministic sums (mirrors drafts-aggregation.test.ts).
|
||||||
@@ -46,19 +47,17 @@ function amountFor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
describe("getOrderTotals (received orders) per-currency aggregation", () => {
|
describe("getOrderTotals (received orders) per-currency aggregation", () => {
|
||||||
it("sums TOTAL incl. VAT per currency over the full filtered set", async () => {
|
it("sums NET total per currency over the full filtered set", async () => {
|
||||||
// Two CZK orders + one EUR, all in the same far-future month. 21% VAT,
|
// Two CZK orders + one EUR, all in the same far-future month. NET only
|
||||||
// applied on the whole subtotal (enrichOrder math).
|
// (enrichOrder math — orders carry no VAT).
|
||||||
// CZK #1: 2 x 1000 = 2000 net -> +21% = 2420
|
// CZK #1: 2 x 1000 = 2000
|
||||||
// CZK #2: 1 x 500 = 500 net -> +21% = 605 => CZK total 3025
|
// CZK #2: 1 x 500 = 500 => CZK total 2500
|
||||||
// EUR : 3 x 100 = 300 net -> +21% = 363 => EUR total 363
|
// EUR : 3 x 100 = 300 => EUR total 300
|
||||||
const mk = async (currency: string, qty: number, price: number) => {
|
const mk = async (currency: string, qty: number, price: number) => {
|
||||||
const res = await createOrder({
|
const res = await createOrder({
|
||||||
status: "prijata",
|
status: "prijata",
|
||||||
currency,
|
currency,
|
||||||
language: "cs",
|
language: "cs",
|
||||||
vat_rate: 21,
|
|
||||||
apply_vat: true,
|
|
||||||
create_project: false,
|
create_project: false,
|
||||||
items: [{ description: "X", quantity: qty, unit_price: price }],
|
items: [{ description: "X", quantity: qty, unit_price: price }],
|
||||||
});
|
});
|
||||||
@@ -83,8 +82,8 @@ describe("getOrderTotals (received orders) per-currency aggregation", () => {
|
|||||||
month: STATS_MONTH,
|
month: STATS_MONTH,
|
||||||
year: STATS_YEAR,
|
year: STATS_YEAR,
|
||||||
});
|
});
|
||||||
expect(amountFor(totals, "CZK")).toBe(3025);
|
expect(amountFor(totals, "CZK")).toBe(2500);
|
||||||
expect(amountFor(totals, "EUR")).toBe(363);
|
expect(amountFor(totals, "EUR")).toBe(300);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("respects the where: a different month and a status filter change the result", async () => {
|
it("respects the where: a different month and a status filter change the result", async () => {
|
||||||
@@ -93,8 +92,6 @@ describe("getOrderTotals (received orders) per-currency aggregation", () => {
|
|||||||
status,
|
status,
|
||||||
currency: "CZK",
|
currency: "CZK",
|
||||||
language: "cs",
|
language: "cs",
|
||||||
vat_rate: 21,
|
|
||||||
apply_vat: true,
|
|
||||||
create_project: false,
|
create_project: false,
|
||||||
items: [{ description: "X", quantity: 1, unit_price: 1000 }],
|
items: [{ description: "X", quantity: 1, unit_price: 1000 }],
|
||||||
});
|
});
|
||||||
@@ -122,49 +119,45 @@ describe("getOrderTotals (received orders) per-currency aggregation", () => {
|
|||||||
await mk("stornovana", 0);
|
await mk("stornovana", 0);
|
||||||
await mk("prijata", 1);
|
await mk("prijata", 1);
|
||||||
|
|
||||||
// Whole month: both same-month orders count -> 2 x 1210 = 2420.
|
// Whole month: both same-month orders count -> 2 x 1000 = 2000 (net).
|
||||||
const whole = await getOrderTotals({
|
const whole = await getOrderTotals({
|
||||||
month: STATS_MONTH,
|
month: STATS_MONTH,
|
||||||
year: STATS_YEAR,
|
year: STATS_YEAR,
|
||||||
});
|
});
|
||||||
expect(amountFor(whole.totals, "CZK")).toBe(2420);
|
expect(amountFor(whole.totals, "CZK")).toBe(2000);
|
||||||
|
|
||||||
// Status filter narrows to the single 'prijata' in the target month -> 1210.
|
// Status filter narrows to the single 'prijata' in the target month -> 1000.
|
||||||
const onlyPrijata = await getOrderTotals({
|
const onlyPrijata = await getOrderTotals({
|
||||||
month: STATS_MONTH,
|
month: STATS_MONTH,
|
||||||
year: STATS_YEAR,
|
year: STATS_YEAR,
|
||||||
status: "prijata",
|
status: "prijata",
|
||||||
});
|
});
|
||||||
expect(amountFor(onlyPrijata.totals, "CZK")).toBe(1210);
|
expect(amountFor(onlyPrijata.totals, "CZK")).toBe(1000);
|
||||||
|
|
||||||
// Next month sees only the one 'prijata' there -> 1210.
|
// Next month sees only the one 'prijata' there -> 1000.
|
||||||
const nextMonth = await getOrderTotals({
|
const nextMonth = await getOrderTotals({
|
||||||
month: STATS_MONTH + 1,
|
month: STATS_MONTH + 1,
|
||||||
year: STATS_YEAR,
|
year: STATS_YEAR,
|
||||||
});
|
});
|
||||||
expect(amountFor(nextMonth.totals, "CZK")).toBe(1210);
|
expect(amountFor(nextMonth.totals, "CZK")).toBe(1000);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("getIssuedOrderTotals (issued orders) per-currency aggregation", () => {
|
describe("getIssuedOrderTotals (issued orders) per-currency aggregation", () => {
|
||||||
it("sums TOTAL incl. VAT per currency over the full filtered set", async () => {
|
it("sums NET total per currency over the full filtered set", async () => {
|
||||||
// order_date is settable at create, so no post-create pin needed. VAT is
|
// order_date is settable at create, so no post-create pin needed. NET only
|
||||||
// applied per-line (computeIssuedOrderTotals) but with a single line per
|
// (computeIssuedOrderTotals — issued orders carry no VAT).
|
||||||
// order here the result matches the on-the-whole subtotal math.
|
// CZK #1: 2 x 1000 = 2000
|
||||||
// CZK #1: 2 x 1000 @21% = 2420
|
// CZK #2: 1 x 500 = 500 => CZK total 2500
|
||||||
// CZK #2: 1 x 500 @21% = 605 => CZK total 3025
|
// EUR : 3 x 100 = 300 => EUR total 300
|
||||||
// EUR : 3 x 100 @21% = 363 => EUR total 363
|
|
||||||
const dateStr = `${STATS_YEAR}-0${STATS_MONTH}-15`;
|
const dateStr = `${STATS_YEAR}-0${STATS_MONTH}-15`;
|
||||||
const mk = async (currency: string, qty: number, price: number) => {
|
const mk = async (currency: string, qty: number, price: number) => {
|
||||||
const o = await createIssuedOrder({
|
const o = await createIssuedOrder({
|
||||||
currency,
|
currency,
|
||||||
vat_rate: 21,
|
|
||||||
apply_vat: true,
|
|
||||||
order_date: dateStr,
|
order_date: dateStr,
|
||||||
items: [
|
items: [{ description: "X", quantity: qty, unit_price: price }],
|
||||||
{ description: "X", quantity: qty, unit_price: price, vat_rate: 21 },
|
|
||||||
],
|
|
||||||
});
|
});
|
||||||
|
if ("error" in o) throw new Error(`createIssuedOrder failed: ${o.error}`);
|
||||||
createdIssuedIds.push(o.id);
|
createdIssuedIds.push(o.id);
|
||||||
return o.id;
|
return o.id;
|
||||||
};
|
};
|
||||||
@@ -177,69 +170,63 @@ describe("getIssuedOrderTotals (issued orders) per-currency aggregation", () =>
|
|||||||
month: STATS_MONTH,
|
month: STATS_MONTH,
|
||||||
year: STATS_YEAR,
|
year: STATS_YEAR,
|
||||||
});
|
});
|
||||||
expect(amountFor(totals, "CZK")).toBe(3025);
|
expect(amountFor(totals, "CZK")).toBe(2500);
|
||||||
expect(amountFor(totals, "EUR")).toBe(363);
|
expect(amountFor(totals, "EUR")).toBe(300);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("respects the where: a different month and a status filter change the result", async () => {
|
it("respects the where: a different month and a status filter change the result", async () => {
|
||||||
const inMonth = `${STATS_YEAR}-0${STATS_MONTH}-15`;
|
const inMonth = `${STATS_YEAR}-0${STATS_MONTH}-15`;
|
||||||
const nextMonth = `${STATS_YEAR}-0${STATS_MONTH + 1}-15`;
|
const nextMonth = `${STATS_YEAR}-0${STATS_MONTH + 1}-15`;
|
||||||
|
|
||||||
// Target month: one draft, one already-sent (both 1210). Next month: one.
|
// Target month: one draft, one already-sent (both 1000 net). Next month: one.
|
||||||
const draft = await createIssuedOrder({
|
const draft = await createIssuedOrder({
|
||||||
currency: "CZK",
|
currency: "CZK",
|
||||||
vat_rate: 21,
|
|
||||||
apply_vat: true,
|
|
||||||
order_date: inMonth,
|
order_date: inMonth,
|
||||||
items: [
|
items: [{ description: "X", quantity: 1, unit_price: 1000 }],
|
||||||
{ description: "X", quantity: 1, unit_price: 1000, vat_rate: 21 },
|
|
||||||
],
|
|
||||||
});
|
});
|
||||||
|
if ("error" in draft)
|
||||||
|
throw new Error(`createIssuedOrder failed: ${draft.error}`);
|
||||||
createdIssuedIds.push(draft.id);
|
createdIssuedIds.push(draft.id);
|
||||||
|
|
||||||
const sent = await createIssuedOrder({
|
const sent = await createIssuedOrder({
|
||||||
status: "sent",
|
status: "sent",
|
||||||
currency: "CZK",
|
currency: "CZK",
|
||||||
vat_rate: 21,
|
|
||||||
apply_vat: true,
|
|
||||||
order_date: inMonth,
|
order_date: inMonth,
|
||||||
items: [
|
items: [{ description: "X", quantity: 1, unit_price: 1000 }],
|
||||||
{ description: "X", quantity: 1, unit_price: 1000, vat_rate: 21 },
|
|
||||||
],
|
|
||||||
});
|
});
|
||||||
|
if ("error" in sent)
|
||||||
|
throw new Error(`createIssuedOrder failed: ${sent.error}`);
|
||||||
createdIssuedIds.push(sent.id);
|
createdIssuedIds.push(sent.id);
|
||||||
|
|
||||||
const next = await createIssuedOrder({
|
const next = await createIssuedOrder({
|
||||||
currency: "CZK",
|
currency: "CZK",
|
||||||
vat_rate: 21,
|
|
||||||
apply_vat: true,
|
|
||||||
order_date: nextMonth,
|
order_date: nextMonth,
|
||||||
items: [
|
items: [{ description: "X", quantity: 1, unit_price: 1000 }],
|
||||||
{ description: "X", quantity: 1, unit_price: 1000, vat_rate: 21 },
|
|
||||||
],
|
|
||||||
});
|
});
|
||||||
|
if ("error" in next)
|
||||||
|
throw new Error(`createIssuedOrder failed: ${next.error}`);
|
||||||
createdIssuedIds.push(next.id);
|
createdIssuedIds.push(next.id);
|
||||||
|
|
||||||
// Whole target month: both count -> 2 x 1210 = 2420.
|
// Whole target month: both count -> 2 x 1000 = 2000 (net).
|
||||||
const whole = await getIssuedOrderTotals({
|
const whole = await getIssuedOrderTotals({
|
||||||
month: STATS_MONTH,
|
month: STATS_MONTH,
|
||||||
year: STATS_YEAR,
|
year: STATS_YEAR,
|
||||||
});
|
});
|
||||||
expect(amountFor(whole.totals, "CZK")).toBe(2420);
|
expect(amountFor(whole.totals, "CZK")).toBe(2000);
|
||||||
|
|
||||||
// Status filter narrows to the single 'sent' in the target month -> 1210.
|
// Status filter narrows to the single 'sent' in the target month -> 1000.
|
||||||
const onlySent = await getIssuedOrderTotals({
|
const onlySent = await getIssuedOrderTotals({
|
||||||
month: STATS_MONTH,
|
month: STATS_MONTH,
|
||||||
year: STATS_YEAR,
|
year: STATS_YEAR,
|
||||||
status: "sent",
|
status: "sent",
|
||||||
});
|
});
|
||||||
expect(amountFor(onlySent.totals, "CZK")).toBe(1210);
|
expect(amountFor(onlySent.totals, "CZK")).toBe(1000);
|
||||||
|
|
||||||
// Next month sees only the one order there -> 1210.
|
// Next month sees only the one order there -> 1000.
|
||||||
const nextStats = await getIssuedOrderTotals({
|
const nextStats = await getIssuedOrderTotals({
|
||||||
month: STATS_MONTH + 1,
|
month: STATS_MONTH + 1,
|
||||||
year: STATS_YEAR,
|
year: STATS_YEAR,
|
||||||
});
|
});
|
||||||
expect(amountFor(nextStats.totals, "CZK")).toBe(1210);
|
expect(amountFor(nextStats.totals, "CZK")).toBe(1000);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -30,8 +30,6 @@ const baseOrder = {
|
|||||||
status: "prijata" as const,
|
status: "prijata" as const,
|
||||||
currency: "CZK",
|
currency: "CZK",
|
||||||
language: "cs",
|
language: "cs",
|
||||||
vat_rate: 21,
|
|
||||||
apply_vat: true,
|
|
||||||
exchange_rate: 1,
|
exchange_rate: 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
247
src/__tests__/pdf-no-vat.test.ts
Normal file
247
src/__tests__/pdf-no-vat.test.ts
Normal file
@@ -0,0 +1,247 @@
|
|||||||
|
import { describe, it, expect, beforeAll, afterAll } from "vitest";
|
||||||
|
import Fastify from "fastify";
|
||||||
|
import jwt from "jsonwebtoken";
|
||||||
|
import prisma from "../config/database";
|
||||||
|
import { config } from "../config/env";
|
||||||
|
import { renderOrderConfirmationHtml } from "../routes/admin/orders-pdf";
|
||||||
|
import offersPdfRoutes from "../routes/admin/offers-pdf";
|
||||||
|
import invoicesPdfRoutes from "../routes/admin/invoices-pdf";
|
||||||
|
import { createInvoice } from "../services/invoices.service";
|
||||||
|
|
||||||
|
// Offers, received orders (their confirmation PDF) and issued orders are NOT
|
||||||
|
// tax documents — VAT must never appear on them. These tests pin that contract
|
||||||
|
// for the confirmation and offer PDFs: no VAT columns/rows/notices, and the
|
||||||
|
// grand total is "Celkem bez DPH" / "Total excl. VAT". (The issued-order PDF
|
||||||
|
// is covered in issued-orders.test.ts.)
|
||||||
|
|
||||||
|
// The explanatory prices-excl.-VAT notice was removed at user request — the
|
||||||
|
// total label alone carries the information. Pin its absence too.
|
||||||
|
const CS_NOTE = "Ceny jsou uvedeny bez DPH";
|
||||||
|
const EN_NOTE = "Prices are exclusive of VAT";
|
||||||
|
|
||||||
|
describe("renderOrderConfirmationHtml (order confirmation PDF)", () => {
|
||||||
|
const order = {
|
||||||
|
order_number: "26730042",
|
||||||
|
customer_order_number: "PO-XYZ",
|
||||||
|
created_at: new Date("2026-06-09T12:00:00"),
|
||||||
|
currency: "CZK",
|
||||||
|
notes: null,
|
||||||
|
customers: null,
|
||||||
|
};
|
||||||
|
const items = [
|
||||||
|
{
|
||||||
|
description: "Materiál",
|
||||||
|
quantity: 2,
|
||||||
|
unit: "ks",
|
||||||
|
unit_price: 100,
|
||||||
|
is_included_in_total: true,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
it("cs: NET total labeled 'Celkem bez DPH', no VAT columns or notice", () => {
|
||||||
|
const html = renderOrderConfirmationHtml(order, items, null, "cs", "Jan");
|
||||||
|
expect(html).not.toContain("%DPH");
|
||||||
|
expect(html).not.toContain(">DPH<");
|
||||||
|
expect(html).not.toContain("Mezisoučet");
|
||||||
|
// Line total = netto (2 × 100), no VAT-on-top anywhere.
|
||||||
|
expect(html).toContain('<td class="right total-cell">200,00</td>');
|
||||||
|
expect(html).toContain("Celkem bez DPH");
|
||||||
|
expect(html).not.toContain(CS_NOTE);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("en: 'Total excl. VAT', no VAT columns or notice", () => {
|
||||||
|
const html = renderOrderConfirmationHtml(order, items, null, "en", "Jan");
|
||||||
|
expect(html).not.toContain("VAT%");
|
||||||
|
expect(html).toContain("Total excl. VAT");
|
||||||
|
expect(html).not.toContain(EN_NOTE);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("excludes not-included lines from the NET total", () => {
|
||||||
|
const html = renderOrderConfirmationHtml(
|
||||||
|
order,
|
||||||
|
[
|
||||||
|
...items,
|
||||||
|
{
|
||||||
|
description: "Mimo cenu",
|
||||||
|
quantity: 1,
|
||||||
|
unit: "ks",
|
||||||
|
unit_price: 999,
|
||||||
|
is_included_in_total: false,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
null,
|
||||||
|
"cs",
|
||||||
|
"Jan",
|
||||||
|
);
|
||||||
|
// Grand total stays 200,00 — the excluded line doesn't count.
|
||||||
|
expect(html).toContain("200,00 CZK");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("uses the shared NBSP-thousands formatter (pdf-shared extraction marker)", () => {
|
||||||
|
const html = renderOrderConfirmationHtml(
|
||||||
|
order,
|
||||||
|
[
|
||||||
|
{
|
||||||
|
description: "Velká položka",
|
||||||
|
quantity: 2,
|
||||||
|
unit: "ks",
|
||||||
|
unit_price: 1000,
|
||||||
|
is_included_in_total: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
null,
|
||||||
|
"cs",
|
||||||
|
"Jan",
|
||||||
|
);
|
||||||
|
// 2 × 1000 = 2 000,00 with the NBSP (U+00A0) thousands separator.
|
||||||
|
expect(html).toContain("2 000,00 CZK");
|
||||||
|
expect(html).not.toContain("1 199,00 CZK");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
/* -------------------------------------------------------------------------- */
|
||||||
|
/* Offer PDF route (returns the HTML for the print view) */
|
||||||
|
/* -------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
let app: ReturnType<typeof Fastify> | null = null;
|
||||||
|
let adminToken = "";
|
||||||
|
const createdQuotationIds: number[] = [];
|
||||||
|
const createdInvoiceIds: number[] = [];
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
app = Fastify({ logger: false });
|
||||||
|
await app.register(offersPdfRoutes, { prefix: "/api/admin/offers-pdf" });
|
||||||
|
await app.register(invoicesPdfRoutes, { prefix: "/api/admin/invoices-pdf" });
|
||||||
|
|
||||||
|
const admin = await prisma.users.findFirst({
|
||||||
|
where: { roles: { name: "admin" } },
|
||||||
|
});
|
||||||
|
if (!admin) throw new Error("Test setup: admin user not found");
|
||||||
|
adminToken = jwt.sign(
|
||||||
|
{ sub: admin.id, username: admin.username, role: "admin" },
|
||||||
|
config.jwt.secret,
|
||||||
|
{ expiresIn: "15m" },
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
for (const id of createdQuotationIds)
|
||||||
|
await prisma.quotations.deleteMany({ where: { id } });
|
||||||
|
for (const id of createdInvoiceIds)
|
||||||
|
await prisma.invoices.deleteMany({ where: { id } });
|
||||||
|
if (app) await app.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("GET /api/admin/offers-pdf/:id (offer PDF html)", () => {
|
||||||
|
it("renders NET-only totals with 'Celkem bez DPH', no notice", async () => {
|
||||||
|
// Direct prisma insert (explicit number → no sequence consumed).
|
||||||
|
const quotation = await prisma.quotations.create({
|
||||||
|
data: {
|
||||||
|
quotation_number: `Q-VATNOTE-${Date.now()}`,
|
||||||
|
status: "active",
|
||||||
|
currency: "CZK",
|
||||||
|
language: "cs",
|
||||||
|
quotation_items: {
|
||||||
|
create: [
|
||||||
|
{
|
||||||
|
description: "Položka",
|
||||||
|
quantity: 2,
|
||||||
|
unit: "ks",
|
||||||
|
unit_price: 1000,
|
||||||
|
is_included_in_total: true,
|
||||||
|
position: 0,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
createdQuotationIds.push(quotation.id);
|
||||||
|
|
||||||
|
const res = await app!.inject({
|
||||||
|
method: "GET",
|
||||||
|
url: `/api/admin/offers-pdf/${quotation.id}`,
|
||||||
|
headers: { Authorization: `Bearer ${adminToken}` },
|
||||||
|
});
|
||||||
|
expect(res.statusCode).toBe(200);
|
||||||
|
const html = res.body;
|
||||||
|
expect(html).toContain("Celkem bez DPH");
|
||||||
|
expect(html).not.toContain(CS_NOTE);
|
||||||
|
expect(html).not.toContain("%DPH");
|
||||||
|
expect(html).not.toContain("Mezisoučet");
|
||||||
|
expect(html).not.toContain("Celkem k úhradě");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("renders a fractional quantity as '1,50', not rounded to '2'", async () => {
|
||||||
|
const quotation = await prisma.quotations.create({
|
||||||
|
data: {
|
||||||
|
quotation_number: `Q-FRACQTY-${Date.now()}`,
|
||||||
|
status: "active",
|
||||||
|
currency: "CZK",
|
||||||
|
language: "cs",
|
||||||
|
quotation_items: {
|
||||||
|
create: [
|
||||||
|
{
|
||||||
|
description: "Půldenní sazba",
|
||||||
|
quantity: 1.5,
|
||||||
|
unit: "ks",
|
||||||
|
unit_price: 1000,
|
||||||
|
is_included_in_total: true,
|
||||||
|
position: 0,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
createdQuotationIds.push(quotation.id);
|
||||||
|
|
||||||
|
const res = await app!.inject({
|
||||||
|
method: "GET",
|
||||||
|
url: `/api/admin/offers-pdf/${quotation.id}`,
|
||||||
|
headers: { Authorization: `Bearer ${adminToken}` },
|
||||||
|
});
|
||||||
|
expect(res.statusCode).toBe(200);
|
||||||
|
const html = res.body;
|
||||||
|
// The old toFixed(0) ROUNDED 1.5 → "2"; integers still render without
|
||||||
|
// decimals elsewhere (covered by the previous test's qty 2).
|
||||||
|
expect(html).toContain("1,50 / ks");
|
||||||
|
expect(html).not.toContain(">2 / ks<");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
/* -------------------------------------------------------------------------- */
|
||||||
|
/* Invoice PDF route — post-pdf-shared-extraction render marker */
|
||||||
|
/* -------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
describe("GET /api/admin/invoices-pdf/:id (invoice PDF html)", () => {
|
||||||
|
it("still renders the invoice HTML after the shared-helper extraction", async () => {
|
||||||
|
// Draft → no number consumed; CZK → no CNB exchange-rate lookup.
|
||||||
|
const invoice = await createInvoice({
|
||||||
|
status: "draft",
|
||||||
|
currency: "CZK",
|
||||||
|
apply_vat: true,
|
||||||
|
vat_rate: 21,
|
||||||
|
items: [
|
||||||
|
{
|
||||||
|
description: "PDF-SHARED-MARKER",
|
||||||
|
quantity: 2,
|
||||||
|
unit_price: 1000,
|
||||||
|
vat_rate: 21,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
createdInvoiceIds.push(invoice.id);
|
||||||
|
|
||||||
|
const res = await app!.inject({
|
||||||
|
method: "GET",
|
||||||
|
url: `/api/admin/invoices-pdf/${invoice.id}`,
|
||||||
|
headers: { Authorization: `Bearer ${adminToken}` },
|
||||||
|
});
|
||||||
|
expect(res.statusCode).toBe(200);
|
||||||
|
const html = res.body;
|
||||||
|
expect(html).toContain("FAKTURA - DAŇOVÝ DOKLAD");
|
||||||
|
expect(html).toContain("PDF-SHARED-MARKER");
|
||||||
|
// Shared NBSP formatNum: 2 × 1000 = 2 000,00 base.
|
||||||
|
expect(html).toContain("2 000,00");
|
||||||
|
expect(html).toContain("Celkem k úhradě");
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -58,34 +58,6 @@ describe("Zod form coercion + NaN rejection", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe("CreateQuotationSchema", () => {
|
describe("CreateQuotationSchema", () => {
|
||||||
it("rejects NaN string in top-level vat_rate", () => {
|
|
||||||
const result = CreateQuotationSchema.safeParse({
|
|
||||||
customer_id: 1,
|
|
||||||
vat_rate: "bad",
|
|
||||||
});
|
|
||||||
expect(result.success).toBe(false);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("accepts valid vat_rate", () => {
|
|
||||||
const result = CreateQuotationSchema.safeParse({
|
|
||||||
customer_id: 1,
|
|
||||||
vat_rate: 21,
|
|
||||||
});
|
|
||||||
expect(result.success).toBe(true);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("coerces a valid numeric STRING vat_rate to a number", () => {
|
|
||||||
const result = CreateQuotationSchema.safeParse({
|
|
||||||
customer_id: 1,
|
|
||||||
vat_rate: "21",
|
|
||||||
});
|
|
||||||
expect(result.success).toBe(true);
|
|
||||||
if (result.success) {
|
|
||||||
expect(result.data.vat_rate).toBe(21);
|
|
||||||
expect(typeof result.data.vat_rate).toBe("number");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
it("rejects NaN string in item quantity", () => {
|
it("rejects NaN string in item quantity", () => {
|
||||||
const result = CreateQuotationSchema.safeParse({
|
const result = CreateQuotationSchema.safeParse({
|
||||||
customer_id: 1,
|
customer_id: 1,
|
||||||
@@ -300,8 +272,8 @@ describe("schema hardening — does not reject previously-valid input", () => {
|
|||||||
expect(b.success).toBe(true);
|
expect(b.success).toBe(true);
|
||||||
if (b.success) expect(b.data.trip_date).toBe("2026-06-09");
|
if (b.success) expect(b.data.trip_date).toBe("2026-06-09");
|
||||||
// Genuinely malformed dates are still rejected.
|
// Genuinely malformed dates are still rejected.
|
||||||
expect(CreateTripSchema.safeParse({ ...base, trip_date: "09/06/2026" }).success).toBe(
|
expect(
|
||||||
false,
|
CreateTripSchema.safeParse({ ...base, trip_date: "09/06/2026" }).success,
|
||||||
);
|
).toBe(false);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -7,8 +7,8 @@ import { useTheme } from "@mui/material/styles";
|
|||||||
import { Modal, Button, TextField, Field } from "../ui";
|
import { Modal, Button, TextField, Field } from "../ui";
|
||||||
import { useAlert } from "../context/AlertContext";
|
import { useAlert } from "../context/AlertContext";
|
||||||
|
|
||||||
// Editable line-item. quantity/unit_price/vat_rate are held as the raw typed
|
// Editable line-item. quantity/unit_price are held as the raw typed string
|
||||||
// string while editing (so a field can be cleared — empty renders fine in a
|
// while editing (so a field can be cleared — empty renders fine in a
|
||||||
// type="number" input) and are coerced to numbers in handleEditGenerate before
|
// type="number" input) and are coerced to numbers in handleEditGenerate before
|
||||||
// being handed to onGenerate (the parent posts them verbatim).
|
// being handed to onGenerate (the parent posts them verbatim).
|
||||||
interface ConfirmationItem {
|
interface ConfirmationItem {
|
||||||
@@ -17,7 +17,6 @@ interface ConfirmationItem {
|
|||||||
unit: string;
|
unit: string;
|
||||||
unit_price: string | number;
|
unit_price: string | number;
|
||||||
is_included_in_total: boolean;
|
is_included_in_total: boolean;
|
||||||
vat_rate: string | number;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Numeric shape handed to the parent (the raw editing strings are coerced in
|
// Numeric shape handed to the parent (the raw editing strings are coerced in
|
||||||
@@ -28,21 +27,14 @@ interface GeneratedItem {
|
|||||||
unit: string;
|
unit: string;
|
||||||
unit_price: number;
|
unit_price: number;
|
||||||
is_included_in_total: boolean;
|
is_included_in_total: boolean;
|
||||||
vat_rate: number;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
interface OrderConfirmationModalProps {
|
interface OrderConfirmationModalProps {
|
||||||
isOpen: boolean;
|
isOpen: boolean;
|
||||||
onClose: () => void;
|
onClose: () => void;
|
||||||
onGenerate: (
|
onGenerate: (lang: string, items?: GeneratedItem[]) => Promise<void>;
|
||||||
lang: string,
|
|
||||||
applyVat: boolean,
|
|
||||||
items?: GeneratedItem[],
|
|
||||||
) => Promise<void>;
|
|
||||||
initialItems: ConfirmationItem[];
|
initialItems: ConfirmationItem[];
|
||||||
orderNumber: string;
|
orderNumber: string;
|
||||||
defaultVatRate: number;
|
|
||||||
applyVat: boolean;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function OrderConfirmationModal({
|
export default function OrderConfirmationModal({
|
||||||
@@ -51,15 +43,12 @@ export default function OrderConfirmationModal({
|
|||||||
onGenerate,
|
onGenerate,
|
||||||
initialItems,
|
initialItems,
|
||||||
orderNumber,
|
orderNumber,
|
||||||
defaultVatRate,
|
|
||||||
applyVat,
|
|
||||||
}: OrderConfirmationModalProps) {
|
}: OrderConfirmationModalProps) {
|
||||||
const alert = useAlert();
|
const alert = useAlert();
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
const isMobile = useMediaQuery(theme.breakpoints.down("sm"));
|
const isMobile = useMediaQuery(theme.breakpoints.down("sm"));
|
||||||
const [step, setStep] = useState<"choose" | "edit">("choose");
|
const [step, setStep] = useState<"choose" | "edit">("choose");
|
||||||
const [lang, setLang] = useState<string>("cs");
|
const [lang, setLang] = useState<string>("cs");
|
||||||
const [applyVatState, setApplyVatState] = useState(applyVat);
|
|
||||||
const [items, setItems] = useState<ConfirmationItem[]>(initialItems);
|
const [items, setItems] = useState<ConfirmationItem[]>(initialItems);
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
|
|
||||||
@@ -72,17 +61,16 @@ export default function OrderConfirmationModal({
|
|||||||
if (!isOpen) return;
|
if (!isOpen) return;
|
||||||
setStep("choose");
|
setStep("choose");
|
||||||
setLang("cs");
|
setLang("cs");
|
||||||
setApplyVatState(applyVat);
|
|
||||||
setItems(initialItems);
|
setItems(initialItems);
|
||||||
// initialItems/applyVat are captured at open time; intentionally not in the
|
// initialItems are captured at open time; intentionally not in the dep
|
||||||
// dep array so an unrelated parent re-render doesn't clobber the user's edits.
|
// array so an unrelated parent re-render doesn't clobber the user's edits.
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
}, [isOpen]);
|
}, [isOpen]);
|
||||||
|
|
||||||
const handleUseExisting = async () => {
|
const handleUseExisting = async () => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
try {
|
try {
|
||||||
await onGenerate(lang, applyVatState, undefined);
|
await onGenerate(lang, undefined);
|
||||||
// Only close on success — a generation error must keep the modal open.
|
// Only close on success — a generation error must keep the modal open.
|
||||||
onClose();
|
onClose();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@@ -104,9 +92,8 @@ export default function OrderConfirmationModal({
|
|||||||
unit: it.unit,
|
unit: it.unit,
|
||||||
unit_price: Number(it.unit_price) || 0,
|
unit_price: Number(it.unit_price) || 0,
|
||||||
is_included_in_total: it.is_included_in_total,
|
is_included_in_total: it.is_included_in_total,
|
||||||
vat_rate: Number(it.vat_rate) || 0,
|
|
||||||
}));
|
}));
|
||||||
await onGenerate(lang, applyVatState, coercedItems);
|
await onGenerate(lang, coercedItems);
|
||||||
// Only close on success — on error keep the user's edited items intact.
|
// Only close on success — on error keep the user's edited items intact.
|
||||||
onClose();
|
onClose();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@@ -145,10 +132,9 @@ export default function OrderConfirmationModal({
|
|||||||
unit: "ks",
|
unit: "ks",
|
||||||
unit_price: 0,
|
unit_price: 0,
|
||||||
is_included_in_total: true,
|
is_included_in_total: true,
|
||||||
vat_rate: defaultVatRate,
|
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
}, [defaultVatRate]);
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Modal
|
<Modal
|
||||||
@@ -186,27 +172,6 @@ export default function OrderConfirmationModal({
|
|||||||
</Box>
|
</Box>
|
||||||
</Field>
|
</Field>
|
||||||
|
|
||||||
<Field label="DPH">
|
|
||||||
<Box sx={{ display: "flex", gap: 1 }}>
|
|
||||||
<Button
|
|
||||||
size="small"
|
|
||||||
variant={applyVatState ? "contained" : "outlined"}
|
|
||||||
color={applyVatState ? "primary" : "inherit"}
|
|
||||||
onClick={() => setApplyVatState(true)}
|
|
||||||
>
|
|
||||||
S DPH
|
|
||||||
</Button>
|
|
||||||
<Button
|
|
||||||
size="small"
|
|
||||||
variant={!applyVatState ? "contained" : "outlined"}
|
|
||||||
color={!applyVatState ? "primary" : "inherit"}
|
|
||||||
onClick={() => setApplyVatState(false)}
|
|
||||||
>
|
|
||||||
Bez DPH
|
|
||||||
</Button>
|
|
||||||
</Box>
|
|
||||||
</Field>
|
|
||||||
|
|
||||||
<Field label="Obsah potvrzení">
|
<Field label="Obsah potvrzení">
|
||||||
<Typography variant="body2" color="text.secondary" sx={{ mb: 1.5 }}>
|
<Typography variant="body2" color="text.secondary" sx={{ mb: 1.5 }}>
|
||||||
Jak chcete připravit potvrzení objednávky?
|
Jak chcete připravit potvrzení objednávky?
|
||||||
@@ -291,7 +256,7 @@ export default function OrderConfirmationModal({
|
|||||||
<Box
|
<Box
|
||||||
sx={{
|
sx={{
|
||||||
display: "grid",
|
display: "grid",
|
||||||
gridTemplateColumns: "repeat(2, minmax(0, 1fr))",
|
gridTemplateColumns: "repeat(3, minmax(0, 1fr))",
|
||||||
gap: 1,
|
gap: 1,
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
@@ -318,15 +283,6 @@ export default function OrderConfirmationModal({
|
|||||||
}
|
}
|
||||||
slotProps={{ htmlInput: { step: "0.01" } }}
|
slotProps={{ htmlInput: { step: "0.01" } }}
|
||||||
/>
|
/>
|
||||||
<TextField
|
|
||||||
label="%DPH"
|
|
||||||
type="number"
|
|
||||||
value={item.vat_rate ?? ""}
|
|
||||||
onChange={(e) =>
|
|
||||||
updateItem(i, "vat_rate", e.target.value)
|
|
||||||
}
|
|
||||||
slotProps={{ htmlInput: { step: "1" } }}
|
|
||||||
/>
|
|
||||||
</Box>
|
</Box>
|
||||||
</Box>
|
</Box>
|
||||||
))}
|
))}
|
||||||
@@ -356,7 +312,6 @@ export default function OrderConfirmationModal({
|
|||||||
<th>Mn.</th>
|
<th>Mn.</th>
|
||||||
<th>Jedn.</th>
|
<th>Jedn.</th>
|
||||||
<th>Cena</th>
|
<th>Cena</th>
|
||||||
<th>%DPH</th>
|
|
||||||
<th style={{ width: "40px" }} />
|
<th style={{ width: "40px" }} />
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
@@ -403,17 +358,6 @@ export default function OrderConfirmationModal({
|
|||||||
slotProps={{ htmlInput: { step: "0.01" } }}
|
slotProps={{ htmlInput: { step: "0.01" } }}
|
||||||
/>
|
/>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
|
||||||
<TextField
|
|
||||||
type="number"
|
|
||||||
value={item.vat_rate ?? ""}
|
|
||||||
onChange={(e) =>
|
|
||||||
updateItem(i, "vat_rate", e.target.value)
|
|
||||||
}
|
|
||||||
sx={{ width: 80 }}
|
|
||||||
slotProps={{ htmlInput: { step: "1" } }}
|
|
||||||
/>
|
|
||||||
</td>
|
|
||||||
<td>
|
<td>
|
||||||
<IconButton
|
<IconButton
|
||||||
size="small"
|
size="small"
|
||||||
|
|||||||
667
src/admin/components/document/DocumentItemsEditor.tsx
Normal file
667
src/admin/components/document/DocumentItemsEditor.tsx
Normal file
@@ -0,0 +1,667 @@
|
|||||||
|
import type { ReactNode } from "react";
|
||||||
|
import Box from "@mui/material/Box";
|
||||||
|
import Typography from "@mui/material/Typography";
|
||||||
|
import IconButton from "@mui/material/IconButton";
|
||||||
|
import Checkbox from "@mui/material/Checkbox";
|
||||||
|
import Table from "@mui/material/Table";
|
||||||
|
import TableBody from "@mui/material/TableBody";
|
||||||
|
import TableCell from "@mui/material/TableCell";
|
||||||
|
import TableContainer from "@mui/material/TableContainer";
|
||||||
|
import TableHead from "@mui/material/TableHead";
|
||||||
|
import TableRow from "@mui/material/TableRow";
|
||||||
|
import useMediaQuery from "@mui/material/useMediaQuery";
|
||||||
|
import { useTheme } from "@mui/material/styles";
|
||||||
|
import {
|
||||||
|
DndContext,
|
||||||
|
closestCenter,
|
||||||
|
KeyboardSensor,
|
||||||
|
MouseSensor,
|
||||||
|
TouchSensor,
|
||||||
|
useSensor,
|
||||||
|
useSensors,
|
||||||
|
type DragEndEvent,
|
||||||
|
} from "@dnd-kit/core";
|
||||||
|
import {
|
||||||
|
SortableContext,
|
||||||
|
verticalListSortingStrategy,
|
||||||
|
useSortable,
|
||||||
|
arrayMove,
|
||||||
|
} from "@dnd-kit/sortable";
|
||||||
|
import {
|
||||||
|
restrictToVerticalAxis,
|
||||||
|
restrictToParentElement,
|
||||||
|
} from "@dnd-kit/modifiers";
|
||||||
|
import { CSS } from "@dnd-kit/utilities";
|
||||||
|
import { Button, Card, TextField } from "../../ui";
|
||||||
|
import { formatCurrency } from "../../utils/formatters";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* One sortable line item of a document (offer / issued order). The numeric
|
||||||
|
* fields are held as the raw typed string while editing so the input can be
|
||||||
|
* cleared (empty renders fine in a type="number" input); coerce via
|
||||||
|
* `Number(x) || 0` only where used (live totals + save payload).
|
||||||
|
*/
|
||||||
|
export interface DocumentItem {
|
||||||
|
_key: string;
|
||||||
|
id?: number;
|
||||||
|
description: string;
|
||||||
|
item_description: string;
|
||||||
|
quantity: string | number;
|
||||||
|
unit: string;
|
||||||
|
unit_price: string | number;
|
||||||
|
/** Offers only — undefined (issued orders) counts as included. */
|
||||||
|
is_included_in_total?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
let documentItemKeyCounter = 0;
|
||||||
|
|
||||||
|
/** Unique client-side key for a (new or hydrated) document item row. */
|
||||||
|
export const nextDocumentItemKey = (): string =>
|
||||||
|
`item-${++documentItemKeyCounter}`;
|
||||||
|
|
||||||
|
export const emptyDocumentItem = (): DocumentItem => ({
|
||||||
|
_key: nextDocumentItemKey(),
|
||||||
|
description: "",
|
||||||
|
item_description: "",
|
||||||
|
quantity: 1,
|
||||||
|
unit: "ks",
|
||||||
|
unit_price: 0,
|
||||||
|
is_included_in_total: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
/** NET total over the included items (documents here carry no VAT). */
|
||||||
|
export const documentItemsTotal = (items: DocumentItem[]): number =>
|
||||||
|
items.reduce(
|
||||||
|
(sum, it) =>
|
||||||
|
it.is_included_in_total === false
|
||||||
|
? sum
|
||||||
|
: sum + (Number(it.quantity) || 0) * (Number(it.unit_price) || 0),
|
||||||
|
0,
|
||||||
|
);
|
||||||
|
|
||||||
|
const DragIcon = (
|
||||||
|
<svg width="14" height="14" viewBox="0 0 24 24" fill="currentColor">
|
||||||
|
<circle cx="9" cy="5" r="1.5" />
|
||||||
|
<circle cx="15" cy="5" r="1.5" />
|
||||||
|
<circle cx="9" cy="12" r="1.5" />
|
||||||
|
<circle cx="15" cy="12" r="1.5" />
|
||||||
|
<circle cx="9" cy="19" r="1.5" />
|
||||||
|
<circle cx="15" cy="19" r="1.5" />
|
||||||
|
</svg>
|
||||||
|
);
|
||||||
|
|
||||||
|
const RemoveIcon = (
|
||||||
|
<svg
|
||||||
|
width="16"
|
||||||
|
height="16"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
strokeWidth="2"
|
||||||
|
>
|
||||||
|
<line x1="18" y1="6" x2="6" y2="18" />
|
||||||
|
<line x1="6" y1="6" x2="18" y2="18" />
|
||||||
|
</svg>
|
||||||
|
);
|
||||||
|
|
||||||
|
function SortableItemRow({
|
||||||
|
item,
|
||||||
|
index,
|
||||||
|
currency,
|
||||||
|
readOnly,
|
||||||
|
canDelete,
|
||||||
|
showIncludedInTotal,
|
||||||
|
itemDescriptionMaxLength,
|
||||||
|
onUpdate,
|
||||||
|
onRemove,
|
||||||
|
}: {
|
||||||
|
item: DocumentItem;
|
||||||
|
index: number;
|
||||||
|
currency: string;
|
||||||
|
readOnly: boolean;
|
||||||
|
canDelete: boolean;
|
||||||
|
showIncludedInTotal: boolean;
|
||||||
|
itemDescriptionMaxLength: number;
|
||||||
|
onUpdate: (field: keyof DocumentItem, value: string | boolean) => void;
|
||||||
|
onRemove: () => void;
|
||||||
|
}) {
|
||||||
|
const {
|
||||||
|
attributes,
|
||||||
|
listeners,
|
||||||
|
setNodeRef,
|
||||||
|
transform,
|
||||||
|
transition,
|
||||||
|
isDragging,
|
||||||
|
} = useSortable({ id: item._key, disabled: readOnly });
|
||||||
|
const theme = useTheme();
|
||||||
|
const isMobile = useMediaQuery(theme.breakpoints.down("sm"));
|
||||||
|
const style = {
|
||||||
|
transform: CSS.Transform.toString(transform),
|
||||||
|
transition,
|
||||||
|
opacity: isDragging ? 0.5 : 1,
|
||||||
|
background: isDragging ? "var(--mui-palette-action-hover)" : undefined,
|
||||||
|
position: "relative" as const,
|
||||||
|
zIndex: isDragging ? 10 : undefined,
|
||||||
|
};
|
||||||
|
const lineTotal =
|
||||||
|
(Number(item.quantity) || 0) * (Number(item.unit_price) || 0);
|
||||||
|
|
||||||
|
if (isMobile) {
|
||||||
|
return (
|
||||||
|
<Box
|
||||||
|
ref={setNodeRef}
|
||||||
|
sx={{
|
||||||
|
border: 1,
|
||||||
|
borderColor: "divider",
|
||||||
|
borderRadius: 2,
|
||||||
|
p: 1.5,
|
||||||
|
display: "flex",
|
||||||
|
flexDirection: "column",
|
||||||
|
gap: 1.25,
|
||||||
|
bgcolor: "background.paper",
|
||||||
|
...style,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Box sx={{ display: "flex", alignItems: "center", gap: 1 }}>
|
||||||
|
{!readOnly && (
|
||||||
|
<IconButton
|
||||||
|
size="small"
|
||||||
|
{...attributes}
|
||||||
|
{...listeners}
|
||||||
|
title="Přetáhnout"
|
||||||
|
aria-label="Přetáhnout"
|
||||||
|
sx={{
|
||||||
|
cursor: "grab",
|
||||||
|
color: "text.secondary",
|
||||||
|
touchAction: "none",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{DragIcon}
|
||||||
|
</IconButton>
|
||||||
|
)}
|
||||||
|
<Typography
|
||||||
|
variant="caption"
|
||||||
|
sx={{ color: "text.secondary", fontWeight: 700 }}
|
||||||
|
>
|
||||||
|
Položka {index + 1}
|
||||||
|
</Typography>
|
||||||
|
<Box sx={{ flex: 1 }} />
|
||||||
|
{!readOnly && (
|
||||||
|
<IconButton
|
||||||
|
color="error"
|
||||||
|
size="small"
|
||||||
|
onClick={onRemove}
|
||||||
|
title="Odebrat"
|
||||||
|
aria-label="Odebrat"
|
||||||
|
disabled={!canDelete}
|
||||||
|
>
|
||||||
|
{RemoveIcon}
|
||||||
|
</IconButton>
|
||||||
|
)}
|
||||||
|
</Box>
|
||||||
|
<TextField
|
||||||
|
label="Popis"
|
||||||
|
value={item.description}
|
||||||
|
onChange={(e) => onUpdate("description", e.target.value)}
|
||||||
|
placeholder="Popis položky…"
|
||||||
|
InputProps={{ readOnly }}
|
||||||
|
slotProps={{ htmlInput: { maxLength: 500 } }}
|
||||||
|
/>
|
||||||
|
<TextField
|
||||||
|
label="Podrobný popis"
|
||||||
|
value={item.item_description}
|
||||||
|
onChange={(e) => onUpdate("item_description", e.target.value)}
|
||||||
|
placeholder="Volitelný"
|
||||||
|
InputProps={{ readOnly }}
|
||||||
|
multiline
|
||||||
|
rows={2}
|
||||||
|
slotProps={{ htmlInput: { maxLength: itemDescriptionMaxLength } }}
|
||||||
|
sx={{ "& textarea": { resize: "vertical" } }}
|
||||||
|
/>
|
||||||
|
<Box
|
||||||
|
sx={{
|
||||||
|
display: "grid",
|
||||||
|
gridTemplateColumns: "repeat(3, minmax(0, 1fr))",
|
||||||
|
gap: 1,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<TextField
|
||||||
|
label="Množství"
|
||||||
|
type="number"
|
||||||
|
value={item.quantity ?? ""}
|
||||||
|
onChange={(e) => onUpdate("quantity", e.target.value)}
|
||||||
|
slotProps={{ htmlInput: { min: "0", step: "any" } }}
|
||||||
|
InputProps={{ readOnly }}
|
||||||
|
sx={{ "& input": { textAlign: "center" } }}
|
||||||
|
/>
|
||||||
|
<TextField
|
||||||
|
label="Jednotka"
|
||||||
|
value={item.unit}
|
||||||
|
onChange={(e) => onUpdate("unit", e.target.value)}
|
||||||
|
placeholder="ks"
|
||||||
|
InputProps={{ readOnly }}
|
||||||
|
slotProps={{ htmlInput: { maxLength: 20 } }}
|
||||||
|
sx={{ "& input": { textAlign: "center" } }}
|
||||||
|
/>
|
||||||
|
<TextField
|
||||||
|
label="Jedn. cena"
|
||||||
|
type="number"
|
||||||
|
value={item.unit_price ?? ""}
|
||||||
|
onChange={(e) => onUpdate("unit_price", e.target.value)}
|
||||||
|
slotProps={{ htmlInput: { min: "0", step: "any" } }}
|
||||||
|
InputProps={{ readOnly }}
|
||||||
|
sx={{ "& input": { textAlign: "right" } }}
|
||||||
|
/>
|
||||||
|
</Box>
|
||||||
|
<Box
|
||||||
|
sx={{
|
||||||
|
display: "flex",
|
||||||
|
justifyContent: showIncludedInTotal ? "space-between" : "flex-end",
|
||||||
|
alignItems: "baseline",
|
||||||
|
gap: 1,
|
||||||
|
pt: 0.5,
|
||||||
|
borderTop: 1,
|
||||||
|
borderColor: "divider",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{showIncludedInTotal && (
|
||||||
|
<Box
|
||||||
|
component="label"
|
||||||
|
sx={{
|
||||||
|
display: "flex",
|
||||||
|
alignItems: "center",
|
||||||
|
gap: 0.5,
|
||||||
|
cursor: readOnly ? "default" : "pointer",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Checkbox
|
||||||
|
checked={item.is_included_in_total !== false}
|
||||||
|
onChange={(e) =>
|
||||||
|
onUpdate("is_included_in_total", e.target.checked)
|
||||||
|
}
|
||||||
|
disabled={readOnly}
|
||||||
|
/>
|
||||||
|
<Typography variant="body2">V ceně</Typography>
|
||||||
|
</Box>
|
||||||
|
)}
|
||||||
|
<Box
|
||||||
|
sx={{
|
||||||
|
display: "flex",
|
||||||
|
alignItems: "baseline",
|
||||||
|
gap: 1,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Typography variant="caption" sx={{ color: "text.secondary" }}>
|
||||||
|
Celkem
|
||||||
|
</Typography>
|
||||||
|
<Typography
|
||||||
|
sx={{
|
||||||
|
fontFamily: "'DM Mono', Menlo, monospace",
|
||||||
|
fontWeight: 600,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{formatCurrency(lineTotal, currency)}
|
||||||
|
</Typography>
|
||||||
|
</Box>
|
||||||
|
</Box>
|
||||||
|
</Box>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<TableRow ref={setNodeRef} sx={style}>
|
||||||
|
{/* Stable column layout: every cell renders in read-only mode too —
|
||||||
|
only the controls inside are hidden. */}
|
||||||
|
<TableCell sx={{ width: "2rem", px: 0.5 }}>
|
||||||
|
{!readOnly && (
|
||||||
|
<IconButton
|
||||||
|
size="small"
|
||||||
|
{...attributes}
|
||||||
|
{...listeners}
|
||||||
|
title="Přetáhnout"
|
||||||
|
aria-label="Přetáhnout"
|
||||||
|
sx={{
|
||||||
|
cursor: "grab",
|
||||||
|
color: "text.secondary",
|
||||||
|
touchAction: "none",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{DragIcon}
|
||||||
|
</IconButton>
|
||||||
|
)}
|
||||||
|
</TableCell>
|
||||||
|
<TableCell
|
||||||
|
align="center"
|
||||||
|
sx={{ color: "text.secondary", fontWeight: 500 }}
|
||||||
|
>
|
||||||
|
{index + 1}
|
||||||
|
</TableCell>
|
||||||
|
<TableCell sx={{ verticalAlign: "top" }}>
|
||||||
|
<Box sx={{ display: "flex", flexDirection: "column", gap: 0.5 }}>
|
||||||
|
<TextField
|
||||||
|
value={item.description}
|
||||||
|
onChange={(e) => onUpdate("description", e.target.value)}
|
||||||
|
placeholder="Popis položky…"
|
||||||
|
InputProps={{ readOnly }}
|
||||||
|
slotProps={{ htmlInput: { maxLength: 500 } }}
|
||||||
|
sx={{ "& input": { fontWeight: 500 } }}
|
||||||
|
/>
|
||||||
|
<TextField
|
||||||
|
value={item.item_description}
|
||||||
|
onChange={(e) => onUpdate("item_description", e.target.value)}
|
||||||
|
placeholder="Podrobný popis (volitelný)"
|
||||||
|
InputProps={{ readOnly }}
|
||||||
|
multiline
|
||||||
|
rows={2}
|
||||||
|
slotProps={{ htmlInput: { maxLength: itemDescriptionMaxLength } }}
|
||||||
|
sx={{
|
||||||
|
"& textarea": {
|
||||||
|
fontSize: "0.8rem",
|
||||||
|
opacity: 0.8,
|
||||||
|
resize: "vertical",
|
||||||
|
},
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</Box>
|
||||||
|
</TableCell>
|
||||||
|
<TableCell>
|
||||||
|
<TextField
|
||||||
|
type="number"
|
||||||
|
value={item.quantity ?? ""}
|
||||||
|
onChange={(e) => onUpdate("quantity", e.target.value)}
|
||||||
|
slotProps={{ htmlInput: { min: "0", step: "any" } }}
|
||||||
|
InputProps={{ readOnly }}
|
||||||
|
sx={{ "& input": { textAlign: "center" } }}
|
||||||
|
/>
|
||||||
|
</TableCell>
|
||||||
|
<TableCell>
|
||||||
|
<TextField
|
||||||
|
value={item.unit}
|
||||||
|
onChange={(e) => onUpdate("unit", e.target.value)}
|
||||||
|
placeholder="ks"
|
||||||
|
InputProps={{ readOnly }}
|
||||||
|
slotProps={{ htmlInput: { maxLength: 20 } }}
|
||||||
|
sx={{ "& input": { textAlign: "center" } }}
|
||||||
|
/>
|
||||||
|
</TableCell>
|
||||||
|
<TableCell>
|
||||||
|
<TextField
|
||||||
|
type="number"
|
||||||
|
value={item.unit_price ?? ""}
|
||||||
|
onChange={(e) => onUpdate("unit_price", e.target.value)}
|
||||||
|
slotProps={{ htmlInput: { min: "0", step: "any" } }}
|
||||||
|
InputProps={{ readOnly }}
|
||||||
|
sx={{ "& input": { textAlign: "right" } }}
|
||||||
|
/>
|
||||||
|
</TableCell>
|
||||||
|
{showIncludedInTotal && (
|
||||||
|
<TableCell align="center">
|
||||||
|
<Checkbox
|
||||||
|
checked={item.is_included_in_total !== false}
|
||||||
|
onChange={(e) => onUpdate("is_included_in_total", e.target.checked)}
|
||||||
|
disabled={readOnly}
|
||||||
|
/>
|
||||||
|
</TableCell>
|
||||||
|
)}
|
||||||
|
<TableCell
|
||||||
|
align="right"
|
||||||
|
sx={{
|
||||||
|
fontWeight: 600,
|
||||||
|
whiteSpace: "nowrap",
|
||||||
|
fontFamily: "'DM Mono', Menlo, monospace",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{formatCurrency(lineTotal, currency)}
|
||||||
|
</TableCell>
|
||||||
|
<TableCell sx={{ width: "2.5rem" }}>
|
||||||
|
{!readOnly && (
|
||||||
|
<IconButton
|
||||||
|
color="error"
|
||||||
|
size="small"
|
||||||
|
onClick={onRemove}
|
||||||
|
title="Odebrat"
|
||||||
|
aria-label="Odebrat"
|
||||||
|
disabled={!canDelete}
|
||||||
|
>
|
||||||
|
{RemoveIcon}
|
||||||
|
</IconButton>
|
||||||
|
)}
|
||||||
|
</TableCell>
|
||||||
|
</TableRow>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
interface DocumentItemsEditorProps {
|
||||||
|
items: DocumentItem[];
|
||||||
|
onChange: (items: DocumentItem[]) => void;
|
||||||
|
currency: string;
|
||||||
|
readOnly: boolean;
|
||||||
|
/** Validation error shown under the section title. */
|
||||||
|
error?: string;
|
||||||
|
/** Render the offers-only "V ceně" (is_included_in_total) column. */
|
||||||
|
showIncludedInTotal?: boolean;
|
||||||
|
/** Schema cap for the detailed description (offers 8000, issued orders 5000). */
|
||||||
|
itemDescriptionMaxLength?: number;
|
||||||
|
/** Optional page-specific control (e.g. item-template Select) next to the add button. */
|
||||||
|
templatesSlot?: ReactNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sortable line-items table shared by the document detail pages (offers,
|
||||||
|
* issued orders): drag-and-drop reorder, mobile card layout, live NET totals
|
||||||
|
* ("Celkem bez DPH" — these documents are not tax documents).
|
||||||
|
*/
|
||||||
|
export default function DocumentItemsEditor({
|
||||||
|
items,
|
||||||
|
onChange,
|
||||||
|
currency,
|
||||||
|
readOnly,
|
||||||
|
error,
|
||||||
|
showIncludedInTotal = false,
|
||||||
|
itemDescriptionMaxLength = 5000,
|
||||||
|
templatesSlot,
|
||||||
|
}: DocumentItemsEditorProps) {
|
||||||
|
const theme = useTheme();
|
||||||
|
const isMobile = useMediaQuery(theme.breakpoints.down("sm"));
|
||||||
|
// MouseSensor (NOT PointerSensor): PointerSensor also receives touch-derived
|
||||||
|
// pointer events and its 5px distance constraint fires before the
|
||||||
|
// TouchSensor's long-press delay — the browser then claims the gesture for
|
||||||
|
// scrolling (pointercancel) and the drag dies. Mouse handles desktop,
|
||||||
|
// TouchSensor handles touch via long-press; the drag handles additionally
|
||||||
|
// carry touch-action: none so the browser never starts a scroll from them.
|
||||||
|
const dndSensors = useSensors(
|
||||||
|
useSensor(MouseSensor, { activationConstraint: { distance: 5 } }),
|
||||||
|
useSensor(TouchSensor, {
|
||||||
|
activationConstraint: { delay: 300, tolerance: 8 },
|
||||||
|
}),
|
||||||
|
useSensor(KeyboardSensor),
|
||||||
|
);
|
||||||
|
|
||||||
|
const updateItem = (
|
||||||
|
index: number,
|
||||||
|
field: keyof DocumentItem,
|
||||||
|
value: string | boolean,
|
||||||
|
) =>
|
||||||
|
onChange(
|
||||||
|
items.map((it, i) => (i === index ? { ...it, [field]: value } : it)),
|
||||||
|
);
|
||||||
|
|
||||||
|
const addItem = () => onChange([...items, emptyDocumentItem()]);
|
||||||
|
|
||||||
|
const removeItem = (index: number) => {
|
||||||
|
if (items.length <= 1) return;
|
||||||
|
onChange(items.filter((_, i) => i !== index));
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleDragEnd = (event: DragEndEvent) => {
|
||||||
|
const { active, over } = event;
|
||||||
|
if (!over || active.id === over.id) return;
|
||||||
|
const oldIndex = items.findIndex((i) => i._key === String(active.id));
|
||||||
|
const newIndex = items.findIndex((i) => i._key === String(over.id));
|
||||||
|
if (oldIndex === -1 || newIndex === -1) return;
|
||||||
|
onChange(arrayMove(items, oldIndex, newIndex));
|
||||||
|
};
|
||||||
|
|
||||||
|
const total = documentItemsTotal(items);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Card sx={{ mb: 3 }}>
|
||||||
|
<Box
|
||||||
|
sx={{
|
||||||
|
display: "flex",
|
||||||
|
justifyContent: "space-between",
|
||||||
|
alignItems: "flex-start",
|
||||||
|
mb: 2,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Box>
|
||||||
|
<Typography variant="subtitle2" sx={{ fontWeight: 600 }}>
|
||||||
|
Položky
|
||||||
|
</Typography>
|
||||||
|
{error && (
|
||||||
|
<Typography variant="caption" color="error">
|
||||||
|
{error}
|
||||||
|
</Typography>
|
||||||
|
)}
|
||||||
|
</Box>
|
||||||
|
{!readOnly && (
|
||||||
|
<Box sx={{ display: "flex", gap: 1, alignItems: "center" }}>
|
||||||
|
{templatesSlot}
|
||||||
|
<Button
|
||||||
|
type="button"
|
||||||
|
onClick={addItem}
|
||||||
|
variant="outlined"
|
||||||
|
color="inherit"
|
||||||
|
size="small"
|
||||||
|
>
|
||||||
|
+ Přidat položku
|
||||||
|
</Button>
|
||||||
|
</Box>
|
||||||
|
)}
|
||||||
|
</Box>
|
||||||
|
|
||||||
|
<DndContext
|
||||||
|
sensors={dndSensors}
|
||||||
|
collisionDetection={closestCenter}
|
||||||
|
modifiers={[restrictToVerticalAxis, restrictToParentElement]}
|
||||||
|
onDragEnd={handleDragEnd}
|
||||||
|
>
|
||||||
|
<SortableContext
|
||||||
|
items={items.map((i) => i._key)}
|
||||||
|
strategy={verticalListSortingStrategy}
|
||||||
|
>
|
||||||
|
{isMobile ? (
|
||||||
|
<Box sx={{ display: "flex", flexDirection: "column", gap: 1.5 }}>
|
||||||
|
{items.map((item, index) => (
|
||||||
|
<SortableItemRow
|
||||||
|
key={item._key}
|
||||||
|
item={item}
|
||||||
|
index={index}
|
||||||
|
currency={currency}
|
||||||
|
readOnly={readOnly}
|
||||||
|
canDelete={items.length > 1}
|
||||||
|
showIncludedInTotal={showIncludedInTotal}
|
||||||
|
itemDescriptionMaxLength={itemDescriptionMaxLength}
|
||||||
|
onUpdate={(field, value) => updateItem(index, field, value)}
|
||||||
|
onRemove={() => removeItem(index)}
|
||||||
|
/>
|
||||||
|
))}
|
||||||
|
</Box>
|
||||||
|
) : (
|
||||||
|
<TableContainer sx={{ overflowX: "auto" }}>
|
||||||
|
<Table
|
||||||
|
size="small"
|
||||||
|
sx={{
|
||||||
|
minWidth: 720,
|
||||||
|
"& td, & th": { borderColor: "divider" },
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<TableHead>
|
||||||
|
<TableRow>
|
||||||
|
<TableCell sx={{ width: "2rem" }} />
|
||||||
|
<TableCell sx={{ width: "2rem" }} align="center">
|
||||||
|
#
|
||||||
|
</TableCell>
|
||||||
|
<TableCell>Popis</TableCell>
|
||||||
|
<TableCell sx={{ width: "7rem" }} align="center">
|
||||||
|
Množství
|
||||||
|
</TableCell>
|
||||||
|
<TableCell sx={{ width: "5.5rem" }} align="center">
|
||||||
|
Jednotka
|
||||||
|
</TableCell>
|
||||||
|
<TableCell sx={{ width: "8rem" }} align="center">
|
||||||
|
Jedn. cena
|
||||||
|
</TableCell>
|
||||||
|
{showIncludedInTotal && (
|
||||||
|
<TableCell sx={{ width: "4rem" }} align="center">
|
||||||
|
V ceně
|
||||||
|
</TableCell>
|
||||||
|
)}
|
||||||
|
<TableCell sx={{ width: "8rem" }} align="right">
|
||||||
|
Celkem
|
||||||
|
</TableCell>
|
||||||
|
<TableCell sx={{ width: "2.5rem" }} />
|
||||||
|
</TableRow>
|
||||||
|
</TableHead>
|
||||||
|
<TableBody>
|
||||||
|
{items.map((item, index) => (
|
||||||
|
<SortableItemRow
|
||||||
|
key={item._key}
|
||||||
|
item={item}
|
||||||
|
index={index}
|
||||||
|
currency={currency}
|
||||||
|
readOnly={readOnly}
|
||||||
|
canDelete={items.length > 1}
|
||||||
|
showIncludedInTotal={showIncludedInTotal}
|
||||||
|
itemDescriptionMaxLength={itemDescriptionMaxLength}
|
||||||
|
onUpdate={(field, value) =>
|
||||||
|
updateItem(index, field, value)
|
||||||
|
}
|
||||||
|
onRemove={() => removeItem(index)}
|
||||||
|
/>
|
||||||
|
))}
|
||||||
|
</TableBody>
|
||||||
|
</Table>
|
||||||
|
</TableContainer>
|
||||||
|
)}
|
||||||
|
</SortableContext>
|
||||||
|
</DndContext>
|
||||||
|
|
||||||
|
{/* Totals (NET only — offers/issued orders are not tax documents) */}
|
||||||
|
<Box
|
||||||
|
sx={{
|
||||||
|
mt: 2,
|
||||||
|
ml: "auto",
|
||||||
|
maxWidth: 360,
|
||||||
|
display: "flex",
|
||||||
|
flexDirection: "column",
|
||||||
|
gap: 0.5,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Box
|
||||||
|
sx={{
|
||||||
|
display: "flex",
|
||||||
|
justifyContent: "space-between",
|
||||||
|
pt: 0.5,
|
||||||
|
mt: 0.5,
|
||||||
|
borderTop: 1,
|
||||||
|
borderColor: "divider",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Typography variant="body2" sx={{ fontWeight: 700 }}>
|
||||||
|
Celkem bez DPH:
|
||||||
|
</Typography>
|
||||||
|
<Typography
|
||||||
|
variant="body2"
|
||||||
|
sx={{
|
||||||
|
fontFamily: "'DM Mono', Menlo, monospace",
|
||||||
|
fontWeight: 700,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{formatCurrency(total, currency)}
|
||||||
|
</Typography>
|
||||||
|
</Box>
|
||||||
|
</Box>
|
||||||
|
</Card>
|
||||||
|
);
|
||||||
|
}
|
||||||
55
src/admin/components/document/LockBanner.tsx
Normal file
55
src/admin/components/document/LockBanner.tsx
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
import Box from "@mui/material/Box";
|
||||||
|
import type { DocumentLockInfo } from "../../hooks/useDocumentLock";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Warning banner shown when another user holds the document edit lock
|
||||||
|
* (extracted from OfferDetail; shared by the document detail pages).
|
||||||
|
* Renders nothing when the lock is free / held by the current user.
|
||||||
|
*
|
||||||
|
* `entityWord` is the Czech accusative for the document ("Nabídku" /
|
||||||
|
* "Objednávku") — both are feminine, so the rest of the sentence is shared.
|
||||||
|
*/
|
||||||
|
export default function LockBanner({
|
||||||
|
lockedBy,
|
||||||
|
entityWord,
|
||||||
|
}: {
|
||||||
|
lockedBy: DocumentLockInfo | null;
|
||||||
|
entityWord: string;
|
||||||
|
}) {
|
||||||
|
if (!lockedBy) return null;
|
||||||
|
return (
|
||||||
|
<Box
|
||||||
|
sx={{
|
||||||
|
bgcolor:
|
||||||
|
"color-mix(in srgb, var(--mui-palette-warning-main) 15%, transparent)",
|
||||||
|
border: 1,
|
||||||
|
borderColor: "warning.main",
|
||||||
|
borderRadius: 1,
|
||||||
|
px: 2,
|
||||||
|
py: 1.5,
|
||||||
|
display: "flex",
|
||||||
|
alignItems: "center",
|
||||||
|
gap: 1,
|
||||||
|
mb: 2,
|
||||||
|
fontSize: "0.875rem",
|
||||||
|
color: "warning.main",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<svg
|
||||||
|
width="18"
|
||||||
|
height="18"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
strokeWidth="2"
|
||||||
|
>
|
||||||
|
<rect x="3" y="11" width="18" height="11" rx="2" ry="2" />
|
||||||
|
<path d="M7 11V7a5 5 0 0 1 10 0v4" />
|
||||||
|
</svg>
|
||||||
|
<span>
|
||||||
|
{entityWord} právě upravuje <strong>{lockedBy.full_name}</strong>.
|
||||||
|
Můžete ji pouze prohlížet.
|
||||||
|
</span>
|
||||||
|
</Box>
|
||||||
|
);
|
||||||
|
}
|
||||||
315
src/admin/components/document/SectionsEditor.tsx
Normal file
315
src/admin/components/document/SectionsEditor.tsx
Normal file
@@ -0,0 +1,315 @@
|
|||||||
|
import type { ReactNode } from "react";
|
||||||
|
import Box from "@mui/material/Box";
|
||||||
|
import Typography from "@mui/material/Typography";
|
||||||
|
import IconButton from "@mui/material/IconButton";
|
||||||
|
import RichEditor from "../RichEditor";
|
||||||
|
import {
|
||||||
|
Button,
|
||||||
|
Card,
|
||||||
|
TextField,
|
||||||
|
Field,
|
||||||
|
StatusChip,
|
||||||
|
EmptyState,
|
||||||
|
} from "../../ui";
|
||||||
|
|
||||||
|
/** One bilingual rich-text PDF section (offers' scope ≡ issued orders' sections). */
|
||||||
|
export interface DocumentSection {
|
||||||
|
title: string;
|
||||||
|
title_cz: string;
|
||||||
|
content: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const emptyDocumentSection = (): DocumentSection => ({
|
||||||
|
title: "",
|
||||||
|
title_cz: "",
|
||||||
|
content: "",
|
||||||
|
});
|
||||||
|
|
||||||
|
const RemoveIcon = (
|
||||||
|
<svg
|
||||||
|
width="16"
|
||||||
|
height="16"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
strokeWidth="2"
|
||||||
|
>
|
||||||
|
<line x1="18" y1="6" x2="6" y2="18" />
|
||||||
|
<line x1="6" y1="6" x2="18" y2="18" />
|
||||||
|
</svg>
|
||||||
|
);
|
||||||
|
|
||||||
|
const UpIcon = (
|
||||||
|
<svg
|
||||||
|
width="16"
|
||||||
|
height="16"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
strokeWidth="2"
|
||||||
|
>
|
||||||
|
<polyline points="18 15 12 9 6 15" />
|
||||||
|
</svg>
|
||||||
|
);
|
||||||
|
|
||||||
|
const DownIcon = (
|
||||||
|
<svg
|
||||||
|
width="16"
|
||||||
|
height="16"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
strokeWidth="2"
|
||||||
|
>
|
||||||
|
<polyline points="6 9 12 15 18 9" />
|
||||||
|
</svg>
|
||||||
|
);
|
||||||
|
|
||||||
|
interface SectionsEditorProps {
|
||||||
|
sections: DocumentSection[];
|
||||||
|
onChange: (sections: DocumentSection[]) => void;
|
||||||
|
readOnly: boolean;
|
||||||
|
/** Card heading. */
|
||||||
|
title?: string;
|
||||||
|
/**
|
||||||
|
* Document language — "CZ"/"cs" prefers the Czech section title in the
|
||||||
|
* "Sekce N — …" header line, anything else the English one.
|
||||||
|
*/
|
||||||
|
language?: string;
|
||||||
|
/** Optional page-specific control (e.g. scope-template Select) next to the add button. */
|
||||||
|
templatesSlot?: ReactNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bilingual rich-text sections editor shared by the document detail pages
|
||||||
|
* (lifted from OfferDetail's "Rozsah projektu" card): bordered section boxes
|
||||||
|
* with EN/CZ title fields (StatusChip badges), Quill content, up/down
|
||||||
|
* reorder and add/remove. Offers pass their template Select via
|
||||||
|
* `templatesSlot`; issued orders don't (no templates on POs).
|
||||||
|
*/
|
||||||
|
export default function SectionsEditor({
|
||||||
|
sections,
|
||||||
|
onChange,
|
||||||
|
readOnly,
|
||||||
|
title = "Rozsah projektu",
|
||||||
|
language,
|
||||||
|
templatesSlot,
|
||||||
|
}: SectionsEditorProps) {
|
||||||
|
const preferCz =
|
||||||
|
(language ?? "").toLowerCase().startsWith("cs") ||
|
||||||
|
(language ?? "").toUpperCase() === "CZ";
|
||||||
|
|
||||||
|
const updateSection = (
|
||||||
|
index: number,
|
||||||
|
field: keyof DocumentSection,
|
||||||
|
value: string,
|
||||||
|
) =>
|
||||||
|
onChange(
|
||||||
|
sections.map((s, i) => (i === index ? { ...s, [field]: value } : s)),
|
||||||
|
);
|
||||||
|
|
||||||
|
const moveSection = (index: number, delta: -1 | 1) => {
|
||||||
|
const target = index + delta;
|
||||||
|
if (target < 0 || target >= sections.length) return;
|
||||||
|
const arr = [...sections];
|
||||||
|
[arr[index], arr[target]] = [arr[target], arr[index]];
|
||||||
|
onChange(arr);
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Card sx={{ mb: 3 }}>
|
||||||
|
<Box
|
||||||
|
sx={{
|
||||||
|
display: "flex",
|
||||||
|
justifyContent: "space-between",
|
||||||
|
alignItems: "center",
|
||||||
|
mb: 2,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Typography variant="subtitle2" sx={{ fontWeight: 600 }}>
|
||||||
|
{title}
|
||||||
|
</Typography>
|
||||||
|
{!readOnly && (
|
||||||
|
<Box sx={{ display: "flex", gap: 1, alignItems: "center" }}>
|
||||||
|
{templatesSlot}
|
||||||
|
<Button
|
||||||
|
type="button"
|
||||||
|
onClick={() => onChange([...sections, emptyDocumentSection()])}
|
||||||
|
variant="outlined"
|
||||||
|
color="inherit"
|
||||||
|
size="small"
|
||||||
|
>
|
||||||
|
+ Přidat sekci
|
||||||
|
</Button>
|
||||||
|
</Box>
|
||||||
|
)}
|
||||||
|
</Box>
|
||||||
|
|
||||||
|
{sections.length === 0 ? (
|
||||||
|
<EmptyState
|
||||||
|
title={
|
||||||
|
templatesSlot
|
||||||
|
? 'Žádné sekce rozsahu. Klikněte na "Přidat sekci" nebo vyberte šablonu.'
|
||||||
|
: 'Žádné sekce rozsahu. Klikněte na "Přidat sekci".'
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
) : (
|
||||||
|
<Box
|
||||||
|
sx={{
|
||||||
|
display: "flex",
|
||||||
|
flexDirection: "column",
|
||||||
|
gap: 3,
|
||||||
|
mt: 1,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{sections.map((section, idx) => (
|
||||||
|
<Box
|
||||||
|
key={idx}
|
||||||
|
sx={{
|
||||||
|
border: 1,
|
||||||
|
borderColor: "divider",
|
||||||
|
borderRadius: 2,
|
||||||
|
p: 2,
|
||||||
|
bgcolor: "action.hover",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Box
|
||||||
|
sx={{
|
||||||
|
display: "flex",
|
||||||
|
justifyContent: "space-between",
|
||||||
|
alignItems: "center",
|
||||||
|
mb: 1.5,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Typography
|
||||||
|
variant="body2"
|
||||||
|
sx={{ fontWeight: 600, color: "text.secondary" }}
|
||||||
|
>
|
||||||
|
Sekce {idx + 1}
|
||||||
|
{(preferCz ? section.title_cz : section.title) && (
|
||||||
|
<Box component="span" sx={{ fontWeight: 400, ml: 1 }}>
|
||||||
|
—{" "}
|
||||||
|
{preferCz
|
||||||
|
? section.title_cz || section.title
|
||||||
|
: section.title}
|
||||||
|
</Box>
|
||||||
|
)}
|
||||||
|
</Typography>
|
||||||
|
{!readOnly && (
|
||||||
|
<Box sx={{ display: "flex", gap: 0.25 }}>
|
||||||
|
{idx > 0 && (
|
||||||
|
<IconButton
|
||||||
|
size="small"
|
||||||
|
onClick={() => moveSection(idx, -1)}
|
||||||
|
title="Posunout nahoru"
|
||||||
|
aria-label="Posunout nahoru"
|
||||||
|
>
|
||||||
|
{UpIcon}
|
||||||
|
</IconButton>
|
||||||
|
)}
|
||||||
|
{idx < sections.length - 1 && (
|
||||||
|
<IconButton
|
||||||
|
size="small"
|
||||||
|
onClick={() => moveSection(idx, 1)}
|
||||||
|
title="Posunout dolů"
|
||||||
|
aria-label="Posunout dolů"
|
||||||
|
>
|
||||||
|
{DownIcon}
|
||||||
|
</IconButton>
|
||||||
|
)}
|
||||||
|
<IconButton
|
||||||
|
size="small"
|
||||||
|
color="error"
|
||||||
|
onClick={() =>
|
||||||
|
onChange(sections.filter((_, i) => i !== idx))
|
||||||
|
}
|
||||||
|
title="Odebrat sekci"
|
||||||
|
aria-label="Odebrat sekci"
|
||||||
|
>
|
||||||
|
{RemoveIcon}
|
||||||
|
</IconButton>
|
||||||
|
</Box>
|
||||||
|
)}
|
||||||
|
</Box>
|
||||||
|
|
||||||
|
<Box
|
||||||
|
sx={{
|
||||||
|
display: "grid",
|
||||||
|
gridTemplateColumns: { xs: "1fr", sm: "1fr 1fr" },
|
||||||
|
gap: 2,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Box sx={{ mb: 2 }}>
|
||||||
|
<Box
|
||||||
|
sx={{
|
||||||
|
display: "flex",
|
||||||
|
alignItems: "center",
|
||||||
|
gap: 0.75,
|
||||||
|
mb: 0.5,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<StatusChip label="EN" color="info" />
|
||||||
|
<Typography
|
||||||
|
component="label"
|
||||||
|
variant="body2"
|
||||||
|
sx={{ fontWeight: 600, color: "text.secondary" }}
|
||||||
|
>
|
||||||
|
Název sekce
|
||||||
|
</Typography>
|
||||||
|
</Box>
|
||||||
|
<TextField
|
||||||
|
value={section.title}
|
||||||
|
onChange={(e) =>
|
||||||
|
updateSection(idx, "title", e.target.value)
|
||||||
|
}
|
||||||
|
placeholder="Název sekce (anglicky)"
|
||||||
|
InputProps={{ readOnly }}
|
||||||
|
slotProps={{ htmlInput: { maxLength: 500 } }}
|
||||||
|
/>
|
||||||
|
</Box>
|
||||||
|
<Box sx={{ mb: 2 }}>
|
||||||
|
<Box
|
||||||
|
sx={{
|
||||||
|
display: "flex",
|
||||||
|
alignItems: "center",
|
||||||
|
gap: 0.75,
|
||||||
|
mb: 0.5,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<StatusChip label="CZ" color="success" />
|
||||||
|
<Typography
|
||||||
|
component="label"
|
||||||
|
variant="body2"
|
||||||
|
sx={{ fontWeight: 600, color: "text.secondary" }}
|
||||||
|
>
|
||||||
|
Název sekce
|
||||||
|
</Typography>
|
||||||
|
</Box>
|
||||||
|
<TextField
|
||||||
|
value={section.title_cz}
|
||||||
|
onChange={(e) =>
|
||||||
|
updateSection(idx, "title_cz", e.target.value)
|
||||||
|
}
|
||||||
|
placeholder="Název sekce (česky)"
|
||||||
|
InputProps={{ readOnly }}
|
||||||
|
slotProps={{ htmlInput: { maxLength: 500 } }}
|
||||||
|
/>
|
||||||
|
</Box>
|
||||||
|
</Box>
|
||||||
|
|
||||||
|
<Field label="Obsah">
|
||||||
|
<RichEditor
|
||||||
|
value={section.content}
|
||||||
|
onChange={(val) => updateSection(idx, "content", val)}
|
||||||
|
placeholder="Obsah sekce..."
|
||||||
|
minHeight="120px"
|
||||||
|
readOnly={readOnly}
|
||||||
|
/>
|
||||||
|
</Field>
|
||||||
|
</Box>
|
||||||
|
))}
|
||||||
|
</Box>
|
||||||
|
)}
|
||||||
|
</Card>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -9,6 +9,7 @@ import {
|
|||||||
type ReactNode,
|
type ReactNode,
|
||||||
} from "react";
|
} from "react";
|
||||||
import { setSessionExpired, setTokenGetter, setRefreshFn } from "../utils/api";
|
import { setSessionExpired, setTokenGetter, setRefreshFn } from "../utils/api";
|
||||||
|
import { queryClient } from "../lib/queryClient";
|
||||||
|
|
||||||
const API_BASE = "/api/admin";
|
const API_BASE = "/api/admin";
|
||||||
|
|
||||||
@@ -232,6 +233,10 @@ export function AuthProvider({ children }: { children: ReactNode }) {
|
|||||||
remember,
|
remember,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
// Query keys are user-agnostic (["dashboard"], ["attendance"], …),
|
||||||
|
// so anything cached from a previously logged-in user would be
|
||||||
|
// served to this one — drop the whole cache on every login.
|
||||||
|
queryClient.clear();
|
||||||
setAccessTokenFn(data.data.access_token, data.data.expires_in);
|
setAccessTokenFn(data.data.access_token, data.data.expires_in);
|
||||||
setUser(mapUser(data.data.user));
|
setUser(mapUser(data.data.user));
|
||||||
cachedUserRef.current = mapUser(data.data.user);
|
cachedUserRef.current = mapUser(data.data.user);
|
||||||
@@ -288,6 +293,9 @@ export function AuthProvider({ children }: { children: ReactNode }) {
|
|||||||
);
|
);
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
if (data.success) {
|
if (data.success) {
|
||||||
|
// Same reason as in login(): the cache may hold the previous
|
||||||
|
// user's data under user-agnostic keys.
|
||||||
|
queryClient.clear();
|
||||||
setAccessTokenFn(data.data.access_token, data.data.expires_in);
|
setAccessTokenFn(data.data.access_token, data.data.expires_in);
|
||||||
setUser(mapUser(data.data.user));
|
setUser(mapUser(data.data.user));
|
||||||
cachedUserRef.current = mapUser(data.data.user);
|
cachedUserRef.current = mapUser(data.data.user);
|
||||||
@@ -327,6 +335,10 @@ export function AuthProvider({ children }: { children: ReactNode }) {
|
|||||||
clearTimeout(refreshTimeoutRef.current);
|
clearTimeout(refreshTimeoutRef.current);
|
||||||
refreshTimeoutRef.current = null;
|
refreshTimeoutRef.current = null;
|
||||||
}
|
}
|
||||||
|
// The React Query cache outlives the session — without this, the next
|
||||||
|
// user to log in on this browser sees the previous user's cached
|
||||||
|
// dashboards/lists until each query refetches.
|
||||||
|
queryClient.clear();
|
||||||
}
|
}
|
||||||
}, [getAccessTokenFn]);
|
}, [getAccessTokenFn]);
|
||||||
|
|
||||||
|
|||||||
@@ -1048,6 +1048,10 @@ export default function useAttendanceAdmin({ alert }: AlertContext) {
|
|||||||
if (result.success) {
|
if (result.success) {
|
||||||
setShowCreateModal(false);
|
setShowCreateModal(false);
|
||||||
queryClient.invalidateQueries({ queryKey: ["attendance"] });
|
queryClient.invalidateQueries({ queryKey: ["attendance"] });
|
||||||
|
// The dashboard embeds attendance (Přítomní dnes / Docházka dnes /
|
||||||
|
// punch-button state) — without this it serves a stale cache for up
|
||||||
|
// to its staleTime after records change here.
|
||||||
|
queryClient.invalidateQueries({ queryKey: ["dashboard"] });
|
||||||
await fetchData(false);
|
await fetchData(false);
|
||||||
await new Promise((resolve) => setTimeout(resolve, 300));
|
await new Promise((resolve) => setTimeout(resolve, 300));
|
||||||
alert.success(
|
alert.success(
|
||||||
@@ -1120,6 +1124,10 @@ export default function useAttendanceAdmin({ alert }: AlertContext) {
|
|||||||
if (result.success) {
|
if (result.success) {
|
||||||
setShowBulkModal(false);
|
setShowBulkModal(false);
|
||||||
queryClient.invalidateQueries({ queryKey: ["attendance"] });
|
queryClient.invalidateQueries({ queryKey: ["attendance"] });
|
||||||
|
// The dashboard embeds attendance (Přítomní dnes / Docházka dnes /
|
||||||
|
// punch-button state) — without this it serves a stale cache for up
|
||||||
|
// to its staleTime after records change here.
|
||||||
|
queryClient.invalidateQueries({ queryKey: ["dashboard"] });
|
||||||
await fetchData(false);
|
await fetchData(false);
|
||||||
await new Promise((resolve) => setTimeout(resolve, 300));
|
await new Promise((resolve) => setTimeout(resolve, 300));
|
||||||
alert.success(
|
alert.success(
|
||||||
@@ -1255,6 +1263,10 @@ export default function useAttendanceAdmin({ alert }: AlertContext) {
|
|||||||
if (result.success) {
|
if (result.success) {
|
||||||
setShowEditModal(false);
|
setShowEditModal(false);
|
||||||
queryClient.invalidateQueries({ queryKey: ["attendance"] });
|
queryClient.invalidateQueries({ queryKey: ["attendance"] });
|
||||||
|
// The dashboard embeds attendance (Přítomní dnes / Docházka dnes /
|
||||||
|
// punch-button state) — without this it serves a stale cache for up
|
||||||
|
// to its staleTime after records change here.
|
||||||
|
queryClient.invalidateQueries({ queryKey: ["dashboard"] });
|
||||||
await fetchData(false);
|
await fetchData(false);
|
||||||
await new Promise((resolve) => setTimeout(resolve, 300));
|
await new Promise((resolve) => setTimeout(resolve, 300));
|
||||||
alert.success(
|
alert.success(
|
||||||
@@ -1285,6 +1297,10 @@ export default function useAttendanceAdmin({ alert }: AlertContext) {
|
|||||||
if (result.success) {
|
if (result.success) {
|
||||||
setDeleteConfirm({ show: false, record: null });
|
setDeleteConfirm({ show: false, record: null });
|
||||||
queryClient.invalidateQueries({ queryKey: ["attendance"] });
|
queryClient.invalidateQueries({ queryKey: ["attendance"] });
|
||||||
|
// The dashboard embeds attendance (Přítomní dnes / Docházka dnes /
|
||||||
|
// punch-button state) — without this it serves a stale cache for up
|
||||||
|
// to its staleTime after records change here.
|
||||||
|
queryClient.invalidateQueries({ queryKey: ["dashboard"] });
|
||||||
await fetchData(false);
|
await fetchData(false);
|
||||||
alert.success(
|
alert.success(
|
||||||
result.message || result.data?.message || "Záznam smazán",
|
result.message || result.data?.message || "Záznam smazán",
|
||||||
|
|||||||
81
src/admin/hooks/useDocumentLock.ts
Normal file
81
src/admin/hooks/useDocumentLock.ts
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
import { useCallback, useEffect, useRef, useState } from "react";
|
||||||
|
import apiFetch from "../utils/api";
|
||||||
|
|
||||||
|
/** Lock holder as enriched by the detail endpoints (offers, issued orders). */
|
||||||
|
export interface DocumentLockInfo {
|
||||||
|
user_id: number;
|
||||||
|
username: string;
|
||||||
|
full_name: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Client side of the document edit lock shared by the offer / issued-order
|
||||||
|
* detail pages (extracted from OfferDetail). The server exposes the same
|
||||||
|
* trio under the entity base URL: POST `:base/lock`, `:base/heartbeat`,
|
||||||
|
* `:base/unlock` (lock expires after 30 s without a heartbeat — 3 missed
|
||||||
|
* 10 s beats, so background-tab interval throttling can't hand the lock away).
|
||||||
|
*
|
||||||
|
* Responsibilities:
|
||||||
|
* - `lockedBy` state — the page hydrates it from the detail response via
|
||||||
|
* `setLockedBy(detail.locked_by ?? null)`.
|
||||||
|
* - `acquire()` — fire-and-forget lock acquisition; the page calls it from
|
||||||
|
* its one-shot hydration effect when the doc is editable, the user holds
|
||||||
|
* the edit permission and nobody else holds the lock.
|
||||||
|
* - 10 s heartbeat while `enabled` (and not locked by another user), with
|
||||||
|
* hard-401 self-stop: a 401 here means the access token expired AND the
|
||||||
|
* refresh failed (apiFetch retries recoverable 401s itself), so we stop
|
||||||
|
* the interval instead of spamming a dead session every 10 s.
|
||||||
|
* - unlock on unmount/disable, with an AbortController so a stale in-flight
|
||||||
|
* unlock can be cancelled by the next one.
|
||||||
|
*/
|
||||||
|
export function useDocumentLock({
|
||||||
|
baseUrl,
|
||||||
|
enabled,
|
||||||
|
}: {
|
||||||
|
/** Entity endpoint base (e.g. `/api/admin/offers/42`); null in create mode. */
|
||||||
|
baseUrl: string | null;
|
||||||
|
/**
|
||||||
|
* External conditions for holding the lock: edit mode + editable status +
|
||||||
|
* edit permission. The hook adds `!isLockedByOther` itself.
|
||||||
|
*/
|
||||||
|
enabled: boolean;
|
||||||
|
}) {
|
||||||
|
const [lockedBy, setLockedBy] = useState<DocumentLockInfo | null>(null);
|
||||||
|
const heartbeatRef = useRef<ReturnType<typeof setInterval> | null>(null);
|
||||||
|
const unlockAbortRef = useRef<AbortController | null>(null);
|
||||||
|
const isLockedByOther = lockedBy !== null;
|
||||||
|
|
||||||
|
const acquire = useCallback(() => {
|
||||||
|
if (!baseUrl) return;
|
||||||
|
apiFetch(`${baseUrl}/lock`, { method: "POST" }).catch(() => {});
|
||||||
|
}, [baseUrl]);
|
||||||
|
|
||||||
|
// Heartbeat to keep the lock alive + release on unmount/disable.
|
||||||
|
useEffect(() => {
|
||||||
|
if (!baseUrl || !enabled || isLockedByOther) return;
|
||||||
|
|
||||||
|
heartbeatRef.current = setInterval(() => {
|
||||||
|
apiFetch(`${baseUrl}/heartbeat`, { method: "POST" })
|
||||||
|
.then((res) => {
|
||||||
|
if (res.status === 401 && heartbeatRef.current) {
|
||||||
|
clearInterval(heartbeatRef.current);
|
||||||
|
heartbeatRef.current = null;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(() => {});
|
||||||
|
}, 10 * 1000); // every 10 seconds
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
if (heartbeatRef.current) clearInterval(heartbeatRef.current);
|
||||||
|
if (unlockAbortRef.current) unlockAbortRef.current.abort();
|
||||||
|
const controller = new AbortController();
|
||||||
|
unlockAbortRef.current = controller;
|
||||||
|
apiFetch(`${baseUrl}/unlock`, {
|
||||||
|
method: "POST",
|
||||||
|
signal: controller.signal,
|
||||||
|
}).catch(() => {});
|
||||||
|
};
|
||||||
|
}, [baseUrl, enabled, isLockedByOther]);
|
||||||
|
|
||||||
|
return { lockedBy, isLockedByOther, setLockedBy, acquire };
|
||||||
|
}
|
||||||
86
src/admin/hooks/useDocumentPdf.ts
Normal file
86
src/admin/hooks/useDocumentPdf.ts
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
import { useCallback, useEffect, useRef, useState } from "react";
|
||||||
|
import apiFetch from "../utils/api";
|
||||||
|
import { useAlert } from "../context/AlertContext";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shared "Zobrazit PDF" handlers for the document pages (offers, issued
|
||||||
|
* orders) — both call their GET `…/:id/file` endpoint (archived NAS PDF with
|
||||||
|
* live-render fallback). Hardened flow extracted from OfferDetail:
|
||||||
|
* - pre-opens the tab synchronously (popup blockers only allow it inside the
|
||||||
|
* click's call stack), then streams the blob URL into it;
|
||||||
|
* - double-click guard via a ref (state updates are async, a ref is not);
|
||||||
|
* - 401 → silently close the tab (apiFetch already flagged the dead session);
|
||||||
|
* - blob URLs are revoked after 60 s; pending revoke timeouts are cleared on
|
||||||
|
* unmount so they can't fire into a dead page.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List variant — ONE hook instance serves every row. `openPdf(id, url)`
|
||||||
|
* fetches that row's PDF; `pdfLoadingId` reports which row is in flight (use
|
||||||
|
* it for the per-row spinner + disabled state).
|
||||||
|
*/
|
||||||
|
export function useDocumentListPdf() {
|
||||||
|
const alert = useAlert();
|
||||||
|
const [pdfLoadingId, setPdfLoadingId] = useState<number | null>(null);
|
||||||
|
const loadingRef = useRef(false);
|
||||||
|
const blobTimeoutsRef = useRef<ReturnType<typeof setTimeout>[]>([]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
// The array object is stable (we only push to it, never reassign), so
|
||||||
|
// capturing it here keeps the cleanup in sync with the ref's content.
|
||||||
|
const timeouts = blobTimeoutsRef.current;
|
||||||
|
return () => {
|
||||||
|
timeouts.forEach(clearTimeout);
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const openPdf = useCallback(
|
||||||
|
async (id: number, url: string) => {
|
||||||
|
if (loadingRef.current) return;
|
||||||
|
const newWindow = window.open("", "_blank");
|
||||||
|
loadingRef.current = true;
|
||||||
|
setPdfLoadingId(id);
|
||||||
|
try {
|
||||||
|
const response = await apiFetch(url);
|
||||||
|
if (response.status === 401) {
|
||||||
|
newWindow?.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!response.ok) {
|
||||||
|
newWindow?.close();
|
||||||
|
alert.error("Nepodařilo se vygenerovat PDF");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const blob = await response.blob();
|
||||||
|
const blobUrl = URL.createObjectURL(blob);
|
||||||
|
if (newWindow) newWindow.location.href = blobUrl;
|
||||||
|
const timeoutId = setTimeout(() => URL.revokeObjectURL(blobUrl), 60000);
|
||||||
|
blobTimeoutsRef.current.push(timeoutId);
|
||||||
|
} catch {
|
||||||
|
newWindow?.close();
|
||||||
|
alert.error("Chyba připojení");
|
||||||
|
} finally {
|
||||||
|
loadingRef.current = false;
|
||||||
|
setPdfLoadingId(null);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[alert],
|
||||||
|
);
|
||||||
|
|
||||||
|
return { openPdf, pdfLoadingId };
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Detail variant — a single fixed URL, `openPdf()` takes no arguments.
|
||||||
|
* Implemented on top of the list variant so the hardened flow lives once.
|
||||||
|
*/
|
||||||
|
export function useDocumentPdf(url: string | null) {
|
||||||
|
const { openPdf: openListPdf, pdfLoadingId } = useDocumentListPdf();
|
||||||
|
|
||||||
|
const openPdf = useCallback(async () => {
|
||||||
|
if (!url) return;
|
||||||
|
await openListPdf(0, url);
|
||||||
|
}, [url, openListPdf]);
|
||||||
|
|
||||||
|
return { openPdf, pdfLoading: pdfLoadingId !== null };
|
||||||
|
}
|
||||||
41
src/admin/hooks/useUnsavedChangesGuard.ts
Normal file
41
src/admin/hooks/useUnsavedChangesGuard.ts
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
import { useCallback, useEffect, useRef } from "react";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dirty-form guard shared by the document detail pages (extracted from
|
||||||
|
* OfferDetail). Serializes `snapshot` (typically `{ form, items, sections }`)
|
||||||
|
* every render, captures a baseline on the first `ready` render — or
|
||||||
|
* explicitly via `markClean` — and registers a `beforeunload` warning while
|
||||||
|
* the current snapshot differs from the baseline.
|
||||||
|
*
|
||||||
|
* `markClean(value?)`:
|
||||||
|
* - with a value: baseline = that value (use from a hydration effect, where
|
||||||
|
* the freshly-mapped objects are ahead of the not-yet-rendered state);
|
||||||
|
* - without: baseline = the last RENDERED snapshot (use after a successful
|
||||||
|
* save, where the handler's closure state equals the rendered state).
|
||||||
|
*/
|
||||||
|
export function useUnsavedChangesGuard(snapshot: unknown, ready: boolean) {
|
||||||
|
const serialized = JSON.stringify(snapshot);
|
||||||
|
const serializedRef = useRef(serialized);
|
||||||
|
serializedRef.current = serialized;
|
||||||
|
const baselineRef = useRef<string | null>(null);
|
||||||
|
if (ready && baselineRef.current === null) baselineRef.current = serialized;
|
||||||
|
const isDirty =
|
||||||
|
baselineRef.current !== null && serialized !== baselineRef.current;
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!isDirty) return;
|
||||||
|
const handler = (e: BeforeUnloadEvent) => {
|
||||||
|
e.preventDefault();
|
||||||
|
e.returnValue = "";
|
||||||
|
};
|
||||||
|
window.addEventListener("beforeunload", handler);
|
||||||
|
return () => window.removeEventListener("beforeunload", handler);
|
||||||
|
}, [isDirty]);
|
||||||
|
|
||||||
|
const markClean = useCallback((value?: unknown) => {
|
||||||
|
baselineRef.current =
|
||||||
|
value !== undefined ? JSON.stringify(value) : serializedRef.current;
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return { isDirty, markClean };
|
||||||
|
}
|
||||||
@@ -6,6 +6,11 @@ export const dashboardOptions = () =>
|
|||||||
queryKey: ["dashboard"],
|
queryKey: ["dashboard"],
|
||||||
queryFn: () => jsonQuery<Record<string, unknown>>("/api/admin/dashboard"),
|
queryFn: () => jsonQuery<Record<string, unknown>>("/api/admin/dashboard"),
|
||||||
staleTime: 60_000,
|
staleTime: 60_000,
|
||||||
|
// The dashboard aggregates MANY domains (attendance, offers, invoices,
|
||||||
|
// orders, projects, leave). Mutations in those domains can't all be
|
||||||
|
// expected to invalidate ["dashboard"], so always refetch on mount —
|
||||||
|
// navigating back to the dashboard must never show pre-mutation data.
|
||||||
|
refetchOnMount: "always",
|
||||||
});
|
});
|
||||||
|
|
||||||
// require2FAOptions lives in ./settings.ts (the single definition consumers
|
// require2FAOptions lives in ./settings.ts (the single definition consumers
|
||||||
|
|||||||
@@ -43,6 +43,14 @@ export interface InvoiceItem {
|
|||||||
position?: number;
|
position?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface InvoiceSection {
|
||||||
|
id?: number;
|
||||||
|
title: string | null;
|
||||||
|
title_cz: string | null;
|
||||||
|
content: string | null;
|
||||||
|
position?: number | null;
|
||||||
|
}
|
||||||
|
|
||||||
export interface InvoiceDetail {
|
export interface InvoiceDetail {
|
||||||
id: number;
|
id: number;
|
||||||
invoice_number: string;
|
invoice_number: string;
|
||||||
@@ -62,7 +70,7 @@ export interface InvoiceDetail {
|
|||||||
vat_rate: number;
|
vat_rate: number;
|
||||||
apply_vat: boolean;
|
apply_vat: boolean;
|
||||||
exchange_rate: string;
|
exchange_rate: string;
|
||||||
notes?: string;
|
internal_notes?: string | null;
|
||||||
payment_method?: string;
|
payment_method?: string;
|
||||||
variable_symbol?: string;
|
variable_symbol?: string;
|
||||||
constant_symbol?: string;
|
constant_symbol?: string;
|
||||||
@@ -75,6 +83,7 @@ export interface InvoiceDetail {
|
|||||||
bank_account?: string;
|
bank_account?: string;
|
||||||
bank_account_id?: number | null;
|
bank_account_id?: number | null;
|
||||||
items?: InvoiceItem[];
|
items?: InvoiceItem[];
|
||||||
|
sections?: InvoiceSection[];
|
||||||
subtotal: number;
|
subtotal: number;
|
||||||
vat_amount: number;
|
vat_amount: number;
|
||||||
total: number;
|
total: number;
|
||||||
|
|||||||
@@ -1,19 +1,35 @@
|
|||||||
import { queryOptions } from "@tanstack/react-query";
|
import { queryOptions } from "@tanstack/react-query";
|
||||||
import { jsonQuery, paginatedJsonQuery } from "../apiAdapter";
|
import { jsonQuery, paginatedJsonQuery } from "../apiAdapter";
|
||||||
|
// Single shared definition (type-only import, erased at compile time) — the
|
||||||
|
// canonical CurrencyAmount lives in offers.ts; re-exported here so existing
|
||||||
|
// importers keep working.
|
||||||
|
import type { CurrencyAmount } from "./offers";
|
||||||
|
|
||||||
|
export type { CurrencyAmount };
|
||||||
|
|
||||||
export interface IssuedOrder {
|
export interface IssuedOrder {
|
||||||
id: number;
|
id: number;
|
||||||
po_number: string | null;
|
po_number: string | null;
|
||||||
customer_id: number | null;
|
supplier_id: number | null;
|
||||||
customer_name: string | null;
|
supplier_name: string | null;
|
||||||
status: string;
|
status: string;
|
||||||
currency: string | null;
|
currency: string | null;
|
||||||
order_date: string | null;
|
order_date: string | null;
|
||||||
subtotal: number;
|
// NET total — issued orders carry no VAT (not tax documents).
|
||||||
vat_amount: number;
|
|
||||||
total: number;
|
total: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Active supplier row from the lightweight PO-picker lookup endpoint. */
|
||||||
|
export interface Supplier {
|
||||||
|
id: number;
|
||||||
|
name: string;
|
||||||
|
ico: string | null;
|
||||||
|
dic: string | null;
|
||||||
|
address: string | null;
|
||||||
|
email: string | null;
|
||||||
|
phone: string | null;
|
||||||
|
}
|
||||||
|
|
||||||
export interface IssuedOrderItem {
|
export interface IssuedOrderItem {
|
||||||
id?: number;
|
id?: number;
|
||||||
description: string | null;
|
description: string | null;
|
||||||
@@ -21,26 +37,45 @@ export interface IssuedOrderItem {
|
|||||||
quantity: number | string | null;
|
quantity: number | string | null;
|
||||||
unit: string | null;
|
unit: string | null;
|
||||||
unit_price: number | string | null;
|
unit_price: number | string | null;
|
||||||
vat_rate: number | string | null;
|
position?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Rich-text CZ/EN PDF section (mirrors offers' scope sections). */
|
||||||
|
export interface IssuedOrderSection {
|
||||||
|
id?: number;
|
||||||
|
title: string | null;
|
||||||
|
title_cz: string | null;
|
||||||
|
content: string | null;
|
||||||
position?: number;
|
position?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IssuedOrderDetail extends IssuedOrder {
|
export interface IssuedOrderDetail extends IssuedOrder {
|
||||||
apply_vat: boolean | null;
|
|
||||||
vat_rate: number | string | null;
|
|
||||||
exchange_rate: number | string | null;
|
exchange_rate: number | string | null;
|
||||||
delivery_date: string | null;
|
delivery_date: string | null;
|
||||||
language: string | null;
|
language: string | null;
|
||||||
delivery_terms: string | null;
|
order_text: string | null;
|
||||||
payment_terms: string | null;
|
|
||||||
issued_by: string | null;
|
|
||||||
notes: string | null;
|
|
||||||
internal_notes: string | null;
|
internal_notes: string | null;
|
||||||
items: IssuedOrderItem[];
|
items: IssuedOrderItem[];
|
||||||
customer: Record<string, unknown> | null;
|
sections: IssuedOrderSection[];
|
||||||
|
supplier: Record<string, unknown> | null;
|
||||||
valid_transitions: string[];
|
valid_transitions: string[];
|
||||||
|
/** Fresh edit lock held by ANOTHER user (same shape as offers), else null. */
|
||||||
|
locked_by: {
|
||||||
|
user_id: number;
|
||||||
|
username: string;
|
||||||
|
full_name: string;
|
||||||
|
} | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Suppliers lookup for the PO supplier picker — hits the issued-orders-scoped
|
||||||
|
// endpoint (orders permissions), NOT the warehouse.manage-guarded CRUD list.
|
||||||
|
export const issuedOrderSuppliersOptions = () =>
|
||||||
|
queryOptions({
|
||||||
|
queryKey: ["issued-orders", "suppliers"],
|
||||||
|
queryFn: () => jsonQuery<Supplier[]>("/api/admin/issued-orders/suppliers"),
|
||||||
|
staleTime: 2 * 60_000,
|
||||||
|
});
|
||||||
|
|
||||||
export const issuedOrderListOptions = (filters: {
|
export const issuedOrderListOptions = (filters: {
|
||||||
search?: string;
|
search?: string;
|
||||||
sort?: string;
|
sort?: string;
|
||||||
@@ -48,6 +83,7 @@ export const issuedOrderListOptions = (filters: {
|
|||||||
page?: number;
|
page?: number;
|
||||||
perPage?: number;
|
perPage?: number;
|
||||||
status?: string;
|
status?: string;
|
||||||
|
supplier_id?: number;
|
||||||
month?: number;
|
month?: number;
|
||||||
year?: number;
|
year?: number;
|
||||||
}) =>
|
}) =>
|
||||||
@@ -61,6 +97,8 @@ export const issuedOrderListOptions = (filters: {
|
|||||||
if (filters.page) params.set("page", String(filters.page));
|
if (filters.page) params.set("page", String(filters.page));
|
||||||
if (filters.perPage) params.set("per_page", String(filters.perPage));
|
if (filters.perPage) params.set("per_page", String(filters.perPage));
|
||||||
if (filters.status) params.set("status", filters.status);
|
if (filters.status) params.set("status", filters.status);
|
||||||
|
if (filters.supplier_id)
|
||||||
|
params.set("supplier_id", String(filters.supplier_id));
|
||||||
if (filters.month) params.set("month", String(filters.month));
|
if (filters.month) params.set("month", String(filters.month));
|
||||||
if (filters.year) params.set("year", String(filters.year));
|
if (filters.year) params.set("year", String(filters.year));
|
||||||
const qs = params.toString();
|
const qs = params.toString();
|
||||||
@@ -70,14 +108,10 @@ export const issuedOrderListOptions = (filters: {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
export interface CurrencyAmount {
|
|
||||||
amount: number;
|
|
||||||
currency: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const issuedOrderStatsOptions = (filters: {
|
export const issuedOrderStatsOptions = (filters: {
|
||||||
search?: string;
|
search?: string;
|
||||||
status?: string;
|
status?: string;
|
||||||
|
supplier_id?: number;
|
||||||
month?: number;
|
month?: number;
|
||||||
year?: number;
|
year?: number;
|
||||||
}) =>
|
}) =>
|
||||||
@@ -87,6 +121,8 @@ export const issuedOrderStatsOptions = (filters: {
|
|||||||
const params = new URLSearchParams();
|
const params = new URLSearchParams();
|
||||||
if (filters.search) params.set("search", filters.search);
|
if (filters.search) params.set("search", filters.search);
|
||||||
if (filters.status) params.set("status", filters.status);
|
if (filters.status) params.set("status", filters.status);
|
||||||
|
if (filters.supplier_id)
|
||||||
|
params.set("supplier_id", String(filters.supplier_id));
|
||||||
if (filters.month) params.set("month", String(filters.month));
|
if (filters.month) params.set("month", String(filters.month));
|
||||||
if (filters.year) params.set("year", String(filters.year));
|
if (filters.year) params.set("year", String(filters.year));
|
||||||
const qs = params.toString();
|
const qs = params.toString();
|
||||||
@@ -103,4 +139,16 @@ export const issuedOrderDetailOptions = (id: string | undefined) =>
|
|||||||
queryFn: () =>
|
queryFn: () =>
|
||||||
jsonQuery<IssuedOrderDetail>(`/api/admin/issued-orders/${id}`),
|
jsonQuery<IssuedOrderDetail>(`/api/admin/issued-orders/${id}`),
|
||||||
enabled: !!id,
|
enabled: !!id,
|
||||||
|
// 404s (deleted/missing orders) are not transient. Retrying just spams
|
||||||
|
// GETs and keeps the detail page on an infinite spinner.
|
||||||
|
retry: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
export const issuedOrderNextNumberOptions = () =>
|
||||||
|
queryOptions({
|
||||||
|
queryKey: ["issued-orders", "next-number"],
|
||||||
|
queryFn: () =>
|
||||||
|
jsonQuery<{ next_number?: string; number?: string }>(
|
||||||
|
"/api/admin/issued-orders/next-number",
|
||||||
|
).then((d) => d?.next_number || d?.number || ""),
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -9,6 +9,32 @@ export interface ApiError extends Error {
|
|||||||
status?: number;
|
status?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Full success envelope (`data` + server `message`) — the result shape of a
|
||||||
|
* mutation created with `envelope: true`. Used by the document detail pages
|
||||||
|
* (offers, issued orders) to pass the server's Czech success message through
|
||||||
|
* to the toast instead of a hardcoded client string.
|
||||||
|
*/
|
||||||
|
export interface ApiEnvelope<T> {
|
||||||
|
data?: T;
|
||||||
|
message?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Czech-safe error message for toasts: returns the server-provided message
|
||||||
|
* from an `ApiError` (those carry `.status` and a Czech `error` string), and
|
||||||
|
* falls back when the message is a client-generated English string — network
|
||||||
|
* failures ("Failed to fetch" has no status), "Invalid JSON response",
|
||||||
|
* "Unauthorized" (401) or the generic "Request failed (NNN)".
|
||||||
|
*/
|
||||||
|
export function apiErrorMessage(err: unknown, fallback: string): string {
|
||||||
|
const status = (err as ApiError | null | undefined)?.status;
|
||||||
|
const message = err instanceof Error ? err.message : "";
|
||||||
|
if (!message || status === undefined || status === 401) return fallback;
|
||||||
|
if (/^Request failed \(\d+\)$/.test(message)) return fallback;
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
export type HttpMethod = "POST" | "PUT" | "PATCH" | "DELETE";
|
export type HttpMethod = "POST" | "PUT" | "PATCH" | "DELETE";
|
||||||
|
|
||||||
interface ApiResponseBody<T> {
|
interface ApiResponseBody<T> {
|
||||||
@@ -22,6 +48,7 @@ async function performMutation<TIn, TOut>(opts: {
|
|||||||
url: string | ((input: TIn) => string);
|
url: string | ((input: TIn) => string);
|
||||||
method: HttpMethod | ((input: TIn) => HttpMethod);
|
method: HttpMethod | ((input: TIn) => HttpMethod);
|
||||||
body?: TIn;
|
body?: TIn;
|
||||||
|
envelope?: boolean;
|
||||||
}): Promise<TOut> {
|
}): Promise<TOut> {
|
||||||
const url =
|
const url =
|
||||||
typeof opts.url === "function" ? opts.url(opts.body as TIn) : opts.url;
|
typeof opts.url === "function" ? opts.url(opts.body as TIn) : opts.url;
|
||||||
@@ -59,6 +86,9 @@ async function performMutation<TIn, TOut>(opts: {
|
|||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (opts.envelope) {
|
||||||
|
return { data: result.data, message: result.message } as TOut;
|
||||||
|
}
|
||||||
return result.data as TOut;
|
return result.data as TOut;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,6 +97,12 @@ export interface ApiMutationOptions<TIn, TOut> {
|
|||||||
method: HttpMethod | ((input: TIn) => HttpMethod);
|
method: HttpMethod | ((input: TIn) => HttpMethod);
|
||||||
/** Query-key prefixes to invalidate on success. Broad per CLAUDE.md. */
|
/** Query-key prefixes to invalidate on success. Broad per CLAUDE.md. */
|
||||||
invalidate?: readonly string[];
|
invalidate?: readonly string[];
|
||||||
|
/**
|
||||||
|
* When true, the mutation resolves with the full `ApiEnvelope` (`{ data,
|
||||||
|
* message }`) instead of the bare `data` — declare `TOut` as
|
||||||
|
* `ApiEnvelope<...>` accordingly.
|
||||||
|
*/
|
||||||
|
envelope?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -89,12 +125,12 @@ export function useApiMutation<TIn, TOut>(
|
|||||||
opts: ApiMutationOptions<TIn, TOut> &
|
opts: ApiMutationOptions<TIn, TOut> &
|
||||||
Omit<UseMutationOptions<TOut, ApiError, TIn>, "mutationFn">,
|
Omit<UseMutationOptions<TOut, ApiError, TIn>, "mutationFn">,
|
||||||
) {
|
) {
|
||||||
const { url, method, invalidate, onSuccess, ...rest } = opts;
|
const { url, method, invalidate, envelope, onSuccess, ...rest } = opts;
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
|
|
||||||
return useMutation<TOut, ApiError, TIn, unknown>({
|
return useMutation<TOut, ApiError, TIn, unknown>({
|
||||||
mutationFn: (input: TIn) =>
|
mutationFn: (input: TIn) =>
|
||||||
performMutation<TIn, TOut>({ url, method, body: input }),
|
performMutation<TIn, TOut>({ url, method, body: input, envelope }),
|
||||||
...rest,
|
...rest,
|
||||||
onSuccess: (data, variables, onMutateResult, context) => {
|
onSuccess: (data, variables, onMutateResult, context) => {
|
||||||
if (invalidate) {
|
if (invalidate) {
|
||||||
|
|||||||
@@ -52,7 +52,9 @@ export interface Customer {
|
|||||||
|
|
||||||
export const offerCustomersOptions = () =>
|
export const offerCustomersOptions = () =>
|
||||||
queryOptions({
|
queryOptions({
|
||||||
queryKey: ["offer-customers"],
|
// Under the broad ["offers"] domain so offer-domain invalidations
|
||||||
|
// (customer CRUD, offer mutations) refresh the picker automatically.
|
||||||
|
queryKey: ["offers", "customers"],
|
||||||
queryFn: () => jsonQuery<Customer[]>("/api/admin/customers"),
|
queryFn: () => jsonQuery<Customer[]>("/api/admin/customers"),
|
||||||
staleTime: 2 * 60_000,
|
staleTime: 2 * 60_000,
|
||||||
});
|
});
|
||||||
@@ -70,6 +72,21 @@ export const scopeTemplatesOptions = () =>
|
|||||||
queryFn: () => jsonQuery<ScopeTemplate[]>("/api/admin/offers-templates"),
|
queryFn: () => jsonQuery<ScopeTemplate[]>("/api/admin/offers-templates"),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/** Offer list row (the shape returned by GET /api/admin/offers). */
|
||||||
|
export interface Quotation {
|
||||||
|
id: number;
|
||||||
|
quotation_number: string;
|
||||||
|
project_code: string;
|
||||||
|
customer_name: string;
|
||||||
|
created_at: string;
|
||||||
|
valid_until: string;
|
||||||
|
currency: string;
|
||||||
|
total: number;
|
||||||
|
status: string;
|
||||||
|
order_id?: number;
|
||||||
|
order_status?: string;
|
||||||
|
}
|
||||||
|
|
||||||
export const offerListOptions = (filters: {
|
export const offerListOptions = (filters: {
|
||||||
search?: string;
|
search?: string;
|
||||||
sort?: string;
|
sort?: string;
|
||||||
@@ -92,7 +109,9 @@ export const offerListOptions = (filters: {
|
|||||||
if (filters.customer_id)
|
if (filters.customer_id)
|
||||||
params.set("customer_id", String(filters.customer_id));
|
params.set("customer_id", String(filters.customer_id));
|
||||||
const qs = params.toString();
|
const qs = params.toString();
|
||||||
return paginatedJsonQuery(`/api/admin/offers${qs ? `?${qs}` : ""}`);
|
return paginatedJsonQuery<Quotation>(
|
||||||
|
`/api/admin/offers${qs ? `?${qs}` : ""}`,
|
||||||
|
);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -157,13 +176,13 @@ export interface OfferDetailData {
|
|||||||
valid_until: string;
|
valid_until: string;
|
||||||
currency: string;
|
currency: string;
|
||||||
language: string;
|
language: string;
|
||||||
vat_rate: number;
|
|
||||||
apply_vat: boolean;
|
|
||||||
items?: OfferItemData[];
|
items?: OfferItemData[];
|
||||||
sections?: OfferSectionData[];
|
sections?: OfferSectionData[];
|
||||||
status: string;
|
status: string;
|
||||||
order: OfferOrderInfo | null;
|
order: OfferOrderInfo | null;
|
||||||
locked_by: OfferLockInfo | null;
|
locked_by: OfferLockInfo | null;
|
||||||
|
/** Legal next statuses, computed server-side (same contract as issued orders). */
|
||||||
|
valid_transitions: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export const offerDetailOptions = (id: string | undefined) =>
|
export const offerDetailOptions = (id: string | undefined) =>
|
||||||
|
|||||||
@@ -43,8 +43,6 @@ export interface OrderData {
|
|||||||
status: string;
|
status: string;
|
||||||
notes: string;
|
notes: string;
|
||||||
attachment_name?: string;
|
attachment_name?: string;
|
||||||
apply_vat: number | boolean;
|
|
||||||
vat_rate: number;
|
|
||||||
language?: string;
|
language?: string;
|
||||||
items: OrderItem[];
|
items: OrderItem[];
|
||||||
sections: OrderSection[];
|
sections: OrderSection[];
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ export const projectListOptions = (filters: {
|
|||||||
order?: string;
|
order?: string;
|
||||||
page?: number;
|
page?: number;
|
||||||
perPage?: number;
|
perPage?: number;
|
||||||
|
status?: string;
|
||||||
}) =>
|
}) =>
|
||||||
queryOptions({
|
queryOptions({
|
||||||
queryKey: ["projects", "list", filters],
|
queryKey: ["projects", "list", filters],
|
||||||
@@ -50,6 +51,7 @@ export const projectListOptions = (filters: {
|
|||||||
if (filters.order) params.set("order", filters.order);
|
if (filters.order) params.set("order", filters.order);
|
||||||
if (filters.page) params.set("page", String(filters.page));
|
if (filters.page) params.set("page", String(filters.page));
|
||||||
if (filters.perPage) params.set("per_page", String(filters.perPage));
|
if (filters.perPage) params.set("per_page", String(filters.perPage));
|
||||||
|
if (filters.status) params.set("status", filters.status);
|
||||||
const qs = params.toString();
|
const qs = params.toString();
|
||||||
return paginatedJsonQuery<Project>(
|
return paginatedJsonQuery<Project>(
|
||||||
`/api/admin/projects${qs ? `?${qs}` : ""}`,
|
`/api/admin/projects${qs ? `?${qs}` : ""}`,
|
||||||
|
|||||||
@@ -274,7 +274,8 @@ export default function Attendance() {
|
|||||||
>({
|
>({
|
||||||
url: () => `${API_BASE}/attendance`,
|
url: () => `${API_BASE}/attendance`,
|
||||||
method: () => "POST",
|
method: () => "POST",
|
||||||
invalidate: ["attendance"],
|
// dashboard included: the punch-button state + presence cards live there.
|
||||||
|
invalidate: ["attendance", "dashboard"],
|
||||||
});
|
});
|
||||||
|
|
||||||
const notesMutation = useApiMutation<{ notes: string }, { message?: string }>(
|
const notesMutation = useApiMutation<{ notes: string }, { message?: string }>(
|
||||||
@@ -300,7 +301,8 @@ export default function Attendance() {
|
|||||||
>({
|
>({
|
||||||
url: () => `${API_BASE}/leave-requests`,
|
url: () => `${API_BASE}/leave-requests`,
|
||||||
method: () => "POST",
|
method: () => "POST",
|
||||||
invalidate: ["attendance", "leave-requests", "leave", "users"],
|
// dashboard included: approvers see the pending-requests KPI there.
|
||||||
|
invalidate: ["attendance", "leave-requests", "leave", "users", "dashboard"],
|
||||||
});
|
});
|
||||||
|
|
||||||
const [submitting, setSubmitting] = useState(false);
|
const [submitting, setSubmitting] = useState(false);
|
||||||
|
|||||||
@@ -74,7 +74,7 @@ export default function AttendanceCreate() {
|
|||||||
const createMutation = useApiMutation<CreatePayload, { message?: string }>({
|
const createMutation = useApiMutation<CreatePayload, { message?: string }>({
|
||||||
url: () => `${API_BASE}/attendance`,
|
url: () => `${API_BASE}/attendance`,
|
||||||
method: () => "POST",
|
method: () => "POST",
|
||||||
invalidate: ["attendance", "users"],
|
invalidate: ["attendance", "users", "dashboard"],
|
||||||
});
|
});
|
||||||
|
|
||||||
const [form, setForm] = useState<CreateForm>(() => {
|
const [form, setForm] = useState<CreateForm>(() => {
|
||||||
|
|||||||
@@ -24,11 +24,14 @@ import { useAlert } from "../context/AlertContext";
|
|||||||
import { useAuth } from "../context/AuthContext";
|
import { useAuth } from "../context/AuthContext";
|
||||||
import Forbidden from "../components/Forbidden";
|
import Forbidden from "../components/Forbidden";
|
||||||
import RichEditor from "../components/RichEditor";
|
import RichEditor from "../components/RichEditor";
|
||||||
|
import SectionsEditor, {
|
||||||
|
type DocumentSection,
|
||||||
|
} from "../components/document/SectionsEditor";
|
||||||
import {
|
import {
|
||||||
DndContext,
|
DndContext,
|
||||||
closestCenter,
|
closestCenter,
|
||||||
KeyboardSensor,
|
KeyboardSensor,
|
||||||
PointerSensor,
|
MouseSensor,
|
||||||
TouchSensor,
|
TouchSensor,
|
||||||
useSensor,
|
useSensor,
|
||||||
useSensors,
|
useSensors,
|
||||||
@@ -198,7 +201,7 @@ interface InvoiceForm {
|
|||||||
constant_symbol: string;
|
constant_symbol: string;
|
||||||
issued_by: string;
|
issued_by: string;
|
||||||
billing_text: string;
|
billing_text: string;
|
||||||
notes: string;
|
internal_notes: string;
|
||||||
language: string;
|
language: string;
|
||||||
bank_account_id: number | string;
|
bank_account_id: number | string;
|
||||||
bank_name: string;
|
bank_name: string;
|
||||||
@@ -275,7 +278,11 @@ function SortableInvoiceRow({
|
|||||||
{...listeners}
|
{...listeners}
|
||||||
title="Přetáhnout"
|
title="Přetáhnout"
|
||||||
aria-label="Přetáhnout"
|
aria-label="Přetáhnout"
|
||||||
sx={{ cursor: "grab", color: "text.secondary" }}
|
sx={{
|
||||||
|
cursor: "grab",
|
||||||
|
color: "text.secondary",
|
||||||
|
touchAction: "none",
|
||||||
|
}}
|
||||||
>
|
>
|
||||||
{DragIcon}
|
{DragIcon}
|
||||||
</IconButton>
|
</IconButton>
|
||||||
@@ -385,7 +392,7 @@ function SortableInvoiceRow({
|
|||||||
{...listeners}
|
{...listeners}
|
||||||
title="Přetáhnout"
|
title="Přetáhnout"
|
||||||
aria-label="Přetáhnout"
|
aria-label="Přetáhnout"
|
||||||
sx={{ cursor: "grab", color: "text.secondary" }}
|
sx={{ cursor: "grab", color: "text.secondary", touchAction: "none" }}
|
||||||
>
|
>
|
||||||
{DragIcon}
|
{DragIcon}
|
||||||
</IconButton>
|
</IconButton>
|
||||||
@@ -508,10 +515,16 @@ export default function InvoiceDetail() {
|
|||||||
[],
|
[],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// MouseSensor (NOT PointerSensor): PointerSensor also receives touch-derived
|
||||||
|
// pointer events and its 5px distance constraint fires before the
|
||||||
|
// TouchSensor's long-press delay — the browser then claims the gesture for
|
||||||
|
// scrolling (pointercancel) and the drag dies. Mouse handles desktop,
|
||||||
|
// TouchSensor handles touch via long-press; the drag handles additionally
|
||||||
|
// carry touch-action: none so the browser never starts a scroll from them.
|
||||||
const dndSensors = useSensors(
|
const dndSensors = useSensors(
|
||||||
useSensor(PointerSensor, { activationConstraint: { distance: 5 } }),
|
useSensor(MouseSensor, { activationConstraint: { distance: 5 } }),
|
||||||
useSensor(TouchSensor, {
|
useSensor(TouchSensor, {
|
||||||
activationConstraint: { delay: 200, tolerance: 5 },
|
activationConstraint: { delay: 300, tolerance: 8 },
|
||||||
}),
|
}),
|
||||||
useSensor(KeyboardSensor),
|
useSensor(KeyboardSensor),
|
||||||
);
|
);
|
||||||
@@ -540,7 +553,7 @@ export default function InvoiceDetail() {
|
|||||||
constant_symbol: "0308",
|
constant_symbol: "0308",
|
||||||
issued_by: user?.fullName || "",
|
issued_by: user?.fullName || "",
|
||||||
billing_text: "",
|
billing_text: "",
|
||||||
notes: "",
|
internal_notes: "",
|
||||||
language: "cs",
|
language: "cs",
|
||||||
bank_account_id: "",
|
bank_account_id: "",
|
||||||
bank_name: "",
|
bank_name: "",
|
||||||
@@ -550,6 +563,9 @@ export default function InvoiceDetail() {
|
|||||||
}));
|
}));
|
||||||
|
|
||||||
const [dueDays, setDueDays] = useState(14);
|
const [dueDays, setDueDays] = useState(14);
|
||||||
|
// Rich-text CZ/EN "Obsah" sections (printed after the items on the PDF —
|
||||||
|
// shared editor with offers/issued orders).
|
||||||
|
const [sections, setSections] = useState<DocumentSection[]>([]);
|
||||||
const [items, setItems] = useState<InvoiceItem[]>(() => [
|
const [items, setItems] = useState<InvoiceItem[]>(() => [
|
||||||
{
|
{
|
||||||
_key: "inv-1",
|
_key: "inv-1",
|
||||||
@@ -626,7 +642,6 @@ export default function InvoiceDetail() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// ─── Edit mode state ───
|
// ─── Edit mode state ───
|
||||||
const [notes, setNotes] = useState("");
|
|
||||||
const [statusChanging, setStatusChanging] = useState<string | null>(null);
|
const [statusChanging, setStatusChanging] = useState<string | null>(null);
|
||||||
const [statusConfirm, setStatusConfirm] = useState<{
|
const [statusConfirm, setStatusConfirm] = useState<{
|
||||||
show: boolean;
|
show: boolean;
|
||||||
@@ -696,7 +711,7 @@ export default function InvoiceDetail() {
|
|||||||
constant_symbol: inv.constant_symbol || "0308",
|
constant_symbol: inv.constant_symbol || "0308",
|
||||||
issued_by: inv.issued_by || "",
|
issued_by: inv.issued_by || "",
|
||||||
billing_text: inv.billing_text || "",
|
billing_text: inv.billing_text || "",
|
||||||
notes: inv.notes || "",
|
internal_notes: inv.internal_notes || "",
|
||||||
language: inv.language || "cs",
|
language: inv.language || "cs",
|
||||||
bank_account_id: matchedBankId,
|
bank_account_id: matchedBankId,
|
||||||
bank_name: inv.bank_name || "",
|
bank_name: inv.bank_name || "",
|
||||||
@@ -705,7 +720,6 @@ export default function InvoiceDetail() {
|
|||||||
bank_account: inv.bank_account || "",
|
bank_account: inv.bank_account || "",
|
||||||
};
|
};
|
||||||
setForm(formData);
|
setForm(formData);
|
||||||
setNotes(inv.notes || "");
|
|
||||||
setInvoiceNumber(inv.invoice_number || "");
|
setInvoiceNumber(inv.invoice_number || "");
|
||||||
|
|
||||||
// Calculate dueDays from existing dates
|
// Calculate dueDays from existing dates
|
||||||
@@ -741,10 +755,22 @@ export default function InvoiceDetail() {
|
|||||||
setItems(mappedItems);
|
setItems(mappedItems);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Populate sections from existing invoice
|
||||||
|
const mappedSections =
|
||||||
|
Array.isArray(inv.sections) && inv.sections.length > 0
|
||||||
|
? inv.sections.map((s) => ({
|
||||||
|
title: s.title || "",
|
||||||
|
title_cz: s.title_cz || "",
|
||||||
|
content: s.content || "",
|
||||||
|
}))
|
||||||
|
: [];
|
||||||
|
setSections(mappedSections);
|
||||||
|
|
||||||
// Capture initial snapshot for dirty-checking
|
// Capture initial snapshot for dirty-checking
|
||||||
initialSnapshotRef.current = JSON.stringify({
|
initialSnapshotRef.current = JSON.stringify({
|
||||||
form: formData,
|
form: formData,
|
||||||
items: mappedItems,
|
items: mappedItems,
|
||||||
|
sections: mappedSections,
|
||||||
});
|
});
|
||||||
|
|
||||||
setDataReady(true);
|
setDataReady(true);
|
||||||
@@ -787,13 +813,12 @@ export default function InvoiceDetail() {
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pre-fill from order
|
// Pre-fill from order. Orders no longer carry VAT (not tax documents) —
|
||||||
|
// the invoice decides its own VAT: default rate from company settings,
|
||||||
|
// apply_vat on.
|
||||||
if (fromOrderId && orderDataQuery.data) {
|
if (fromOrderId && orderDataQuery.data) {
|
||||||
const order = orderDataQuery.data;
|
const order = orderDataQuery.data;
|
||||||
const vatRate = numberOr(
|
const vatRate = numberOr(companySettings?.default_vat_rate, 21);
|
||||||
order.vat_rate,
|
|
||||||
companySettings?.default_vat_rate ?? 21,
|
|
||||||
);
|
|
||||||
setForm((prev) => ({
|
setForm((prev) => ({
|
||||||
...prev,
|
...prev,
|
||||||
customer_id: order.customer_id as number,
|
customer_id: order.customer_id as number,
|
||||||
@@ -803,7 +828,7 @@ export default function InvoiceDetail() {
|
|||||||
(order.currency as string) ||
|
(order.currency as string) ||
|
||||||
companySettings?.default_currency ||
|
companySettings?.default_currency ||
|
||||||
"CZK",
|
"CZK",
|
||||||
apply_vat: Number(order.apply_vat) || 0,
|
apply_vat: 1,
|
||||||
vat_rate: vatRate,
|
vat_rate: vatRate,
|
||||||
}));
|
}));
|
||||||
const orderItems = order.items as Record<string, unknown>[] | undefined;
|
const orderItems = order.items as Record<string, unknown>[] | undefined;
|
||||||
@@ -842,13 +867,15 @@ export default function InvoiceDetail() {
|
|||||||
// Edit mode: captured inside the sync effect from raw query data.
|
// Edit mode: captured inside the sync effect from raw query data.
|
||||||
// Create mode: captured on the first render after sync effects populate the form.
|
// Create mode: captured on the first render after sync effects populate the form.
|
||||||
if (dataReady && !initialSnapshotRef.current) {
|
if (dataReady && !initialSnapshotRef.current) {
|
||||||
initialSnapshotRef.current = JSON.stringify({ form, items });
|
initialSnapshotRef.current = JSON.stringify({ form, items, sections });
|
||||||
}
|
}
|
||||||
|
|
||||||
const isDirty = useMemo(() => {
|
const isDirty = useMemo(() => {
|
||||||
if (!initialSnapshotRef.current) return false;
|
if (!initialSnapshotRef.current) return false;
|
||||||
return JSON.stringify({ form, items }) !== initialSnapshotRef.current;
|
return (
|
||||||
}, [form, items]);
|
JSON.stringify({ form, items, sections }) !== initialSnapshotRef.current
|
||||||
|
);
|
||||||
|
}, [form, items, sections]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!isDirty) return;
|
if (!isDirty) return;
|
||||||
@@ -1006,6 +1033,12 @@ export default function InvoiceDetail() {
|
|||||||
unit_price: Number(item.unit_price) || 0,
|
unit_price: Number(item.unit_price) || 0,
|
||||||
position: i,
|
position: i,
|
||||||
})),
|
})),
|
||||||
|
sections: sections.map((s, i) => ({
|
||||||
|
title: s.title,
|
||||||
|
title_cz: s.title_cz,
|
||||||
|
content: s.content,
|
||||||
|
position: i,
|
||||||
|
})),
|
||||||
};
|
};
|
||||||
// Only set status when a target is given (create-as-draft / create-as-live
|
// Only set status when a target is given (create-as-draft / create-as-live
|
||||||
// / finalize). Editing a live invoice sends no status — the backend keeps
|
// / finalize). Editing a live invoice sends no status — the backend keeps
|
||||||
@@ -1018,7 +1051,7 @@ export default function InvoiceDetail() {
|
|||||||
|
|
||||||
const data = await saveMutation.mutateAsync(payload);
|
const data = await saveMutation.mutateAsync(payload);
|
||||||
alert.success(isEdit ? "Faktura byla uložena" : "Faktura byla vytvořena");
|
alert.success(isEdit ? "Faktura byla uložena" : "Faktura byla vytvořena");
|
||||||
initialSnapshotRef.current = JSON.stringify({ form, items });
|
initialSnapshotRef.current = JSON.stringify({ form, items, sections });
|
||||||
if (!isEdit) {
|
if (!isEdit) {
|
||||||
navigate(`/invoices/${data.invoice_id}`);
|
navigate(`/invoices/${data.invoice_id}`);
|
||||||
}
|
}
|
||||||
@@ -1149,7 +1182,13 @@ export default function InvoiceDetail() {
|
|||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<Typography variant="h4">
|
<Typography variant="h4">
|
||||||
Faktura {documentNumberLabel(invoice.invoice_number)}
|
Faktura
|
||||||
|
<Box
|
||||||
|
component="span"
|
||||||
|
sx={{ color: "text.secondary", ml: 1, fontWeight: 400 }}
|
||||||
|
>
|
||||||
|
{documentNumberLabel(invoice.invoice_number)}
|
||||||
|
</Box>
|
||||||
</Typography>
|
</Typography>
|
||||||
<StatusChip
|
<StatusChip
|
||||||
label={statusLabel(INVOICE_STATUS, invoice.status)}
|
label={statusLabel(INVOICE_STATUS, invoice.status)}
|
||||||
@@ -1282,11 +1321,6 @@ export default function InvoiceDetail() {
|
|||||||
<Field label="Variabilní symbol">
|
<Field label="Variabilní symbol">
|
||||||
<Typography variant="body2">{invoice.invoice_number}</Typography>
|
<Typography variant="body2">{invoice.invoice_number}</Typography>
|
||||||
</Field>
|
</Field>
|
||||||
<Field label="Vystavil">
|
|
||||||
<Typography variant="body2">
|
|
||||||
{invoice.issued_by || "—"}
|
|
||||||
</Typography>
|
|
||||||
</Field>
|
|
||||||
</Box>
|
</Box>
|
||||||
{invoice.paid_date && (
|
{invoice.paid_date && (
|
||||||
<Box sx={{ mt: 2 }}>
|
<Box sx={{ mt: 2 }}>
|
||||||
@@ -1598,14 +1632,55 @@ export default function InvoiceDetail() {
|
|||||||
</Box>
|
</Box>
|
||||||
</Card>
|
</Card>
|
||||||
|
|
||||||
{/* Notes (read-only) */}
|
{/* Obsah sections (read-only) — shown only when any section has content */}
|
||||||
|
{(invoice.sections ?? []).some(
|
||||||
|
(s) =>
|
||||||
|
(s.title || "").trim() ||
|
||||||
|
(s.title_cz || "").trim() ||
|
||||||
|
(s.content || "").replace(/<[^>]*>/g, "").trim(),
|
||||||
|
) && (
|
||||||
|
<Card sx={{ mb: 3 }}>
|
||||||
|
<Typography variant="subtitle2" sx={{ mb: 2, fontWeight: 600 }}>
|
||||||
|
Obsah
|
||||||
|
</Typography>
|
||||||
|
<Box sx={{ display: "flex", flexDirection: "column", gap: 2 }}>
|
||||||
|
{(invoice.sections ?? []).map((s, idx) => (
|
||||||
|
<Box key={s.id ?? idx}>
|
||||||
|
{((invoice.language === "cs"
|
||||||
|
? s.title_cz || s.title
|
||||||
|
: s.title) ||
|
||||||
|
"") && (
|
||||||
|
<Typography variant="body2" sx={{ fontWeight: 600 }}>
|
||||||
|
{invoice.language === "cs"
|
||||||
|
? s.title_cz || s.title
|
||||||
|
: s.title}
|
||||||
|
</Typography>
|
||||||
|
)}
|
||||||
|
{(s.content || "").replace(/<[^>]*>/g, "").trim() && (
|
||||||
|
<RichTextView
|
||||||
|
dangerouslySetInnerHTML={{
|
||||||
|
__html: DOMPurify.sanitize(s.content || ""),
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</Box>
|
||||||
|
))}
|
||||||
|
</Box>
|
||||||
|
</Card>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Internal notes (read-only, never printed) */}
|
||||||
<Card sx={{ mb: 3 }}>
|
<Card sx={{ mb: 3 }}>
|
||||||
<Typography variant="subtitle2" sx={{ mb: 2, fontWeight: 600 }}>
|
<Typography variant="subtitle2" sx={{ mb: 2, fontWeight: 600 }}>
|
||||||
Veřejné poznámky na faktuře
|
Interní poznámky
|
||||||
</Typography>
|
</Typography>
|
||||||
{notes && notes.trim() && notes !== "<p><br></p>" ? (
|
{invoice.internal_notes &&
|
||||||
|
invoice.internal_notes.trim() &&
|
||||||
|
invoice.internal_notes !== "<p><br></p>" ? (
|
||||||
<RichTextView
|
<RichTextView
|
||||||
dangerouslySetInnerHTML={{ __html: DOMPurify.sanitize(notes) }}
|
dangerouslySetInnerHTML={{
|
||||||
|
__html: DOMPurify.sanitize(invoice.internal_notes),
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
) : (
|
) : (
|
||||||
<Typography variant="body2" color="text.secondary">
|
<Typography variant="body2" color="text.secondary">
|
||||||
@@ -1660,43 +1735,44 @@ export default function InvoiceDetail() {
|
|||||||
Zpět
|
Zpět
|
||||||
</Button>
|
</Button>
|
||||||
<Box>
|
<Box>
|
||||||
{isEdit && invoice ? (
|
<Box
|
||||||
<Box
|
sx={{
|
||||||
sx={{
|
display: "flex",
|
||||||
display: "flex",
|
alignItems: "center",
|
||||||
alignItems: "center",
|
gap: 1.5,
|
||||||
gap: 1.5,
|
flexWrap: "wrap",
|
||||||
flexWrap: "wrap",
|
}}
|
||||||
}}
|
>
|
||||||
>
|
<Typography variant="h4">
|
||||||
<Typography variant="h4">
|
{isEdit ? "Faktura" : "Nová faktura"}
|
||||||
Faktura {documentNumberLabel(invoice.invoice_number)}
|
{/* Edit mode: a draft has no number yet → show "Koncept".
|
||||||
</Typography>
|
Create mode: show the previewed next-number when available. */}
|
||||||
|
{(isEdit || invoiceNumber) && (
|
||||||
|
<Box
|
||||||
|
component="span"
|
||||||
|
sx={{ color: "text.secondary", ml: 1, fontWeight: 400 }}
|
||||||
|
>
|
||||||
|
{isEdit
|
||||||
|
? documentNumberLabel(invoice?.invoice_number)
|
||||||
|
: invoiceNumber}
|
||||||
|
</Box>
|
||||||
|
)}
|
||||||
|
</Typography>
|
||||||
|
{isEdit && invoice && (
|
||||||
<StatusChip
|
<StatusChip
|
||||||
label={statusLabel(INVOICE_STATUS, invoice.status)}
|
label={statusLabel(INVOICE_STATUS, invoice.status)}
|
||||||
color={statusColor(INVOICE_STATUS, invoice.status)}
|
color={statusColor(INVOICE_STATUS, invoice.status)}
|
||||||
/>
|
/>
|
||||||
</Box>
|
)}
|
||||||
) : (
|
</Box>
|
||||||
<>
|
{!isEdit && fromOrderId && (
|
||||||
<Typography variant="h4">
|
<Typography
|
||||||
Nová faktura{" "}
|
variant="body2"
|
||||||
{invoiceNumber && (
|
color="text.secondary"
|
||||||
<Box component="span" sx={{ color: "text.secondary" }}>
|
sx={{ mt: 0.5 }}
|
||||||
({invoiceNumber})
|
>
|
||||||
</Box>
|
Z objednávky
|
||||||
)}
|
</Typography>
|
||||||
</Typography>
|
|
||||||
{fromOrderId && (
|
|
||||||
<Typography
|
|
||||||
variant="body2"
|
|
||||||
color="text.secondary"
|
|
||||||
sx={{ mt: 0.5 }}
|
|
||||||
>
|
|
||||||
Z objednávky
|
|
||||||
</Typography>
|
|
||||||
)}
|
|
||||||
</>
|
|
||||||
)}
|
)}
|
||||||
</Box>
|
</Box>
|
||||||
</Box>
|
</Box>
|
||||||
@@ -1867,37 +1943,19 @@ export default function InvoiceDetail() {
|
|||||||
<Typography variant="subtitle2" sx={{ mb: 2, fontWeight: 600 }}>
|
<Typography variant="subtitle2" sx={{ mb: 2, fontWeight: 600 }}>
|
||||||
Základní údaje
|
Základní údaje
|
||||||
</Typography>
|
</Typography>
|
||||||
<Box
|
{/* Číslo faktury and Vystavil are not form fields anymore: the
|
||||||
sx={{
|
number lives in the page header (deferred numbering — "Koncept"
|
||||||
display: "grid",
|
until finalize) and issued_by is auto-filled from the logged-in
|
||||||
gridTemplateColumns: { xs: "1fr", md: "1fr 1fr 1fr" },
|
user and still submitted with the payload (the PDF prints it). */}
|
||||||
gap: 2,
|
<Field label="Odběratel" error={errors.customer_id} required>
|
||||||
}}
|
<CustomerPicker
|
||||||
>
|
customers={customers}
|
||||||
<Field label="Číslo faktury">
|
value={form.customer_id}
|
||||||
<TextField
|
onChange={selectCustomer}
|
||||||
value={invoiceNumber}
|
error={errors.customer_id}
|
||||||
InputProps={{ readOnly: true }}
|
placeholder="Hledat zákazníka (název, IČ)..."
|
||||||
sx={{ "& .MuiInputBase-root": { bgcolor: "action.hover" } }}
|
/>
|
||||||
/>
|
</Field>
|
||||||
</Field>
|
|
||||||
<Field label="Odběratel" error={errors.customer_id} required>
|
|
||||||
<CustomerPicker
|
|
||||||
customers={customers}
|
|
||||||
value={form.customer_id}
|
|
||||||
onChange={selectCustomer}
|
|
||||||
error={errors.customer_id}
|
|
||||||
placeholder="Hledat zákazníka (název, IČ)..."
|
|
||||||
/>
|
|
||||||
</Field>
|
|
||||||
<Field label="Vystavil">
|
|
||||||
<TextField
|
|
||||||
value={form.issued_by}
|
|
||||||
InputProps={{ readOnly: true }}
|
|
||||||
sx={{ "& .MuiInputBase-root": { bgcolor: "action.hover" } }}
|
|
||||||
/>
|
|
||||||
</Field>
|
|
||||||
</Box>
|
|
||||||
|
|
||||||
<Field label="Text fakturace (na PDF)">
|
<Field label="Text fakturace (na PDF)">
|
||||||
<TextField
|
<TextField
|
||||||
@@ -2243,16 +2301,26 @@ export default function InvoiceDetail() {
|
|||||||
</Box>
|
</Box>
|
||||||
</Card>
|
</Card>
|
||||||
|
|
||||||
{/* Notes */}
|
{/* Rich-text PDF sections — printed after the items (like orders) */}
|
||||||
|
<SectionsEditor
|
||||||
|
sections={sections}
|
||||||
|
onChange={setSections}
|
||||||
|
readOnly={false}
|
||||||
|
title="Obsah"
|
||||||
|
language={form.language}
|
||||||
|
/>
|
||||||
|
|
||||||
|
{/* Internal notes */}
|
||||||
<Card sx={{ mb: 3 }}>
|
<Card sx={{ mb: 3 }}>
|
||||||
<Typography variant="subtitle2" sx={{ mb: 2, fontWeight: 600 }}>
|
<Field label="Interní poznámky" hint="Nezobrazí se na PDF.">
|
||||||
Veřejné poznámky na faktuře
|
<RichEditor
|
||||||
</Typography>
|
value={form.internal_notes}
|
||||||
<RichEditor
|
onChange={(val) =>
|
||||||
value={form.notes}
|
setForm((prev) => ({ ...prev, internal_notes: val }))
|
||||||
onChange={(val) => setForm((prev) => ({ ...prev, notes: val }))}
|
}
|
||||||
placeholder="Poznámky zobrazené na faktuře..."
|
placeholder="Interní poznámky..."
|
||||||
/>
|
/>
|
||||||
|
</Field>
|
||||||
</Card>
|
</Card>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -2,7 +2,9 @@ import { useState, useEffect, useRef } from "react";
|
|||||||
import { useQuery } from "@tanstack/react-query";
|
import { useQuery } from "@tanstack/react-query";
|
||||||
import { useNavigate, Link as RouterLink } from "react-router-dom";
|
import { useNavigate, Link as RouterLink } from "react-router-dom";
|
||||||
import Box from "@mui/material/Box";
|
import Box from "@mui/material/Box";
|
||||||
|
import Typography from "@mui/material/Typography";
|
||||||
import IconButton from "@mui/material/IconButton";
|
import IconButton from "@mui/material/IconButton";
|
||||||
|
import CircularProgress from "@mui/material/CircularProgress";
|
||||||
import { useAlert } from "../context/AlertContext";
|
import { useAlert } from "../context/AlertContext";
|
||||||
import { useAuth } from "../context/AuthContext";
|
import { useAuth } from "../context/AuthContext";
|
||||||
import Forbidden from "../components/Forbidden";
|
import Forbidden from "../components/Forbidden";
|
||||||
@@ -10,18 +12,21 @@ import {
|
|||||||
formatCurrency,
|
formatCurrency,
|
||||||
formatDate,
|
formatDate,
|
||||||
formatMultiCurrency,
|
formatMultiCurrency,
|
||||||
|
czechPlural,
|
||||||
} from "../utils/formatters";
|
} from "../utils/formatters";
|
||||||
import useTableSort from "../hooks/useTableSort";
|
import useTableSort from "../hooks/useTableSort";
|
||||||
import useDebounce from "../hooks/useDebounce";
|
import useDebounce from "../hooks/useDebounce";
|
||||||
import { usePaginatedQuery } from "../hooks/usePaginatedQuery";
|
import { usePaginatedQuery } from "../hooks/usePaginatedQuery";
|
||||||
import { useApiMutation } from "../lib/queries/mutations";
|
import { useApiMutation } from "../lib/queries/mutations";
|
||||||
import apiFetch from "../utils/api";
|
import { useDocumentListPdf } from "../hooks/useDocumentPdf";
|
||||||
import {
|
import {
|
||||||
issuedOrderListOptions,
|
issuedOrderListOptions,
|
||||||
issuedOrderStatsOptions,
|
issuedOrderStatsOptions,
|
||||||
|
issuedOrderSuppliersOptions,
|
||||||
type IssuedOrder,
|
type IssuedOrder,
|
||||||
} from "../lib/queries/issued-orders";
|
} from "../lib/queries/issued-orders";
|
||||||
import {
|
import {
|
||||||
|
Button,
|
||||||
Card,
|
Card,
|
||||||
DataTable,
|
DataTable,
|
||||||
Pagination,
|
Pagination,
|
||||||
@@ -44,9 +49,15 @@ import {
|
|||||||
|
|
||||||
const API_BASE = "/api/admin";
|
const API_BASE = "/api/admin";
|
||||||
|
|
||||||
|
// DELIBERATE deviation from the Offers status-Tabs row: this page is embedded
|
||||||
|
// under the parent Orders page, which already renders the identical centered
|
||||||
|
// kit <Tabs> (Vydané/Přijaté) directly above this component — a second
|
||||||
|
// same-styled tab row would visually fight it (and the sibling "Přijaté" tab
|
||||||
|
// filters via a Select too). So the status filter stays a Select, with the
|
||||||
|
// "all" entry relabeled to match the offers tabs ("Všechny").
|
||||||
const STATUS_OPTIONS = statusOptions(ISSUED_ORDER_STATUS, {
|
const STATUS_OPTIONS = statusOptions(ISSUED_ORDER_STATUS, {
|
||||||
value: "",
|
value: "",
|
||||||
label: "Všechny stavy",
|
label: "Všechny",
|
||||||
});
|
});
|
||||||
|
|
||||||
const ViewIcon = (
|
const ViewIcon = (
|
||||||
@@ -62,6 +73,19 @@ const ViewIcon = (
|
|||||||
<circle cx="12" cy="12" r="3" />
|
<circle cx="12" cy="12" r="3" />
|
||||||
</svg>
|
</svg>
|
||||||
);
|
);
|
||||||
|
const EditIcon = (
|
||||||
|
<svg
|
||||||
|
width="18"
|
||||||
|
height="18"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
strokeWidth="2"
|
||||||
|
>
|
||||||
|
<path d="M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7" />
|
||||||
|
<path d="M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z" />
|
||||||
|
</svg>
|
||||||
|
);
|
||||||
const PdfIcon = (
|
const PdfIcon = (
|
||||||
<svg
|
<svg
|
||||||
width="18"
|
width="18"
|
||||||
@@ -107,11 +131,18 @@ export default function IssuedOrders({ month, year }: IssuedOrdersProps) {
|
|||||||
const [search, setSearch] = useState("");
|
const [search, setSearch] = useState("");
|
||||||
const debouncedSearch = useDebounce(search, 300);
|
const debouncedSearch = useDebounce(search, 300);
|
||||||
const [status, setStatus] = useState("");
|
const [status, setStatus] = useState("");
|
||||||
|
const [supplierFilter, setSupplierFilter] = useState<number | "">("");
|
||||||
const [page, setPage] = useState(1);
|
const [page, setPage] = useState(1);
|
||||||
// Track first successful load so later refetches (filter/status/page change)
|
// Track first successful load so later refetches (filter/status/page change)
|
||||||
// keep the table visible instead of flashing the full-page skeleton.
|
// keep the table visible instead of flashing the full-page skeleton.
|
||||||
const hasLoadedOnce = useRef(false);
|
const hasLoadedOnce = useRef(false);
|
||||||
|
|
||||||
|
const { data: suppliers } = useQuery(issuedOrderSuppliersOptions());
|
||||||
|
|
||||||
|
// Shared hardened PDF flow (per-row spinner, double-click guard, 401 silent
|
||||||
|
// close, blob-URL revoke + unmount cleanup) — same hook as the Offers list.
|
||||||
|
const { openPdf, pdfLoadingId } = useDocumentListPdf();
|
||||||
|
|
||||||
const [deleteConfirm, setDeleteConfirm] = useState<{
|
const [deleteConfirm, setDeleteConfirm] = useState<{
|
||||||
show: boolean;
|
show: boolean;
|
||||||
order: IssuedOrder | null;
|
order: IssuedOrder | null;
|
||||||
@@ -141,8 +172,8 @@ export default function IssuedOrders({ month, year }: IssuedOrdersProps) {
|
|||||||
sort,
|
sort,
|
||||||
order,
|
order,
|
||||||
page,
|
page,
|
||||||
perPage: 20,
|
|
||||||
status: status || undefined,
|
status: status || undefined,
|
||||||
|
supplier_id: supplierFilter || undefined,
|
||||||
month,
|
month,
|
||||||
year,
|
year,
|
||||||
}),
|
}),
|
||||||
@@ -154,6 +185,7 @@ export default function IssuedOrders({ month, year }: IssuedOrdersProps) {
|
|||||||
issuedOrderStatsOptions({
|
issuedOrderStatsOptions({
|
||||||
search: debouncedSearch,
|
search: debouncedSearch,
|
||||||
status: status || undefined,
|
status: status || undefined,
|
||||||
|
supplier_id: supplierFilter || undefined,
|
||||||
month,
|
month,
|
||||||
year,
|
year,
|
||||||
}),
|
}),
|
||||||
@@ -176,30 +208,6 @@ export default function IssuedOrders({ month, year }: IssuedOrdersProps) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleExportPdf = async (o: IssuedOrder) => {
|
|
||||||
// Authenticated open: apiFetch attaches the access token, then we hand the
|
|
||||||
// resulting PDF blob to a window. A raw window.open(url) would be an
|
|
||||||
// unauthenticated top-level navigation and 401 on the token-guarded route.
|
|
||||||
const newWindow = window.open("", "_blank");
|
|
||||||
try {
|
|
||||||
const response = await apiFetch(
|
|
||||||
`${API_BASE}/issued-orders-pdf/${o.id}?lang=cs`,
|
|
||||||
);
|
|
||||||
if (!response.ok) {
|
|
||||||
newWindow?.close();
|
|
||||||
alert.error("Nepodařilo se vygenerovat PDF");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const blob = await response.blob();
|
|
||||||
const url = URL.createObjectURL(blob);
|
|
||||||
if (newWindow) newWindow.location.href = url;
|
|
||||||
setTimeout(() => URL.revokeObjectURL(url), 60000);
|
|
||||||
} catch {
|
|
||||||
newWindow?.close();
|
|
||||||
alert.error("Chyba při generování PDF");
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Only show the full-page skeleton on the very first load; on subsequent
|
// Only show the full-page skeleton on the very first load; on subsequent
|
||||||
// refetches (filter/status/page change) keep the table visible (the Card
|
// refetches (filter/status/page change) keep the table visible (the Card
|
||||||
// dims via isFetching) so it doesn't flash.
|
// dims via isFetching) so it doesn't flash.
|
||||||
@@ -229,10 +237,18 @@ export default function IssuedOrders({ month, year }: IssuedOrdersProps) {
|
|||||||
),
|
),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
key: "customer_name",
|
key: "supplier_name",
|
||||||
header: "Dodavatel",
|
header: "Dodavatel",
|
||||||
width: "24%",
|
width: "24%",
|
||||||
render: (o) => o.customer_name || "—",
|
render: (o) => o.supplier_name || "—",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: "order_date",
|
||||||
|
header: "Datum",
|
||||||
|
width: "13%",
|
||||||
|
sortKey: "order_date",
|
||||||
|
mono: true,
|
||||||
|
render: (o) => (o.order_date ? formatDate(o.order_date) : "—"),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
key: "status",
|
key: "status",
|
||||||
@@ -246,14 +262,6 @@ export default function IssuedOrders({ month, year }: IssuedOrdersProps) {
|
|||||||
/>
|
/>
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
{
|
|
||||||
key: "order_date",
|
|
||||||
header: "Datum",
|
|
||||||
width: "13%",
|
|
||||||
sortKey: "order_date",
|
|
||||||
mono: true,
|
|
||||||
render: (o) => (o.order_date ? formatDate(o.order_date) : "—"),
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
key: "total",
|
key: "total",
|
||||||
header: "Celkem",
|
header: "Celkem",
|
||||||
@@ -268,39 +276,52 @@ export default function IssuedOrders({ month, year }: IssuedOrdersProps) {
|
|||||||
header: "Akce",
|
header: "Akce",
|
||||||
width: "14%",
|
width: "14%",
|
||||||
align: "right",
|
align: "right",
|
||||||
render: (o) => (
|
render: (o) => {
|
||||||
<Box sx={{ display: "flex", gap: 0.5, justifyContent: "flex-end" }}>
|
// Offers' semantics: read-only rows (completed/cancelled) get the
|
||||||
<IconButton
|
// view icon, editable rows get the edit icon.
|
||||||
size="small"
|
const readOnly = o.status === "completed" || o.status === "cancelled";
|
||||||
onClick={() => navigate(`/orders/issued/${o.id}`)}
|
return (
|
||||||
aria-label={hasPermission("orders.edit") ? "Upravit" : "Detail"}
|
<Box sx={{ display: "flex", gap: 0.5, justifyContent: "flex-end" }}>
|
||||||
title={hasPermission("orders.edit") ? "Upravit" : "Detail"}
|
|
||||||
>
|
|
||||||
{ViewIcon}
|
|
||||||
</IconButton>
|
|
||||||
{hasPermission("orders.view") && (
|
|
||||||
<IconButton
|
<IconButton
|
||||||
size="small"
|
size="small"
|
||||||
onClick={() => handleExportPdf(o)}
|
onClick={() => navigate(`/orders/issued/${o.id}`)}
|
||||||
aria-label="PDF"
|
aria-label={readOnly ? "Zobrazit" : "Upravit"}
|
||||||
title="PDF"
|
title={readOnly ? "Zobrazit" : "Upravit"}
|
||||||
>
|
>
|
||||||
{PdfIcon}
|
{readOnly ? ViewIcon : EditIcon}
|
||||||
</IconButton>
|
</IconButton>
|
||||||
)}
|
{/* Drafts have no number yet — /file 404s for them. */}
|
||||||
{hasPermission("orders.delete") && (
|
{hasPermission("orders.view") && o.po_number && (
|
||||||
<IconButton
|
<IconButton
|
||||||
size="small"
|
size="small"
|
||||||
color="error"
|
onClick={() =>
|
||||||
onClick={() => setDeleteConfirm({ show: true, order: o })}
|
openPdf(o.id, `${API_BASE}/issued-orders/${o.id}/file`)
|
||||||
aria-label="Smazat"
|
}
|
||||||
title="Smazat"
|
aria-label="Zobrazit objednávku"
|
||||||
>
|
title="Zobrazit objednávku"
|
||||||
{DeleteIcon}
|
disabled={pdfLoadingId === o.id}
|
||||||
</IconButton>
|
>
|
||||||
)}
|
{pdfLoadingId === o.id ? (
|
||||||
</Box>
|
<CircularProgress size={16} />
|
||||||
),
|
) : (
|
||||||
|
PdfIcon
|
||||||
|
)}
|
||||||
|
</IconButton>
|
||||||
|
)}
|
||||||
|
{hasPermission("orders.delete") && (
|
||||||
|
<IconButton
|
||||||
|
size="small"
|
||||||
|
color="error"
|
||||||
|
onClick={() => setDeleteConfirm({ show: true, order: o })}
|
||||||
|
aria-label="Smazat"
|
||||||
|
title="Smazat"
|
||||||
|
>
|
||||||
|
{DeleteIcon}
|
||||||
|
</IconButton>
|
||||||
|
)}
|
||||||
|
</Box>
|
||||||
|
);
|
||||||
|
},
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
@@ -326,12 +347,25 @@ export default function IssuedOrders({ month, year }: IssuedOrdersProps) {
|
|||||||
return {};
|
return {};
|
||||||
};
|
};
|
||||||
|
|
||||||
// Search/status filter is active → an empty list means "nothing matches"
|
// Search/status/supplier filter is active → an empty list means "nothing
|
||||||
// (no create CTA); a genuinely empty list keeps the create-CTA empty state.
|
// matches" (no create CTA); a genuinely empty list keeps the create-CTA
|
||||||
const isFiltered = !!debouncedSearch || !!status;
|
// empty state.
|
||||||
|
const isFiltered = !!debouncedSearch || !!status || !!supplierFilter;
|
||||||
|
|
||||||
|
const total = pagination?.total ?? orders.length;
|
||||||
|
const countLine = `${total} ${czechPlural(
|
||||||
|
total,
|
||||||
|
"objednávka",
|
||||||
|
"objednávky",
|
||||||
|
"objednávek",
|
||||||
|
)}`;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
<Typography variant="body2" color="text.secondary" sx={{ mb: 1.5 }}>
|
||||||
|
{countLine}
|
||||||
|
</Typography>
|
||||||
|
|
||||||
<FilterBar>
|
<FilterBar>
|
||||||
<Box sx={{ flex: "1 1 320px" }}>
|
<Box sx={{ flex: "1 1 320px" }}>
|
||||||
<TextField
|
<TextField
|
||||||
@@ -354,6 +388,22 @@ export default function IssuedOrders({ month, year }: IssuedOrdersProps) {
|
|||||||
options={STATUS_OPTIONS}
|
options={STATUS_OPTIONS}
|
||||||
/>
|
/>
|
||||||
</Box>
|
</Box>
|
||||||
|
<Box sx={{ flex: "0 0 220px" }}>
|
||||||
|
<Select
|
||||||
|
value={supplierFilter === "" ? "" : String(supplierFilter)}
|
||||||
|
onChange={(value) => {
|
||||||
|
setSupplierFilter(value ? Number(value) : "");
|
||||||
|
setPage(1);
|
||||||
|
}}
|
||||||
|
options={[
|
||||||
|
{ value: "", label: "Všichni dodavatelé" },
|
||||||
|
...(suppliers ?? []).map((s) => ({
|
||||||
|
value: String(s.id),
|
||||||
|
label: s.name,
|
||||||
|
})),
|
||||||
|
]}
|
||||||
|
/>
|
||||||
|
</Box>
|
||||||
</FilterBar>
|
</FilterBar>
|
||||||
|
|
||||||
<Card sx={{ opacity: isFetching ? 0.6 : 1, transition: "opacity .2s" }}>
|
<Card sx={{ opacity: isFetching ? 0.6 : 1, transition: "opacity .2s" }}>
|
||||||
@@ -367,14 +417,19 @@ export default function IssuedOrders({ month, year }: IssuedOrdersProps) {
|
|||||||
onSort={handleSort}
|
onSort={handleSort}
|
||||||
empty={
|
empty={
|
||||||
isFiltered ? (
|
isFiltered ? (
|
||||||
<EmptyState title="Žádné objednávky neodpovídají filtru." />
|
<EmptyState
|
||||||
|
title="Žádné objednávky neodpovídají filtru"
|
||||||
|
description="Zkuste změnit filtr nebo hledaný výraz."
|
||||||
|
/>
|
||||||
) : (
|
) : (
|
||||||
<EmptyState
|
<EmptyState
|
||||||
title="Zatím žádné vydané objednávky."
|
title="Zatím žádné vydané objednávky."
|
||||||
description={
|
action={
|
||||||
hasPermission("orders.create")
|
hasPermission("orders.create") ? (
|
||||||
? "Vytvořte první tlačítkem „Vytvořit objednávku."
|
<Button component={RouterLink} to="/orders/issued/new">
|
||||||
: undefined
|
Vytvořit objednávku
|
||||||
|
</Button>
|
||||||
|
) : undefined
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
)
|
)
|
||||||
@@ -392,7 +447,7 @@ export default function IssuedOrders({ month, year }: IssuedOrdersProps) {
|
|||||||
fontSize: "0.9rem",
|
fontSize: "0.9rem",
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<span>Celkem:</span>
|
<span>Celkem bez DPH:</span>
|
||||||
<Box
|
<Box
|
||||||
component="span"
|
component="span"
|
||||||
sx={{ fontWeight: 700, color: "text.primary" }}
|
sx={{ fontWeight: 700, color: "text.primary" }}
|
||||||
@@ -415,11 +470,10 @@ export default function IssuedOrders({ month, year }: IssuedOrdersProps) {
|
|||||||
title="Smazat objednávku"
|
title="Smazat objednávku"
|
||||||
message={
|
message={
|
||||||
deleteConfirm.order
|
deleteConfirm.order
|
||||||
? `Opravdu chcete smazat objednávku „${deleteConfirm.order.po_number || ""}"? Tato akce je nevratná.`
|
? `Opravdu chcete smazat objednávku „${documentNumberLabel(deleteConfirm.order.po_number)}“? Budou smazány i všechny položky. Tato akce je nevratná.`
|
||||||
: ""
|
: ""
|
||||||
}
|
}
|
||||||
confirmText="Smazat"
|
confirmText="Smazat"
|
||||||
cancelText="Zrušit"
|
|
||||||
confirmVariant="danger"
|
confirmVariant="danger"
|
||||||
loading={deleteMutation.isPending}
|
loading={deleteMutation.isPending}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -161,7 +161,7 @@ export default function LeaveApproval() {
|
|||||||
>({
|
>({
|
||||||
url: ({ id }) => `${API_BASE}/leave-requests/${id}`,
|
url: ({ id }) => `${API_BASE}/leave-requests/${id}`,
|
||||||
method: () => "PUT",
|
method: () => "PUT",
|
||||||
invalidate: ["leave-requests", "leave", "attendance", "users"],
|
invalidate: ["leave-requests", "leave", "attendance", "users", "dashboard"],
|
||||||
onSuccess: () => {
|
onSuccess: () => {
|
||||||
setApproveModal({ open: false, request: null });
|
setApproveModal({ open: false, request: null });
|
||||||
alert.success("Žádost byla schválena");
|
alert.success("Žádost byla schválena");
|
||||||
@@ -174,7 +174,7 @@ export default function LeaveApproval() {
|
|||||||
>({
|
>({
|
||||||
url: ({ id }) => `${API_BASE}/leave-requests/${id}`,
|
url: ({ id }) => `${API_BASE}/leave-requests/${id}`,
|
||||||
method: () => "PUT",
|
method: () => "PUT",
|
||||||
invalidate: ["leave-requests", "leave", "attendance", "users"],
|
invalidate: ["leave-requests", "leave", "attendance", "users", "dashboard"],
|
||||||
onSuccess: () => {
|
onSuccess: () => {
|
||||||
setRejectModal({ open: false, request: null });
|
setRejectModal({ open: false, request: null });
|
||||||
setRejectNote("");
|
setRejectNote("");
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -23,10 +23,12 @@ import useTableSort from "../hooks/useTableSort";
|
|||||||
import useDebounce from "../hooks/useDebounce";
|
import useDebounce from "../hooks/useDebounce";
|
||||||
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
||||||
import { usePaginatedQuery } from "../hooks/usePaginatedQuery";
|
import { usePaginatedQuery } from "../hooks/usePaginatedQuery";
|
||||||
|
import { useDocumentListPdf } from "../hooks/useDocumentPdf";
|
||||||
import {
|
import {
|
||||||
offerListOptions,
|
offerListOptions,
|
||||||
offerStatsOptions,
|
offerStatsOptions,
|
||||||
offerCustomersOptions,
|
offerCustomersOptions,
|
||||||
|
type Quotation,
|
||||||
} from "../lib/queries/offers";
|
} from "../lib/queries/offers";
|
||||||
import { useApiMutation } from "../lib/queries/mutations";
|
import { useApiMutation } from "../lib/queries/mutations";
|
||||||
import {
|
import {
|
||||||
@@ -69,20 +71,6 @@ const STATUS_FILTERS = statusOptions(OFFER_STATUS, {
|
|||||||
label: "Všechny",
|
label: "Všechny",
|
||||||
}).filter((o) => o.value !== "expired");
|
}).filter((o) => o.value !== "expired");
|
||||||
|
|
||||||
interface Quotation {
|
|
||||||
id: number;
|
|
||||||
quotation_number: string;
|
|
||||||
project_code: string;
|
|
||||||
customer_name: string;
|
|
||||||
created_at: string;
|
|
||||||
valid_until: string;
|
|
||||||
currency: string;
|
|
||||||
total: number;
|
|
||||||
status: string;
|
|
||||||
order_id?: number;
|
|
||||||
order_status?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
const TemplatesIcon = (
|
const TemplatesIcon = (
|
||||||
<svg
|
<svg
|
||||||
width="20"
|
width="20"
|
||||||
@@ -336,25 +324,17 @@ export default function Offers() {
|
|||||||
show: boolean;
|
show: boolean;
|
||||||
quotation: Quotation | null;
|
quotation: Quotation | null;
|
||||||
}>({ show: false, quotation: null });
|
}>({ show: false, quotation: null });
|
||||||
const [deleting, setDeleting] = useState(false);
|
|
||||||
const [invalidateConfirm, setInvalidateConfirm] = useState<{
|
const [invalidateConfirm, setInvalidateConfirm] = useState<{
|
||||||
show: boolean;
|
show: boolean;
|
||||||
quotation: Quotation | null;
|
quotation: Quotation | null;
|
||||||
}>({ show: false, quotation: null });
|
}>({ show: false, quotation: null });
|
||||||
const [invalidating, setInvalidating] = useState(false);
|
const [invalidating, setInvalidating] = useState(false);
|
||||||
const blobUrlRef = useRef<string | null>(null);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
return () => {
|
|
||||||
if (blobUrlRef.current) {
|
|
||||||
URL.revokeObjectURL(blobUrlRef.current);
|
|
||||||
blobUrlRef.current = null;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}, []);
|
|
||||||
const [duplicating, setDuplicating] = useState<number | null>(null);
|
const [duplicating, setDuplicating] = useState<number | null>(null);
|
||||||
const [pdfLoading, setPdfLoading] = useState<number | null>(null);
|
|
||||||
const [creatingOrder, setCreatingOrder] = useState<number | null>(null);
|
const [creatingOrder, setCreatingOrder] = useState<number | null>(null);
|
||||||
|
|
||||||
|
// Shared hardened PDF flow (per-row spinner, double-click guard, 401 silent
|
||||||
|
// close, blob-URL revoke + unmount cleanup) — same hook as the issued list.
|
||||||
|
const { openPdf, pdfLoadingId } = useDocumentListPdf();
|
||||||
const [orderModal, setOrderModal] = useState<{
|
const [orderModal, setOrderModal] = useState<{
|
||||||
show: boolean;
|
show: boolean;
|
||||||
quotation: Quotation | null;
|
quotation: Quotation | null;
|
||||||
@@ -490,13 +470,10 @@ export default function Offers() {
|
|||||||
|
|
||||||
const handleDelete = async () => {
|
const handleDelete = async () => {
|
||||||
if (!deleteConfirm.quotation) return;
|
if (!deleteConfirm.quotation) return;
|
||||||
setDeleting(true);
|
|
||||||
try {
|
try {
|
||||||
await deleteOfferMutation.mutateAsync(deleteConfirm.quotation.id);
|
await deleteOfferMutation.mutateAsync(deleteConfirm.quotation.id);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
alert.error(e instanceof Error ? e.message : "Chyba připojení");
|
alert.error(e instanceof Error ? e.message : "Chyba připojení");
|
||||||
} finally {
|
|
||||||
setDeleting(false);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -512,37 +489,6 @@ export default function Offers() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handlePdf = async (quotation: Quotation) => {
|
|
||||||
if (pdfLoading) return;
|
|
||||||
const newWindow = window.open("", "_blank");
|
|
||||||
setPdfLoading(quotation.id);
|
|
||||||
try {
|
|
||||||
const response = await apiFetch(
|
|
||||||
`${API_BASE}/offers/${quotation.id}/file`,
|
|
||||||
);
|
|
||||||
if (response.status === 401) {
|
|
||||||
newWindow?.close();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!response.ok) {
|
|
||||||
newWindow?.close();
|
|
||||||
alert.error("PDF soubor nenalezen — otevřete nabídku a uložte ji");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const blob = await response.blob();
|
|
||||||
if (blobUrlRef.current) {
|
|
||||||
URL.revokeObjectURL(blobUrlRef.current);
|
|
||||||
}
|
|
||||||
blobUrlRef.current = URL.createObjectURL(blob);
|
|
||||||
if (newWindow) newWindow.location.href = blobUrlRef.current;
|
|
||||||
} catch {
|
|
||||||
newWindow?.close();
|
|
||||||
alert.error("Chyba připojení");
|
|
||||||
} finally {
|
|
||||||
setPdfLoading(null);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Only show the full-page skeleton on the very first load; on subsequent
|
// Only show the full-page skeleton on the very first load; on subsequent
|
||||||
// refetches (filter/customer/tab/page change) keep the table visible (the
|
// refetches (filter/customer/tab/page change) keep the table visible (the
|
||||||
// Card dims via isFetching) so it doesn't flash.
|
// Card dims via isFetching) so it doesn't flash.
|
||||||
@@ -573,6 +519,7 @@ export default function Offers() {
|
|||||||
header: "Číslo",
|
header: "Číslo",
|
||||||
width: "13%",
|
width: "13%",
|
||||||
sortKey: "quotation_number",
|
sortKey: "quotation_number",
|
||||||
|
mono: true,
|
||||||
render: (q) => (
|
render: (q) => (
|
||||||
<Box
|
<Box
|
||||||
component={RouterLink}
|
component={RouterLink}
|
||||||
@@ -597,7 +544,7 @@ export default function Offers() {
|
|||||||
{
|
{
|
||||||
key: "customer_name",
|
key: "customer_name",
|
||||||
header: "Zákazník",
|
header: "Zákazník",
|
||||||
width: "13%",
|
width: "16%",
|
||||||
render: (q) => q.customer_name || "—",
|
render: (q) => q.customer_name || "—",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -637,17 +584,10 @@ export default function Offers() {
|
|||||||
);
|
);
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
|
||||||
key: "currency",
|
|
||||||
header: "Měna",
|
|
||||||
width: "7%",
|
|
||||||
sortKey: "currency",
|
|
||||||
render: (q) => <StatusChip label={q.currency} color="default" />,
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
key: "total",
|
key: "total",
|
||||||
header: "Celkem",
|
header: "Celkem",
|
||||||
width: "11%",
|
width: "12%",
|
||||||
align: "right",
|
align: "right",
|
||||||
mono: true,
|
mono: true,
|
||||||
bold: true,
|
bold: true,
|
||||||
@@ -693,7 +633,9 @@ export default function Offers() {
|
|||||||
{OrderViewIcon}
|
{OrderViewIcon}
|
||||||
</IconButton>
|
</IconButton>
|
||||||
) : (
|
) : (
|
||||||
!readOnly &&
|
// Only active offers can spawn an order — the server transition
|
||||||
|
// guard rejects drafts/invalidated with a 400.
|
||||||
|
q.status === "active" &&
|
||||||
hasPermission("orders.create") && (
|
hasPermission("orders.create") && (
|
||||||
<IconButton
|
<IconButton
|
||||||
size="small"
|
size="small"
|
||||||
@@ -714,15 +656,20 @@ export default function Offers() {
|
|||||||
</IconButton>
|
</IconButton>
|
||||||
)
|
)
|
||||||
)}
|
)}
|
||||||
{hasPermission("offers.view") && (
|
{/* Drafts have no number yet — /file 404s for them. */}
|
||||||
|
{hasPermission("offers.view") && q.quotation_number && (
|
||||||
<IconButton
|
<IconButton
|
||||||
size="small"
|
size="small"
|
||||||
onClick={() => handlePdf(q)}
|
onClick={() => openPdf(q.id, `${API_BASE}/offers/${q.id}/file`)}
|
||||||
aria-label="Zobrazit nabídku"
|
aria-label="Zobrazit nabídku"
|
||||||
title="Zobrazit nabídku"
|
title="Zobrazit nabídku"
|
||||||
disabled={pdfLoading === q.id}
|
disabled={pdfLoadingId === q.id}
|
||||||
>
|
>
|
||||||
{pdfLoading === q.id ? <CircularProgress size={16} /> : PdfIcon}
|
{pdfLoadingId === q.id ? (
|
||||||
|
<CircularProgress size={16} />
|
||||||
|
) : (
|
||||||
|
PdfIcon
|
||||||
|
)}
|
||||||
</IconButton>
|
</IconButton>
|
||||||
)}
|
)}
|
||||||
{hasPermission("offers.delete") && (
|
{hasPermission("offers.delete") && (
|
||||||
@@ -906,7 +853,7 @@ export default function Offers() {
|
|||||||
fontSize: "0.9rem",
|
fontSize: "0.9rem",
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<span>Celkem:</span>
|
<span>Celkem bez DPH:</span>
|
||||||
<Box
|
<Box
|
||||||
component="span"
|
component="span"
|
||||||
sx={{ fontWeight: 700, color: "text.primary" }}
|
sx={{ fontWeight: 700, color: "text.primary" }}
|
||||||
@@ -927,10 +874,10 @@ export default function Offers() {
|
|||||||
onClose={() => setDeleteConfirm({ show: false, quotation: null })}
|
onClose={() => setDeleteConfirm({ show: false, quotation: null })}
|
||||||
onConfirm={handleDelete}
|
onConfirm={handleDelete}
|
||||||
title="Smazat nabídku"
|
title="Smazat nabídku"
|
||||||
message={`Opravdu chcete smazat nabídku "${deleteConfirm.quotation?.quotation_number}"? Budou smazány i všechny položky a sekce. Tato akce je nevratná.`}
|
message={`Opravdu chcete smazat nabídku „${documentNumberLabel(deleteConfirm.quotation?.quotation_number)}“? Budou smazány i všechny položky a sekce. Tato akce je nevratná.`}
|
||||||
confirmText="Smazat"
|
confirmText="Smazat"
|
||||||
confirmVariant="danger"
|
confirmVariant="danger"
|
||||||
loading={deleting}
|
loading={deleteOfferMutation.isPending}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<ConfirmDialog
|
<ConfirmDialog
|
||||||
@@ -938,7 +885,7 @@ export default function Offers() {
|
|||||||
onClose={() => setInvalidateConfirm({ show: false, quotation: null })}
|
onClose={() => setInvalidateConfirm({ show: false, quotation: null })}
|
||||||
onConfirm={handleInvalidate}
|
onConfirm={handleInvalidate}
|
||||||
title="Zneplatnit nabídku"
|
title="Zneplatnit nabídku"
|
||||||
message={`Opravdu chcete zneplatnit nabídku "${invalidateConfirm.quotation?.quotation_number}"? Nabídka bude pouze pro čtení a nepůjde upravovat.`}
|
message={`Opravdu chcete zneplatnit nabídku „${documentNumberLabel(invalidateConfirm.quotation?.quotation_number)}“? Nabídka bude pouze pro čtení a nepůjde upravovat.`}
|
||||||
confirmText="Zneplatnit"
|
confirmText="Zneplatnit"
|
||||||
confirmVariant="danger"
|
confirmVariant="danger"
|
||||||
loading={invalidating}
|
loading={invalidating}
|
||||||
|
|||||||
@@ -198,7 +198,8 @@ export default function OffersCustomers() {
|
|||||||
? `${API_BASE}/customers/${editingCustomer.id}`
|
? `${API_BASE}/customers/${editingCustomer.id}`
|
||||||
: `${API_BASE}/customers`,
|
: `${API_BASE}/customers`,
|
||||||
method: () => (editingCustomer ? "PUT" : "POST"),
|
method: () => (editingCustomer ? "PUT" : "POST"),
|
||||||
invalidate: ["offer-customers", "offers"],
|
// Broad domain key — covers the ["offers","customers"] picker query too.
|
||||||
|
invalidate: ["offers"],
|
||||||
onSuccess: (data) => {
|
onSuccess: (data) => {
|
||||||
closeModal();
|
closeModal();
|
||||||
setTimeout(
|
setTimeout(
|
||||||
@@ -214,7 +215,8 @@ export default function OffersCustomers() {
|
|||||||
>({
|
>({
|
||||||
url: (id) => `${API_BASE}/customers/${id}`,
|
url: (id) => `${API_BASE}/customers/${id}`,
|
||||||
method: () => "DELETE",
|
method: () => "DELETE",
|
||||||
invalidate: ["offer-customers", "offers"],
|
// Broad domain key — covers the ["offers","customers"] picker query too.
|
||||||
|
invalidate: ["offers"],
|
||||||
onSuccess: (data) => {
|
onSuccess: (data) => {
|
||||||
setDeleteConfirm({ show: false, customer: null });
|
setDeleteConfirm({ show: false, customer: null });
|
||||||
alert.success(data?.message || "Zákazník byl smazán");
|
alert.success(data?.message || "Zákazník byl smazán");
|
||||||
|
|||||||
@@ -144,9 +144,10 @@ export default function OrderDetail() {
|
|||||||
return () => window.removeEventListener("beforeunload", handler);
|
return () => window.removeEventListener("beforeunload", handler);
|
||||||
}, [isDirty]);
|
}, [isDirty]);
|
||||||
|
|
||||||
|
// NET only — received orders are not tax documents (no VAT on them).
|
||||||
const totals = useMemo(() => {
|
const totals = useMemo(() => {
|
||||||
if (!order?.items) return { subtotal: 0, vatAmount: 0, total: 0 };
|
if (!order?.items) return { total: 0 };
|
||||||
const subtotal = order.items.reduce((sum, item) => {
|
const total = order.items.reduce((sum, item) => {
|
||||||
if (Number(item.is_included_in_total)) {
|
if (Number(item.is_included_in_total)) {
|
||||||
return (
|
return (
|
||||||
sum + (Number(item.quantity) || 0) * (Number(item.unit_price) || 0)
|
sum + (Number(item.quantity) || 0) * (Number(item.unit_price) || 0)
|
||||||
@@ -154,10 +155,7 @@ export default function OrderDetail() {
|
|||||||
}
|
}
|
||||||
return sum;
|
return sum;
|
||||||
}, 0);
|
}, 0);
|
||||||
const vatAmount = Number(order.apply_vat)
|
return { total };
|
||||||
? subtotal * ((Number(order.vat_rate) || 0) / 100)
|
|
||||||
: 0;
|
|
||||||
return { subtotal, vatAmount, total: subtotal + vatAmount };
|
|
||||||
}, [order]);
|
}, [order]);
|
||||||
|
|
||||||
const statusMutation = useApiMutation<{ status: string }, unknown>({
|
const statusMutation = useApiMutation<{ status: string }, unknown>({
|
||||||
@@ -236,14 +234,12 @@ export default function OrderDetail() {
|
|||||||
|
|
||||||
const handleGenerateConfirmation = async (
|
const handleGenerateConfirmation = async (
|
||||||
lang: string,
|
lang: string,
|
||||||
applyVat: boolean,
|
|
||||||
customItems?: Array<{
|
customItems?: Array<{
|
||||||
description: string;
|
description: string;
|
||||||
quantity: number;
|
quantity: number;
|
||||||
unit: string;
|
unit: string;
|
||||||
unit_price: number;
|
unit_price: number;
|
||||||
is_included_in_total: boolean;
|
is_included_in_total: boolean;
|
||||||
vat_rate: number;
|
|
||||||
}>,
|
}>,
|
||||||
) => {
|
) => {
|
||||||
setConfirmationLoading(true);
|
setConfirmationLoading(true);
|
||||||
@@ -253,7 +249,7 @@ export default function OrderDetail() {
|
|||||||
{
|
{
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
body: JSON.stringify({ lang, applyVat, items: customItems }),
|
body: JSON.stringify({ lang, items: customItems }),
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
@@ -625,7 +621,7 @@ export default function OrderDetail() {
|
|||||||
empty={<EmptyState title="Žádné položky." />}
|
empty={<EmptyState title="Žádné položky." />}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
{/* Totals */}
|
{/* Totals (NET only — orders are not tax documents) */}
|
||||||
<Box
|
<Box
|
||||||
sx={{
|
sx={{
|
||||||
mt: 2,
|
mt: 2,
|
||||||
@@ -636,30 +632,6 @@ export default function OrderDetail() {
|
|||||||
gap: 0.5,
|
gap: 0.5,
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<Box sx={{ display: "flex", justifyContent: "space-between" }}>
|
|
||||||
<Typography variant="body2" color="text.secondary">
|
|
||||||
Mezisoučet:
|
|
||||||
</Typography>
|
|
||||||
<Typography
|
|
||||||
variant="body2"
|
|
||||||
sx={{ fontFamily: "'DM Mono', Menlo, monospace" }}
|
|
||||||
>
|
|
||||||
{formatCurrency(totals.subtotal, order.currency)}
|
|
||||||
</Typography>
|
|
||||||
</Box>
|
|
||||||
{Number(order.apply_vat) > 0 && (
|
|
||||||
<Box sx={{ display: "flex", justifyContent: "space-between" }}>
|
|
||||||
<Typography variant="body2" color="text.secondary">
|
|
||||||
DPH ({order.vat_rate}%):
|
|
||||||
</Typography>
|
|
||||||
<Typography
|
|
||||||
variant="body2"
|
|
||||||
sx={{ fontFamily: "'DM Mono', Menlo, monospace" }}
|
|
||||||
>
|
|
||||||
{formatCurrency(totals.vatAmount, order.currency)}
|
|
||||||
</Typography>
|
|
||||||
</Box>
|
|
||||||
)}
|
|
||||||
<Box
|
<Box
|
||||||
sx={{
|
sx={{
|
||||||
display: "flex",
|
display: "flex",
|
||||||
@@ -671,7 +643,7 @@ export default function OrderDetail() {
|
|||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<Typography variant="body2" sx={{ fontWeight: 700 }}>
|
<Typography variant="body2" sx={{ fontWeight: 700 }}>
|
||||||
Celkem k úhradě:
|
Celkem bez DPH:
|
||||||
</Typography>
|
</Typography>
|
||||||
<Typography
|
<Typography
|
||||||
variant="body2"
|
variant="body2"
|
||||||
@@ -829,11 +801,8 @@ export default function OrderDetail() {
|
|||||||
unit: it.unit || "",
|
unit: it.unit || "",
|
||||||
unit_price: Number(it.unit_price) || 0,
|
unit_price: Number(it.unit_price) || 0,
|
||||||
is_included_in_total: Number(it.is_included_in_total) !== 0,
|
is_included_in_total: Number(it.is_included_in_total) !== 0,
|
||||||
vat_rate: Number(order.vat_rate) || 21,
|
|
||||||
}))}
|
}))}
|
||||||
orderNumber={order.order_number}
|
orderNumber={order.order_number}
|
||||||
defaultVatRate={Number(order.vat_rate) || 21}
|
|
||||||
applyVat={!!order.apply_vat}
|
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
</PageEnter>
|
</PageEnter>
|
||||||
|
|||||||
@@ -34,6 +34,8 @@ import {
|
|||||||
CheckboxField,
|
CheckboxField,
|
||||||
LoadingState,
|
LoadingState,
|
||||||
PageEnter,
|
PageEnter,
|
||||||
|
Tabs,
|
||||||
|
type TabDef,
|
||||||
type DataColumn,
|
type DataColumn,
|
||||||
} from "../ui";
|
} from "../ui";
|
||||||
|
|
||||||
@@ -54,6 +56,12 @@ const STATUS_COLORS: Record<
|
|||||||
zruseny: "default",
|
zruseny: "default",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Status filter tabs (mirrors the offers page): "" = all.
|
||||||
|
const STATUS_TABS: TabDef[] = [
|
||||||
|
{ value: "", label: "Všechny" },
|
||||||
|
...Object.entries(STATUS_LABELS).map(([value, label]) => ({ value, label })),
|
||||||
|
];
|
||||||
|
|
||||||
interface Project {
|
interface Project {
|
||||||
id: number;
|
id: number;
|
||||||
project_number: string;
|
project_number: string;
|
||||||
@@ -120,6 +128,7 @@ export default function Projects() {
|
|||||||
const [search, setSearch] = useState("");
|
const [search, setSearch] = useState("");
|
||||||
const debouncedSearch = useDebounce(search, 300);
|
const debouncedSearch = useDebounce(search, 300);
|
||||||
const [page, setPage] = useState(1);
|
const [page, setPage] = useState(1);
|
||||||
|
const [statusFilter, setStatusFilter] = useState("");
|
||||||
const [deleteTarget, setDeleteTarget] = useState<Project | null>(null);
|
const [deleteTarget, setDeleteTarget] = useState<Project | null>(null);
|
||||||
const [deleteFiles, setDeleteFiles] = useState(false);
|
const [deleteFiles, setDeleteFiles] = useState(false);
|
||||||
|
|
||||||
@@ -230,7 +239,13 @@ export default function Projects() {
|
|||||||
isPending,
|
isPending,
|
||||||
isFetching,
|
isFetching,
|
||||||
} = usePaginatedQuery<Project>(
|
} = usePaginatedQuery<Project>(
|
||||||
projectListOptions({ search: debouncedSearch, sort, order, page }),
|
projectListOptions({
|
||||||
|
search: debouncedSearch,
|
||||||
|
sort,
|
||||||
|
order,
|
||||||
|
page,
|
||||||
|
status: statusFilter || undefined,
|
||||||
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!hasPermission("projects.view")) return <Forbidden />;
|
if (!hasPermission("projects.view")) return <Forbidden />;
|
||||||
@@ -372,6 +387,31 @@ export default function Projects() {
|
|||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
|
// Per-status row tints (mirrors the offers list): LOW-ALPHA washes via the
|
||||||
|
// palette channel vars in the badge's color — never solid `.light` fills
|
||||||
|
// (invisible white text in dark mode). Cancelled rows are faded/muted like
|
||||||
|
// invalidated offers.
|
||||||
|
const rowSx = (p: Project) => {
|
||||||
|
if (p.status === "dokonceny") {
|
||||||
|
return {
|
||||||
|
backgroundColor: "rgba(var(--mui-palette-info-mainChannel) / 0.12)",
|
||||||
|
"&:hover": {
|
||||||
|
backgroundColor: "rgba(var(--mui-palette-info-mainChannel) / 0.18)",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if (p.status === "zruseny") {
|
||||||
|
return {
|
||||||
|
opacity: 0.6,
|
||||||
|
"& td": { color: "var(--mui-palette-text-secondary)" },
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return {};
|
||||||
|
};
|
||||||
|
|
||||||
|
// Search/status filter active → empty means "nothing matches" (no CTA).
|
||||||
|
const isFiltered = !!debouncedSearch || !!statusFilter;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<PageEnter>
|
<PageEnter>
|
||||||
<Box
|
<Box
|
||||||
@@ -392,6 +432,17 @@ export default function Projects() {
|
|||||||
)}
|
)}
|
||||||
</Box>
|
</Box>
|
||||||
|
|
||||||
|
<Box sx={{ display: "flex", justifyContent: "center", mb: 2.5 }}>
|
||||||
|
<Tabs
|
||||||
|
value={statusFilter}
|
||||||
|
onChange={(v) => {
|
||||||
|
setStatusFilter(v);
|
||||||
|
setPage(1);
|
||||||
|
}}
|
||||||
|
tabs={STATUS_TABS}
|
||||||
|
/>
|
||||||
|
</Box>
|
||||||
|
|
||||||
<FilterBar>
|
<FilterBar>
|
||||||
<Box sx={{ flex: "1 1 320px" }}>
|
<Box sx={{ flex: "1 1 320px" }}>
|
||||||
<TextField
|
<TextField
|
||||||
@@ -411,19 +462,31 @@ export default function Projects() {
|
|||||||
columns={columns}
|
columns={columns}
|
||||||
rows={projects}
|
rows={projects}
|
||||||
rowKey={(p) => p.id}
|
rowKey={(p) => p.id}
|
||||||
|
rowSx={rowSx}
|
||||||
sortBy={sort}
|
sortBy={sort}
|
||||||
sortDir={order}
|
sortDir={order}
|
||||||
onSort={handleSort}
|
onSort={handleSort}
|
||||||
empty={
|
empty={
|
||||||
<Box sx={{ textAlign: "center", py: 6 }}>
|
isFiltered ? (
|
||||||
<Typography color="text.secondary" gutterBottom>
|
<Box sx={{ textAlign: "center", py: 6 }}>
|
||||||
Zatím nejsou žádné projekty.
|
<Typography color="text.secondary" gutterBottom>
|
||||||
</Typography>
|
Žádné projekty neodpovídají filtru.
|
||||||
<Typography variant="body2" color="text.secondary">
|
</Typography>
|
||||||
Projekty vznikají z objednávek nebo je můžete vytvořit ručně
|
<Typography variant="body2" color="text.secondary">
|
||||||
tlačítkem „Přidat projekt".
|
Zkuste změnit filtr nebo hledaný výraz.
|
||||||
</Typography>
|
</Typography>
|
||||||
</Box>
|
</Box>
|
||||||
|
) : (
|
||||||
|
<Box sx={{ textAlign: "center", py: 6 }}>
|
||||||
|
<Typography color="text.secondary" gutterBottom>
|
||||||
|
Zatím nejsou žádné projekty.
|
||||||
|
</Typography>
|
||||||
|
<Typography variant="body2" color="text.secondary">
|
||||||
|
Projekty vznikají z objednávek nebo je můžete vytvořit ručně
|
||||||
|
tlačítkem „Přidat projekt".
|
||||||
|
</Typography>
|
||||||
|
</Box>
|
||||||
|
)
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
<Pagination
|
<Pagination
|
||||||
|
|||||||
@@ -184,8 +184,6 @@ export default function OrdersReceived({
|
|||||||
customer_id: "",
|
customer_id: "",
|
||||||
customer_order_number: "",
|
customer_order_number: "",
|
||||||
currency: "CZK",
|
currency: "CZK",
|
||||||
vat_rate: "21",
|
|
||||||
apply_vat: true,
|
|
||||||
scope_title: "",
|
scope_title: "",
|
||||||
scope_description: "",
|
scope_description: "",
|
||||||
notes: "",
|
notes: "",
|
||||||
@@ -219,8 +217,6 @@ export default function OrdersReceived({
|
|||||||
customer_id: "",
|
customer_id: "",
|
||||||
customer_order_number: "",
|
customer_order_number: "",
|
||||||
currency: "CZK",
|
currency: "CZK",
|
||||||
vat_rate: "21",
|
|
||||||
apply_vat: true,
|
|
||||||
scope_title: "",
|
scope_title: "",
|
||||||
scope_description: "",
|
scope_description: "",
|
||||||
notes: "",
|
notes: "",
|
||||||
@@ -255,8 +251,6 @@ export default function OrdersReceived({
|
|||||||
fd.append("customer_id", createForm.customer_id);
|
fd.append("customer_id", createForm.customer_id);
|
||||||
fd.append("customer_order_number", createForm.customer_order_number);
|
fd.append("customer_order_number", createForm.customer_order_number);
|
||||||
fd.append("currency", createForm.currency);
|
fd.append("currency", createForm.currency);
|
||||||
fd.append("vat_rate", createForm.vat_rate);
|
|
||||||
fd.append("apply_vat", createForm.apply_vat ? "1" : "0");
|
|
||||||
fd.append("scope_title", createForm.scope_title);
|
fd.append("scope_title", createForm.scope_title);
|
||||||
fd.append("scope_description", createForm.scope_description);
|
fd.append("scope_description", createForm.scope_description);
|
||||||
fd.append("notes", createForm.notes);
|
fd.append("notes", createForm.notes);
|
||||||
@@ -274,8 +268,6 @@ export default function OrdersReceived({
|
|||||||
: null,
|
: null,
|
||||||
customer_order_number: createForm.customer_order_number,
|
customer_order_number: createForm.customer_order_number,
|
||||||
currency: createForm.currency,
|
currency: createForm.currency,
|
||||||
vat_rate: createForm.vat_rate,
|
|
||||||
apply_vat: createForm.apply_vat,
|
|
||||||
scope_title: createForm.scope_title,
|
scope_title: createForm.scope_title,
|
||||||
scope_description: createForm.scope_description,
|
scope_description: createForm.scope_description,
|
||||||
notes: createForm.notes,
|
notes: createForm.notes,
|
||||||
@@ -571,7 +563,7 @@ export default function OrdersReceived({
|
|||||||
fontSize: "0.9rem",
|
fontSize: "0.9rem",
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<span>Celkem:</span>
|
<span>Celkem bez DPH:</span>
|
||||||
<Box
|
<Box
|
||||||
component="span"
|
component="span"
|
||||||
sx={{ fontWeight: 700, color: "text.primary" }}
|
sx={{ fontWeight: 700, color: "text.primary" }}
|
||||||
@@ -669,19 +661,6 @@ export default function OrdersReceived({
|
|||||||
/>
|
/>
|
||||||
</Field>
|
</Field>
|
||||||
</Box>
|
</Box>
|
||||||
<Box sx={{ flex: "1 1 200px" }}>
|
|
||||||
<Field label="Sazba DPH (%)">
|
|
||||||
<TextField
|
|
||||||
type="number"
|
|
||||||
inputMode="numeric"
|
|
||||||
value={createForm.vat_rate}
|
|
||||||
onChange={(e) =>
|
|
||||||
setCreateForm({ ...createForm, vat_rate: e.target.value })
|
|
||||||
}
|
|
||||||
slotProps={{ htmlInput: { min: 0 } }}
|
|
||||||
/>
|
|
||||||
</Field>
|
|
||||||
</Box>
|
|
||||||
</Box>
|
</Box>
|
||||||
|
|
||||||
<Box sx={{ display: "flex", gap: 2, flexWrap: "wrap" }}>
|
<Box sx={{ display: "flex", gap: 2, flexWrap: "wrap" }}>
|
||||||
@@ -757,12 +736,6 @@ export default function OrdersReceived({
|
|||||||
/>
|
/>
|
||||||
</Field>
|
</Field>
|
||||||
|
|
||||||
<CheckboxField
|
|
||||||
label="Účtovat DPH"
|
|
||||||
checked={createForm.apply_vat}
|
|
||||||
onChange={(v) => setCreateForm({ ...createForm, apply_vat: v })}
|
|
||||||
/>
|
|
||||||
|
|
||||||
<CheckboxField
|
<CheckboxField
|
||||||
label="Vytvořit propojený projekt"
|
label="Vytvořit propojený projekt"
|
||||||
checked={createForm.create_project}
|
checked={createForm.create_project}
|
||||||
|
|||||||
@@ -137,7 +137,9 @@ export default function WarehouseSuppliers() {
|
|||||||
url: () =>
|
url: () =>
|
||||||
editingSupplier ? `${API_BASE}/${editingSupplier.id}` : API_BASE,
|
editingSupplier ? `${API_BASE}/${editingSupplier.id}` : API_BASE,
|
||||||
method: () => (editingSupplier ? "PUT" : "POST"),
|
method: () => (editingSupplier ? "PUT" : "POST"),
|
||||||
invalidate: ["warehouse"],
|
// issued-orders included: the PO form's supplier picker caches under
|
||||||
|
// ["issued-orders","suppliers"] and must see supplier CRUD immediately.
|
||||||
|
invalidate: ["warehouse", "issued-orders"],
|
||||||
onSuccess: (data) => {
|
onSuccess: (data) => {
|
||||||
setShowModal(false);
|
setShowModal(false);
|
||||||
alert.success(data?.message || "Dodavatel byl uložen");
|
alert.success(data?.message || "Dodavatel byl uložen");
|
||||||
@@ -147,7 +149,9 @@ export default function WarehouseSuppliers() {
|
|||||||
const deleteMutation = useApiMutation<number, { message?: string }>({
|
const deleteMutation = useApiMutation<number, { message?: string }>({
|
||||||
url: (id) => `${API_BASE}/${id}`,
|
url: (id) => `${API_BASE}/${id}`,
|
||||||
method: () => "DELETE",
|
method: () => "DELETE",
|
||||||
invalidate: ["warehouse"],
|
// issued-orders included: the PO form's supplier picker caches under
|
||||||
|
// ["issued-orders","suppliers"] and must see supplier CRUD immediately.
|
||||||
|
invalidate: ["warehouse", "issued-orders"],
|
||||||
onSuccess: (data) => {
|
onSuccess: (data) => {
|
||||||
setDeactivateConfirm({ show: false, supplier: null });
|
setDeactivateConfirm({ show: false, supplier: null });
|
||||||
alert.success(data?.message || "Dodavatel byl smazán");
|
alert.success(data?.message || "Dodavatel byl smazán");
|
||||||
@@ -163,7 +167,9 @@ export default function WarehouseSuppliers() {
|
|||||||
>({
|
>({
|
||||||
url: ({ id }) => `${API_BASE}/${id}`,
|
url: ({ id }) => `${API_BASE}/${id}`,
|
||||||
method: () => "PUT",
|
method: () => "PUT",
|
||||||
invalidate: ["warehouse"],
|
// issued-orders included: the PO form's supplier picker caches under
|
||||||
|
// ["issued-orders","suppliers"] and must see supplier CRUD immediately.
|
||||||
|
invalidate: ["warehouse", "issued-orders"],
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!hasPermission("warehouse.manage")) return <Forbidden />;
|
if (!hasPermission("warehouse.manage")) return <Forbidden />;
|
||||||
|
|||||||
88
src/admin/ui/SupplierPicker.tsx
Normal file
88
src/admin/ui/SupplierPicker.tsx
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
import Autocomplete, { createFilterOptions } from "@mui/material/Autocomplete";
|
||||||
|
import MuiTextField from "@mui/material/TextField";
|
||||||
|
import Box from "@mui/material/Box";
|
||||||
|
import Typography from "@mui/material/Typography";
|
||||||
|
import type { Supplier } from "../lib/queries/issued-orders";
|
||||||
|
|
||||||
|
interface SupplierPickerProps {
|
||||||
|
suppliers: Supplier[];
|
||||||
|
/** Selected supplier id (FK), or null when none chosen. */
|
||||||
|
value: number | null;
|
||||||
|
onChange: (id: number | null) => void;
|
||||||
|
disabled?: boolean;
|
||||||
|
/**
|
||||||
|
* When set, draws the input in its error state (red border). The error TEXT
|
||||||
|
* is owned by the surrounding <Field> wrapper, so we deliberately do NOT
|
||||||
|
* render helperText here to avoid duplicating the message.
|
||||||
|
*/
|
||||||
|
error?: string;
|
||||||
|
placeholder?: string;
|
||||||
|
/** Optional id forwarded by <Field> for label association. */
|
||||||
|
id?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Search matches the supplier name AND the IČO, so typing either finds the
|
||||||
|
// record. Match on a stringified "name + ico" so MUI's default
|
||||||
|
// case-insensitive substring filtering covers both.
|
||||||
|
const filterSuppliers = createFilterOptions<Supplier>({
|
||||||
|
stringify: (s) => `${s.name} ${s.ico ?? ""}`,
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Searchable supplier picker for issued orders (purchase orders) — a
|
||||||
|
* controlled MUI Autocomplete over the sklad_suppliers lookup list, bound to
|
||||||
|
* the supplier id. Modeled on CustomerPicker (offers/invoices keep that one);
|
||||||
|
* renders bare (no label/error text); wrap it in the page's
|
||||||
|
* <Field label error> for the label + error line.
|
||||||
|
*/
|
||||||
|
export default function SupplierPicker({
|
||||||
|
suppliers,
|
||||||
|
value,
|
||||||
|
onChange,
|
||||||
|
disabled,
|
||||||
|
error,
|
||||||
|
placeholder,
|
||||||
|
id,
|
||||||
|
}: SupplierPickerProps) {
|
||||||
|
const selected = suppliers.find((s) => s.id === value) ?? null;
|
||||||
|
return (
|
||||||
|
<Autocomplete<Supplier>
|
||||||
|
options={suppliers}
|
||||||
|
value={selected}
|
||||||
|
onChange={(_, opt) => onChange(opt ? opt.id : null)}
|
||||||
|
getOptionLabel={(s) => s?.name ?? ""}
|
||||||
|
isOptionEqualToValue={(o, v) => o.id === v.id}
|
||||||
|
filterOptions={filterSuppliers}
|
||||||
|
disabled={disabled}
|
||||||
|
size="small"
|
||||||
|
fullWidth
|
||||||
|
autoHighlight
|
||||||
|
renderOption={(props, s) => {
|
||||||
|
const { key, ...rest } = props as typeof props & { key: string };
|
||||||
|
return (
|
||||||
|
<Box component="li" key={key} {...rest}>
|
||||||
|
<Box>
|
||||||
|
{s.name}
|
||||||
|
{s.ico && (
|
||||||
|
<Typography
|
||||||
|
variant="caption"
|
||||||
|
sx={{ display: "block", color: "text.secondary" }}
|
||||||
|
>
|
||||||
|
IČ: {s.ico}
|
||||||
|
</Typography>
|
||||||
|
)}
|
||||||
|
</Box>
|
||||||
|
</Box>
|
||||||
|
);
|
||||||
|
}}
|
||||||
|
renderInput={(params) => (
|
||||||
|
<MuiTextField
|
||||||
|
{...params}
|
||||||
|
id={id}
|
||||||
|
placeholder={placeholder ?? "Vyberte dodavatele…"}
|
||||||
|
error={!!error}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -10,6 +10,7 @@ export { Field, SwitchField } from "./Field";
|
|||||||
export { default as Select } from "./Select";
|
export { default as Select } from "./Select";
|
||||||
export type { SelectOption } from "./Select";
|
export type { SelectOption } from "./Select";
|
||||||
export { default as CustomerPicker } from "./CustomerPicker";
|
export { default as CustomerPicker } from "./CustomerPicker";
|
||||||
|
export { default as SupplierPicker } from "./SupplierPicker";
|
||||||
export { default as StatusChip } from "./StatusChip";
|
export { default as StatusChip } from "./StatusChip";
|
||||||
export { CheckboxField } from "./Checkbox";
|
export { CheckboxField } from "./Checkbox";
|
||||||
export { default as Alert } from "./Alert";
|
export { default as Alert } from "./Alert";
|
||||||
|
|||||||
@@ -11,15 +11,16 @@ export default async function dashboardRoutes(
|
|||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
fastify.get("/", { preHandler: requireAuth }, async (request, reply) => {
|
fastify.get("/", { preHandler: requireAuth }, async (request, reply) => {
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
|
// shift_date is @db.Date: Prisma truncates a filter Date to its UTC date
|
||||||
|
// part, so these boundaries MUST be UTC-midnight instants of the LOCAL
|
||||||
|
// (Prague) calendar day. A local-midnight Date (new Date(y, m, d)) is
|
||||||
|
// 22:00/23:00 UTC of the PREVIOUS day and silently shifts the window to
|
||||||
|
// [yesterday, today) — the "Docházka dnes" card then matches zero rows.
|
||||||
const todayStart = new Date(
|
const todayStart = new Date(
|
||||||
now.getFullYear(),
|
Date.UTC(now.getFullYear(), now.getMonth(), now.getDate()),
|
||||||
now.getMonth(),
|
|
||||||
now.getDate(),
|
|
||||||
);
|
);
|
||||||
const todayEnd = new Date(
|
const todayEnd = new Date(
|
||||||
now.getFullYear(),
|
Date.UTC(now.getFullYear(), now.getMonth(), now.getDate() + 1),
|
||||||
now.getMonth(),
|
|
||||||
now.getDate() + 1,
|
|
||||||
);
|
);
|
||||||
const monthStart = new Date(now.getFullYear(), now.getMonth(), 1);
|
const monthStart = new Date(now.getFullYear(), now.getMonth(), 1);
|
||||||
const monthEnd = new Date(now.getFullYear(), now.getMonth() + 1, 1);
|
const monthEnd = new Date(now.getFullYear(), now.getMonth() + 1, 1);
|
||||||
|
|||||||
@@ -2,12 +2,20 @@ import { FastifyInstance } from "fastify";
|
|||||||
import QRCode from "qrcode";
|
import QRCode from "qrcode";
|
||||||
import prisma from "../../config/database";
|
import prisma from "../../config/database";
|
||||||
import { requirePermission } from "../../middleware/auth";
|
import { requirePermission } from "../../middleware/auth";
|
||||||
import { localDateCzStr } from "../../utils/date";
|
|
||||||
import { nasInvoicesManager } from "../../services/nas-financials-manager";
|
import { nasInvoicesManager } from "../../services/nas-financials-manager";
|
||||||
import { htmlToPdf } from "../../utils/html-to-pdf";
|
import { htmlToPdf } from "../../utils/html-to-pdf";
|
||||||
import { getRate } from "../../services/exchange-rates";
|
import { getRate } from "../../services/exchange-rates";
|
||||||
import { localDateStr } from "../../utils/date";
|
import { localDateStr } from "../../utils/date";
|
||||||
import { parseId, success } from "../../utils/response";
|
import { parseId, success } from "../../utils/response";
|
||||||
|
import {
|
||||||
|
formatDate,
|
||||||
|
formatNum,
|
||||||
|
escapeHtml,
|
||||||
|
cleanQuillHtml,
|
||||||
|
buildQuillIndentCss,
|
||||||
|
logoDataUriFromSettings,
|
||||||
|
buildPdfHeaderTemplate,
|
||||||
|
} from "../../utils/pdf-shared";
|
||||||
import createDOMPurify from "dompurify";
|
import createDOMPurify from "dompurify";
|
||||||
import { JSDOM } from "jsdom";
|
import { JSDOM } from "jsdom";
|
||||||
|
|
||||||
@@ -16,62 +24,6 @@ const DOMPurify = createDOMPurify(window);
|
|||||||
|
|
||||||
/* ── Helpers ─────────────────────────────────────────────────────── */
|
/* ── Helpers ─────────────────────────────────────────────────────── */
|
||||||
|
|
||||||
function formatDate(date: Date | string | null | undefined): string {
|
|
||||||
if (!date) return "";
|
|
||||||
const d = new Date(date);
|
|
||||||
if (isNaN(d.getTime())) return String(date);
|
|
||||||
return localDateCzStr(d);
|
|
||||||
}
|
|
||||||
|
|
||||||
function formatNum(n: number, decimals = 2): string {
|
|
||||||
const abs = Math.abs(n);
|
|
||||||
const fixed = abs.toFixed(decimals);
|
|
||||||
const [intPart, decPart] = fixed.split(".");
|
|
||||||
const withSep = intPart.replace(/\B(?=(\d{3})+(?!\d))/g, "\u00A0");
|
|
||||||
const result = decPart ? `${withSep},${decPart}` : withSep;
|
|
||||||
return n < 0 ? `-${result}` : result;
|
|
||||||
}
|
|
||||||
|
|
||||||
function escapeHtml(str: string | null | undefined): string {
|
|
||||||
if (!str) return "";
|
|
||||||
return str
|
|
||||||
.replace(/&/g, "&")
|
|
||||||
.replace(/</g, "<")
|
|
||||||
.replace(/>/g, ">")
|
|
||||||
.replace(/"/g, """);
|
|
||||||
}
|
|
||||||
|
|
||||||
function cleanQuillHtml(html: string | null | undefined): string {
|
|
||||||
if (!html) return "";
|
|
||||||
let s = html;
|
|
||||||
s = s.replace(
|
|
||||||
/<(script|iframe|object|embed|style|link|meta|base|form|input|textarea|button|select|svg|math)[^>]*>[\s\S]*?<\/\1>/gi,
|
|
||||||
"",
|
|
||||||
);
|
|
||||||
s = s.replace(
|
|
||||||
/<(script|iframe|object|embed|style|link|meta|base|form|input|textarea|button|select|svg|math)[^>]*\/?>/gi,
|
|
||||||
"",
|
|
||||||
);
|
|
||||||
s = s.replace(/\s+on\w+\s*=\s*("[^"]*"|'[^']*'|[^\s>]*)/gi, "");
|
|
||||||
s = s.replace(/\s+on\w+\s*=\s*[^\s>]*/gi, "");
|
|
||||||
s = s.replace(
|
|
||||||
/href\s*=\s*["']?\s*(javascript|data|vbscript)\s*:[^"'>\s]*/gi,
|
|
||||||
'href="#"',
|
|
||||||
);
|
|
||||||
s = s.replace(
|
|
||||||
/src\s*=\s*["']?\s*(javascript|data|vbscript)\s*:[^"'>\s]*/gi,
|
|
||||||
'src=""',
|
|
||||||
);
|
|
||||||
s = s.replace(/( )/g, " ");
|
|
||||||
s = s.replace(/\s+style\s*=\s*("[^"]*"|'[^']*'|[^\s>]*)/gi, "");
|
|
||||||
let prev = "";
|
|
||||||
while (prev !== s) {
|
|
||||||
prev = s;
|
|
||||||
s = s.replace(/<span([^>]*)>(.*?)<\/span>\s*<span\1>/gs, "<span$1>$2");
|
|
||||||
}
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface AddressResult {
|
interface AddressResult {
|
||||||
name: string;
|
name: string;
|
||||||
lines: string[];
|
lines: string[];
|
||||||
@@ -282,6 +234,47 @@ const translations: Record<string, Record<string, string>> = {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/** Tag-stripped emptiness check — "<p><br></p>" (empty Quill) counts as empty. */
|
||||||
|
function stripTags(html: string | null | undefined): string {
|
||||||
|
return (html || "").replace(/<[^>]*>/g, "").trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Repeating per-page footer for the invoice PDF, rendered by Puppeteer in the
|
||||||
|
* bottom page margin (mirrors buildIssuedOrderPdfFooter): "Vystavil: <name>"
|
||||||
|
* left, "Strana X z Y" / "Page X of Y" (by document language) centered.
|
||||||
|
* Styles must stay inline and the font-size explicit — Chromium defaults
|
||||||
|
* footer text to 0.
|
||||||
|
*/
|
||||||
|
export function buildInvoicePdfFooter(
|
||||||
|
lang: string,
|
||||||
|
issuerLine: string,
|
||||||
|
): string {
|
||||||
|
const t = translations[lang] || translations.cs;
|
||||||
|
const pageWord = lang === "cs" ? "Strana" : "Page";
|
||||||
|
const ofWord = lang === "cs" ? "z" : "of";
|
||||||
|
return `<div style="width:100%; font-size:8pt; font-family:Tahoma, sans-serif; color:#646464; padding:0 12mm; box-sizing:border-box; display:flex; align-items:center;">
|
||||||
|
<div style="flex:1; text-align:left;"><span style="font-weight:600;">${escapeHtml(t.issued_by)}</span> ${escapeHtml(issuerLine)}</div>
|
||||||
|
<div style="flex:1; text-align:center;">${pageWord} <span class="pageNumber"></span> ${ofWord} <span class="totalPages"></span></div>
|
||||||
|
<div style="flex:1;"></div>
|
||||||
|
</div>`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Repeating per-page header for the invoice PDF — the unified red-accent
|
||||||
|
* family header (shared with issued orders): logo left, red
|
||||||
|
* "FAKTURA - DAŇOVÝ DOKLAD č. X" right, red rule underneath, on EVERY page.
|
||||||
|
* The body's own header is print-hidden so page 1 doesn't show it twice.
|
||||||
|
*/
|
||||||
|
export function buildInvoicePdfHeader(
|
||||||
|
lang: string,
|
||||||
|
logoDataUri: string,
|
||||||
|
invoiceNumber: string,
|
||||||
|
): string {
|
||||||
|
const t = translations[lang] || translations.cs;
|
||||||
|
return buildPdfHeaderTemplate(logoDataUri, `${t.heading} ${invoiceNumber}`);
|
||||||
|
}
|
||||||
|
|
||||||
/* ── Route ───────────────────────────────────────────────────────── */
|
/* ── Route ───────────────────────────────────────────────────────── */
|
||||||
|
|
||||||
export default async function invoicesPdfRoutes(
|
export default async function invoicesPdfRoutes(
|
||||||
@@ -314,6 +307,11 @@ export default async function invoicesPdfRoutes(
|
|||||||
where: { invoice_id: id },
|
where: { invoice_id: id },
|
||||||
orderBy: { position: "asc" },
|
orderBy: { position: "asc" },
|
||||||
});
|
});
|
||||||
|
// id tiebreak: position can repeat, keep the order deterministic.
|
||||||
|
const sections = await prisma.invoice_sections.findMany({
|
||||||
|
where: { invoice_id: id },
|
||||||
|
orderBy: [{ position: "asc" }, { id: "asc" }],
|
||||||
|
});
|
||||||
|
|
||||||
let customer: Record<string, unknown> | null = null;
|
let customer: Record<string, unknown> | null = null;
|
||||||
if (invoice.customer_id) {
|
if (invoice.customer_id) {
|
||||||
@@ -350,16 +348,12 @@ export default async function invoicesPdfRoutes(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let logoImg = "";
|
// Logo as a data URI — used by the body header (screen/HTML view) AND
|
||||||
if (settings?.logo_data) {
|
// the Puppeteer headerTemplate (templates can only load data: URLs).
|
||||||
const buf = Buffer.from(settings.logo_data as Buffer);
|
const logoDataUri = logoDataUriFromSettings(settings);
|
||||||
let mime = "image/png";
|
const logoImg = logoDataUri
|
||||||
if (buf[0] === 0xff && buf[1] === 0xd8) mime = "image/jpeg";
|
? `<img src="${logoDataUri}" class="logo" />`
|
||||||
else if (buf[0] === 0x47 && buf[1] === 0x49) mime = "image/gif";
|
: "";
|
||||||
else if (buf[0] === 0x52 && buf[1] === 0x49) mime = "image/webp";
|
|
||||||
const b64 = buf.toString("base64");
|
|
||||||
logoImg = `<img src="data:${escapeHtml(mime)};base64,${b64}" class="logo" />`;
|
|
||||||
}
|
|
||||||
|
|
||||||
const currency = invoice.currency || "CZK";
|
const currency = invoice.currency || "CZK";
|
||||||
const applyVat = !!invoice.apply_vat;
|
const applyVat = !!invoice.apply_vat;
|
||||||
@@ -522,26 +516,44 @@ export default async function invoicesPdfRoutes(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const notesRaw = invoice.notes ?? "";
|
// ── Sections ("Obsah") — flow INLINE right after the items/totals
|
||||||
const notesStripped = notesRaw.replace(/<[^>]*>/g, "").trim();
|
// block (mirrors issued orders): no separate page, long content
|
||||||
const notesHtml = notesStripped
|
// paginates naturally and the Puppeteer footer template stamps every
|
||||||
? `
|
// page. Suppressed entirely when every section is empty (tag-stripped
|
||||||
<!-- Poznamky -->
|
// check, so an empty-Quill "<p><br></p>" doesn't count). cs prefers
|
||||||
<div class="invoice-notes">
|
// the Czech title when present; en (or a missing Czech title) falls
|
||||||
<div class="invoice-notes-label">${escapeHtml(t.notes)}</div>
|
// back to the EN title.
|
||||||
<div class="invoice-notes-content">${cleanQuillHtml(DOMPurify.sanitize(notesRaw))}</div>
|
const resolveSectionTitle = (s: {
|
||||||
</div>
|
title: string | null;
|
||||||
`
|
title_cz: string | null;
|
||||||
: "";
|
}): string =>
|
||||||
|
lang === "cs" && (s.title_cz || "").trim()
|
||||||
|
? s.title_cz || ""
|
||||||
|
: s.title || "";
|
||||||
|
// Visibility must use the SAME per-language title rule as the render
|
||||||
|
// loop — otherwise a section with only the other language's title
|
||||||
|
// would count as content and produce an empty sections block.
|
||||||
|
const visibleSections = sections.filter(
|
||||||
|
(s) => resolveSectionTitle(s).trim() || stripTags(s.content),
|
||||||
|
);
|
||||||
|
let sectionsHtml = "";
|
||||||
|
if (visibleSections.length > 0) {
|
||||||
|
sectionsHtml += '<div class="scope-sections">';
|
||||||
|
for (const section of visibleSections) {
|
||||||
|
const title = resolveSectionTitle(section);
|
||||||
|
const content = (section.content || "").trim();
|
||||||
|
sectionsHtml += '<div class="scope-section">';
|
||||||
|
if (title.trim())
|
||||||
|
sectionsHtml += `<div class="scope-section-title">${escapeHtml(title)}</div>`;
|
||||||
|
if (content)
|
||||||
|
sectionsHtml += `<div class="section-content">${cleanQuillHtml(DOMPurify.sanitize(content))}</div>`;
|
||||||
|
sectionsHtml += "</div>";
|
||||||
|
}
|
||||||
|
sectionsHtml += "</div>";
|
||||||
|
}
|
||||||
|
|
||||||
// Quill indent CSS
|
// Quill indent CSS
|
||||||
let indentCSS = "";
|
const indentCSS = buildQuillIndentCss();
|
||||||
for (let n = 1; n <= 9; n++) {
|
|
||||||
const pad = n * 3;
|
|
||||||
const liPad = n * 3 + 1.5;
|
|
||||||
indentCSS += ` .ql-indent-${n} { padding-left: ${pad}em; }\n`;
|
|
||||||
indentCSS += ` li.ql-indent-${n} { padding-left: ${liPad}em; }\n`;
|
|
||||||
}
|
|
||||||
|
|
||||||
const html = `<!DOCTYPE html>
|
const html = `<!DOCTYPE html>
|
||||||
<html lang="${escapeHtml(lang)}">
|
<html lang="${escapeHtml(lang)}">
|
||||||
@@ -552,7 +564,11 @@ export default async function invoicesPdfRoutes(
|
|||||||
<style>
|
<style>
|
||||||
@page {
|
@page {
|
||||||
size: A4;
|
size: A4;
|
||||||
margin: 8mm 12mm 10mm 12mm;
|
/* Chromium uses THESE margins for layout (they override Puppeteer's
|
||||||
|
margin option) — they must match the header/footer template space:
|
||||||
|
32mm top (22mm logo + red heading + rule), 18mm bottom (Vystavil +
|
||||||
|
Strana X z Y). Same geometry as the issued-order PDF. */
|
||||||
|
margin: 32mm 12mm 18mm 12mm;
|
||||||
}
|
}
|
||||||
* { margin: 0; padding: 0; box-sizing: border-box; }
|
* { margin: 0; padding: 0; box-sizing: border-box; }
|
||||||
html, body {
|
html, body {
|
||||||
@@ -565,11 +581,19 @@ export default async function invoicesPdfRoutes(
|
|||||||
.invoice-page {
|
.invoice-page {
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
min-height: calc(297mm - 27mm);
|
/* Printable area with the Puppeteer header (32mm top) + footer (18mm
|
||||||
|
bottom) margins, minus 1mm slack so a one-page invoice can't spill. */
|
||||||
|
min-height: calc(297mm - 51mm);
|
||||||
}
|
}
|
||||||
.invoice-content { flex: 1 1 auto; }
|
.invoice-content { flex: 1 1 auto; }
|
||||||
|
/* The bottom block (notice + QR/VAT recap + Převzal/Razítko) must never be
|
||||||
|
SPLIT by a page break — on a one-page invoice the flex pinning keeps it
|
||||||
|
at the page bottom; when the content overflows, break-inside moves the
|
||||||
|
whole block to the next page as one unit instead of stranding the notice
|
||||||
|
on the previous page. */
|
||||||
.invoice-footer {
|
.invoice-footer {
|
||||||
flex-shrink: 0;
|
flex-shrink: 0;
|
||||||
|
break-inside: avoid;
|
||||||
}
|
}
|
||||||
|
|
||||||
.accent { color: #de3a3a; }
|
.accent { color: #de3a3a; }
|
||||||
@@ -781,13 +805,8 @@ export default async function invoicesPdfRoutes(
|
|||||||
margin-top: 2mm;
|
margin-top: 2mm;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Vystavil */
|
/* Vystavil lives in Puppeteer's footerTemplate (bottom page margin) on
|
||||||
.issued-by {
|
every page — same as the issued-order PDF; no body block. */
|
||||||
font-size: 9pt;
|
|
||||||
margin: 2mm 0;
|
|
||||||
line-height: 1.4;
|
|
||||||
}
|
|
||||||
.issued-by .lbl { font-weight: 600; }
|
|
||||||
|
|
||||||
/* Upozorneni */
|
/* Upozorneni */
|
||||||
.notice {
|
.notice {
|
||||||
@@ -853,32 +872,42 @@ export default async function invoicesPdfRoutes(
|
|||||||
color: #555;
|
color: #555;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Poznamky */
|
/* Sekce ("Obsah") — tecou inline za tabulkou polozek; zadna nova stranka,
|
||||||
.invoice-notes {
|
dlouhy obsah strankuje prirozene a paticku tiskne Puppeteer na kazdou
|
||||||
margin-top: 4mm;
|
stranu (mirrors issued-orders-pdf). */
|
||||||
font-size: 10pt;
|
.scope-sections {
|
||||||
line-height: 1.5;
|
margin-top: 6mm;
|
||||||
color: #1a1a1a;
|
|
||||||
}
|
}
|
||||||
.invoice-notes-label {
|
.scope-section {
|
||||||
font-weight: 600;
|
width: 100%;
|
||||||
font-size: 9pt;
|
max-width: 100%;
|
||||||
text-transform: uppercase;
|
margin-bottom: 3mm;
|
||||||
color: #555;
|
break-inside: avoid;
|
||||||
|
}
|
||||||
|
.scope-section-title {
|
||||||
|
font-size: 11pt;
|
||||||
|
font-weight: 700;
|
||||||
|
color: #1a1a1a;
|
||||||
margin-bottom: 1mm;
|
margin-bottom: 1mm;
|
||||||
}
|
}
|
||||||
.invoice-notes-content p { margin: 0 0 0.4em 0; }
|
.section-content {
|
||||||
.invoice-notes-content ul, .invoice-notes-content ol { margin: 0 0 0.4em 1.5em; }
|
color: #1a1a1a;
|
||||||
.invoice-notes-content li { margin-bottom: 0.2em; }
|
line-height: 1.5;
|
||||||
.invoice-notes-content,
|
word-break: normal;
|
||||||
.invoice-notes-content * {
|
overflow-wrap: anywhere;
|
||||||
|
}
|
||||||
|
.section-content,
|
||||||
|
.section-content * {
|
||||||
font-family: Tahoma, sans-serif !important;
|
font-family: Tahoma, sans-serif !important;
|
||||||
}
|
}
|
||||||
.invoice-notes-content { font-size: 14px; }
|
.section-content { font-size: 14px; }
|
||||||
.invoice-notes-content h1 { font-size: 20px; }
|
.section-content h1 { font-size: 20px; }
|
||||||
.invoice-notes-content h2 { font-size: 18px; }
|
.section-content h2 { font-size: 18px; }
|
||||||
.invoice-notes-content h3 { font-size: 16px; }
|
.section-content h3 { font-size: 16px; }
|
||||||
.invoice-notes-content h4 { font-size: 15px; }
|
.section-content h4 { font-size: 15px; }
|
||||||
|
.section-content p { margin: 0 0 0.4em 0; }
|
||||||
|
.section-content ul, .section-content ol { margin: 0 0 0.4em 1.5em; }
|
||||||
|
.section-content li { margin-bottom: 0.2em; }
|
||||||
|
|
||||||
/* Quill fonty – v PDF vynuceno Tahoma */
|
/* Quill fonty – v PDF vynuceno Tahoma */
|
||||||
[class*="ql-font-"] { font-family: Tahoma, sans-serif !important; }
|
[class*="ql-font-"] { font-family: Tahoma, sans-serif !important; }
|
||||||
@@ -889,6 +918,10 @@ ${indentCSS}
|
|||||||
|
|
||||||
@media print {
|
@media print {
|
||||||
body { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
|
body { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
|
||||||
|
/* The logo + red heading print via Puppeteer's headerTemplate on EVERY
|
||||||
|
page — hide the body copy so page 1 doesn't show it twice. The screen
|
||||||
|
(HTML preview) keeps the body header. */
|
||||||
|
.invoice-header { display: none; }
|
||||||
}
|
}
|
||||||
@media screen {
|
@media screen {
|
||||||
html { background: #525659; }
|
html { background: #525659; }
|
||||||
@@ -1000,16 +1033,12 @@ ${indentCSS}
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
${notesHtml}
|
${sectionsHtml}
|
||||||
|
|
||||||
</div><!-- /.invoice-content -->
|
</div><!-- /.invoice-content -->
|
||||||
<div class="invoice-footer">
|
<div class="invoice-footer">
|
||||||
|
|
||||||
<!-- Vystavil -->
|
<!-- Vystavil tiskne Puppeteer footerTemplate na kazde strance -->
|
||||||
<div class="issued-by">
|
|
||||||
<span class="lbl">${escapeHtml(t.issued_by)}</span> ${escapeHtml(invoice.issued_by || "")}
|
|
||||||
${suppEmail ? `<br> ${escapeHtml(suppEmail)}` : ""}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- Upozorneni -->
|
<!-- Upozorneni -->
|
||||||
<div class="notice">
|
<div class="notice">
|
||||||
@@ -1074,7 +1103,20 @@ ${indentCSS}
|
|||||||
? new Date(invoice.issue_date)
|
? new Date(invoice.issue_date)
|
||||||
: new Date();
|
: new Date();
|
||||||
nasInvoicesManager.cleanIssued(invoice.invoice_number!);
|
nasInvoicesManager.cleanIssued(invoice.invoice_number!);
|
||||||
const pdfBuffer = await htmlToPdf(html);
|
// Per-page "Vystavil + Strana X z Y" footer (same as issued
|
||||||
|
// orders); the supplier e-mail rides along on the left (it used to
|
||||||
|
// print under the body Vystavil block).
|
||||||
|
const issuerLine = `${invoice.issued_by || ""}${
|
||||||
|
suppEmail ? `${invoice.issued_by ? " · " : ""}${suppEmail}` : ""
|
||||||
|
}`;
|
||||||
|
const pdfBuffer = await htmlToPdf(html, {
|
||||||
|
headerTemplate: buildInvoicePdfHeader(
|
||||||
|
lang,
|
||||||
|
logoDataUri,
|
||||||
|
invoice.invoice_number || "",
|
||||||
|
),
|
||||||
|
footerTemplate: buildInvoicePdfFooter(lang, issuerLine),
|
||||||
|
});
|
||||||
nasInvoicesManager.saveIssuedPdf(
|
nasInvoicesManager.saveIssuedPdf(
|
||||||
invoice.invoice_number!,
|
invoice.invoice_number!,
|
||||||
issueDate.getFullYear(),
|
issueDate.getFullYear(),
|
||||||
|
|||||||
@@ -2,65 +2,25 @@ import { FastifyInstance } from "fastify";
|
|||||||
import prisma from "../../config/database";
|
import prisma from "../../config/database";
|
||||||
import { requirePermission } from "../../middleware/auth";
|
import { requirePermission } from "../../middleware/auth";
|
||||||
import { parseId, success } from "../../utils/response";
|
import { parseId, success } from "../../utils/response";
|
||||||
import { localDateCzStr } from "../../utils/date";
|
|
||||||
import { htmlToPdf } from "../../utils/html-to-pdf";
|
import { htmlToPdf } from "../../utils/html-to-pdf";
|
||||||
import { nasOrdersManager } from "../../services/nas-financials-manager";
|
import { nasOrdersManager } from "../../services/nas-financials-manager";
|
||||||
|
import {
|
||||||
|
formatDate,
|
||||||
|
formatNum,
|
||||||
|
formatCurrency,
|
||||||
|
escapeHtml,
|
||||||
|
cleanQuillHtml,
|
||||||
|
buildQuillIndentCss,
|
||||||
|
logoDataUriFromSettings,
|
||||||
|
buildPdfHeaderTemplate,
|
||||||
|
} from "../../utils/pdf-shared";
|
||||||
import createDOMPurify from "dompurify";
|
import createDOMPurify from "dompurify";
|
||||||
import { JSDOM } from "jsdom";
|
import { JSDOM } from "jsdom";
|
||||||
|
|
||||||
const window = new JSDOM("").window;
|
const window = new JSDOM("").window;
|
||||||
const DOMPurify = createDOMPurify(window);
|
const DOMPurify = createDOMPurify(window);
|
||||||
|
|
||||||
/* ── Helpers (copied from orders-pdf.ts — the shared confirmation template) ── */
|
/* ── Helpers — shared with the other PDF routes via utils/pdf-shared ── */
|
||||||
|
|
||||||
function formatDate(date: Date | string | null | undefined): string {
|
|
||||||
if (!date) return "";
|
|
||||||
const d = new Date(date);
|
|
||||||
if (isNaN(d.getTime())) return String(date);
|
|
||||||
return localDateCzStr(d);
|
|
||||||
}
|
|
||||||
|
|
||||||
function formatNum(n: number, decimals = 2): string {
|
|
||||||
const abs = Math.abs(n);
|
|
||||||
const fixed = abs.toFixed(decimals);
|
|
||||||
const [intPart, decPart] = fixed.split(".");
|
|
||||||
const withSep = intPart.replace(/\B(?=(\d{3})+(?!\d))/g, " ");
|
|
||||||
const result = decPart ? `${withSep},${decPart}` : withSep;
|
|
||||||
return n < 0 ? `-${result}` : result;
|
|
||||||
}
|
|
||||||
|
|
||||||
function escapeHtml(str: string | null | undefined): string {
|
|
||||||
if (!str) return "";
|
|
||||||
return str
|
|
||||||
.replace(/&/g, "&")
|
|
||||||
.replace(/</g, "<")
|
|
||||||
.replace(/>/g, ">")
|
|
||||||
.replace(/"/g, """);
|
|
||||||
}
|
|
||||||
|
|
||||||
function cleanQuillHtml(html: string | null | undefined): string {
|
|
||||||
if (!html) return "";
|
|
||||||
let s = html;
|
|
||||||
s = s.replace(
|
|
||||||
/<(script|iframe|object|embed|style|link|meta|base|form|input|textarea|button|select|svg|math)[^>]*>[\s\S]*?<\/\1>/gi,
|
|
||||||
"",
|
|
||||||
);
|
|
||||||
s = s.replace(
|
|
||||||
/<(script|iframe|object|embed|style|link|meta|base|form|input|textarea|button|select|svg|math)[^>]*\/?>/gi,
|
|
||||||
"",
|
|
||||||
);
|
|
||||||
s = s.replace(/\s+on\w+\s*=\s*("[^"]*"|'[^']*'|[^\s>]*)/gi, "");
|
|
||||||
s = s.replace(/\s+on\w+\s*=\s*[^\s>]*/gi, "");
|
|
||||||
s = s.replace(/href\s*=\s*["']?\s*javascript\s*:[^"'>\s]*/gi, 'href="#"');
|
|
||||||
s = s.replace(/( )/g, " ");
|
|
||||||
s = s.replace(/\s+style\s*=\s*("[^"]*"|'[^']*'|[^\s>]*)/gi, "");
|
|
||||||
let prev = "";
|
|
||||||
while (prev !== s) {
|
|
||||||
prev = s;
|
|
||||||
s = s.replace(/<span([^>]*)>(.*?)<\/span>\s*<span\1>/gs, "<span$1>$2");
|
|
||||||
}
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface AddressResult {
|
interface AddressResult {
|
||||||
name: string;
|
name: string;
|
||||||
@@ -156,6 +116,31 @@ function buildAddressLines(
|
|||||||
return { name, lines };
|
return { name, lines };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Address block for the sklad_suppliers counterparty. Unlike customers (which
|
||||||
|
* have structured street/city/postal columns), suppliers.address is a single
|
||||||
|
* Text blob — split it on newlines into one rendered line each (a blob without
|
||||||
|
* newlines renders as one line). IČO/DIČ come from the supplier's ico/dic
|
||||||
|
* columns, prefixed with the same translated labels the customer block used.
|
||||||
|
*/
|
||||||
|
function buildSupplierLines(
|
||||||
|
supplier: Record<string, unknown> | null,
|
||||||
|
tObj: Record<string, string>,
|
||||||
|
): AddressResult {
|
||||||
|
if (!supplier) return { name: "", lines: [] };
|
||||||
|
const name = String(supplier.name || "");
|
||||||
|
const lines: string[] = [];
|
||||||
|
if (supplier.address) {
|
||||||
|
for (const part of String(supplier.address).split(/\r?\n/)) {
|
||||||
|
const line = part.trim();
|
||||||
|
if (line) lines.push(line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (supplier.ico) lines.push(`${tObj.ico}${supplier.ico}`);
|
||||||
|
if (supplier.dic) lines.push(`${tObj.dic}${supplier.dic}`);
|
||||||
|
return { name, lines };
|
||||||
|
}
|
||||||
|
|
||||||
/* ── Translations ────────────────────────────────────────────────── */
|
/* ── Translations ────────────────────────────────────────────────── */
|
||||||
|
|
||||||
type Lang = "cs" | "en";
|
type Lang = "cs" | "en";
|
||||||
@@ -165,27 +150,18 @@ const translations: Record<Lang, Record<string, string>> = {
|
|||||||
title: "OBJEDNÁVKA",
|
title: "OBJEDNÁVKA",
|
||||||
heading: "OBJEDNÁVKA č.",
|
heading: "OBJEDNÁVKA č.",
|
||||||
// PO direction: WE (company) are the buyer (Odběratel), the
|
// PO direction: WE (company) are the buyer (Odběratel), the
|
||||||
// selected customer record is the supplier (Dodavatel).
|
// selected sklad_suppliers record is the supplier (Dodavatel).
|
||||||
supplier: "Dodavatel",
|
supplier: "Dodavatel",
|
||||||
buyer: "Odběratel",
|
buyer: "Odběratel",
|
||||||
issue_date: "Datum vystavení:",
|
issue_date: "Datum vystavení:",
|
||||||
delivery_date: "Požadované dodání:",
|
delivery_date: "Požadované dodání:",
|
||||||
billing: "Objednáváme u Vás:",
|
billing: "Objednáváme si u Vás:",
|
||||||
col_no: "Č.",
|
col_no: "Č.",
|
||||||
col_desc: "Popis",
|
col_desc: "Popis",
|
||||||
col_qty: "Množství",
|
col_qty: "Množství",
|
||||||
col_unit_price: "Jedn. cena",
|
col_unit_price: "Jedn. cena",
|
||||||
col_price: "Cena",
|
|
||||||
col_vat_pct: "%DPH",
|
|
||||||
col_vat: "DPH",
|
|
||||||
col_total: "Celkem",
|
col_total: "Celkem",
|
||||||
subtotal: "Mezisoučet:",
|
total_no_vat: "Celkem bez DPH",
|
||||||
vat_label: "DPH",
|
|
||||||
total: "Celkem",
|
|
||||||
amounts_in: "Částky jsou uvedeny v",
|
|
||||||
notes: "Poznámky",
|
|
||||||
delivery_terms: "Dodací podmínky:",
|
|
||||||
payment_terms: "Platební podmínky:",
|
|
||||||
issued_by: "Vystavil:",
|
issued_by: "Vystavil:",
|
||||||
ico: "IČ: ",
|
ico: "IČ: ",
|
||||||
dic: "DIČ: ",
|
dic: "DIČ: ",
|
||||||
@@ -202,17 +178,8 @@ const translations: Record<Lang, Record<string, string>> = {
|
|||||||
col_desc: "Description",
|
col_desc: "Description",
|
||||||
col_qty: "Quantity",
|
col_qty: "Quantity",
|
||||||
col_unit_price: "Unit price",
|
col_unit_price: "Unit price",
|
||||||
col_price: "Price",
|
|
||||||
col_vat_pct: "VAT%",
|
|
||||||
col_vat: "VAT",
|
|
||||||
col_total: "Total",
|
col_total: "Total",
|
||||||
subtotal: "Subtotal:",
|
total_no_vat: "Total excl. VAT",
|
||||||
vat_label: "VAT",
|
|
||||||
total: "Total",
|
|
||||||
amounts_in: "Amounts are in",
|
|
||||||
notes: "Notes",
|
|
||||||
delivery_terms: "Delivery terms:",
|
|
||||||
payment_terms: "Payment terms:",
|
|
||||||
issued_by: "Issued by:",
|
issued_by: "Issued by:",
|
||||||
ico: "Reg. No.: ",
|
ico: "Reg. No.: ",
|
||||||
dic: "Tax ID: ",
|
dic: "Tax ID: ",
|
||||||
@@ -224,12 +191,8 @@ interface IssuedOrderPdfData {
|
|||||||
order_date: Date | null;
|
order_date: Date | null;
|
||||||
delivery_date: Date | null;
|
delivery_date: Date | null;
|
||||||
currency: string | null;
|
currency: string | null;
|
||||||
apply_vat: boolean | null;
|
// Editable heading above the items table; null → t.billing default.
|
||||||
vat_rate: unknown;
|
order_text?: string | null;
|
||||||
notes: string | null;
|
|
||||||
delivery_terms: string | null;
|
|
||||||
payment_terms: string | null;
|
|
||||||
issued_by: string | null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
interface IssuedOrderPdfItem {
|
interface IssuedOrderPdfItem {
|
||||||
@@ -238,72 +201,98 @@ interface IssuedOrderPdfItem {
|
|||||||
quantity: unknown;
|
quantity: unknown;
|
||||||
unit: string | null;
|
unit: string | null;
|
||||||
unit_price: unknown;
|
unit_price: unknown;
|
||||||
vat_rate: unknown;
|
}
|
||||||
|
|
||||||
|
export interface IssuedOrderPdfSection {
|
||||||
|
title: string | null;
|
||||||
|
title_cz: string | null;
|
||||||
|
content: string | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Tag-stripped emptiness check — "<p><br></p>" (empty Quill) counts as empty. */
|
||||||
|
function stripTags(html: string | null | undefined): string {
|
||||||
|
return (html || "").replace(/<[^>]*>/g, "").trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Repeating per-page footer for the issued-order PDF, rendered by Puppeteer
|
||||||
|
* in the bottom page margin (the only true repeating footer Chromium
|
||||||
|
* supports): "Vystavil: <name>" left, "Strana X z Y" / "Page X of Y"
|
||||||
|
* (by document language) centered. Styles must stay inline and the
|
||||||
|
* font-size explicit — Chromium defaults footer text to 0.
|
||||||
|
*/
|
||||||
|
export function buildIssuedOrderPdfFooter(
|
||||||
|
lang: Lang,
|
||||||
|
issuerName: string,
|
||||||
|
): string {
|
||||||
|
const t = translations[lang];
|
||||||
|
const pageWord = lang === "cs" ? "Strana" : "Page";
|
||||||
|
const ofWord = lang === "cs" ? "z" : "of";
|
||||||
|
return `<div style="width:100%; font-size:8pt; font-family:Tahoma, sans-serif; color:#646464; padding:0 12mm; box-sizing:border-box; display:flex; align-items:center;">
|
||||||
|
<div style="flex:1; text-align:left;"><span style="font-weight:600;">${escapeHtml(t.issued_by)}</span> ${escapeHtml(issuerName)}</div>
|
||||||
|
<div style="flex:1; text-align:center;">${pageWord} <span class="pageNumber"></span> ${ofWord} <span class="totalPages"></span></div>
|
||||||
|
<div style="flex:1;"></div>
|
||||||
|
</div>`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Repeating per-page header for the issued-order PDF — the unified
|
||||||
|
* red-accent family header (shared with invoices): logo left, red
|
||||||
|
* "OBJEDNÁVKA č. X" right, red rule underneath, on EVERY page. The body's
|
||||||
|
* own header is print-hidden so page 1 doesn't show it twice.
|
||||||
|
*/
|
||||||
|
export function buildIssuedOrderPdfHeader(
|
||||||
|
lang: Lang,
|
||||||
|
logoDataUri: string,
|
||||||
|
poNumber: string,
|
||||||
|
): string {
|
||||||
|
const t = translations[lang];
|
||||||
|
return buildPdfHeaderTemplate(logoDataUri, `${t.heading} ${poNumber}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function renderIssuedOrderHtml(
|
export function renderIssuedOrderHtml(
|
||||||
order: IssuedOrderPdfData,
|
order: IssuedOrderPdfData,
|
||||||
items: IssuedOrderPdfItem[],
|
items: IssuedOrderPdfItem[],
|
||||||
customer: Record<string, unknown> | null,
|
supplier: Record<string, unknown> | null,
|
||||||
settings: Record<string, unknown> | null,
|
settings: Record<string, unknown> | null,
|
||||||
lang: Lang,
|
lang: Lang,
|
||||||
issuer: { name: string },
|
// Kept for signature stability across the many call sites: the issuer no
|
||||||
|
// longer renders in the BODY — it prints in the Puppeteer footer template
|
||||||
|
// (buildIssuedOrderPdfFooter) on every page.
|
||||||
|
_issuer: { name: string },
|
||||||
|
sections: IssuedOrderPdfSection[] = [],
|
||||||
): string {
|
): string {
|
||||||
const t = translations[lang];
|
const t = translations[lang];
|
||||||
const applyVat = order.apply_vat !== false;
|
|
||||||
const currency = order.currency || "CZK";
|
const currency = order.currency || "CZK";
|
||||||
const docRate = order.vat_rate != null ? Number(order.vat_rate) : 21;
|
|
||||||
const poNumber = escapeHtml(order.po_number || "");
|
const poNumber = escapeHtml(order.po_number || "");
|
||||||
|
|
||||||
// Logo embedding (same logic as the confirmation template).
|
// Logo embedding — shared helper (also feeds the Puppeteer headerTemplate).
|
||||||
let logoImg = "";
|
const logoDataUri = logoDataUriFromSettings(settings);
|
||||||
if (settings?.logo_data) {
|
const logoImg = logoDataUri
|
||||||
const buf = Buffer.from(settings.logo_data as Buffer);
|
? `<img src="${logoDataUri}" class="logo" />`
|
||||||
let mime = "image/png";
|
: "";
|
||||||
if (buf[0] === 0xff && buf[1] === 0xd8) mime = "image/jpeg";
|
|
||||||
else if (buf[0] === 0x47 && buf[1] === 0x49) mime = "image/gif";
|
|
||||||
else if (buf[0] === 0x52 && buf[1] === 0x49) mime = "image/webp";
|
|
||||||
const b64 = buf.toString("base64");
|
|
||||||
logoImg = `<img src="data:${escapeHtml(mime)};base64,${b64}" class="logo" />`;
|
|
||||||
}
|
|
||||||
|
|
||||||
// PO direction: our company (settings) = Odběratel (buyer);
|
// PO direction: our company (settings) = Odběratel (buyer);
|
||||||
// the customer record = Dodavatel (supplier).
|
// the sklad_suppliers record = Dodavatel (supplier).
|
||||||
const buyer = buildAddressLines(settings, true, t); // company → Odběratel
|
const buyer = buildAddressLines(settings, true, t); // company → Odběratel
|
||||||
const supplier = buildAddressLines(customer, false, t); // customer → Dodavatel
|
const supplierAddr = buildSupplierLines(supplier, t); // supplier → Dodavatel
|
||||||
|
|
||||||
const buyerLinesHtml = buyer.lines
|
const buyerLinesHtml = buyer.lines
|
||||||
.map((l) => `<div class="address-line">${escapeHtml(l)}</div>`)
|
.map((l) => `<div class="address-line">${escapeHtml(l)}</div>`)
|
||||||
.join("");
|
.join("");
|
||||||
const supplierLinesHtml = supplier.lines
|
const supplierLinesHtml = supplierAddr.lines
|
||||||
.map((l) => `<div class="address-line">${escapeHtml(l)}</div>`)
|
.map((l) => `<div class="address-line">${escapeHtml(l)}</div>`)
|
||||||
.join("");
|
.join("");
|
||||||
|
|
||||||
// Items — NET + per-line VAT-on-top, rounded per line (same math the
|
// Items — NET only. A purchase order is not a tax document: no VAT columns,
|
||||||
// service computeIssuedOrderTotals uses; mirrors the confirmation loop).
|
// no VAT math (same as the service computeIssuedOrderTotals).
|
||||||
let subtotal = 0;
|
let total = 0;
|
||||||
let totalVat = 0;
|
|
||||||
const vatSummary: Record<string, { base: number; vat: number }> = {};
|
|
||||||
const itemsHtml = items
|
const itemsHtml = items
|
||||||
.map((it, i) => {
|
.map((it, i) => {
|
||||||
const qty = Number(it.quantity) || 0;
|
const qty = Number(it.quantity) || 0;
|
||||||
const unitPrice = Number(it.unit_price) || 0;
|
const unitPrice = Number(it.unit_price) || 0;
|
||||||
const rate =
|
const lineTotal = qty * unitPrice;
|
||||||
it.vat_rate != null && it.vat_rate !== ""
|
total += lineTotal;
|
||||||
? Number(it.vat_rate)
|
|
||||||
: docRate;
|
|
||||||
const lineSubtotal = qty * unitPrice;
|
|
||||||
const lineVat = applyVat
|
|
||||||
? Math.round(lineSubtotal * (rate / 100) * 100) / 100
|
|
||||||
: 0;
|
|
||||||
const lineTotal = lineSubtotal + lineVat;
|
|
||||||
|
|
||||||
subtotal += lineSubtotal;
|
|
||||||
totalVat += lineVat;
|
|
||||||
const key = String(rate);
|
|
||||||
if (!vatSummary[key]) vatSummary[key] = { base: 0, vat: 0 };
|
|
||||||
vatSummary[key].base += lineSubtotal;
|
|
||||||
vatSummary[key].vat += lineVat;
|
|
||||||
|
|
||||||
const qtyDecimals = Math.floor(qty) === qty ? 0 : 2;
|
const qtyDecimals = Math.floor(qty) === qty ? 0 : 2;
|
||||||
const descHtml = `${escapeHtml(it.description)}${
|
const descHtml = `${escapeHtml(it.description)}${
|
||||||
@@ -315,63 +304,53 @@ export function renderIssuedOrderHtml(
|
|||||||
<td class="row-num">${i + 1}</td>
|
<td class="row-num">${i + 1}</td>
|
||||||
<td class="desc">${descHtml}</td>
|
<td class="desc">${descHtml}</td>
|
||||||
<td class="center">${formatNum(qty, qtyDecimals)}${it.unit ? ` / ${escapeHtml(it.unit)}` : ""}</td>
|
<td class="center">${formatNum(qty, qtyDecimals)}${it.unit ? ` / ${escapeHtml(it.unit)}` : ""}</td>
|
||||||
<td class="right">${formatNum(unitPrice)}</td>
|
<td class="right">${formatCurrency(unitPrice, currency)}</td>
|
||||||
<td class="right">${formatNum(lineSubtotal)}</td>
|
<td class="right total-cell">${formatCurrency(lineTotal, currency)}</td>
|
||||||
<td class="center">${applyVat ? Math.floor(rate) : 0}%</td>
|
|
||||||
<td class="right">${formatNum(lineVat)}</td>
|
|
||||||
<td class="right total-cell">${formatNum(lineTotal)}</td>
|
|
||||||
</tr>`;
|
</tr>`;
|
||||||
})
|
})
|
||||||
.join("");
|
.join("");
|
||||||
|
|
||||||
subtotal = Math.round(subtotal * 100) / 100;
|
total = Math.round(total * 100) / 100;
|
||||||
totalVat = Math.round(totalVat * 100) / 100;
|
|
||||||
const totalToPay = Math.round((subtotal + totalVat) * 100) / 100;
|
|
||||||
|
|
||||||
let vatDetailHtml = "";
|
|
||||||
if (applyVat) {
|
|
||||||
for (const [rate, data] of Object.entries(vatSummary)) {
|
|
||||||
if (data.vat > 0) {
|
|
||||||
vatDetailHtml += `
|
|
||||||
<div class="row">
|
|
||||||
<span class="label">${escapeHtml(t.vat_label)} ${Math.floor(Number(rate))}%:</span>
|
|
||||||
<span class="value">${formatNum(data.vat)} ${escapeHtml(currency)}</span>
|
|
||||||
</div>`;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const notesRaw = order.notes ?? "";
|
|
||||||
const notesStripped = notesRaw.replace(/<[^>]*>/g, "").trim();
|
|
||||||
const notesHtml = notesStripped
|
|
||||||
? `
|
|
||||||
<div class="invoice-notes">
|
|
||||||
<div class="invoice-notes-label">${escapeHtml(t.notes)}</div>
|
|
||||||
<div class="invoice-notes-content">${cleanQuillHtml(DOMPurify.sanitize(notesRaw))}</div>
|
|
||||||
</div>
|
|
||||||
`
|
|
||||||
: "";
|
|
||||||
|
|
||||||
const issueDateStr = formatDate(order.order_date);
|
const issueDateStr = formatDate(order.order_date);
|
||||||
const deliveryDateStr = formatDate(order.delivery_date);
|
const deliveryDateStr = formatDate(order.delivery_date);
|
||||||
|
|
||||||
// Order-specific terms (purchase-order addition; not on invoices).
|
|
||||||
let termsHtml = "";
|
|
||||||
if (order.delivery_terms) {
|
|
||||||
termsHtml += `<div class="terms-row"><span class="lbl">${escapeHtml(t.delivery_terms)}</span> ${escapeHtml(order.delivery_terms)}</div>`;
|
|
||||||
}
|
|
||||||
if (order.payment_terms) {
|
|
||||||
termsHtml += `<div class="terms-row"><span class="lbl">${escapeHtml(t.payment_terms)}</span> ${escapeHtml(order.payment_terms)}</div>`;
|
|
||||||
}
|
|
||||||
const termsBlock = termsHtml ? `<div class="terms">${termsHtml}</div>` : "";
|
|
||||||
|
|
||||||
// Quill indent CSS
|
// Quill indent CSS
|
||||||
let indentCSS = "";
|
const indentCSS = buildQuillIndentCss();
|
||||||
for (let n = 1; n <= 9; n++) {
|
|
||||||
const pad = n * 3;
|
// ── Sections ("Obsah") — flow INLINE after the items/totals block (user
|
||||||
const liPad = n * 3 + 1.5;
|
// decision: no separate page, no repeated header). Each section keeps
|
||||||
indentCSS += ` .ql-indent-${n} { padding-left: ${pad}em; }\n`;
|
// break-inside: avoid; long content paginates naturally and the Puppeteer
|
||||||
indentCSS += ` li.ql-indent-${n} { padding-left: ${liPad}em; }\n`;
|
// footer template stamps every page. Suppressed entirely when every
|
||||||
|
// section is empty (tag-stripped check, so an empty-Quill "<p><br></p>"
|
||||||
|
// doesn't count). cs prefers the Czech title when present; en (or a
|
||||||
|
// missing Czech title) falls back to the EN title (same rule as offers).
|
||||||
|
const resolveSectionTitle = (s: IssuedOrderPdfSection): string =>
|
||||||
|
lang === "cs" && (s.title_cz || "").trim()
|
||||||
|
? s.title_cz || ""
|
||||||
|
: s.title || "";
|
||||||
|
// Visibility must use the SAME per-language title rule as the render loop —
|
||||||
|
// otherwise a section with only the other language's title would count as
|
||||||
|
// content and produce an empty sections block.
|
||||||
|
const visibleSections = sections.filter(
|
||||||
|
(s) => resolveSectionTitle(s).trim() || stripTags(s.content),
|
||||||
|
);
|
||||||
|
const hasSectionContent = visibleSections.length > 0;
|
||||||
|
|
||||||
|
let scopeHtml = "";
|
||||||
|
if (hasSectionContent) {
|
||||||
|
scopeHtml += '<div class="scope-sections">';
|
||||||
|
for (const section of visibleSections) {
|
||||||
|
const title = resolveSectionTitle(section);
|
||||||
|
const content = (section.content || "").trim();
|
||||||
|
scopeHtml += '<div class="scope-section">';
|
||||||
|
if (title.trim())
|
||||||
|
scopeHtml += `<div class="scope-section-title">${escapeHtml(title)}</div>`;
|
||||||
|
if (content)
|
||||||
|
scopeHtml += `<div class="section-content">${cleanQuillHtml(DOMPurify.sanitize(content))}</div>`;
|
||||||
|
scopeHtml += "</div>";
|
||||||
|
}
|
||||||
|
scopeHtml += "</div>";
|
||||||
}
|
}
|
||||||
|
|
||||||
return `<!DOCTYPE html>
|
return `<!DOCTYPE html>
|
||||||
@@ -382,7 +361,11 @@ export function renderIssuedOrderHtml(
|
|||||||
<style>
|
<style>
|
||||||
@page {
|
@page {
|
||||||
size: A4;
|
size: A4;
|
||||||
margin: 8mm 12mm 10mm 12mm;
|
/* Chromium uses THESE margins for layout (they override Puppeteer's
|
||||||
|
margin option) — they must match the header/footer template space:
|
||||||
|
32mm top (22mm logo + red heading + rule), 18mm bottom (Vystavil +
|
||||||
|
Strana X z Y). Same geometry as the invoice PDF. */
|
||||||
|
margin: 32mm 12mm 18mm 12mm;
|
||||||
}
|
}
|
||||||
* { margin: 0; padding: 0; box-sizing: border-box; }
|
* { margin: 0; padding: 0; box-sizing: border-box; }
|
||||||
html, body {
|
html, body {
|
||||||
@@ -392,15 +375,9 @@ export function renderIssuedOrderHtml(
|
|||||||
width: 186mm;
|
width: 186mm;
|
||||||
}
|
}
|
||||||
|
|
||||||
.invoice-page {
|
/* The repeating "Vystavil / Strana X z Y" footer lives in Puppeteer's
|
||||||
display: flex;
|
footerTemplate (bottom page margin), NOT in the body — the old flex
|
||||||
flex-direction: column;
|
min-height pinning is gone with it. */
|
||||||
min-height: calc(297mm - 27mm);
|
|
||||||
}
|
|
||||||
.invoice-content { flex: 1 1 auto; }
|
|
||||||
.invoice-footer {
|
|
||||||
flex-shrink: 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
.accent { color: #de3a3a; }
|
.accent { color: #de3a3a; }
|
||||||
|
|
||||||
@@ -450,7 +427,7 @@ export function renderIssuedOrderHtml(
|
|||||||
vertical-align: top;
|
vertical-align: top;
|
||||||
width: 50%;
|
width: 50%;
|
||||||
}
|
}
|
||||||
.header-grid td.addr-customer {
|
.header-grid td.addr-supplier {
|
||||||
background: #f5f5f5;
|
background: #f5f5f5;
|
||||||
}
|
}
|
||||||
.header-grid td.details-bank {
|
.header-grid td.details-bank {
|
||||||
@@ -605,34 +582,6 @@ export function renderIssuedOrderHtml(
|
|||||||
border-bottom: 2.5pt solid #de3a3a;
|
border-bottom: 2.5pt solid #de3a3a;
|
||||||
padding-bottom: 1mm;
|
padding-bottom: 1mm;
|
||||||
}
|
}
|
||||||
.totals .currency-note {
|
|
||||||
text-align: right;
|
|
||||||
font-size: 8pt;
|
|
||||||
color: #1a1a1a;
|
|
||||||
margin-top: 2mm;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Dodaci / platebni podminky (PO-specificke) */
|
|
||||||
.terms {
|
|
||||||
margin-top: 4mm;
|
|
||||||
font-size: 9pt;
|
|
||||||
line-height: 1.5;
|
|
||||||
color: #1a1a1a;
|
|
||||||
}
|
|
||||||
.terms-row { margin-bottom: 1mm; }
|
|
||||||
.terms-row .lbl {
|
|
||||||
font-weight: 600;
|
|
||||||
color: #555;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Vystavil */
|
|
||||||
.issued-by {
|
|
||||||
font-size: 9pt;
|
|
||||||
margin: 2mm 0;
|
|
||||||
line-height: 1.4;
|
|
||||||
}
|
|
||||||
.issued-by .lbl { font-weight: 600; }
|
|
||||||
|
|
||||||
/* Upozorneni */
|
/* Upozorneni */
|
||||||
.notice {
|
.notice {
|
||||||
font-size: 8pt;
|
font-size: 8pt;
|
||||||
@@ -641,32 +590,42 @@ export function renderIssuedOrderHtml(
|
|||||||
line-height: 1.3;
|
line-height: 1.3;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Poznamky */
|
/* Sekce ("Obsah") — tecou inline za tabulkou polozek; zadna nova stranka,
|
||||||
.invoice-notes {
|
dlouhy obsah strankuje prirozene a paticku tiskne Puppeteer na kazdou
|
||||||
margin-top: 4mm;
|
stranu. */
|
||||||
font-size: 10pt;
|
.scope-sections {
|
||||||
line-height: 1.5;
|
margin-top: 6mm;
|
||||||
color: #1a1a1a;
|
|
||||||
}
|
}
|
||||||
.invoice-notes-label {
|
.scope-section {
|
||||||
font-weight: 600;
|
width: 100%;
|
||||||
font-size: 9pt;
|
max-width: 100%;
|
||||||
text-transform: uppercase;
|
margin-bottom: 3mm;
|
||||||
color: #555;
|
break-inside: avoid;
|
||||||
|
}
|
||||||
|
.scope-section-title {
|
||||||
|
font-size: 11pt;
|
||||||
|
font-weight: 700;
|
||||||
|
color: #1a1a1a;
|
||||||
margin-bottom: 1mm;
|
margin-bottom: 1mm;
|
||||||
}
|
}
|
||||||
.invoice-notes-content p { margin: 0 0 0.4em 0; }
|
.section-content {
|
||||||
.invoice-notes-content ul, .invoice-notes-content ol { margin: 0 0 0.4em 1.5em; }
|
color: #1a1a1a;
|
||||||
.invoice-notes-content li { margin-bottom: 0.2em; }
|
line-height: 1.5;
|
||||||
.invoice-notes-content,
|
word-break: normal;
|
||||||
.invoice-notes-content * {
|
overflow-wrap: anywhere;
|
||||||
|
}
|
||||||
|
.section-content,
|
||||||
|
.section-content * {
|
||||||
font-family: Tahoma, sans-serif !important;
|
font-family: Tahoma, sans-serif !important;
|
||||||
}
|
}
|
||||||
.invoice-notes-content { font-size: 14px; }
|
.section-content { font-size: 14px; }
|
||||||
.invoice-notes-content h1 { font-size: 20px; }
|
.section-content h1 { font-size: 20px; }
|
||||||
.invoice-notes-content h2 { font-size: 18px; }
|
.section-content h2 { font-size: 18px; }
|
||||||
.invoice-notes-content h3 { font-size: 16px; }
|
.section-content h3 { font-size: 16px; }
|
||||||
.invoice-notes-content h4 { font-size: 15px; }
|
.section-content h4 { font-size: 15px; }
|
||||||
|
.section-content p { margin: 0 0 0.4em 0; }
|
||||||
|
.section-content ul, .section-content ol { margin: 0 0 0.4em 1.5em; }
|
||||||
|
.section-content li { margin-bottom: 0.2em; }
|
||||||
|
|
||||||
/* Quill fonty – v PDF vynuceno Tahoma */
|
/* Quill fonty – v PDF vynuceno Tahoma */
|
||||||
[class*="ql-font-"] { font-family: Tahoma, sans-serif !important; }
|
[class*="ql-font-"] { font-family: Tahoma, sans-serif !important; }
|
||||||
@@ -677,6 +636,10 @@ ${indentCSS}
|
|||||||
|
|
||||||
@media print {
|
@media print {
|
||||||
body { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
|
body { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
|
||||||
|
/* The logo + red heading print via Puppeteer's headerTemplate on EVERY
|
||||||
|
page — hide the body copy so page 1 doesn't show it twice. The screen
|
||||||
|
(HTML preview) keeps the body header. */
|
||||||
|
.invoice-header { display: none; }
|
||||||
}
|
}
|
||||||
@media screen {
|
@media screen {
|
||||||
html { background: #525659; }
|
html { background: #525659; }
|
||||||
@@ -688,6 +651,7 @@ ${indentCSS}
|
|||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
|
gap: 30px;
|
||||||
min-height: 100vh;
|
min-height: 100vh;
|
||||||
overflow-x: hidden;
|
overflow-x: hidden;
|
||||||
}
|
}
|
||||||
@@ -715,7 +679,7 @@ ${indentCSS}
|
|||||||
<div class="invoice-title">${escapeHtml(t.heading)} ${poNumber}</div>
|
<div class="invoice-title">${escapeHtml(t.heading)} ${poNumber}</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Odberatel (nase firma) / Dodavatel (zakaznik) + Detaily -->
|
<!-- Odberatel (nase firma) / Dodavatel (sklad_suppliers) + Detaily -->
|
||||||
<table class="header-grid" cellspacing="0">
|
<table class="header-grid" cellspacing="0">
|
||||||
<tr>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
@@ -723,9 +687,9 @@ ${indentCSS}
|
|||||||
<div class="address-name">${escapeHtml(buyer.name)}</div>
|
<div class="address-name">${escapeHtml(buyer.name)}</div>
|
||||||
${buyerLinesHtml}
|
${buyerLinesHtml}
|
||||||
</td>
|
</td>
|
||||||
<td class="addr-customer">
|
<td class="addr-supplier">
|
||||||
<div class="address-label">${escapeHtml(t.supplier)}</div>
|
<div class="address-label">${escapeHtml(t.supplier)}</div>
|
||||||
<div class="address-name">${escapeHtml(supplier.name)}</div>
|
<div class="address-name">${escapeHtml(supplierAddr.name)}</div>
|
||||||
${supplierLinesHtml}
|
${supplierLinesHtml}
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
@@ -739,18 +703,15 @@ ${indentCSS}
|
|||||||
</table>
|
</table>
|
||||||
|
|
||||||
<!-- Polozky -->
|
<!-- Polozky -->
|
||||||
<div class="billing-label">${escapeHtml(t.billing)}</div>
|
<div class="billing-label">${escapeHtml(order.order_text || t.billing)}</div>
|
||||||
<table class="items">
|
<table class="items">
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th class="center" style="width:3%">${escapeHtml(t.col_no)}</th>
|
<th class="center" style="width:3%">${escapeHtml(t.col_no)}</th>
|
||||||
<th style="width:36%">${escapeHtml(t.col_desc)}</th>
|
<th style="width:56%">${escapeHtml(t.col_desc)}</th>
|
||||||
<th class="center" style="width:10%">${escapeHtml(t.col_qty)}</th>
|
<th class="center" style="width:10%">${escapeHtml(t.col_qty)}</th>
|
||||||
<th class="right" style="width:10%">${escapeHtml(t.col_unit_price)}</th>
|
<th class="right" style="width:10%">${escapeHtml(t.col_unit_price)}</th>
|
||||||
<th class="right" style="width:10%">${escapeHtml(t.col_price)}</th>
|
<th class="right" style="width:21%">${escapeHtml(t.col_total)}</th>
|
||||||
<th class="center" style="width:5%">${escapeHtml(t.col_vat_pct)}</th>
|
|
||||||
<th class="right" style="width:10%">${escapeHtml(t.col_vat)}</th>
|
|
||||||
<th class="right" style="width:16%">${escapeHtml(t.col_total)}</th>
|
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
@@ -758,34 +719,21 @@ ${indentCSS}
|
|||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
<!-- Soucty -->
|
<!-- Soucty (jen souhrnny radek bez DPH - mezisoucet by ho jen opakoval;
|
||||||
|
mena je soucasti castky pres formatCurrency, takze zadna "Částky jsou
|
||||||
|
uvedeny v …" poznamka) -->
|
||||||
<div class="totals-wrapper">
|
<div class="totals-wrapper">
|
||||||
<div class="totals">
|
<div class="totals">
|
||||||
<div class="detail-rows">
|
|
||||||
<div class="row">
|
|
||||||
<span class="label">${escapeHtml(t.subtotal)}</span>
|
|
||||||
<span class="value">${formatNum(subtotal)} ${escapeHtml(currency)}</span>
|
|
||||||
</div>${vatDetailHtml}
|
|
||||||
</div>
|
|
||||||
<div class="grand">
|
<div class="grand">
|
||||||
<span class="label">${escapeHtml(t.total)}</span>
|
<span class="label">${escapeHtml(t.total_no_vat)}</span>
|
||||||
<span class="value">${formatNum(totalToPay)} ${escapeHtml(currency)}</span>
|
<span class="value">${formatCurrency(total, currency)}</span>
|
||||||
</div>
|
</div>
|
||||||
<div class="currency-note">${escapeHtml(t.amounts_in)} ${escapeHtml(currency)}</div>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
${notesHtml}
|
${scopeHtml}
|
||||||
|
|
||||||
${termsBlock}
|
|
||||||
|
|
||||||
</div><!-- /.invoice-content -->
|
</div><!-- /.invoice-content -->
|
||||||
<div class="invoice-footer">
|
|
||||||
|
|
||||||
<!-- Vystavil (jmeno prihlaseneho uzivatele) -->
|
|
||||||
<div class="issued-by"><span class="lbl">${escapeHtml(t.issued_by)}</span> ${escapeHtml(issuer.name)}</div>
|
|
||||||
|
|
||||||
</div><!-- /.invoice-footer -->
|
|
||||||
</div><!-- /.invoice-page -->
|
</div><!-- /.invoice-page -->
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
@@ -804,7 +752,6 @@ export default async function issuedOrdersPdfRoutes(fastify: FastifyInstance) {
|
|||||||
const id = parseId(request.params.id, reply);
|
const id = parseId(request.params.id, reply);
|
||||||
if (id === null) return;
|
if (id === null) return;
|
||||||
const query = request.query as Record<string, string>;
|
const query = request.query as Record<string, string>;
|
||||||
const lang: Lang = query.lang === "en" ? "en" : "cs";
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const order = await prisma.issued_orders.findUnique({ where: { id } });
|
const order = await prisma.issued_orders.findUnique({ where: { id } });
|
||||||
@@ -814,13 +761,26 @@ export default async function issuedOrdersPdfRoutes(fastify: FastifyInstance) {
|
|||||||
.type("text/html")
|
.type("text/html")
|
||||||
.send("<html><body><h1>Objednávka nenalezena</h1></body></html>");
|
.send("<html><body><h1>Objednávka nenalezena</h1></body></html>");
|
||||||
}
|
}
|
||||||
|
// Language comes from the document's own column; ?lang= is an
|
||||||
|
// explicit override only (mirrors the /:id/file fallback semantics).
|
||||||
|
const lang: Lang =
|
||||||
|
query.lang === "en" || query.lang === "cs"
|
||||||
|
? query.lang
|
||||||
|
: order.language === "en"
|
||||||
|
? "en"
|
||||||
|
: "cs";
|
||||||
const items = await prisma.issued_order_items.findMany({
|
const items = await prisma.issued_order_items.findMany({
|
||||||
where: { issued_order_id: id },
|
where: { issued_order_id: id },
|
||||||
orderBy: { position: "asc" },
|
orderBy: { position: "asc" },
|
||||||
});
|
});
|
||||||
const customer = order.customer_id
|
// id tiebreak: position can repeat, keep the order deterministic.
|
||||||
? ((await prisma.customers.findUnique({
|
const sections = await prisma.issued_order_sections.findMany({
|
||||||
where: { id: order.customer_id },
|
where: { issued_order_id: id },
|
||||||
|
orderBy: [{ position: "asc" }, { id: "asc" }],
|
||||||
|
});
|
||||||
|
const supplier = order.supplier_id
|
||||||
|
? ((await prisma.sklad_suppliers.findUnique({
|
||||||
|
where: { id: order.supplier_id },
|
||||||
})) as Record<string, unknown> | null)
|
})) as Record<string, unknown> | null)
|
||||||
: null;
|
: null;
|
||||||
const settings = (await prisma.company_settings.findFirst()) as Record<
|
const settings = (await prisma.company_settings.findFirst()) as Record<
|
||||||
@@ -838,10 +798,11 @@ export default async function issuedOrdersPdfRoutes(fastify: FastifyInstance) {
|
|||||||
const html = renderIssuedOrderHtml(
|
const html = renderIssuedOrderHtml(
|
||||||
order,
|
order,
|
||||||
items,
|
items,
|
||||||
customer,
|
supplier,
|
||||||
settings,
|
settings,
|
||||||
lang,
|
lang,
|
||||||
issuer,
|
issuer,
|
||||||
|
sections,
|
||||||
);
|
);
|
||||||
|
|
||||||
// ?save=1 → archive the PDF to NAS instead of returning it (mirrors
|
// ?save=1 → archive the PDF to NAS instead of returning it (mirrors
|
||||||
@@ -852,7 +813,14 @@ export default async function issuedOrdersPdfRoutes(fastify: FastifyInstance) {
|
|||||||
? new Date(order.order_date)
|
? new Date(order.order_date)
|
||||||
: new Date();
|
: new Date();
|
||||||
nasOrdersManager.cleanIssued(order.po_number);
|
nasOrdersManager.cleanIssued(order.po_number);
|
||||||
const pdfBuffer = await htmlToPdf(html);
|
const pdfBuffer = await htmlToPdf(html, {
|
||||||
|
headerTemplate: buildIssuedOrderPdfHeader(
|
||||||
|
lang,
|
||||||
|
logoDataUriFromSettings(settings),
|
||||||
|
order.po_number || "",
|
||||||
|
),
|
||||||
|
footerTemplate: buildIssuedOrderPdfFooter(lang, issuer.name),
|
||||||
|
});
|
||||||
nasOrdersManager.saveIssuedPdf(
|
nasOrdersManager.saveIssuedPdf(
|
||||||
order.po_number,
|
order.po_number,
|
||||||
baseDate.getFullYear(),
|
baseDate.getFullYear(),
|
||||||
@@ -862,7 +830,14 @@ export default async function issuedOrdersPdfRoutes(fastify: FastifyInstance) {
|
|||||||
return success(reply, null, 200, "PDF uloženo");
|
return success(reply, null, 200, "PDF uloženo");
|
||||||
}
|
}
|
||||||
|
|
||||||
const pdf = await htmlToPdf(html);
|
const pdf = await htmlToPdf(html, {
|
||||||
|
headerTemplate: buildIssuedOrderPdfHeader(
|
||||||
|
lang,
|
||||||
|
logoDataUriFromSettings(settings),
|
||||||
|
order.po_number || "",
|
||||||
|
),
|
||||||
|
footerTemplate: buildIssuedOrderPdfFooter(lang, issuer.name),
|
||||||
|
});
|
||||||
const filename = `Objednavka-${order.po_number || String(id)}.pdf`;
|
const filename = `Objednavka-${order.po_number || String(id)}.pdf`;
|
||||||
return reply
|
return reply
|
||||||
.type("application/pdf")
|
.type("application/pdf")
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { FastifyInstance } from "fastify";
|
import { FastifyInstance } from "fastify";
|
||||||
import { requirePermission } from "../../middleware/auth";
|
import prisma from "../../config/database";
|
||||||
|
import { requirePermission, requireAnyPermission } from "../../middleware/auth";
|
||||||
import { logAudit } from "../../services/audit";
|
import { logAudit } from "../../services/audit";
|
||||||
import { success, error, parseId, paginated } from "../../utils/response";
|
import { success, error, parseId, paginated } from "../../utils/response";
|
||||||
import { parsePagination, buildPaginationMeta } from "../../utils/pagination";
|
import { parsePagination, buildPaginationMeta } from "../../utils/pagination";
|
||||||
@@ -17,6 +18,20 @@ import {
|
|||||||
deleteIssuedOrder,
|
deleteIssuedOrder,
|
||||||
getNextIssuedOrderNumberPreview,
|
getNextIssuedOrderNumberPreview,
|
||||||
} from "../../services/issued-orders.service";
|
} from "../../services/issued-orders.service";
|
||||||
|
import {
|
||||||
|
renderIssuedOrderHtml,
|
||||||
|
buildIssuedOrderPdfFooter,
|
||||||
|
buildIssuedOrderPdfHeader,
|
||||||
|
} from "./issued-orders-pdf";
|
||||||
|
import { htmlToPdf } from "../../utils/html-to-pdf";
|
||||||
|
import { logoDataUriFromSettings } from "../../utils/pdf-shared";
|
||||||
|
import { nasOrdersManager } from "../../services/nas-financials-manager";
|
||||||
|
import { contentDisposition } from "../../utils/content-disposition";
|
||||||
|
|
||||||
|
// 3× the client's 10s heartbeat: a single missed beat (background-tab
|
||||||
|
// throttling, transient network) must not silently hand the lock to a second
|
||||||
|
// editor. Keep in sync with quotations.ts and useDocumentLock.
|
||||||
|
const LOCK_TIMEOUT_MS = 30 * 1000;
|
||||||
|
|
||||||
export default async function issuedOrdersRoutes(fastify: FastifyInstance) {
|
export default async function issuedOrdersRoutes(fastify: FastifyInstance) {
|
||||||
// GET /api/admin/issued-orders
|
// GET /api/admin/issued-orders
|
||||||
@@ -25,16 +40,16 @@ export default async function issuedOrdersRoutes(fastify: FastifyInstance) {
|
|||||||
{ preHandler: requirePermission("orders.view") },
|
{ preHandler: requirePermission("orders.view") },
|
||||||
async (request, reply) => {
|
async (request, reply) => {
|
||||||
const query = request.query as Record<string, unknown>;
|
const query = request.query as Record<string, unknown>;
|
||||||
const { page, limit, skip, order, search } = parsePagination(query);
|
const { page, limit, skip, sort, order, search } = parsePagination(query);
|
||||||
const result = await listIssuedOrders({
|
const result = await listIssuedOrders({
|
||||||
page,
|
page,
|
||||||
limit,
|
limit,
|
||||||
skip,
|
skip,
|
||||||
sort: String(query.sort || ""),
|
sort,
|
||||||
order,
|
order,
|
||||||
search,
|
search,
|
||||||
status: query.status ? String(query.status) : undefined,
|
status: query.status ? String(query.status) : undefined,
|
||||||
customer_id: query.customer_id ? Number(query.customer_id) : undefined,
|
supplier_id: query.supplier_id ? Number(query.supplier_id) : undefined,
|
||||||
month: query.month ? Number(query.month) : undefined,
|
month: query.month ? Number(query.month) : undefined,
|
||||||
year: query.year ? Number(query.year) : undefined,
|
year: query.year ? Number(query.year) : undefined,
|
||||||
});
|
});
|
||||||
@@ -55,9 +70,10 @@ export default async function issuedOrdersRoutes(fastify: FastifyInstance) {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
// GET /api/admin/issued-orders/stats — per-currency total (incl. VAT) summed
|
// GET /api/admin/issued-orders/stats — per-currency NET total summed over
|
||||||
// over the WHOLE filtered set (not one page). Registered BEFORE "/:id" so the
|
// the WHOLE filtered set (not one page). Issued orders are not tax documents
|
||||||
// literal "stats" path isn't captured as an order id.
|
// — no VAT anywhere. Registered BEFORE "/:id" so the literal "stats" path
|
||||||
|
// isn't captured as an order id.
|
||||||
fastify.get(
|
fastify.get(
|
||||||
"/stats",
|
"/stats",
|
||||||
{ preHandler: requirePermission("orders.view") },
|
{ preHandler: requirePermission("orders.view") },
|
||||||
@@ -66,7 +82,7 @@ export default async function issuedOrdersRoutes(fastify: FastifyInstance) {
|
|||||||
const result = await getIssuedOrderTotals({
|
const result = await getIssuedOrderTotals({
|
||||||
search: query.search ? String(query.search) : undefined,
|
search: query.search ? String(query.search) : undefined,
|
||||||
status: query.status ? String(query.status) : undefined,
|
status: query.status ? String(query.status) : undefined,
|
||||||
customer_id: query.customer_id ? Number(query.customer_id) : undefined,
|
supplier_id: query.supplier_id ? Number(query.supplier_id) : undefined,
|
||||||
month: query.month ? Number(query.month) : undefined,
|
month: query.month ? Number(query.month) : undefined,
|
||||||
year: query.year ? Number(query.year) : undefined,
|
year: query.year ? Number(query.year) : undefined,
|
||||||
});
|
});
|
||||||
@@ -74,6 +90,40 @@ export default async function issuedOrdersRoutes(fastify: FastifyInstance) {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// GET /api/admin/issued-orders/suppliers — lightweight supplier lookup for
|
||||||
|
// the PO supplier picker. Guarded by the ORDERS permissions (any of
|
||||||
|
// view/create/edit), NOT warehouse.manage: the warehouse suppliers CRUD is
|
||||||
|
// gated by warehouse.manage, which orders users typically lack — without
|
||||||
|
// this endpoint they couldn't populate the picker. Registered BEFORE "/:id"
|
||||||
|
// so the literal "suppliers" path isn't captured as an order id.
|
||||||
|
fastify.get(
|
||||||
|
"/suppliers",
|
||||||
|
{
|
||||||
|
preHandler: requireAnyPermission(
|
||||||
|
"orders.view",
|
||||||
|
"orders.create",
|
||||||
|
"orders.edit",
|
||||||
|
),
|
||||||
|
},
|
||||||
|
async (_request, reply) => {
|
||||||
|
const suppliers = await prisma.sklad_suppliers.findMany({
|
||||||
|
where: { is_active: true },
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
name: true,
|
||||||
|
ico: true,
|
||||||
|
dic: true,
|
||||||
|
address: true,
|
||||||
|
email: true,
|
||||||
|
phone: true,
|
||||||
|
},
|
||||||
|
// id tiebreak so same-name suppliers sort deterministically.
|
||||||
|
orderBy: [{ name: "asc" }, { id: "asc" }],
|
||||||
|
});
|
||||||
|
return success(reply, suppliers);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
// GET /api/admin/issued-orders/:id
|
// GET /api/admin/issued-orders/:id
|
||||||
fastify.get<{ Params: { id: string } }>(
|
fastify.get<{ Params: { id: string } }>(
|
||||||
"/:id",
|
"/:id",
|
||||||
@@ -83,7 +133,222 @@ export default async function issuedOrdersRoutes(fastify: FastifyInstance) {
|
|||||||
if (id === null) return;
|
if (id === null) return;
|
||||||
const order = await getIssuedOrder(id);
|
const order = await getIssuedOrder(id);
|
||||||
if (!order) return error(reply, "Objednávka nenalezena", 404);
|
if (!order) return error(reply, "Objednávka nenalezena", 404);
|
||||||
return success(reply, order);
|
|
||||||
|
// `getIssuedOrder` already loaded locked_by/locked_at — enrich locked_by
|
||||||
|
// to {user_id, username, full_name} ONLY when the lock is fresh and held
|
||||||
|
// by ANOTHER user (same shape as offers so the frontend lock hook can be
|
||||||
|
// shared); otherwise null.
|
||||||
|
let lockedBy: {
|
||||||
|
user_id: number;
|
||||||
|
username: string;
|
||||||
|
full_name: string;
|
||||||
|
} | null = null;
|
||||||
|
if (order.locked_by && order.locked_at) {
|
||||||
|
const lockAge = Date.now() - new Date(order.locked_at).getTime();
|
||||||
|
if (
|
||||||
|
lockAge < LOCK_TIMEOUT_MS &&
|
||||||
|
order.locked_by !== request.authData!.userId
|
||||||
|
) {
|
||||||
|
const lockUser = await prisma.users.findUnique({
|
||||||
|
where: { id: order.locked_by },
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
username: true,
|
||||||
|
first_name: true,
|
||||||
|
last_name: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
if (lockUser) {
|
||||||
|
lockedBy = {
|
||||||
|
user_id: lockUser.id,
|
||||||
|
username: lockUser.username,
|
||||||
|
full_name: `${lockUser.first_name} ${lockUser.last_name}`.trim(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return success(reply, { ...order, locked_by: lockedBy });
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
// POST /api/admin/issued-orders/:id/lock — acquire edit lock (mirrors offers)
|
||||||
|
fastify.post<{ Params: { id: string } }>(
|
||||||
|
"/:id/lock",
|
||||||
|
{ preHandler: requirePermission("orders.edit") },
|
||||||
|
async (request, reply) => {
|
||||||
|
const id = parseId(request.params.id, reply);
|
||||||
|
if (id === null) return;
|
||||||
|
|
||||||
|
const order = await prisma.issued_orders.findUnique({
|
||||||
|
where: { id },
|
||||||
|
select: { locked_by: true, locked_at: true },
|
||||||
|
});
|
||||||
|
if (!order) return error(reply, "Objednávka nenalezena", 404);
|
||||||
|
|
||||||
|
// Check if locked by someone else and lock is fresh
|
||||||
|
if (order.locked_by && order.locked_at) {
|
||||||
|
const lockAge = Date.now() - new Date(order.locked_at).getTime();
|
||||||
|
if (
|
||||||
|
lockAge < LOCK_TIMEOUT_MS &&
|
||||||
|
order.locked_by !== request.authData!.userId
|
||||||
|
) {
|
||||||
|
const lockUser = await prisma.users.findUnique({
|
||||||
|
where: { id: order.locked_by },
|
||||||
|
select: { first_name: true, last_name: true },
|
||||||
|
});
|
||||||
|
return error(
|
||||||
|
reply,
|
||||||
|
`Objednávku právě upravuje ${lockUser ? `${lockUser.first_name} ${lockUser.last_name}`.trim() : "jiný uživatel"}`,
|
||||||
|
423,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
await prisma.issued_orders.update({
|
||||||
|
where: { id },
|
||||||
|
data: { locked_by: request.authData!.userId, locked_at: new Date() },
|
||||||
|
});
|
||||||
|
|
||||||
|
return success(reply, null, 200, "Zámek nastaven");
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
// POST /api/admin/issued-orders/:id/heartbeat — keep lock alive
|
||||||
|
fastify.post<{ Params: { id: string } }>(
|
||||||
|
"/:id/heartbeat",
|
||||||
|
{ preHandler: requirePermission("orders.edit") },
|
||||||
|
async (request, reply) => {
|
||||||
|
const id = parseId(request.params.id, reply);
|
||||||
|
if (id === null) return;
|
||||||
|
|
||||||
|
await prisma.issued_orders.updateMany({
|
||||||
|
where: { id, locked_by: request.authData!.userId },
|
||||||
|
data: { locked_at: new Date() },
|
||||||
|
});
|
||||||
|
|
||||||
|
return success(reply, null);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
// POST /api/admin/issued-orders/:id/unlock — release lock
|
||||||
|
fastify.post<{ Params: { id: string } }>(
|
||||||
|
"/:id/unlock",
|
||||||
|
{ preHandler: requirePermission("orders.edit") },
|
||||||
|
async (request, reply) => {
|
||||||
|
const id = parseId(request.params.id, reply);
|
||||||
|
if (id === null) return;
|
||||||
|
|
||||||
|
await prisma.issued_orders.updateMany({
|
||||||
|
where: { id, locked_by: request.authData!.userId },
|
||||||
|
data: { locked_by: null, locked_at: null },
|
||||||
|
});
|
||||||
|
|
||||||
|
return success(reply, null);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
// GET /api/admin/issued-orders/:id/file — serve the archived PDF from the
|
||||||
|
// NAS; on an archive miss, fall back to a LIVE render from current data,
|
||||||
|
// re-archive the fresh PDF, and serve it (archived-file model with live
|
||||||
|
// fallback — same model as offers' /:id/file).
|
||||||
|
fastify.get<{ Params: { id: string } }>(
|
||||||
|
"/:id/file",
|
||||||
|
{ preHandler: requirePermission("orders.view") },
|
||||||
|
async (request, reply) => {
|
||||||
|
const id = parseId(request.params.id, reply);
|
||||||
|
if (id === null) return;
|
||||||
|
|
||||||
|
const order = await prisma.issued_orders.findUnique({ where: { id } });
|
||||||
|
// A draft has no po_number → nothing was ever archived; 404 like offers.
|
||||||
|
if (!order?.po_number) return error(reply, "Objednávka nenalezena", 404);
|
||||||
|
|
||||||
|
const fileName = `Objednavka-${order.po_number}.pdf`;
|
||||||
|
|
||||||
|
// Archive hit: the by-number sweep finds the PDF in whichever
|
||||||
|
// Vydané/YYYY/MM folder it was saved to (order_date can move after
|
||||||
|
// archiving, so a fixed expected path would miss).
|
||||||
|
const archived = nasOrdersManager.readIssuedByNumber(order.po_number);
|
||||||
|
if (archived) {
|
||||||
|
return reply
|
||||||
|
.type("application/pdf")
|
||||||
|
.header("Content-Disposition", contentDisposition("inline", fileName))
|
||||||
|
.send(archived.data);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Archive miss → live render (same data path as issued-orders-pdf),
|
||||||
|
// then archive the fresh PDF so the next request is a plain hit.
|
||||||
|
try {
|
||||||
|
const items = await prisma.issued_order_items.findMany({
|
||||||
|
where: { issued_order_id: id },
|
||||||
|
orderBy: { position: "asc" },
|
||||||
|
});
|
||||||
|
const sections = await prisma.issued_order_sections.findMany({
|
||||||
|
where: { issued_order_id: id },
|
||||||
|
orderBy: [{ position: "asc" }, { id: "asc" }],
|
||||||
|
});
|
||||||
|
const supplier = order.supplier_id
|
||||||
|
? ((await prisma.sklad_suppliers.findUnique({
|
||||||
|
where: { id: order.supplier_id },
|
||||||
|
})) as Record<string, unknown> | null)
|
||||||
|
: null;
|
||||||
|
const settings = (await prisma.company_settings.findFirst()) as Record<
|
||||||
|
string,
|
||||||
|
unknown
|
||||||
|
> | null;
|
||||||
|
const lang =
|
||||||
|
order.language === "en" ? ("en" as const) : ("cs" as const);
|
||||||
|
const issuer = {
|
||||||
|
name: `${request.authData?.firstName ?? ""} ${request.authData?.lastName ?? ""}`.trim(),
|
||||||
|
};
|
||||||
|
|
||||||
|
const html = renderIssuedOrderHtml(
|
||||||
|
order,
|
||||||
|
items,
|
||||||
|
supplier,
|
||||||
|
settings,
|
||||||
|
lang,
|
||||||
|
issuer,
|
||||||
|
sections,
|
||||||
|
);
|
||||||
|
const pdf = await htmlToPdf(html, {
|
||||||
|
headerTemplate: buildIssuedOrderPdfHeader(
|
||||||
|
lang,
|
||||||
|
logoDataUriFromSettings(settings),
|
||||||
|
order.po_number || "",
|
||||||
|
),
|
||||||
|
footerTemplate: buildIssuedOrderPdfFooter(lang, issuer.name),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (nasOrdersManager.isConfigured()) {
|
||||||
|
const baseDate = order.order_date
|
||||||
|
? new Date(order.order_date)
|
||||||
|
: new Date();
|
||||||
|
nasOrdersManager.cleanIssued(order.po_number);
|
||||||
|
const saved = nasOrdersManager.saveIssuedPdf(
|
||||||
|
order.po_number,
|
||||||
|
baseDate.getFullYear(),
|
||||||
|
baseDate.getMonth() + 1,
|
||||||
|
pdf,
|
||||||
|
);
|
||||||
|
if (!saved) {
|
||||||
|
request.log.error(
|
||||||
|
`Archivace PDF objednávky ${order.po_number} na NAS při /file fallbacku selhala`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return reply
|
||||||
|
.type("application/pdf")
|
||||||
|
.header("Content-Disposition", contentDisposition("inline", fileName))
|
||||||
|
.send(pdf);
|
||||||
|
} catch (err) {
|
||||||
|
request.log.error(
|
||||||
|
err,
|
||||||
|
"Issued order /file live-render fallback failed",
|
||||||
|
);
|
||||||
|
return error(reply, "Chyba při generování PDF", 500);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -95,6 +360,13 @@ export default async function issuedOrdersRoutes(fastify: FastifyInstance) {
|
|||||||
const parsed = parseBody(CreateIssuedOrderSchema, request.body);
|
const parsed = parseBody(CreateIssuedOrderSchema, request.body);
|
||||||
if ("error" in parsed) return error(reply, parsed.error, 400);
|
if ("error" in parsed) return error(reply, parsed.error, 400);
|
||||||
const order = await createIssuedOrder(parsed.data);
|
const order = await createIssuedOrder(parsed.data);
|
||||||
|
if ("error" in order) {
|
||||||
|
if (order.error === "supplier_not_found")
|
||||||
|
return error(reply, "Dodavatel nenalezen", 400);
|
||||||
|
if (order.error === "po_number_taken")
|
||||||
|
return error(reply, "Číslo objednávky je již použito", 409);
|
||||||
|
return error(reply, "Neznámá chyba", 500);
|
||||||
|
}
|
||||||
await logAudit({
|
await logAudit({
|
||||||
request,
|
request,
|
||||||
authData: request.authData,
|
authData: request.authData,
|
||||||
@@ -125,6 +397,10 @@ export default async function issuedOrdersRoutes(fastify: FastifyInstance) {
|
|||||||
if ("error" in result) {
|
if ("error" in result) {
|
||||||
if (result.error === "not_found")
|
if (result.error === "not_found")
|
||||||
return error(reply, "Objednávka nenalezena", 404);
|
return error(reply, "Objednávka nenalezena", 404);
|
||||||
|
if (result.error === "not_editable")
|
||||||
|
return error(reply, "Objednávku v tomto stavu nelze upravovat", 400);
|
||||||
|
if (result.error === "supplier_not_found")
|
||||||
|
return error(reply, "Dodavatel nenalezen", 400);
|
||||||
if (result.error === "invalid_transition")
|
if (result.error === "invalid_transition")
|
||||||
return error(
|
return error(
|
||||||
reply,
|
reply,
|
||||||
@@ -139,9 +415,18 @@ export default async function issuedOrdersRoutes(fastify: FastifyInstance) {
|
|||||||
action: "update",
|
action: "update",
|
||||||
entityType: "issued_order",
|
entityType: "issued_order",
|
||||||
entityId: id,
|
entityId: id,
|
||||||
description: `Upravena vydaná objednávka ${result.po_number}`,
|
description: `Upravena vydaná objednávka ${result.po_number ?? "koncept"}`,
|
||||||
|
oldValues: result.oldValues,
|
||||||
|
newValues: result.newValues,
|
||||||
});
|
});
|
||||||
return success(reply, { id }, 200, "Objednávka byla aktualizována");
|
// po_number in the response so a draft->sent finalize immediately shows
|
||||||
|
// the assigned number (POST already returns it).
|
||||||
|
return success(
|
||||||
|
reply,
|
||||||
|
{ id, po_number: result.po_number },
|
||||||
|
200,
|
||||||
|
"Objednávka byla aktualizována",
|
||||||
|
);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -160,7 +445,8 @@ export default async function issuedOrdersRoutes(fastify: FastifyInstance) {
|
|||||||
action: "delete",
|
action: "delete",
|
||||||
entityType: "issued_order",
|
entityType: "issued_order",
|
||||||
entityId: id,
|
entityId: id,
|
||||||
description: `Smazána vydaná objednávka ${existing.po_number}`,
|
description: `Smazána vydaná objednávka ${existing.po_number ?? "koncept"}`,
|
||||||
|
oldValues: existing as unknown as Record<string, unknown>,
|
||||||
});
|
});
|
||||||
return success(reply, null, 200, "Objednávka smazána");
|
return success(reply, null, 200, "Objednávka smazána");
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,102 +1,24 @@
|
|||||||
import { FastifyInstance } from "fastify";
|
import { FastifyInstance } from "fastify";
|
||||||
|
import type { Prisma, company_settings } from "@prisma/client";
|
||||||
import prisma from "../../config/database";
|
import prisma from "../../config/database";
|
||||||
import { requirePermission } from "../../middleware/auth";
|
import { requirePermission } from "../../middleware/auth";
|
||||||
import { localDateCzStr } from "../../utils/date";
|
|
||||||
import { nasOffersManager } from "../../services/nas-offers-manager";
|
import { nasOffersManager } from "../../services/nas-offers-manager";
|
||||||
import { htmlToPdf } from "../../utils/html-to-pdf";
|
import { htmlToPdf } from "../../utils/html-to-pdf";
|
||||||
import { parseId, success } from "../../utils/response";
|
import { parseId, success } from "../../utils/response";
|
||||||
|
import {
|
||||||
|
formatDate,
|
||||||
|
formatNum,
|
||||||
|
formatCurrency,
|
||||||
|
escapeHtml,
|
||||||
|
cleanQuillHtml,
|
||||||
|
buildQuillIndentCss,
|
||||||
|
} from "../../utils/pdf-shared";
|
||||||
import createDOMPurify from "dompurify";
|
import createDOMPurify from "dompurify";
|
||||||
import { JSDOM } from "jsdom";
|
import { JSDOM } from "jsdom";
|
||||||
|
|
||||||
const window = new JSDOM("").window;
|
const window = new JSDOM("").window;
|
||||||
const DOMPurify = createDOMPurify(window);
|
const DOMPurify = createDOMPurify(window);
|
||||||
|
|
||||||
function formatDate(date: Date | string | null | undefined): string {
|
|
||||||
if (!date) return "";
|
|
||||||
const d = new Date(date);
|
|
||||||
if (isNaN(d.getTime())) return String(date);
|
|
||||||
return localDateCzStr(d);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Format number with comma decimal separator and non-breaking space thousands separator */
|
|
||||||
function formatNum(n: number, decimals: number): string {
|
|
||||||
const abs = Math.abs(n);
|
|
||||||
const fixed = abs.toFixed(decimals);
|
|
||||||
const [intPart, decPart] = fixed.split(".");
|
|
||||||
const withSep = intPart.replace(/\B(?=(\d{3})+(?!\d))/g, "\u00A0");
|
|
||||||
const result = decPart ? `${withSep},${decPart}` : withSep;
|
|
||||||
return n < 0 ? `-${result}` : result;
|
|
||||||
}
|
|
||||||
|
|
||||||
function formatCurrency(amount: number, currency: string): string {
|
|
||||||
const n = Number(amount) || 0;
|
|
||||||
switch (currency) {
|
|
||||||
case "EUR":
|
|
||||||
return `${formatNum(n, 2)} \u20AC`;
|
|
||||||
case "USD":
|
|
||||||
return `$${Math.abs(n)
|
|
||||||
.toFixed(2)
|
|
||||||
.replace(/\B(?=(\d{3})+(?!\d))/g, ",")}`;
|
|
||||||
case "CZK":
|
|
||||||
return `${formatNum(n, 2)} K\u010D`;
|
|
||||||
case "GBP":
|
|
||||||
return `\u00A3${Math.abs(n)
|
|
||||||
.toFixed(2)
|
|
||||||
.replace(/\B(?=(\d{3})+(?!\d))/g, ",")}`;
|
|
||||||
default:
|
|
||||||
return `${formatNum(n, 2)} ${currency}`;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function escapeHtml(str: string | null | undefined): string {
|
|
||||||
if (!str) return "";
|
|
||||||
return str
|
|
||||||
.replace(/&/g, "&")
|
|
||||||
.replace(/</g, "<")
|
|
||||||
.replace(/>/g, ">")
|
|
||||||
.replace(/"/g, """);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Sanitize Quill HTML: keep safe tags, remove event handlers, merge adjacent spans */
|
|
||||||
function cleanQuillHtml(html: string | null | undefined): string {
|
|
||||||
if (!html) return "";
|
|
||||||
const allowedTags =
|
|
||||||
"<p><br><strong><em><u><s><ul><ol><li><span><sub><sup><a><h1><h2><h3><h4><blockquote><pre>";
|
|
||||||
let s = html;
|
|
||||||
// Remove dangerous tags with content
|
|
||||||
s = s.replace(
|
|
||||||
/<(script|iframe|object|embed|style|link|meta|base|form|input|textarea|button|select|svg|math)[^>]*>[\s\S]*?<\/\1>/gi,
|
|
||||||
"",
|
|
||||||
);
|
|
||||||
s = s.replace(
|
|
||||||
/<(script|iframe|object|embed|style|link|meta|base|form|input|textarea|button|select|svg|math)[^>]*\/?>/gi,
|
|
||||||
"",
|
|
||||||
);
|
|
||||||
// Strip event handlers
|
|
||||||
s = s.replace(/\s+on\w+\s*=\s*("[^"]*"|'[^']*'|[^\s>]*)/gi, "");
|
|
||||||
s = s.replace(/\s+on\w+\s*=\s*[^\s>]*/gi, "");
|
|
||||||
// Strip javascript:/data:/vbscript: in href and src (defense-in-depth,
|
|
||||||
// matching the invoices/orders copies — DOMPurify runs first).
|
|
||||||
s = s.replace(
|
|
||||||
/href\s*=\s*["']?\s*(javascript|data|vbscript)\s*:[^"'>\s]*/gi,
|
|
||||||
'href="#"',
|
|
||||||
);
|
|
||||||
s = s.replace(
|
|
||||||
/src\s*=\s*["']?\s*(javascript|data|vbscript)\s*:[^"'>\s]*/gi,
|
|
||||||
'src=""',
|
|
||||||
);
|
|
||||||
// Replace with regular space (outside of tags)
|
|
||||||
s = s.replace(/( )/g, " ");
|
|
||||||
s = s.replace(/\s+style\s*=\s*("[^"]*"|'[^']*'|[^\s>]*)/gi, "");
|
|
||||||
// Merge adjacent spans with same attributes
|
|
||||||
let prev = "";
|
|
||||||
while (prev !== s) {
|
|
||||||
prev = s;
|
|
||||||
s = s.replace(/<span([^>]*)>(.*?)<\/span>\s*<span\1>/gs, "<span$1>$2");
|
|
||||||
}
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface AddressResult {
|
interface AddressResult {
|
||||||
name: string;
|
name: string;
|
||||||
lines: string[];
|
lines: string[];
|
||||||
@@ -203,144 +125,124 @@ const TRANSLATIONS: Record<string, Record<string, string>> = {
|
|||||||
unit_price: { EN: "Unit Price", CZ: "Jedn. cena" },
|
unit_price: { EN: "Unit Price", CZ: "Jedn. cena" },
|
||||||
included: { EN: "Included", CZ: "Zahrnuto" },
|
included: { EN: "Included", CZ: "Zahrnuto" },
|
||||||
total: { EN: "Total", CZ: "Celkem" },
|
total: { EN: "Total", CZ: "Celkem" },
|
||||||
subtotal: { EN: "Subtotal", CZ: "Mezisou\u010Det" },
|
total_no_vat: { EN: "Total excl. VAT", CZ: "Celkem bez DPH" },
|
||||||
vat: { EN: "VAT", CZ: "DPH" },
|
|
||||||
total_to_pay: { EN: "Total to pay", CZ: "Celkem k \u00FAhrad\u011B" },
|
|
||||||
ico: { EN: "ID", CZ: "I\u010CO" },
|
ico: { EN: "ID", CZ: "I\u010CO" },
|
||||||
dic: { EN: "VAT ID", CZ: "DI\u010C" },
|
dic: { EN: "VAT ID", CZ: "DI\u010C" },
|
||||||
page: { EN: "Page", CZ: "Strana" },
|
page: { EN: "Page", CZ: "Strana" },
|
||||||
of: { EN: "of", CZ: "z" },
|
of: { EN: "of", CZ: "z" },
|
||||||
};
|
};
|
||||||
|
|
||||||
export default async function offersPdfRoutes(
|
/** Quotation row with the relations the PDF template renders. */
|
||||||
fastify: FastifyInstance,
|
export type OfferForPdf = Prisma.quotationsGetPayload<{
|
||||||
): Promise<void> {
|
include: {
|
||||||
fastify.get<{ Params: { id: string } }>(
|
customers: true;
|
||||||
"/:id",
|
quotation_items: true;
|
||||||
{ preHandler: requirePermission("offers.view") },
|
scope_sections: true;
|
||||||
async (request, reply) => {
|
};
|
||||||
const id = parseId(request.params.id, reply);
|
}>;
|
||||||
if (id === null) return;
|
|
||||||
const query = request.query as Record<string, string>;
|
|
||||||
|
|
||||||
try {
|
/**
|
||||||
const quotation = await prisma.quotations.findUnique({
|
* Build the full offer PDF/preview HTML. Extracted from the route handler so
|
||||||
where: { id },
|
* the offers /:id/file live-render fallback (quotations.ts) can reuse the
|
||||||
include: {
|
* exact same template without going through HTTP.
|
||||||
customers: true,
|
*/
|
||||||
quotation_items: { orderBy: { position: "asc" } },
|
export function renderOfferHtml(
|
||||||
scope_sections: { orderBy: { position: "asc" } },
|
quotation: OfferForPdf,
|
||||||
},
|
settings: company_settings | null,
|
||||||
});
|
): string {
|
||||||
|
const isCzech = (quotation.language ?? "EN") !== "EN";
|
||||||
|
const langKey = isCzech ? "CZ" : "EN";
|
||||||
|
const currency = quotation.currency || "EUR";
|
||||||
|
const t = (key: string): string => TRANSLATIONS[key]?.[langKey] || key;
|
||||||
|
|
||||||
if (!quotation) {
|
let logoImg = "";
|
||||||
return reply
|
if (settings?.logo_data) {
|
||||||
.status(404)
|
const buf = Buffer.from(settings.logo_data);
|
||||||
.type("text/html")
|
let mime = "image/png";
|
||||||
.send("<html><body><h1>Nab\u00EDdka nenalezena</h1></body></html>");
|
if (buf[0] === 0xff && buf[1] === 0xd8) mime = "image/jpeg";
|
||||||
}
|
else if (buf[0] === 0x47 && buf[1] === 0x49) mime = "image/gif";
|
||||||
|
else if (buf[0] === 0x52 && buf[1] === 0x49) mime = "image/webp";
|
||||||
|
logoImg = `<img src="data:${escapeHtml(mime)};base64,${buf.toString("base64")}" class="logo" />`;
|
||||||
|
}
|
||||||
|
|
||||||
const settings = await prisma.company_settings.findFirst();
|
// Offers are NOT tax documents — the total is NET only (no VAT math).
|
||||||
const isCzech = (quotation.language ?? "EN") !== "EN";
|
const items = quotation.quotation_items;
|
||||||
const langKey = isCzech ? "CZ" : "EN";
|
let total = 0;
|
||||||
const currency = quotation.currency || "EUR";
|
for (const item of items) {
|
||||||
const t = (key: string): string => TRANSLATIONS[key]?.[langKey] || key;
|
if (item.is_included_in_total !== false) {
|
||||||
|
total += (Number(item.quantity) || 0) * (Number(item.unit_price) || 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Visibility uses the SAME per-language title rule as the render loop plus
|
||||||
|
// a tag-stripped content check (an empty-Quill "<p><br></p>" doesn't count)
|
||||||
|
// — otherwise a section with only the other language's title would count as
|
||||||
|
// content and produce a scope page holding nothing but the header. Mirrors
|
||||||
|
// issued-orders-pdf.
|
||||||
|
const stripSectionTags = (html: string | null | undefined): string =>
|
||||||
|
(html || "")
|
||||||
|
.replace(/<[^>]*>/g, "")
|
||||||
|
.replace(/ /g, " ")
|
||||||
|
.trim();
|
||||||
|
const resolveSectionTitle = (s: {
|
||||||
|
title: string | null;
|
||||||
|
title_cz: string | null;
|
||||||
|
}): string =>
|
||||||
|
isCzech && (s.title_cz || "").trim() ? s.title_cz || "" : s.title || "";
|
||||||
|
const visibleSections = quotation.scope_sections.filter(
|
||||||
|
(s) => resolveSectionTitle(s).trim() || stripSectionTags(s.content),
|
||||||
|
);
|
||||||
|
const hasScopeContent = visibleSections.length > 0;
|
||||||
|
|
||||||
let logoImg = "";
|
const cust = buildAddressLines(
|
||||||
if (settings?.logo_data) {
|
quotation.customers as unknown as Record<string, unknown>,
|
||||||
const buf = Buffer.from(settings.logo_data);
|
false,
|
||||||
let mime = "image/png";
|
t,
|
||||||
if (buf[0] === 0xff && buf[1] === 0xd8) mime = "image/jpeg";
|
);
|
||||||
else if (buf[0] === 0x47 && buf[1] === 0x49) mime = "image/gif";
|
const supp = buildAddressLines(
|
||||||
else if (buf[0] === 0x52 && buf[1] === 0x49) mime = "image/webp";
|
settings as unknown as Record<string, unknown>,
|
||||||
logoImg = `<img src="data:${escapeHtml(mime)};base64,${buf.toString("base64")}" class="logo" />`;
|
true,
|
||||||
}
|
t,
|
||||||
|
);
|
||||||
|
|
||||||
const items = quotation.quotation_items;
|
const custLinesHtml = cust.lines
|
||||||
let subtotal = 0;
|
.map((l) => `<div class="address-line">${escapeHtml(l)}</div>`)
|
||||||
for (const item of items) {
|
.join("");
|
||||||
if (item.is_included_in_total !== false) {
|
const suppLinesHtml = supp.lines
|
||||||
subtotal +=
|
.map((l) => `<div class="address-line">${escapeHtml(l)}</div>`)
|
||||||
(Number(item.quantity) || 0) * (Number(item.unit_price) || 0);
|
.join("");
|
||||||
}
|
|
||||||
}
|
|
||||||
const applyVat = !!quotation.apply_vat;
|
|
||||||
const vatRate = Number(quotation.vat_rate) || 21;
|
|
||||||
const vatAmount = applyVat ? subtotal * (vatRate / 100) : 0;
|
|
||||||
const totalToPay = subtotal + vatAmount;
|
|
||||||
let hasScopeContent = false;
|
|
||||||
for (const s of quotation.scope_sections) {
|
|
||||||
if ((s.content || "").trim() || (s.title || "").trim()) {
|
|
||||||
hasScopeContent = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const cust = buildAddressLines(
|
// Indentation CSS for Quill
|
||||||
quotation.customers as unknown as Record<string, unknown>,
|
const indentCSS = buildQuillIndentCss();
|
||||||
false,
|
|
||||||
t,
|
|
||||||
);
|
|
||||||
const supp = buildAddressLines(
|
|
||||||
settings as unknown as Record<string, unknown>,
|
|
||||||
true,
|
|
||||||
t,
|
|
||||||
);
|
|
||||||
|
|
||||||
const custLinesHtml = cust.lines
|
let itemsHtml = "";
|
||||||
.map((l) => `<div class="address-line">${escapeHtml(l)}</div>`)
|
items.forEach((item, i) => {
|
||||||
.join("");
|
const qty = Number(item.quantity) || 0;
|
||||||
const suppLinesHtml = supp.lines
|
const lineTotal = qty * (Number(item.unit_price) || 0);
|
||||||
.map((l) => `<div class="address-line">${escapeHtml(l)}</div>`)
|
// Integers render without decimals; fractional quantities keep 2 decimals
|
||||||
.join("");
|
// (the old toFixed(0) ROUNDED 1.5 → 2 on the printed document).
|
||||||
|
const qtyDecimals = Math.floor(qty) === qty ? 0 : 2;
|
||||||
// Indentation CSS for Quill
|
const subDesc = item.item_description || "";
|
||||||
let indentCSS = "";
|
const evenClass = i % 2 === 1 ? ' class="even"' : "";
|
||||||
for (let n = 1; n <= 9; n++) {
|
itemsHtml += `<tr${evenClass}>
|
||||||
const pad = n * 3;
|
|
||||||
const liPad = n * 3 + 1.5;
|
|
||||||
indentCSS += ` .ql-indent-${n} { padding-left: ${pad}em; }\n`;
|
|
||||||
indentCSS += ` li.ql-indent-${n} { padding-left: ${liPad}em; }\n`;
|
|
||||||
}
|
|
||||||
|
|
||||||
let itemsHtml = "";
|
|
||||||
items.forEach((item, i) => {
|
|
||||||
const lineTotal =
|
|
||||||
(Number(item.quantity) || 0) * (Number(item.unit_price) || 0);
|
|
||||||
const subDesc = item.item_description || "";
|
|
||||||
const evenClass = i % 2 === 1 ? ' class="even"' : "";
|
|
||||||
itemsHtml += `<tr${evenClass}>
|
|
||||||
<td class="row-num">${i + 1}</td>
|
<td class="row-num">${i + 1}</td>
|
||||||
<td class="desc">${escapeHtml(item.description)}${subDesc ? `<div class="item-subdesc">${escapeHtml(subDesc)}</div>` : ""}</td>
|
<td class="desc">${escapeHtml(item.description)}${subDesc ? `<div class="item-subdesc">${escapeHtml(subDesc)}</div>` : ""}</td>
|
||||||
<td class="center">${formatNum(Number(item.quantity) || 1, 0)}${(item.unit || "").trim() ? ` / ${escapeHtml((item.unit || "").trim())}` : ""}</td>
|
<td class="center">${formatNum(qty, qtyDecimals)}${(item.unit || "").trim() ? ` / ${escapeHtml((item.unit || "").trim())}` : ""}</td>
|
||||||
<td class="right">${formatCurrency(Number(item.unit_price) || 0, currency)}</td>
|
<td class="right">${formatCurrency(Number(item.unit_price) || 0, currency)}</td>
|
||||||
<td class="right total-cell">${formatCurrency(lineTotal, currency)}</td>
|
<td class="right total-cell">${formatCurrency(lineTotal, currency)}</td>
|
||||||
</tr>`;
|
</tr>`;
|
||||||
});
|
});
|
||||||
|
|
||||||
let totalsHtml = "";
|
// No Mezisoučet/VAT rows — they would only duplicate the net total.
|
||||||
if (applyVat) {
|
const totalsHtml = `<div class="grand">
|
||||||
totalsHtml += `<div class="detail-rows">
|
<span class="label">${escapeHtml(t("total_no_vat"))}</span>
|
||||||
<div class="row">
|
<span class="value">${formatCurrency(total, currency)}</span>
|
||||||
<span class="label">${escapeHtml(t("subtotal"))}:</span>
|
|
||||||
<span class="value">${formatCurrency(subtotal, currency)}</span>
|
|
||||||
</div>
|
|
||||||
<div class="row">
|
|
||||||
<span class="label">${escapeHtml(t("vat"))} (${Math.round(vatRate)}%):</span>
|
|
||||||
<span class="value">${formatCurrency(vatAmount, currency)}</span>
|
|
||||||
</div>
|
|
||||||
</div>`;
|
|
||||||
}
|
|
||||||
totalsHtml += `<div class="grand">
|
|
||||||
<span class="label">${escapeHtml(t("total_to_pay"))}</span>
|
|
||||||
<span class="value">${formatCurrency(totalToPay, currency)}</span>
|
|
||||||
</div>`;
|
</div>`;
|
||||||
const quotationNumber = escapeHtml(quotation.quotation_number);
|
const quotationNumber = escapeHtml(quotation.quotation_number);
|
||||||
|
|
||||||
let scopeHtml = "";
|
let scopeHtml = "";
|
||||||
if (hasScopeContent) {
|
if (hasScopeContent) {
|
||||||
scopeHtml += '<div class="scope-page">';
|
scopeHtml += '<div class="scope-page">';
|
||||||
scopeHtml += `<div class="page-header">
|
scopeHtml += `<div class="page-header">
|
||||||
<div class="left">
|
<div class="left">
|
||||||
<div class="page-title">${escapeHtml(t("title"))}</div>
|
<div class="page-title">${escapeHtml(t("title"))}</div>
|
||||||
<div class="quotation-number">${quotationNumber}</div>
|
<div class="quotation-number">${quotationNumber}</div>
|
||||||
@@ -351,27 +253,23 @@ export default async function offersPdfRoutes(
|
|||||||
</div>
|
</div>
|
||||||
<hr class="separator" />`;
|
<hr class="separator" />`;
|
||||||
|
|
||||||
for (const section of quotation.scope_sections) {
|
for (const section of visibleSections) {
|
||||||
const title =
|
const title = resolveSectionTitle(section);
|
||||||
isCzech && (section.title_cz || "").trim()
|
const content = (section.content || "").trim();
|
||||||
? section.title_cz
|
scopeHtml += '<div class="scope-section">';
|
||||||
: section.title || "";
|
if (title.trim())
|
||||||
const content = (section.content || "").trim();
|
scopeHtml += `<div class="scope-section-title">${escapeHtml(title)}</div>`;
|
||||||
if (!title && !content) continue;
|
if (content)
|
||||||
scopeHtml += '<div class="scope-section">';
|
scopeHtml += `<div class="section-content">${cleanQuillHtml(DOMPurify.sanitize(content))}</div>`;
|
||||||
if (title)
|
scopeHtml += "</div>";
|
||||||
scopeHtml += `<div class="scope-section-title">${escapeHtml(title)}</div>`;
|
}
|
||||||
if (content)
|
scopeHtml += "</div>";
|
||||||
scopeHtml += `<div class="section-content">${cleanQuillHtml(DOMPurify.sanitize(content))}</div>`;
|
}
|
||||||
scopeHtml += "</div>";
|
|
||||||
}
|
|
||||||
scopeHtml += "</div>";
|
|
||||||
}
|
|
||||||
|
|
||||||
const pageLabel = escapeHtml(t("page"));
|
const pageLabel = escapeHtml(t("page"));
|
||||||
const ofLabel = escapeHtml(t("of"));
|
const ofLabel = escapeHtml(t("of"));
|
||||||
|
|
||||||
const html = `<!DOCTYPE html>
|
const html = `<!DOCTYPE html>
|
||||||
<html lang="${isCzech ? "cs" : "en"}">
|
<html lang="${isCzech ? "cs" : "en"}">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8" />
|
<meta charset="utf-8" />
|
||||||
@@ -749,6 +647,40 @@ ${indentCSS}
|
|||||||
</body>
|
</body>
|
||||||
</html>`;
|
</html>`;
|
||||||
|
|
||||||
|
return html;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default async function offersPdfRoutes(
|
||||||
|
fastify: FastifyInstance,
|
||||||
|
): Promise<void> {
|
||||||
|
fastify.get<{ Params: { id: string } }>(
|
||||||
|
"/:id",
|
||||||
|
{ preHandler: requirePermission("offers.view") },
|
||||||
|
async (request, reply) => {
|
||||||
|
const id = parseId(request.params.id, reply);
|
||||||
|
if (id === null) return;
|
||||||
|
const query = request.query as Record<string, string>;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const quotation = await prisma.quotations.findUnique({
|
||||||
|
where: { id },
|
||||||
|
include: {
|
||||||
|
customers: true,
|
||||||
|
quotation_items: { orderBy: { position: "asc" } },
|
||||||
|
scope_sections: { orderBy: [{ position: "asc" }, { id: "asc" }] },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!quotation) {
|
||||||
|
return reply
|
||||||
|
.status(404)
|
||||||
|
.type("text/html")
|
||||||
|
.send("<html><body><h1>Nabídka nenalezena</h1></body></html>");
|
||||||
|
}
|
||||||
|
|
||||||
|
const settings = await prisma.company_settings.findFirst();
|
||||||
|
const html = renderOfferHtml(quotation, settings);
|
||||||
|
|
||||||
const saveMode = query.save === "1";
|
const saveMode = query.save === "1";
|
||||||
|
|
||||||
// Save PDF to NAS
|
// Save PDF to NAS
|
||||||
|
|||||||
@@ -1,9 +1,15 @@
|
|||||||
import { FastifyInstance } from "fastify";
|
import { FastifyInstance } from "fastify";
|
||||||
import prisma from "../../config/database";
|
import prisma from "../../config/database";
|
||||||
import { requirePermission } from "../../middleware/auth";
|
import { requirePermission } from "../../middleware/auth";
|
||||||
import { localDateCzStr } from "../../utils/date";
|
|
||||||
import { htmlToPdf } from "../../utils/html-to-pdf";
|
import { htmlToPdf } from "../../utils/html-to-pdf";
|
||||||
import { parseId, error } from "../../utils/response";
|
import { parseId, error } from "../../utils/response";
|
||||||
|
import {
|
||||||
|
formatDate,
|
||||||
|
formatNum,
|
||||||
|
escapeHtml,
|
||||||
|
cleanQuillHtml,
|
||||||
|
buildQuillIndentCss,
|
||||||
|
} from "../../utils/pdf-shared";
|
||||||
import { parseBody } from "../../schemas/common";
|
import { parseBody } from "../../schemas/common";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import createDOMPurify from "dompurify";
|
import createDOMPurify from "dompurify";
|
||||||
@@ -20,69 +26,18 @@ const OrderPdfItemSchema = z.object({
|
|||||||
unit: z.string().max(255),
|
unit: z.string().max(255),
|
||||||
unit_price: z.number().min(0).finite(),
|
unit_price: z.number().min(0).finite(),
|
||||||
is_included_in_total: z.boolean().optional(),
|
is_included_in_total: z.boolean().optional(),
|
||||||
vat_rate: z.number().min(0).max(100).finite(),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// `z.looseObject` is the Zod 4 replacement for the deprecated `.passthrough()`.
|
// `z.looseObject` is the Zod 4 replacement for the deprecated `.passthrough()`.
|
||||||
// `items` is strictly validated; `lang`/`applyVat` are typed explicitly so the
|
// `items` is strictly validated; `lang` is typed explicitly so the handler no
|
||||||
// handler no longer reads them as untyped passthrough keys.
|
// longer reads it as an untyped passthrough key.
|
||||||
const OrderPdfBodySchema = z.looseObject({
|
const OrderPdfBodySchema = z.looseObject({
|
||||||
items: z.array(OrderPdfItemSchema).optional(),
|
items: z.array(OrderPdfItemSchema).optional(),
|
||||||
lang: z.string().max(10).optional(),
|
lang: z.string().max(10).optional(),
|
||||||
applyVat: z.boolean().optional(),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
/* ── Helpers ─────────────────────────────────────────────────────── */
|
/* ── Helpers ─────────────────────────────────────────────────────── */
|
||||||
|
|
||||||
function formatDate(date: Date | string | null | undefined): string {
|
|
||||||
if (!date) return "";
|
|
||||||
const d = new Date(date);
|
|
||||||
if (isNaN(d.getTime())) return String(date);
|
|
||||||
return localDateCzStr(d);
|
|
||||||
}
|
|
||||||
|
|
||||||
function formatNum(n: number, decimals = 2): string {
|
|
||||||
const abs = Math.abs(n);
|
|
||||||
const fixed = abs.toFixed(decimals);
|
|
||||||
const [intPart, decPart] = fixed.split(".");
|
|
||||||
const withSep = intPart.replace(/\B(?=(\d{3})+(?!\d))/g, " ");
|
|
||||||
const result = decPart ? `${withSep},${decPart}` : withSep;
|
|
||||||
return n < 0 ? `-${result}` : result;
|
|
||||||
}
|
|
||||||
|
|
||||||
function escapeHtml(str: string | null | undefined): string {
|
|
||||||
if (!str) return "";
|
|
||||||
return str
|
|
||||||
.replace(/&/g, "&")
|
|
||||||
.replace(/</g, "<")
|
|
||||||
.replace(/>/g, ">")
|
|
||||||
.replace(/"/g, """);
|
|
||||||
}
|
|
||||||
|
|
||||||
function cleanQuillHtml(html: string | null | undefined): string {
|
|
||||||
if (!html) return "";
|
|
||||||
let s = html;
|
|
||||||
s = s.replace(
|
|
||||||
/<(script|iframe|object|embed|style|link|meta|base|form|input|textarea|button|select|svg|math)[^>]*>[\s\S]*?<\/\1>/gi,
|
|
||||||
"",
|
|
||||||
);
|
|
||||||
s = s.replace(
|
|
||||||
/<(script|iframe|object|embed|style|link|meta|base|form|input|textarea|button|select|svg|math)[^>]*\/?>/gi,
|
|
||||||
"",
|
|
||||||
);
|
|
||||||
s = s.replace(/\s+on\w+\s*=\s*("[^"]*"|'[^']*'|[^\s>]*)/gi, "");
|
|
||||||
s = s.replace(/\s+on\w+\s*=\s*[^\s>]*/gi, "");
|
|
||||||
s = s.replace(/href\s*=\s*["']?\s*javascript\s*:[^"'>\s]*/gi, 'href="#"');
|
|
||||||
s = s.replace(/( )/g, " ");
|
|
||||||
s = s.replace(/\s+style\s*=\s*("[^"]*"|'[^']*'|[^\s>]*)/gi, "");
|
|
||||||
let prev = "";
|
|
||||||
while (prev !== s) {
|
|
||||||
prev = s;
|
|
||||||
s = s.replace(/<span([^>]*)>(.*?)<\/span>\s*<span\1>/gs, "<span$1>$2");
|
|
||||||
}
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface AddressResult {
|
interface AddressResult {
|
||||||
name: string;
|
name: string;
|
||||||
lines: string[];
|
lines: string[];
|
||||||
@@ -193,13 +148,8 @@ const translations: Record<string, Record<string, string>> = {
|
|||||||
col_desc: "Popis",
|
col_desc: "Popis",
|
||||||
col_qty: "Množství",
|
col_qty: "Množství",
|
||||||
col_unit_price: "Jedn. cena",
|
col_unit_price: "Jedn. cena",
|
||||||
col_price: "Cena",
|
|
||||||
col_vat_pct: "%DPH",
|
|
||||||
col_vat: "DPH",
|
|
||||||
col_total: "Celkem",
|
col_total: "Celkem",
|
||||||
subtotal: "Mezisoučet:",
|
total_no_vat: "Celkem bez DPH",
|
||||||
vat_label: "DPH",
|
|
||||||
total: "Celkem",
|
|
||||||
amounts_in: "Částky jsou uvedeny v",
|
amounts_in: "Částky jsou uvedeny v",
|
||||||
notes: "Poznámky",
|
notes: "Poznámky",
|
||||||
issued_by: "Vystavil:",
|
issued_by: "Vystavil:",
|
||||||
@@ -221,13 +171,8 @@ const translations: Record<string, Record<string, string>> = {
|
|||||||
col_desc: "Description",
|
col_desc: "Description",
|
||||||
col_qty: "Quantity",
|
col_qty: "Quantity",
|
||||||
col_unit_price: "Unit price",
|
col_unit_price: "Unit price",
|
||||||
col_price: "Price",
|
|
||||||
col_vat_pct: "VAT%",
|
|
||||||
col_vat: "VAT",
|
|
||||||
col_total: "Total",
|
col_total: "Total",
|
||||||
subtotal: "Subtotal:",
|
total_no_vat: "Total excl. VAT",
|
||||||
vat_label: "VAT",
|
|
||||||
total: "Total",
|
|
||||||
amounts_in: "Amounts are in",
|
amounts_in: "Amounts are in",
|
||||||
notes: "Notes",
|
notes: "Notes",
|
||||||
issued_by: "Issued by:",
|
issued_by: "Issued by:",
|
||||||
@@ -238,207 +183,112 @@ const translations: Record<string, Record<string, string>> = {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
/* ── Route ───────────────────────────────────────────────────────── */
|
/* ── Template ────────────────────────────────────────────────────── */
|
||||||
|
|
||||||
export default async function ordersPdfRoutes(
|
export interface OrderConfirmationPdfItem {
|
||||||
fastify: FastifyInstance,
|
description: string;
|
||||||
): Promise<void> {
|
quantity: number;
|
||||||
fastify.post<{ Params: { id: string }; Body: Record<string, unknown> }>(
|
unit: string;
|
||||||
"/:id/confirmation",
|
unit_price: number;
|
||||||
{ preHandler: requirePermission("orders.view") },
|
is_included_in_total: boolean;
|
||||||
async (request, reply) => {
|
}
|
||||||
const id = parseId(request.params.id, reply);
|
|
||||||
if (id === null) return;
|
|
||||||
const parsed = parseBody(OrderPdfBodySchema, request.body || {});
|
|
||||||
if ("error" in parsed) return error(reply, parsed.error, 400);
|
|
||||||
const body = parsed.data;
|
|
||||||
|
|
||||||
try {
|
interface OrderConfirmationPdfData {
|
||||||
const lang = body.lang === "en" ? "en" : "cs";
|
order_number: string | null;
|
||||||
const t = translations[lang];
|
customer_order_number: string | null;
|
||||||
|
created_at: Date | string | null;
|
||||||
|
currency: string | null;
|
||||||
|
notes: string | null;
|
||||||
|
// Not a real orders column today — read defensively (PHP-era data carried it).
|
||||||
|
payment_method?: string | null;
|
||||||
|
customers?: Record<string, unknown> | null;
|
||||||
|
}
|
||||||
|
|
||||||
const order = await prisma.orders.findUnique({
|
/**
|
||||||
where: { id },
|
* Order-confirmation HTML. The confirmation is NOT a tax document — prices
|
||||||
// The confirmation PDF never renders the PO attachment — don't pull
|
* are NET only: no VAT columns or VAT summary; the grand total is labeled
|
||||||
// the blob just to read the order header/items.
|
* "Celkem bez DPH". Exported for tests.
|
||||||
omit: { attachment_data: true },
|
*/
|
||||||
include: {
|
export function renderOrderConfirmationHtml(
|
||||||
customers: true,
|
order: OrderConfirmationPdfData,
|
||||||
order_items: { orderBy: { position: "asc" } },
|
items: OrderConfirmationPdfItem[],
|
||||||
},
|
settings: Record<string, unknown> | null,
|
||||||
});
|
lang: "cs" | "en",
|
||||||
|
userName: string,
|
||||||
|
): string {
|
||||||
|
const t = translations[lang];
|
||||||
|
|
||||||
if (!order) {
|
let logoImg = "";
|
||||||
return reply
|
if (settings?.logo_data) {
|
||||||
.status(404)
|
const buf = Buffer.from(settings.logo_data as Buffer);
|
||||||
.type("text/html")
|
let mime = "image/png";
|
||||||
.send("<html><body><h1>Objednávka nenalezena</h1></body></html>");
|
if (buf[0] === 0xff && buf[1] === 0xd8) mime = "image/jpeg";
|
||||||
}
|
else if (buf[0] === 0x47 && buf[1] === 0x49) mime = "image/gif";
|
||||||
|
else if (buf[0] === 0x52 && buf[1] === 0x49) mime = "image/webp";
|
||||||
|
const b64 = buf.toString("base64");
|
||||||
|
logoImg = `<img src="data:${escapeHtml(mime)};base64,${b64}" class="logo" />`;
|
||||||
|
}
|
||||||
|
|
||||||
const settings = (await prisma.company_settings.findFirst()) as Record<
|
const currency = order.currency || "CZK";
|
||||||
string,
|
|
||||||
unknown
|
|
||||||
> | null;
|
|
||||||
|
|
||||||
let logoImg = "";
|
// NET-only total over the included lines — no VAT math anywhere.
|
||||||
if (settings?.logo_data) {
|
let total = 0;
|
||||||
const buf = Buffer.from(settings.logo_data as Buffer);
|
for (const item of items) {
|
||||||
let mime = "image/png";
|
if (item.is_included_in_total) total += item.quantity * item.unit_price;
|
||||||
if (buf[0] === 0xff && buf[1] === 0xd8) mime = "image/jpeg";
|
}
|
||||||
else if (buf[0] === 0x47 && buf[1] === 0x49) mime = "image/gif";
|
total = Math.round(total * 100) / 100;
|
||||||
else if (buf[0] === 0x52 && buf[1] === 0x49) mime = "image/webp";
|
|
||||||
const b64 = buf.toString("base64");
|
|
||||||
logoImg = `<img src="data:${escapeHtml(mime)};base64,${b64}" class="logo" />`;
|
|
||||||
}
|
|
||||||
|
|
||||||
const currency = order.currency || "CZK";
|
const supp = buildAddressLines(settings, true, t);
|
||||||
const applyVat =
|
const cust = buildAddressLines(
|
||||||
body.applyVat !== undefined ? !!body.applyVat : !!order.apply_vat;
|
(order.customers as Record<string, unknown>) || null,
|
||||||
const orderVatRate = Number(order.vat_rate) || 21;
|
false,
|
||||||
|
t,
|
||||||
|
);
|
||||||
|
|
||||||
// The confirmation PDF can be rendered from client-supplied items (e.g.
|
const suppLinesHtml = supp.lines
|
||||||
// a live preview of unsaved edits on the detail page) OR from the
|
.map((l) => `<div class="address-line">${escapeHtml(l)}</div>`)
|
||||||
// stored order. Fabricating descriptions/prices that don't reflect the
|
.join("");
|
||||||
// stored order is an editing action, so the custom-items path requires
|
const custLinesHtml = cust.lines
|
||||||
// `orders.edit` (admins bypass). A view-only caller is silently served
|
.map((l) => `<div class="address-line">${escapeHtml(l)}</div>`)
|
||||||
// the STORED order items instead — preventing a `orders.view` holder
|
.join("");
|
||||||
// from producing an "official" confirmation with invented figures.
|
|
||||||
const authData = request.authData;
|
|
||||||
const canUseCustomItems =
|
|
||||||
authData?.roleName === "admin" ||
|
|
||||||
!!authData?.permissions.includes("orders.edit");
|
|
||||||
const customItemsRaw =
|
|
||||||
canUseCustomItems && Array.isArray(body.items) ? body.items : null;
|
|
||||||
|
|
||||||
let items: Array<{
|
const orderNumber = escapeHtml(order.order_number || "");
|
||||||
description: string;
|
const poNumber = escapeHtml(order.customer_order_number || "");
|
||||||
quantity: number;
|
const orderDateStr = formatDate(order.created_at);
|
||||||
unit: string;
|
|
||||||
unit_price: number;
|
|
||||||
is_included_in_total: boolean;
|
|
||||||
vat_rate: number;
|
|
||||||
}> = [];
|
|
||||||
|
|
||||||
if (customItemsRaw && customItemsRaw.length > 0) {
|
const itemsHtml = items
|
||||||
items = customItemsRaw.map((it) => ({
|
.map((item, i) => {
|
||||||
description: it.description,
|
const lineTotal = item.quantity * item.unit_price;
|
||||||
quantity: it.quantity,
|
const qtyDecimals = Math.floor(item.quantity) === item.quantity ? 0 : 2;
|
||||||
unit: it.unit,
|
return `<tr>
|
||||||
unit_price: it.unit_price,
|
|
||||||
is_included_in_total: it.is_included_in_total !== false,
|
|
||||||
vat_rate: it.vat_rate,
|
|
||||||
}));
|
|
||||||
} else {
|
|
||||||
items = order.order_items.map((it) => ({
|
|
||||||
description: it.description || "",
|
|
||||||
quantity: Number(it.quantity) || 0,
|
|
||||||
unit: it.unit || "",
|
|
||||||
unit_price: Number(it.unit_price) || 0,
|
|
||||||
is_included_in_total: !!it.is_included_in_total,
|
|
||||||
vat_rate: orderVatRate,
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
let subtotal = 0;
|
|
||||||
let totalVat = 0;
|
|
||||||
const vatSummary: Record<string, { base: number; vat: number }> = {};
|
|
||||||
for (const item of items) {
|
|
||||||
if (item.is_included_in_total) {
|
|
||||||
const lineTotal = item.quantity * item.unit_price;
|
|
||||||
subtotal += lineTotal;
|
|
||||||
const rate = item.vat_rate;
|
|
||||||
const key = String(rate);
|
|
||||||
if (!vatSummary[key]) vatSummary[key] = { base: 0, vat: 0 };
|
|
||||||
vatSummary[key].base += lineTotal;
|
|
||||||
if (applyVat) {
|
|
||||||
const lineVat = (lineTotal * rate) / 100;
|
|
||||||
vatSummary[key].vat += lineVat;
|
|
||||||
totalVat += lineVat;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const totalToPay = subtotal + totalVat;
|
|
||||||
|
|
||||||
const userName = request.authData
|
|
||||||
? `${request.authData.firstName || ""} ${request.authData.lastName || ""}`.trim()
|
|
||||||
: "";
|
|
||||||
|
|
||||||
const supp = buildAddressLines(settings, true, t);
|
|
||||||
const cust = buildAddressLines(
|
|
||||||
(order.customers as Record<string, unknown>) || null,
|
|
||||||
false,
|
|
||||||
t,
|
|
||||||
);
|
|
||||||
|
|
||||||
const suppLinesHtml = supp.lines
|
|
||||||
.map((l) => `<div class="address-line">${escapeHtml(l)}</div>`)
|
|
||||||
.join("");
|
|
||||||
const custLinesHtml = cust.lines
|
|
||||||
.map((l) => `<div class="address-line">${escapeHtml(l)}</div>`)
|
|
||||||
.join("");
|
|
||||||
|
|
||||||
const orderNumber = escapeHtml(order.order_number || "");
|
|
||||||
const poNumber = escapeHtml(order.customer_order_number || "");
|
|
||||||
const orderDateStr = formatDate(order.created_at);
|
|
||||||
|
|
||||||
const itemsHtml = items
|
|
||||||
.map((item, i) => {
|
|
||||||
const lineSubtotal = item.quantity * item.unit_price;
|
|
||||||
const lineVat = applyVat ? (lineSubtotal * item.vat_rate) / 100 : 0;
|
|
||||||
const lineTotal = lineSubtotal + lineVat;
|
|
||||||
const qtyDecimals =
|
|
||||||
Math.floor(item.quantity) === item.quantity ? 0 : 2;
|
|
||||||
return `<tr>
|
|
||||||
<td class="row-num">${i + 1}</td>
|
<td class="row-num">${i + 1}</td>
|
||||||
<td class="desc">${escapeHtml(item.description)}</td>
|
<td class="desc">${escapeHtml(item.description)}</td>
|
||||||
<td class="center">${formatNum(item.quantity, qtyDecimals)}${item.unit ? ` / ${escapeHtml(item.unit)}` : ""}</td>
|
<td class="center">${formatNum(item.quantity, qtyDecimals)}${item.unit ? ` / ${escapeHtml(item.unit)}` : ""}</td>
|
||||||
<td class="right">${formatNum(item.unit_price)}</td>
|
<td class="right">${formatNum(item.unit_price)}</td>
|
||||||
<td class="right">${formatNum(lineSubtotal)}</td>
|
|
||||||
<td class="center">${applyVat ? Math.floor(item.vat_rate) : 0}%</td>
|
|
||||||
<td class="right">${formatNum(lineVat)}</td>
|
|
||||||
<td class="right total-cell">${formatNum(lineTotal)}</td>
|
<td class="right total-cell">${formatNum(lineTotal)}</td>
|
||||||
</tr>`;
|
</tr>`;
|
||||||
})
|
})
|
||||||
.join("");
|
.join("");
|
||||||
|
|
||||||
const paymentMethod =
|
const paymentMethod =
|
||||||
String((order as Record<string, unknown>).payment_method || "") ||
|
String(order.payment_method || "") ||
|
||||||
(lang === "cs" ? "převodem" : "Bank transfer");
|
(lang === "cs" ? "převodem" : "Bank transfer");
|
||||||
|
|
||||||
let vatDetailHtml = "";
|
const notesRaw = order.notes ?? "";
|
||||||
if (applyVat) {
|
const notesStripped = notesRaw.replace(/<[^>]*>/g, "").trim();
|
||||||
for (const [rate, data] of Object.entries(vatSummary)) {
|
const notesHtml = notesStripped
|
||||||
if (data.vat > 0) {
|
? `
|
||||||
vatDetailHtml += `
|
|
||||||
<div class="row">
|
|
||||||
<span class="label">${escapeHtml(t.vat_label)} ${Math.floor(Number(rate))}%:</span>
|
|
||||||
<span class="value">${formatNum(data.vat)} ${escapeHtml(currency)}</span>
|
|
||||||
</div>`;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const notesRaw = order.notes ?? "";
|
|
||||||
const notesStripped = notesRaw.replace(/<[^>]*>/g, "").trim();
|
|
||||||
const notesHtml = notesStripped
|
|
||||||
? `
|
|
||||||
<div class="invoice-notes">
|
<div class="invoice-notes">
|
||||||
<div class="invoice-notes-label">${escapeHtml(t.notes)}</div>
|
<div class="invoice-notes-label">${escapeHtml(t.notes)}</div>
|
||||||
<div class="invoice-notes-content">${cleanQuillHtml(DOMPurify.sanitize(notesRaw))}</div>
|
<div class="invoice-notes-content">${cleanQuillHtml(DOMPurify.sanitize(notesRaw))}</div>
|
||||||
</div>
|
</div>
|
||||||
`
|
`
|
||||||
: "";
|
: "";
|
||||||
|
|
||||||
// Quill indent CSS
|
// Quill indent CSS
|
||||||
let indentCSS = "";
|
const indentCSS = buildQuillIndentCss();
|
||||||
for (let n = 1; n <= 9; n++) {
|
|
||||||
const pad = n * 3;
|
|
||||||
const liPad = n * 3 + 1.5;
|
|
||||||
indentCSS += ` .ql-indent-${n} { padding-left: ${pad}em; }\n`;
|
|
||||||
indentCSS += ` li.ql-indent-${n} { padding-left: ${liPad}em; }\n`;
|
|
||||||
}
|
|
||||||
|
|
||||||
const html = `<!DOCTYPE html>
|
return `<!DOCTYPE html>
|
||||||
<html lang="${escapeHtml(lang)}">
|
<html lang="${escapeHtml(lang)}">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8" />
|
<meta charset="utf-8" />
|
||||||
@@ -686,47 +536,6 @@ export default async function ordersPdfRoutes(
|
|||||||
line-height: 1.3;
|
line-height: 1.3;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* DPH rekapitulace + QR */
|
|
||||||
.recap-section {
|
|
||||||
display: flex;
|
|
||||||
gap: 5mm;
|
|
||||||
align-items: flex-start;
|
|
||||||
margin-top: 1mm;
|
|
||||||
}
|
|
||||||
.recap-section .qr {
|
|
||||||
flex-shrink: 0;
|
|
||||||
width: 28mm;
|
|
||||||
}
|
|
||||||
.recap-section .qr img,
|
|
||||||
.recap-section .qr svg { width: 28mm; height: 28mm; }
|
|
||||||
|
|
||||||
.recap-section table {
|
|
||||||
border-collapse: collapse;
|
|
||||||
font-size: 9pt;
|
|
||||||
flex: 1;
|
|
||||||
}
|
|
||||||
.recap-section table th {
|
|
||||||
font-size: 8pt;
|
|
||||||
font-weight: 600;
|
|
||||||
color: #555;
|
|
||||||
padding: 3px 6px;
|
|
||||||
text-align: right;
|
|
||||||
border-bottom: 0.5pt solid #ccc;
|
|
||||||
}
|
|
||||||
.recap-section table td {
|
|
||||||
padding: 3px 6px;
|
|
||||||
text-align: right;
|
|
||||||
border-bottom: 0.5pt solid #eee;
|
|
||||||
}
|
|
||||||
.recap-section table td.center { text-align: center; }
|
|
||||||
.recap-section table td.cnb-rate {
|
|
||||||
font-size: 8pt;
|
|
||||||
color: #888;
|
|
||||||
text-align: right;
|
|
||||||
border-bottom: none;
|
|
||||||
padding-top: 4px;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Prevzal / razitko */
|
/* Prevzal / razitko */
|
||||||
.footer-row {
|
.footer-row {
|
||||||
display: flex;
|
display: flex;
|
||||||
@@ -848,13 +657,10 @@ ${indentCSS}
|
|||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th class="center" style="width:3%">${escapeHtml(t.col_no)}</th>
|
<th class="center" style="width:3%">${escapeHtml(t.col_no)}</th>
|
||||||
<th style="width:36%">${escapeHtml(t.col_desc)}</th>
|
<th style="width:56%">${escapeHtml(t.col_desc)}</th>
|
||||||
<th class="center" style="width:10%">${escapeHtml(t.col_qty)}</th>
|
<th class="center" style="width:10%">${escapeHtml(t.col_qty)}</th>
|
||||||
<th class="right" style="width:10%">${escapeHtml(t.col_unit_price)}</th>
|
<th class="right" style="width:10%">${escapeHtml(t.col_unit_price)}</th>
|
||||||
<th class="right" style="width:10%">${escapeHtml(t.col_price)}</th>
|
<th class="right" style="width:21%">${escapeHtml(t.col_total)}</th>
|
||||||
<th class="center" style="width:5%">${escapeHtml(t.col_vat_pct)}</th>
|
|
||||||
<th class="right" style="width:10%">${escapeHtml(t.col_vat)}</th>
|
|
||||||
<th class="right" style="width:16%">${escapeHtml(t.col_total)}</th>
|
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
@@ -862,18 +668,12 @@ ${indentCSS}
|
|||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
<!-- Soucty -->
|
<!-- Soucty (jen souhrnny radek bez DPH - mezisoucet by ho jen opakoval) -->
|
||||||
<div class="totals-wrapper">
|
<div class="totals-wrapper">
|
||||||
<div class="totals">
|
<div class="totals">
|
||||||
<div class="detail-rows">
|
|
||||||
<div class="row">
|
|
||||||
<span class="label">${escapeHtml(t.subtotal)}</span>
|
|
||||||
<span class="value">${formatNum(subtotal)} ${escapeHtml(currency)}</span>
|
|
||||||
</div>${vatDetailHtml}
|
|
||||||
</div>
|
|
||||||
<div class="grand">
|
<div class="grand">
|
||||||
<span class="label">${escapeHtml(t.total)}</span>
|
<span class="label">${escapeHtml(t.total_no_vat)}</span>
|
||||||
<span class="value">${formatNum(totalToPay)} ${escapeHtml(currency)}</span>
|
<span class="value">${formatNum(total)} ${escapeHtml(currency)}</span>
|
||||||
</div>
|
</div>
|
||||||
<div class="currency-note">${escapeHtml(t.amounts_in)} ${escapeHtml(currency)}</div>
|
<div class="currency-note">${escapeHtml(t.amounts_in)} ${escapeHtml(currency)}</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -900,9 +700,96 @@ ${indentCSS}
|
|||||||
|
|
||||||
</body>
|
</body>
|
||||||
</html>`;
|
</html>`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ── Route ───────────────────────────────────────────────────────── */
|
||||||
|
|
||||||
|
export default async function ordersPdfRoutes(
|
||||||
|
fastify: FastifyInstance,
|
||||||
|
): Promise<void> {
|
||||||
|
fastify.post<{ Params: { id: string }; Body: Record<string, unknown> }>(
|
||||||
|
"/:id/confirmation",
|
||||||
|
{ preHandler: requirePermission("orders.view") },
|
||||||
|
async (request, reply) => {
|
||||||
|
const id = parseId(request.params.id, reply);
|
||||||
|
if (id === null) return;
|
||||||
|
const parsed = parseBody(OrderPdfBodySchema, request.body || {});
|
||||||
|
if ("error" in parsed) return error(reply, parsed.error, 400);
|
||||||
|
const body = parsed.data;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const lang = body.lang === "en" ? "en" : "cs";
|
||||||
|
|
||||||
|
const order = await prisma.orders.findUnique({
|
||||||
|
where: { id },
|
||||||
|
// The confirmation PDF never renders the PO attachment — don't pull
|
||||||
|
// the blob just to read the order header/items.
|
||||||
|
omit: { attachment_data: true },
|
||||||
|
include: {
|
||||||
|
customers: true,
|
||||||
|
order_items: { orderBy: { position: "asc" } },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!order) {
|
||||||
|
return reply
|
||||||
|
.status(404)
|
||||||
|
.type("text/html")
|
||||||
|
.send("<html><body><h1>Objednávka nenalezena</h1></body></html>");
|
||||||
|
}
|
||||||
|
|
||||||
|
const settings = (await prisma.company_settings.findFirst()) as Record<
|
||||||
|
string,
|
||||||
|
unknown
|
||||||
|
> | null;
|
||||||
|
|
||||||
|
// The confirmation PDF can be rendered from client-supplied items (e.g.
|
||||||
|
// a live preview of unsaved edits on the detail page) OR from the
|
||||||
|
// stored order. Fabricating descriptions/prices that don't reflect the
|
||||||
|
// stored order is an editing action, so the custom-items path requires
|
||||||
|
// `orders.edit` (admins bypass). A view-only caller is silently served
|
||||||
|
// the STORED order items instead — preventing a `orders.view` holder
|
||||||
|
// from producing an "official" confirmation with invented figures.
|
||||||
|
const authData = request.authData;
|
||||||
|
const canUseCustomItems =
|
||||||
|
authData?.roleName === "admin" ||
|
||||||
|
!!authData?.permissions.includes("orders.edit");
|
||||||
|
const customItemsRaw =
|
||||||
|
canUseCustomItems && Array.isArray(body.items) ? body.items : null;
|
||||||
|
|
||||||
|
let items: OrderConfirmationPdfItem[];
|
||||||
|
if (customItemsRaw && customItemsRaw.length > 0) {
|
||||||
|
items = customItemsRaw.map((it) => ({
|
||||||
|
description: it.description,
|
||||||
|
quantity: it.quantity,
|
||||||
|
unit: it.unit,
|
||||||
|
unit_price: it.unit_price,
|
||||||
|
is_included_in_total: it.is_included_in_total !== false,
|
||||||
|
}));
|
||||||
|
} else {
|
||||||
|
items = order.order_items.map((it) => ({
|
||||||
|
description: it.description || "",
|
||||||
|
quantity: Number(it.quantity) || 0,
|
||||||
|
unit: it.unit || "",
|
||||||
|
unit_price: Number(it.unit_price) || 0,
|
||||||
|
is_included_in_total: !!it.is_included_in_total,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
const userName = request.authData
|
||||||
|
? `${request.authData.firstName || ""} ${request.authData.lastName || ""}`.trim()
|
||||||
|
: "";
|
||||||
|
|
||||||
|
const html = renderOrderConfirmationHtml(
|
||||||
|
order,
|
||||||
|
items,
|
||||||
|
settings,
|
||||||
|
lang,
|
||||||
|
userName,
|
||||||
|
);
|
||||||
|
|
||||||
const pdfBuffer = await htmlToPdf(html);
|
const pdfBuffer = await htmlToPdf(html);
|
||||||
const filename = `Potvrzeni-${orderNumber || String(id)}.pdf`;
|
const filename = `Potvrzeni-${order.order_number || String(id)}.pdf`;
|
||||||
|
|
||||||
return reply
|
return reply
|
||||||
.type("application/pdf")
|
.type("application/pdf")
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { requirePermission } from "../../middleware/auth";
|
|||||||
import { logAudit } from "../../services/audit";
|
import { logAudit } from "../../services/audit";
|
||||||
import { success, error, parseId, paginated } from "../../utils/response";
|
import { success, error, parseId, paginated } from "../../utils/response";
|
||||||
import { parsePagination, buildPaginationMeta } from "../../utils/pagination";
|
import { parsePagination, buildPaginationMeta } from "../../utils/pagination";
|
||||||
|
import { contentDisposition } from "../../utils/content-disposition";
|
||||||
import { parseBody } from "../../schemas/common";
|
import { parseBody } from "../../schemas/common";
|
||||||
import {
|
import {
|
||||||
CreateQuotationSchema,
|
CreateQuotationSchema,
|
||||||
@@ -21,8 +22,13 @@ import {
|
|||||||
getNextOfferNumber,
|
getNextOfferNumber,
|
||||||
} from "../../services/offers.service";
|
} from "../../services/offers.service";
|
||||||
import { nasOffersManager } from "../../services/nas-offers-manager";
|
import { nasOffersManager } from "../../services/nas-offers-manager";
|
||||||
|
import { renderOfferHtml } from "./offers-pdf";
|
||||||
|
import { htmlToPdf } from "../../utils/html-to-pdf";
|
||||||
|
|
||||||
const LOCK_TIMEOUT_MS = 10 * 1000; // 10 seconds — lock expires if no heartbeat
|
// 3× the client's 10s heartbeat: a single missed beat (background-tab
|
||||||
|
// throttling, transient network) must not silently hand the lock to a second
|
||||||
|
// editor. Keep in sync with issued-orders.ts and useDocumentLock.
|
||||||
|
const LOCK_TIMEOUT_MS = 30 * 1000;
|
||||||
|
|
||||||
export default async function quotationsRoutes(
|
export default async function quotationsRoutes(
|
||||||
fastify: FastifyInstance,
|
fastify: FastifyInstance,
|
||||||
@@ -53,9 +59,10 @@ export default async function quotationsRoutes(
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
// GET /api/admin/offers/stats — per-currency total (incl. VAT) summed over the
|
// GET /api/admin/offers/stats — per-currency NET total summed over the WHOLE
|
||||||
// WHOLE filtered set (not one page). Offers have NO month/year filter; the
|
// filtered set (not one page). Offers are not tax documents — no VAT
|
||||||
// list filters by search + status + customer_id, so the totals use the same.
|
// anywhere. Offers have NO month/year filter; the list filters by
|
||||||
|
// search + status + customer_id, so the totals use the same.
|
||||||
// Registered BEFORE "/:id" so the literal "stats" path isn't captured as an id.
|
// Registered BEFORE "/:id" so the literal "stats" path isn't captured as an id.
|
||||||
fastify.get(
|
fastify.get(
|
||||||
"/stats",
|
"/stats",
|
||||||
@@ -117,8 +124,19 @@ export default async function quotationsRoutes(
|
|||||||
const id = parseId(request.params.id, reply);
|
const id = parseId(request.params.id, reply);
|
||||||
if (id === null) return;
|
if (id === null) return;
|
||||||
|
|
||||||
const existing = await invalidateOffer(id);
|
const result = await invalidateOffer(id);
|
||||||
if (!existing) return error(reply, "Nabídka nenalezena", 404);
|
if ("error" in result) {
|
||||||
|
if (result.error === "not_found")
|
||||||
|
return error(reply, "Nabídka nenalezena", 404);
|
||||||
|
if (result.error === "invalid_transition")
|
||||||
|
return error(
|
||||||
|
reply,
|
||||||
|
`Neplatný přechod stavu z "${result.currentStatus}" na "${result.newStatus}"`,
|
||||||
|
400,
|
||||||
|
);
|
||||||
|
return error(reply, "Neznámá chyba", 500);
|
||||||
|
}
|
||||||
|
const existing = result.data;
|
||||||
|
|
||||||
await logAudit({
|
await logAudit({
|
||||||
request,
|
request,
|
||||||
@@ -126,7 +144,9 @@ export default async function quotationsRoutes(
|
|||||||
action: "update",
|
action: "update",
|
||||||
entityType: "quotation",
|
entityType: "quotation",
|
||||||
entityId: id,
|
entityId: id,
|
||||||
description: `Zneplatněna nabídka ${existing.quotation_number}`,
|
description: `Zneplatněna nabídka ${existing.quotation_number ?? "koncept"}`,
|
||||||
|
oldValues: { status: existing.status },
|
||||||
|
newValues: { status: "invalidated" },
|
||||||
});
|
});
|
||||||
return success(reply, null, 200, "Nabídka zneplatněna");
|
return success(reply, null, 200, "Nabídka zneplatněna");
|
||||||
},
|
},
|
||||||
@@ -262,8 +282,13 @@ export default async function quotationsRoutes(
|
|||||||
if ("error" in parsed) return error(reply, parsed.error, 400);
|
if ("error" in parsed) return error(reply, parsed.error, 400);
|
||||||
|
|
||||||
const quotation = await createOffer(parsed.data);
|
const quotation = await createOffer(parsed.data);
|
||||||
if ("error" in quotation)
|
if ("error" in quotation) {
|
||||||
return error(reply, quotation.error!, quotation.status!);
|
if (quotation.error === "customer_not_found")
|
||||||
|
return error(reply, "Zákazník nenalezen", 400);
|
||||||
|
if (quotation.error === "number_taken")
|
||||||
|
return error(reply, "Číslo nabídky je již použito", 409);
|
||||||
|
return error(reply, "Neznámá chyba", 500);
|
||||||
|
}
|
||||||
|
|
||||||
await logAudit({
|
await logAudit({
|
||||||
request,
|
request,
|
||||||
@@ -295,13 +320,17 @@ export default async function quotationsRoutes(
|
|||||||
if ("error" in result) {
|
if ("error" in result) {
|
||||||
if (result.error === "not_found")
|
if (result.error === "not_found")
|
||||||
return error(reply, "Nabídka nenalezena", 404);
|
return error(reply, "Nabídka nenalezena", 404);
|
||||||
if (result.error === "invalidated")
|
if (result.error === "not_editable")
|
||||||
return error(reply, "Nelze upravit zneplatněnou nabídku", 400);
|
return error(reply, "Nabídku v tomto stavu nelze upravovat", 400);
|
||||||
return error(
|
if (result.error === "invalid_transition")
|
||||||
reply,
|
return error(
|
||||||
result.error || "Neznámá chyba",
|
reply,
|
||||||
"status" in result ? result.status : 400,
|
`Neplatný přechod stavu z "${result.currentStatus}" na "${result.newStatus}"`,
|
||||||
);
|
400,
|
||||||
|
);
|
||||||
|
if (result.error === "customer_not_found")
|
||||||
|
return error(reply, "Zákazník nenalezen", 400);
|
||||||
|
return error(reply, "Neznámá chyba", 500);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Keep lock — user stays on the page after save
|
// Keep lock — user stays on the page after save
|
||||||
@@ -312,7 +341,9 @@ export default async function quotationsRoutes(
|
|||||||
action: "update",
|
action: "update",
|
||||||
entityType: "quotation",
|
entityType: "quotation",
|
||||||
entityId: id,
|
entityId: id,
|
||||||
description: `Upravena nabídka ${result.quotation_number}`,
|
description: `Upravena nabídka ${result.quotation_number ?? "koncept"}`,
|
||||||
|
oldValues: result.oldValues,
|
||||||
|
newValues: result.newValues,
|
||||||
});
|
});
|
||||||
return success(reply, { id }, 200, "Nabídka byla uložena");
|
return success(reply, { id }, 200, "Nabídka byla uložena");
|
||||||
},
|
},
|
||||||
@@ -325,25 +356,23 @@ export default async function quotationsRoutes(
|
|||||||
const id = parseId(request.params.id, reply);
|
const id = parseId(request.params.id, reply);
|
||||||
if (id === null) return;
|
if (id === null) return;
|
||||||
|
|
||||||
const existing = await deleteOffer(id);
|
const result = await deleteOffer(id);
|
||||||
if (!existing) return error(reply, "Nabídka nenalezena", 404);
|
if ("error" in result) {
|
||||||
|
if (result.error === "not_found")
|
||||||
// Delete PDF from NAS
|
return error(reply, "Nabídka nenalezena", 404);
|
||||||
if (existing.quotation_number && existing.created_at) {
|
if (result.error === "linked")
|
||||||
const yr = new Date(existing.created_at).getFullYear();
|
return error(
|
||||||
try {
|
reply,
|
||||||
await nasOffersManager.deleteOfferPdf(
|
"Nabídku nelze smazat, je navázána na objednávku nebo projekt",
|
||||||
nasOffersManager.buildRelativePath(existing.quotation_number, yr),
|
409,
|
||||||
);
|
);
|
||||||
} catch (e) {
|
return error(reply, "Neznámá chyba", 500);
|
||||||
// Non-fatal: NAS delete may fail if the file does not exist — but
|
|
||||||
// never swallow silently (project never-swallow rule).
|
|
||||||
request.log.warn(
|
|
||||||
e,
|
|
||||||
`Smazání PDF nabídky ${existing.quotation_number} z NAS selhalo`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
const existing = result.data;
|
||||||
|
|
||||||
|
// NAS PDF cleanup is owned by deleteOffer (single owner) — the route
|
||||||
|
// used to repeat it here, which always failed the second time and logged
|
||||||
|
// a spurious error.
|
||||||
|
|
||||||
await logAudit({
|
await logAudit({
|
||||||
request,
|
request,
|
||||||
@@ -351,7 +380,8 @@ export default async function quotationsRoutes(
|
|||||||
action: "delete",
|
action: "delete",
|
||||||
entityType: "quotation",
|
entityType: "quotation",
|
||||||
entityId: id,
|
entityId: id,
|
||||||
description: `Smazána nabídka ${existing.quotation_number}`,
|
description: `Smazána nabídka ${existing.quotation_number ?? "koncept"}`,
|
||||||
|
oldValues: existing as unknown as Record<string, unknown>,
|
||||||
});
|
});
|
||||||
return success(reply, null, 200, "Nabídka smazána");
|
return success(reply, null, 200, "Nabídka smazána");
|
||||||
},
|
},
|
||||||
@@ -379,15 +409,71 @@ export default async function quotationsRoutes(
|
|||||||
year,
|
year,
|
||||||
);
|
);
|
||||||
const file = nasOffersManager.readOfferPdf(relPath);
|
const file = nasOffersManager.readOfferPdf(relPath);
|
||||||
if (!file) return error(reply, "PDF soubor nenalezen", 404);
|
if (file) {
|
||||||
|
return reply
|
||||||
|
.type("application/pdf")
|
||||||
|
.header(
|
||||||
|
"Content-Disposition",
|
||||||
|
// Quotation numbers contain "/" segments — not header- or
|
||||||
|
// filename-safe raw; the helper handles non-ASCII per RFC 5987.
|
||||||
|
contentDisposition(
|
||||||
|
"inline",
|
||||||
|
`Nabidka-${(offer.quotation_number || "").replace(/\//g, "-")}.pdf`,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.send(file.data);
|
||||||
|
}
|
||||||
|
|
||||||
return reply
|
// Archive miss → live-render fallback (same model as issued orders'
|
||||||
.type("application/pdf")
|
// /:id/file): rebuild the PDF from current data with the exact PDF-route
|
||||||
.header(
|
// template, re-archive it so the next request is a plain hit, and serve
|
||||||
"Content-Disposition",
|
// the fresh bytes with identical response semantics.
|
||||||
`inline; filename="${offer.quotation_number}.pdf"`,
|
try {
|
||||||
)
|
const quotation = await prisma.quotations.findUnique({
|
||||||
.send(file.data);
|
where: { id },
|
||||||
|
include: {
|
||||||
|
customers: true,
|
||||||
|
quotation_items: { orderBy: { position: "asc" } },
|
||||||
|
scope_sections: { orderBy: [{ position: "asc" }, { id: "asc" }] },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
if (!quotation) return error(reply, "Nabídka nenalezena", 404);
|
||||||
|
|
||||||
|
const settings = await prisma.company_settings.findFirst();
|
||||||
|
const html = renderOfferHtml(quotation, settings);
|
||||||
|
const pdfBuffer = await htmlToPdf(html);
|
||||||
|
|
||||||
|
if (nasOffersManager.isConfigured()) {
|
||||||
|
// saveOfferPdf logs internally and returns null on failure —
|
||||||
|
// archiving is best-effort here, serving the PDF must not fail.
|
||||||
|
const saved = nasOffersManager.saveOfferPdf(
|
||||||
|
offer.quotation_number,
|
||||||
|
year,
|
||||||
|
pdfBuffer,
|
||||||
|
);
|
||||||
|
if (!saved) {
|
||||||
|
request.log.error(
|
||||||
|
`Archivace PDF nabídky ${offer.quotation_number} na NAS při /file fallbacku selhala`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return reply
|
||||||
|
.type("application/pdf")
|
||||||
|
.header(
|
||||||
|
"Content-Disposition",
|
||||||
|
// Quotation numbers contain "/" segments — not header- or
|
||||||
|
// filename-safe raw; the helper handles non-ASCII per RFC 5987.
|
||||||
|
contentDisposition(
|
||||||
|
"inline",
|
||||||
|
`Nabidka-${(offer.quotation_number || "").replace(/\//g, "-")}.pdf`,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.send(pdfBuffer);
|
||||||
|
} catch (err) {
|
||||||
|
request.log.error(err, "Offer /file live-render fallback failed");
|
||||||
|
return error(reply, "Chyba při generování PDF", 500);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import {
|
|||||||
} from "../../schemas/received-invoices.schema";
|
} from "../../schemas/received-invoices.schema";
|
||||||
import { nasInvoicesManager } from "../../services/nas-financials-manager";
|
import { nasInvoicesManager } from "../../services/nas-financials-manager";
|
||||||
import { toCzk } from "../../services/exchange-rates";
|
import { toCzk } from "../../services/exchange-rates";
|
||||||
|
import { utcMidnightOfLocalDay } from "../../utils/date";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
|
|
||||||
const VALID_STATUSES = ["unpaid", "paid"] as const;
|
const VALID_STATUSES = ["unpaid", "paid"] as const;
|
||||||
@@ -561,14 +562,17 @@ export default async function receivedInvoicesRoutes(
|
|||||||
// `amount` is the GROSS total (VAT included); VAT is the portion within it.
|
// `amount` is the GROSS total (VAT included); VAT is the portion within it.
|
||||||
const computedVat = vatFromGross(finalAmount, finalVatRate);
|
const computedVat = vatFromGross(finalAmount, finalVatRate);
|
||||||
|
|
||||||
// Auto-set paid_date when status transitions to paid (matching PHP)
|
// Auto-set paid_date when status transitions to paid (matching PHP).
|
||||||
|
// paid_date is @db.Date (truncated to the UTC date part) —
|
||||||
|
// utcMidnightOfLocalDay keeps the LOCAL calendar day even during the
|
||||||
|
// 00:00–02:00 Prague window (a bare new Date() stored yesterday there).
|
||||||
const newStatus =
|
const newStatus =
|
||||||
body.status !== undefined
|
body.status !== undefined
|
||||||
? String(body.status)
|
? String(body.status)
|
||||||
: String(existing.status);
|
: String(existing.status);
|
||||||
const paidDate =
|
const paidDate =
|
||||||
newStatus === "paid" && String(existing.status) !== "paid"
|
newStatus === "paid" && String(existing.status) !== "paid"
|
||||||
? new Date()
|
? utcMidnightOfLocalDay()
|
||||||
: body.paid_date !== undefined
|
: body.paid_date !== undefined
|
||||||
? body.paid_date
|
? body.paid_date
|
||||||
? new Date(String(body.paid_date))
|
? new Date(String(body.paid_date))
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import {
|
import {
|
||||||
intIdFromForm,
|
intIdFromForm,
|
||||||
|
isoDateString,
|
||||||
nullableDateTimeString,
|
nullableDateTimeString,
|
||||||
nullableIntIdFromForm,
|
nullableIntIdFromForm,
|
||||||
numberFromForm,
|
numberFromForm,
|
||||||
@@ -73,7 +74,10 @@ export const AttendancePunchSchema = z.object({
|
|||||||
// NOT bare HH:MM times.
|
// NOT bare HH:MM times.
|
||||||
export const CreateAttendanceSchema = z.object({
|
export const CreateAttendanceSchema = z.object({
|
||||||
user_id: intIdFromForm.optional(),
|
user_id: intIdFromForm.optional(),
|
||||||
shift_date: z.string(),
|
// Must be a bare "YYYY-MM-DD" (parses to UTC midnight) — the service's
|
||||||
|
// duplicate-validation window and the stored @db.Date both depend on it.
|
||||||
|
// isoDateString leniently strips a trailing time component.
|
||||||
|
shift_date: isoDateString,
|
||||||
arrival_time: nullableDateTimeString.optional(),
|
arrival_time: nullableDateTimeString.optional(),
|
||||||
arrival_lat: numberFromForm.nullish(),
|
arrival_lat: numberFromForm.nullish(),
|
||||||
arrival_lng: numberFromForm.nullish(),
|
arrival_lng: numberFromForm.nullish(),
|
||||||
|
|||||||
@@ -61,6 +61,14 @@ export const positiveNumberFromForm = z
|
|||||||
message: "Číslo musí být kladné",
|
message: "Číslo musí být kladné",
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/** number|string → non-negative integer (e.g. list positions). */
|
||||||
|
export const nonNegativeIntFromForm = z
|
||||||
|
.union([z.number(), z.string()])
|
||||||
|
.transform(coerceNum)
|
||||||
|
.refine((n) => Number.isInteger(n) && n >= 0, {
|
||||||
|
message: "Číslo musí být nezáporné celé číslo",
|
||||||
|
});
|
||||||
|
|
||||||
/** number|string → positive integer id; rejects NaN/≤0/non-integer. */
|
/** number|string → positive integer id; rejects NaN/≤0/non-integer. */
|
||||||
export const intIdFromForm = z
|
export const intIdFromForm = z
|
||||||
.union([z.number(), z.string()])
|
.union([z.number(), z.string()])
|
||||||
@@ -154,3 +162,17 @@ export const emailOrEmpty = z.union([
|
|||||||
z.literal(""),
|
z.literal(""),
|
||||||
z.string().max(255).email("Neplatný email"),
|
z.string().max(255).email("Neplatný email"),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shared rich-text document section — offers' scope_sections and issued
|
||||||
|
* orders' issued_order_sections are 1:1 mirror tables, so both schemas use
|
||||||
|
* this. Limits are DB-aligned: title/title_cz are VarChar(500) (the old
|
||||||
|
* offers-local copy under-shot at 255), content is Text with the app-level
|
||||||
|
* 8000 cap used by the other rich-text fields.
|
||||||
|
*/
|
||||||
|
export const DocumentSectionSchema = z.object({
|
||||||
|
title: z.string().max(500).nullish(),
|
||||||
|
title_cz: z.string().max(500).nullish(),
|
||||||
|
content: z.string().max(8000).nullish(),
|
||||||
|
position: nonNegativeIntFromForm.optional(),
|
||||||
|
});
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import {
|
|||||||
positiveNumberFromForm,
|
positiveNumberFromForm,
|
||||||
nullableIntIdFromForm,
|
nullableIntIdFromForm,
|
||||||
booleanFromForm,
|
booleanFromForm,
|
||||||
|
DocumentSectionSchema,
|
||||||
} from "./common";
|
} from "./common";
|
||||||
|
|
||||||
const InvoiceItemSchema = z.object({
|
const InvoiceItemSchema = z.object({
|
||||||
@@ -38,9 +39,14 @@ export const CreateInvoiceSchema = z.object({
|
|||||||
issued_by: z.string().max(255).nullish(),
|
issued_by: z.string().max(255).nullish(),
|
||||||
billing_text: z.string().max(8000).nullish(),
|
billing_text: z.string().max(8000).nullish(),
|
||||||
language: z.string().max(20).optional().default("cs"),
|
language: z.string().max(20).optional().default("cs"),
|
||||||
notes: z.string().max(8000).nullish(),
|
// `notes` was dropped (printed-notes block removed — free-form PDF content
|
||||||
|
// lives in the sections); legacy clients still sending it are silently
|
||||||
|
// stripped by z.object. internal_notes is never printed.
|
||||||
internal_notes: z.string().max(8000).nullish(),
|
internal_notes: z.string().max(8000).nullish(),
|
||||||
items: z.array(InvoiceItemSchema).optional(),
|
items: z.array(InvoiceItemSchema).optional(),
|
||||||
|
// Rich-text CZ/EN "Obsah" sections rendered after the items on the PDF
|
||||||
|
// (mirrors issued orders — shared DocumentSectionSchema, DB-aligned limits).
|
||||||
|
sections: z.array(DocumentSectionSchema).optional(),
|
||||||
});
|
});
|
||||||
|
|
||||||
export const UpdateInvoiceSchema = z.object({
|
export const UpdateInvoiceSchema = z.object({
|
||||||
@@ -61,10 +67,10 @@ export const UpdateInvoiceSchema = z.object({
|
|||||||
issue_date: z.union([z.string().max(255), z.null()]).optional(),
|
issue_date: z.union([z.string().max(255), z.null()]).optional(),
|
||||||
due_date: z.union([z.string().max(255), z.null()]).optional(),
|
due_date: z.union([z.string().max(255), z.null()]).optional(),
|
||||||
tax_date: z.union([z.string().max(255), z.null()]).optional(),
|
tax_date: z.union([z.string().max(255), z.null()]).optional(),
|
||||||
notes: z.string().max(8000).nullish(),
|
|
||||||
internal_notes: z.string().max(8000).nullish(),
|
internal_notes: z.string().max(8000).nullish(),
|
||||||
paid_date: z.union([z.string().max(255), z.null()]).optional(),
|
paid_date: z.union([z.string().max(255), z.null()]).optional(),
|
||||||
items: z.array(InvoiceItemSchema).optional(),
|
items: z.array(InvoiceItemSchema).optional(),
|
||||||
|
sections: z.array(DocumentSectionSchema).optional(),
|
||||||
});
|
});
|
||||||
|
|
||||||
export type CreateInvoiceInput = z.infer<typeof CreateInvoiceSchema>;
|
export type CreateInvoiceInput = z.infer<typeof CreateInvoiceSchema>;
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import {
|
import {
|
||||||
numberInRange,
|
|
||||||
nonNegativeNumberFromForm,
|
nonNegativeNumberFromForm,
|
||||||
positiveNumberFromForm,
|
positiveNumberFromForm,
|
||||||
nullableIntIdFromForm,
|
nullableIntIdFromForm,
|
||||||
booleanFromForm,
|
|
||||||
isoDateString,
|
isoDateString,
|
||||||
|
DocumentSectionSchema,
|
||||||
} from "./common";
|
} from "./common";
|
||||||
|
|
||||||
export const IssuedOrderItemSchema = z.object({
|
export const IssuedOrderItemSchema = z.object({
|
||||||
@@ -14,7 +13,6 @@ export const IssuedOrderItemSchema = z.object({
|
|||||||
quantity: positiveNumberFromForm.optional(),
|
quantity: positiveNumberFromForm.optional(),
|
||||||
unit: z.string().max(20).nullish(),
|
unit: z.string().max(20).nullish(),
|
||||||
unit_price: nonNegativeNumberFromForm.optional(),
|
unit_price: nonNegativeNumberFromForm.optional(),
|
||||||
vat_rate: numberInRange(0, 100).optional(),
|
|
||||||
position: z.number().int().nonnegative().optional(),
|
position: z.number().int().nonnegative().optional(),
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -28,21 +26,26 @@ const ISSUED_ORDER_STATUSES = [
|
|||||||
|
|
||||||
export const CreateIssuedOrderSchema = z.object({
|
export const CreateIssuedOrderSchema = z.object({
|
||||||
po_number: z.string().max(50).nullish(),
|
po_number: z.string().max(50).nullish(),
|
||||||
customer_id: nullableIntIdFromForm.nullish(),
|
supplier_id: nullableIntIdFromForm.nullish(),
|
||||||
status: z.enum(ISSUED_ORDER_STATUSES).optional(),
|
status: z.enum(ISSUED_ORDER_STATUSES).optional(),
|
||||||
currency: z.string().max(10).optional(),
|
currency: z.string().max(10).optional(),
|
||||||
vat_rate: numberInRange(0, 100).optional(),
|
|
||||||
apply_vat: booleanFromForm.optional(),
|
|
||||||
exchange_rate: nonNegativeNumberFromForm.optional(),
|
exchange_rate: nonNegativeNumberFromForm.optional(),
|
||||||
order_date: isoDateString.nullish(),
|
order_date: isoDateString.nullish(),
|
||||||
delivery_date: isoDateString.nullish(),
|
delivery_date: isoDateString.nullish(),
|
||||||
language: z.string().max(5).optional(),
|
language: z.string().max(5).optional(),
|
||||||
delivery_terms: z.string().max(500).nullish(),
|
// Editable heading above the PDF items table; empty/null falls back to the
|
||||||
payment_terms: z.string().max(500).nullish(),
|
// default "Objednáváme si u Vás:" (issued-orders-pdf t.billing).
|
||||||
issued_by: z.string().max(255).nullish(),
|
order_text: z.string().max(500).nullish(),
|
||||||
notes: z.string().nullish(),
|
|
||||||
internal_notes: z.string().nullish(),
|
internal_notes: z.string().nullish(),
|
||||||
items: z.array(IssuedOrderItemSchema).optional(),
|
items: z.array(IssuedOrderItemSchema).optional(),
|
||||||
|
// Rich-text CZ/EN sections rendered on their own PDF page (mirrors offers'
|
||||||
|
// scope_sections — shared DocumentSectionSchema, DB-aligned limits).
|
||||||
|
sections: z.array(DocumentSectionSchema).optional(),
|
||||||
});
|
});
|
||||||
|
|
||||||
export const UpdateIssuedOrderSchema = CreateIssuedOrderSchema.partial();
|
// Update = partial create MINUS the number: PO numbers are immutable
|
||||||
|
// (assigned by the sequence on finalize), so the field is stripped before it
|
||||||
|
// can ever reach the service (which never reads it on update anyway).
|
||||||
|
export const UpdateIssuedOrderSchema = CreateIssuedOrderSchema.partial().omit({
|
||||||
|
po_number: true,
|
||||||
|
});
|
||||||
|
|||||||
@@ -1,59 +1,45 @@
|
|||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import {
|
import {
|
||||||
numberFromForm,
|
nonNegativeIntFromForm,
|
||||||
numberInRange,
|
|
||||||
nonNegativeNumberFromForm,
|
nonNegativeNumberFromForm,
|
||||||
positiveNumberFromForm,
|
positiveNumberFromForm,
|
||||||
nullableIntIdFromForm,
|
nullableIntIdFromForm,
|
||||||
booleanFromForm,
|
booleanFromForm,
|
||||||
|
isoDateString,
|
||||||
|
DocumentSectionSchema,
|
||||||
} from "./common";
|
} from "./common";
|
||||||
|
|
||||||
|
// Limits are DB-aligned: description VarChar(500), unit VarChar(20);
|
||||||
|
// item_description is Text with the app-level 8000 cap used elsewhere.
|
||||||
const QuotationItemSchema = z.object({
|
const QuotationItemSchema = z.object({
|
||||||
description: z.string().max(8000).nullish(),
|
description: z.string().max(500).nullish(),
|
||||||
item_description: z.string().max(8000).nullish(),
|
item_description: z.string().max(8000).nullish(),
|
||||||
quantity: positiveNumberFromForm.default(1),
|
quantity: positiveNumberFromForm.default(1),
|
||||||
unit: z.string().max(255).nullish(),
|
unit: z.string().max(20).nullish(),
|
||||||
unit_price: numberFromForm.default(0),
|
unit_price: nonNegativeNumberFromForm.default(0),
|
||||||
is_included_in_total: booleanFromForm.optional().default(true),
|
is_included_in_total: booleanFromForm.optional().default(true),
|
||||||
position: nonNegativeNumberFromForm.optional(),
|
// Int column — a float position would 500 at Prisma.
|
||||||
|
position: nonNegativeIntFromForm.optional(),
|
||||||
});
|
});
|
||||||
|
|
||||||
const ScopeSectionSchema = z.object({
|
// Shared with issued orders (scope_sections ≡ issued_order_sections).
|
||||||
title: z.string().max(255).nullish(),
|
const ScopeSectionSchema = DocumentSectionSchema;
|
||||||
title_cz: z.string().max(255).nullish(),
|
|
||||||
content: z.string().max(8000).nullish(),
|
|
||||||
position: nonNegativeNumberFromForm.optional(),
|
|
||||||
});
|
|
||||||
|
|
||||||
|
// NOTE: no top-level `.default()`s here — `UpdateQuotationSchema` derives from
|
||||||
|
// this schema via `.partial()`, and Zod 4 still applies a field default when
|
||||||
|
// the key is absent, which would silently inject status/currency/language
|
||||||
|
// resets into every update payload. Create-time defaults live in
|
||||||
|
// `createOffer` (service) instead.
|
||||||
export const CreateQuotationSchema = z.object({
|
export const CreateQuotationSchema = z.object({
|
||||||
quotation_number: z.string().max(255).nullish(),
|
// DB: quotations.quotation_number VarChar(50).
|
||||||
project_code: z.string().max(255).nullish(),
|
quotation_number: z.string().max(50).nullish(),
|
||||||
|
// DB column is VarChar(100) — a longer value would 500 at Prisma.
|
||||||
|
project_code: z.string().max(100).nullish(),
|
||||||
customer_id: nullableIntIdFromForm.nullish(),
|
customer_id: nullableIntIdFromForm.nullish(),
|
||||||
created_at: z.string().max(255).nullish(),
|
created_at: isoDateString.nullish(),
|
||||||
valid_until: z.string().max(255).nullish(),
|
valid_until: isoDateString.nullish(),
|
||||||
currency: z.string().max(20).optional().default("CZK"),
|
|
||||||
language: z.string().max(20).optional().default("cs"),
|
|
||||||
vat_rate: numberInRange(0, 100).optional().default(21.0),
|
|
||||||
apply_vat: booleanFromForm.optional().default(true),
|
|
||||||
status: z
|
|
||||||
.enum(["draft", "active", "ordered", "invalidated"])
|
|
||||||
.optional()
|
|
||||||
.default("draft"),
|
|
||||||
scope_title: z.string().max(255).nullish(),
|
|
||||||
scope_description: z.string().max(8000).nullish(),
|
|
||||||
items: z.array(QuotationItemSchema).optional(),
|
|
||||||
sections: z.array(ScopeSectionSchema).optional(),
|
|
||||||
});
|
|
||||||
|
|
||||||
export const UpdateQuotationSchema = z.object({
|
|
||||||
project_code: z.string().max(255).nullish(),
|
|
||||||
customer_id: nullableIntIdFromForm.optional(),
|
|
||||||
created_at: z.union([z.string().max(255), z.null()]).optional(),
|
|
||||||
valid_until: z.union([z.string().max(255), z.null()]).optional(),
|
|
||||||
currency: z.string().max(20).optional(),
|
currency: z.string().max(20).optional(),
|
||||||
language: z.string().max(20).optional(),
|
language: z.string().max(20).optional(),
|
||||||
vat_rate: numberInRange(0, 100).optional(),
|
|
||||||
apply_vat: booleanFromForm.optional(),
|
|
||||||
status: z.enum(["draft", "active", "ordered", "invalidated"]).optional(),
|
status: z.enum(["draft", "active", "ordered", "invalidated"]).optional(),
|
||||||
scope_title: z.string().max(255).nullish(),
|
scope_title: z.string().max(255).nullish(),
|
||||||
scope_description: z.string().max(8000).nullish(),
|
scope_description: z.string().max(8000).nullish(),
|
||||||
@@ -61,5 +47,12 @@ export const UpdateQuotationSchema = z.object({
|
|||||||
sections: z.array(ScopeSectionSchema).optional(),
|
sections: z.array(ScopeSectionSchema).optional(),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Update = partial create MINUS the number: quotation numbers are immutable
|
||||||
|
// (assigned by the sequence on finalize), so the field is stripped before it
|
||||||
|
// can ever reach the service.
|
||||||
|
export const UpdateQuotationSchema = CreateQuotationSchema.partial().omit({
|
||||||
|
quotation_number: true,
|
||||||
|
});
|
||||||
|
|
||||||
export type CreateQuotationInput = z.infer<typeof CreateQuotationSchema>;
|
export type CreateQuotationInput = z.infer<typeof CreateQuotationSchema>;
|
||||||
export type UpdateQuotationInput = z.infer<typeof UpdateQuotationSchema>;
|
export type UpdateQuotationInput = z.infer<typeof UpdateQuotationSchema>;
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import {
|
import {
|
||||||
numberFromForm,
|
numberFromForm,
|
||||||
numberInRange,
|
|
||||||
nonNegativeNumberFromForm,
|
nonNegativeNumberFromForm,
|
||||||
positiveNumberFromForm,
|
positiveNumberFromForm,
|
||||||
intIdFromForm,
|
intIdFromForm,
|
||||||
@@ -42,8 +41,6 @@ export const CreateOrderSchema = z.object({
|
|||||||
.default("prijata"),
|
.default("prijata"),
|
||||||
currency: z.string().max(20).optional().default("CZK"),
|
currency: z.string().max(20).optional().default("CZK"),
|
||||||
language: z.string().max(20).optional().default("cs"),
|
language: z.string().max(20).optional().default("cs"),
|
||||||
vat_rate: numberInRange(0, 100).optional().default(21.0),
|
|
||||||
apply_vat: booleanFromForm.optional().default(true),
|
|
||||||
exchange_rate: positiveNumberFromForm.optional().default(1.0),
|
exchange_rate: positiveNumberFromForm.optional().default(1.0),
|
||||||
scope_title: z.string().max(255).nullish(),
|
scope_title: z.string().max(255).nullish(),
|
||||||
scope_description: z.string().max(8000).nullish(),
|
scope_description: z.string().max(8000).nullish(),
|
||||||
@@ -62,8 +59,6 @@ export const UpdateOrderSchema = z.object({
|
|||||||
scope_description: z.string().max(8000).nullish(),
|
scope_description: z.string().max(8000).nullish(),
|
||||||
notes: z.string().max(8000).nullish(),
|
notes: z.string().max(8000).nullish(),
|
||||||
customer_id: nullableIntIdFromForm.optional(),
|
customer_id: nullableIntIdFromForm.optional(),
|
||||||
vat_rate: numberInRange(0, 100).optional(),
|
|
||||||
apply_vat: booleanFromForm.optional(),
|
|
||||||
items: z.array(OrderItemSchema).optional(),
|
items: z.array(OrderItemSchema).optional(),
|
||||||
sections: z.array(OrderSectionSchema).optional(),
|
sections: z.array(OrderSectionSchema).optional(),
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -189,12 +189,16 @@ export async function getStatus(userId: number) {
|
|||||||
const y = now.getFullYear(),
|
const y = now.getFullYear(),
|
||||||
m = now.getMonth(),
|
m = now.getMonth(),
|
||||||
d = now.getDate();
|
d = now.getDate();
|
||||||
const todayStart = new Date(y, m, d, 0, 0, 0);
|
// shift_date is @db.Date: Prisma compares it by its UTC date part, so the
|
||||||
const todayEnd = new Date(y, m, d, 23, 59, 59);
|
// range boundaries must be UTC midnights of the LOCAL calendar day. A local
|
||||||
|
// midnight (= 22:00/23:00 UTC of the previous day) shifts the whole window
|
||||||
|
// one day back. Half-open [gte, lt) ranges.
|
||||||
|
const todayStart = new Date(Date.UTC(y, m, d));
|
||||||
|
const todayEnd = new Date(Date.UTC(y, m, d + 1));
|
||||||
|
|
||||||
// Monthly fund range (used by query #4)
|
// Monthly fund range (used by query #4)
|
||||||
const monthStart = new Date(y, m, 1);
|
const monthStart = new Date(Date.UTC(y, m, 1));
|
||||||
const monthEnd = new Date(y, m + 1, 0, 23, 59, 59);
|
const monthEnd = new Date(Date.UTC(y, m + 1, 1));
|
||||||
|
|
||||||
// Queries 1-4 are independent of one another → run them in parallel.
|
// Queries 1-4 are independent of one another → run them in parallel.
|
||||||
const [ongoingShift, todayShiftsRaw, balance, monthRecords] =
|
const [ongoingShift, todayShiftsRaw, balance, monthRecords] =
|
||||||
@@ -212,7 +216,7 @@ export async function getStatus(userId: number) {
|
|||||||
prisma.attendance.findMany({
|
prisma.attendance.findMany({
|
||||||
where: {
|
where: {
|
||||||
user_id: userId,
|
user_id: userId,
|
||||||
shift_date: { gte: todayStart, lte: todayEnd },
|
shift_date: { gte: todayStart, lt: todayEnd },
|
||||||
departure_time: { not: null },
|
departure_time: { not: null },
|
||||||
},
|
},
|
||||||
include: {
|
include: {
|
||||||
@@ -228,7 +232,7 @@ export async function getStatus(userId: number) {
|
|||||||
prisma.attendance.findMany({
|
prisma.attendance.findMany({
|
||||||
where: {
|
where: {
|
||||||
user_id: userId,
|
user_id: userId,
|
||||||
shift_date: { gte: monthStart, lte: monthEnd },
|
shift_date: { gte: monthStart, lt: monthEnd },
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
]);
|
]);
|
||||||
@@ -585,10 +589,13 @@ export async function getWorkfund(year: number) {
|
|||||||
: bizDays;
|
: bizDays;
|
||||||
const fund = bizDays * 8;
|
const fund = bizDays * 8;
|
||||||
const fundToDate = bizDaysToDate * 8;
|
const fundToDate = bizDaysToDate * 8;
|
||||||
const monthStart = new Date(year, m, 1);
|
// shift_date is @db.Date (compared by UTC date part) → UTC-midnight
|
||||||
const monthEnd = new Date(year, m + 1, 0, 23, 59, 59);
|
// month boundaries, half-open range. Local midnights would double-count
|
||||||
|
// each previous month's last day.
|
||||||
|
const monthStart = new Date(Date.UTC(year, m, 1));
|
||||||
|
const monthEnd = new Date(Date.UTC(year, m + 1, 1));
|
||||||
const monthRecords = await prisma.attendance.findMany({
|
const monthRecords = await prisma.attendance.findMany({
|
||||||
where: { shift_date: { gte: monthStart, lte: monthEnd } },
|
where: { shift_date: { gte: monthStart, lt: monthEnd } },
|
||||||
select: {
|
select: {
|
||||||
user_id: true,
|
user_id: true,
|
||||||
shift_date: true,
|
shift_date: true,
|
||||||
@@ -846,13 +853,16 @@ export async function getPrintData(
|
|||||||
const yr = Number(yearStr);
|
const yr = Number(yearStr);
|
||||||
const mo = Number(monthNumStr);
|
const mo = Number(monthNumStr);
|
||||||
|
|
||||||
const monthStart = new Date(yr, mo - 1, 1);
|
// shift_date is @db.Date (compared by UTC date part) → UTC-midnight month
|
||||||
const monthEnd = new Date(yr, mo, 0, 23, 59, 59);
|
// boundaries, half-open range (local midnights pulled in the previous
|
||||||
|
// month's last day).
|
||||||
|
const monthStart = new Date(Date.UTC(yr, mo - 1, 1));
|
||||||
|
const monthEnd = new Date(Date.UTC(yr, mo, 1));
|
||||||
|
|
||||||
const users = await getAttendanceUsers();
|
const users = await getAttendanceUsers();
|
||||||
|
|
||||||
const where: Record<string, unknown> = {
|
const where: Record<string, unknown> = {
|
||||||
shift_date: { gte: monthStart, lte: monthEnd },
|
shift_date: { gte: monthStart, lt: monthEnd },
|
||||||
};
|
};
|
||||||
if (filterUserId) where.user_id = filterUserId;
|
if (filterUserId) where.user_id = filterUserId;
|
||||||
|
|
||||||
@@ -1043,9 +1053,12 @@ export async function listAttendance(params: ListAttendanceParams) {
|
|||||||
where.user_id = params.userId;
|
where.user_id = params.userId;
|
||||||
}
|
}
|
||||||
if (params.month && params.year) {
|
if (params.month && params.year) {
|
||||||
|
// shift_date is @db.Date: Prisma compares by UTC date part, so the month
|
||||||
|
// boundaries must be UTC midnights. Local midnights shifted the window a
|
||||||
|
// day back (included prev-month's last day, dropped this month's last day).
|
||||||
where.shift_date = {
|
where.shift_date = {
|
||||||
gte: new Date(params.year, params.month - 1, 1),
|
gte: new Date(Date.UTC(params.year, params.month - 1, 1)),
|
||||||
lt: new Date(params.year, params.month, 1),
|
lt: new Date(Date.UTC(params.year, params.month, 1)),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1527,15 +1540,25 @@ export async function createAttendance(
|
|||||||
) {
|
) {
|
||||||
const userId = data.user_id ?? authUserId;
|
const userId = data.user_id ?? authUserId;
|
||||||
const shiftDate = new Date(data.shift_date);
|
const shiftDate = new Date(data.shift_date);
|
||||||
|
// shift_date arrives as "YYYY-MM-DD" → parsed as UTC midnight, and the
|
||||||
|
// @db.Date column is compared by UTC date part. The duplicate/overlap
|
||||||
|
// window must therefore be the UTC day [shiftDate, shiftDate+1) — building
|
||||||
|
// it from LOCAL getters made both bounds 22:00/23:00 UTC of the previous
|
||||||
|
// day, so the validation queried ONLY yesterday's records (same-day
|
||||||
|
// overlaps undetected, false duplicates when yesterday had records).
|
||||||
const startOfDay = new Date(
|
const startOfDay = new Date(
|
||||||
shiftDate.getFullYear(),
|
Date.UTC(
|
||||||
shiftDate.getMonth(),
|
shiftDate.getUTCFullYear(),
|
||||||
shiftDate.getDate(),
|
shiftDate.getUTCMonth(),
|
||||||
|
shiftDate.getUTCDate(),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
const endOfDay = new Date(
|
const endOfDay = new Date(
|
||||||
shiftDate.getFullYear(),
|
Date.UTC(
|
||||||
shiftDate.getMonth(),
|
shiftDate.getUTCFullYear(),
|
||||||
shiftDate.getDate() + 1,
|
shiftDate.getUTCMonth(),
|
||||||
|
shiftDate.getUTCDate() + 1,
|
||||||
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
// Multiple work shifts per day are allowed — only block when the new
|
// Multiple work shifts per day are allowed — only block when the new
|
||||||
|
|||||||
@@ -1,7 +1,11 @@
|
|||||||
import prisma from "../config/database";
|
import prisma from "../config/database";
|
||||||
import { config } from "../config/env";
|
import { config } from "../config/env";
|
||||||
import { sendMail } from "./mailer";
|
import { sendMail } from "./mailer";
|
||||||
import { localDateCzStr, localDateStr } from "../utils/date";
|
import {
|
||||||
|
localDateCzStr,
|
||||||
|
localDateStr,
|
||||||
|
utcMidnightOfLocalDay,
|
||||||
|
} from "../utils/date";
|
||||||
import { getSystemSettings } from "./system-settings";
|
import { getSystemSettings } from "./system-settings";
|
||||||
|
|
||||||
interface AlertInvoice {
|
interface AlertInvoice {
|
||||||
@@ -33,17 +37,35 @@ function formatAmount(n: number | { toNumber?: () => number }): string {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Alert window [today, today+3] for the due-date queries + the local date
|
||||||
|
* strings the classifier matches against.
|
||||||
|
*
|
||||||
|
* due_date is @db.Date: Prisma truncates the filter Dates to their UTC date
|
||||||
|
* part, so the window boundaries must be UTC midnights of the LOCAL calendar
|
||||||
|
* day. A local midnight (22:00/23:00 UTC of the previous day) shifted the
|
||||||
|
* whole window a day early — the "splatnost za 3 dny" advance alert
|
||||||
|
* (due == today+3) could then never fire.
|
||||||
|
*
|
||||||
|
* Exported (with an injectable `now`) so the window math is unit-testable.
|
||||||
|
*/
|
||||||
|
export function computeAlertWindow(now: Date = new Date()) {
|
||||||
|
const today = utcMidnightOfLocalDay(now);
|
||||||
|
const todayStr = localDateStr(now);
|
||||||
|
const in3days = new Date(today);
|
||||||
|
in3days.setUTCDate(in3days.getUTCDate() + 3);
|
||||||
|
const in3daysStr = localDateStr(
|
||||||
|
new Date(now.getFullYear(), now.getMonth(), now.getDate() + 3),
|
||||||
|
);
|
||||||
|
return { today, todayStr, in3days, in3daysStr };
|
||||||
|
}
|
||||||
|
|
||||||
export async function checkInvoiceAlerts(): Promise<void> {
|
export async function checkInvoiceAlerts(): Promise<void> {
|
||||||
const settings = await getSystemSettings();
|
const settings = await getSystemSettings();
|
||||||
const alertEmail = settings.invoice_alert_email || config.email.invoiceAlert;
|
const alertEmail = settings.invoice_alert_email || config.email.invoiceAlert;
|
||||||
if (!alertEmail) return;
|
if (!alertEmail) return;
|
||||||
|
|
||||||
const today = new Date();
|
const { today, todayStr, in3days, in3daysStr } = computeAlertWindow();
|
||||||
today.setHours(0, 0, 0, 0);
|
|
||||||
const todayStr = localDateStr(today);
|
|
||||||
const in3days = new Date(today);
|
|
||||||
in3days.setDate(in3days.getDate() + 3);
|
|
||||||
const in3daysStr = localDateStr(in3days);
|
|
||||||
|
|
||||||
// Classify a due date into an alert type/label, or null if it doesn't match.
|
// Classify a due date into an alert type/label, or null if it doesn't match.
|
||||||
const classify = (
|
const classify = (
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import prisma from "../config/database";
|
import prisma from "../config/database";
|
||||||
|
import { utcMidnightOfLocalDay } from "../utils/date";
|
||||||
import { toCzk } from "./exchange-rates";
|
import { toCzk } from "./exchange-rates";
|
||||||
import {
|
import {
|
||||||
generateInvoiceNumber,
|
generateInvoiceNumber,
|
||||||
@@ -34,6 +35,12 @@ interface InvoiceItemInput {
|
|||||||
position?: number;
|
position?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface InvoiceSectionInput {
|
||||||
|
title?: string | null;
|
||||||
|
title_cz?: string | null;
|
||||||
|
content?: string | null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Body shape accepted by createInvoice/updateInvoice. All fields optional and
|
* Body shape accepted by createInvoice/updateInvoice. All fields optional and
|
||||||
* loosely typed because the payload arrives validated-but-coerced from the Zod
|
* loosely typed because the payload arrives validated-but-coerced from the Zod
|
||||||
@@ -61,10 +68,10 @@ export interface InvoiceInput {
|
|||||||
issued_by?: string | null;
|
issued_by?: string | null;
|
||||||
billing_text?: string | null;
|
billing_text?: string | null;
|
||||||
language?: string;
|
language?: string;
|
||||||
notes?: string | null;
|
|
||||||
internal_notes?: string | null;
|
internal_notes?: string | null;
|
||||||
paid_date?: string | null;
|
paid_date?: string | null;
|
||||||
items?: InvoiceItemInput[];
|
items?: InvoiceItemInput[];
|
||||||
|
sections?: InvoiceSectionInput[];
|
||||||
[key: string]: unknown;
|
[key: string]: unknown;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -139,8 +146,12 @@ function buildInvoiceWhere(
|
|||||||
if (status) where.status = status;
|
if (status) where.status = status;
|
||||||
if (customer_id) where.customer_id = customer_id;
|
if (customer_id) where.customer_id = customer_id;
|
||||||
if (month && year) {
|
if (month && year) {
|
||||||
const from = new Date(year, month - 1, 1);
|
// issue_date is @db.Date: Prisma compares by UTC date part, so the month
|
||||||
const to = new Date(year, month, 1);
|
// boundaries must be UTC midnights. Local midnights shifted the window a
|
||||||
|
// day back — the filter included the previous month's last day and
|
||||||
|
// DROPPED invoices issued on the selected month's last day.
|
||||||
|
const from = new Date(Date.UTC(year, month - 1, 1));
|
||||||
|
const to = new Date(Date.UTC(year, month, 1));
|
||||||
where.issue_date = { gte: from, lt: to };
|
where.issue_date = { gte: from, lt: to };
|
||||||
}
|
}
|
||||||
if (search) {
|
if (search) {
|
||||||
@@ -180,13 +191,18 @@ function computeInvoiceTotals(
|
|||||||
|
|
||||||
export async function markOverdueInvoices() {
|
export async function markOverdueInvoices() {
|
||||||
try {
|
try {
|
||||||
|
// due_date is @db.Date (UTC midnight of the calendar day). Compare
|
||||||
|
// against UTC midnight of the LOCAL today — a bare new Date() has
|
||||||
|
// yesterday's UTC date during the 00:00–02:00 Prague window, so the
|
||||||
|
// overdue flip lagged a day there. Due TODAY = not yet overdue.
|
||||||
|
const today = utcMidnightOfLocalDay();
|
||||||
await prisma.invoices.updateMany({
|
await prisma.invoices.updateMany({
|
||||||
where: { status: "issued", due_date: { lt: new Date() } },
|
where: { status: "issued", due_date: { lt: today } },
|
||||||
data: { status: "overdue" },
|
data: { status: "overdue" },
|
||||||
});
|
});
|
||||||
// Reverse: if due_date was changed to future, set back to issued
|
// Reverse: if due_date was changed to today/future, set back to issued
|
||||||
await prisma.invoices.updateMany({
|
await prisma.invoices.updateMany({
|
||||||
where: { status: "overdue", due_date: { gte: new Date() } },
|
where: { status: "overdue", due_date: { gte: today } },
|
||||||
data: { status: "issued" },
|
data: { status: "issued" },
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@@ -312,10 +328,13 @@ export async function getInvoiceStats(queryMonth?: number, queryYear?: number) {
|
|||||||
const year = queryYear || now.getFullYear();
|
const year = queryYear || now.getFullYear();
|
||||||
const month = queryMonth || now.getMonth() + 1;
|
const month = queryMonth || now.getMonth() + 1;
|
||||||
|
|
||||||
const monthStart = new Date(year, month - 1, 1);
|
// issue_date is @db.Date (compared by UTC date part) → UTC-midnight period
|
||||||
const monthEnd = new Date(year, month, 0, 23, 59, 59);
|
// boundaries, half-open [gte, lt) ranges. Local-midnight bounds included
|
||||||
const startOfYear = new Date(year, 0, 1);
|
// the previous period's boundary day in the stats.
|
||||||
const endOfYear = new Date(year, 11, 31, 23, 59, 59);
|
const monthStart = new Date(Date.UTC(year, month - 1, 1));
|
||||||
|
const nextMonthStart = new Date(Date.UTC(year, month, 1));
|
||||||
|
const startOfYear = new Date(Date.UTC(year, 0, 1));
|
||||||
|
const startOfNextYear = new Date(Date.UTC(year + 1, 0, 1));
|
||||||
|
|
||||||
const [monthInvoices, awaitingInvoices, overdueInvoices] = await Promise.all([
|
const [monthInvoices, awaitingInvoices, overdueInvoices] = await Promise.all([
|
||||||
prisma.invoices.findMany({
|
prisma.invoices.findMany({
|
||||||
@@ -323,21 +342,21 @@ export async function getInvoiceStats(queryMonth?: number, queryYear?: number) {
|
|||||||
// Drafts are not real financial documents — exclude them so their
|
// Drafts are not real financial documents — exclude them so their
|
||||||
// VAT/amounts never inflate the monthly stats (vat_month etc.).
|
// VAT/amounts never inflate the monthly stats (vat_month etc.).
|
||||||
status: { not: "draft" },
|
status: { not: "draft" },
|
||||||
issue_date: { gte: monthStart, lte: monthEnd },
|
issue_date: { gte: monthStart, lt: nextMonthStart },
|
||||||
},
|
},
|
||||||
include: { invoice_items: true },
|
include: { invoice_items: true },
|
||||||
}),
|
}),
|
||||||
prisma.invoices.findMany({
|
prisma.invoices.findMany({
|
||||||
where: {
|
where: {
|
||||||
status: "issued",
|
status: "issued",
|
||||||
issue_date: { gte: startOfYear, lte: endOfYear },
|
issue_date: { gte: startOfYear, lt: startOfNextYear },
|
||||||
},
|
},
|
||||||
include: { invoice_items: true },
|
include: { invoice_items: true },
|
||||||
}),
|
}),
|
||||||
prisma.invoices.findMany({
|
prisma.invoices.findMany({
|
||||||
where: {
|
where: {
|
||||||
status: "overdue",
|
status: "overdue",
|
||||||
issue_date: { gte: startOfYear, lte: endOfYear },
|
issue_date: { gte: startOfYear, lt: startOfNextYear },
|
||||||
},
|
},
|
||||||
include: { invoice_items: true },
|
include: { invoice_items: true },
|
||||||
}),
|
}),
|
||||||
@@ -456,14 +475,17 @@ export async function getInvoice(id: number) {
|
|||||||
include: {
|
include: {
|
||||||
customers: true,
|
customers: true,
|
||||||
invoice_items: { orderBy: { position: "asc" } },
|
invoice_items: { orderBy: { position: "asc" } },
|
||||||
|
// id tiebreak: position can repeat, keep the order deterministic.
|
||||||
|
invoice_sections: { orderBy: [{ position: "asc" }, { id: "asc" }] },
|
||||||
orders: { select: { id: true, order_number: true } },
|
orders: { select: { id: true, order_number: true } },
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
if (!invoice) return null;
|
if (!invoice) return null;
|
||||||
const { invoice_items, ...rest } = invoice;
|
const { invoice_items, invoice_sections, ...rest } = invoice;
|
||||||
return {
|
return {
|
||||||
...rest,
|
...rest,
|
||||||
items: invoice_items,
|
items: invoice_items,
|
||||||
|
sections: invoice_sections,
|
||||||
customer: invoice.customers,
|
customer: invoice.customers,
|
||||||
customer_name: invoice.customers?.name || null,
|
customer_name: invoice.customers?.name || null,
|
||||||
order_number: invoice.orders?.order_number || null,
|
order_number: invoice.orders?.order_number || null,
|
||||||
@@ -486,48 +508,69 @@ export async function createInvoice(body: InvoiceInput) {
|
|||||||
explicit ??
|
explicit ??
|
||||||
(status === "draft" ? null : (await generateInvoiceNumber()).number);
|
(status === "draft" ? null : (await generateInvoiceNumber()).number);
|
||||||
|
|
||||||
const invoice = await prisma.invoices.create({
|
// Header + items + sections are ONE write — a failure mid-way must not
|
||||||
data: {
|
// leave a headerless/partial invoice behind.
|
||||||
invoice_number: invoiceNumber,
|
const invoice = await prisma.$transaction(async (tx) => {
|
||||||
order_id: body.order_id ? Number(body.order_id) : null,
|
const created = await tx.invoices.create({
|
||||||
customer_id: body.customer_id ? Number(body.customer_id) : null,
|
data: {
|
||||||
status,
|
invoice_number: invoiceNumber,
|
||||||
currency: body.currency ? String(body.currency) : "CZK",
|
order_id: body.order_id ? Number(body.order_id) : null,
|
||||||
vat_rate: body.vat_rate != null ? Number(body.vat_rate) : 21.0,
|
customer_id: body.customer_id ? Number(body.customer_id) : null,
|
||||||
apply_vat: body.apply_vat !== false,
|
status,
|
||||||
payment_method: body.payment_method ? String(body.payment_method) : null,
|
currency: body.currency ? String(body.currency) : "CZK",
|
||||||
constant_symbol: body.constant_symbol
|
vat_rate: body.vat_rate != null ? Number(body.vat_rate) : 21.0,
|
||||||
? String(body.constant_symbol)
|
apply_vat: body.apply_vat !== false,
|
||||||
: null,
|
payment_method: body.payment_method
|
||||||
bank_name: body.bank_name ? String(body.bank_name) : null,
|
? String(body.payment_method)
|
||||||
bank_swift: body.bank_swift ? String(body.bank_swift) : null,
|
: null,
|
||||||
bank_iban: body.bank_iban ? String(body.bank_iban) : null,
|
constant_symbol: body.constant_symbol
|
||||||
bank_account: body.bank_account ? String(body.bank_account) : null,
|
? String(body.constant_symbol)
|
||||||
issue_date: body.issue_date ? new Date(String(body.issue_date)) : null,
|
: null,
|
||||||
due_date: body.due_date ? new Date(String(body.due_date)) : null,
|
bank_name: body.bank_name ? String(body.bank_name) : null,
|
||||||
tax_date: body.tax_date ? new Date(String(body.tax_date)) : null,
|
bank_swift: body.bank_swift ? String(body.bank_swift) : null,
|
||||||
issued_by: body.issued_by ? String(body.issued_by) : null,
|
bank_iban: body.bank_iban ? String(body.bank_iban) : null,
|
||||||
billing_text: body.billing_text ? String(body.billing_text) : null,
|
bank_account: body.bank_account ? String(body.bank_account) : null,
|
||||||
language: body.language ? String(body.language) : "cs",
|
issue_date: body.issue_date ? new Date(String(body.issue_date)) : null,
|
||||||
notes: body.notes ? String(body.notes) : null,
|
due_date: body.due_date ? new Date(String(body.due_date)) : null,
|
||||||
internal_notes: body.internal_notes ? String(body.internal_notes) : null,
|
tax_date: body.tax_date ? new Date(String(body.tax_date)) : null,
|
||||||
},
|
issued_by: body.issued_by ? String(body.issued_by) : null,
|
||||||
});
|
billing_text: body.billing_text ? String(body.billing_text) : null,
|
||||||
|
language: body.language ? String(body.language) : "cs",
|
||||||
if (Array.isArray(body.items)) {
|
internal_notes: body.internal_notes
|
||||||
await prisma.invoice_items.createMany({
|
? String(body.internal_notes)
|
||||||
data: body.items.map((item, i) => ({
|
: null,
|
||||||
invoice_id: invoice.id,
|
},
|
||||||
description: item.description ?? null,
|
|
||||||
item_description: item.item_description ?? null,
|
|
||||||
quantity: item.quantity ?? 1,
|
|
||||||
unit: item.unit ?? null,
|
|
||||||
unit_price: item.unit_price ?? 0,
|
|
||||||
vat_rate: item.vat_rate ?? 21.0,
|
|
||||||
position: item.position ?? i,
|
|
||||||
})),
|
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
if (Array.isArray(body.items)) {
|
||||||
|
await tx.invoice_items.createMany({
|
||||||
|
data: body.items.map((item, i) => ({
|
||||||
|
invoice_id: created.id,
|
||||||
|
description: item.description ?? null,
|
||||||
|
item_description: item.item_description ?? null,
|
||||||
|
quantity: item.quantity ?? 1,
|
||||||
|
unit: item.unit ?? null,
|
||||||
|
unit_price: item.unit_price ?? 0,
|
||||||
|
vat_rate: item.vat_rate ?? 21.0,
|
||||||
|
position: item.position ?? i,
|
||||||
|
})),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Array.isArray(body.sections)) {
|
||||||
|
await tx.invoice_sections.createMany({
|
||||||
|
data: body.sections.map((s, i) => ({
|
||||||
|
invoice_id: created.id,
|
||||||
|
title: s.title ?? null,
|
||||||
|
title_cz: s.title_cz ?? null,
|
||||||
|
content: s.content ?? null,
|
||||||
|
position: i,
|
||||||
|
})),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return created;
|
||||||
|
});
|
||||||
|
|
||||||
return invoice;
|
return invoice;
|
||||||
}
|
}
|
||||||
@@ -588,10 +631,8 @@ export async function updateInvoice(id: number, body: InvoiceInput) {
|
|||||||
data.tax_date = body.tax_date ? new Date(String(body.tax_date)) : null;
|
data.tax_date = body.tax_date ? new Date(String(body.tax_date)) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Notes editable in draft/issued/overdue
|
// Internal notes editable in draft/issued/overdue (never printed)
|
||||||
if (editable) {
|
if (editable) {
|
||||||
if (body.notes !== undefined)
|
|
||||||
data.notes = body.notes ? String(body.notes) : null;
|
|
||||||
if (body.internal_notes !== undefined)
|
if (body.internal_notes !== undefined)
|
||||||
data.internal_notes = body.internal_notes
|
data.internal_notes = body.internal_notes
|
||||||
? String(body.internal_notes)
|
? String(body.internal_notes)
|
||||||
@@ -601,9 +642,11 @@ export async function updateInvoice(id: number, body: InvoiceInput) {
|
|||||||
// Status change
|
// Status change
|
||||||
if (body.status !== undefined) {
|
if (body.status !== undefined) {
|
||||||
data.status = String(body.status);
|
data.status = String(body.status);
|
||||||
// Auto-set paid_date when transitioning to paid
|
// Auto-set paid_date when transitioning to paid. paid_date is @db.Date
|
||||||
|
// (truncated to the UTC date part) — utcMidnightOfLocalDay keeps the
|
||||||
|
// LOCAL calendar day even during the 00:00–02:00 Prague window.
|
||||||
if (String(body.status) === "paid" && !existing.paid_date) {
|
if (String(body.status) === "paid" && !existing.paid_date) {
|
||||||
data.paid_date = new Date();
|
data.paid_date = utcMidnightOfLocalDay();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -629,22 +672,39 @@ export async function updateInvoice(id: number, body: InvoiceInput) {
|
|||||||
await prisma.invoices.update({ where: { id }, data });
|
await prisma.invoices.update({ where: { id }, data });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Allow items update while editable (draft/issued/overdue).
|
// Allow items/sections full-replace while editable (draft/issued/overdue) —
|
||||||
if (editable && Array.isArray(body.items)) {
|
// both in ONE transaction so a failure can't leave a half-replaced document.
|
||||||
|
const replaceItems = editable && Array.isArray(body.items);
|
||||||
|
const replaceSections = editable && Array.isArray(body.sections);
|
||||||
|
if (replaceItems || replaceSections) {
|
||||||
await prisma.$transaction(async (tx) => {
|
await prisma.$transaction(async (tx) => {
|
||||||
await tx.invoice_items.deleteMany({ where: { invoice_id: id } });
|
if (replaceItems) {
|
||||||
await tx.invoice_items.createMany({
|
await tx.invoice_items.deleteMany({ where: { invoice_id: id } });
|
||||||
data: body.items!.map((item, i) => ({
|
await tx.invoice_items.createMany({
|
||||||
invoice_id: id,
|
data: body.items!.map((item, i) => ({
|
||||||
description: item.description ?? null,
|
invoice_id: id,
|
||||||
item_description: item.item_description ?? null,
|
description: item.description ?? null,
|
||||||
quantity: item.quantity ?? 1,
|
item_description: item.item_description ?? null,
|
||||||
unit: item.unit ?? null,
|
quantity: item.quantity ?? 1,
|
||||||
unit_price: item.unit_price ?? 0,
|
unit: item.unit ?? null,
|
||||||
vat_rate: item.vat_rate ?? 21.0,
|
unit_price: item.unit_price ?? 0,
|
||||||
position: item.position ?? i,
|
vat_rate: item.vat_rate ?? 21.0,
|
||||||
})),
|
position: item.position ?? i,
|
||||||
});
|
})),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (replaceSections) {
|
||||||
|
await tx.invoice_sections.deleteMany({ where: { invoice_id: id } });
|
||||||
|
await tx.invoice_sections.createMany({
|
||||||
|
data: body.sections!.map((s, i) => ({
|
||||||
|
invoice_id: id,
|
||||||
|
title: s.title ?? null,
|
||||||
|
title_cz: s.title_cz ?? null,
|
||||||
|
content: s.content ?? null,
|
||||||
|
position: i,
|
||||||
|
})),
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,14 @@
|
|||||||
import type { $Enums } from "@prisma/client";
|
import { Prisma, type $Enums } from "@prisma/client";
|
||||||
import prisma from "../config/database";
|
import prisma from "../config/database";
|
||||||
|
import { utcMidnightOfLocalDay } from "../utils/date";
|
||||||
import {
|
import {
|
||||||
generateIssuedOrderNumber,
|
generateIssuedOrderNumber,
|
||||||
previewIssuedOrderNumber,
|
previewIssuedOrderNumber,
|
||||||
releaseIssuedOrderNumber,
|
releaseIssuedOrderNumber,
|
||||||
assignIssuedOrderNumber,
|
assignIssuedOrderNumber,
|
||||||
|
isIssuedOrderNumberTaken,
|
||||||
} from "./numbering.service";
|
} from "./numbering.service";
|
||||||
|
import { nasOrdersManager } from "./nas-financials-manager";
|
||||||
|
|
||||||
export interface IssuedOrderItemInput {
|
export interface IssuedOrderItemInput {
|
||||||
description?: string | null;
|
description?: string | null;
|
||||||
@@ -13,34 +16,36 @@ export interface IssuedOrderItemInput {
|
|||||||
quantity?: number | string | null;
|
quantity?: number | string | null;
|
||||||
unit?: string | null;
|
unit?: string | null;
|
||||||
unit_price?: number | string | null;
|
unit_price?: number | string | null;
|
||||||
vat_rate?: number | string | null;
|
position?: number | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IssuedOrderSectionInput {
|
||||||
|
title?: string | null;
|
||||||
|
title_cz?: string | null;
|
||||||
|
content?: string | null;
|
||||||
position?: number | null;
|
position?: number | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IssuedOrderInput {
|
export interface IssuedOrderInput {
|
||||||
po_number?: string | number | null;
|
po_number?: string | number | null;
|
||||||
customer_id?: number | string | null;
|
supplier_id?: number | string | null;
|
||||||
status?: string;
|
status?: string;
|
||||||
currency?: string;
|
currency?: string;
|
||||||
vat_rate?: number | string | null;
|
|
||||||
apply_vat?: boolean | number | string;
|
|
||||||
exchange_rate?: number | string | null;
|
exchange_rate?: number | string | null;
|
||||||
order_date?: string | null;
|
order_date?: string | null;
|
||||||
delivery_date?: string | null;
|
delivery_date?: string | null;
|
||||||
language?: string;
|
language?: string;
|
||||||
delivery_terms?: string | null;
|
order_text?: string | null;
|
||||||
payment_terms?: string | null;
|
|
||||||
issued_by?: string | null;
|
|
||||||
notes?: string | null;
|
|
||||||
internal_notes?: string | null;
|
internal_notes?: string | null;
|
||||||
items?: IssuedOrderItemInput[];
|
items?: IssuedOrderItemInput[];
|
||||||
|
sections?: IssuedOrderSectionInput[];
|
||||||
[key: string]: unknown;
|
[key: string]: unknown;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface IssuedOrderFilterParams {
|
interface IssuedOrderFilterParams {
|
||||||
search?: string;
|
search?: string;
|
||||||
status?: string;
|
status?: string;
|
||||||
customer_id?: number;
|
supplier_id?: number;
|
||||||
month?: number;
|
month?: number;
|
||||||
year?: number;
|
year?: number;
|
||||||
}
|
}
|
||||||
@@ -66,20 +71,23 @@ export interface CurrencyAmount {
|
|||||||
function buildIssuedOrderWhere(
|
function buildIssuedOrderWhere(
|
||||||
params: IssuedOrderFilterParams,
|
params: IssuedOrderFilterParams,
|
||||||
): Record<string, unknown> {
|
): Record<string, unknown> {
|
||||||
const { search, status, customer_id, month, year } = params;
|
const { search, status, supplier_id, month, year } = params;
|
||||||
const where: Record<string, unknown> = {};
|
const where: Record<string, unknown> = {};
|
||||||
if (status) where.status = status;
|
if (status) where.status = status;
|
||||||
if (customer_id) where.customer_id = customer_id;
|
if (supplier_id) where.supplier_id = supplier_id;
|
||||||
if (search) {
|
if (search) {
|
||||||
where.OR = [
|
where.OR = [
|
||||||
{ po_number: { contains: search } },
|
{ po_number: { contains: search } },
|
||||||
{ customers: { name: { contains: search } } },
|
{ suppliers: { name: { contains: search } } },
|
||||||
{ customers: { company_id: { contains: search } } },
|
{ suppliers: { ico: { contains: search } } },
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
if (month && year) {
|
if (month && year) {
|
||||||
const from = new Date(year, month - 1, 1);
|
// order_date is @db.Date: Prisma compares by UTC date part, so the month
|
||||||
const to = new Date(year, month, 1);
|
// boundaries must be UTC midnights. Local midnights shifted the window a
|
||||||
|
// day back (included prev-month's last day, dropped this month's last day).
|
||||||
|
const from = new Date(Date.UTC(year, month - 1, 1));
|
||||||
|
const to = new Date(Date.UTC(year, month, 1));
|
||||||
where.order_date = { gte: from, lt: to };
|
where.order_date = { gte: from, lt: to };
|
||||||
}
|
}
|
||||||
return where;
|
return where;
|
||||||
@@ -98,35 +106,41 @@ const ALLOWED_SORT_FIELDS = [
|
|||||||
"po_number",
|
"po_number",
|
||||||
"status",
|
"status",
|
||||||
"order_date",
|
"order_date",
|
||||||
|
"created_at",
|
||||||
"currency",
|
"currency",
|
||||||
];
|
];
|
||||||
|
|
||||||
/** NET base + VAT-on-top, rounded per line before accumulation (mirrors invoices). */
|
/**
|
||||||
|
* Non-status update fields — used by the editable-state guard so a field edit
|
||||||
|
* on a confirmed/completed/cancelled order is rejected EXPLICITLY (it used to
|
||||||
|
* be silently dropped). `po_number` is stripped by the schema and the service
|
||||||
|
* never reads it on update.
|
||||||
|
*/
|
||||||
|
const NON_STATUS_UPDATE_FIELDS = [
|
||||||
|
"supplier_id",
|
||||||
|
"currency",
|
||||||
|
"exchange_rate",
|
||||||
|
"order_date",
|
||||||
|
"delivery_date",
|
||||||
|
"language",
|
||||||
|
"order_text",
|
||||||
|
"internal_notes",
|
||||||
|
"items",
|
||||||
|
"sections",
|
||||||
|
] as const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* NET-only total: issued orders (PO) are not tax documents — VAT never appears
|
||||||
|
* on them. The total is the plain sum of qty × unit_price, rounded to 2dp.
|
||||||
|
*/
|
||||||
export function computeIssuedOrderTotals(
|
export function computeIssuedOrderTotals(
|
||||||
items: Array<{ quantity: unknown; unit_price: unknown; vat_rate: unknown }>,
|
items: Array<{ quantity: unknown; unit_price: unknown }>,
|
||||||
applyVat: boolean | null,
|
|
||||||
defaultVatRate: unknown,
|
|
||||||
) {
|
) {
|
||||||
let subtotal = 0;
|
let total = 0;
|
||||||
let vat = 0;
|
|
||||||
for (const it of items) {
|
for (const it of items) {
|
||||||
const base = (Number(it.quantity) || 0) * (Number(it.unit_price) || 0);
|
total += (Number(it.quantity) || 0) * (Number(it.unit_price) || 0);
|
||||||
subtotal += base;
|
|
||||||
if (applyVat) {
|
|
||||||
const rate =
|
|
||||||
it.vat_rate != null && it.vat_rate !== ""
|
|
||||||
? Number(it.vat_rate)
|
|
||||||
: defaultVatRate != null
|
|
||||||
? Number(defaultVatRate)
|
|
||||||
: 21;
|
|
||||||
vat += Math.round(base * (rate / 100) * 100) / 100;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return {
|
return { total: Math.round(total * 100) / 100 };
|
||||||
subtotal: Math.round(subtotal * 100) / 100,
|
|
||||||
vat_amount: Math.round(vat * 100) / 100,
|
|
||||||
total: Math.round((subtotal + vat) * 100) / 100,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function listIssuedOrders(params: ListIssuedOrdersParams) {
|
export async function listIssuedOrders(params: ListIssuedOrdersParams) {
|
||||||
@@ -150,7 +164,7 @@ export async function listIssuedOrders(params: ListIssuedOrdersParams) {
|
|||||||
take: limit,
|
take: limit,
|
||||||
orderBy,
|
orderBy,
|
||||||
include: {
|
include: {
|
||||||
customers: { select: { id: true, name: true } },
|
suppliers: { select: { id: true, name: true } },
|
||||||
issued_order_items: true,
|
issued_order_items: true,
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
@@ -158,16 +172,12 @@ export async function listIssuedOrders(params: ListIssuedOrdersParams) {
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
const enriched = rows.map((o) => {
|
const enriched = rows.map((o) => {
|
||||||
const totals = computeIssuedOrderTotals(
|
const totals = computeIssuedOrderTotals(o.issued_order_items);
|
||||||
o.issued_order_items,
|
|
||||||
o.apply_vat,
|
|
||||||
o.vat_rate,
|
|
||||||
);
|
|
||||||
const { issued_order_items, ...rest } = o;
|
const { issued_order_items, ...rest } = o;
|
||||||
return {
|
return {
|
||||||
...rest,
|
...rest,
|
||||||
items: issued_order_items,
|
items: issued_order_items,
|
||||||
customer_name: o.customers?.name || null,
|
supplier_name: o.suppliers?.name || null,
|
||||||
...totals,
|
...totals,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
@@ -176,7 +186,7 @@ export async function listIssuedOrders(params: ListIssuedOrdersParams) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sum issued-order TOTAL (incl. VAT) per currency across the WHOLE filtered set
|
* Sum issued-order NET total per currency across the WHOLE filtered set
|
||||||
* (not a single page). Reuses `buildIssuedOrderWhere` so filters track the list,
|
* (not a single page). Reuses `buildIssuedOrderWhere` so filters track the list,
|
||||||
* and `computeIssuedOrderTotals` so per-order math matches the list/detail.
|
* and `computeIssuedOrderTotals` so per-order math matches the list/detail.
|
||||||
* Currency defaults to "CZK" when the column is null. Returns one entry per
|
* Currency defaults to "CZK" when the column is null. Returns one entry per
|
||||||
@@ -194,11 +204,7 @@ export async function getIssuedOrderTotals(
|
|||||||
|
|
||||||
const byCurrency: Record<string, number> = {};
|
const byCurrency: Record<string, number> = {};
|
||||||
for (const o of rows) {
|
for (const o of rows) {
|
||||||
const { total } = computeIssuedOrderTotals(
|
const { total } = computeIssuedOrderTotals(o.issued_order_items);
|
||||||
o.issued_order_items,
|
|
||||||
o.apply_vat,
|
|
||||||
o.vat_rate,
|
|
||||||
);
|
|
||||||
const cur = o.currency || "CZK";
|
const cur = o.currency || "CZK";
|
||||||
byCurrency[cur] = (byCurrency[cur] || 0) + (Number(total) || 0);
|
byCurrency[cur] = (byCurrency[cur] || 0) + (Number(total) || 0);
|
||||||
}
|
}
|
||||||
@@ -217,87 +223,144 @@ export async function getIssuedOrder(id: number) {
|
|||||||
const order = await prisma.issued_orders.findUnique({
|
const order = await prisma.issued_orders.findUnique({
|
||||||
where: { id },
|
where: { id },
|
||||||
include: {
|
include: {
|
||||||
customers: true,
|
// Same minimal field set as the picker lookup endpoint — the full row
|
||||||
|
// would expose internal notes/contact to any orders.view holder.
|
||||||
|
suppliers: {
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
name: true,
|
||||||
|
ico: true,
|
||||||
|
dic: true,
|
||||||
|
address: true,
|
||||||
|
email: true,
|
||||||
|
phone: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
issued_order_items: { orderBy: { position: "asc" } },
|
issued_order_items: { orderBy: { position: "asc" } },
|
||||||
|
// id tiebreak: position can repeat, keep the order deterministic.
|
||||||
|
issued_order_sections: {
|
||||||
|
orderBy: [{ position: "asc" }, { id: "asc" }],
|
||||||
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
if (!order) return null;
|
if (!order) return null;
|
||||||
const { issued_order_items, ...rest } = order;
|
const { issued_order_items, issued_order_sections, ...rest } = order;
|
||||||
return {
|
return {
|
||||||
...rest,
|
...rest,
|
||||||
items: issued_order_items,
|
items: issued_order_items,
|
||||||
customer: order.customers,
|
sections: issued_order_sections,
|
||||||
customer_name: order.customers?.name || null,
|
supplier: order.suppliers,
|
||||||
|
supplier_name: order.suppliers?.name || null,
|
||||||
valid_transitions: VALID_TRANSITIONS[order.status as string] || [],
|
valid_transitions: VALID_TRANSITIONS[order.status as string] || [],
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function createIssuedOrder(body: IssuedOrderInput) {
|
export async function createIssuedOrder(body: IssuedOrderInput) {
|
||||||
return prisma.$transaction(async (tx) => {
|
try {
|
||||||
const status = body.status ? String(body.status) : "draft";
|
return await prisma.$transaction(async (tx) => {
|
||||||
|
const status = body.status ? String(body.status) : "draft";
|
||||||
|
|
||||||
// Deferred numbering: a draft carries NO po_number (the column is
|
// Validate the referenced supplier exists BEFORE the insert — a dangling
|
||||||
// nullable-unique so many drafts coexist). The official number is consumed
|
// FK would otherwise surface as a P2003 500 instead of a clean 400.
|
||||||
// only when the draft is finalized (assignIssuedOrderNumber on draft->sent).
|
const supplierId = body.supplier_id ? Number(body.supplier_id) : null;
|
||||||
// A caller-supplied number, or a doc created already-finalized, numbers now.
|
if (supplierId) {
|
||||||
const explicit =
|
const supplier = await tx.sklad_suppliers.findUnique({
|
||||||
body.po_number !== undefined &&
|
where: { id: supplierId },
|
||||||
body.po_number !== null &&
|
select: { id: true },
|
||||||
body.po_number !== ""
|
});
|
||||||
? String(body.po_number)
|
if (!supplier) return { error: "supplier_not_found" as const };
|
||||||
: null;
|
}
|
||||||
const poNumber =
|
|
||||||
explicit ??
|
|
||||||
(status === "draft"
|
|
||||||
? null
|
|
||||||
: (await generateIssuedOrderNumber(undefined, tx)).number);
|
|
||||||
|
|
||||||
const order = await tx.issued_orders.create({
|
// Deferred numbering: a draft carries NO po_number (the column is
|
||||||
data: {
|
// nullable-unique so many drafts coexist). The official number is consumed
|
||||||
po_number: poNumber,
|
// only when the draft is finalized (assignIssuedOrderNumber on draft->sent).
|
||||||
customer_id: body.customer_id ? Number(body.customer_id) : null,
|
// A caller-supplied number, or a doc created already-finalized, numbers now.
|
||||||
status: status as $Enums.issued_orders_status,
|
const explicit =
|
||||||
currency: body.currency ? String(body.currency) : "CZK",
|
body.po_number !== undefined &&
|
||||||
vat_rate: body.vat_rate != null ? Number(body.vat_rate) : 21.0,
|
body.po_number !== null &&
|
||||||
apply_vat: body.apply_vat !== false,
|
body.po_number !== ""
|
||||||
exchange_rate:
|
? String(body.po_number)
|
||||||
body.exchange_rate != null ? Number(body.exchange_rate) : 1.0,
|
: null;
|
||||||
order_date: body.order_date
|
|
||||||
? new Date(String(body.order_date))
|
|
||||||
: new Date(),
|
|
||||||
delivery_date: body.delivery_date
|
|
||||||
? new Date(String(body.delivery_date))
|
|
||||||
: null,
|
|
||||||
language: body.language ? String(body.language) : "cs",
|
|
||||||
delivery_terms: body.delivery_terms
|
|
||||||
? String(body.delivery_terms)
|
|
||||||
: null,
|
|
||||||
payment_terms: body.payment_terms ? String(body.payment_terms) : null,
|
|
||||||
issued_by: body.issued_by ? String(body.issued_by) : null,
|
|
||||||
notes: body.notes ? String(body.notes) : null,
|
|
||||||
internal_notes: body.internal_notes
|
|
||||||
? String(body.internal_notes)
|
|
||||||
: null,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
if (Array.isArray(body.items)) {
|
// Re-check caller-supplied uniqueness INSIDE the transaction so the
|
||||||
await tx.issued_order_items.createMany({
|
// check-then-insert window can't race a concurrent create (mirrors
|
||||||
data: body.items.map((item, i) => ({
|
// createOffer). generateIssuedOrderNumber verifies its own numbers,
|
||||||
issued_order_id: order.id,
|
// so this guards only the explicit path.
|
||||||
description: item.description ?? null,
|
if (explicit !== null && (await isIssuedOrderNumberTaken(explicit, tx))) {
|
||||||
item_description: item.item_description ?? null,
|
return { error: "po_number_taken" as const };
|
||||||
quantity: item.quantity ?? 1,
|
}
|
||||||
unit: item.unit ?? null,
|
|
||||||
unit_price: item.unit_price ?? 0,
|
const poNumber =
|
||||||
vat_rate: item.vat_rate ?? 21.0,
|
explicit ??
|
||||||
position: item.position ?? i,
|
(status === "draft"
|
||||||
})),
|
? null
|
||||||
|
: (await generateIssuedOrderNumber(undefined, tx)).number);
|
||||||
|
|
||||||
|
const order = await tx.issued_orders.create({
|
||||||
|
data: {
|
||||||
|
po_number: poNumber,
|
||||||
|
supplier_id: supplierId,
|
||||||
|
status: status as $Enums.issued_orders_status,
|
||||||
|
currency: body.currency ? String(body.currency) : "CZK",
|
||||||
|
exchange_rate:
|
||||||
|
body.exchange_rate != null ? Number(body.exchange_rate) : 1.0,
|
||||||
|
// order_date is @db.Date (truncated to the UTC date part) — the
|
||||||
|
// "today" default must be utcMidnightOfLocalDay, or it stores
|
||||||
|
// yesterday during the 00:00–02:00 Prague window.
|
||||||
|
order_date: body.order_date
|
||||||
|
? new Date(String(body.order_date))
|
||||||
|
: utcMidnightOfLocalDay(),
|
||||||
|
delivery_date: body.delivery_date
|
||||||
|
? new Date(String(body.delivery_date))
|
||||||
|
: null,
|
||||||
|
language: body.language ? String(body.language) : "cs",
|
||||||
|
order_text: body.order_text ? String(body.order_text) : null,
|
||||||
|
internal_notes: body.internal_notes
|
||||||
|
? String(body.internal_notes)
|
||||||
|
: null,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
return order;
|
if (Array.isArray(body.items)) {
|
||||||
});
|
await tx.issued_order_items.createMany({
|
||||||
|
data: body.items.map((item, i) => ({
|
||||||
|
issued_order_id: order.id,
|
||||||
|
description: item.description ?? null,
|
||||||
|
item_description: item.item_description ?? null,
|
||||||
|
quantity: item.quantity ?? 1,
|
||||||
|
unit: item.unit ?? null,
|
||||||
|
unit_price: item.unit_price ?? 0,
|
||||||
|
position: item.position ?? i,
|
||||||
|
})),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Array.isArray(body.sections)) {
|
||||||
|
await tx.issued_order_sections.createMany({
|
||||||
|
data: body.sections.map((s, i) => ({
|
||||||
|
issued_order_id: order.id,
|
||||||
|
title: s.title ?? null,
|
||||||
|
title_cz: s.title_cz ?? null,
|
||||||
|
content: s.content ?? null,
|
||||||
|
position: s.position ?? i,
|
||||||
|
})),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return order;
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
// P2002 on the po_number unique index = a concurrent insert won the race
|
||||||
|
// between the in-tx check and our insert — surface the same clean
|
||||||
|
// conflict token instead of a 500 (mirrors offers).
|
||||||
|
if (
|
||||||
|
err instanceof Prisma.PrismaClientKnownRequestError &&
|
||||||
|
err.code === "P2002"
|
||||||
|
) {
|
||||||
|
return { error: "po_number_taken" as const };
|
||||||
|
}
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function updateIssuedOrder(id: number, body: IssuedOrderInput) {
|
export async function updateIssuedOrder(id: number, body: IssuedOrderInput) {
|
||||||
@@ -314,28 +377,37 @@ export async function updateIssuedOrder(id: number, body: IssuedOrderInput) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Editable-state guard: non-status fields may change only in draft/sent.
|
||||||
|
// In confirmed/completed/cancelled a field edit is REJECTED explicitly
|
||||||
|
// (offers parity — it used to be silently ignored). Status-only transition
|
||||||
|
// payloads (e.g. confirmed -> completed) still work.
|
||||||
const editable = currentStatus === "draft" || currentStatus === "sent";
|
const editable = currentStatus === "draft" || currentStatus === "sent";
|
||||||
|
if (
|
||||||
|
!editable &&
|
||||||
|
NON_STATUS_UPDATE_FIELDS.some((f) => body[f] !== undefined)
|
||||||
|
) {
|
||||||
|
return { error: "not_editable" as const, currentStatus };
|
||||||
|
}
|
||||||
|
|
||||||
const data: Record<string, unknown> = { modified_at: new Date() };
|
const data: Record<string, unknown> = { modified_at: new Date() };
|
||||||
|
|
||||||
if (editable) {
|
if (editable) {
|
||||||
const strFields = [
|
const strFields = ["currency", "language", "order_text"];
|
||||||
"currency",
|
|
||||||
"language",
|
|
||||||
"delivery_terms",
|
|
||||||
"payment_terms",
|
|
||||||
"issued_by",
|
|
||||||
];
|
|
||||||
for (const f of strFields) {
|
for (const f of strFields) {
|
||||||
if (body[f] !== undefined) data[f] = body[f] ? String(body[f]) : null;
|
if (body[f] !== undefined) data[f] = body[f] ? String(body[f]) : null;
|
||||||
}
|
}
|
||||||
if (body.customer_id !== undefined)
|
if (body.supplier_id !== undefined) {
|
||||||
data.customer_id = body.customer_id ? Number(body.customer_id) : null;
|
const supplierId = body.supplier_id ? Number(body.supplier_id) : null;
|
||||||
if (body.vat_rate !== undefined) data.vat_rate = Number(body.vat_rate);
|
if (supplierId) {
|
||||||
if (body.apply_vat !== undefined)
|
// Same dangling-FK guard as create: 400, not a P2003 500.
|
||||||
data.apply_vat =
|
const supplier = await prisma.sklad_suppliers.findUnique({
|
||||||
body.apply_vat === true ||
|
where: { id: supplierId },
|
||||||
body.apply_vat === 1 ||
|
select: { id: true },
|
||||||
body.apply_vat === "1";
|
});
|
||||||
|
if (!supplier) return { error: "supplier_not_found" as const };
|
||||||
|
}
|
||||||
|
data.supplier_id = supplierId;
|
||||||
|
}
|
||||||
if (body.exchange_rate !== undefined)
|
if (body.exchange_rate !== undefined)
|
||||||
data.exchange_rate =
|
data.exchange_rate =
|
||||||
body.exchange_rate != null ? Number(body.exchange_rate) : null;
|
body.exchange_rate != null ? Number(body.exchange_rate) : null;
|
||||||
@@ -347,8 +419,6 @@ export async function updateIssuedOrder(id: number, body: IssuedOrderInput) {
|
|||||||
data.delivery_date = body.delivery_date
|
data.delivery_date = body.delivery_date
|
||||||
? new Date(String(body.delivery_date))
|
? new Date(String(body.delivery_date))
|
||||||
: null;
|
: null;
|
||||||
if (body.notes !== undefined)
|
|
||||||
data.notes = body.notes ? String(body.notes) : null;
|
|
||||||
if (body.internal_notes !== undefined)
|
if (body.internal_notes !== undefined)
|
||||||
data.internal_notes = body.internal_notes
|
data.internal_notes = body.internal_notes
|
||||||
? String(body.internal_notes)
|
? String(body.internal_notes)
|
||||||
@@ -366,47 +436,67 @@ export async function updateIssuedOrder(id: number, body: IssuedOrderInput) {
|
|||||||
body.status !== undefined &&
|
body.status !== undefined &&
|
||||||
String(body.status) === "sent";
|
String(body.status) === "sent";
|
||||||
|
|
||||||
if (finalizing) {
|
// ONE transaction for the header write, the finalize numbering AND the
|
||||||
|
// items/sections full-replace. The items replace used to run in a SECOND
|
||||||
|
// transaction after the header tx — a failure between the two left a torn
|
||||||
|
// write (new header, old items). Now either everything lands or nothing.
|
||||||
|
const replaceItems = editable && Array.isArray(body.items);
|
||||||
|
const replaceSections = editable && Array.isArray(body.sections);
|
||||||
|
|
||||||
|
let assignedNumber: string | null = null;
|
||||||
|
if (finalizing || replaceItems || replaceSections) {
|
||||||
await prisma.$transaction(async (tx) => {
|
await prisma.$transaction(async (tx) => {
|
||||||
await tx.issued_orders.update({ where: { id }, data });
|
await tx.issued_orders.update({ where: { id }, data });
|
||||||
await assignIssuedOrderNumber(id, tx);
|
if (finalizing) assignedNumber = await assignIssuedOrderNumber(id, tx);
|
||||||
|
if (replaceItems) {
|
||||||
|
await tx.issued_order_items.deleteMany({
|
||||||
|
where: { issued_order_id: id },
|
||||||
|
});
|
||||||
|
await tx.issued_order_items.createMany({
|
||||||
|
data: body.items!.map((item, i) => ({
|
||||||
|
issued_order_id: id,
|
||||||
|
description: item.description ?? null,
|
||||||
|
item_description: item.item_description ?? null,
|
||||||
|
quantity: item.quantity ?? 1,
|
||||||
|
unit: item.unit ?? null,
|
||||||
|
unit_price: item.unit_price ?? 0,
|
||||||
|
position: item.position ?? i,
|
||||||
|
})),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (replaceSections) {
|
||||||
|
await tx.issued_order_sections.deleteMany({
|
||||||
|
where: { issued_order_id: id },
|
||||||
|
});
|
||||||
|
await tx.issued_order_sections.createMany({
|
||||||
|
data: body.sections!.map((s, i) => ({
|
||||||
|
issued_order_id: id,
|
||||||
|
title: s.title ?? null,
|
||||||
|
title_cz: s.title_cz ?? null,
|
||||||
|
content: s.content ?? null,
|
||||||
|
position: s.position ?? i,
|
||||||
|
})),
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
await prisma.issued_orders.update({ where: { id }, data });
|
await prisma.issued_orders.update({ where: { id }, data });
|
||||||
}
|
}
|
||||||
|
|
||||||
if (editable && Array.isArray(body.items)) {
|
// newValues for the audit diff: the header fields actually written plus the
|
||||||
await prisma.$transaction(async (tx) => {
|
// replaced collections (JSON.stringify drops the undefined = untouched keys).
|
||||||
await tx.issued_order_items.deleteMany({
|
const newValues: Record<string, unknown> = { ...data };
|
||||||
where: { issued_order_id: id },
|
if (replaceItems) newValues.items = body.items;
|
||||||
});
|
if (replaceSections) newValues.sections = body.sections;
|
||||||
await tx.issued_order_items.createMany({
|
|
||||||
data: body.items!.map((item, i) => ({
|
|
||||||
issued_order_id: id,
|
|
||||||
description: item.description ?? null,
|
|
||||||
item_description: item.item_description ?? null,
|
|
||||||
quantity: item.quantity ?? 1,
|
|
||||||
unit: item.unit ?? null,
|
|
||||||
unit_price: item.unit_price ?? 0,
|
|
||||||
vat_rate: item.vat_rate ?? 21.0,
|
|
||||||
position: item.position ?? i,
|
|
||||||
})),
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reflect the number the finalize transition just assigned (existing was read
|
// Reflect the number the finalize transition just assigned (existing was read
|
||||||
// before the assign, so its po_number is still the pre-finalize null).
|
// before the assign, so its po_number is still the pre-finalize null).
|
||||||
const poNumber = finalizing
|
return {
|
||||||
? ((
|
id,
|
||||||
await prisma.issued_orders.findUnique({
|
po_number: assignedNumber ?? existing.po_number,
|
||||||
where: { id },
|
oldValues: existing as unknown as Record<string, unknown>,
|
||||||
select: { po_number: true },
|
newValues,
|
||||||
})
|
};
|
||||||
)?.po_number ?? existing.po_number)
|
|
||||||
: existing.po_number;
|
|
||||||
|
|
||||||
return { id, po_number: poNumber };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function deleteIssuedOrder(id: number) {
|
export async function deleteIssuedOrder(id: number) {
|
||||||
@@ -420,6 +510,23 @@ export async function deleteIssuedOrder(id: number) {
|
|||||||
? new Date(existing.created_at).getFullYear()
|
? new Date(existing.created_at).getFullYear()
|
||||||
: new Date().getFullYear();
|
: new Date().getFullYear();
|
||||||
await releaseIssuedOrderNumber(year, existing.po_number ?? undefined);
|
await releaseIssuedOrderNumber(year, existing.po_number ?? undefined);
|
||||||
|
|
||||||
|
// Remove the archived PDF from the NAS so the DB delete doesn't orphan it
|
||||||
|
// (mirrors deleteOffer). cleanIssued sweeps every Vydané/YYYY/MM folder, so
|
||||||
|
// an order whose order_date moved after archiving is still found.
|
||||||
|
// Best-effort: log, never throw.
|
||||||
|
if (existing.po_number && nasOrdersManager.isConfigured()) {
|
||||||
|
try {
|
||||||
|
nasOrdersManager.cleanIssued(existing.po_number);
|
||||||
|
} catch (e) {
|
||||||
|
console.error(
|
||||||
|
"[issued-orders.service] NAS issued-order PDF delete failed for",
|
||||||
|
existing.po_number,
|
||||||
|
e,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return existing;
|
return existing;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -52,10 +52,20 @@ export class NasDocumentManager {
|
|||||||
|
|
||||||
// ── Created (issued) documents ───────────────────────────────────
|
// ── Created (issued) documents ───────────────────────────────────
|
||||||
|
|
||||||
/** Remove any existing PDF for this document number across all year/month folders */
|
/**
|
||||||
|
* Remove any existing PDF for this document number across all year/month
|
||||||
|
* folders — including stale `<number>_N.pdf` duplicates that the old
|
||||||
|
* uniquePath-based save left behind when two archive calls raced (the
|
||||||
|
* save-after-edit fire-and-forget vs. the /file fallback). Document
|
||||||
|
* numbers are digit-only and never contain "_", so the suffix match
|
||||||
|
* cannot hit a different document.
|
||||||
|
*/
|
||||||
cleanIssued(documentNumber: string): void {
|
cleanIssued(documentNumber: string): void {
|
||||||
if (!this.basePath) return;
|
if (!this.basePath) return;
|
||||||
const safeName = this.sanitizeFilename(documentNumber) + ".pdf";
|
const safeBase = this.sanitizeFilename(documentNumber);
|
||||||
|
const stalePattern = new RegExp(
|
||||||
|
`^${safeBase.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}(_\\d+)?\\.pdf$`,
|
||||||
|
);
|
||||||
const issuedDir = path.join(this.basePath, DIR_ISSUED);
|
const issuedDir = path.join(this.basePath, DIR_ISSUED);
|
||||||
try {
|
try {
|
||||||
if (!fs.existsSync(issuedDir)) return;
|
if (!fs.existsSync(issuedDir)) return;
|
||||||
@@ -74,9 +84,10 @@ export class NasDocumentManager {
|
|||||||
);
|
);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
const filePath = path.join(monthPath, safeName);
|
for (const file of fs.readdirSync(monthPath)) {
|
||||||
if (fs.existsSync(filePath)) {
|
if (stalePattern.test(file)) {
|
||||||
fs.unlinkSync(filePath);
|
fs.unlinkSync(path.join(monthPath, file));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -103,12 +114,17 @@ export class NasDocumentManager {
|
|||||||
const dir = this.ensureDir(DIR_ISSUED, year, month);
|
const dir = this.ensureDir(DIR_ISSUED, year, month);
|
||||||
if (!dir) return null;
|
if (!dir) return null;
|
||||||
|
|
||||||
|
// Deterministic path, OVERWRITE in place (same model as the offers
|
||||||
|
// manager): the archive of a numbered document has exactly one canonical
|
||||||
|
// file. uniquePath here used to spawn `<number>_1.pdf` duplicates
|
||||||
|
// whenever two archive calls raced — those are user-visible junk on the
|
||||||
|
// NAS and invisible to the reader/cleaner, which match the exact name.
|
||||||
const safeName = this.sanitizeFilename(documentNumber) + ".pdf";
|
const safeName = this.sanitizeFilename(documentNumber) + ".pdf";
|
||||||
const fullPath = this.uniquePath(dir, safeName);
|
const fullPath = path.join(dir, safeName);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
fs.writeFileSync(fullPath, pdfBuffer);
|
fs.writeFileSync(fullPath, pdfBuffer);
|
||||||
return `${DIR_ISSUED}/${year}/${this.pad(month)}/${path.basename(fullPath)}`;
|
return `${DIR_ISSUED}/${year}/${this.pad(month)}/${safeName}`;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(
|
console.error(
|
||||||
"[nas-financials-manager] write failed for issued document:",
|
"[nas-financials-manager] write failed for issued document:",
|
||||||
@@ -119,6 +135,54 @@ export class NasDocumentManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Locate and read the archived PDF for an issued document by its number
|
||||||
|
* alone, sweeping every Vydané/YYYY/MM folder (the same walk cleanIssued
|
||||||
|
* uses). Needed because the archive folder is derived from the document
|
||||||
|
* date at save time and the date can be edited afterwards — a fixed
|
||||||
|
* expected-path read would miss a file that does exist in another month
|
||||||
|
* folder. Returns null when not found (callers fall back to a live render).
|
||||||
|
*/
|
||||||
|
readIssuedByNumber(
|
||||||
|
documentNumber: string,
|
||||||
|
): { data: Buffer; fileName: string } | null {
|
||||||
|
if (!this.basePath) return null;
|
||||||
|
const safeName = this.sanitizeFilename(documentNumber) + ".pdf";
|
||||||
|
const issuedDir = path.join(this.basePath, DIR_ISSUED);
|
||||||
|
try {
|
||||||
|
if (!fs.existsSync(issuedDir)) return null;
|
||||||
|
for (const yearDir of fs.readdirSync(issuedDir)) {
|
||||||
|
const yearPath = path.join(issuedDir, yearDir);
|
||||||
|
if (!fs.statSync(yearPath).isDirectory()) continue;
|
||||||
|
for (const monthDir of fs.readdirSync(yearPath)) {
|
||||||
|
const monthPath = path.join(yearPath, monthDir);
|
||||||
|
try {
|
||||||
|
if (!fs.statSync(monthPath).isDirectory()) continue;
|
||||||
|
} catch (err) {
|
||||||
|
console.error(
|
||||||
|
"[nas-financials-manager] stat failed for monthPath:",
|
||||||
|
monthPath,
|
||||||
|
err,
|
||||||
|
);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const filePath = path.join(monthPath, safeName);
|
||||||
|
if (fs.existsSync(filePath)) {
|
||||||
|
return { data: fs.readFileSync(filePath), fileName: safeName };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
} catch (err) {
|
||||||
|
console.error(
|
||||||
|
"[nas-financials-manager] readIssuedByNumber failed for:",
|
||||||
|
documentNumber,
|
||||||
|
err,
|
||||||
|
);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
readIssued(relativePath: string): { data: Buffer; fileName: string } | null {
|
readIssued(relativePath: string): { data: Buffer; fileName: string } | null {
|
||||||
const fullPath = this.resolveSafePath(relativePath);
|
const fullPath = this.resolveSafePath(relativePath);
|
||||||
if (!fullPath) return null;
|
if (!fullPath) return null;
|
||||||
|
|||||||
@@ -228,18 +228,30 @@ async function isInvoiceNumberTaken(number: string): Promise<boolean> {
|
|||||||
return !!existing;
|
return !!existing;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Verify an issued-order (PO) number is not already used. */
|
/** Verify an issued-order (PO) number is not already used. Pass the tx client
|
||||||
async function isIssuedOrderNumberTaken(number: string): Promise<boolean> {
|
* when the check must be part of a transaction (otherwise it runs on a
|
||||||
const existing = await prisma.issued_orders.findFirst({
|
* separate connection and is only a best-effort pre-check). */
|
||||||
|
export async function isIssuedOrderNumberTaken(
|
||||||
|
number: string,
|
||||||
|
client: Prisma.TransactionClient | PrismaClient = prisma,
|
||||||
|
): Promise<boolean> {
|
||||||
|
const existing = await client.issued_orders.findFirst({
|
||||||
where: { po_number: number },
|
where: { po_number: number },
|
||||||
|
select: { id: true },
|
||||||
});
|
});
|
||||||
return !!existing;
|
return !!existing;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Verify an offer/quotation number is not already used. */
|
/** Verify an offer/quotation number is not already used. Pass the tx client
|
||||||
export async function isOfferNumberTaken(number: string): Promise<boolean> {
|
* when the check must be part of a transaction (otherwise it runs on a
|
||||||
const existing = await prisma.quotations.findFirst({
|
* separate connection and is only a best-effort pre-check). */
|
||||||
|
export async function isOfferNumberTaken(
|
||||||
|
number: string,
|
||||||
|
client: Prisma.TransactionClient | PrismaClient = prisma,
|
||||||
|
): Promise<boolean> {
|
||||||
|
const existing = await client.quotations.findFirst({
|
||||||
where: { quotation_number: number },
|
where: { quotation_number: number },
|
||||||
|
select: { id: true },
|
||||||
});
|
});
|
||||||
return !!existing;
|
return !!existing;
|
||||||
}
|
}
|
||||||
@@ -440,7 +452,18 @@ export async function previewInvoiceNumber(
|
|||||||
const code = settings?.invoice_type_code || "81";
|
const code = settings?.invoice_type_code || "81";
|
||||||
const year = _year || new Date().getFullYear();
|
const year = _year || new Date().getFullYear();
|
||||||
|
|
||||||
const seq = await previewNextSequence("invoice", year);
|
// Mirror generateInvoiceNumber's collision check (without consuming the
|
||||||
|
// sequence). The number_sequences counter can lag behind real data, so the
|
||||||
|
// raw next sequence may already be taken — advance past any number already
|
||||||
|
// used by an invoice so the preview matches what generateInvoiceNumber
|
||||||
|
// would actually assign instead of showing a stale, already-used number.
|
||||||
|
let seq = await previewNextSequence("invoice", year);
|
||||||
|
for (let attempt = 0; attempt < 1000; attempt++) {
|
||||||
|
const number = applyPattern(pattern, { year, prefix: "", code, seq });
|
||||||
|
if (!(await isInvoiceNumberTaken(number)))
|
||||||
|
return { number, next_number: number };
|
||||||
|
seq++;
|
||||||
|
}
|
||||||
const number = applyPattern(pattern, { year, prefix: "", code, seq });
|
const number = applyPattern(pattern, { year, prefix: "", code, seq });
|
||||||
return { number, next_number: number };
|
return { number, next_number: number };
|
||||||
}
|
}
|
||||||
@@ -482,7 +505,18 @@ export async function previewIssuedOrderNumber(
|
|||||||
const code = settings?.issued_order_type_code || "72";
|
const code = settings?.issued_order_type_code || "72";
|
||||||
const year = _year || new Date().getFullYear();
|
const year = _year || new Date().getFullYear();
|
||||||
|
|
||||||
const seq = await previewNextSequence("issued_order", year);
|
// Mirror generateIssuedOrderNumber's collision check (without consuming the
|
||||||
|
// sequence). The number_sequences counter can lag behind real data, so the
|
||||||
|
// raw next sequence may already be taken — advance past any number already
|
||||||
|
// used by an issued order so the preview matches what
|
||||||
|
// generateIssuedOrderNumber would actually assign.
|
||||||
|
let seq = await previewNextSequence("issued_order", year);
|
||||||
|
for (let attempt = 0; attempt < 1000; attempt++) {
|
||||||
|
const number = applyPattern(pattern, { year, prefix: "", code, seq });
|
||||||
|
if (!(await isIssuedOrderNumberTaken(number)))
|
||||||
|
return { number, next_number: number };
|
||||||
|
seq++;
|
||||||
|
}
|
||||||
const number = applyPattern(pattern, { year, prefix: "", code, seq });
|
const number = applyPattern(pattern, { year, prefix: "", code, seq });
|
||||||
return { number, next_number: number };
|
return { number, next_number: number };
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
|
import { Prisma } from "@prisma/client";
|
||||||
import prisma from "../config/database";
|
import prisma from "../config/database";
|
||||||
import {
|
import {
|
||||||
generateOfferNumber,
|
generateOfferNumber,
|
||||||
previewOfferNumber,
|
|
||||||
releaseOfferNumber,
|
releaseOfferNumber,
|
||||||
isOfferNumberTaken,
|
isOfferNumberTaken,
|
||||||
assignOfferNumber,
|
assignOfferNumber,
|
||||||
@@ -27,6 +27,44 @@ interface ScopeSectionInput {
|
|||||||
// Re-export for convenience
|
// Re-export for convenience
|
||||||
export { previewOfferNumber as getNextOfferNumber } from "./numbering.service";
|
export { previewOfferNumber as getNextOfferNumber } from "./numbering.service";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Offer status lifecycle (mirrors issued orders' table): a draft must be
|
||||||
|
* finalized to `active` first (that's when the number is assigned), an active
|
||||||
|
* offer can be ordered or invalidated, an ordered one only invalidated, and
|
||||||
|
* `invalidated` is terminal. The create-order flow (orders.service
|
||||||
|
* createOrderFromQuotation) consults this table too — an offer can only become
|
||||||
|
* `ordered` from a status that allows the transition.
|
||||||
|
*/
|
||||||
|
export const VALID_TRANSITIONS: Record<string, string[]> = {
|
||||||
|
draft: ["active"],
|
||||||
|
active: ["ordered", "invalidated"],
|
||||||
|
ordered: ["invalidated"],
|
||||||
|
invalidated: [],
|
||||||
|
};
|
||||||
|
|
||||||
|
/** Fields (non-status) are editable only while the offer is draft/active. */
|
||||||
|
function isEditableStatus(status: string): boolean {
|
||||||
|
return status === "draft" || status === "active";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Non-status update fields — used by the editable-state guard so a field edit
|
||||||
|
* on an ordered/invalidated offer is rejected EXPLICITLY (not silently
|
||||||
|
* dropped). `quotation_number` is stripped by the schema and is not listed.
|
||||||
|
*/
|
||||||
|
const NON_STATUS_UPDATE_FIELDS = [
|
||||||
|
"project_code",
|
||||||
|
"customer_id",
|
||||||
|
"created_at",
|
||||||
|
"valid_until",
|
||||||
|
"currency",
|
||||||
|
"language",
|
||||||
|
"scope_title",
|
||||||
|
"scope_description",
|
||||||
|
"items",
|
||||||
|
"sections",
|
||||||
|
] as const;
|
||||||
|
|
||||||
const ALLOWED_SORT_FIELDS = [
|
const ALLOWED_SORT_FIELDS = [
|
||||||
"id",
|
"id",
|
||||||
"quotation_number",
|
"quotation_number",
|
||||||
@@ -77,28 +115,22 @@ function buildOfferWhere(params: OfferFilterParams): Record<string, unknown> {
|
|||||||
return where;
|
return where;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Offers are NOT tax documents — totals are NET only (no VAT anywhere).
|
||||||
function enrichQuotation(q: any) {
|
function enrichQuotation(q: any) {
|
||||||
const subtotal = q.quotation_items
|
const total = q.quotation_items
|
||||||
.filter((i: any) => i.is_included_in_total !== false)
|
.filter((i: any) => i.is_included_in_total !== false)
|
||||||
.reduce(
|
.reduce(
|
||||||
(s: number, i: any) =>
|
(s: number, i: any) =>
|
||||||
s + (Number(i.quantity) || 0) * (Number(i.unit_price) || 0),
|
s + (Number(i.quantity) || 0) * (Number(i.unit_price) || 0),
|
||||||
0,
|
0,
|
||||||
);
|
);
|
||||||
const vatAmount = q.apply_vat
|
|
||||||
? subtotal *
|
|
||||||
((q.vat_rate != null && q.vat_rate !== "" ? Number(q.vat_rate) : 21) /
|
|
||||||
100)
|
|
||||||
: 0;
|
|
||||||
const { quotation_items, scope_sections, ...rest } = q;
|
const { quotation_items, scope_sections, ...rest } = q;
|
||||||
return {
|
return {
|
||||||
...rest,
|
...rest,
|
||||||
items: quotation_items,
|
items: quotation_items,
|
||||||
sections: scope_sections,
|
sections: scope_sections,
|
||||||
customer_name: q.customers?.name || null,
|
customer_name: q.customers?.name || null,
|
||||||
subtotal: Math.round(subtotal * 100) / 100,
|
total: Math.round(total * 100) / 100,
|
||||||
vat_amount: Math.round(vatAmount * 100) / 100,
|
|
||||||
total: Math.round((subtotal + vatAmount) * 100) / 100,
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -108,16 +140,24 @@ export async function listOffers(params: ListOffersParams) {
|
|||||||
|
|
||||||
const where = buildOfferWhere(params);
|
const where = buildOfferWhere(params);
|
||||||
|
|
||||||
|
// Tiebreak on id: created_at is second-precision, so same-second rows would
|
||||||
|
// otherwise paginate non-deterministically (project ordering rule).
|
||||||
|
const orderBy: Record<string, "asc" | "desc">[] = [
|
||||||
|
{ [sortField]: order },
|
||||||
|
{ id: order },
|
||||||
|
];
|
||||||
|
|
||||||
const [quotations, total] = await Promise.all([
|
const [quotations, total] = await Promise.all([
|
||||||
prisma.quotations.findMany({
|
prisma.quotations.findMany({
|
||||||
where,
|
where,
|
||||||
skip,
|
skip,
|
||||||
take: limit,
|
take: limit,
|
||||||
orderBy: { [sortField]: order },
|
orderBy,
|
||||||
include: {
|
include: {
|
||||||
customers: { select: { id: true, name: true } },
|
customers: { select: { id: true, name: true } },
|
||||||
quotation_items: { orderBy: { position: "asc" } },
|
quotation_items: { orderBy: { position: "asc" } },
|
||||||
scope_sections: { orderBy: { position: "asc" } },
|
// id tiebreak: position can repeat, keep the order deterministic.
|
||||||
|
scope_sections: { orderBy: [{ position: "asc" }, { id: "asc" }] },
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
prisma.quotations.count({ where }),
|
prisma.quotations.count({ where }),
|
||||||
@@ -148,7 +188,7 @@ export async function listOffers(params: ListOffersParams) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sum offer TOTAL (incl. VAT) per currency across the WHOLE filtered set (not a
|
* Sum offer NET total per currency across the WHOLE filtered set (not a
|
||||||
* single page). Reuses `buildOfferWhere` so the filters track the list, and
|
* single page). Reuses `buildOfferWhere` so the filters track the list, and
|
||||||
* `enrichQuotation` so the per-offer math matches the list/detail exactly.
|
* `enrichQuotation` so the per-offer math matches the list/detail exactly.
|
||||||
* Returns one entry per currency, rounded to 2dp, zero/empty totals dropped.
|
* Returns one entry per currency, rounded to 2dp, zero/empty totals dropped.
|
||||||
@@ -158,12 +198,12 @@ export async function getOfferTotals(
|
|||||||
): Promise<{ totals: CurrencyAmount[] }> {
|
): Promise<{ totals: CurrencyAmount[] }> {
|
||||||
const where = buildOfferWhere(params);
|
const where = buildOfferWhere(params);
|
||||||
|
|
||||||
|
// Only quotation_items — the per-currency NET math needs nothing else
|
||||||
|
// (customers/scope_sections would just inflate the full-set scan).
|
||||||
const quotations = await prisma.quotations.findMany({
|
const quotations = await prisma.quotations.findMany({
|
||||||
where,
|
where,
|
||||||
include: {
|
include: {
|
||||||
customers: { select: { id: true, name: true } },
|
quotation_items: true,
|
||||||
quotation_items: { orderBy: { position: "asc" } },
|
|
||||||
scope_sections: { orderBy: { position: "asc" } },
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -190,7 +230,8 @@ export async function getOffer(id: number) {
|
|||||||
include: {
|
include: {
|
||||||
customers: true,
|
customers: true,
|
||||||
quotation_items: { orderBy: { position: "asc" } },
|
quotation_items: { orderBy: { position: "asc" } },
|
||||||
scope_sections: { orderBy: { position: "asc" } },
|
// id tiebreak: position can repeat, keep the order deterministic.
|
||||||
|
scope_sections: { orderBy: [{ position: "asc" }, { id: "asc" }] },
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
if (!quotation) return null;
|
if (!quotation) return null;
|
||||||
@@ -213,6 +254,9 @@ export async function getOffer(id: number) {
|
|||||||
customer: quotation.customers,
|
customer: quotation.customers,
|
||||||
customer_name: quotation.customers?.name || null,
|
customer_name: quotation.customers?.name || null,
|
||||||
order: orderInfo,
|
order: orderInfo,
|
||||||
|
// Computed server-side so the frontend renders only legal status buttons
|
||||||
|
// (same contract as getIssuedOrder).
|
||||||
|
valid_transitions: VALID_TRANSITIONS[quotation.status] || [],
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -225,6 +269,18 @@ export async function createOffer(body: Record<string, any>) {
|
|||||||
? String(body.quotation_number)
|
? String(body.quotation_number)
|
||||||
: null;
|
: null;
|
||||||
|
|
||||||
|
// Validate the referenced customer exists BEFORE the insert — a dangling
|
||||||
|
// FK would otherwise surface as a P2003 500 instead of a clean 400
|
||||||
|
// (mirrors createIssuedOrder's supplier check).
|
||||||
|
const customerId = body.customer_id ? Number(body.customer_id) : null;
|
||||||
|
if (customerId) {
|
||||||
|
const customer = await tx.customers.findUnique({
|
||||||
|
where: { id: customerId },
|
||||||
|
select: { id: true },
|
||||||
|
});
|
||||||
|
if (!customer) return { error: "customer_not_found" as const };
|
||||||
|
}
|
||||||
|
|
||||||
// Deferred numbering: a draft carries NO quotation_number (the column is
|
// Deferred numbering: a draft carries NO quotation_number (the column is
|
||||||
// nullable-unique so many drafts coexist). The official number is consumed
|
// nullable-unique so many drafts coexist). The official number is consumed
|
||||||
// only when the draft is finalized (assignOfferNumber on draft->active).
|
// only when the draft is finalized (assignOfferNumber on draft->active).
|
||||||
@@ -238,19 +294,15 @@ export async function createOffer(body: Record<string, any>) {
|
|||||||
// generateOfferNumber already verifies its own generated numbers, so this
|
// generateOfferNumber already verifies its own generated numbers, so this
|
||||||
// guards the caller-supplied path.
|
// guards the caller-supplied path.
|
||||||
if (explicitNumber !== null) {
|
if (explicitNumber !== null) {
|
||||||
const taken = await isOfferNumberTaken(explicitNumber);
|
const taken = await isOfferNumberTaken(explicitNumber, tx);
|
||||||
if (taken) {
|
if (taken) return { error: "number_taken" as const };
|
||||||
throw Object.assign(new Error("Číslo nabídky je již použito"), {
|
|
||||||
status: 400,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const quotation = await tx.quotations.create({
|
const quotation = await tx.quotations.create({
|
||||||
data: {
|
data: {
|
||||||
quotation_number: quotationNumber,
|
quotation_number: quotationNumber,
|
||||||
project_code: body.project_code ? String(body.project_code) : null,
|
project_code: body.project_code ? String(body.project_code) : null,
|
||||||
customer_id: body.customer_id ? Number(body.customer_id) : null,
|
customer_id: customerId,
|
||||||
created_at: body.created_at
|
created_at: body.created_at
|
||||||
? new Date(String(body.created_at))
|
? new Date(String(body.created_at))
|
||||||
: undefined,
|
: undefined,
|
||||||
@@ -259,8 +311,6 @@ export async function createOffer(body: Record<string, any>) {
|
|||||||
: null,
|
: null,
|
||||||
currency: body.currency ? String(body.currency) : "CZK",
|
currency: body.currency ? String(body.currency) : "CZK",
|
||||||
language: body.language ? String(body.language) : "cs",
|
language: body.language ? String(body.language) : "cs",
|
||||||
vat_rate: body.vat_rate != null ? Number(body.vat_rate) : 21.0,
|
|
||||||
apply_vat: body.apply_vat !== false,
|
|
||||||
status,
|
status,
|
||||||
scope_title: body.scope_title ? String(body.scope_title) : null,
|
scope_title: body.scope_title ? String(body.scope_title) : null,
|
||||||
scope_description: body.scope_description
|
scope_description: body.scope_description
|
||||||
@@ -299,11 +349,14 @@ export async function createOffer(body: Record<string, any>) {
|
|||||||
return quotation;
|
return quotation;
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err instanceof Error && "status" in err) {
|
// P2002 on the quotation_number unique index = a concurrent insert won the
|
||||||
return {
|
// race between the in-tx check and our insert — surface the same clean
|
||||||
error: err.message,
|
// conflict token instead of a 500 (mirrors createIssuedOrder).
|
||||||
status: (err as Error & { status: number }).status,
|
if (
|
||||||
} as const;
|
err instanceof Prisma.PrismaClientKnownRequestError &&
|
||||||
|
err.code === "P2002"
|
||||||
|
) {
|
||||||
|
return { error: "number_taken" as const };
|
||||||
}
|
}
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
@@ -312,31 +365,53 @@ export async function createOffer(body: Record<string, any>) {
|
|||||||
export async function updateOffer(id: number, body: Record<string, any>) {
|
export async function updateOffer(id: number, body: Record<string, any>) {
|
||||||
const existing = await prisma.quotations.findUnique({ where: { id } });
|
const existing = await prisma.quotations.findUnique({ where: { id } });
|
||||||
if (!existing) return { error: "not_found" as const };
|
if (!existing) return { error: "not_found" as const };
|
||||||
if (existing.status === "invalidated")
|
|
||||||
return { error: "invalidated" as const };
|
|
||||||
|
|
||||||
// A finalized offer's number is immutable. A draft has no number yet
|
const currentStatus = existing.status;
|
||||||
// (quotation_number === null) — it is assigned on finalize, not editable here,
|
|
||||||
// so an empty/absent submitted value on a draft is not a "change" and must not
|
// Status changes must follow the transition table (issued-orders parity).
|
||||||
// block the draft->active finalize.
|
if (body.status !== undefined && String(body.status) !== currentStatus) {
|
||||||
|
const newStatus = String(body.status);
|
||||||
|
const allowed = VALID_TRANSITIONS[currentStatus] || [];
|
||||||
|
if (!allowed.includes(newStatus)) {
|
||||||
|
return { error: "invalid_transition" as const, currentStatus, newStatus };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Editable-state guard: non-status fields may change only in draft/active.
|
||||||
|
// In ordered/invalidated a field edit is REJECTED explicitly (it used to be
|
||||||
|
// possible to rewrite everything in any status except invalidated).
|
||||||
|
// Status-only transition payloads (e.g. ordered -> invalidated) still work.
|
||||||
|
const editable = isEditableStatus(currentStatus);
|
||||||
if (
|
if (
|
||||||
existing.quotation_number !== null &&
|
!editable &&
|
||||||
body.quotation_number !== undefined &&
|
NON_STATUS_UPDATE_FIELDS.some((f) => body[f] !== undefined)
|
||||||
String(body.quotation_number) !== existing.quotation_number
|
|
||||||
) {
|
) {
|
||||||
return { error: "Číslo nabídky nelze změnit", status: 400 } as const;
|
return { error: "not_editable" as const, currentStatus };
|
||||||
|
}
|
||||||
|
|
||||||
|
// Explicit null CLEARS the customer; a set id must exist (dangling FK would
|
||||||
|
// surface as a P2003 500). The old `Number(null)` coerced null to 0.
|
||||||
|
let customerId: number | null | undefined = undefined;
|
||||||
|
if (body.customer_id !== undefined) {
|
||||||
|
customerId = body.customer_id ? Number(body.customer_id) : null;
|
||||||
|
if (customerId) {
|
||||||
|
const customer = await prisma.customers.findUnique({
|
||||||
|
where: { id: customerId },
|
||||||
|
select: { id: true },
|
||||||
|
});
|
||||||
|
if (!customer) return { error: "customer_not_found" as const };
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Finalize transition draft -> active: assign the official number ATOMICALLY
|
// Finalize transition draft -> active: assign the official number ATOMICALLY
|
||||||
// with the status change. assignOfferNumber is idempotent.
|
// with the status change. assignOfferNumber is idempotent.
|
||||||
const finalizing =
|
const finalizing =
|
||||||
existing.status === "draft" &&
|
currentStatus === "draft" &&
|
||||||
body.status !== undefined &&
|
body.status !== undefined &&
|
||||||
String(body.status) === "active";
|
String(body.status) === "active";
|
||||||
|
|
||||||
const data = {
|
const data = {
|
||||||
customer_id:
|
customer_id: customerId,
|
||||||
body.customer_id !== undefined ? Number(body.customer_id) : undefined,
|
|
||||||
created_at:
|
created_at:
|
||||||
body.created_at !== undefined
|
body.created_at !== undefined
|
||||||
? body.created_at
|
? body.created_at
|
||||||
@@ -351,13 +426,6 @@ export async function updateOffer(id: number, body: Record<string, any>) {
|
|||||||
: undefined,
|
: undefined,
|
||||||
currency: body.currency !== undefined ? String(body.currency) : undefined,
|
currency: body.currency !== undefined ? String(body.currency) : undefined,
|
||||||
language: body.language !== undefined ? String(body.language) : undefined,
|
language: body.language !== undefined ? String(body.language) : undefined,
|
||||||
vat_rate: body.vat_rate !== undefined ? Number(body.vat_rate) : undefined,
|
|
||||||
apply_vat:
|
|
||||||
body.apply_vat !== undefined
|
|
||||||
? body.apply_vat === true ||
|
|
||||||
body.apply_vat === 1 ||
|
|
||||||
body.apply_vat === "1"
|
|
||||||
: undefined,
|
|
||||||
status: body.status !== undefined ? String(body.status) : undefined,
|
status: body.status !== undefined ? String(body.status) : undefined,
|
||||||
project_code:
|
project_code:
|
||||||
body.project_code !== undefined
|
body.project_code !== undefined
|
||||||
@@ -417,19 +485,39 @@ export async function updateOffer(id: number, body: Record<string, any>) {
|
|||||||
await prisma.quotations.update({ where: { id }, data });
|
await prisma.quotations.update({ where: { id }, data });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// newValues for the audit diff: the header fields actually written plus the
|
||||||
|
// replaced collections (JSON.stringify drops the undefined = untouched keys).
|
||||||
|
const newValues: Record<string, unknown> = { ...data };
|
||||||
|
if (Array.isArray(body.items)) newValues.items = body.items;
|
||||||
|
if (Array.isArray(body.sections)) newValues.sections = body.sections;
|
||||||
|
|
||||||
// Reflect the number the finalize transition just assigned (existing was read
|
// Reflect the number the finalize transition just assigned (existing was read
|
||||||
// before the assign, so its quotation_number is still the pre-finalize null).
|
// before the assign, so its quotation_number is still the pre-finalize null).
|
||||||
return {
|
return {
|
||||||
id,
|
id,
|
||||||
quotation_number: assignedNumber ?? existing.quotation_number,
|
quotation_number: assignedNumber ?? existing.quotation_number,
|
||||||
|
oldValues: existing as unknown as Record<string, unknown>,
|
||||||
|
newValues,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function deleteOffer(id: number) {
|
export async function deleteOffer(id: number) {
|
||||||
const existing = await prisma.quotations.findUnique({ where: { id } });
|
const existing = await prisma.quotations.findUnique({ where: { id } });
|
||||||
if (!existing) return null;
|
if (!existing) return { error: "not_found" as const };
|
||||||
|
|
||||||
await prisma.quotations.delete({ where: { id } });
|
try {
|
||||||
|
await prisma.quotations.delete({ where: { id } });
|
||||||
|
} catch (err) {
|
||||||
|
// P2003 = the offer is referenced by an order/project via a Restrict FK —
|
||||||
|
// surface a clean conflict token instead of a 500.
|
||||||
|
if (
|
||||||
|
err instanceof Prisma.PrismaClientKnownRequestError &&
|
||||||
|
err.code === "P2003"
|
||||||
|
) {
|
||||||
|
return { error: "linked" as const };
|
||||||
|
}
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
|
||||||
const year = existing.created_at
|
const year = existing.created_at
|
||||||
? new Date(existing.created_at).getFullYear()
|
? new Date(existing.created_at).getFullYear()
|
||||||
@@ -437,6 +525,8 @@ export async function deleteOffer(id: number) {
|
|||||||
await releaseOfferNumber(year, existing.quotation_number ?? undefined);
|
await releaseOfferNumber(year, existing.quotation_number ?? undefined);
|
||||||
|
|
||||||
// Remove the offer PDF from the NAS so the DB delete doesn't orphan it.
|
// Remove the offer PDF from the NAS so the DB delete doesn't orphan it.
|
||||||
|
// The service is the SINGLE owner of this cleanup (the route used to repeat
|
||||||
|
// it and always logged a spurious second-delete failure).
|
||||||
// Best-effort and idempotent: deleteOfferPdf returns false (and logs) if the
|
// Best-effort and idempotent: deleteOfferPdf returns false (and logs) if the
|
||||||
// file is already gone, so a NAS outage / missing file never throws here.
|
// file is already gone, so a NAS outage / missing file never throws here.
|
||||||
if (existing.quotation_number && nasOffersManager.isConfigured()) {
|
if (existing.quotation_number && nasOffersManager.isConfigured()) {
|
||||||
@@ -455,7 +545,7 @@ export async function deleteOffer(id: number) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return existing;
|
return { data: existing };
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function duplicateOffer(id: number) {
|
export async function duplicateOffer(id: number) {
|
||||||
@@ -463,7 +553,8 @@ export async function duplicateOffer(id: number) {
|
|||||||
where: { id },
|
where: { id },
|
||||||
include: {
|
include: {
|
||||||
quotation_items: { orderBy: { position: "asc" } },
|
quotation_items: { orderBy: { position: "asc" } },
|
||||||
scope_sections: { orderBy: { position: "asc" } },
|
// id tiebreak: position can repeat, keep the order deterministic.
|
||||||
|
scope_sections: { orderBy: [{ position: "asc" }, { id: "asc" }] },
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
if (!original) return null;
|
if (!original) return null;
|
||||||
@@ -479,8 +570,6 @@ export async function duplicateOffer(id: number) {
|
|||||||
valid_until: null,
|
valid_until: null,
|
||||||
currency: original.currency,
|
currency: original.currency,
|
||||||
language: original.language,
|
language: original.language,
|
||||||
vat_rate: original.vat_rate,
|
|
||||||
apply_vat: original.apply_vat,
|
|
||||||
status: "active",
|
status: "active",
|
||||||
scope_title: original.scope_title,
|
scope_title: original.scope_title,
|
||||||
scope_description: original.scope_description,
|
scope_description: original.scope_description,
|
||||||
@@ -520,11 +609,23 @@ export async function duplicateOffer(id: number) {
|
|||||||
|
|
||||||
export async function invalidateOffer(id: number) {
|
export async function invalidateOffer(id: number) {
|
||||||
const existing = await prisma.quotations.findUnique({ where: { id } });
|
const existing = await prisma.quotations.findUnique({ where: { id } });
|
||||||
if (!existing) return null;
|
if (!existing) return { error: "not_found" as const };
|
||||||
|
|
||||||
|
// The dedicated endpoint must respect the same transition table as PUT:
|
||||||
|
// only active/ordered offers can be invalidated (a draft is deleted instead,
|
||||||
|
// invalidated is terminal).
|
||||||
|
const allowed = VALID_TRANSITIONS[existing.status] || [];
|
||||||
|
if (!allowed.includes("invalidated")) {
|
||||||
|
return {
|
||||||
|
error: "invalid_transition" as const,
|
||||||
|
currentStatus: existing.status,
|
||||||
|
newStatus: "invalidated",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
await prisma.quotations.update({
|
await prisma.quotations.update({
|
||||||
where: { id },
|
where: { id },
|
||||||
data: { status: "invalidated", modified_at: new Date() },
|
data: { status: "invalidated", modified_at: new Date() },
|
||||||
});
|
});
|
||||||
return existing;
|
return { data: existing };
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import {
|
|||||||
releaseProjectNumber,
|
releaseProjectNumber,
|
||||||
} from "./numbering.service";
|
} from "./numbering.service";
|
||||||
import { NasFileManager } from "./nas-file-manager";
|
import { NasFileManager } from "./nas-file-manager";
|
||||||
|
import { VALID_TRANSITIONS as OFFER_VALID_TRANSITIONS } from "./offers.service";
|
||||||
|
|
||||||
// Best-effort NAS folder creation for order-spawned projects, mirroring
|
// Best-effort NAS folder creation for order-spawned projects, mirroring
|
||||||
// projects.service. Stateless singleton; reads NAS_PATH at construction.
|
// projects.service. Stateless singleton; reads NAS_PATH at construction.
|
||||||
@@ -72,23 +73,20 @@ async function syncProjectStatus(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// ⚠ Also called by getOrderTotals with a MINIMAL select (currency, apply_vat,
|
// ⚠ Also called by getOrderTotals with a MINIMAL select (currency, item
|
||||||
// vat_rate, item quantity/unit_price/is_included_in_total). If you read a NEW
|
// quantity/unit_price/is_included_in_total). If you read a NEW order/item
|
||||||
// order/item field here, add it to that select — a field missing from the
|
// field here, add it to that select — a field missing from the select is
|
||||||
// select is silently 0 in the per-currency totals.
|
// silently 0 in the per-currency totals.
|
||||||
|
//
|
||||||
|
// Orders are NOT tax documents — totals are NET only (no VAT anywhere).
|
||||||
function enrichOrder(o: any) {
|
function enrichOrder(o: any) {
|
||||||
const subtotal = o.order_items
|
const total = o.order_items
|
||||||
.filter((i: any) => i.is_included_in_total !== false)
|
.filter((i: any) => i.is_included_in_total !== false)
|
||||||
.reduce(
|
.reduce(
|
||||||
(s: number, i: any) =>
|
(s: number, i: any) =>
|
||||||
s + (Number(i.quantity) || 0) * (Number(i.unit_price) || 0),
|
s + (Number(i.quantity) || 0) * (Number(i.unit_price) || 0),
|
||||||
0,
|
0,
|
||||||
);
|
);
|
||||||
const vatAmount = o.apply_vat
|
|
||||||
? subtotal *
|
|
||||||
((o.vat_rate != null && o.vat_rate !== "" ? Number(o.vat_rate) : 21) /
|
|
||||||
100)
|
|
||||||
: 0;
|
|
||||||
const { order_items, order_sections, ...rest } = o;
|
const { order_items, order_sections, ...rest } = o;
|
||||||
const invoice = o.invoices?.[0] || null;
|
const invoice = o.invoices?.[0] || null;
|
||||||
return {
|
return {
|
||||||
@@ -100,9 +98,7 @@ function enrichOrder(o: any) {
|
|||||||
project_code: o.quotations?.project_code || null,
|
project_code: o.quotations?.project_code || null,
|
||||||
invoice_id: invoice?.id || null,
|
invoice_id: invoice?.id || null,
|
||||||
invoice_number: invoice?.invoice_number || null,
|
invoice_number: invoice?.invoice_number || null,
|
||||||
subtotal: Math.round(subtotal * 100) / 100,
|
total: Math.round(total * 100) / 100,
|
||||||
vat_amount: Math.round(vatAmount * 100) / 100,
|
|
||||||
total: Math.round((subtotal + vatAmount) * 100) / 100,
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -192,7 +188,7 @@ export interface CurrencyAmount {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sum order TOTAL (incl. VAT) per currency across the WHOLE filtered set (not a
|
* Sum order NET total per currency across the WHOLE filtered set (not a
|
||||||
* single page). Reuses `buildOrderWhere` so the filters track the list, and
|
* single page). Reuses `buildOrderWhere` so the filters track the list, and
|
||||||
* `enrichOrder` so the per-order math matches the list/detail exactly. Returns
|
* `enrichOrder` so the per-order math matches the list/detail exactly. Returns
|
||||||
* one entry per currency, rounded to 2dp, zero/empty totals dropped.
|
* one entry per currency, rounded to 2dp, zero/empty totals dropped.
|
||||||
@@ -202,15 +198,13 @@ export async function getOrderTotals(
|
|||||||
): Promise<{ totals: CurrencyAmount[] }> {
|
): Promise<{ totals: CurrencyAmount[] }> {
|
||||||
const where = buildOrderWhere(params);
|
const where = buildOrderWhere(params);
|
||||||
|
|
||||||
// Select ONLY what the math needs (currency + VAT flags + item numbers).
|
// Select ONLY what the math needs (currency + item numbers).
|
||||||
// This runs over the WHOLE filtered set, so pulling attachment blobs,
|
// This runs over the WHOLE filtered set, so pulling attachment blobs,
|
||||||
// sections HTML or relations here would multiply the query size for nothing.
|
// sections HTML or relations here would multiply the query size for nothing.
|
||||||
const orders = await prisma.orders.findMany({
|
const orders = await prisma.orders.findMany({
|
||||||
where,
|
where,
|
||||||
select: {
|
select: {
|
||||||
currency: true,
|
currency: true,
|
||||||
apply_vat: true,
|
|
||||||
vat_rate: true,
|
|
||||||
order_items: {
|
order_items: {
|
||||||
select: {
|
select: {
|
||||||
quantity: true,
|
quantity: true,
|
||||||
@@ -308,7 +302,7 @@ export async function createOrderFromQuotation(
|
|||||||
where: { id: quotationId },
|
where: { id: quotationId },
|
||||||
include: {
|
include: {
|
||||||
quotation_items: { orderBy: { position: "asc" } },
|
quotation_items: { orderBy: { position: "asc" } },
|
||||||
scope_sections: { orderBy: { position: "asc" } },
|
scope_sections: { orderBy: [{ position: "asc" }, { id: "asc" }] },
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -319,6 +313,19 @@ export async function createOrderFromQuotation(
|
|||||||
status: 400,
|
status: 400,
|
||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
|
// The flow sets the offer's status to `ordered`, so it must respect the
|
||||||
|
// offers transition table: only an offer whose current status allows the
|
||||||
|
// `ordered` transition qualifies. A draft must be finalized first (it has no
|
||||||
|
// number yet — an ordered offer without a number must never exist) and an
|
||||||
|
// invalidated offer is terminal.
|
||||||
|
const allowedFromStatus = OFFER_VALID_TRANSITIONS[quotation.status] || [];
|
||||||
|
if (!allowedFromStatus.includes("ordered")) {
|
||||||
|
return {
|
||||||
|
error: `Objednávku nelze vytvořit z nabídky ve stavu "${quotation.status}"`,
|
||||||
|
status: 400,
|
||||||
|
} as const;
|
||||||
|
}
|
||||||
|
|
||||||
const result = await prisma.$transaction(async (tx) => {
|
const result = await prisma.$transaction(async (tx) => {
|
||||||
const orderNumber = await generateSharedNumber(tx);
|
const orderNumber = await generateSharedNumber(tx);
|
||||||
// The order keeps the shared (order) sequence; the auto-created project
|
// The order keeps the shared (order) sequence; the auto-created project
|
||||||
@@ -335,8 +342,6 @@ export async function createOrderFromQuotation(
|
|||||||
status: "prijata",
|
status: "prijata",
|
||||||
currency: quotation.currency || "CZK",
|
currency: quotation.currency || "CZK",
|
||||||
language: quotation.language || "cs",
|
language: quotation.language || "cs",
|
||||||
vat_rate: quotation.vat_rate ?? 21.0,
|
|
||||||
apply_vat: quotation.apply_vat ?? true,
|
|
||||||
scope_title: quotation.scope_title,
|
scope_title: quotation.scope_title,
|
||||||
scope_description: quotation.scope_description,
|
scope_description: quotation.scope_description,
|
||||||
attachment_data: attachmentBuffer
|
attachment_data: attachmentBuffer
|
||||||
@@ -428,8 +433,6 @@ interface CreateOrderData {
|
|||||||
status: string;
|
status: string;
|
||||||
currency: string;
|
currency: string;
|
||||||
language: string;
|
language: string;
|
||||||
vat_rate: number;
|
|
||||||
apply_vat?: boolean;
|
|
||||||
exchange_rate?: number;
|
exchange_rate?: number;
|
||||||
scope_title?: string | null;
|
scope_title?: string | null;
|
||||||
scope_description?: string | null;
|
scope_description?: string | null;
|
||||||
@@ -469,8 +472,6 @@ export async function createOrder(
|
|||||||
status: body.status,
|
status: body.status,
|
||||||
currency: body.currency,
|
currency: body.currency,
|
||||||
language: body.language,
|
language: body.language,
|
||||||
vat_rate: body.vat_rate,
|
|
||||||
apply_vat: body.apply_vat !== false,
|
|
||||||
exchange_rate: body.exchange_rate,
|
exchange_rate: body.exchange_rate,
|
||||||
scope_title: body.scope_title ?? null,
|
scope_title: body.scope_title ?? null,
|
||||||
scope_description: body.scope_description ?? null,
|
scope_description: body.scope_description ?? null,
|
||||||
@@ -629,10 +630,6 @@ export async function updateOrder(id: number, body: UpdateOrderData) {
|
|||||||
}
|
}
|
||||||
if (body.customer_id !== undefined)
|
if (body.customer_id !== undefined)
|
||||||
data.customer_id = body.customer_id ? Number(body.customer_id) : null;
|
data.customer_id = body.customer_id ? Number(body.customer_id) : null;
|
||||||
if (body.vat_rate !== undefined) data.vat_rate = Number(body.vat_rate);
|
|
||||||
if (body.apply_vat !== undefined)
|
|
||||||
data.apply_vat =
|
|
||||||
body.apply_vat === true || body.apply_vat === 1 || body.apply_vat === "1";
|
|
||||||
|
|
||||||
if (Array.isArray(body.items) || Array.isArray(body.sections)) {
|
if (Array.isArray(body.items) || Array.isArray(body.sections)) {
|
||||||
if (currentStatus !== "prijata" && currentStatus !== "v_realizaci") {
|
if (currentStatus !== "prijata" && currentStatus !== "v_realizaci") {
|
||||||
@@ -764,7 +761,16 @@ export async function deleteOrder(id: number, deleteFiles = false) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clear quotation back-reference (matching PHP)
|
// Clear the quotation back-reference AND revert its status: an
|
||||||
|
// `ordered` offer with no order is a dead end (the transition table
|
||||||
|
// only allows ordered -> invalidated), so it goes back to `active`,
|
||||||
|
// making it orderable again.
|
||||||
|
await tx.quotations.updateMany({
|
||||||
|
where: { order_id: id, status: "ordered" },
|
||||||
|
data: { order_id: null, status: "active", modified_at: new Date() },
|
||||||
|
});
|
||||||
|
// Backstop for rows in any other status (shouldn't exist — only an
|
||||||
|
// ordered offer can hold an order_id).
|
||||||
await tx.quotations.updateMany({
|
await tx.quotations.updateMany({
|
||||||
where: { order_id: id },
|
where: { order_id: id },
|
||||||
data: { order_id: null },
|
data: { order_id: null },
|
||||||
|
|||||||
@@ -8,6 +8,22 @@
|
|||||||
* of JSON serialization (e.g., building lookup keys, shift_date strings).
|
* of JSON serialization (e.g., building lookup keys, shift_date strings).
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* UTC midnight of the LOCAL calendar day of `d` (default: now).
|
||||||
|
*
|
||||||
|
* Prisma truncates a JS Date used against a `@db.Date` column (filter or
|
||||||
|
* write) to its **UTC** date part. With TZ=Europe/Prague, a local-midnight
|
||||||
|
* Date — `new Date(y, m, d)` — is 22:00/23:00 UTC of the PREVIOUS day, so it
|
||||||
|
* filters/stores as the previous calendar date; and a bare `new Date()`
|
||||||
|
* written to a `@db.Date` column stores yesterday during the 00:00–02:00
|
||||||
|
* Prague window. Always build `@db.Date` values and range boundaries from
|
||||||
|
* `Date.UTC(...)` of the LOCAL calendar components — this helper does exactly
|
||||||
|
* that for "today" (or any reference Date).
|
||||||
|
*/
|
||||||
|
export function utcMidnightOfLocalDay(d: Date = new Date()): Date {
|
||||||
|
return new Date(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate()));
|
||||||
|
}
|
||||||
|
|
||||||
/** YYYY-MM-DD in local time */
|
/** YYYY-MM-DD in local time */
|
||||||
export function localDateStr(d: Date): string {
|
export function localDateStr(d: Date): string {
|
||||||
const y = d.getFullYear();
|
const y = d.getFullYear();
|
||||||
|
|||||||
@@ -63,7 +63,32 @@ async function getBrowser(): Promise<Browser> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function htmlToPdf(html: string): Promise<Buffer> {
|
export interface HtmlToPdfOptions {
|
||||||
|
/**
|
||||||
|
* Chromium footerTemplate HTML rendered in the bottom margin of EVERY page
|
||||||
|
* — the only true repeating footer Puppeteer supports (CSS @page margin
|
||||||
|
* boxes are not implemented in Chromium). Styles must be inline and the
|
||||||
|
* font-size explicit (Chromium defaults it to 0); `.pageNumber` /
|
||||||
|
* `.totalPages` spans are substituted by Chromium. When set, the bottom
|
||||||
|
* margin grows to make room and an empty headerTemplate suppresses
|
||||||
|
* Chromium's default date/title header.
|
||||||
|
*/
|
||||||
|
footerTemplate?: string;
|
||||||
|
/**
|
||||||
|
* Chromium headerTemplate HTML rendered in the top margin of EVERY page
|
||||||
|
* (same rules as footerTemplate: inline styles, explicit font-size; images
|
||||||
|
* must be data: URLs). When set, the top margin grows to 32mm to make room
|
||||||
|
* — the document body must NOT render its own header then, or page 1 shows
|
||||||
|
* it twice. NOTE: Chromium lays pages out by the document's CSS @page
|
||||||
|
* margins when present (they override this option) — keep them in sync.
|
||||||
|
*/
|
||||||
|
headerTemplate?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function htmlToPdf(
|
||||||
|
html: string,
|
||||||
|
options: HtmlToPdfOptions = {},
|
||||||
|
): Promise<Buffer> {
|
||||||
const b = await getBrowser();
|
const b = await getBrowser();
|
||||||
const page = await b.newPage();
|
const page = await b.newPage();
|
||||||
// Per-request timeouts so one stuck render (e.g. image tag pointing at a
|
// Per-request timeouts so one stuck render (e.g. image tag pointing at a
|
||||||
@@ -78,7 +103,19 @@ export async function htmlToPdf(html: string): Promise<Buffer> {
|
|||||||
const pdf = await page.pdf({
|
const pdf = await page.pdf({
|
||||||
format: "A4",
|
format: "A4",
|
||||||
printBackground: true,
|
printBackground: true,
|
||||||
margin: { top: "10mm", bottom: "10mm", left: "10mm", right: "10mm" },
|
margin: {
|
||||||
|
top: options.headerTemplate ? "32mm" : "10mm",
|
||||||
|
bottom: options.footerTemplate ? "18mm" : "10mm",
|
||||||
|
left: "10mm",
|
||||||
|
right: "10mm",
|
||||||
|
},
|
||||||
|
...(options.footerTemplate || options.headerTemplate
|
||||||
|
? {
|
||||||
|
displayHeaderFooter: true,
|
||||||
|
headerTemplate: options.headerTemplate || "<span></span>",
|
||||||
|
footerTemplate: options.footerTemplate || "<span></span>",
|
||||||
|
}
|
||||||
|
: {}),
|
||||||
timeout: 15_000,
|
timeout: 15_000,
|
||||||
});
|
});
|
||||||
return Buffer.from(pdf);
|
return Buffer.from(pdf);
|
||||||
|
|||||||
157
src/utils/pdf-shared.ts
Normal file
157
src/utils/pdf-shared.ts
Normal file
@@ -0,0 +1,157 @@
|
|||||||
|
import { localDateCzStr } from "./date";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shared helpers for the four PDF routes (offers-pdf, issued-orders-pdf,
|
||||||
|
* invoices-pdf, orders-pdf). These used to be near-verbatim copies in each
|
||||||
|
* route file; the canonical variants live here:
|
||||||
|
*
|
||||||
|
* - `cleanQuillHtml` is the STRICT variant (strips javascript:/data:/vbscript:
|
||||||
|
* in BOTH href and src — defense-in-depth, DOMPurify runs first).
|
||||||
|
* - `formatNum` uses the NBSP (U+00A0) thousands separator so amounts never
|
||||||
|
* line-wrap inside the number.
|
||||||
|
* - `formatCurrency` is the per-currency-symbol variant from offers
|
||||||
|
* (€ / $ / Kč / £, anything else falls back to "<num> <code>").
|
||||||
|
*/
|
||||||
|
|
||||||
|
/** dd.MM.yyyy in Europe/Prague; invalid input echoes back as-is. */
|
||||||
|
export function formatDate(date: Date | string | null | undefined): string {
|
||||||
|
if (!date) return "";
|
||||||
|
const d = new Date(date);
|
||||||
|
if (isNaN(d.getTime())) return String(date);
|
||||||
|
return localDateCzStr(d);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Format number with comma decimal separator and NBSP thousands separator. */
|
||||||
|
export function formatNum(n: number, decimals = 2): string {
|
||||||
|
const abs = Math.abs(n);
|
||||||
|
const fixed = abs.toFixed(decimals);
|
||||||
|
const [intPart, decPart] = fixed.split(".");
|
||||||
|
const withSep = intPart.replace(/\B(?=(\d{3})+(?!\d))/g, " ");
|
||||||
|
const result = decPart ? `${withSep},${decPart}` : withSep;
|
||||||
|
return n < 0 ? `-${result}` : result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Per-currency symbol formatting (offers variant). */
|
||||||
|
export function formatCurrency(amount: number, currency: string): string {
|
||||||
|
const n = Number(amount) || 0;
|
||||||
|
switch (currency) {
|
||||||
|
case "EUR":
|
||||||
|
return `${formatNum(n, 2)} €`;
|
||||||
|
case "USD":
|
||||||
|
return `$${Math.abs(n)
|
||||||
|
.toFixed(2)
|
||||||
|
.replace(/\B(?=(\d{3})+(?!\d))/g, ",")}`;
|
||||||
|
case "CZK":
|
||||||
|
return `${formatNum(n, 2)} Kč`;
|
||||||
|
case "GBP":
|
||||||
|
return `£${Math.abs(n)
|
||||||
|
.toFixed(2)
|
||||||
|
.replace(/\B(?=(\d{3})+(?!\d))/g, ",")}`;
|
||||||
|
default:
|
||||||
|
return `${formatNum(n, 2)} ${currency}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function escapeHtml(str: string | null | undefined): string {
|
||||||
|
if (!str) return "";
|
||||||
|
return str
|
||||||
|
.replace(/&/g, "&")
|
||||||
|
.replace(/</g, "<")
|
||||||
|
.replace(/>/g, ">")
|
||||||
|
.replace(/"/g, """);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Company logo from company_settings.logo_data as a data: URI (mime sniffed
|
||||||
|
* from magic bytes). Data URIs are the ONLY image source Chromium loads in
|
||||||
|
* header/footer templates; the body templates reuse the same URI.
|
||||||
|
*/
|
||||||
|
export function logoDataUriFromSettings(
|
||||||
|
settings: Record<string, unknown> | null,
|
||||||
|
): string {
|
||||||
|
if (!settings?.logo_data) return "";
|
||||||
|
const buf = Buffer.from(settings.logo_data as Buffer);
|
||||||
|
let mime = "image/png";
|
||||||
|
if (buf[0] === 0xff && buf[1] === 0xd8) mime = "image/jpeg";
|
||||||
|
else if (buf[0] === 0x47 && buf[1] === 0x49) mime = "image/gif";
|
||||||
|
else if (buf[0] === 0x52 && buf[1] === 0x49) mime = "image/webp";
|
||||||
|
return `data:${escapeHtml(mime)};base64,${buf.toString("base64")}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unified repeating per-page header for the red-accent PDF family (invoices +
|
||||||
|
* issued orders), rendered by Puppeteer in the top page margin: company logo
|
||||||
|
* left (max 22mm tall — same size as the documents' original body header),
|
||||||
|
* red bold heading right, 2pt #de3a3a rule underneath.
|
||||||
|
*
|
||||||
|
* Contract shared with the footers: inline styles only, explicit font-size
|
||||||
|
* (Chromium defaults template text to 0), images as data: URIs only, padding
|
||||||
|
* matched to the documents' 12mm @page side margins. The @page top margin
|
||||||
|
* must be 32mm to make room (htmlToPdf grows it when headerTemplate is set,
|
||||||
|
* but Chromium lays out by the CSS @page margins — keep them in sync).
|
||||||
|
*/
|
||||||
|
export function buildPdfHeaderTemplate(
|
||||||
|
logoDataUri: string,
|
||||||
|
heading: string,
|
||||||
|
): string {
|
||||||
|
return `<div style="width:100%; font-family:Tahoma, sans-serif; padding:0 12mm; box-sizing:border-box;">
|
||||||
|
<div style="display:flex; justify-content:space-between; align-items:center; padding-bottom:1mm; border-bottom:2pt solid #de3a3a;">
|
||||||
|
<div style="flex:0 0 auto;">${logoDataUri ? `<img src="${logoDataUri}" style="max-width:42mm; max-height:22mm; object-fit:contain;" />` : ""}</div>
|
||||||
|
<div style="font-size:13pt; font-weight:700; color:#de3a3a; text-align:right; letter-spacing:0.03em;">${escapeHtml(heading)}</div>
|
||||||
|
</div>
|
||||||
|
</div>`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sanitize Quill HTML for the PDF templates: remove dangerous tags, event
|
||||||
|
* handlers, javascript:/data:/vbscript: URLs (href AND src), inline styles,
|
||||||
|
* and merge adjacent identical spans. Runs AFTER DOMPurify at every call site
|
||||||
|
* (regex pass = defense-in-depth, not the primary sanitizer).
|
||||||
|
*/
|
||||||
|
export function cleanQuillHtml(html: string | null | undefined): string {
|
||||||
|
if (!html) return "";
|
||||||
|
let s = html;
|
||||||
|
// Remove dangerous tags with content
|
||||||
|
s = s.replace(
|
||||||
|
/<(script|iframe|object|embed|style|link|meta|base|form|input|textarea|button|select|svg|math)[^>]*>[\s\S]*?<\/\1>/gi,
|
||||||
|
"",
|
||||||
|
);
|
||||||
|
s = s.replace(
|
||||||
|
/<(script|iframe|object|embed|style|link|meta|base|form|input|textarea|button|select|svg|math)[^>]*\/?>/gi,
|
||||||
|
"",
|
||||||
|
);
|
||||||
|
// Strip event handlers
|
||||||
|
s = s.replace(/\s+on\w+\s*=\s*("[^"]*"|'[^']*'|[^\s>]*)/gi, "");
|
||||||
|
s = s.replace(/\s+on\w+\s*=\s*[^\s>]*/gi, "");
|
||||||
|
// Strip javascript:/data:/vbscript: in href and src
|
||||||
|
s = s.replace(
|
||||||
|
/href\s*=\s*["']?\s*(javascript|data|vbscript)\s*:[^"'>\s]*/gi,
|
||||||
|
'href="#"',
|
||||||
|
);
|
||||||
|
s = s.replace(
|
||||||
|
/src\s*=\s*["']?\s*(javascript|data|vbscript)\s*:[^"'>\s]*/gi,
|
||||||
|
'src=""',
|
||||||
|
);
|
||||||
|
// Replace with regular space (outside of tags)
|
||||||
|
s = s.replace(/( )/g, " ");
|
||||||
|
s = s.replace(/\s+style\s*=\s*("[^"]*"|'[^']*'|[^\s>]*)/gi, "");
|
||||||
|
// Merge adjacent spans with same attributes
|
||||||
|
let prev = "";
|
||||||
|
while (prev !== s) {
|
||||||
|
prev = s;
|
||||||
|
s = s.replace(/<span([^>]*)>(.*?)<\/span>\s*<span\1>/gs, "<span$1>$2");
|
||||||
|
}
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** CSS rules for Quill's .ql-indent-1..9 classes (identical in all templates). */
|
||||||
|
export function buildQuillIndentCss(): string {
|
||||||
|
let indentCSS = "";
|
||||||
|
for (let n = 1; n <= 9; n++) {
|
||||||
|
const pad = n * 3;
|
||||||
|
const liPad = n * 3 + 1.5;
|
||||||
|
indentCSS += ` .ql-indent-${n} { padding-left: ${pad}em; }\n`;
|
||||||
|
indentCSS += ` li.ql-indent-${n} { padding-left: ${liPad}em; }\n`;
|
||||||
|
}
|
||||||
|
return indentCSS;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user