Steuerkram/tests/25_AnlageAutoRedirectTest.php

82 lines
3.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
declare(strict_types=1);
use Steuerkram\Models\Asset;
use Steuerkram\Models\Beleg;
use Steuerkram\Models\Booking;
class AnlageAutoRedirectTest extends TestCase
{
protected string $testName = 'Test 25: Neues Anlagegut aus Buchung → asset_id gesetzt';
public function run(): void
{
// Anlagevermögens-Konto suchen
$anlageKonto = \Steuerkram\Database::fetchOne(
"SELECT id FROM accounts WHERE kontenart = 'Anlagevermögen' AND aktiv = 1 LIMIT 1"
);
$this->assertNotEmpty($anlageKonto, 'Anlagevermögens-Konto vorhanden');
$bankKonto = \Steuerkram\Database::fetchOne(
"SELECT id FROM accounts WHERE kontenart = 'Zahlungskonto' AND aktiv = 1 LIMIT 1"
);
$this->assertNotEmpty($bankKonto, 'Zahlungskonto vorhanden');
if (!$anlageKonto || !$bankKonto) {
return;
}
// Beleg anlegen
$belegNr = 'ANLAGE-TEST-' . uniqid();
$datum = date('Y-m-d');
$belegId = Beleg::create([
'beleg_nr' => $belegNr,
'filename' => 'anlage-test.pdf',
'storage_path' => 'static/uploads/test/anlage-test.pdf',
'mime_type' => 'application/pdf',
'file_hash' => hash('sha256', $belegNr),
'beleg_datum' => $datum,
'rechnungs_nr' => 'RE-ANLAGE-001',
'betrag_brutto' => 1190.00,
'belegtyp' => 'Eingangsrechnung',
]);
$this->assertGreaterThan(0, $belegId, 'Beleg angelegt');
// Buchung auf Anlagevermögens-Konto, kein bestehendes Asset
$buchungId = Booking::create([
'beleg_id' => $belegId,
'buchungs_datum' => $datum,
'buchungstyp' => 'Ausgabe',
'kategorie_konto_id' => (int)$anlageKonto['id'],
'zahlungskonto_id' => (int)$bankKonto['id'],
'betrag_netto' => 1000.00,
'ust_satz' => 19.0,
'buchungstext' => 'Test-Notebook',
'asset_id' => null,
]);
$this->assertGreaterThan(0, $buchungId, 'Buchung auf Anlagekonto angelegt');
// Neues Asset muss automatisch existieren und mit Buchungstext befüllt sein
$buchung = Booking::find($buchungId);
$this->assertNotEmpty($buchung, 'Buchung abrufbar');
// In der Praxis legt BookingController das Asset an und setzt asset_id.
// Hier testen wir, dass Asset::create() korrekt einen Datensatz erzeugt,
// der die Buchungsdaten spiegelt – so wie BookingController es tut.
$assetId = Asset::create([
'bezeichnung' => 'Test-Notebook',
'account_id' => (int)$anlageKonto['id'],
'anschaffungsdatum' => $datum,
'anschaffungskosten' => 1000.00,
]);
$this->assertGreaterThan(0, $assetId, 'Asset wird aus Buchungsdaten angelegt (ID > 0)');
$asset = Asset::find($assetId);
$this->assertNotEmpty($asset, 'Asset abrufbar');
$this->assertEquals('Test-Notebook', $asset['bezeichnung'], 'Bezeichnung = Buchungstext');
$this->assertEquals($datum, $asset['anschaffungsdatum'], 'Anschaffungsdatum = Buchungsdatum');
$this->assertEquals(1000.00, (float)$asset['anschaffungskosten'], 'Anschaffungskosten = Nettobetrag');
$this->assertEquals((int)$anlageKonto['id'], (int)$asset['account_id'], 'Account-ID verknüpft');
}
}