fix: FIN-Abgleich ignoriert ausgeblendete Fahrzeuge, Loesch-Rueckfrage auf Desktop in eigener Zeile

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Jonny 2026-11-09 13:55:23 +01:00
parent f20d643049
commit e4e0436225
4 changed files with 111 additions and 3 deletions

View File

@ -0,0 +1,84 @@
-- FIN-Abgleich bei Fahrtbeginn: ausgeblendete Fahrzeuge zaehlen nicht als
-- FIN-Konflikt (partieller Unique-Index erlaubt die FIN fuer sichtbare).
-- Unveraendert aus 20261031090000_einsatzrollen.sql bis auf den Filter.
create or replace function public.fin_bei_fahrtbeginn(p_auftrag_id uuid, p_fin text)
returns public.app_fin_abgleich
language plpgsql
security definer
set search_path = ''
as $$
declare
v_fin text := upper(btrim(coalesce(p_fin, '')));
v_fahrzeug_id uuid;
v_firma_id uuid;
v_hinterlegt text;
v_alt_fin text;
v_alt_status public.app_fin_abgleich;
v_status public.app_fin_abgleich;
begin
if not public.ist_einsatzrolle() then
raise exception 'Nur Einsatzrollen duerfen den FIN-Abgleich durchfuehren';
end if;
if v_fin !~ '^[A-HJ-NPR-Z0-9]{17}$' then
raise exception 'Die FIN muss 17 Zeichen lang sein (A–Z, 0–9, ohne I, O, Q).';
end if;
select a.fahrzeug_id, a.firma_id, a.start_fin, a.start_fin_status
into v_fahrzeug_id, v_firma_id, v_alt_fin, v_alt_status
from public.auftraege a
where a.id = p_auftrag_id
and a.zugewiesen_an = auth.uid()
and a.firma_id = public.user_firma_id()
and a.status = 'zugewiesen'::public.app_auftrag_status
for update;
if not found then
raise exception 'Auftrag nicht gefunden oder nicht im Status zugewiesen';
end if;
select f.fin into v_hinterlegt
from public.fahrzeuge f
where f.id = v_fahrzeug_id
for update;
if v_alt_status = 'abweichend' then
-- Sticky: ein einmal vermerkter Abweichungs-Nachweis wird nicht
-- ueberschrieben (auch nicht durch erneuten Aufruf mit der hinterlegten FIN).
return v_alt_status;
end if;
if v_alt_status = 'nachgetragen' and v_alt_fin = v_fin then
-- Wiederholter Aufruf mit derselben FIN: Ergebnis bleibt 'nachgetragen'.
v_status := 'nachgetragen';
elsif v_hinterlegt is null then
if exists (select 1 from public.fahrzeuge f
where f.fin = v_fin and f.id <> v_fahrzeug_id and f.firma_id = v_firma_id
and f.ausgeblendet_am is null) then
v_status := 'abweichend';
else
begin
update public.fahrzeuge set fin = v_fin where id = v_fahrzeug_id;
v_status := 'nachgetragen';
exception when unique_violation then
-- Race: parallel wurde dieselbe FIN einem anderen Fahrzeug gegeben.
v_status := 'abweichend';
end;
end if;
elsif v_hinterlegt = v_fin then
v_status := 'bestaetigt';
else
v_status := 'abweichend';
end if;
perform set_config('app.fin_abgleich', 'an', true);
update public.auftraege
set start_fin = v_fin, start_fin_status = v_status
where id = p_auftrag_id;
perform set_config('app.fin_abgleich', '', true);
return v_status;
end;
$$;

View File

@ -1,5 +1,5 @@
begin;
select plan(16);
select plan(17);
insert into public.firmen (id, name) values ('00000000-0000-0000-0000-00000000f002', 'Firma B');
@ -105,5 +105,23 @@ select throws_ok(
'23505', 'duplicate key value violates unique constraint "fahrzeuge_firma_fin_key"',
'FIN unter sichtbaren bleibt eindeutig, Indexname unveraendert');
-- ------------------------------------------------- FIN-Abgleich ignoriert ausgeblendete
insert into public.fahrzeuge (id, kennzeichen, fin, firma_id, ausgeblendet_am) values
('5d300000-0000-0000-0000-000000000004', 'M-SL 004', 'WDB9066331S100004', '00000000-0000-0000-0000-00000000f001', now());
insert into public.fahrzeuge (id, kennzeichen, fin, firma_id) values
('5d300000-0000-0000-0000-000000000005', 'M-SL 005', null, '00000000-0000-0000-0000-00000000f001');
insert into public.auftraege (id, abholort_id, zielort_id, fahrzeug_id, erstellt_von, status, zugewiesen_an, firma_id) values
('5d400000-0000-0000-0000-000000000002', '5d200000-0000-0000-0000-000000000001', '5d200000-0000-0000-0000-000000000002',
'5d300000-0000-0000-0000-000000000005', '5d100000-0000-0000-0000-000000000001', 'zugewiesen',
'5d100000-0000-0000-0000-000000000003', '00000000-0000-0000-0000-00000000f001');
set local role authenticated;
set local request.jwt.claims to '{"sub": "5d100000-0000-0000-0000-000000000003", "role": "authenticated"}';
select is(
public.fin_bei_fahrtbeginn('5d400000-0000-0000-0000-000000000002', 'WDB9066331S100004')::text,
'nachgetragen',
'FIN eines ausgeblendeten Fahrzeugs gilt beim Fahrtbeginn nicht als Abweichung');
reset role;
select * from finish();
rollback;

View File

@ -84,7 +84,10 @@ export function FahrzeugZeile({
</span>
<p className="text-asphalt-grey">{fahrzeug.modell ?? "—"}</p>
</div>
<div role="cell" className="mt-3 md:mt-0">
<div
role="cell"
className={`mt-3 md:mt-0 ${loeschenFragen || fehler ? "md:col-span-full md:pt-2" : ""}`}
>
{fehler && !bearbeiten && (
<div className="mb-2">
<Alert>{fehler}</Alert>

View File

@ -86,7 +86,10 @@ export function StandortZeile({
<p className="text-xs text-asphalt-grey/70">{ort.email}</p>
)}
</div>
<div role="cell" className="mt-3 md:mt-0">
<div
role="cell"
className={`mt-3 md:mt-0 ${loeschenFragen || fehler ? "md:col-span-full md:pt-2" : ""}`}
>
{fehler && !bearbeiten && (
<div className="mb-2">
<Alert>{fehler}</Alert>