145 lines
4.7 KiB
PHP
145 lines
4.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Steuerkram\Controllers;
|
|
|
|
use Steuerkram\Models\Customer;
|
|
use Steuerkram\View;
|
|
|
|
class CustomerController extends BaseController
|
|
{
|
|
public function index(array $params): void
|
|
{
|
|
$filter = $_GET;
|
|
$customers = Customer::all($filter);
|
|
|
|
$view = new View();
|
|
$view->render('layout/base.php', [
|
|
'pageTitle' => 'Kunden',
|
|
'content' => $this->renderView('customers/list', [
|
|
'customers' => $customers,
|
|
'filter' => $filter,
|
|
]),
|
|
'topbarActions' => '<a href="/customers/new" class="btn btn-primary" title="Kunde anlegen" style="display:inline-flex;align-items:center;justify-content:center;padding:.36rem;border-radius:50%;width:1.8rem;height:1.8rem">'
|
|
. '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round">'
|
|
. '<line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/>'
|
|
. '</svg>'
|
|
. '</a>',
|
|
]);
|
|
}
|
|
|
|
public function create(array $params): void
|
|
{
|
|
$view = new View();
|
|
$view->render('layout/base.php', [
|
|
'pageTitle' => 'Kunde anlegen',
|
|
'content' => $this->renderView('customers/form', [
|
|
'customer' => [],
|
|
'errors' => [],
|
|
'action' => '/customers',
|
|
]),
|
|
]);
|
|
}
|
|
|
|
public function store(array $params): void
|
|
{
|
|
$data = $_POST;
|
|
$errors = $this->validate($data);
|
|
|
|
if ($errors) {
|
|
$view = new View();
|
|
$view->render('layout/base.php', [
|
|
'pageTitle' => 'Kunde anlegen',
|
|
'content' => $this->renderView('customers/form', [
|
|
'customer' => $data,
|
|
'errors' => $errors,
|
|
'action' => '/customers',
|
|
]),
|
|
]);
|
|
return;
|
|
}
|
|
|
|
Customer::create($data);
|
|
View::redirect('/customers', 'Kunde erfolgreich angelegt.');
|
|
}
|
|
|
|
public function show(array $params): void
|
|
{
|
|
$customer = Customer::find((int)$params['id']);
|
|
if (!$customer) { http_response_code(404); return; }
|
|
|
|
$invoices = \Steuerkram\Database::fetchAll(
|
|
'SELECT * FROM invoices WHERE customer_id=? ORDER BY rechnungs_datum DESC LIMIT 20',
|
|
[$customer['id']]
|
|
);
|
|
|
|
$view = new View();
|
|
$view->render('layout/base.php', [
|
|
'pageTitle' => Customer::displayName($customer),
|
|
'content' => $this->renderView('customers/show', [
|
|
'customer' => $customer,
|
|
'invoices' => $invoices,
|
|
]),
|
|
]);
|
|
}
|
|
|
|
public function edit(array $params): void
|
|
{
|
|
$customer = Customer::find((int)$params['id']);
|
|
if (!$customer) { http_response_code(404); return; }
|
|
|
|
$view = new View();
|
|
$view->render('layout/base.php', [
|
|
'pageTitle' => 'Kunde bearbeiten',
|
|
'content' => $this->renderView('customers/form', [
|
|
'customer' => $customer,
|
|
'errors' => [],
|
|
'action' => '/customers/' . $customer['id'],
|
|
]),
|
|
]);
|
|
}
|
|
|
|
public function update(array $params): void
|
|
{
|
|
$id = (int)$params['id'];
|
|
$customer = Customer::find($id);
|
|
if (!$customer) { http_response_code(404); return; }
|
|
|
|
$data = $_POST;
|
|
$errors = $this->validate($data);
|
|
|
|
if ($errors) {
|
|
$view = new View();
|
|
$view->render('layout/base.php', [
|
|
'pageTitle' => 'Kunde bearbeiten',
|
|
'content' => $this->renderView('customers/form', [
|
|
'customer' => array_merge($customer, $data),
|
|
'errors' => $errors,
|
|
'action' => '/customers/' . $id,
|
|
]),
|
|
]);
|
|
return;
|
|
}
|
|
|
|
Customer::update($id, $data);
|
|
View::redirect('/customers/' . $id, 'Kundendaten aktualisiert.');
|
|
}
|
|
|
|
public function archive(array $params): void
|
|
{
|
|
Customer::archive((int)$params['id']);
|
|
View::redirect('/customers', 'Kunde archiviert.');
|
|
}
|
|
|
|
private function validate(array $data): array
|
|
{
|
|
$errors = [];
|
|
if (empty($data['nachname'])) $errors['nachname'] = 'Pflichtfeld';
|
|
if (empty($data['strasse'])) $errors['strasse'] = 'Pflichtfeld';
|
|
if (empty($data['plz'])) $errors['plz'] = 'Pflichtfeld';
|
|
if (empty($data['ort'])) $errors['ort'] = 'Pflichtfeld';
|
|
return $errors;
|
|
}
|
|
}
|
|
|