iobroker.e-invoices 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -70,9 +70,15 @@ network.
70
70
  Open `http://<iobroker-host>:8093/` for the invoice dashboard, the offers tab
71
71
  ("Angebote"), the multi-step wizard (seller → buyer → lines → review), invoice
72
72
  details with validation, attachments and downloads, layout templates with PDF
73
- preview, backups and a status page. The header bar carries the tabs only — the
74
- "+ Neu" buttons sit where their lists are, and the status page names the app
75
- next to the version and the schema.
73
+ preview and the **System** page. The header bar carries the tabs only — the
74
+ "+ Neu" buttons sit where their lists are.
75
+
76
+ The **System** page holds what is rarely needed, behind a row of tabs inside the page, one
77
+ topic at a time: **Status** (app name, version, schema, counters, and the appearance — light,
78
+ dark or follow the device), **Backup** (backup and restore), **Firma** (the company data,
79
+ filled in once) and **Druckvorlagen** (print templates). It replaces the former header tabs
80
+ "Backup", "Firma", "Druckvorlagen" and "Status"; their old addresses `#/backup`, `#/company`,
81
+ `#/templates` and `#/status` are the tabs of the page, so old bookmarks keep working.
76
82
 
77
83
  ### Print templates: fine-tuning the header
78
84
 
@@ -345,9 +351,18 @@ duty to **issue** them for B2B grows in stages. An e-invoice is a structured XML
345
351
 
346
352
  - An **issued invoice is immutable**. A mistake is corrected with a **Storno** (credit note) and a
347
353
  new invoice — never by editing. The number circle stays gap-free per year and employee code.
348
- - Every issued invoice records its **earliest deletion date** (`retain_until`, ten years from the
349
- end of the issue year: § 147 AO, § 14b UStG). Only drafts can be deleted, never an issued
350
- invoice.
354
+ - Every issued invoice records its **earliest deletion date** (`retain_until`): the end of the **tenth** year after
355
+ the issue year. That is a deliberately cautious choice, not the statutory minimum: for invoices the retention
356
+ period is **eight years** since 2025 (§ 147 AO and § 14b UStG as amended by the Fourth Bureaucracy Relief Act,
357
+ BEG IV), while other records (for example business letters or the annual accounts) can need more. Ask your tax
358
+ advisor which period applies to you — the adapter never deletes anything of its own accord. Only drafts can be
359
+ deleted, never an issued invoice.
360
+ - **Issuing is checked before the number is taken.** The documents are made once with a placeholder number first; a
361
+ defect of the layout, the attachments or the XML stops there, with the draft untouched and no number used. Should a
362
+ document still end up numbered **without its files** (the disk was full or not writable), the dashboard shows it
363
+ under "Issued but without file" and *Rebuild files* makes exactly the missing ones from the stored data — the XML
364
+ from the record, the PDF with the layout frozen at issue, the Excel copy. A file that exists is never touched, and
365
+ the invoice itself does not change.
351
366
  - Re-rendering a PDF (for example after a layout change) **never overwrites** the delivered file:
352
367
  the original is archived as `<number>.orig-<n>.pdf`, the reason is logged, and the XML stays
353
368
  unchanged. The layout an invoice was issued with is frozen with it and can be re-used.
@@ -379,7 +394,13 @@ duty to **issue** them for B2B grows in stages. An e-invoice is a structured XML
379
394
  logged as a warning on startup — use a token in that case.
380
395
  - **Rate limits:** 600 requests per minute and client on `/api`, but only 10 on
381
396
  `/api/restore*`, which replaces the whole database. Exceeding a budget answers
382
- `429 Too many requests` and logs method plus path (never the token).
397
+ `429 Too many requests` and logs method plus path (never the token). **Failed
398
+ sign-ins have a budget of their own:** twenty wrong tokens per minute and client (asking without any token does not count),
399
+ then `429` — even for the right token until the minute is over — so guessing the token
400
+ is slow and a stranger cannot use up your general budget by asking without a token.
401
+ - **What the open health route says:** `/api/health` (no token) names only status, version,
402
+ schema version and the web app language. The number of documents per status sits behind
403
+ the token (`/api/status`).
383
404
  - **Browser hardening:** API and PWA answer with a self-only Content Security
384
405
  Policy (`default-src 'self'`, `frame-ancestors 'none'`), `X-Content-Type-Options:
385
406
  nosniff`, `Referrer-Policy: no-referrer` and without `X-Powered-By`.
@@ -393,6 +414,9 @@ duty to **issue** them for B2B grows in stages. An e-invoice is a structured XML
393
414
  - **Restore limits:** a backup ZIP may be at most 512 MB and expand to at most
394
415
  1 GB (`dump.json` at most 256 MB); a crafted archive is rejected before it is
395
416
  unpacked. Entry paths, per-file checksums and the manifest are verified.
417
+ The same limits apply when a backup is **created**: a backup that the restore would refuse is not written, the
418
+ error names the limit that is exceeded (attachments travel as base64 in `dump.json`, about a third more than the
419
+ files) — better no backup than one that only looks like protection.
396
420
  - **Where the PWA keeps the token:** in the browser profile's `localStorage`, so a
397
421
  reload does not ask again. It is only ever sent as an `Authorization` header to
398
422
  the adapter (never in a URL), but a script running on the same origin could read
@@ -475,6 +499,18 @@ validation and hybrid embedding, `pdfkit`, `exceljs`, `jszip`,
475
499
  Placeholder for the next version (at the beginning of the line):
476
500
  ### **WORK IN PROGRESS**
477
501
  -->
502
+ ### 1.0.2 (2026-10-04)
503
+
504
+ * (alex) System page: the topics are tabs inside the page (*Status*, *Backup*, *Firma*, *Druckvorlagen*) and only the chosen one is shown, instead of one long page with everything below each other.
505
+
506
+ ### 1.0.1 (2026-10-04)
507
+
508
+ * (alex) External validation of the generated e-invoices is automated: `npm run validate` makes the sample cases over the real API stack and checks every XML with the KoSIT validator (EN 16931, CII; tools downloaded once and pinned by SHA-256); the GitHub workflow `Validate invoices` runs it monthly and on changes of the XML/PDF code, and checks the PDF files with veraPDF (PDF/A-3b). It is not part of the release workflow.
509
+ * (alex) Hardening: wrong tokens are throttled separately (twenty per minute and client; asking without a token is not counted); the open health route no longer reports document counts (they moved to the authenticated `/api/status`). Issuing a document dated in an earlier year asks first, because the number follows the year of the date and the numbers are then not in time order (the log says so as well). The retention note now names the statutory minimum for invoices (eight years since 2025) next to the ten years the adapter keeps to be on the safe side. Dependabot no longer merges patches of the libraries that write the invoice (better-sqlite3, pdfkit, pdf-lib, jszip, express, factur-x) by itself.
510
+ * (alex) Backup: a backup that the restore would refuse (ZIP, `dump.json`, unpacked size or number of files above the restore limits) is no longer created without notice — it is refused with a message that names the limit. Attachments count as base64 in `dump.json`.
511
+ * (alex) New **System** page: status, backup and restore, company data, print templates and appearance on one page, so the header bar loses the tabs "Backup", "Firma", "Druckvorlagen" and "Status" (twelve entries became eight; the old addresses still work).
512
+ * (alex) Issuing is checked before the number is taken: the documents are made once with a placeholder number first, so a defect stops there with the draft untouched. A document that is still left without its files shows up on the dashboard and *Rebuild files* makes only the missing ones from the stored data (the XML from the record, the PDF with the layout frozen at issue, the Excel copy); existing files are never touched.
513
+
478
514
  ### 1.0.0 (2026-10-04)
479
515
 
480
516
  * (alex) First stable release: ZUGFeRD / EN 16931 e-invoices with offers, Storno, attachments, print templates, several companies, open items, dunning, exports with a date range, automatic backup and a German/English web app in a light or dark appearance. No functional change compared with 0.9.5; the changelog was shortened (older entries are in `CHANGELOG_OLD.md`).
@@ -487,14 +523,6 @@ validation and hybrid embedding, `pdfkit`, `exceljs`, `jszip`,
487
523
 
488
524
  * (alex) Invoice list: the period (quick choice, *Von*, *Bis*) sits in the same filter row as the other filters, with the labels above the two days.
489
525
 
490
- ### 0.9.3 (2026-10-04)
491
-
492
- * (alex) Date range for the invoice list and the exports: quick choices (this/last month, quarter, year) or free *Von* / *Bis* days on the invoice date, both days included. Excel, CSV and DATEV use the same filter as the list and name the range in the file name (`rechnungen_2026-08-01_2026-08-31.csv`). `from` / `to` on the list and export routes. Fixed on the way: an export was cut at 500 invoices — it now contains everything that matches, in date order, and never a draft (a draft has no number to book).
493
-
494
- ### 0.9.2 (2026-10-04)
495
-
496
- * (alex) Backup: the automatic backup is now **on by default** (daily) and keeps the newest 7 automatic backups (new setting *Automatic backups to keep*); only automatic backups are ever deleted, never those made by hand or the safety copies before a restore. The schedule counts from the last automatic backup, so a restart neither skips nor repeats one. The backup page and the new state `info.backupWarning` warn when there is no backup or it is old. Reason: an ioBroker backup (BackItUp) does not contain the invoice database `invoices.db` — it reaches the backup only through the adapter ZIP in `e-invoices.0.storage/backups/`. README compares BackItUp and the adapter backup. Instances installed earlier keep their saved interval (0 = off), set it in the instance settings.
497
-
498
526
  Older changes: see [CHANGELOG_OLD.md](CHANGELOG_OLD.md).
499
527
 
500
528
  ## License
@@ -271,7 +271,7 @@ function secretEquals(provided, expected) {
271
271
  return (0, import_node_crypto.timingSafeEqual)(a, b);
272
272
  }
273
273
  function createApiServer(deps) {
274
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t;
274
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v;
275
275
  const { db, storage, log, version, authToken } = deps;
276
276
  const settings = {
277
277
  defaultVatRate: import_invoice_model.ALLOWED_VAT_RATES.includes(Number((_a = deps.settings) == null ? void 0 : _a.defaultVatRate)) ? Number((_b = deps.settings) == null ? void 0 : _b.defaultVatRate) : 19,
@@ -288,7 +288,10 @@ function createApiServer(deps) {
288
288
  };
289
289
  const limits = {
290
290
  api: Math.max(1, Math.round((_r = (_q = deps.limits) == null ? void 0 : _q.api) != null ? _r : 600)),
291
- restore: Math.max(1, Math.round((_t = (_s = deps.limits) == null ? void 0 : _s.restore) != null ? _t : 10))
291
+ restore: Math.max(1, Math.round((_t = (_s = deps.limits) == null ? void 0 : _s.restore) != null ? _t : 10)),
292
+ // N1: wrong tokens per minute and client — far below the general budget, so guessing the token is slow;
293
+ // twenty leaves room for the few requests a web app with a stale token fires at once
294
+ authFail: Math.max(1, Math.round((_v = (_u = deps.limits) == null ? void 0 : _u.authFail) != null ? _v : 20))
292
295
  };
293
296
  const app = (0, import_express.default)();
294
297
  app.disable("x-powered-by");
@@ -371,16 +374,39 @@ function createApiServer(deps) {
371
374
  next();
372
375
  });
373
376
  }
377
+ const authFailures = /* @__PURE__ */ new Map();
374
378
  if (authToken) {
375
379
  app.use("/api", (req, res, next) => {
380
+ var _a2, _b2, _c2;
376
381
  if (req.path === "/health") {
377
382
  next();
378
383
  return;
379
384
  }
380
- if (req.headers.authorization && secretEquals(req.headers.authorization, `Bearer ${authToken}`)) {
385
+ const sent = typeof req.headers.authorization === "string" && req.headers.authorization !== "";
386
+ const now = Date.now();
387
+ const client = (_a2 = req.ip) != null ? _a2 : "unknown";
388
+ const recent = ((_b2 = authFailures.get(client)) != null ? _b2 : []).filter((at) => now - at < 6e4);
389
+ if (sent && recent.length >= limits.authFail) {
390
+ authFailures.set(client, recent);
391
+ (_c2 = log.warn) == null ? void 0 : _c2.call(log, `Too many failed sign-ins: ${req.method} ${req.path}`);
392
+ res.status(429).json({ error: "Too many failed attempts" });
393
+ return;
394
+ }
395
+ if (sent && secretEquals(req.headers.authorization, `Bearer ${authToken}`)) {
381
396
  next();
382
397
  return;
383
398
  }
399
+ if (sent) {
400
+ recent.push(now);
401
+ authFailures.set(client, recent);
402
+ if (authFailures.size > 1e3) {
403
+ for (const [key, stamps] of authFailures) {
404
+ if (stamps.every((at) => now - at >= 6e4)) {
405
+ authFailures.delete(key);
406
+ }
407
+ }
408
+ }
409
+ }
384
410
  res.status(401).json({ error: "Unauthorized" });
385
411
  });
386
412
  }
@@ -399,10 +425,12 @@ function createApiServer(deps) {
399
425
  status: "ok",
400
426
  version,
401
427
  schemaVersion: db.currentVersion(),
402
- counts: db.countByStatus(),
403
428
  pwaLanguage: settings.pwaLanguage
404
429
  });
405
430
  });
431
+ app.get("/api/status", (_req, res) => {
432
+ res.json({ counts: db.countByStatus() });
433
+ });
406
434
  app.get("/api/settings", (_req, res) => {
407
435
  res.json(settings);
408
436
  });
@@ -508,6 +536,21 @@ ${(0, import_csv.renderDatevRows)(filteredInvoices(db, req.query, "invoice", ran
508
536
  }
509
537
  );
510
538
  });
539
+ app.get(
540
+ "/api/invoices/incomplete",
541
+ route((_req, res) => {
542
+ res.json(
543
+ db.listIncompleteDocuments().map((invoice) => ({
544
+ id: invoice.id,
545
+ number: invoice.number,
546
+ issueDate: invoice.issueDate,
547
+ customer: invoice.buyer.name,
548
+ docType: invoice.docType,
549
+ missing: (0, import_issue_service.missingArtifacts)(invoice)
550
+ }))
551
+ );
552
+ })
553
+ );
511
554
  const revenueReport = (query) => {
512
555
  let year;
513
556
  if (typeof query.year === "string" && query.year !== "") {
@@ -1148,6 +1191,17 @@ ${(0, import_csv.renderDatevRows)(filteredInvoices(db, req.query, "invoice", ran
1148
1191
  }
1149
1192
  })
1150
1193
  );
1194
+ app.post(
1195
+ "/api/invoices/:id/repair",
1196
+ route(async (req, res) => {
1197
+ try {
1198
+ const result = await (0, import_issue_service.repairMissingArtifacts)(db, log, routeParam(req, "id"), storage);
1199
+ res.json({ invoice: result.invoice, created: result.created });
1200
+ } catch (error) {
1201
+ res.status(isMissingError(error) ? 404 : 400).json({ error: error.message });
1202
+ }
1203
+ })
1204
+ );
1151
1205
  app.post(
1152
1206
  "/api/invoices/:id/rerender",
1153
1207
  route(async (req, res) => {