155 lines
5.1 KiB
PHP
155 lines
5.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Steuerkram\Models;
|
|
|
|
use Steuerkram\Database;
|
|
|
|
class Asset
|
|
{
|
|
public static function all(?int $fiscalYearId = null): array
|
|
{
|
|
$where = '1=1';
|
|
$params = [];
|
|
|
|
if ($fiscalYearId !== null) {
|
|
$where = 'a.fiscal_year_id = ?';
|
|
$params[] = $fiscalYearId;
|
|
}
|
|
|
|
return Database::fetchAll(
|
|
"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 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
|
|
{
|
|
$row = Database::fetchOne('SELECT COALESCE(MAX(lfd_nr), 0) + 1 AS next FROM assets');
|
|
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;
|
|
if (!empty($data['anschaffungsdatum'])) {
|
|
$year = (int)date('Y', strtotime($data['anschaffungsdatum']));
|
|
$fy = FiscalYear::findOrCreateByYear($year);
|
|
$fyId = $fy['id'];
|
|
}
|
|
|
|
$anlageNr = $data['anlage_nr'] ?? self::generateAnlageNr();
|
|
|
|
Database::execute(
|
|
'INSERT INTO assets
|
|
(lfd_nr, anlage_nr, bezeichnung, account_id, anschaffungsdatum, anschaffungskosten,
|
|
zugaenge, abgaenge, abschreibung_jahr, nutzungsdauer, notizen, fiscal_year_id)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
|
|
[
|
|
$data['lfd_nr'] ?? self::nextLfdNr(),
|
|
$anlageNr,
|
|
$data['bezeichnung'],
|
|
isset($data['account_id']) && $data['account_id'] !== '' ? (int)$data['account_id'] : null,
|
|
$data['anschaffungsdatum'],
|
|
$data['anschaffungskosten'] ?? 0,
|
|
$data['zugaenge'] ?? 0,
|
|
$data['abgaenge'] ?? 0,
|
|
$data['abschreibung_jahr'] ?? 0,
|
|
isset($data['nutzungsdauer']) && $data['nutzungsdauer'] !== '' ? (int)$data['nutzungsdauer'] : null,
|
|
$data['notizen'] ?? null,
|
|
$fyId,
|
|
]
|
|
);
|
|
return (int)Database::lastInsertId();
|
|
}
|
|
|
|
public static function update(int $id, array $data): void
|
|
{
|
|
$fyId = null;
|
|
if (!empty($data['anschaffungsdatum'])) {
|
|
$year = (int)date('Y', strtotime($data['anschaffungsdatum']));
|
|
$fy = FiscalYear::findOrCreateByYear($year);
|
|
$fyId = $fy['id'];
|
|
}
|
|
|
|
Database::execute(
|
|
'UPDATE assets SET
|
|
bezeichnung = ?,
|
|
account_id = ?,
|
|
anschaffungsdatum = ?,
|
|
anschaffungskosten = ?,
|
|
zugaenge = ?,
|
|
abgaenge = ?,
|
|
abschreibung_jahr = ?,
|
|
nutzungsdauer = ?,
|
|
notizen = ?,
|
|
fiscal_year_id = COALESCE(?, fiscal_year_id),
|
|
updated_at = NOW()
|
|
WHERE id = ?',
|
|
[
|
|
$data['bezeichnung'],
|
|
isset($data['account_id']) && $data['account_id'] !== '' ? (int)$data['account_id'] : null,
|
|
$data['anschaffungsdatum'],
|
|
$data['anschaffungskosten'] ?? 0,
|
|
$data['zugaenge'] ?? 0,
|
|
$data['abgaenge'] ?? 0,
|
|
$data['abschreibung_jahr'] ?? 0,
|
|
isset($data['nutzungsdauer']) && $data['nutzungsdauer'] !== '' ? (int)$data['nutzungsdauer'] : null,
|
|
$data['notizen'] ?? null,
|
|
$fyId,
|
|
$id,
|
|
]
|
|
);
|
|
}
|
|
|
|
public static function delete(int $id): bool
|
|
{
|
|
$asset = self::find($id);
|
|
if (!$asset) return false;
|
|
|
|
Database::execute('DELETE FROM assets WHERE id = ?', [$id]);
|
|
return true;
|
|
}
|
|
}
|