f3eacb436d
* fix(security): gate replace_sie_import behind owner/admin membership The RPC was SECURITY DEFINER with EXECUTE granted to PUBLIC and anon, no company_members lookup, no auth.uid() reference and no unauthorized raise, while setting gnubok.allow_delete to disarm the BFL immutability and retention triggers. Any caller holding a company_id and an import id could hard delete another tenant's verifikationer. Confirmed live in production. Applies the same fail closed owner/admin guard that undo_sie_import already carries (migration 20260624120000), resolving the actor from COALESCE(p_user_id, auth.uid()) so it denies when the role is NULL, then revokes EXECUTE from PUBLIC and anon. search_path and the raised statement_timeout are restated, since CREATE OR REPLACE drops settings that are not repeated. userId is a required parameter on replaceSIEImport: the service client has a NULL auth.uid(), so a caller without an explicit actor now fails to compile rather than hitting the closed gate at runtime. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix(security): validate arcim OAuth callback state server side The callback route is skipAuth and decoded the state parameter as plain base64url JSON, trusting consentId and provider from it. A one time code was minted at flow start and never read. An unauthenticated attacker who learned a consent id could run an OAuth flow on their own provider account and post the callback with a forged state, landing their tokens on another tenant's consent, so the victim's next migration imported the attacker's ledger. State is now an opaque randomBytes(32) pointer to a provider_otc row, consumed by a single atomic UPDATE guarded on used_at IS NULL and expires_at, so a replay loses the row lock race and updates nothing. provider is read from provider_consents rather than trusted from the client. provider_otc already existed for exactly this purpose and was never wired up. Also scopes getConsent to an owning company, closing a cross tenant status oracle where the preview and migrate paths echoed a consent's status before the scoped check ran. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix(security): scope documents storage to company_id (phase A) The documents bucket policies matched on auth.uid(), and upload keys were documents/{userId}/..., so company membership was never consulted. Removing a member revoked nothing: their session still authenticated and they kept direct Storage read access to every receipt, supplier invoice and bank statement they had uploaded. The same bug was fixed for sie-files in 20260416120000; this bucket was left behind. Phase A is additive. Company scoped policies are added alongside the uploader scoped ones, uploads move to documents/{companyId}/{userId}/..., and reads accept either layout so nothing breaks mid migration. Phase C, which drops the old policies, is gated on the backfill reporting zero remaining legacy prefix objects. The policy compares the company segment as text rather than casting to uuid the way sie-files does: this bucket holds keys whose second segment is not a uuid (MCP audit packages), and Postgres does not guarantee the bucket prefix qual runs before the cast, so a planner reordering would raise 22P02 and fail the whole query instead of filtering the row out. deleteDocument now removes both candidate keys. Removing only the stored pointer would leave a readable orphan copy of a document the user asked to erase. The backfill script is included but has never been run. It defaults to dry run, refuses .env.local by name, and verifies each copy is readable and SHA-256 identical before repointing the row. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix(security): enforce events:read scope and membership on /api/events This was the only one of the three validateApiKey call sites with no downstream guard: v1 and the MCP server both check scope and re-verify company membership, this route did neither. An events:read scope existed and was documented as gating the endpoint but was never called, so a legacy key falling back to DEFAULT_SCOPES read the full log. The bound company id went straight from the api_keys row into a service role query, so a key whose user had been removed from the company kept reading. Adds the scope check before any database access, re-verifies company_members with archived_at IS NULL, honours test mode by stamping X-Gnubok-Mode instead of ignoring it, applies minimisePayload so the pull surface can never return a wider payload than the push surface, and replaces the three flat error strings with the canonical envelope. Test key reads are served rather than blocked: TEST_KEY_WRITE_BLOCKED is gated on mutations in with-api-v1, so a read gets the same treatment as every other v1 read endpoint. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * perf(bookkeeping): sweep remaining journal_entries!inner embeds A previous refactor removed this pattern from lib/reports and introduced fetchEntryLines, but the class was never swept. Seventeen sites remained and had become the top application consumer of production database time: measured across the resulting query shapes, 32,694 calls and 25,848 seconds of execution, mean 790ms, with shapes averaging 2.6s and 3.0s and maxing at 7,962ms against the 8s statement_timeout, which surfaced to users as 500s on the booking path. PostgREST compiles an embed with filters on the embedded side into a correlated INNER JOIN LATERAL with a parameterized LIMIT, which stops Postgres reordering the join, so each query walked the whole journal_entry_lines table across all tenants. Driving from the entries side instead turns that into two indexed round trips. Converted sites keep their existing shape: the helper reattaches the parent entry under the same key the embed produced. Several conversions also remove a latent silent truncation where an unpaginated query was capped at PostgREST's 1000 row ceiling. Two deliberate exceptions. The free text ilike legs of the MCP display query stay on the embed, because each is capped at legLimit and that cap drives the truncation contract the tool reports, while the helper is unbounded. The accounts route moves to the existing get_account_usage_counts RPC instead, since its embed was a head count and the helper returns rows. commitEntry's write path is untouched: the change there is confined to the read query of the pre-commit dimension rule check. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix(api): anchor v1 list cursors on created_at Page two returned page one, forever, while still advertising a fresh next_cursor. The three routes sorted by and encoded a Postgres date column, which serializes as YYYY-MM-DD, but decodeDefaultCursor validates the cursor timestamp as full ISO-8601 and returned null, so the keyset filter was never applied and has_more never went false. An integrator syncing verifikat looped on the newest rows indefinitely. The transactions route already solved this and its comment names the trap; the fix was never ported. All three now order and encode on created_at with an id tie break, matching the transactions keyset predicate exactly. ISO_TIMESTAMP is deliberately left alone: relaxing it would silently change sort semantics on the route that currently works. Default ordering therefore moves from business date to insert order. Every business date is still on the row, and the invoices list gains date_from and date_to filters so a date range is still reachable; the other two already had them. The tests use an in-memory PostgREST that actually evaluates the filters, because the repo's pass-through mock cannot catch this class of bug: the bug is that the filter is never sent. They walk to exhaustion with a hard iteration cap, so an unterminated walk fails instead of hanging. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix(api): separate dry run from commit in the idempotency hash The request hash was built from url.pathname, which excludes the query string, so a dry run and its commit hashed identically. Following the flow documented in dry-run.ts, re-issuing the request with the same Idempotency-Key returned the cached preview with Idempotent-Replayed set and wrote nothing, while reporting 200. An agent or integrator saw success for a write that never happened. dry_run is folded into the hash only when true, not as an unconditional boolean. Including it as false would change the hash of every ordinary write, and with a 24h idempotency TTL any key in flight across the deploy would fail the request_hash comparison and 409 on a legitimate retry. Both hash call sites now go through one shared helper so they cannot drift into a permanent cache miss, and dry run responses are no longer stored at all. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * ci: install the Bedrock SDK out of tree in the compliance review The Swedish accounting compliance gate had failed ten consecutive runs and so was posting nothing. With --no-package-lock npm discarded the lockfile and re-resolved the whole tree from package.json, floating @hookform/resolvers to 5.4.3, whose valibot ^1 peer conflicts with the pinned valibot 0.39.0. Installing into the parent of the checkout resolves only that one package, so an unrelated peer conflict can never take the gate down again. Node still finds it because ESM bare specifiers walk up parent node_modules; NODE_PATH would not have worked, as it is CommonJS only. --legacy-peer-deps was rejected because it masks future genuine peer conflicts and still reifies the full tree. The same step's SDK version is aligned from 0.31.0 back to the 0.29.1 that package.json and check:guards enforce after the streaming outage. That drift went unnoticed because the pin guard only inspects package.json and the lockfile, never workflow files. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * build(docker): generate crontabs from vercel.json vercel.json defines 16 cron jobs; both Docker crontabs carried 9, and were byte identical to each other. Self hosted deployments therefore never sent recurring invoices, never dispatched webhooks and never cleaned up idempotency keys. tax-deadlines also ran once a year on 2 January instead of daily, and documents/verify weekly instead of daily. Extension crons are included rather than excluded. The Dockerfile copies the whole tree before building, so every extension cron route is compiled into the image regardless of the enabled preset, and each returns 200 when its extension is unconfigured, so curl -sf logs no failure. Two such entries were already present in the crontab for extensions absent from the preset, which settles the intent. documents/verify is treated as drift rather than a self hosted concession: the weekly cadence was present in the hosted crontab too, and the run is capped at 200 documents walking a nulls-first queue, so weekly drains the integrity queue seven times slower on a check that exists for BFL retention. webhooks/dispatch keeps its per minute cadence, adding 1,440 requests a day on self hosted. A gentler tick would silently stretch the first retry, since the retry ladder opens at 60 seconds. SCHEDULE_OVERRIDES is the one line place to change that. A parity test asserts the path sets match minus a documented exclusion list, and ratchets three cron routes that are currently scheduled nowhere so they are named rather than silently rotting. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * chore(observability): add a provider agnostic error sink There is no error tracking in this codebase: logs go to console and Vercel retention and nowhere else, nothing alerts on the 16 cron jobs, and seven code comments across lib, app, components and extensions asserted that Sentry captures errors when Sentry is not a dependency. The two most recent bug fixes on this repo were both discovered by customer email. This adds the sink, not a vendor. No dependency is taken: the interface has a no-op default and a registration point, so behaviour is unchanged until an adapter is registered. Releases are tagged from the build id already inlined by next.config.ts. Redaction moved out of lib/logger.ts into a leaf module that both the logger and the sink import, so there is one denylist and no path from application data to a third party can skip the personnummer regex, including direct sink calls that bypass the logger. That matters here because these logs carry personnummer and financial data. verifyCronSecret now reports its own 401s, which covers all 16 jobs without touching a route file and catches the case where CRON_SECRET is rotated without updating the scheduler and every job silently 401s forever. The threshold is one failure rather than the backup alert's three: suppressing the first occurrence is precisely how an outage stays invisible. The seven misleading comments are corrected to describe what the code actually does, including the two cases that still are not covered: the client side one, since the sink is server side, and a warn level call that is not forwarded. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix: remediate the 2026-07-26 similar-sweep findings across all surfaces Resolves the ~150-finding sweep (dev_docs/similar-sweep-2026-07-26.md) with one agent per finding; every behavioural fix carries a regression test proven to fail at HEAD. Full status, corrections to the sweep, refusals and open decisions in dev_docs/similar-sweep-2026-07-26-remediation-status.md. Structural roots closed: - resolveSekAmountOrNull(): honest SEK resolution refuses instead of booking 1:1; four duplicated toSek closures now refuse via INVOICE_FX_RATE_MISSING - ledger-line-amount.ts: journal_entry_lines.currency labels the document, not the amount; SQL pre-filter decoy proven and fixed - sparse-patch.ts: .partial() does not strip .default() in Zod 4.4.3; the exploitable salary payslip-line PATCH and KPI preferences sinks fixed - tests/schema: migration-replay phantom-column guard (13k+ refs, closed CHECK sets, onConflict targets); found 28 real defects, all fixed, all four baselines now empty - three new ratchet guards: sek-labelled-amount, cross-extension-import, ungated-extension-route Highlights: lawful VAT-rate set on all seven invoice surfaces (ML 6 kap), RC input VAT mismatch wired on web + both MCP callers, missing-underlag resource delegates to the shared RPC predicate, push-notifications consent polarity fail-closed, deadlines undo honours requested state, silent-failure and read-side-fabrication classes fixed across settings/KPI/inbox/Stripe/ Arcim/kassaflodesanalys, error-envelope stringification fixed at 10+ sites with isSwedishUserMessage extended. Also includes the parallel session's MCP invoice tools (update_invoice, recurring schedules, invoice deliveries) which share files with the sweep work and are verified green together. 13 new migrations are NOT applied anywhere; they apply via branch merge. 20260726120000 backfills 1247 supplier-invoice rows. pg tests for new DDL are written but unrun (no local Postgres). Verified: 11088 tests / 881 files green, tsc 0 non-test errors, lint 0 errors, check:guards passing, MCP payload 57475/57500. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(migrations): rename replace_sie_import migration off main's 20260726090000 version origin/main shipped 20260726090000_agent_quota_rpc_caller_guard.sql; keeping our replace_sie_import migration on the same version would abort the Supabase apply with a schema_migrations_pkey duplicate at merge time. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(review): remediate pre-publish deep-review findings across all slices A 13-agent review of the full branch diff surfaced 1 critical, 5 high and ~45 further findings; this commit resolves them in one pass: - replace_sie_import / undo_sie_import: p_user_id honored only for service_role callers; any other caller is pinned to auth.uid() (impersonation gate bypass), authz raise errcode 42501 mapped to a Swedish 403 in the route, new caller-guard migration for undo - bulk_book_transactions refuses homogeneous non-SEK batches instead of writing foreign magnitudes into SEK ledger columns - credit-note cap trigger: company-match on credited_invoice_id, no cross-tenant figures in exception text - link_voucher RPCs resolve NULL invoice currency as SEK end to end - personal-number ciphertext CHECK split into NOT VALID + VALIDATE - same-currency foreign settlements clear 1510 at booking rate and book realized diff to 3960/7960; rate-less foreign write paths refuse - receivables revaluation covers partially_paid and outstanding amounts - period lock guard paginates candidates past the PostgREST 1000 cap - documents: service-client storage removals after authz, dual-layout reads in integrity cron and archive export, backfill delete-source sweep actually deletes with hash verification and shared-key grouping - invoice matching normalizes NULL/lowercase currencies (regression), duplicate candidates stop claiming amount matches they never ran - match-invoice aborts on any booking failure (no paid-without-verifikat) - refresh-exchange-rate reverts on concurrent booking (TOCTOU window) - KPI preferences upsert arbiter aligned to the company-scoped constraint - personnummer_last4 stripped from all salary responses incl. MCP tools - worked-hours batch restores destroyed rows on conflict and error paths - MCP: shared duplicate-claim builder (no more 'null kr'), short-circuit on tag_journal_lines overflow, auto_send schedules stage as high risk - observability sink redacts emails/IBANs/API keys and keeps redacted stacks in prod; assorted small guards (safe-return-to /@, dry_run=True, cursor helper off-by-one, OAuth state TTL 10 min, arcim saveMappings call removed) Full dispositions, deferred items and hand-verified accounting numbers are documented in the PR body and DECISIONS.md. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(personnummer): implement masking and encryption for personal numbers with tests * fix(review): address CI and compliance-bot findings for PR #1215 pg-real: the CI image's auth shim reads the legacy request.jwt.claim.role GUC, so both service-role simulations (runAsServiceRole and the invoice-delivery test's local helper) never satisfied auth.role() = 'service_role' and every legitimate p_user_id path failed closed; the shared helper now sets both GUC shapes plus SET LOCAL ROLE with a fail-loud sanity check, and the delivery test reuses it. The link-voucher migration had recreated both RPCs from pre-rewrite file text, reintroducing the NULL-unsafe membership pattern the null-safe-tenant-guards ratchet bans; both guards now use public.caller_is_company_member() with all currency changes preserved. Compliance bots: the customers export now emits the standard masked form instead of raw AES-256-GCM ciphertext in the Org-/personnummer column, and maskCustomerRow returns a non-round-trippable placeholder on decrypt failure instead of 500ing the list. MCP parity: gnubok_lock_period's staging pre-check now runs the exact countUnbookedInPeriod the commit path enforces (exported from period-service; local mirror deleted), and gnubok_agi_status resolves AGI state run-scoped so a correction run no longer renders as already filed. Declined with evidence: PR-Agent's opening-balances null-zeroing concern (all mergeable columns are NOT NULL with defaults per 20260713101000). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(review): address codex review findings on PR #1215 - restore 20260726140000 to its preview-recorded content and restate the NULL-safe tenant guard under 20260727130000: a recorded migration version never re-runs, so the in-place edit could not reach the preview branch - replace toFixed() with sv-SE two-decimal formatting in the ROT/RUT cap warning texts and update the pinned test expectations - drop the em dash in the fiscal-periods route comment - strip trailing whitespace in import-existing.test.ts Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * test(reports): raise timeout on real PDF render tests renderToBuffer does real @react-pdf layout work and exceeds the 5s default when the full suite saturates the CPU; tests pass in isolation. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(review): remediate the 2026-07-27 compliance and security review findings - ROT/RUT deduction base is arbetskostnaden INKLUSIVE moms (HUSFL 2009:194 6-9 par.): computeDeduction takes the line vat_rate, all five call sites pass it, and tests pin Skatteverkets worked example (18 000 kr excl = 22 500 incl, ROT 6 750). - Momsdeklaration: new SALES_OUTPUT_VAT_SHORTFALL warning catches output VAT short of the reported sales base (one-directional, never filing-blocking). - SIE import: #RAR records validated for every year index (dates, ordering, 18-month BFL cap as warn-and-keep). - build-invoice-write: SEK invoices populate the *_sek twin columns (rate 1) so both creation paths produce the same row shape. - CI: daily Trivy SCA scan of the npm lockfile (replaces removed Dependabot); compliance review fails loudly on empty review.md. - arcim migration FX logging routed through the redacting structured logger. - docs/security/: authorization policy for the SIE bulk-delete RPC pair and the observability redaction contract. - Rewrote the swedish-payroll ob-overtime reference (was a byte-identical copy of sick-pay.md); skills:generate emitted the atom-body seed migration. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
1871 lines
68 KiB
TypeScript
1871 lines
68 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||
import { getRevenueAccount, getOutputVatAccount } from '../invoice-entries'
|
||
import { roundOre } from '@/lib/money'
|
||
import type { Invoice, InvoiceItem, CreateJournalEntryInput } from '@/types'
|
||
|
||
// Mock the engine so we can capture the input passed to createJournalEntry
|
||
vi.mock('../engine', () => ({
|
||
findFiscalPeriod: vi.fn().mockResolvedValue('period-1'),
|
||
createJournalEntry: vi.fn().mockImplementation(
|
||
async (_supabase: unknown, _companyId: string, _userId: string, input: CreateJournalEntryInput) => ({
|
||
id: 'entry-1',
|
||
...input,
|
||
lines: input.lines,
|
||
})
|
||
),
|
||
}))
|
||
|
||
// Mock vat-entries to avoid indirect dependency issues
|
||
vi.mock('../vat-entries', () => ({
|
||
generateSalesVatLines: vi.fn().mockImplementation(({ vatTreatment, baseAmount }: { vatTreatment: string; baseAmount: number }) => {
|
||
const rate = vatTreatment === 'standard_25' ? 0.25
|
||
: vatTreatment === 'reduced_12' ? 0.12
|
||
: vatTreatment === 'reduced_6' ? 0.06 : 0
|
||
if (rate === 0) return []
|
||
const account = vatTreatment === 'standard_25' ? '2611'
|
||
: vatTreatment === 'reduced_12' ? '2621' : '2631'
|
||
return [{
|
||
account_number: account,
|
||
debit_amount: 0,
|
||
credit_amount: Math.round(baseAmount * rate * 100) / 100,
|
||
line_description: `Utgående moms`,
|
||
}]
|
||
}),
|
||
generateReverseChargeLines: vi.fn().mockReturnValue([]),
|
||
}))
|
||
|
||
const { createJournalEntry } = await import('../engine')
|
||
const mockedCreateEntry = vi.mocked(createJournalEntry)
|
||
|
||
// Import functions under test AFTER mocks are set up
|
||
const {
|
||
createInvoiceJournalEntry,
|
||
createInvoicePaymentJournalEntry,
|
||
createCreditNoteJournalEntry,
|
||
createInvoiceCashEntry,
|
||
} = await import('../invoice-entries')
|
||
|
||
// Helper to build a minimal Invoice with items
|
||
function makeInvoice(overrides: Partial<Invoice> & { items?: InvoiceItem[] }): Invoice {
|
||
return {
|
||
id: 'inv-1',
|
||
user_id: 'user-1',
|
||
customer_id: 'cust-1',
|
||
invoice_number: '1001',
|
||
invoice_date: '2024-06-15',
|
||
due_date: '2024-07-15',
|
||
currency: 'SEK',
|
||
exchange_rate: null,
|
||
exchange_rate_date: null,
|
||
subtotal: 1000,
|
||
subtotal_sek: null,
|
||
vat_amount: 250,
|
||
vat_amount_sek: null,
|
||
total: 1250,
|
||
total_sek: null,
|
||
vat_treatment: 'standard_25',
|
||
vat_rate: 25,
|
||
moms_ruta: '05',
|
||
reverse_charge_text: null,
|
||
your_reference: null,
|
||
our_reference: null,
|
||
notes: null,
|
||
status: 'sent',
|
||
sent_at: null,
|
||
paid_at: null,
|
||
payment_date: null,
|
||
credited_invoice_id: null,
|
||
journal_entry_id: null,
|
||
payment_journal_entry_id: null,
|
||
document_type: 'invoice',
|
||
created_at: '2024-06-15T00:00:00Z',
|
||
updated_at: '2024-06-15T00:00:00Z',
|
||
items: [],
|
||
...overrides,
|
||
} as Invoice
|
||
}
|
||
|
||
function makeItem(overrides: Partial<InvoiceItem> = {}): InvoiceItem {
|
||
return {
|
||
id: 'item-1',
|
||
invoice_id: 'inv-1',
|
||
sort_order: 0,
|
||
description: 'Service',
|
||
quantity: 1,
|
||
unit: 'st',
|
||
unit_price: 1000,
|
||
line_total: 1000,
|
||
vat_rate: 25,
|
||
vat_amount: 250,
|
||
created_at: '2024-06-15T00:00:00Z',
|
||
...overrides,
|
||
}
|
||
}
|
||
|
||
describe('getRevenueAccount', () => {
|
||
it('standard_25 returns 3001', () => {
|
||
expect(getRevenueAccount('standard_25')).toBe('3001')
|
||
})
|
||
|
||
it('reduced_12 returns 3002', () => {
|
||
expect(getRevenueAccount('reduced_12')).toBe('3002')
|
||
})
|
||
|
||
it('reduced_6 returns 3003', () => {
|
||
expect(getRevenueAccount('reduced_6')).toBe('3003')
|
||
})
|
||
|
||
it('reverse_charge returns 3308', () => {
|
||
expect(getRevenueAccount('reverse_charge')).toBe('3308')
|
||
})
|
||
|
||
it('export returns 3305', () => {
|
||
expect(getRevenueAccount('export')).toBe('3305')
|
||
})
|
||
|
||
it('exempt defaults to 3100 for enskild_firma', () => {
|
||
expect(getRevenueAccount('exempt')).toBe('3100')
|
||
expect(getRevenueAccount('exempt', 'enskild_firma')).toBe('3100')
|
||
})
|
||
|
||
it('exempt returns 3004 for aktiebolag', () => {
|
||
expect(getRevenueAccount('exempt', 'aktiebolag')).toBe('3004')
|
||
})
|
||
|
||
it('entityType does not affect non-exempt treatments', () => {
|
||
expect(getRevenueAccount('standard_25', 'aktiebolag')).toBe('3001')
|
||
expect(getRevenueAccount('reduced_12', 'aktiebolag')).toBe('3002')
|
||
expect(getRevenueAccount('export', 'aktiebolag')).toBe('3305')
|
||
})
|
||
})
|
||
|
||
describe('getOutputVatAccount', () => {
|
||
it('standard_25 returns 2611', () => {
|
||
expect(getOutputVatAccount('standard_25')).toBe('2611')
|
||
})
|
||
|
||
it('reduced_12 returns 2621', () => {
|
||
expect(getOutputVatAccount('reduced_12')).toBe('2621')
|
||
})
|
||
|
||
it('reduced_6 returns 2631', () => {
|
||
expect(getOutputVatAccount('reduced_6')).toBe('2631')
|
||
})
|
||
})
|
||
|
||
describe('createInvoiceJournalEntry: per-line VAT', () => {
|
||
beforeEach(() => {
|
||
vi.clearAllMocks()
|
||
})
|
||
|
||
it('single-rate invoice creates one revenue + one VAT line', async () => {
|
||
const invoice = makeInvoice({
|
||
subtotal: 1000,
|
||
vat_amount: 250,
|
||
total: 1250,
|
||
vat_treatment: 'standard_25',
|
||
items: [
|
||
makeItem({ description: 'A', quantity: 2, unit_price: 300, line_total: 600, vat_rate: 25, vat_amount: 150 }),
|
||
makeItem({ id: 'item-2', description: 'B', quantity: 1, unit_price: 400, line_total: 400, vat_rate: 25, vat_amount: 100 }),
|
||
],
|
||
})
|
||
|
||
await createInvoiceJournalEntry(null as never, 'company-1', 'user-1', invoice)
|
||
|
||
expect(mockedCreateEntry).toHaveBeenCalledOnce()
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
|
||
// Should have 3 lines: 1510 debit, 3001 credit, 2611 credit
|
||
expect(input.lines).toHaveLength(3)
|
||
|
||
// Debit 1510 = total
|
||
const debit1510 = input.lines.find((l) => l.account_number === '1510')
|
||
expect(debit1510?.debit_amount).toBe(1250)
|
||
expect(debit1510?.credit_amount).toBe(0)
|
||
|
||
// Credit 3001 = subtotal
|
||
const credit3001 = input.lines.find((l) => l.account_number === '3001')
|
||
expect(credit3001?.debit_amount).toBe(0)
|
||
expect(credit3001?.credit_amount).toBe(1000)
|
||
|
||
// Credit 2611 = VAT
|
||
const credit2611 = input.lines.find((l) => l.account_number === '2611')
|
||
expect(credit2611?.debit_amount).toBe(0)
|
||
expect(credit2611?.credit_amount).toBe(250)
|
||
})
|
||
|
||
it('mixed 25%/12% creates two revenue + two VAT lines', async () => {
|
||
const invoice = makeInvoice({
|
||
subtotal: 1000,
|
||
vat_amount: 184, // 600*0.25 + 400*0.12 = 150 + 48 = 198... let's recalc
|
||
total: 1198,
|
||
vat_treatment: 'standard_25',
|
||
vat_rate: null as unknown as number,
|
||
items: [
|
||
makeItem({ description: 'Consulting', quantity: 1, unit_price: 600, line_total: 600, vat_rate: 25, vat_amount: 150 }),
|
||
makeItem({ id: 'item-2', description: 'Food service', quantity: 1, unit_price: 400, line_total: 400, vat_rate: 12, vat_amount: 48 }),
|
||
],
|
||
})
|
||
invoice.vat_amount = 198
|
||
invoice.total = 1198
|
||
|
||
await createInvoiceJournalEntry(null as never, 'company-1', 'user-1', invoice)
|
||
|
||
expect(mockedCreateEntry).toHaveBeenCalledOnce()
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
|
||
// Should have 5 lines: 1510, 3001(25%), 2611(25%), 3002(12%), 2621(12%)
|
||
expect(input.lines).toHaveLength(5)
|
||
|
||
// Debit 1510 = total
|
||
const debit1510 = input.lines.find((l) => l.account_number === '1510')
|
||
expect(debit1510?.debit_amount).toBe(1198)
|
||
|
||
// Revenue 3001 (25% group)
|
||
const credit3001 = input.lines.find((l) => l.account_number === '3001')
|
||
expect(credit3001?.credit_amount).toBe(600)
|
||
|
||
// VAT 2611 (25% group)
|
||
const credit2611 = input.lines.find((l) => l.account_number === '2611')
|
||
expect(credit2611?.credit_amount).toBe(150)
|
||
|
||
// Revenue 3002 (12% group)
|
||
const credit3002 = input.lines.find((l) => l.account_number === '3002')
|
||
expect(credit3002?.credit_amount).toBe(400)
|
||
|
||
// VAT 2621 (12% group)
|
||
const credit2621 = input.lines.find((l) => l.account_number === '2621')
|
||
expect(credit2621?.credit_amount).toBe(48)
|
||
})
|
||
|
||
it('reverse charge creates single 3308, no VAT lines', async () => {
|
||
const invoice = makeInvoice({
|
||
subtotal: 5000,
|
||
vat_amount: 0,
|
||
total: 5000,
|
||
vat_treatment: 'reverse_charge',
|
||
vat_rate: 0,
|
||
items: [
|
||
makeItem({ quantity: 1, unit_price: 5000, line_total: 5000, vat_rate: 0, vat_amount: 0 }),
|
||
],
|
||
})
|
||
|
||
await createInvoiceJournalEntry(null as never, 'company-1', 'user-1', invoice)
|
||
|
||
expect(mockedCreateEntry).toHaveBeenCalledOnce()
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
|
||
// Should have 2 lines: 1510 debit, 3308 credit (no VAT)
|
||
expect(input.lines).toHaveLength(2)
|
||
|
||
const debit1510 = input.lines.find((l) => l.account_number === '1510')
|
||
expect(debit1510?.debit_amount).toBe(5000)
|
||
|
||
const credit3308 = input.lines.find((l) => l.account_number === '3308')
|
||
expect(credit3308?.credit_amount).toBe(5000)
|
||
|
||
// No VAT lines
|
||
const vatLines = input.lines.filter((l) =>
|
||
l.account_number.startsWith('26')
|
||
)
|
||
expect(vatLines).toHaveLength(0)
|
||
})
|
||
|
||
it('balance: debit(1510) = sum(revenue + VAT credits)', async () => {
|
||
const invoice = makeInvoice({
|
||
subtotal: 2000,
|
||
vat_amount: 380, // 1200*0.25 + 500*0.12 + 300*0.06 = 300 + 60 + 18 = 378
|
||
total: 2378,
|
||
vat_treatment: 'standard_25',
|
||
vat_rate: null as unknown as number,
|
||
items: [
|
||
makeItem({ description: 'A', quantity: 1, unit_price: 1200, line_total: 1200, vat_rate: 25, vat_amount: 300 }),
|
||
makeItem({ id: 'item-2', description: 'B', quantity: 1, unit_price: 500, line_total: 500, vat_rate: 12, vat_amount: 60 }),
|
||
makeItem({ id: 'item-3', description: 'C', quantity: 1, unit_price: 300, line_total: 300, vat_rate: 6, vat_amount: 18 }),
|
||
],
|
||
})
|
||
invoice.vat_amount = 378
|
||
invoice.total = 2378
|
||
|
||
await createInvoiceJournalEntry(null as never, 'company-1', 'user-1', invoice)
|
||
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
|
||
const totalDebit = input.lines.reduce((sum, l) => sum + l.debit_amount, 0)
|
||
const totalCredit = input.lines.reduce((sum, l) => sum + l.credit_amount, 0)
|
||
|
||
expect(totalDebit).toBe(totalCredit)
|
||
expect(totalDebit).toBe(2378)
|
||
})
|
||
})
|
||
|
||
describe('createInvoiceJournalEntry: per-article revenue account override', () => {
|
||
beforeEach(() => {
|
||
vi.clearAllMocks()
|
||
})
|
||
|
||
it('without an override, two 25% lines collapse into one 3001 revenue line (unchanged behaviour)', async () => {
|
||
const invoice = makeInvoice({
|
||
subtotal: 1000,
|
||
vat_amount: 250,
|
||
total: 1250,
|
||
vat_treatment: 'standard_25',
|
||
vat_rate: null as unknown as number,
|
||
items: [
|
||
makeItem({ description: 'A', unit_price: 600, line_total: 600, vat_rate: 25, vat_amount: 150 }),
|
||
makeItem({ id: 'item-2', description: 'B', unit_price: 400, line_total: 400, vat_rate: 25, vat_amount: 100 }),
|
||
],
|
||
})
|
||
|
||
await createInvoiceJournalEntry(null as never, 'company-1', 'user-1', invoice)
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
|
||
const rev3001 = input.lines.filter((l) => l.account_number === '3001')
|
||
expect(rev3001).toHaveLength(1)
|
||
expect(rev3001[0].credit_amount).toBe(1000)
|
||
const vat2611 = input.lines.filter((l) => l.account_number === '2611')
|
||
expect(vat2611).toHaveLength(1)
|
||
expect(vat2611[0].credit_amount).toBe(250)
|
||
})
|
||
|
||
it('splits one rate into two revenue accounts but keeps a single VAT line, balanced', async () => {
|
||
const invoice = makeInvoice({
|
||
subtotal: 1000,
|
||
vat_amount: 250,
|
||
total: 1250,
|
||
vat_treatment: 'standard_25',
|
||
vat_rate: null as unknown as number,
|
||
items: [
|
||
makeItem({ description: 'Goods', unit_price: 600, line_total: 600, vat_rate: 25, vat_amount: 150 }), // no override → 3001
|
||
makeItem({ id: 'item-2', description: 'Consulting', unit_price: 400, line_total: 400, vat_rate: 25, vat_amount: 100, revenue_account: '3041' }),
|
||
],
|
||
})
|
||
|
||
await createInvoiceJournalEntry(null as never, 'company-1', 'user-1', invoice)
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
|
||
expect(input.lines.find((l) => l.account_number === '3001')?.credit_amount).toBe(600)
|
||
expect(input.lines.find((l) => l.account_number === '3041')?.credit_amount).toBe(400)
|
||
const vat = input.lines.filter((l) => l.account_number === '2611')
|
||
expect(vat).toHaveLength(1)
|
||
expect(vat[0].credit_amount).toBe(250)
|
||
|
||
const debit = input.lines.reduce((s, l) => s + l.debit_amount, 0)
|
||
const credit = input.lines.reduce((s, l) => s + l.credit_amount, 0)
|
||
expect(debit).toBe(credit)
|
||
expect(debit).toBe(1250)
|
||
})
|
||
|
||
it('credits a selected liability account without treating the principal as revenue', async () => {
|
||
const invoice = makeInvoice({
|
||
subtotal: 10000,
|
||
vat_amount: 0,
|
||
total: 10000,
|
||
vat_treatment: 'exempt',
|
||
vat_rate: 0,
|
||
items: [
|
||
makeItem({
|
||
description: 'Återbetalningsbar deposition',
|
||
unit_price: 10000,
|
||
line_total: 10000,
|
||
vat_rate: 0,
|
||
vat_amount: 0,
|
||
revenue_account: '2897',
|
||
}),
|
||
],
|
||
})
|
||
|
||
await createInvoiceJournalEntry(null as never, 'company-1', 'user-1', invoice)
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
|
||
expect(input.lines.find((line) => line.account_number === '1510')?.debit_amount).toBe(10000)
|
||
expect(input.lines.find((line) => line.account_number === '2897')?.credit_amount).toBe(10000)
|
||
expect(input.lines.some((line) => line.account_number.startsWith('3'))).toBe(false)
|
||
expect(input.lines.some((line) => line.account_number.startsWith('26'))).toBe(false)
|
||
})
|
||
|
||
it('ignores a per-line override on reverse charge: revenue stays on 3308', async () => {
|
||
const invoice = makeInvoice({
|
||
subtotal: 5000,
|
||
vat_amount: 0,
|
||
total: 5000,
|
||
vat_treatment: 'reverse_charge',
|
||
vat_rate: 0,
|
||
items: [
|
||
makeItem({ unit_price: 5000, line_total: 5000, vat_rate: 0, vat_amount: 0, revenue_account: '3041' }),
|
||
],
|
||
})
|
||
|
||
await createInvoiceJournalEntry(null as never, 'company-1', 'user-1', invoice)
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
|
||
expect(input.lines.find((l) => l.account_number === '3308')?.credit_amount).toBe(5000)
|
||
expect(input.lines.find((l) => l.account_number === '3041')).toBeUndefined()
|
||
})
|
||
|
||
it('absorbs rounding on the last account so a split rate still balances against 1510', async () => {
|
||
// Two 25% lines to different accounts whose individual SEK rounding would
|
||
// otherwise drift from the rate-level total (10.005 → 10.01 each = 20.02,
|
||
// but the rate total is round(20.01) = 20.01).
|
||
const invoice = makeInvoice({
|
||
subtotal: 20.01,
|
||
vat_amount: 5.0,
|
||
total: 25.01,
|
||
vat_treatment: 'standard_25',
|
||
vat_rate: null as unknown as number,
|
||
items: [
|
||
makeItem({ description: 'A', unit_price: 10.005, line_total: 10.005, vat_rate: 25, vat_amount: 2.5 }),
|
||
makeItem({ id: 'item-2', description: 'B', unit_price: 10.005, line_total: 10.005, vat_rate: 25, vat_amount: 2.5, revenue_account: '3041' }),
|
||
],
|
||
})
|
||
|
||
await createInvoiceJournalEntry(null as never, 'company-1', 'user-1', invoice)
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
|
||
const revSum = input.lines
|
||
.filter((l) => l.account_number === '3001' || l.account_number === '3041')
|
||
.reduce((s, l) => s + l.credit_amount, 0)
|
||
expect(Math.round(revSum * 100) / 100).toBe(20.01)
|
||
|
||
const debit = Math.round(input.lines.reduce((s, l) => s + l.debit_amount, 0) * 100) / 100
|
||
const credit = Math.round(input.lines.reduce((s, l) => s + l.credit_amount, 0) * 100) / 100
|
||
expect(debit).toBe(credit)
|
||
expect(debit).toBe(25.01)
|
||
})
|
||
})
|
||
|
||
describe('createCreditNoteJournalEntry: per-line VAT', () => {
|
||
beforeEach(() => {
|
||
vi.clearAllMocks()
|
||
})
|
||
|
||
it('reverses per-rate lines correctly for mixed rates', async () => {
|
||
const creditNote = makeInvoice({
|
||
invoice_number: 'KR-1001',
|
||
subtotal: -1000,
|
||
vat_amount: -198,
|
||
total: -1198,
|
||
vat_treatment: 'standard_25',
|
||
items: [
|
||
makeItem({ quantity: -1, unit_price: 600, line_total: -600, vat_rate: 25, vat_amount: -150 }),
|
||
makeItem({ id: 'item-2', quantity: -1, unit_price: 400, line_total: -400, vat_rate: 12, vat_amount: -48 }),
|
||
],
|
||
})
|
||
|
||
await createCreditNoteJournalEntry(null as never, 'company-1', 'user-1', creditNote)
|
||
|
||
expect(mockedCreateEntry).toHaveBeenCalledOnce()
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
|
||
// Revenue and VAT lines should be debits (reversed)
|
||
const debit3001 = input.lines.find((l) => l.account_number === '3001')
|
||
expect(debit3001?.debit_amount).toBe(600)
|
||
expect(debit3001?.credit_amount).toBe(0)
|
||
|
||
const debit2611 = input.lines.find((l) => l.account_number === '2611')
|
||
expect(debit2611?.debit_amount).toBe(150)
|
||
|
||
const debit3002 = input.lines.find((l) => l.account_number === '3002')
|
||
expect(debit3002?.debit_amount).toBe(400)
|
||
|
||
const debit2621 = input.lines.find((l) => l.account_number === '2621')
|
||
expect(debit2621?.debit_amount).toBe(48)
|
||
|
||
// 1510 should be credit
|
||
const credit1510 = input.lines.find((l) => l.account_number === '1510')
|
||
expect(credit1510?.credit_amount).toBe(1198)
|
||
expect(credit1510?.debit_amount).toBe(0)
|
||
|
||
// Balance check
|
||
const totalDebit = input.lines.reduce((sum, l) => sum + l.debit_amount, 0)
|
||
const totalCredit = input.lines.reduce((sum, l) => sum + l.credit_amount, 0)
|
||
expect(totalDebit).toBe(totalCredit)
|
||
})
|
||
|
||
it.each([
|
||
// Deduction base is the line total INCL. VAT (HUSFL 6-9 §§):
|
||
// rot 30% x 12 500 = 3 750, rut 50% x 12 500 = 6 250.
|
||
['rot' as const, 3750, 8750],
|
||
['rut' as const, 6250, 6250],
|
||
])('reverses the %s receivable split across 1510 and 1513', async (deductionType, taxCredit, customerCredit) => {
|
||
const creditNote = makeInvoice({
|
||
invoice_number: 'KR-1002',
|
||
subtotal: -10000,
|
||
vat_amount: -2500,
|
||
total: -12500,
|
||
vat_treatment: 'standard_25',
|
||
items: [
|
||
makeItem({
|
||
quantity: -1,
|
||
unit_price: 10000,
|
||
line_total: -10000,
|
||
vat_rate: 25,
|
||
vat_amount: -2500,
|
||
deduction_type: deductionType,
|
||
dimensions: { '6': 'P001' },
|
||
}),
|
||
],
|
||
default_dimensions: { '1': 'KS01' },
|
||
})
|
||
|
||
await createCreditNoteJournalEntry(null as never, 'company-1', 'user-1', creditNote)
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
|
||
expect(input.lines.find((line) => line.account_number === '1510')?.credit_amount)
|
||
.toBe(customerCredit)
|
||
const line1513 = input.lines.find((line) => line.account_number === '1513')
|
||
expect(line1513?.credit_amount).toBe(taxCredit)
|
||
expect(line1513?.debit_amount).toBe(0)
|
||
expect(line1513?.dimensions).toEqual({ '1': 'KS01', '6': 'P001' })
|
||
|
||
const totalDebit = input.lines.reduce((sum, line) => sum + line.debit_amount, 0)
|
||
const totalCredit = input.lines.reduce((sum, line) => sum + line.credit_amount, 0)
|
||
expect(totalDebit).toBe(totalCredit)
|
||
})
|
||
})
|
||
|
||
describe('createInvoiceCashEntry: per-line VAT', () => {
|
||
beforeEach(() => {
|
||
vi.clearAllMocks()
|
||
})
|
||
|
||
it('cash method with mixed rates creates per-rate revenue + VAT', async () => {
|
||
const invoice = makeInvoice({
|
||
subtotal: 1000,
|
||
vat_amount: 198,
|
||
total: 1198,
|
||
vat_treatment: 'standard_25',
|
||
items: [
|
||
makeItem({ quantity: 1, unit_price: 600, line_total: 600, vat_rate: 25, vat_amount: 150 }),
|
||
makeItem({ id: 'item-2', quantity: 1, unit_price: 400, line_total: 400, vat_rate: 12, vat_amount: 48 }),
|
||
],
|
||
})
|
||
|
||
await createInvoiceCashEntry(null as never, 'company-1', 'user-1', invoice, '2024-07-01')
|
||
|
||
expect(mockedCreateEntry).toHaveBeenCalledOnce()
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
|
||
// Debit 1930 (bank account) instead of 1510
|
||
const debit1930 = input.lines.find((l) => l.account_number === '1930')
|
||
expect(debit1930?.debit_amount).toBe(1198)
|
||
|
||
// Same per-rate credits as accrual
|
||
const credit3001 = input.lines.find((l) => l.account_number === '3001')
|
||
expect(credit3001?.credit_amount).toBe(600)
|
||
|
||
const credit2611 = input.lines.find((l) => l.account_number === '2611')
|
||
expect(credit2611?.credit_amount).toBe(150)
|
||
|
||
const credit3002 = input.lines.find((l) => l.account_number === '3002')
|
||
expect(credit3002?.credit_amount).toBe(400)
|
||
|
||
// Balance
|
||
const totalDebit = input.lines.reduce((sum, l) => sum + l.debit_amount, 0)
|
||
const totalCredit = input.lines.reduce((sum, l) => sum + l.credit_amount, 0)
|
||
expect(totalDebit).toBe(totalCredit)
|
||
})
|
||
|
||
it('cash method credits a selected liability account at payment', async () => {
|
||
const invoice = makeInvoice({
|
||
subtotal: 10000,
|
||
vat_amount: 0,
|
||
total: 10000,
|
||
vat_treatment: 'exempt',
|
||
items: [
|
||
makeItem({
|
||
description: 'Återbetalningsbar deposition',
|
||
unit_price: 10000,
|
||
line_total: 10000,
|
||
vat_rate: 0,
|
||
vat_amount: 0,
|
||
revenue_account: '2897',
|
||
}),
|
||
],
|
||
})
|
||
|
||
await createInvoiceCashEntry(null as never, 'company-1', 'user-1', invoice, '2024-07-01')
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
|
||
expect(input.lines.find((line) => line.account_number === '1930')?.debit_amount).toBe(10000)
|
||
expect(input.lines.find((line) => line.account_number === '2897')?.credit_amount).toBe(10000)
|
||
expect(input.lines.some((line) => line.account_number.startsWith('3'))).toBe(false)
|
||
})
|
||
})
|
||
|
||
describe('createInvoiceJournalEntry: EUR foreign currency', () => {
|
||
beforeEach(() => {
|
||
vi.clearAllMocks()
|
||
})
|
||
|
||
it('EUR invoice converts amounts to SEK using exchange rate', async () => {
|
||
// EUR 1,000 + EUR 250 VAT = EUR 1,250 total, rate 11.5
|
||
const invoice = makeInvoice({
|
||
currency: 'EUR',
|
||
exchange_rate: 11.5,
|
||
subtotal: 1000,
|
||
subtotal_sek: 11500,
|
||
vat_amount: 250,
|
||
vat_amount_sek: 2875,
|
||
total: 1250,
|
||
total_sek: 14375,
|
||
vat_treatment: 'standard_25',
|
||
items: [
|
||
makeItem({ line_total: 1000, vat_rate: 25, vat_amount: 250 }),
|
||
],
|
||
})
|
||
|
||
await createInvoiceJournalEntry(null as never, 'company-1', 'user-1', invoice)
|
||
|
||
expect(mockedCreateEntry).toHaveBeenCalledOnce()
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
|
||
// All amounts should be in SEK
|
||
const debit1510 = input.lines.find((l) => l.account_number === '1510')
|
||
expect(debit1510?.debit_amount).toBe(14375) // 1000*11.5 + 250*11.5 = 14375
|
||
|
||
const credit3001 = input.lines.find((l) => l.account_number === '3001')
|
||
expect(credit3001?.credit_amount).toBe(11500) // 1000 * 11.5
|
||
|
||
const credit2611 = input.lines.find((l) => l.account_number === '2611')
|
||
expect(credit2611?.credit_amount).toBe(2875) // 250 * 11.5
|
||
|
||
// 1510 line should have currency metadata
|
||
expect(debit1510?.currency).toBe('EUR')
|
||
expect(debit1510?.amount_in_currency).toBe(1250)
|
||
expect(debit1510?.exchange_rate).toBe(11.5)
|
||
|
||
// Balance check
|
||
const totalDebit = input.lines.reduce((sum, l) => sum + l.debit_amount, 0)
|
||
const totalCredit = input.lines.reduce((sum, l) => sum + l.credit_amount, 0)
|
||
expect(totalDebit).toBe(totalCredit)
|
||
})
|
||
|
||
it('EUR invoice uses total_sek when available', async () => {
|
||
// Edge case: total_sek differs slightly from computed (e.g. pre-computed at different rate)
|
||
const invoice = makeInvoice({
|
||
currency: 'EUR',
|
||
exchange_rate: 11.5,
|
||
subtotal: 1000,
|
||
subtotal_sek: null,
|
||
vat_amount: 0,
|
||
vat_amount_sek: null,
|
||
total: 1000,
|
||
total_sek: null,
|
||
vat_treatment: 'export',
|
||
items: [
|
||
makeItem({ line_total: 1000, vat_rate: 0, vat_amount: 0 }),
|
||
],
|
||
})
|
||
|
||
await createInvoiceJournalEntry(null as never, 'company-1', 'user-1', invoice)
|
||
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
|
||
// Revenue should be computed via exchange rate
|
||
const credit3305 = input.lines.find((l) => l.account_number === '3305')
|
||
expect(credit3305?.credit_amount).toBe(11500)
|
||
|
||
const debit1510 = input.lines.find((l) => l.account_number === '1510')
|
||
expect(debit1510?.debit_amount).toBe(11500)
|
||
})
|
||
|
||
it('SEK invoice still works unchanged (backward compatibility)', async () => {
|
||
const invoice = makeInvoice({
|
||
subtotal: 800,
|
||
vat_amount: 200,
|
||
total: 1000,
|
||
vat_treatment: 'standard_25',
|
||
items: [
|
||
makeItem({ line_total: 800, vat_rate: 25, vat_amount: 200 }),
|
||
],
|
||
})
|
||
|
||
await createInvoiceJournalEntry(null as never, 'company-1', 'user-1', invoice)
|
||
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
const debit1510 = input.lines.find((l) => l.account_number === '1510')
|
||
expect(debit1510?.debit_amount).toBe(1000)
|
||
|
||
// No currency metadata for SEK
|
||
expect(debit1510?.currency).toBeUndefined()
|
||
expect(debit1510?.amount_in_currency).toBeUndefined()
|
||
})
|
||
})
|
||
|
||
/**
|
||
* A foreign invoice with no exchange rate has no SEK value at item granularity
|
||
* (InvoiceItem has no *_sek column). The old fallback returned the raw foreign
|
||
* amount, and since the 1510 debit is derived from the sum of the credits on the
|
||
* FX branch, the verifikation still balanced: 1 000 EUR posted as 1 000 kr to
|
||
* 3001 and 250 kr to 2611 instead of 11 500 kr and 2 875 kr, understating ruta
|
||
* 05 and ruta 10. Nothing downstream could detect it, so the generator refuses.
|
||
*/
|
||
describe('foreign currency without an exchange rate is refused, not relabelled', () => {
|
||
beforeEach(() => {
|
||
vi.clearAllMocks()
|
||
})
|
||
|
||
function eurInvoiceWithoutRate(overrides: Partial<Invoice> = {}): Invoice {
|
||
return makeInvoice({
|
||
currency: 'EUR',
|
||
exchange_rate: null,
|
||
subtotal: 1000,
|
||
subtotal_sek: null,
|
||
vat_amount: 250,
|
||
vat_amount_sek: null,
|
||
total: 1250,
|
||
total_sek: null,
|
||
vat_treatment: 'standard_25',
|
||
items: [makeItem({ line_total: 1000, vat_rate: 25, vat_amount: 250 })],
|
||
...overrides,
|
||
})
|
||
}
|
||
|
||
it('createInvoiceJournalEntry throws INVOICE_FX_RATE_MISSING and posts nothing', async () => {
|
||
await expect(
|
||
createInvoiceJournalEntry(null as never, 'company-1', 'user-1', eurInvoiceWithoutRate())
|
||
).rejects.toMatchObject({ code: 'INVOICE_FX_RATE_MISSING', currency: 'EUR' })
|
||
|
||
expect(mockedCreateEntry).not.toHaveBeenCalled()
|
||
})
|
||
|
||
it('refuses a zero exchange rate the same way', async () => {
|
||
await expect(
|
||
createInvoiceJournalEntry(
|
||
null as never,
|
||
'company-1',
|
||
'user-1',
|
||
eurInvoiceWithoutRate({ exchange_rate: 0 })
|
||
)
|
||
).rejects.toMatchObject({ code: 'INVOICE_FX_RATE_MISSING' })
|
||
expect(mockedCreateEntry).not.toHaveBeenCalled()
|
||
})
|
||
|
||
it('createInvoiceCashEntry refuses too', async () => {
|
||
await expect(
|
||
createInvoiceCashEntry(
|
||
null as never,
|
||
'company-1',
|
||
'user-1',
|
||
eurInvoiceWithoutRate(),
|
||
'2024-06-20'
|
||
)
|
||
).rejects.toMatchObject({ code: 'INVOICE_FX_RATE_MISSING' })
|
||
expect(mockedCreateEntry).not.toHaveBeenCalled()
|
||
})
|
||
|
||
it('createCreditNoteJournalEntry refuses too', async () => {
|
||
const creditNote = eurInvoiceWithoutRate({
|
||
credited_invoice_id: 'inv-original',
|
||
subtotal: -1000,
|
||
vat_amount: -250,
|
||
total: -1250,
|
||
items: [makeItem({ line_total: -1000, quantity: -1, vat_rate: 25, vat_amount: -250 })],
|
||
})
|
||
|
||
await expect(
|
||
createCreditNoteJournalEntry(null as never, 'company-1', 'user-1', creditNote)
|
||
).rejects.toMatchObject({ code: 'INVOICE_FX_RATE_MISSING' })
|
||
expect(mockedCreateEntry).not.toHaveBeenCalled()
|
||
})
|
||
|
||
// generateRotRutLines has its own conversion; 1513 is a kronor receivable on
|
||
// Skatteverket, so it must refuse on the same terms. Note that
|
||
// generatePerRateLines runs first and refuses first, so this asserts the
|
||
// outcome for a ROT invoice; the positive case below is what proves the 1513
|
||
// leg itself goes through the honest ladder rather than a private fallback.
|
||
it('a ROT invoice in EUR without a rate is refused, nothing lands on 1513', async () => {
|
||
const invoice = eurInvoiceWithoutRate({
|
||
items: [
|
||
makeItem({
|
||
line_total: 1000,
|
||
unit_price: 1000,
|
||
quantity: 1,
|
||
vat_rate: 25,
|
||
vat_amount: 250,
|
||
deduction_type: 'rot',
|
||
} as Partial<InvoiceItem>),
|
||
],
|
||
})
|
||
|
||
await expect(
|
||
createInvoiceJournalEntry(null as never, 'company-1', 'user-1', invoice)
|
||
).rejects.toMatchObject({ code: 'INVOICE_FX_RATE_MISSING' })
|
||
expect(mockedCreateEntry).not.toHaveBeenCalled()
|
||
})
|
||
|
||
it('the ROT 1513 leg converts at the rate and the split still balances', async () => {
|
||
// 1 000 EUR labour + 25% VAT = 1 250 EUR incl. moms; ROT is 30% of the
|
||
// inkl.-moms labour = 375 EUR = 4 312,50 kr at 11,50 on 1513, so 1510
|
||
// carries 14 375 - 4 312,50 = 10 062,50 kr.
|
||
const invoice = eurInvoiceWithoutRate({
|
||
exchange_rate: 11.5,
|
||
items: [
|
||
makeItem({
|
||
line_total: 1000,
|
||
unit_price: 1000,
|
||
quantity: 1,
|
||
vat_rate: 25,
|
||
vat_amount: 250,
|
||
deduction_type: 'rot',
|
||
} as Partial<InvoiceItem>),
|
||
],
|
||
})
|
||
|
||
await createInvoiceJournalEntry(null as never, 'company-1', 'user-1', invoice)
|
||
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
const debit1513 = input.lines.find((l) => l.account_number === '1513')
|
||
expect(debit1513?.debit_amount).toBe(4312.5)
|
||
expect(input.lines.find((l) => l.account_number === '1510')?.debit_amount).toBe(10062.5)
|
||
|
||
const totalDebit = input.lines.reduce((sum, l) => sum + l.debit_amount, 0)
|
||
const totalCredit = input.lines.reduce((sum, l) => sum + l.credit_amount, 0)
|
||
expect(Math.round(totalDebit * 100)).toBe(Math.round(totalCredit * 100))
|
||
})
|
||
|
||
it('a SEK invoice with no exchange rate is completely unaffected', async () => {
|
||
const invoice = makeInvoice({
|
||
currency: 'SEK',
|
||
exchange_rate: null,
|
||
subtotal: 1000,
|
||
vat_amount: 250,
|
||
total: 1250,
|
||
items: [makeItem({ line_total: 1000, vat_rate: 25, vat_amount: 250 })],
|
||
})
|
||
|
||
await createInvoiceJournalEntry(null as never, 'company-1', 'user-1', invoice)
|
||
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
const totalDebit = input.lines.reduce((sum, l) => sum + l.debit_amount, 0)
|
||
const totalCredit = input.lines.reduce((sum, l) => sum + l.credit_amount, 0)
|
||
expect(totalDebit).toBe(1250)
|
||
expect(totalDebit).toBe(totalCredit)
|
||
})
|
||
|
||
it('an EUR invoice WITH a rate still books, and still balances', async () => {
|
||
const invoice = eurInvoiceWithoutRate({ exchange_rate: 11.5 })
|
||
|
||
await createInvoiceJournalEntry(null as never, 'company-1', 'user-1', invoice)
|
||
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
expect(input.lines.find((l) => l.account_number === '3001')?.credit_amount).toBe(11500)
|
||
expect(input.lines.find((l) => l.account_number === '2611')?.credit_amount).toBe(2875)
|
||
const totalDebit = input.lines.reduce((sum, l) => sum + l.debit_amount, 0)
|
||
const totalCredit = input.lines.reduce((sum, l) => sum + l.credit_amount, 0)
|
||
expect(totalDebit).toBe(14375)
|
||
expect(totalDebit).toBe(totalCredit)
|
||
})
|
||
})
|
||
|
||
/**
|
||
* The no-items header fallback and the payment entry used to convert through
|
||
* the lenient resolveSekAmount ladder (READ-ONLY CODE ONLY per currency-utils):
|
||
* a rate-less foreign invoice booked via a caller without hydrated items posted
|
||
* its raw foreign number as kronor (1 250 EUR → 1 250 kr on 1510), balanced and
|
||
* undetectable. They now refuse with the same INVOICE_FX_RATE_MISSING code as
|
||
* the item-driven generators. Rows that DO carry *_sek or a usable rate keep
|
||
* byte-identical numbers.
|
||
*/
|
||
describe('header fallback and payment path: strict FX conversion', () => {
|
||
beforeEach(() => {
|
||
vi.clearAllMocks()
|
||
})
|
||
|
||
function eurHeaderOnlyInvoice(overrides: Partial<Invoice> = {}): Invoice {
|
||
return makeInvoice({
|
||
currency: 'EUR',
|
||
exchange_rate: null,
|
||
subtotal: 1000,
|
||
subtotal_sek: null,
|
||
vat_amount: 250,
|
||
vat_amount_sek: null,
|
||
total: 1250,
|
||
total_sek: null,
|
||
vat_treatment: 'standard_25',
|
||
items: [],
|
||
...overrides,
|
||
})
|
||
}
|
||
|
||
it('createInvoiceJournalEntry (no items, no rate, no *_sek) refuses instead of posting 1 250 EUR as 1 250 kr', async () => {
|
||
await expect(
|
||
createInvoiceJournalEntry(null as never, 'company-1', 'user-1', eurHeaderOnlyInvoice())
|
||
).rejects.toMatchObject({ code: 'INVOICE_FX_RATE_MISSING', currency: 'EUR' })
|
||
expect(mockedCreateEntry).not.toHaveBeenCalled()
|
||
})
|
||
|
||
it('createInvoiceJournalEntry (no items) books from *_sek columns even without a rate', async () => {
|
||
// Header rows carry SEK twins; those ARE an honest SEK source, so nothing
|
||
// changes for them: 11 500 + 2 875 credits, 14 375 debit on 1510.
|
||
const invoice = eurHeaderOnlyInvoice({
|
||
subtotal_sek: 11500,
|
||
vat_amount_sek: 2875,
|
||
total_sek: 14375,
|
||
})
|
||
|
||
await createInvoiceJournalEntry(null as never, 'company-1', 'user-1', invoice)
|
||
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
expect(input.lines.find((l) => l.account_number === '3001')?.credit_amount).toBe(11500)
|
||
expect(input.lines.find((l) => l.account_number === '2611')?.credit_amount).toBe(2875)
|
||
expect(input.lines.find((l) => l.account_number === '1510')?.debit_amount).toBe(14375)
|
||
|
||
const totalDebit = input.lines.reduce((sum, l) => sum + l.debit_amount, 0)
|
||
const totalCredit = input.lines.reduce((sum, l) => sum + l.credit_amount, 0)
|
||
expect(Math.round(totalDebit * 100)).toBe(Math.round(totalCredit * 100))
|
||
})
|
||
|
||
it('createInvoiceJournalEntry (no items) with a rate books converted, balanced lines', async () => {
|
||
// 1 000 EUR + 250 EUR moms at 11,50: 11 500 + 2 875 = 14 375 on 1510.
|
||
const invoice = eurHeaderOnlyInvoice({ exchange_rate: 11.5 })
|
||
|
||
await createInvoiceJournalEntry(null as never, 'company-1', 'user-1', invoice)
|
||
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
expect(input.lines.find((l) => l.account_number === '3001')?.credit_amount).toBe(11500)
|
||
expect(input.lines.find((l) => l.account_number === '2611')?.credit_amount).toBe(2875)
|
||
expect(input.lines.find((l) => l.account_number === '1510')?.debit_amount).toBe(14375)
|
||
})
|
||
|
||
it('createInvoiceCashEntry (no items, no rate) refuses too', async () => {
|
||
await expect(
|
||
createInvoiceCashEntry(null as never, 'company-1', 'user-1', eurHeaderOnlyInvoice(), '2024-07-01')
|
||
).rejects.toMatchObject({ code: 'INVOICE_FX_RATE_MISSING' })
|
||
expect(mockedCreateEntry).not.toHaveBeenCalled()
|
||
})
|
||
|
||
it('createCreditNoteJournalEntry (no items, no rate) refuses too', async () => {
|
||
const creditNote = eurHeaderOnlyInvoice({
|
||
invoice_number: 'KR-1001',
|
||
credited_invoice_id: 'inv-original',
|
||
subtotal: -1000,
|
||
vat_amount: -250,
|
||
total: -1250,
|
||
})
|
||
|
||
await expect(
|
||
createCreditNoteJournalEntry(null as never, 'company-1', 'user-1', creditNote)
|
||
).rejects.toMatchObject({ code: 'INVOICE_FX_RATE_MISSING' })
|
||
expect(mockedCreateEntry).not.toHaveBeenCalled()
|
||
})
|
||
|
||
it('createInvoicePaymentJournalEntry (full, no rate, no total_sek) refuses instead of clearing 1510 with a mislabelled number', async () => {
|
||
await expect(
|
||
createInvoicePaymentJournalEntry(
|
||
null as never, 'company-1', 'user-1', eurHeaderOnlyInvoice(), '2024-07-15'
|
||
)
|
||
).rejects.toMatchObject({ code: 'INVOICE_FX_RATE_MISSING', currency: 'EUR' })
|
||
expect(mockedCreateEntry).not.toHaveBeenCalled()
|
||
})
|
||
|
||
it('createInvoicePaymentJournalEntry (partial, no rate) refuses: paymentAmount is in EUR and has no SEK source', async () => {
|
||
await expect(
|
||
createInvoicePaymentJournalEntry(
|
||
null as never, 'company-1', 'user-1', eurHeaderOnlyInvoice(), '2024-07-15',
|
||
undefined, undefined, 400,
|
||
)
|
||
).rejects.toMatchObject({ code: 'INVOICE_FX_RATE_MISSING' })
|
||
expect(mockedCreateEntry).not.toHaveBeenCalled()
|
||
})
|
||
|
||
it('createInvoicePaymentJournalEntry (partial, with rate) converts the partial amount: 400 EUR at 11,50 = 4 600,00', async () => {
|
||
const invoice = eurHeaderOnlyInvoice({ exchange_rate: 11.5 })
|
||
|
||
await createInvoicePaymentJournalEntry(
|
||
null as never, 'company-1', 'user-1', invoice, '2024-07-15',
|
||
undefined, undefined, 400,
|
||
)
|
||
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
expect(input.lines).toHaveLength(2)
|
||
expect(input.lines.find((l) => l.account_number === '1930')?.debit_amount).toBe(4600)
|
||
expect(input.lines.find((l) => l.account_number === '1510')?.credit_amount).toBe(4600)
|
||
})
|
||
|
||
it('createInvoicePaymentJournalEntry keeps identical numbers when total_sek is present without a rate', async () => {
|
||
const invoice = eurHeaderOnlyInvoice({ total_sek: 11500 })
|
||
|
||
await createInvoicePaymentJournalEntry(null as never, 'company-1', 'user-1', invoice, '2024-07-15')
|
||
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
expect(input.lines.find((l) => l.account_number === '1930')?.debit_amount).toBe(11500)
|
||
expect(input.lines.find((l) => l.account_number === '1510')?.credit_amount).toBe(11500)
|
||
})
|
||
|
||
it('a SEK invoice without items or rate is completely unaffected', async () => {
|
||
const invoice = makeInvoice({
|
||
currency: 'SEK',
|
||
exchange_rate: null,
|
||
subtotal: 1000,
|
||
vat_amount: 250,
|
||
total: 1250,
|
||
items: [],
|
||
})
|
||
|
||
await createInvoiceJournalEntry(null as never, 'company-1', 'user-1', invoice)
|
||
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
const totalDebit = input.lines.reduce((sum, l) => sum + l.debit_amount, 0)
|
||
const totalCredit = input.lines.reduce((sum, l) => sum + l.credit_amount, 0)
|
||
expect(totalDebit).toBe(1250)
|
||
expect(totalDebit).toBe(totalCredit)
|
||
})
|
||
})
|
||
|
||
describe('BFL-compliant descriptions with counterparty names', () => {
|
||
beforeEach(() => {
|
||
vi.clearAllMocks()
|
||
})
|
||
|
||
it('createInvoiceJournalEntry includes customer name in description', async () => {
|
||
const invoice = makeInvoice({
|
||
items: [makeItem()],
|
||
})
|
||
|
||
await createInvoiceJournalEntry(null as never, 'company-1', 'user-1', invoice, 'enskild_firma', 'Foretag AB')
|
||
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
expect(input.description).toBe('Kundfaktura 1001, Foretag AB')
|
||
})
|
||
|
||
it('createInvoiceJournalEntry falls back without customer name', async () => {
|
||
const invoice = makeInvoice({
|
||
items: [makeItem()],
|
||
})
|
||
|
||
await createInvoiceJournalEntry(null as never, 'company-1', 'user-1', invoice, 'enskild_firma')
|
||
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
expect(input.description).toBe('Kundfaktura 1001')
|
||
})
|
||
|
||
it('createInvoicePaymentJournalEntry includes customer name', async () => {
|
||
const invoice = makeInvoice({ total: 1250 })
|
||
|
||
await createInvoicePaymentJournalEntry(null as never, 'company-1', 'user-1', invoice, '2024-07-15', undefined, 'Foretag AB')
|
||
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
expect(input.description).toBe('Inbetalning kundfaktura 1001, Foretag AB')
|
||
})
|
||
|
||
it('createInvoicePaymentJournalEntry falls back without customer name', async () => {
|
||
const invoice = makeInvoice({ total: 1250 })
|
||
|
||
await createInvoicePaymentJournalEntry(null as never, 'company-1', 'user-1', invoice, '2024-07-15')
|
||
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
expect(input.description).toBe('Inbetalning kundfaktura 1001')
|
||
})
|
||
|
||
it('createCreditNoteJournalEntry includes customer name', async () => {
|
||
const creditNote = makeInvoice({
|
||
invoice_number: 'KR-1001',
|
||
subtotal: -1000,
|
||
vat_amount: -250,
|
||
total: -1250,
|
||
items: [makeItem({ quantity: -1, line_total: -1000, vat_amount: -250 })],
|
||
})
|
||
|
||
await createCreditNoteJournalEntry(null as never, 'company-1', 'user-1', creditNote, 'enskild_firma', 'Foretag AB')
|
||
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
expect(input.description).toBe('Kreditfaktura KR-1001, Foretag AB')
|
||
})
|
||
|
||
it('createInvoiceCashEntry includes customer name', async () => {
|
||
const invoice = makeInvoice({
|
||
items: [makeItem()],
|
||
})
|
||
|
||
await createInvoiceCashEntry(null as never, 'company-1', 'user-1', invoice, '2024-07-01', 'enskild_firma', 'Foretag AB')
|
||
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
expect(input.description).toBe('Kontantbetalning kundfaktura 1001, Foretag AB')
|
||
})
|
||
|
||
it('createInvoiceCashEntry falls back without customer name', async () => {
|
||
const invoice = makeInvoice({
|
||
items: [makeItem()],
|
||
})
|
||
|
||
await createInvoiceCashEntry(null as never, 'company-1', 'user-1', invoice, '2024-07-01', 'enskild_firma')
|
||
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
expect(input.description).toBe('Kontantbetalning kundfaktura 1001')
|
||
})
|
||
})
|
||
|
||
describe('createInvoicePaymentJournalEntry: exchange rate difference', () => {
|
||
beforeEach(() => {
|
||
vi.clearAllMocks()
|
||
})
|
||
|
||
it('SEK payment creates simple 2-line entry', async () => {
|
||
const invoice = makeInvoice({ total: 1250 })
|
||
|
||
await createInvoicePaymentJournalEntry(null as never, 'company-1', 'user-1', invoice, '2024-07-15')
|
||
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
expect(input.lines).toHaveLength(2)
|
||
|
||
const debit1930 = input.lines.find((l) => l.account_number === '1930')
|
||
expect(debit1930?.debit_amount).toBe(1250)
|
||
|
||
const credit1510 = input.lines.find((l) => l.account_number === '1510')
|
||
expect(credit1510?.credit_amount).toBe(1250)
|
||
})
|
||
|
||
it('EUR payment with positive exchange rate difference (gain) creates 3 lines', async () => {
|
||
const invoice = makeInvoice({
|
||
currency: 'EUR',
|
||
exchange_rate: 11.5,
|
||
total: 1000,
|
||
total_sek: 11500,
|
||
})
|
||
|
||
// Gain of 200 SEK (received more than booked)
|
||
await createInvoicePaymentJournalEntry(null as never, 'company-1', 'user-1', invoice, '2024-07-15', 200)
|
||
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
expect(input.lines).toHaveLength(3)
|
||
|
||
// Debit 1930: actual SEK received = 11500 + 200 = 11700
|
||
const debit1930 = input.lines.find((l) => l.account_number === '1930')
|
||
expect(debit1930?.debit_amount).toBe(11700)
|
||
|
||
// Credit 1510: original booked amount
|
||
const credit1510 = input.lines.find((l) => l.account_number === '1510')
|
||
expect(credit1510?.credit_amount).toBe(11500)
|
||
|
||
// Credit 3960: exchange rate gain
|
||
const credit3960 = input.lines.find((l) => l.account_number === '3960')
|
||
expect(credit3960?.credit_amount).toBe(200)
|
||
|
||
// Balance check
|
||
const totalDebit = input.lines.reduce((sum, l) => sum + l.debit_amount, 0)
|
||
const totalCredit = input.lines.reduce((sum, l) => sum + l.credit_amount, 0)
|
||
expect(totalDebit).toBe(totalCredit)
|
||
})
|
||
|
||
it('EUR payment with negative exchange rate difference (loss) creates 3 lines', async () => {
|
||
const invoice = makeInvoice({
|
||
currency: 'EUR',
|
||
exchange_rate: 11.5,
|
||
total: 1000,
|
||
total_sek: 11500,
|
||
})
|
||
|
||
// Loss of 300 SEK (received less than booked)
|
||
await createInvoicePaymentJournalEntry(null as never, 'company-1', 'user-1', invoice, '2024-07-15', -300)
|
||
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
expect(input.lines).toHaveLength(3)
|
||
|
||
// Debit 1930: actual SEK received = 11500 + (-300) = 11200
|
||
const debit1930 = input.lines.find((l) => l.account_number === '1930')
|
||
expect(debit1930?.debit_amount).toBe(11200)
|
||
|
||
// Credit 1510: original booked amount
|
||
const credit1510 = input.lines.find((l) => l.account_number === '1510')
|
||
expect(credit1510?.credit_amount).toBe(11500)
|
||
|
||
// Debit 7960: exchange rate loss
|
||
const debit7960 = input.lines.find((l) => l.account_number === '7960')
|
||
expect(debit7960?.debit_amount).toBe(300)
|
||
|
||
// Balance check
|
||
const totalDebit = input.lines.reduce((sum, l) => sum + l.debit_amount, 0)
|
||
const totalCredit = input.lines.reduce((sum, l) => sum + l.credit_amount, 0)
|
||
expect(totalDebit).toBe(totalCredit)
|
||
})
|
||
})
|
||
|
||
describe('paymentAccount parameter (settlement-account resolution)', () => {
|
||
beforeEach(() => {
|
||
vi.clearAllMocks()
|
||
})
|
||
|
||
it('createInvoicePaymentJournalEntry defaults the bank leg to 1930 when paymentAccount is omitted', async () => {
|
||
const invoice = makeInvoice({ total: 1250 })
|
||
|
||
await createInvoicePaymentJournalEntry(null as never, 'company-1', 'user-1', invoice, '2024-07-15')
|
||
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
expect(input.lines.find((l) => l.account_number === '1930')?.debit_amount).toBe(1250)
|
||
})
|
||
|
||
it('createInvoicePaymentJournalEntry books the bank leg to the resolved account (SEK, no FX)', async () => {
|
||
const invoice = makeInvoice({ total: 1250 })
|
||
|
||
await createInvoicePaymentJournalEntry(
|
||
null as never, 'company-1', 'user-1', invoice, '2024-07-15',
|
||
undefined, undefined, undefined, '1940',
|
||
)
|
||
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
expect(input.lines.find((l) => l.account_number === '1940')?.debit_amount).toBe(1250)
|
||
expect(input.lines.find((l) => l.account_number === '1930')).toBeUndefined()
|
||
// 1510 credit is untouched by the payment-account override.
|
||
expect(input.lines.find((l) => l.account_number === '1510')?.credit_amount).toBe(1250)
|
||
})
|
||
|
||
it('createInvoicePaymentJournalEntry books the FX-branch bank leg to the resolved account, leaving 3960/1510 untouched', async () => {
|
||
const invoice = makeInvoice({
|
||
currency: 'EUR',
|
||
exchange_rate: 11.5,
|
||
total: 1000,
|
||
total_sek: 11500,
|
||
})
|
||
|
||
await createInvoicePaymentJournalEntry(
|
||
null as never, 'company-1', 'user-1', invoice, '2024-07-15',
|
||
200, undefined, undefined, '1940',
|
||
)
|
||
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
expect(input.lines.find((l) => l.account_number === '1940')?.debit_amount).toBe(11700)
|
||
expect(input.lines.find((l) => l.account_number === '1930')).toBeUndefined()
|
||
expect(input.lines.find((l) => l.account_number === '1510')?.credit_amount).toBe(11500)
|
||
expect(input.lines.find((l) => l.account_number === '3960')?.credit_amount).toBe(200)
|
||
})
|
||
|
||
it('createInvoiceCashEntry defaults the bank leg to 1930 when paymentAccount is omitted', async () => {
|
||
const invoice = makeInvoice({ total: 1198 })
|
||
|
||
await createInvoiceCashEntry(null as never, 'company-1', 'user-1', invoice, '2024-07-01')
|
||
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
expect(input.lines.find((l) => l.account_number === '1930')?.debit_amount).toBe(1198)
|
||
})
|
||
|
||
it('createInvoiceCashEntry books the bank leg to the resolved account, leaving revenue/VAT credits untouched', async () => {
|
||
const invoice = makeInvoice({
|
||
subtotal: 1000,
|
||
vat_amount: 198,
|
||
total: 1198,
|
||
vat_treatment: 'standard_25',
|
||
})
|
||
|
||
await createInvoiceCashEntry(
|
||
null as never, 'company-1', 'user-1', invoice, '2024-07-01',
|
||
'enskild_firma', undefined, '1940',
|
||
)
|
||
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
expect(input.lines.find((l) => l.account_number === '1940')?.debit_amount).toBe(1198)
|
||
expect(input.lines.find((l) => l.account_number === '1930')).toBeUndefined()
|
||
expect(input.lines.find((l) => l.account_number === '3001')?.credit_amount).toBe(1000)
|
||
expect(input.lines.find((l) => l.account_number === '2611')?.credit_amount).toBe(198)
|
||
})
|
||
})
|
||
|
||
describe('createInvoiceJournalEntry: ROT/RUT-avdrag', () => {
|
||
beforeEach(() => {
|
||
vi.clearAllMocks()
|
||
})
|
||
|
||
it('single ROT line: 10 000 kr labor → 1513 debit 3 750, 1510 debit 8 750', async () => {
|
||
// 10 000 kr labor with 25% VAT = 12 500 total. ROT = 30% of the
|
||
// inkl.-moms labor (HUSFL 6-9 §§) = 30% of 12 500 = 3 750.
|
||
// Customer owes (12 500 - 3 750) = 8 750. Skatteverket pays 3 750.
|
||
const invoice = makeInvoice({
|
||
subtotal: 10000,
|
||
vat_amount: 2500,
|
||
total: 12500,
|
||
vat_treatment: 'standard_25',
|
||
items: [
|
||
makeItem({
|
||
quantity: 1,
|
||
unit_price: 10000,
|
||
line_total: 10000,
|
||
vat_rate: 25,
|
||
vat_amount: 2500,
|
||
deduction_type: 'rot',
|
||
deduction_amount: 3000,
|
||
}),
|
||
],
|
||
})
|
||
|
||
await createInvoiceJournalEntry(null as never, 'company-1', 'user-1', invoice)
|
||
|
||
expect(mockedCreateEntry).toHaveBeenCalledOnce()
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
|
||
// Lines: 1510 (debit 8750) + 1513 (debit 3750) + 3001 (credit 10000) + 2611 (credit 2500)
|
||
expect(input.lines).toHaveLength(4)
|
||
|
||
const debit1510 = input.lines.find((l) => l.account_number === '1510')
|
||
expect(debit1510?.debit_amount).toBe(8750)
|
||
|
||
const debit1513 = input.lines.find((l) => l.account_number === '1513')
|
||
expect(debit1513?.debit_amount).toBe(3750)
|
||
expect(debit1513?.credit_amount).toBe(0)
|
||
|
||
const credit3001 = input.lines.find((l) => l.account_number === '3001')
|
||
expect(credit3001?.credit_amount).toBe(10000)
|
||
|
||
const credit2611 = input.lines.find((l) => l.account_number === '2611')
|
||
expect(credit2611?.credit_amount).toBe(2500)
|
||
|
||
// Balance: 8750 + 3750 = 12500 = 10000 + 2500
|
||
const totalDebit = input.lines.reduce((sum, l) => sum + l.debit_amount, 0)
|
||
const totalCredit = input.lines.reduce((sum, l) => sum + l.credit_amount, 0)
|
||
expect(totalDebit).toBe(totalCredit)
|
||
expect(totalDebit).toBe(12500)
|
||
})
|
||
|
||
it('mixed invoice: ROT line + non-deduction line, per-item handling', async () => {
|
||
// ROT line 10 000 (deduction 30% of 12 500 inkl. moms = 3 750) +
|
||
// non-deduction materials line 4 000.
|
||
// Total 14 000 + 25% VAT = 17 500. Customer owes 13 750. Skatteverket 3 750.
|
||
const invoice = makeInvoice({
|
||
subtotal: 14000,
|
||
vat_amount: 3500,
|
||
total: 17500,
|
||
vat_treatment: 'standard_25',
|
||
items: [
|
||
makeItem({
|
||
quantity: 1,
|
||
unit_price: 10000,
|
||
line_total: 10000,
|
||
vat_rate: 25,
|
||
vat_amount: 2500,
|
||
deduction_type: 'rot',
|
||
deduction_amount: 3000,
|
||
}),
|
||
makeItem({
|
||
id: 'item-2',
|
||
quantity: 1,
|
||
unit_price: 4000,
|
||
line_total: 4000,
|
||
vat_rate: 25,
|
||
vat_amount: 1000,
|
||
// No deduction
|
||
}),
|
||
],
|
||
})
|
||
|
||
await createInvoiceJournalEntry(null as never, 'company-1', 'user-1', invoice)
|
||
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
|
||
// Lines: 1510 (debit 13750) + 1513 (debit 3750) + 3001 (credit 14000) + 2611 (credit 3500)
|
||
expect(input.lines).toHaveLength(4)
|
||
|
||
const debit1510 = input.lines.find((l) => l.account_number === '1510')
|
||
expect(debit1510?.debit_amount).toBe(13750)
|
||
|
||
const debit1513 = input.lines.find((l) => l.account_number === '1513')
|
||
expect(debit1513?.debit_amount).toBe(3750)
|
||
|
||
// Balance: 13750 + 3750 = 17500
|
||
const totalDebit = input.lines.reduce((sum, l) => sum + l.debit_amount, 0)
|
||
const totalCredit = input.lines.reduce((sum, l) => sum + l.credit_amount, 0)
|
||
expect(totalDebit).toBe(totalCredit)
|
||
expect(totalDebit).toBe(17500)
|
||
})
|
||
|
||
it('RUT line with 50% rate: 5 000 kr → 1513 debit 3 125', async () => {
|
||
// 5 000 labor with 25% VAT = 6 250 total. RUT = 50% of the inkl.-moms
|
||
// labor = 50% of 6 250 = 3 125.
|
||
const invoice = makeInvoice({
|
||
subtotal: 5000,
|
||
vat_amount: 1250,
|
||
total: 6250,
|
||
vat_treatment: 'standard_25',
|
||
items: [
|
||
makeItem({
|
||
quantity: 1,
|
||
unit_price: 5000,
|
||
line_total: 5000,
|
||
vat_rate: 25,
|
||
vat_amount: 1250,
|
||
deduction_type: 'rut',
|
||
deduction_amount: 2500,
|
||
}),
|
||
],
|
||
})
|
||
|
||
await createInvoiceJournalEntry(null as never, 'company-1', 'user-1', invoice)
|
||
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
|
||
const debit1513 = input.lines.find((l) => l.account_number === '1513')
|
||
expect(debit1513?.debit_amount).toBe(3125)
|
||
expect(debit1513?.line_description).toMatch(/RUT/)
|
||
|
||
const debit1510 = input.lines.find((l) => l.account_number === '1510')
|
||
expect(debit1510?.debit_amount).toBe(3125) // 6250 - 3125
|
||
|
||
// Balance
|
||
const totalDebit = input.lines.reduce((sum, l) => sum + l.debit_amount, 0)
|
||
const totalCredit = input.lines.reduce((sum, l) => sum + l.credit_amount, 0)
|
||
expect(totalDebit).toBe(totalCredit)
|
||
})
|
||
|
||
it('no deduction_type → no 1513 line, normal AR debit', async () => {
|
||
const invoice = makeInvoice({
|
||
subtotal: 1000,
|
||
vat_amount: 250,
|
||
total: 1250,
|
||
items: [
|
||
makeItem({ quantity: 1, unit_price: 1000, line_total: 1000, vat_rate: 25, vat_amount: 250 }),
|
||
],
|
||
})
|
||
|
||
await createInvoiceJournalEntry(null as never, 'company-1', 'user-1', invoice)
|
||
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
|
||
expect(input.lines.find((l) => l.account_number === '1513')).toBeUndefined()
|
||
const debit1510 = input.lines.find((l) => l.account_number === '1510')
|
||
expect(debit1510?.debit_amount).toBe(1250)
|
||
})
|
||
|
||
it('two ROT lines: per-line 1513 debits sum to invoice deduction total', async () => {
|
||
// 6 000 + 4 000 labor @ 25%, both ROT 30% of the inkl.-moms line:
|
||
// 30% x 7 500 + 30% x 5 000 = 2 250 + 1 500 = 3 750 total.
|
||
const invoice = makeInvoice({
|
||
subtotal: 10000,
|
||
vat_amount: 2500,
|
||
total: 12500,
|
||
vat_treatment: 'standard_25',
|
||
items: [
|
||
makeItem({
|
||
quantity: 1,
|
||
unit_price: 6000,
|
||
line_total: 6000,
|
||
vat_rate: 25,
|
||
vat_amount: 1500,
|
||
deduction_type: 'rot',
|
||
deduction_amount: 1800,
|
||
}),
|
||
makeItem({
|
||
id: 'item-2',
|
||
quantity: 1,
|
||
unit_price: 4000,
|
||
line_total: 4000,
|
||
vat_rate: 25,
|
||
vat_amount: 1000,
|
||
deduction_type: 'rot',
|
||
deduction_amount: 1200,
|
||
}),
|
||
],
|
||
})
|
||
|
||
await createInvoiceJournalEntry(null as never, 'company-1', 'user-1', invoice)
|
||
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
|
||
const debit1513Lines = input.lines.filter((l) => l.account_number === '1513')
|
||
expect(debit1513Lines).toHaveLength(2)
|
||
const total1513 = debit1513Lines.reduce((sum, l) => sum + l.debit_amount, 0)
|
||
expect(total1513).toBe(3750)
|
||
|
||
const debit1510 = input.lines.find((l) => l.account_number === '1510')
|
||
expect(debit1510?.debit_amount).toBe(8750) // 12500 - 3750
|
||
|
||
// Balance
|
||
const totalDebit = input.lines.reduce((sum, l) => sum + l.debit_amount, 0)
|
||
const totalCredit = input.lines.reduce((sum, l) => sum + l.credit_amount, 0)
|
||
expect(totalDebit).toBe(totalCredit)
|
||
})
|
||
})
|
||
|
||
describe('dimensions propagation (PR7): createInvoiceJournalEntry', () => {
|
||
beforeEach(() => {
|
||
vi.clearAllMocks()
|
||
})
|
||
|
||
it('1510 + VAT lines carry the invoice default; revenue lines carry the merged item-over-default bag', async () => {
|
||
const invoice = makeInvoice({
|
||
subtotal: 1000,
|
||
vat_amount: 250,
|
||
total: 1250,
|
||
vat_treatment: 'standard_25',
|
||
default_dimensions: { '1': 'KS01' },
|
||
items: [
|
||
makeItem({ description: 'A', unit_price: 600, line_total: 600, vat_rate: 25, vat_amount: 150, dimensions: { '6': 'P001' } }),
|
||
makeItem({ id: 'item-2', description: 'B', unit_price: 400, line_total: 400, vat_rate: 25, vat_amount: 100, dimensions: { '1': 'KS02' } }),
|
||
],
|
||
})
|
||
|
||
await createInvoiceJournalEntry(null as never, 'company-1', 'user-1', invoice)
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
|
||
const debit1510 = input.lines.find((l) => l.account_number === '1510')
|
||
expect(debit1510?.dimensions).toEqual({ '1': 'KS01' })
|
||
|
||
const vat2611 = input.lines.filter((l) => l.account_number === '2611')
|
||
expect(vat2611).toHaveLength(1)
|
||
expect(vat2611[0].dimensions).toEqual({ '1': 'KS01' })
|
||
|
||
// Same vat_rate + account, DIFFERENT bags → separate revenue lines
|
||
// (aggregation identity = account + bag).
|
||
const rev3001 = input.lines.filter((l) => l.account_number === '3001')
|
||
expect(rev3001).toHaveLength(2)
|
||
expect(rev3001[0].credit_amount).toBe(600)
|
||
expect(rev3001[0].dimensions).toEqual({ '1': 'KS01', '6': 'P001' })
|
||
expect(rev3001[1].credit_amount).toBe(400)
|
||
// The item bag wins per key over the invoice default.
|
||
expect(rev3001[1].dimensions).toEqual({ '1': 'KS02' })
|
||
})
|
||
|
||
it('items with the identical merged bag still aggregate into one revenue line', async () => {
|
||
const invoice = makeInvoice({
|
||
subtotal: 1000,
|
||
vat_amount: 250,
|
||
total: 1250,
|
||
vat_treatment: 'standard_25',
|
||
default_dimensions: { '1': 'KS01' },
|
||
items: [
|
||
makeItem({ description: 'A', unit_price: 600, line_total: 600, vat_rate: 25, vat_amount: 150, dimensions: { '6': 'P001' } }),
|
||
makeItem({ id: 'item-2', description: 'B', unit_price: 400, line_total: 400, vat_rate: 25, vat_amount: 100, dimensions: { '6': 'P001' } }),
|
||
],
|
||
})
|
||
|
||
await createInvoiceJournalEntry(null as never, 'company-1', 'user-1', invoice)
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
|
||
const rev3001 = input.lines.filter((l) => l.account_number === '3001')
|
||
expect(rev3001).toHaveLength(1)
|
||
expect(rev3001[0].credit_amount).toBe(1000)
|
||
expect(rev3001[0].dimensions).toEqual({ '1': 'KS01', '6': 'P001' })
|
||
})
|
||
|
||
it('per-rate rounding remainder is absorbed by the last dimension bucket (balanced against 1510)', async () => {
|
||
// Same account, same 25% rate: split only by the dimensions bag. The
|
||
// rate-level total (20.01) is the balance anchor; independent per-bucket
|
||
// rounding would give 2 × 10.01 = 20.02, so the last bucket must absorb.
|
||
const invoice = makeInvoice({
|
||
subtotal: 20.01,
|
||
vat_amount: 5.0,
|
||
total: 25.01,
|
||
vat_treatment: 'standard_25',
|
||
vat_rate: null as unknown as number,
|
||
items: [
|
||
makeItem({ description: 'A', unit_price: 10.005, line_total: 10.005, vat_rate: 25, vat_amount: 2.5 }),
|
||
makeItem({ id: 'item-2', description: 'B', unit_price: 10.005, line_total: 10.005, vat_rate: 25, vat_amount: 2.5, dimensions: { '6': 'P001' } }),
|
||
],
|
||
})
|
||
|
||
await createInvoiceJournalEntry(null as never, 'company-1', 'user-1', invoice)
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
|
||
const rev3001 = input.lines.filter((l) => l.account_number === '3001')
|
||
expect(rev3001).toHaveLength(2)
|
||
expect(rev3001[0].dimensions).toBeUndefined()
|
||
expect(rev3001[1].dimensions).toEqual({ '6': 'P001' })
|
||
|
||
const revSum = rev3001.reduce((s, l) => s + l.credit_amount, 0)
|
||
expect(roundOre(revSum)).toBe(20.01)
|
||
|
||
const debit = roundOre(input.lines.reduce((s, l) => s + l.debit_amount, 0))
|
||
const credit = roundOre(input.lines.reduce((s, l) => s + l.credit_amount, 0))
|
||
expect(debit).toBe(credit)
|
||
expect(debit).toBe(25.01)
|
||
})
|
||
|
||
it('ROT 1513 line carries the item merged bag', async () => {
|
||
const invoice = makeInvoice({
|
||
subtotal: 10000,
|
||
vat_amount: 2500,
|
||
total: 12500,
|
||
vat_treatment: 'standard_25',
|
||
default_dimensions: { '1': 'KS01' },
|
||
items: [
|
||
makeItem({
|
||
quantity: 1,
|
||
unit_price: 10000,
|
||
line_total: 10000,
|
||
vat_rate: 25,
|
||
vat_amount: 2500,
|
||
deduction_type: 'rot',
|
||
deduction_amount: 3000,
|
||
dimensions: { '6': 'P001' },
|
||
}),
|
||
],
|
||
})
|
||
|
||
await createInvoiceJournalEntry(null as never, 'company-1', 'user-1', invoice)
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
|
||
const debit1513 = input.lines.find((l) => l.account_number === '1513')
|
||
expect(debit1513?.debit_amount).toBe(3750)
|
||
expect(debit1513?.dimensions).toEqual({ '1': 'KS01', '6': 'P001' })
|
||
|
||
// 1510 still carries the default only.
|
||
const debit1510 = input.lines.find((l) => l.account_number === '1510')
|
||
expect(debit1510?.dimensions).toEqual({ '1': 'KS01' })
|
||
})
|
||
|
||
it('fallback path (no items) carries the invoice default on revenue + VAT lines', async () => {
|
||
const invoice = makeInvoice({
|
||
subtotal: 1000,
|
||
vat_amount: 250,
|
||
total: 1250,
|
||
vat_treatment: 'standard_25',
|
||
default_dimensions: { '1': 'KS01', '6': 'P001' },
|
||
items: [],
|
||
})
|
||
|
||
await createInvoiceJournalEntry(null as never, 'company-1', 'user-1', invoice)
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
|
||
const rev3001 = input.lines.find((l) => l.account_number === '3001')
|
||
expect(rev3001?.dimensions).toEqual({ '1': 'KS01', '6': 'P001' })
|
||
const vat2611 = input.lines.find((l) => l.account_number === '2611')
|
||
expect(vat2611?.dimensions).toEqual({ '1': 'KS01', '6': 'P001' })
|
||
const debit1510 = input.lines.find((l) => l.account_number === '1510')
|
||
expect(debit1510?.dimensions).toEqual({ '1': 'KS01', '6': 'P001' })
|
||
})
|
||
|
||
it('no default and no item bags → line.dimensions stays undefined', async () => {
|
||
const invoice = makeInvoice({
|
||
subtotal: 1000,
|
||
vat_amount: 250,
|
||
total: 1250,
|
||
vat_treatment: 'standard_25',
|
||
items: [
|
||
makeItem({ unit_price: 1000, line_total: 1000, vat_rate: 25, vat_amount: 250 }),
|
||
],
|
||
})
|
||
|
||
await createInvoiceJournalEntry(null as never, 'company-1', 'user-1', invoice)
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
|
||
const debit1510 = input.lines.find((l) => l.account_number === '1510')
|
||
// toEqual ignores undefined-valued keys: the line shape is unchanged.
|
||
expect(debit1510).toEqual({
|
||
account_number: '1510',
|
||
debit_amount: 1250,
|
||
credit_amount: 0,
|
||
line_description: 'Faktura 1001',
|
||
})
|
||
expect(debit1510?.dimensions).toBeUndefined()
|
||
expect(input.lines.find((l) => l.account_number === '3001')?.dimensions).toBeUndefined()
|
||
expect(input.lines.find((l) => l.account_number === '2611')?.dimensions).toBeUndefined()
|
||
})
|
||
})
|
||
|
||
describe('dimensions propagation (PR7): createInvoicePaymentJournalEntry', () => {
|
||
beforeEach(() => {
|
||
vi.clearAllMocks()
|
||
})
|
||
|
||
it('SEK payment: both 1930 and 1510 carry the invoice default', async () => {
|
||
const invoice = makeInvoice({ total: 1250, default_dimensions: { '1': 'KS01', '6': 'P001' } })
|
||
|
||
await createInvoicePaymentJournalEntry(null as never, 'company-1', 'user-1', invoice, '2024-07-15')
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
|
||
expect(input.lines).toHaveLength(2)
|
||
for (const line of input.lines) {
|
||
expect(line.dimensions).toEqual({ '1': 'KS01', '6': 'P001' })
|
||
}
|
||
})
|
||
|
||
it('FX payment: the 3960 kursvinst line carries the default too', async () => {
|
||
const invoice = makeInvoice({
|
||
currency: 'EUR',
|
||
exchange_rate: 11.5,
|
||
total: 1000,
|
||
total_sek: 11500,
|
||
default_dimensions: { '6': 'P001' },
|
||
})
|
||
|
||
await createInvoicePaymentJournalEntry(null as never, 'company-1', 'user-1', invoice, '2024-07-15', 200)
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
|
||
expect(input.lines).toHaveLength(3)
|
||
for (const account of ['1930', '1510', '3960']) {
|
||
const line = input.lines.find((l) => l.account_number === account)
|
||
expect(line?.dimensions).toEqual({ '6': 'P001' })
|
||
}
|
||
})
|
||
|
||
it('FX loss payment: the 7960 kursförlust line carries the default too', async () => {
|
||
const invoice = makeInvoice({
|
||
currency: 'EUR',
|
||
exchange_rate: 11.5,
|
||
total: 1000,
|
||
total_sek: 11500,
|
||
default_dimensions: { '6': 'P001' },
|
||
})
|
||
|
||
await createInvoicePaymentJournalEntry(null as never, 'company-1', 'user-1', invoice, '2024-07-15', -300)
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
|
||
const loss7960 = input.lines.find((l) => l.account_number === '7960')
|
||
expect(loss7960?.dimensions).toEqual({ '6': 'P001' })
|
||
})
|
||
|
||
it('no default bag → payment lines carry no dimensions', async () => {
|
||
const invoice = makeInvoice({ total: 1250 })
|
||
|
||
await createInvoicePaymentJournalEntry(null as never, 'company-1', 'user-1', invoice, '2024-07-15')
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
|
||
for (const line of input.lines) {
|
||
expect(line.dimensions).toBeUndefined()
|
||
}
|
||
})
|
||
})
|
||
|
||
describe('dimensions propagation (PR7): createInvoiceCashEntry', () => {
|
||
beforeEach(() => {
|
||
vi.clearAllMocks()
|
||
})
|
||
|
||
it('1930 carries the default; revenue carries the merged item bag; VAT carries the default', async () => {
|
||
const invoice = makeInvoice({
|
||
subtotal: 1000,
|
||
vat_amount: 250,
|
||
total: 1250,
|
||
vat_treatment: 'standard_25',
|
||
default_dimensions: { '1': 'KS01' },
|
||
items: [
|
||
makeItem({ unit_price: 1000, line_total: 1000, vat_rate: 25, vat_amount: 250, dimensions: { '6': 'P001' } }),
|
||
],
|
||
})
|
||
|
||
await createInvoiceCashEntry(null as never, 'company-1', 'user-1', invoice, '2024-07-01')
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
|
||
const debit1930 = input.lines.find((l) => l.account_number === '1930')
|
||
expect(debit1930?.dimensions).toEqual({ '1': 'KS01' })
|
||
|
||
const rev3001 = input.lines.find((l) => l.account_number === '3001')
|
||
expect(rev3001?.dimensions).toEqual({ '1': 'KS01', '6': 'P001' })
|
||
|
||
const vat2611 = input.lines.find((l) => l.account_number === '2611')
|
||
expect(vat2611?.dimensions).toEqual({ '1': 'KS01' })
|
||
})
|
||
})
|
||
|
||
describe('dimensions propagation (PR7): createCreditNoteJournalEntry', () => {
|
||
beforeEach(() => {
|
||
vi.clearAllMocks()
|
||
})
|
||
|
||
it('swapped lines keep the item bags; 1510 carries the credit note default', async () => {
|
||
const creditNote = makeInvoice({
|
||
invoice_number: 'KR-1001',
|
||
subtotal: -1000,
|
||
vat_amount: -198,
|
||
total: -1198,
|
||
vat_treatment: 'standard_25',
|
||
default_dimensions: { '1': 'KS01' },
|
||
items: [
|
||
makeItem({ quantity: -1, unit_price: 600, line_total: -600, vat_rate: 25, vat_amount: -150, dimensions: { '6': 'P001' } }),
|
||
makeItem({ id: 'item-2', quantity: -1, unit_price: 400, line_total: -400, vat_rate: 12, vat_amount: -48 }),
|
||
],
|
||
})
|
||
|
||
await createCreditNoteJournalEntry(null as never, 'company-1', 'user-1', creditNote)
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
|
||
// The reversed (debit) revenue lines keep the merged item bags.
|
||
const debit3001 = input.lines.find((l) => l.account_number === '3001')
|
||
expect(debit3001?.debit_amount).toBe(600)
|
||
expect(debit3001?.dimensions).toEqual({ '1': 'KS01', '6': 'P001' })
|
||
|
||
const debit3002 = input.lines.find((l) => l.account_number === '3002')
|
||
expect(debit3002?.dimensions).toEqual({ '1': 'KS01' })
|
||
|
||
// VAT lines carry the default only.
|
||
expect(input.lines.find((l) => l.account_number === '2611')?.dimensions).toEqual({ '1': 'KS01' })
|
||
expect(input.lines.find((l) => l.account_number === '2621')?.dimensions).toEqual({ '1': 'KS01' })
|
||
|
||
// 1510 carries the credit note's own default bag.
|
||
const credit1510 = input.lines.find((l) => l.account_number === '1510')
|
||
expect(credit1510?.credit_amount).toBe(1198)
|
||
expect(credit1510?.dimensions).toEqual({ '1': 'KS01' })
|
||
})
|
||
})
|
||
|
||
describe('createInvoiceCashEntry: ROT/RUT-avdrag', () => {
|
||
beforeEach(() => {
|
||
vi.clearAllMocks()
|
||
})
|
||
|
||
it('cash method ROT: 1930 debit reduced by deduction, 1513 carries the rest', async () => {
|
||
const invoice = makeInvoice({
|
||
subtotal: 10000,
|
||
vat_amount: 2500,
|
||
total: 12500,
|
||
vat_treatment: 'standard_25',
|
||
items: [
|
||
makeItem({
|
||
quantity: 1,
|
||
unit_price: 10000,
|
||
line_total: 10000,
|
||
vat_rate: 25,
|
||
vat_amount: 2500,
|
||
deduction_type: 'rot',
|
||
deduction_amount: 3000,
|
||
}),
|
||
],
|
||
})
|
||
|
||
await createInvoiceCashEntry(null as never, 'company-1', 'user-1', invoice, '2024-07-01')
|
||
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
|
||
const debit1930 = input.lines.find((l) => l.account_number === '1930')
|
||
expect(debit1930?.debit_amount).toBe(8750)
|
||
|
||
const debit1513 = input.lines.find((l) => l.account_number === '1513')
|
||
expect(debit1513?.debit_amount).toBe(3750)
|
||
|
||
// Balance
|
||
const totalDebit = input.lines.reduce((sum, l) => sum + l.debit_amount, 0)
|
||
const totalCredit = input.lines.reduce((sum, l) => sum + l.credit_amount, 0)
|
||
expect(totalDebit).toBe(totalCredit)
|
||
})
|
||
|
||
it('cash method ROT with a non-default paymentAccount: bank leg moves, 1513 stays fixed', async () => {
|
||
const invoice = makeInvoice({
|
||
subtotal: 10000,
|
||
vat_amount: 2500,
|
||
total: 12500,
|
||
vat_treatment: 'standard_25',
|
||
items: [
|
||
makeItem({
|
||
quantity: 1,
|
||
unit_price: 10000,
|
||
line_total: 10000,
|
||
vat_rate: 25,
|
||
vat_amount: 2500,
|
||
deduction_type: 'rot',
|
||
deduction_amount: 3000,
|
||
}),
|
||
],
|
||
})
|
||
|
||
await createInvoiceCashEntry(
|
||
null as never, 'company-1', 'user-1', invoice, '2024-07-01',
|
||
'enskild_firma', undefined, '1940',
|
||
)
|
||
|
||
const input = mockedCreateEntry.mock.calls[0][3]
|
||
|
||
// The bank leg follows the resolved paymentAccount, still reduced by the deduction.
|
||
const debit1940 = input.lines.find((l) => l.account_number === '1940')
|
||
expect(debit1940?.debit_amount).toBe(8750)
|
||
expect(input.lines.find((l) => l.account_number === '1930')).toBeUndefined()
|
||
|
||
// The ROT/RUT receivable from Skatteverket is never the bank leg, so it
|
||
// must stay on 1513 regardless of paymentAccount.
|
||
const debit1513NonDefault = input.lines.find((l) => l.account_number === '1513')
|
||
expect(debit1513NonDefault?.debit_amount).toBe(3750)
|
||
|
||
// Balance
|
||
const totalDebit = input.lines.reduce((sum, l) => sum + l.debit_amount, 0)
|
||
const totalCredit = input.lines.reduce((sum, l) => sum + l.credit_amount, 0)
|
||
expect(totalDebit).toBe(totalCredit)
|
||
})
|
||
})
|