Voucher series switcher (#219)

* Implement company and account deletion features

- Add event types for company and account deletion to CoreEvent.
- Enhance Supabase middleware to handle company context resolution and cookie management for archived companies.
- Create API routes for deleting accounts and companies, including necessary validations and event emissions.
- Implement tests for account and company deletion endpoints to ensure proper functionality and error handling.
- Add retention notice component to inform users about bookkeeping data retention during destructive actions.
- Create database migrations to support soft deletion of companies and anonymization of user accounts, ensuring compliance with retention laws.

* feat: enhance account deletion process and update user notifications

* Add service client for onboarding completion check and update escape hatch visibility

* Enhance invite flow and email handling for company members

* Refactor company context and RLS policies for active company isolation

- Update `switchCompany` to remove unnecessary revalidation as client handles navigation.
- Revise `getActiveCompanyId` to prioritize `user_preferences` and validate against non-archived memberships.
- Modify `setActiveCompany` to ensure `user_preferences` is the authoritative source while maintaining cookie compatibility.
- Enhance middleware to resolve active company using `user_preferences` and fallback to first non-archived membership.
- Introduce new API route `/api/company/current` to fetch the active company ID for cross-tab synchronization.
- Implement `CompanyTabSync` component for real-time active company enforcement across tabs.
- Create migration for RLS policies to enforce single-active-company isolation using `current_active_company_id()`.

* feat: implement viewer role enforcement for write permissions

- Added `useCanWrite` hook to determine if the current user has write permissions based on their role in the active company.
- Updated various components (JournalEntryForm, CustomerForm, DeadlineForm, etc.) to disable write actions and show a lock icon with a tooltip for users without write permissions.
- Introduced `requireWritePermission` function to enforce write permissions at the API level, returning a 403 response for viewers.
- Created tests to verify the behavior of the viewer role and write permissions.
- Added database migration to enforce read-only access for viewers at the database level.

* Add default voucher series configuration for manual journal entries
This commit is contained in:
Mattsson
2026-04-12 23:39:14 +02:00
committed by GitHub
parent a1a816b4a5
commit 973a2b81a0
8 changed files with 106 additions and 9 deletions
+30 -1
View File
@@ -72,6 +72,7 @@ export default function JournalEntryForm({
const [lines, setLines] = useState<FormLine[]>(
initialLines ?? [{ ...BLANK_LINE }, { ...BLANK_LINE }]
)
const [voucherSeries, setVoucherSeries] = useState('A')
const [isSubmitting, setIsSubmitting] = useState(false)
const [showReview, setShowReview] = useState(false)
const [showNoDocWarning, setShowNoDocWarning] = useState(false)
@@ -109,6 +110,12 @@ export default function JournalEntryForm({
useEffect(() => {
fetchPeriods()
fetchAccounts()
// Fetch default voucher series from company settings
if (!embedded) {
fetch('/api/settings').then(r => r.json()).then(({ data }) => {
if (data?.default_voucher_series) setVoucherSeries(data.default_voucher_series)
}).catch(() => {/* keep 'A' */})
}
}, [])
// Fetch exchange rate from Riksbanken when currency changes
@@ -250,6 +257,7 @@ export default function JournalEntryForm({
description,
source_type: sourceType ?? 'manual',
source_id: sourceId,
voucher_series: voucherSeries || 'A',
lines: entryLines,
}),
})
@@ -311,7 +319,13 @@ export default function JournalEntryForm({
const formContent = (
<div className="space-y-4">
<div className={`grid gap-4 grid-cols-1 ${embedded && initialDate ? 'sm:grid-cols-2' : 'sm:grid-cols-3'}`}>
<div className={`grid gap-4 grid-cols-1 ${
embedded && initialDate
? 'sm:grid-cols-2'
: embedded
? 'sm:grid-cols-3'
: 'sm:grid-cols-[1fr_auto_1fr_3.5rem]'
}`}>
<div>
<Label>Räkenskapsår</Label>
<Select value={selectedPeriod} onValueChange={setSelectedPeriod}>
@@ -345,6 +359,20 @@ export default function JournalEntryForm({
placeholder="Verifikationstext..."
/>
</div>
{!embedded && (
<div>
<Label>Serie</Label>
<Input
value={voucherSeries}
onChange={(e) => {
const v = e.target.value.toUpperCase().replace(/[^A-Z]/g, '').slice(0, 1)
setVoucherSeries(v || 'A')
}}
className="mt-1 text-center font-mono"
maxLength={1}
/>
</div>
)}
</div>
{/* Currency section */}
@@ -644,6 +672,7 @@ export default function JournalEntryForm({
periodName={periods.find((p) => p.id === selectedPeriod)?.name || ''}
entryDate={entryDate}
description={description}
voucherSeries={!embedded ? voucherSeries : undefined}
lines={lines}
totalDebit={totalDebit}
totalCredit={totalCredit}
@@ -15,6 +15,7 @@ interface JournalEntryReviewContentProps {
periodName: string
entryDate: string
description: string
voucherSeries?: string
lines: ReviewLine[]
totalDebit: number
totalCredit: number
@@ -31,6 +32,7 @@ export function JournalEntryReviewContent({
periodName,
entryDate,
description,
voucherSeries,
lines,
totalDebit,
totalCredit,
@@ -46,7 +48,7 @@ export function JournalEntryReviewContent({
<div className="space-y-4">
{/* Header info */}
<div className="bg-muted rounded-lg p-4 space-y-2">
<div className={`grid gap-4 text-sm ${hideDate ? 'grid-cols-1' : 'grid-cols-2'}`}>
<div className={`grid gap-4 text-sm ${hideDate && !voucherSeries ? 'grid-cols-1' : hideDate || !voucherSeries ? 'grid-cols-2' : 'grid-cols-3'}`}>
<div>
<span className="text-muted-foreground">Räkenskapsår</span>
<p className="font-medium">{periodName}</p>
@@ -57,6 +59,12 @@ export function JournalEntryReviewContent({
<p className="font-medium">{entryDate}</p>
</div>
)}
{voucherSeries && (
<div>
<span className="text-muted-foreground">Serie</span>
<p className="font-medium font-mono">{voucherSeries}</p>
</div>
)}
</div>
<div className="text-sm">
<span className="text-muted-foreground">Beskrivning</span>
+17 -3
View File
@@ -4,6 +4,7 @@ import { useState, useEffect, useCallback } from 'react'
import { createClient } from '@/lib/supabase/client'
import { useCompany } from '@/contexts/CompanyContext'
import { Label } from '@/components/ui/label'
import { Badge } from '@/components/ui/badge'
interface VoucherSeries {
voucher_series: string
@@ -11,7 +12,11 @@ interface VoucherSeries {
fiscal_period_id: string
}
export function VoucherSeriesManager() {
interface VoucherSeriesManagerProps {
defaultSeries?: string
}
export function VoucherSeriesManager({ defaultSeries }: VoucherSeriesManagerProps) {
const { company } = useCompany()
const [series, setSeries] = useState<VoucherSeries[]>([])
const [isLoading, setIsLoading] = useState(true)
@@ -52,7 +57,7 @@ export function VoucherSeriesManager() {
</div>
) : seriesEntries.length === 0 ? (
<p className="text-sm text-muted-foreground">
Inga verifikationsserier ännu. Serie A skapas automatiskt vid första verifikationen.
Inga verifikationsserier ännu. Serie {defaultSeries || 'A'} skapas automatiskt vid första verifikationen.
</p>
) : (
<div className="space-y-2">
@@ -60,7 +65,12 @@ export function VoucherSeriesManager() {
<div className="divide-y divide-border/8">
{seriesEntries.map(([letter, lastNum]) => (
<div key={letter} className="flex items-center justify-between py-2">
<span className="text-sm font-medium tabular-nums">Serie {letter}</span>
<div className="flex items-center gap-2">
<span className="text-sm font-medium tabular-nums">Serie {letter}</span>
{letter === (defaultSeries || 'A') && (
<Badge variant="outline" className="text-[10px] px-1.5 py-0">standard</Badge>
)}
</div>
<span className="text-sm text-muted-foreground tabular-nums">
Senaste nr: {lastNum}
</span>
@@ -69,6 +79,10 @@ export function VoucherSeriesManager() {
</div>
</div>
)}
<p className="text-xs text-muted-foreground">
Nya serier skapas automatiskt första gången de används vid bokföring.
</p>
</section>
)
}