Vaihe 1/2: asiakkaat, yhteyshenkilöt ja laskutusyksiköt

- customers-moduuli Clean Architecture -kerroksin: asiakas (yritys/kuluttaja),
  Y-tunnuksen tarkiste ja yksilöllisyys, yhteyshenkilöt, laskutusyksiköt
  (verkkolaskuosoite vaatii välittäjätunnuksen)
- henkilötunnus salattuna (Fernet) ja HMAC-tiiviste päällekkäisyyksiin;
  pääkäyttäjä ja johto näkevät ja poistavat, muut näkevät peitettynä
- sisäinen asiakas data-migraatiolla, ei poistettavissa
- yleinen poistonesto käänteisten relaatioiden perusteella (tulevat
  PROTECT-viiteavaimet estävät poiston automaattisesti)
- asiakkaan sivu: lisäys ja muokkaus dialogissa HTMX:llä, fokuksen hallinta
  ja ruudunlukijailmoitukset; HTMX näyttää nyt 422-validointivirheet
- testit: domain, application, integraatio ja selaintestit (axe);
  yhteiset fixturet apps/conftest.py:hin, FakeUnitOfWork shared-moduuliin
- import-linter: moduulien riippumattomuussäännöt
- dokumentit päivitetty

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-11-29 13:47:09 +02:00
co-authored by Claude Opus 5.5
parent e7b2745916
commit a81c073d6a
68 changed files with 3470 additions and 144 deletions
@@ -0,0 +1,15 @@
{% extends "base.html" %}
{% block title %}{{ customer.name }}{% endblock %}
{% block content %}
<nav class="breadcrumbs text-sm" aria-label="{% translate 'Breadcrumbs' %}">
<ul>
<li><a href="{% url 'customers:list' %}">{% translate "Customers" %}</a></li>
<li><a href="{% url 'customers:detail' customer.id %}">{{ customer.name }}</a></li>
</ul>
</nav>
<div class="card bg-base-100 shadow-sm max-w-2xl">
<div class="card-body">
{% include "customers/partials/child_form.html" %}
</div>
</div>
{% endblock %}
@@ -0,0 +1,42 @@
{% extends "base.html" %}{% load customers_tags %}
{% block title %}{{ customer.name }}{% endblock %}
{% block content %}
<nav class="breadcrumbs text-sm" aria-label="{% translate 'Breadcrumbs' %}">
<ul><li><a href="{% url 'customers:list' %}">{% translate "Customers" %}</a></li><li aria-current="page">{{ customer.name }}</li></ul>
</nav>
<div class="flex flex-wrap items-center justify-between gap-2 mb-4">
<h1 class="text-2xl font-bold">
{{ customer.name }}
{% if customer.is_internal %}<span class="badge badge-info badge-soft align-middle">{% translate "Internal" %}</span>{% endif %}
</h1>
{% include "customers/partials/actions.html" %}
</div>
<section class="card bg-base-100 shadow-sm mb-4" aria-labelledby="basic-heading">
<div class="card-body">
<h2 id="basic-heading" class="card-title">{% translate "Basic information" %}</h2>
<dl class="grid gap-x-6 gap-y-2 sm:grid-cols-[max-content_1fr]">
<dt class="font-semibold">{% translate "Type" %}</dt><dd>{{ customer.type|customer_type_label }}</dd>
{% if customer.type == "company" %}
<dt class="font-semibold">{% translate "Business ID" %}</dt><dd>{{ customer.business_id|default:"–" }}</dd>
{% else %}
<dt class="font-semibold">{% translate "Personal identity code" %}</dt>
<dd id="personal-id">{% include "customers/partials/personal_id.html" %}</dd>
{% endif %}
</dl>
{% if customer.is_internal %}
<p class="text-sm mt-2">{% translate "Internal work (administration, sales, training) is recorded on the jobs of the internal customer. It cannot be deleted." %}</p>
{% endif %}
</div>
</section>
<div class="grid gap-4 lg:grid-cols-2">
{% include "customers/partials/contacts.html" %}
{% include "customers/partials/billing_units.html" %}
</div>
<dialog id="item-dialog" class="modal" aria-labelledby="item-dialog-title">
<div class="modal-box" id="item-dialog-body"></div>
</dialog>
{% endblock %}
@@ -0,0 +1,34 @@
{% extends "base.html" %}
{% block title %}{% if customer %}{% translate "Edit customer" %}{% else %}{% translate "New customer" %}{% endif %}{% endblock %}
{% block content %}
<nav class="breadcrumbs text-sm" aria-label="{% translate 'Breadcrumbs' %}">
<ul>
<li><a href="{% url 'customers:list' %}">{% translate "Customers" %}</a></li>
{% if customer %}<li><a href="{% url 'customers:detail' customer.id %}">{{ customer.name }}</a></li>{% endif %}
<li aria-current="page">{% if customer %}{% translate "Edit" %}{% else %}{% translate "New customer" %}{% endif %}</li>
</ul>
</nav>
<h1 class="text-2xl font-bold mb-4">{% if customer %}{% translate "Edit customer" %}{% else %}{% translate "New customer" %}{% endif %}</h1>
<form method="post" novalidate class="card bg-base-100 shadow-sm max-w-2xl"
x-data="{ type: '{{ form.type.value|default:'company'|escapejs }}' }">
{% csrf_token %}
<div class="card-body">
{% include "components/non_field_errors.html" %}
{{ form.name.as_field_group }}
{{ form.type.as_field_group }}
<div x-show="type === 'company'" x-cloak>
{{ form.business_id.as_field_group }}
</div>
<div x-show="type === 'consumer'" x-cloak>
{{ form.personal_identity_code.as_field_group }}
{% if form.remove_personal_id %}{{ form.remove_personal_id.as_field_group }}{% endif %}
<p class="text-sm mt-2">{% translate "The personal identity code is specially protected data. It is stored encrypted and used only for invoicing." %}</p>
</div>
<div class="card-actions justify-end mt-4">
<a href="{% if customer %}{% url 'customers:detail' customer.id %}{% else %}{% url 'customers:list' %}{% endif %}" class="btn">{% translate "Cancel" %}</a>
<button type="submit" class="btn btn-primary">{% translate "Save" %}</button>
</div>
</div>
</form>
{% endblock %}
@@ -0,0 +1,23 @@
{% extends "base.html" %}
{% block title %}{% translate "Customers" %}{% endblock %}
{% block content %}
<div class="flex flex-wrap items-center justify-between gap-2 mb-4">
<h1 class="text-2xl font-bold">{% translate "Customers" %}</h1>
<a href="{% url 'customers:create' %}" class="btn btn-primary">{% translate "New customer" %}</a>
</div>
<form method="get" role="search" aria-label="{% translate 'Filter customers' %}"
class="card bg-base-100 shadow-sm mb-4"
hx-get="{% url 'customers:list' %}" hx-target="#customer-table" hx-push-url="true"
hx-trigger="input changed delay:300ms from:input[name='q'], change from:select, submit">
<div class="card-body grid gap-2 sm:grid-cols-2 py-4">
{{ filters.q.as_field_group }}
{{ filters.type.as_field_group }}
<noscript><button type="submit" class="btn">{% translate "Filter" %}</button></noscript>
</div>
</form>
<div id="customer-table">
{% include "customers/partials/customer_table.html" %}
</div>
{% endblock %}
@@ -0,0 +1,13 @@
<div id="customer-actions" class="flex gap-2"{% if oob %} hx-swap-oob="true"{% endif %}>
<a href="{% url 'customers:edit' customer.id %}" class="btn">{% translate "Edit" %}</a>
{% if is_deletable %}
<button type="button" class="btn btn-error btn-outline" onclick="document.getElementById('confirm-delete-customer').showModal()">{% translate "Delete customer" %}</button>
{% endif %}
{% if is_deletable %}
{% url 'customers:delete' customer.id as delete_url %}
{% translate "Delete the customer?" as dialog_title %}
{% translate "The customer is deleted permanently." as dialog_body %}
{% translate "Delete" as dialog_confirm %}
{% include "components/confirm_dialog.html" with id="confirm-delete-customer" title=dialog_title body=dialog_body action_url=delete_url confirm_label=dialog_confirm %}
{% endif %}
</div>
@@ -0,0 +1,46 @@
<section id="billing-units-section" class="card bg-base-100 shadow-sm" aria-labelledby="billing-units-heading">
<div class="card-body">
<div class="flex flex-wrap items-center justify-between gap-2">
<h2 id="billing-units-heading" class="card-title" tabindex="-1">{% translate "Billing units" %}</h2>
<a href="{% url 'customers:child_create' customer.id 'billing-units' %}" class="btn btn-sm"
hx-get="{% url 'customers:child_create' customer.id 'billing-units' %}" hx-target="#item-dialog-body"
hx-on::after-request="document.getElementById('item-dialog').showModal()">{% translate "Add billing unit" %}</a>
</div>
<p class="text-sm">{% translate "Each billing unit is invoiced separately." %}</p>
{% if billing_units %}
<ul class="divide-y divide-base-300">
{% for u in billing_units %}{% with id_str=u.id|stringformat:"s" %}
<li class="py-3 flex flex-wrap justify-between gap-2">
<div>
<p class="font-semibold">{{ u.name }}</p>
{% if u.billing_address %}<p class="whitespace-pre-line">{{ u.billing_address }}</p>{% endif %}
{% if u.e_invoice_address %}
<p>{% translate "E-invoice" %}: {{ u.e_invoice_address }} ({% translate "operator" %} {{ u.e_invoice_operator }})</p>
{% endif %}
{% if u.reference %}<p>{% translate "Reference" %}: {{ u.reference }}</p>{% endif %}
</div>
<div class="flex gap-1 items-start">
<a class="btn btn-sm btn-ghost" href="{% url 'customers:child_edit' customer.id 'billing-units' u.id %}"
hx-get="{% url 'customers:child_edit' customer.id 'billing-units' u.id %}" hx-target="#item-dialog-body"
hx-on::after-request="document.getElementById('item-dialog').showModal()">
{% translate "Edit" %}<span class="sr-only">: {{ u.name }}</span></a>
{% if u.in_use %}
<span class="text-sm">{% translate "In use in a job" %}</span>
{% else %}
<button type="button" class="btn btn-sm btn-ghost" onclick="document.getElementById('confirm-delete-unit-{{ u.id }}').showModal()">
{% translate "Delete" %}<span class="sr-only">: {{ u.name }}</span></button>
{% url 'customers:child_delete' customer.id 'billing-units' u.id as delete_url %}
{% blocktranslate with name=u.name asvar dialog_title %}Delete billing unit {{ name }}?{% endblocktranslate %}
{% translate "The billing unit is deleted permanently." as dialog_body %}
{% translate "Delete" as dialog_confirm %}
{% with dialog_id="confirm-delete-unit-"|add:id_str %}{% include "components/confirm_dialog.html" with id=dialog_id title=dialog_title body=dialog_body action_url=delete_url confirm_label=dialog_confirm hx_target="#billing-units-section" %}{% endwith %}
{% endif %}
</div>
</li>
{% endwith %}{% endfor %}
</ul>
{% else %}
<p>{% translate "No billing units yet." %}</p>
{% endif %}
</div>
</section>
@@ -0,0 +1,21 @@
{% if item_id %}
{% url 'customers:child_edit' customer.id section item_id as action_url %}
{% else %}
{% url 'customers:child_create' customer.id section as action_url %}
{% endif %}
<h2 id="item-dialog-title" class="text-lg font-bold">
{% if section == "contacts" %}
{% if item_id %}{% translate "Edit contact" %}{% else %}{% translate "Add contact" %}{% endif %}
{% else %}
{% if item_id %}{% translate "Edit billing unit" %}{% else %}{% translate "Add billing unit" %}{% endif %}
{% endif %}
</h2>
<form method="post" action="{{ action_url }}" novalidate class="space-y-1 mt-2"
hx-post="{{ action_url }}" hx-target="#item-dialog-body">
{% csrf_token %}
{{ form }}
<div class="modal-action">
<button type="button" class="btn" onclick="this.closest('dialog') ? this.closest('dialog').close() : history.back()">{% translate "Cancel" %}</button>
<button type="submit" class="btn btn-primary">{% translate "Save" %}</button>
</div>
</form>
@@ -0,0 +1,43 @@
<section id="contacts-section" class="card bg-base-100 shadow-sm" aria-labelledby="contacts-heading">
<div class="card-body">
<div class="flex flex-wrap items-center justify-between gap-2">
<h2 id="contacts-heading" class="card-title" tabindex="-1">{% translate "Contacts" %}</h2>
<a href="{% url 'customers:child_create' customer.id 'contacts' %}" class="btn btn-sm"
hx-get="{% url 'customers:child_create' customer.id 'contacts' %}" hx-target="#item-dialog-body"
hx-on::after-request="document.getElementById('item-dialog').showModal()">{% translate "Add contact" %}</a>
</div>
{% if contacts %}
<ul class="divide-y divide-base-300">
{% for c in contacts %}{% with id_str=c.id|stringformat:"s" %}
<li class="py-3 flex flex-wrap justify-between gap-2">
<div>
<p class="font-semibold">{{ c.name }}{% if c.title %}, <span class="font-normal">{{ c.title }}</span>{% endif %}</p>
{% if c.phone %}<p><a class="link" href="tel:{{ c.phone }}">{{ c.phone }}</a></p>{% endif %}
{% if c.email %}<p><a class="link break-all" href="mailto:{{ c.email }}">{{ c.email }}</a></p>{% endif %}
{% if c.note %}<p class="text-sm whitespace-pre-line">{{ c.note }}</p>{% endif %}
</div>
<div class="flex gap-1 items-start">
<a class="btn btn-sm btn-ghost" href="{% url 'customers:child_edit' customer.id 'contacts' c.id %}"
hx-get="{% url 'customers:child_edit' customer.id 'contacts' c.id %}" hx-target="#item-dialog-body"
hx-on::after-request="document.getElementById('item-dialog').showModal()">
{% translate "Edit" %}<span class="sr-only">: {{ c.name }}</span></a>
{% if c.in_use %}
<span class="text-sm">{% translate "In use in a job" %}</span>
{% else %}
<button type="button" class="btn btn-sm btn-ghost" onclick="document.getElementById('confirm-delete-contact-{{ c.id }}').showModal()">
{% translate "Delete" %}<span class="sr-only">: {{ c.name }}</span></button>
{% url 'customers:child_delete' customer.id 'contacts' c.id as delete_url %}
{% blocktranslate with name=c.name asvar dialog_title %}Delete contact {{ name }}?{% endblocktranslate %}
{% translate "The contact is deleted permanently." as dialog_body %}
{% translate "Delete" as dialog_confirm %}
{% with dialog_id="confirm-delete-contact-"|add:id_str %}{% include "components/confirm_dialog.html" with id=dialog_id title=dialog_title body=dialog_body action_url=delete_url confirm_label=dialog_confirm hx_target="#contacts-section" %}{% endwith %}
{% endif %}
</div>
</li>
{% endwith %}{% endfor %}
</ul>
{% else %}
<p>{% translate "No contacts yet." %}</p>
{% endif %}
</div>
</section>
@@ -0,0 +1,41 @@
{% load customers_tags %}
<p class="sr-only" role="status">{% blocktranslate count counter=page.paginator.count %}{{ counter }} customer found{% plural %}{{ counter }} customers found{% endblocktranslate %}</p>
<div class="overflow-x-auto card bg-base-100 shadow-sm">
<table class="table">
<caption class="sr-only">{% translate "Customers" %}</caption>
<thead>
<tr>
<th scope="col">{% translate "Name" %}</th>
<th scope="col">{% translate "Type" %}</th>
<th scope="col">{% translate "Business ID" %}</th>
</tr>
</thead>
<tbody>
{% for c in page.object_list %}
<tr>
<td>
<a class="link" href="{% url 'customers:detail' c.id %}">{{ c.name }}</a>
{% if c.is_internal %}<span class="badge badge-info badge-soft ml-1">{% translate "Internal" %}</span>{% endif %}
</td>
<td>{{ c.type|customer_type_label }}</td>
<td>{{ c.business_id|default:"–" }}</td>
</tr>
{% empty %}
<tr><td colspan="3">{% translate "No customers match the filters." %}</td></tr>
{% endfor %}
</tbody>
</table>
</div>
{% if page.paginator.num_pages > 1 %}
<nav aria-label="{% translate 'Pages' %}" class="join mt-4">
{% if page.has_previous %}
<a class="join-item btn" href="?{{ query }}{% if query %}&amp;{% endif %}page={{ page.previous_page_number }}"
hx-get="?{{ query }}{% if query %}&amp;{% endif %}page={{ page.previous_page_number }}" hx-target="#customer-table" hx-push-url="true">{% translate "Previous" %}</a>
{% endif %}
<span class="join-item btn btn-disabled" aria-current="page">{% blocktranslate with n=page.number total=page.paginator.num_pages %}Page {{ n }} / {{ total }}{% endblocktranslate %}</span>
{% if page.has_next %}
<a class="join-item btn" href="?{{ query }}{% if query %}&amp;{% endif %}page={{ page.next_page_number }}"
hx-get="?{{ query }}{% if query %}&amp;{% endif %}page={{ page.next_page_number }}" hx-target="#customer-table" hx-push-url="true">{% translate "Next" %}</a>
{% endif %}
</nav>
{% endif %}
@@ -0,0 +1,13 @@
{% if customer.personal_id_full %}
<span class="font-mono" tabindex="-1" data-autofocus>{{ customer.personal_id_full }}</span>
{% elif customer.personal_id_masked %}
<span class="font-mono">{{ customer.personal_id_masked }}</span>
{% if can_view_personal_id %}
<button type="button" class="btn btn-ghost btn-sm ml-2"
hx-get="{% url 'customers:personal_id' customer.id %}" hx-target="#personal-id">
{% translate "Show personal identity code" %}
</button>
{% endif %}
{% else %}
<span>{% translate "Not given" %}</span>
{% endif %}