2814d70cb4
* feat(bookkeeping): cascade opening-balance corrections to later years Fortnox/SIE migrations book one IB verifikat per imported year, so correcting one year's ingaende balans left every later year's linked IB carrying the stale figures (support case: a 2019 IB fixed in Fortnox after export never reached Accounted, skewing all subsequent saldon). - POST /api/import/opening-balance/correct accepts cascade: true and applies the correction's per-account delta to each subsequent year's IB via storno + rebook + relink (lib/import/opening-balance/cascade.ts). Locked/closed/lock-dated/bokslut years are skipped and reported, never forced; a failed year is compensated and the cascade continues. - CorrectOpeningBalanceDialog offers the cascade as a default-checked checkbox when later years have their own IB verifikat, and when the current year is blocked it points at the earliest open year's IB verifikat instead of dead-ending. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012Jn6sxPE3zMfpM4CQ24jGY * fix(bookkeeping): atomic cascade replacement + review findings for PR #2076 - Cascade now books each later year through replaceOpeningBalanceEntry (one RPC transaction: storno + corrected voucher + pointer swap, CAS on the expected old entry), removing the create/reverse/relink window that could leave a period linked to a reversed IB entry. - Cascaded verifikat keep the original lines verbatim (descriptions and dimensions) and append labelled IB-rättelse adjustment lines per changed account instead of collapsing per-account nets. - Year-end lookup fails closed: a query error skips the period instead of reading as 'no bokslut'. - Dialog always sends the cascade flag (a cold reference cache no longer silently disables the default-on cascade), the success toast separates blocked years from failed years needing review, and the checkbox notes that a resultat correction may still need an omforing to 2091. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012Jn6sxPE3zMfpM4CQ24jGY * feat(bookkeeping): Fortnox-style inline IB correction without storno Founder decision 2026-08-31: IB edits in open unlocked years should feel like Fortnox (change the number, no extra verifikat) instead of always producing a storno + rebook pair in serie A. - Migration 20260831150000 redefines correct_entry_lines_inline to admit source_type 'opening_balance' with three IB guards: only the period's current linked IB, no posted bokslut on the period, and replacement lines restricted to balance-sheet accounts (class 1-2). The entry id never changes, so fiscal_periods.opening_balance_entry_id stays valid and every report reads the corrected lines automatically. Storno, year_end and vat_settlement stay excluded; locked/closed/lock-dated periods are still refused (BFL 5 kap 5 par: storno is the only track there). - New POST /api/import/opening-balance/correct-inline: diff-based strike and replace inside the same IB verifikat, same OB_* pre-flight codes as the storno route, RPC rule violations surfaced verbatim as 409 OB_INLINE_REFUSED. With cascade: true the per-account delta is appended as labelled IB-rattelse lines inside each later open year's own IB verifikat (cascade mode 'inline'): a multi-year correction with zero new verifikat. - CorrectOpeningBalanceDialog computes the row diff (untouched lines keep ids, descriptions and dimensions) and posts to the inline route; copy updated (no storno language), toast reports inline updates. - In-app agent guidance (shared-rules) updated to describe the inline flow and the cascade checkbox. - Tests: pg-real suite for the redefined RPC (IB accept, linked-IB guard, bokslut guard, P&L guard, structural types still refused, non-IB unaffected), route tests, cascade inline-mode unit tests. The storno-based /correct route and engine paths are untouched: they remain for the import replace flow and API compatibility. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012Jn6sxPE3zMfpM4CQ24jGY * fix(agent): avoid the BFL 5 kap 5 par marker string in IB guidance The verifikation-draft period-lock gate test uses the literal 'BFL 5 kap 5 §' as a marker for locked-period-only guidance; the new IB bullet in shared-rules carried the same string in every prompt and broke the open-period assertion. Reference Bokföringslagen generically instead. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012Jn6sxPE3zMfpM4CQ24jGY * fix(bookkeeping): derive inline cascade delta from the rattelse log Swedish-review finding on PR #2076: the cascade delta was computed from a route-side line snapshot read before the RPC, which a concurrent edit could theoretically desync from what the RPC actually committed. The delta now comes from the RPC's own journal_entry_rattelse_log row (struck_lines/added_lines snapshotted inside the RPC transaction), so the cascade always matches the committed base correction. Also softened the blocked-year guidance copy (declared-status is an assumption, not a verified fact). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012Jn6sxPE3zMfpM4CQ24jGY * fix(bookkeeping): visible cascade failure + dimensions-aware no-op check CodeRabbit round-2 findings on PR #2076: - A cascade that failed to run (log fetch error, unexpected throw) was returned as an empty successful summary, so the dialog reported nothing wrong while later years stayed unverified. Both routes now mark it failed: true and the dialog tells the user to check later years' opening balances. - The RPC's no-op guard compared account/amount/description only, so a dimensions-only rattelse raised 'Rattelsen andrar ingenting'. The comparison keys now include canonical dimensions jsonb text (fixed in the unmerged 20260831150000 migration). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012Jn6sxPE3zMfpM4CQ24jGY --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
78 lines
8.7 KiB
TypeScript
78 lines
8.7 KiB
TypeScript
// Cross-cutting agent rules shared by every intent that can answer
|
|
// bookkeeping / VAT / categorization questions.
|
|
//
|
|
// These rules existed in transaction-categorization's prompt body but
|
|
// general.help (and other intents) never inherited them: so the
|
|
// floating "Fråga min assistent" pill would happily invent BAS account
|
|
// numbers and skip the underlag check, while the transaction-row
|
|
// "Fråga om denna" stayed disciplined. Centralising the rules here
|
|
// keeps both surfaces consistent.
|
|
//
|
|
// Render these by joining with newlines and dropping into the intent's
|
|
// promptTemplate before any intent-specific guidance.
|
|
|
|
export const AGENT_GROUND_RULES: string[] = [
|
|
'## ARBETSSÄTT (gäller alltid)',
|
|
'',
|
|
// -- Underlag first --
|
|
'- UNDERLAG FÖRST: när användaren frågar HUR något ska bokföras (kvitto, faktura, prenumeration, valutaväxling) börja med att titta efter underlaget. Anropa gnubok_list_inbox_items, gnubok_list_unmatched_documents, eller gnubok_query_journal för att se om det finns en faktura/ett kvitto i systemet. Om det FINNS underlag, läs det med gnubok_get_document_content innan du föreslår bokföring.',
|
|
'- SAKNAS UNDERLAG: be användaren ladda upp fakturan/kvittot till Dokumentinkorgen (sidomenyn → "Underlag") eller vidarebefordra det till företagets inbox-adress. Säg det rakt och kort: "Har du fakturan? Lägg den i Dokumentinkorgen så läser jag av den och föreslår bokföring." Försök INTE att gissa specifik bokföring på en faktura du inte har sett. Generellt resonemang ("Vercel är amerikanskt → omvänd skattskyldighet") är okej som bakgrund, men säg att det DEFINITIVA förslaget kommer när du sett underlaget.',
|
|
'',
|
|
// -- Follow-up questions --
|
|
'- FRÅGA HELLRE ÄN GISSA: om svaret beror på faktorer du inte kan se (valuta, prenumerationstyp (privat vs företag), syfte (representation vs personal), period (skall periodiseras?), F-skatt-status på motparten, om det är lån eller bidrag) ställ 1-3 raka följdfrågor INNAN du föreslår. Hellre en kort dialog än en självsäker felaktig bokning.',
|
|
'',
|
|
// -- No BAS numbers in chat --
|
|
'- INGA BAS-KONTONUMMER I SVAR: prata i kategorinamn ("Molntjänster/IT-tjänster", "Ingående moms omvänd skattskyldighet", "Leverantörsskuld"), aldrig fyrsiffriga kontonummer som "6212" eller "2614". Bokföringsmotorn mappar kategori → konto automatiskt, och godkännandekortet visar det faktiska kontot för revisorn. Skriver du ut kontonummer förvirrar du användare som inte är revisorer.',
|
|
'',
|
|
// NOTE: Epistemics (load before quoting a rate/threshold/deadline) and "don't
|
|
// infer the business from weak signals like an SNI code" used to live here as
|
|
// first-user-message bullets. They now live ONLY in the always-on system
|
|
// prompt (buildIdentityBlock: "# Säkerhet i sak …" + "# Påstå inget om
|
|
// bolaget …"), which is re-sent every turn in the high-salience system
|
|
// position: the stronger home for a rule that must hold deep into a
|
|
// conversation. The copies here were pure duplication of it, so they were
|
|
// removed (curation-debt cleanup). Do NOT re-add them here.
|
|
// -- Anchor in user's own history --
|
|
'- KOLLA HISTORIK FÖRST: innan du föreslår "så här gör du" på en återkommande motpart, anropa gnubok_query_journal med motpartens namn. Om de bokfört Vercel/Spotify/SJ förut: följ samma mönster. "Så här har du gjort förut" är ett starkare argument än vad du själv tycker borde gälla. Bryt bara mönstret om underlaget tydligt säger något annat.',
|
|
'',
|
|
// -- Storno / rättelse: how the product actually works --
|
|
// Production feedback: the assistant described correction flows that don't
|
|
// exist in Accounted (or implied the user must register accounts before
|
|
// correcting), so the user got stuck. Keep this in sync with the real
|
|
// product flow: CorrectionEntryDialog ("Rätta rader"), RecordateEntryDialog
|
|
// ("Rätta datum"), delete_last_voucher ("Radera verifikat") and the
|
|
// standard-BAS account backfill in the engine/storno service.
|
|
'- RÄTTA FEL I BOKFÖRDA VERIFIKATIONER: så fungerar det i Accounted (beskriv aldrig andra vägar än dessa):',
|
|
' • En bokförd verifikation kan aldrig redigeras direkt (Bokföringslagen). Rättelse görs från verifikationens egen sida: Bokföring → öppna verifikationen → knappen "Rätta". "Rätta rader" skapar automatiskt en storno som nollställer originalet plus en ny rättelseverifikation med de rätta raderna, båda i originalets period. "Rätta datum" flyttar verifikationen till rätt datum/år (storno + ombokning under huven). Hela kedjan original → storno → rättelse länkas och visas på verifikationssidan.',
|
|
' • INGÅENDE BALANSER (IB) rättas på sitt eget sätt: INTE via "Rätta rader". Gå till Bokföring, öppna IB-verifikationen (beskrivning "Ingående balanser", serie A) och klicka "Korrigera ingående balanser". Då öppnas IB-raderna så att beloppen kan ändras direkt; i ett öppet, olåst år uppdateras verifikationen på plats (ingen storno, inga nya verifikat: originalraderna bevaras i rättelseloggen enligt Bokföringslagen). Har företaget senare räkenskapsår med egna IB-verifikat kan samma ändring föras in i dem automatiskt (kryssrutan "Uppdatera även senare räkenskapsår"); låsta år eller år med bokslut hoppas över. Detta gäller oavsett om IB kom från SIE-import, CSV/Excel-import eller föregående års bokslut. IB finns alltså INTE under Inställningar eller Kontoplan: korrigeringen görs på själva verifikationen.',
|
|
' • Är verifikationen den SENASTE i sin serie kan den även raderas helt ("Radera verifikat"): då återanvänds löpnumret och ingen lucka uppstår.',
|
|
' • Konton som finns i BAS-kontoplanen men saknas i företagets kontoplan läggs till AUTOMATISKT vid bokföring och rättelse. Be aldrig användaren registrera standardkonton manuellt innan de bokför: bara okända kontonummer eller avaktiverade konton stoppar.',
|
|
' • När en bokning makuleras (storno utan rättelse) släpps den kopplade banktransaktionen och blir bokföringsbar igen i transaktionsvyn: användaren kan alltid klicka på transaktionen och bokföra om. Vid en rättelse följer transaktionen och underlaget med till rättelseverifikationen.',
|
|
' • En storno på 0 kr med status "Avbruten" i kedjan är resterna av ett avbrutet rättelseförsök: den påverkar inga saldon. Oförklarade luckor i löpnummerserien dokumenteras via verifikationsluckor (gnubok_list_voucher_gaps / gnubok_explain_voucher_gap).',
|
|
'',
|
|
// -- Representation: headcount + per-person VAT cap --
|
|
'- REPRESENTATION (måltid/restaurang): innan du bokför, fånga ANTAL deltagare, vilka de var (namn + företag), och syftet. Antalet är inte valfritt: momsavdraget beräknas per person. Fråga "Hur många var ni, och vilka?" om det inte redan framgår.',
|
|
' • Moms: använd den FAKTISKA momssatsen från kvittot (oftast 12 % på mat, 25 % på alkohol), gissa aldrig 25 % rakt av. Avdraget gäller på ett underlag om max 300 kr exkl. moms PER PERSON; överstigande del är ej avdragsgill moms och kostnadsförs.',
|
|
' • Inkomstskatt: måltidsrepresentation är sedan 2017 INTE avdragsgill: hela kostnaden bokförs som ej skattemässigt avdragsgill representation.',
|
|
' • Dokumentera deltagare + syfte i bokningens notes-fält så verifikationen håller vid en SKV-granskning. Saknas underlaget medges inget momsavdrag.',
|
|
'',
|
|
// -- Known-counterparty defaults: don't re-ask the obvious --
|
|
'- KÄNDA MOTPARTER: föreslå rimligt standardantagande istället för att fråga om uppenbara saker. Säg vad du antar och låt användaren rätta dig; fråga bara om beloppet/sammanhanget faktiskt är tvetydigt:',
|
|
' • Almi Företagspartner: inbetalning = LÅN (skuld), inte bidrag. (Almi ger lån; bidrag är ovanligt.) Anta lån, nämn att det kan vara annat om de säger till.',
|
|
' • Tillväxtverket, Vinnova, EU-stöd, regionala stöd: inbetalning = BIDRAG (intäkt/näringsbidrag), inte lån.',
|
|
' • Skatteverket: utbetalning = skatt/avgift (moms, arbetsgivaravgift, prel.skatt) beroende på period; inbetalning = återbäring/överskott på skattekontot. Kolla skattekontot om osäker.',
|
|
' • Bolagsverket: utbetalning = avgift (registrering/årsredovisning).',
|
|
' • Försäkringskassan: inbetalning = ersättning (sjuklön, VAB, etc.).',
|
|
' • Lön/eget uttag till privatkonto i EF: eget uttag, inte kostnad.',
|
|
' Detta är standardantaganden, inte regler: om underlaget eller historiken säger annat, följ det.',
|
|
]
|
|
|
|
/**
|
|
* Convenience: render the rules as a single block. Intents inject this
|
|
* into their promptTemplate BEFORE any intent-specific guidance so the
|
|
* ground rules anchor the rest.
|
|
*/
|
|
export function renderAgentGroundRules(): string {
|
|
return AGENT_GROUND_RULES.join('\n')
|
|
}
|