Files
accounted/supabase/migrations/20260904190000_supplier_credit_note_not_payable.sql
T
e684606b23 fix(supplier-invoices): a credit note is never a payable, so it never waits for attest (#2289)
* fix(supplier-invoices): a credit note is never a payable, so it never waits for attest

A supplier credit note created with Kreditera was inserted at 'registered',
the attest entry state, while the detail page (rightly) offered no attest
for it. The worklist counted every 'registered' row, so Att göra showed
"1 leverantörsfaktura att attestera" that nobody could clear (support case
2026-09-04; 14 such rows on prod plus one MCP-approved credit note).

- One row builder (lib/supplier-invoices/credit-note.ts) for the dashboard
  route, the MCP executor and the v1 API: the credit note rests at
  'credited' from birth, the status the provider importers already use.
- CHECK supplier_invoices_credit_note_not_payable keeps every writer out of
  the payable states; migration backfills the stuck rows (one immutable
  reset-source row skipped, hence NOT VALID).
- The worklist attest count excludes credit notes explicitly.
- GET /api/supplier-invoices/[id] hydrates credited_original with a second
  scoped query: PostgREST cannot pick a direction for a self-referencing
  embed hint and returned the one-to-many side (an empty array), which the
  page rendered as "Krediterar: Ankomst #" with no number.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01S62AGZwsMoBc8x8obBDVqE

* test(supplier-invoices): type the GET route response in the credited_original tests

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01S62AGZwsMoBc8x8obBDVqE

* fix(migration): NOT EXISTS instead of NOT IN for the reset-source exclusion

A NULL source_company_id in the subquery would make NOT IN never true and
silently skip the whole backfill.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01S62AGZwsMoBc8x8obBDVqE

* test(schema): raise the unresolved-payload ceiling by 3 for the credit-note row builder

The three credit-note creation paths now insert the row from one builder,
so the scanner sees three dynamic payloads instead of three literals. The
columns are the literal in lib/supplier-invoices/credit-note.ts, pinned by
its test; the resting status is additionally held by the DB CHECK.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01S62AGZwsMoBc8x8obBDVqE

* test(pg): credit-note fixtures in the overdue-cron tests rest at credited

The two fixtures that seeded a credit note on 'registered'/'overdue' now
violate supplier_invoices_credit_note_not_payable. The cron test seeds the
credit note the way the routes create it since 20260904190000; the 20260607
backfill case asserts the scenario it repaired is now unreachable.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01S62AGZwsMoBc8x8obBDVqE

* test(pg): ledger-usage-stats seeds its credit note at credited

The fixture defaulted every row to 'registered', which the CHECK
supplier_invoices_credit_note_not_payable now refuses for a credit note.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01S62AGZwsMoBc8x8obBDVqE

---------

Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com>
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-05 09:43:00 +02:00

43 lines
2.0 KiB
SQL

-- Supplier credit notes are reversals, not payables (support case 2026-09-04).
--
-- A kreditfaktura from a supplier is created from the original invoice with
-- Kreditera: the reversing verifikat is booked in the same request and the
-- original moves to 'credited'. Nothing is left to attest or to pay, yet the
-- credit note was inserted at the payable lifecycle's entry state,
-- 'registered', which every consumer reads as "waiting for attest". The
-- worklist counted it as a supplier invoice to attest while the detail page
-- (correctly) offered no attest for a credit note, so the item could never be
-- cleared. 'approved' was reachable the same way through the MCP approve
-- executor.
--
-- 1. Move every stuck credit note to 'credited', the status the provider
-- importers already give incoming credit notes. Archived migration-reset
-- sources are skipped: block_migration_reset_source_mutation() makes their
-- rows immutable and would abort the migration.
-- 2. Add a CHECK so no writer (dashboard, MCP executor, v1 API, importers)
-- can put a credit note into a payable state again. NOT VALID because the
-- archived reset-source row above cannot be repaired; the constraint is
-- enforced on every new insert and update regardless.
-- pg-test: covered-by tests/pg/supplier-credit-note-not-payable.pg.test.ts
UPDATE public.supplier_invoices
SET status = 'credited'
WHERE is_credit_note
AND status IN ('registered', 'approved', 'overdue')
AND NOT EXISTS (
SELECT 1 FROM public.company_migration_resets r
WHERE r.source_company_id = supplier_invoices.company_id
);
ALTER TABLE public.supplier_invoices
ADD CONSTRAINT supplier_invoices_credit_note_not_payable
CHECK (
NOT is_credit_note
OR status NOT IN ('registered', 'approved', 'overdue', 'paid', 'partially_paid')
) NOT VALID;
COMMENT ON CONSTRAINT supplier_invoices_credit_note_not_payable ON public.supplier_invoices IS
'A supplier credit note is a reversal, never a payable: it cannot use the attest or payment lifecycle states.';
NOTIFY pgrst, 'reload schema';