diff --git a/migrations/031_anlage_nr.sql b/migrations/031_anlage_nr.sql new file mode 100644 index 0000000..a0c0460 --- /dev/null +++ b/migrations/031_anlage_nr.sql @@ -0,0 +1,13 @@ +-- Migration 031: Anlagen-Nummernkreis mit konfiguriertem Präfix +ALTER TABLE company_settings + ADD COLUMN anlage_nr_prefix VARCHAR(10) NOT NULL DEFAULT 'ANL' + COMMENT 'Präfix für Anlagen-Nummern (z.B. ANL)' + AFTER beleg_nr_next, + ADD COLUMN anlage_nr_next INT UNSIGNED NOT NULL DEFAULT 1 + COMMENT 'Nächste laufende Nummer für Anlagegüter' + AFTER anlage_nr_prefix; + +ALTER TABLE assets + ADD COLUMN anlage_nr VARCHAR(30) NOT NULL DEFAULT '' + COMMENT 'Formatierte Anlagen-Nummer (z.B. ANL-2026-0001)' + AFTER lfd_nr; diff --git a/src/Models/Asset.php b/src/Models/Asset.php index be04244..6307cef 100644 --- a/src/Models/Asset.php +++ b/src/Models/Asset.php @@ -54,6 +54,23 @@ class Asset return (int)($row['next'] ?? 1); } + public static function generateAnlageNr(): string + { + $settings = Database::fetchOne( + 'SELECT anlage_nr_prefix, anlage_nr_next FROM company_settings WHERE id=1' + ); + $prefix = $settings['anlage_nr_prefix'] ?? 'ANL'; + $next = (int)($settings['anlage_nr_next'] ?? 1); + $year = date('Y'); + $nr = sprintf('%s-%s-%04d', $prefix, $year, $next); + + Database::execute( + 'UPDATE company_settings SET anlage_nr_next = anlage_nr_next + 1 WHERE id=1' + ); + + return $nr; + } + public static function create(array $data): int { $fyId = null; @@ -63,13 +80,16 @@ class Asset $fyId = $fy['id']; } + $anlageNr = $data['anlage_nr'] ?? self::generateAnlageNr(); + Database::execute( 'INSERT INTO assets - (lfd_nr, bezeichnung, account_id, anschaffungsdatum, anschaffungskosten, + (lfd_nr, anlage_nr, bezeichnung, account_id, anschaffungsdatum, anschaffungskosten, zugaenge, abgaenge, abschreibung_jahr, nutzungsdauer, notizen, fiscal_year_id) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', [ $data['lfd_nr'] ?? self::nextLfdNr(), + $anlageNr, $data['bezeichnung'], isset($data['account_id']) && $data['account_id'] !== '' ? (int)$data['account_id'] : null, $data['anschaffungsdatum'], diff --git a/src/Models/Setting.php b/src/Models/Setting.php index 903e4c2..7db02ba 100644 --- a/src/Models/Setting.php +++ b/src/Models/Setting.php @@ -28,7 +28,7 @@ class Setting ust_id=?, steuernummer=?, finanzamt=?, iban=?, bic=?, bank_name=?, ustva_steuernummer=?, ustva_bundesland=?, - invoice_nr_prefix=?, beleg_nr_prefix=?, + invoice_nr_prefix=?, beleg_nr_prefix=?, anlage_nr_prefix=?, payment_days_default=?, test_mode=?, ki_vorschlag_aktiv=?, ocr_aktiv=?, nextcloud_url=?, nextcloud_user=?, nextcloud_pass=?, nextcloud_folder=?, nextcloud_done=? @@ -52,6 +52,7 @@ class Setting $data['ustva_bundesland'] ?? null, $data['invoice_nr_prefix'] ?? 'AR', $data['beleg_nr_prefix'] ?? 'BLG', + $data['anlage_nr_prefix'] ?? 'ANL', (int)($data['payment_days_default'] ?? 14), isset($data['test_mode']) ? 1 : 0, isset($data['ki_vorschlag_aktiv']) ? 1 : 0, diff --git a/src/Views/assets/list.php b/src/Views/assets/list.php index ea21c59..743e3bf 100644 --- a/src/Views/assets/list.php +++ b/src/Views/assets/list.php @@ -19,7 +19,7 @@