57 lines
1.5 KiB
PHP
57 lines
1.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Steuerkram\Models;
|
|
|
|
use Steuerkram\Database;
|
|
|
|
class FiscalYear
|
|
{
|
|
public static function all(): array
|
|
{
|
|
return Database::fetchAll('SELECT * FROM fiscal_years ORDER BY year DESC');
|
|
}
|
|
|
|
public static function find(int $id): array|false
|
|
{
|
|
return Database::fetchOne('SELECT * FROM fiscal_years WHERE id = ?', [$id]);
|
|
}
|
|
|
|
public static function findByYear(int $year): array|false
|
|
{
|
|
return Database::fetchOne('SELECT * FROM fiscal_years WHERE year = ?', [$year]);
|
|
}
|
|
|
|
public static function findOrCreateByYear(int $year): array
|
|
{
|
|
$fy = self::findByYear($year);
|
|
if ($fy) return $fy;
|
|
|
|
Database::execute(
|
|
'INSERT INTO fiscal_years (year, start_date, end_date) VALUES (?, ?, ?)',
|
|
[$year, "{$year}-01-01", "{$year}-12-31"]
|
|
);
|
|
|
|
return self::findByYear($year);
|
|
}
|
|
|
|
public static function currentYear(): int
|
|
{
|
|
return (int)date('Y');
|
|
}
|
|
|
|
public static function availableYears(): array
|
|
{
|
|
$years = Database::fetchAll(
|
|
'SELECT DISTINCT YEAR(buchungs_datum) as year FROM bookings ORDER BY year DESC'
|
|
);
|
|
return array_column($years, 'year');
|
|
}
|
|
|
|
/** Angelegte Geschäftsjahre (fiscal_years-Tabelle), nicht aus Buchungen abgeleitet. */
|
|
public static function registeredYears(): array
|
|
{
|
|
return Database::fetchAll('SELECT year FROM fiscal_years ORDER BY year DESC');
|
|
}
|
|
}
|