Files
accounted/supabase/migrations/20260621131000_backfill_fiscal_period_previous_id.sql
Mattsson 241959513b Fix/mcp and req (#753)
* feat(api): test-mode API keys force dry-run on the v1 REST API

A key created with mode='test' (prefix gnubok_sk_test_) binds to the real
company, but the v1 wrapper forces dry_run on every write so nothing is
persisted or sent. Mutations on endpoints that can't be simulated
(dryRunSupported=false or unregistered) are refused with 403
TEST_KEY_WRITE_BLOCKED — fail-closed. Reads pass through unchanged and every
test-key response carries X-Gnubok-Mode: test. Live keys are unaffected
(mode defaults to 'live').

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* feat(invoices): company default "Vår referens" + per-line sales-account override

Add company_settings.default_our_reference (settings form, schema, type); the
invoice editor pre-fills our_reference from it on new invoices only, never
overwriting an edited draft. Separately, add an optional per-line
försäljningskonto (class-3) override in the editor — left blank, the engine
still derives the revenue account from the VAT rate, and reverse-charge/export
lines ignore the override.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* feat(invoices): render a Swish payment QR on invoice PDFs

Build the Swish "Type C" QR payload offline (no Swish API call) and embed it as
a PNG in the invoice PDF payment box when Swish display is enabled, the invoice
is in SEK, and the amount is positive. Also surface the invoice number in the
payment box. Wired through every PDF render path: send, mark-sent and pdf
routes (both legacy and v1), the recurring-schedule sender, and the staged-send
commit.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* feat(bookkeeping): draft exclusion + correction-chain collapse on verifikationslista

Extend list_fiscal_period_entries_with_related with two opt-in params:
p_exclude_draft (keep drafts off the committed list — they get their own
surface) and p_collapse_corrections (render a correction group as the single
live correction, hiding the mechanical storno and the reversed original).
Both default false; nothing is deleted, every voucher keeps its number, and a
"show all" toggle exposes the full chain.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(reports): link multi-year SIE periods so resultatrapport shows the prior year

SIE import now sets fiscal_periods.previous_period_id in both directions when
creating a period, so multi-year files chain correctly regardless of #RAR order.
A backfill migration repairs periods imported before this (idempotent; only
touches NULL links on first-of-month periods). generateResultatrapport falls
back to the date-adjacent prior period when the chain is still null, so the
comparison column works for legacy data too.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(articles): hide the VAT field for non-momsregistrerade companies

The article form reads company_settings.vat_registered and, when false, hides
the moms field and forces vat_rate to 0 on submit — mirroring the invoice
editor so a non-VAT-registered company never sets a rate it can't charge.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* feat(import): allow file-based imports in the sandbox

Bank-file, CSV/Excel and SIE imports run entirely on uploaded data with no
external service, so they're now reachable in the sandbox. Only the API-backed
options that need live third-party credentials (PSD2 bank connection, provider
migration) stay disabled. Updates the sandbox notice copy to match.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* feat(bookkeeping): add edit draft functionality for journal entries

* feat(database): add default "Vår referens" column to company_settings for invoicing

* fix(tests): set SHOW_SWISH_ON_INVOICE to false in PDF template mocks

* @
fix(payments): use roundOre for Swish amount formatting

Replace naive Math.round(x*100)/100 with roundOre from @/lib/money to
satisfy the antipattern guard.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-18 11:49:33 +02:00

42 lines
2.0 KiB
SQL

-- Backfill fiscal_periods.previous_period_id for periods created without it.
--
-- SIE import (lib/import/sie-import.ts ensureFiscalPeriod) historically inserted
-- fiscal periods without linking previous_period_id, so multi-year imports left
-- the BFNAR 2013:2 continuity chain broken. The resultatrapport prior-period
-- comparison walks that chain to find the prior year — with it null, the
-- comparison column showed only dashes. The balansrapport was unaffected (it
-- sums prior lines via compute_prior_opening_balances, not the chain).
--
-- This sets each period's previous_period_id to the chronologically closest
-- preceding period in the same company. Idempotent: only touches rows where the
-- link is currently NULL, so manually-chained periods are preserved and re-runs
-- are no-ops. No trigger maintains this column (enforce_opening_balance_
-- immutability only guards opening_balance_entry_id / closing_entry_id).
--
-- Guard: only periods that start on the 1st of a month are touched. The
-- enforce_first_of_month_for_subsequent_periods trigger fires BEFORE UPDATE and
-- re-validates period_start, rejecting any period that starts mid-month while an
-- earlier period exists (a legacy/förlängt period that is no longer the
-- chronologically first). Such a row would abort the whole set-based UPDATE.
-- They are rare and left NULL on purpose — generateResultatrapport falls back to
-- the date-adjacent prior period when previous_period_id is null, so the
-- comparison still works for them.
UPDATE public.fiscal_periods AS target
SET previous_period_id = (
SELECT p.id
FROM public.fiscal_periods p
WHERE p.company_id = target.company_id
AND p.period_end < target.period_start
ORDER BY p.period_end DESC
LIMIT 1
)
WHERE target.previous_period_id IS NULL
AND EXTRACT(DAY FROM target.period_start) = 1
AND EXISTS (
SELECT 1
FROM public.fiscal_periods p2
WHERE p2.company_id = target.company_id
AND p2.period_end < target.period_start
);