diff --git a/migrations/029_assets_anlageart.sql b/migrations/029_assets_anlageart.sql new file mode 100644 index 0000000..fa4fed2 --- /dev/null +++ b/migrations/029_assets_anlageart.sql @@ -0,0 +1,6 @@ +-- Migration 029: Anlageart (Kontonummer) zu assets hinzufügen +ALTER TABLE assets + ADD COLUMN account_id INT UNSIGNED NULL + COMMENT 'FK auf accounts – Anlagevermögenskonto (Anlageart)' + AFTER bezeichnung, + ADD KEY idx_assets_account (account_id); diff --git a/src/Controllers/AssetController.php b/src/Controllers/AssetController.php index 6f435ac..dded27f 100644 --- a/src/Controllers/AssetController.php +++ b/src/Controllers/AssetController.php @@ -29,9 +29,10 @@ class AssetController $view->render('layout/base.php', [ 'pageTitle' => 'Wirtschaftsgut anlegen', 'content' => $this->renderView('assets/form', [ - 'asset' => [], - 'errors' => [], - 'action' => '/assets', + 'asset' => [], + 'errors' => [], + 'action' => '/assets', + 'anlagearten' => Asset::anlagearten(), ]), ]); } @@ -46,9 +47,10 @@ class AssetController $view->render('layout/base.php', [ 'pageTitle' => 'Wirtschaftsgut anlegen', 'content' => $this->renderView('assets/form', [ - 'asset' => $data, - 'errors' => $errors, - 'action' => '/assets', + 'asset' => $data, + 'errors' => $errors, + 'action' => '/assets', + 'anlagearten' => Asset::anlagearten(), ]), ]); return; @@ -67,9 +69,10 @@ class AssetController $view->render('layout/base.php', [ 'pageTitle' => 'Wirtschaftsgut bearbeiten', 'content' => $this->renderView('assets/form', [ - 'asset' => $asset, - 'errors' => [], - 'action' => '/assets/' . $asset['id'], + 'asset' => $asset, + 'errors' => [], + 'action' => '/assets/' . $asset['id'], + 'anlagearten' => Asset::anlagearten(), ]), ]); } @@ -88,9 +91,10 @@ class AssetController $view->render('layout/base.php', [ 'pageTitle' => 'Wirtschaftsgut bearbeiten', 'content' => $this->renderView('assets/form', [ - 'asset' => array_merge($asset, $data), - 'errors' => $errors, - 'action' => '/assets/' . $id, + 'asset' => array_merge($asset, $data), + 'errors' => $errors, + 'action' => '/assets/' . $id, + 'anlagearten' => Asset::anlagearten(), ]), ]); return; @@ -115,6 +119,7 @@ class AssetController { return [ 'bezeichnung' => trim($_POST['bezeichnung'] ?? ''), + 'account_id' => trim($_POST['account_id'] ?? ''), 'anschaffungsdatum' => trim($_POST['anschaffungsdatum'] ?? ''), 'anschaffungskosten' => $_POST['anschaffungskosten'] ?? '0', 'zugaenge' => $_POST['zugaenge'] ?? '0', diff --git a/src/Models/Asset.php b/src/Models/Asset.php index 9b7f5e5..be04244 100644 --- a/src/Models/Asset.php +++ b/src/Models/Asset.php @@ -14,19 +14,38 @@ class Asset $params = []; if ($fiscalYearId !== null) { - $where = 'fiscal_year_id = ?'; + $where = 'a.fiscal_year_id = ?'; $params[] = $fiscalYearId; } return Database::fetchAll( - "SELECT * FROM assets WHERE $where ORDER BY lfd_nr ASC, id ASC", + "SELECT a.*, ac.kontonummer AS anlageart_kontonummer, ac.bezeichnung AS anlageart_bezeichnung + FROM assets a + LEFT JOIN accounts ac ON a.account_id = ac.id + WHERE $where + ORDER BY a.lfd_nr ASC, a.id ASC", $params ); } public static function find(int $id): array|false { - return Database::fetchOne('SELECT * FROM assets WHERE id = ?', [$id]); + return Database::fetchOne( + 'SELECT a.*, ac.kontonummer AS anlageart_kontonummer, ac.bezeichnung AS anlageart_bezeichnung + FROM assets a + LEFT JOIN accounts ac ON a.account_id = ac.id + WHERE a.id = ?', + [$id] + ); + } + + public static function anlagearten(): array + { + return Database::fetchAll( + "SELECT id, kontonummer, bezeichnung FROM accounts + WHERE kontenart = 'Anlagevermögen' + ORDER BY kontonummer ASC" + ); } public static function nextLfdNr(): int @@ -46,12 +65,13 @@ class Asset Database::execute( 'INSERT INTO assets - (lfd_nr, bezeichnung, anschaffungsdatum, anschaffungskosten, + (lfd_nr, bezeichnung, account_id, anschaffungsdatum, anschaffungskosten, zugaenge, abgaenge, abschreibung_jahr, nutzungsdauer, notizen, fiscal_year_id) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', [ $data['lfd_nr'] ?? self::nextLfdNr(), $data['bezeichnung'], + isset($data['account_id']) && $data['account_id'] !== '' ? (int)$data['account_id'] : null, $data['anschaffungsdatum'], $data['anschaffungskosten'] ?? 0, $data['zugaenge'] ?? 0, @@ -77,6 +97,7 @@ class Asset Database::execute( 'UPDATE assets SET bezeichnung = ?, + account_id = ?, anschaffungsdatum = ?, anschaffungskosten = ?, zugaenge = ?, @@ -89,6 +110,7 @@ class Asset WHERE id = ?', [ $data['bezeichnung'], + isset($data['account_id']) && $data['account_id'] !== '' ? (int)$data['account_id'] : null, $data['anschaffungsdatum'], $data['anschaffungskosten'] ?? 0, $data['zugaenge'] ?? 0, diff --git a/src/Views/assets/form.php b/src/Views/assets/form.php index 5f50eb5..e5b5aea 100644 --- a/src/Views/assets/form.php +++ b/src/Views/assets/form.php @@ -4,6 +4,19 @@
+
+ + +
+
Nr. Anschaffungsdatum + Anlageart Wirtschaftsgut Anschaffungskosten Zugänge @@ -20,6 +21,13 @@ + + + + + – + + € € @@ -46,7 +54,7 @@ - + Noch keine Wirtschaftsgüter angelegt.