fix: use range overlap for duplicate SIE period check (BFL 4:1) (#163)

* fix: use range overlap for duplicate period check (BFL 4:1 compliance)

The period duplicate check used exact-match on fiscal_year_start/end, missing
overlapping periods (e.g., partial-year file vs full-year import). Now uses
standard interval overlap test (start <= other_end AND end >= other_start).

Updated Swedish error messages to reflect overlap semantics.

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

* fix: align JSDoc citation to BFL 4:1 for period overlap check

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

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jakob Wennberg
2026-04-02 12:21:42 +02:00
committed by GitHub
co-authored by Claude Opus 4.6
parent d23cb4c859
commit 8855ef1485
2 changed files with 8 additions and 5 deletions
+1 -1
View File
@@ -79,7 +79,7 @@ export async function POST(request: Request) {
if (periodDuplicate) {
return NextResponse.json({
error: 'duplicate_period',
message: `En SIE-import för perioden ${parsed.stats.fiscalYearStart} – ${parsed.stats.fiscalYearEnd} finns redan (importerad ${periodDuplicate.imported_at ? new Date(periodDuplicate.imported_at).toLocaleDateString('sv-SE') : 'okänt datum'})`,
message: `En SIE-import för ett överlappande räkenskapsår (${periodDuplicate.fiscal_year_start} – ${periodDuplicate.fiscal_year_end}) finns redan (importerad ${periodDuplicate.imported_at ? new Date(periodDuplicate.imported_at).toLocaleDateString('sv-SE') : 'okänt datum'})`,
importId: periodDuplicate.id,
}, { status: 409 })
}
+7 -4
View File
@@ -104,7 +104,7 @@ export async function checkDuplicateImport(
/**
* Check if a completed SIE import already exists for the same fiscal year period.
* Prevents importing two different SIE files that cover the same accounting period,
* which would create duplicate verifikationer violating BFNAR 2013:2.
* which would create duplicate verifikationer violating BFL 4:1 (löpande bokföring).
* Only blocks on status='completed' — failed/pending imports don't prevent retries.
*/
export async function checkDuplicatePeriodImport(
@@ -113,13 +113,16 @@ export async function checkDuplicatePeriodImport(
fiscalYearStart: string,
fiscalYearEnd: string
): Promise<SIEImport | null> {
// Range overlap check: start <= other_end AND end >= other_start.
// Two imports whose räkenskapsår overlap would produce duplicate
// verifikationer, violating BFL 4:1 (löpande bokföring).
const { data } = await supabase
.from('sie_imports')
.select('*')
.eq('company_id', companyId)
.eq('fiscal_year_start', fiscalYearStart)
.eq('fiscal_year_end', fiscalYearEnd)
.eq('status', 'completed')
.lte('fiscal_year_start', fiscalYearEnd)
.gte('fiscal_year_end', fiscalYearStart)
.limit(1)
.maybeSingle()
@@ -1334,7 +1337,7 @@ export async function executeSIEImport(
)
if (periodDuplicate) {
result.errors.push(
`En SIE-import för perioden ${fiscalYearStart} – ${fiscalYearEnd} finns redan (ID: ${periodDuplicate.id})`
`En SIE-import för ett överlappande räkenskapsår (${periodDuplicate.fiscal_year_start} – ${periodDuplicate.fiscal_year_end}) finns redan`
)
return result
}