From ec5fd78e3e2f86fff7b71c0777315265ac133739 Mon Sep 17 00:00:00 2001 From: Mattsson <111893710+mattssonn@users.noreply.github.com> Date: Mon, 27 Apr 2026 14:38:34 +0200 Subject: [PATCH] Fix/fy and timeout (#364) * fix: ensure corrected entries retain original entry date for storno and correction types * feat: add script to repair entry_date misalignment for storno and correction entries * fix: update correction entry message for clarity and remove outdated script --- .../bookkeeping/CorrectionEntryDialog.tsx | 3 +++ .../__tests__/storno-service.test.ts | 27 ++++++++++++++++--- lib/core/bookkeeping/storno-service.ts | 6 ++--- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/components/bookkeeping/CorrectionEntryDialog.tsx b/components/bookkeeping/CorrectionEntryDialog.tsx index 62a25334..b7a434f7 100644 --- a/components/bookkeeping/CorrectionEntryDialog.tsx +++ b/components/bookkeeping/CorrectionEntryDialog.tsx @@ -156,6 +156,9 @@ export default function CorrectionEntryDialog({ entry, open, onOpenChange, onCor
  • En stornoverifikation som nollställer den ursprungliga
  • En ny verifikation med dina rättade uppgifter
  • +

    + Rättelsen bokförs i samma räkenskapsperiod som originalet — du hittar den under originalets räkenskapsår. +

    {/* Original entry (read-only) */} diff --git a/lib/core/bookkeeping/__tests__/storno-service.test.ts b/lib/core/bookkeeping/__tests__/storno-service.test.ts index e1e5add0..2bcbf7c9 100644 --- a/lib/core/bookkeeping/__tests__/storno-service.test.ts +++ b/lib/core/bookkeeping/__tests__/storno-service.test.ts @@ -9,12 +9,17 @@ import { BookkeepingDatabaseError } from '@/lib/bookkeeping/errors' let resultIdx: number let results: Array<{ data?: unknown; error?: unknown }> +let inserts: Array<{ table: string; payload: unknown }> -function makeBuilder() { +function makeBuilder(table: string) { const b: Record = {} - for (const m of ['select', 'eq', 'in', 'insert', 'update', 'delete']) { + for (const m of ['select', 'eq', 'in', 'update', 'delete']) { b[m] = vi.fn().mockReturnValue(b) } + b.insert = vi.fn().mockImplementation((payload: unknown) => { + inserts.push({ table, payload }) + return b + }) b.single = vi.fn().mockImplementation(async () => results[resultIdx++] ?? { data: null, error: null }) b.then = (resolve: (v: unknown) => void) => resolve(results[resultIdx++] ?? { data: null, error: null }) return b @@ -22,7 +27,7 @@ function makeBuilder() { function makeClient() { return { - from: vi.fn().mockImplementation(() => makeBuilder()), + from: vi.fn().mockImplementation((table: string) => makeBuilder(table)), rpc: vi.fn().mockImplementation(async () => results[resultIdx++] ?? { data: null, error: null }), } } @@ -30,7 +35,6 @@ function makeClient() { vi.mock('@/lib/bookkeeping/engine', () => ({ validateBalance: vi.fn().mockReturnValue({ valid: true, totalDebit: 1000, totalCredit: 1000 }), getNextVoucherNumber: vi.fn(async () => ++resultIdx), // just increment - getSwedishLocalDate: vi.fn().mockReturnValue('2024-06-15'), })) import { correctEntry } from '../storno-service' @@ -41,6 +45,7 @@ beforeEach(() => { eventBus.clear() resultIdx = 0 results = [] + inserts = [] // Reset the mock implementations after clearAllMocks vi.mocked(validateBalance).mockReturnValue({ valid: true, totalDebit: 1000, totalCredit: 1000 }) @@ -193,6 +198,20 @@ describe('correctEntry', () => { ).rejects.toThrow(BookkeepingDatabaseError) }) + it('mirrors original.entry_date on storno + corrected entries (rättelsen stannar i ursprungsperioden)', async () => { + setupResults() + const supabase = makeClient() + await correctEntry(supabase as never, 'company-1', 'user-1', 'orig-1', correctedLines) + + const journalEntryInserts = inserts + .filter((i) => i.table === 'journal_entries') + .map((i) => i.payload as { entry_date: string; source_type: string }) + + expect(journalEntryInserts).toHaveLength(2) + expect(journalEntryInserts[0]).toMatchObject({ source_type: 'storno', entry_date: '2024-06-15' }) + expect(journalEntryInserts[1]).toMatchObject({ source_type: 'correction', entry_date: '2024-06-15' }) + }) + it('emits journal_entry.corrected event', async () => { setupResults() diff --git a/lib/core/bookkeeping/storno-service.ts b/lib/core/bookkeeping/storno-service.ts index 5b5ca2ce..fc53b19e 100644 --- a/lib/core/bookkeeping/storno-service.ts +++ b/lib/core/bookkeeping/storno-service.ts @@ -5,7 +5,7 @@ import type { JournalEntry, JournalEntryLine, } from '@/types' -import { validateBalance, getNextVoucherNumber, getSwedishLocalDate } from '@/lib/bookkeeping/engine' +import { validateBalance, getNextVoucherNumber } from '@/lib/bookkeeping/engine' import { AccountsNotInChartError, BookkeepingDatabaseError, @@ -99,7 +99,7 @@ export async function correctEntry( fiscal_period_id: original.fiscal_period_id, voucher_number: reversalVoucherNumber, voucher_series: original.voucher_series || 'A', - entry_date: getSwedishLocalDate(), + entry_date: original.entry_date, description: `Storno: ${original.description}`, source_type: 'storno', reverses_id: originalEntryId, @@ -195,7 +195,7 @@ export async function correctEntry( fiscal_period_id: original.fiscal_period_id, voucher_number: correctedVoucherNumber, voucher_series: original.voucher_series || 'A', - entry_date: getSwedishLocalDate(), + entry_date: original.entry_date, description: `Rättelse: ${original.description}`, source_type: 'correction', correction_of_id: originalEntryId,