diff --git a/src/Controllers/InvoiceController.php b/src/Controllers/InvoiceController.php index 6421c16..556779f 100644 --- a/src/Controllers/InvoiceController.php +++ b/src/Controllers/InvoiceController.php @@ -40,15 +40,26 @@ class InvoiceController $customers = Customer::all(); $settings = Setting::get(); + $invoiceDefaults = [ + 'rechnungs_datum' => date('Y-m-d'), + 'leistungszeitraum_von' => date('Y-m-01'), + 'leistungszeitraum_bis' => date('Y-m-t'), + ]; + + $preCustomerId = (int)($_GET['customer_id'] ?? 0); + if ($preCustomerId > 0) { + $customer = Customer::find($preCustomerId); + if ($customer) { + $invoiceDefaults['customer_id'] = $preCustomerId; + $invoiceDefaults['payment_days'] = $customer['payment_days']; + } + } + $view = new View(); $view->render('layout/base.php', [ 'pageTitle' => 'Neue Rechnung', 'content' => $this->renderView('invoices/form', [ - 'invoice' => [ - 'rechnungs_datum' => date('Y-m-d'), - 'leistungszeitraum_von' => date('Y-m-01'), - 'leistungszeitraum_bis' => date('Y-m-t'), - ], + 'invoice' => $invoiceDefaults, 'lines' => [], 'customers' => $customers, 'errors' => [], diff --git a/src/Views/invoices/form.php b/src/Views/invoices/form.php index 066e648..ee88763 100644 --- a/src/Views/invoices/form.php +++ b/src/Views/invoices/form.php @@ -149,4 +149,10 @@ function updatePaymentDays(sel) { const days = opt.dataset.payment; if (days) document.getElementById('payment_days').value = days; } + +document.addEventListener('DOMContentLoaded', function () { + const sel = document.getElementById('customer_id'); + if (sel && sel.value) updatePaymentDays(sel); +}); + diff --git a/tests/22_RechnungZahlungszielTest.php b/tests/22_RechnungZahlungszielTest.php new file mode 100644 index 0000000..a1ba63d --- /dev/null +++ b/tests/22_RechnungZahlungszielTest.php @@ -0,0 +1,63 @@ + 'ZahlungszielTestKunde', + 'strasse' => 'Musterstr. 1', + 'plz' => '12345', + 'ort' => 'Musterstadt', + 'land' => 'DE', + 'payment_days' => 30, + ]); + + $customer = Customer::find($customerId); + $this->assert($customer !== false, 'Kunde wurde angelegt'); + $this->assertEquals(30, (int)$customer['payment_days'], 'Kundendatensatz enthält payment_days=30'); + + // Simuliere Controller-Logik: customer_id per GET → payment_days vorbelegen + $preCustomerId = $customerId; + $invoiceDefaults = [ + 'rechnungs_datum' => date('Y-m-d'), + 'leistungszeitraum_von' => date('Y-m-01'), + 'leistungszeitraum_bis' => date('Y-m-t'), + ]; + if ($preCustomerId > 0) { + $c = Customer::find($preCustomerId); + if ($c) { + $invoiceDefaults['customer_id'] = $preCustomerId; + $invoiceDefaults['payment_days'] = $c['payment_days']; + } + } + + $this->assertEquals($customerId, $invoiceDefaults['customer_id'], 'customer_id in Rechnungsdefaults gesetzt'); + $this->assertEquals(30, (int)$invoiceDefaults['payment_days'], 'payment_days aus Kundendaten vorbelegt'); + + // Ohne vorausgewählten Kunden bleibt payment_days ungesetzt + $defaultsOhneKunde = [ + 'rechnungs_datum' => date('Y-m-d'), + ]; + $this->assert(!isset($defaultsOhneKunde['payment_days']), 'payment_days ohne Kunden nicht gesetzt'); + + // Anderer Kunde mit abweichendem Zahlungsziel + $customer2Id = Customer::create([ + 'nachname' => 'ZahlungszielTestKunde2', + 'strasse' => 'Testgasse 2', + 'plz' => '54321', + 'ort' => 'Testdorf', + 'land' => 'DE', + 'payment_days' => 7, + ]); + $c2 = Customer::find($customer2Id); + $this->assertEquals(7, (int)$c2['payment_days'], 'Zweiter Kunde hat payment_days=7'); + } +}