diff --git a/Changes.md b/Changes.md index a4e3153..060f028 100644 --- a/Changes.md +++ b/Changes.md @@ -2,6 +2,14 @@ --- +## 2026-04-27 – Feature: Neues Rechnungsnummernformat (PPPYYYYMM-nn) + +### Geändert +- `src/Models/Invoice.php`: `generateRechnungsNr()` erzeugt jetzt das Format `PPPYYYYMM-nn` (Prefix + Jahr + Monat + zweistellige Sequenz, die bei Bedarf wächst); Zähler wird monatlich zurückgesetzt +- `migrations/038_invoice_nr_month.sql`: Neue Spalte `invoice_nr_month` in `company_settings` + +--- + ## 2026-04-26 – Fix: Eigenanteil-Buchung als Einnahme statt Ausgabe ### Geändert diff --git a/migrations/038_invoice_nr_month.sql b/migrations/038_invoice_nr_month.sql new file mode 100644 index 0000000..b1be4ab --- /dev/null +++ b/migrations/038_invoice_nr_month.sql @@ -0,0 +1,2 @@ +ALTER TABLE company_settings + ADD COLUMN IF NOT EXISTS invoice_nr_month TINYINT UNSIGNED NOT NULL DEFAULT 0; diff --git a/src/Models/Invoice.php b/src/Models/Invoice.php index 2660e53..8fbbc20 100644 --- a/src/Models/Invoice.php +++ b/src/Models/Invoice.php @@ -262,24 +262,26 @@ class Invoice public static function generateRechnungsNr(): string { - $settings = Database::fetchOne( - 'SELECT invoice_nr_prefix, invoice_nr_next, invoice_nr_year FROM company_settings WHERE id=1' + $settings = Database::fetchOne( + 'SELECT invoice_nr_prefix, invoice_nr_next, invoice_nr_year, invoice_nr_month FROM company_settings WHERE id=1' ); - $prefix = $settings['invoice_nr_prefix'] ?? 'AR'; - $next = (int)($settings['invoice_nr_next'] ?? 1); - $savedYear = (int)($settings['invoice_nr_year'] ?? 0); - $year = (int)date('Y'); + $prefix = $settings['invoice_nr_prefix'] ?? 'AR'; + $next = (int)($settings['invoice_nr_next'] ?? 1); + $savedYear = (int)($settings['invoice_nr_year'] ?? 0); + $savedMonth = (int)($settings['invoice_nr_month'] ?? 0); + $year = (int)date('Y'); + $month = (int)date('n'); - // Reset counter on new year - if ($savedYear !== $year) { + if ($savedYear !== $year || $savedMonth !== $month) { $next = 1; } - $nr = sprintf('%s-%s-%04d', $prefix, $year, $next); + $digits = max(2, strlen((string)$next)); + $nr = sprintf('%s%04d%02d-%0' . $digits . 'd', $prefix, $year, $month, $next); Database::execute( - 'UPDATE company_settings SET invoice_nr_next=?, invoice_nr_year=? WHERE id=1', - [$next + 1, $year] + 'UPDATE company_settings SET invoice_nr_next=?, invoice_nr_year=?, invoice_nr_month=? WHERE id=1', + [$next + 1, $year, $month] ); return $nr;