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:
@@ -10,6 +10,8 @@ import { Label } from '@/components/ui/label'
|
||||
import { ExternalLink } from 'lucide-react'
|
||||
import type { CompanySettings } from '@/types'
|
||||
|
||||
const SERIES_OPTIONS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('')
|
||||
|
||||
export default function BookkeepingSettingsPage() {
|
||||
const { settings, isLoading, updateSettings } = useSettings()
|
||||
|
||||
@@ -19,11 +21,13 @@ export default function BookkeepingSettingsPage() {
|
||||
const autoLockValue = formData.get('auto_lock_period_days') as string
|
||||
const lockedThrough = (formData.get('bookkeeping_locked_through') as string) || null
|
||||
const accountingMethod = (formData.get('accounting_method') as string) || 'accrual'
|
||||
const defaultVoucherSeries = (formData.get('default_voucher_series') as string) || 'A'
|
||||
|
||||
const updates: Record<string, unknown> = {
|
||||
bookkeeping_locked_through: lockedThrough,
|
||||
auto_lock_period_days: autoLockValue === 'none' ? null : parseInt(autoLockValue),
|
||||
accounting_method: accountingMethod,
|
||||
default_voucher_series: defaultVoucherSeries,
|
||||
}
|
||||
return {
|
||||
updates,
|
||||
@@ -60,15 +64,40 @@ export default function BookkeepingSettingsPage() {
|
||||
</div>
|
||||
</section>
|
||||
|
||||
{/* Default voucher series */}
|
||||
<div className="border-t border-border/8 pt-8">
|
||||
<section className="space-y-4">
|
||||
<h2 className="text-sm font-medium uppercase tracking-wider text-muted-foreground">
|
||||
Standardserie för verifikationer
|
||||
</h2>
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="default_voucher_series">Serie</Label>
|
||||
<select
|
||||
id="default_voucher_series"
|
||||
name="default_voucher_series"
|
||||
defaultValue={settings.default_voucher_series || 'A'}
|
||||
className="flex h-10 w-16 rounded-md border border-input bg-background px-3 py-2 text-sm font-mono ring-offset-background focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2"
|
||||
>
|
||||
{SERIES_OPTIONS.map((letter) => (
|
||||
<option key={letter} value={letter}>{letter}</option>
|
||||
))}
|
||||
</select>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
Vilken serie som förväljs vid manuell bokföring. Kan ändras per verifikation.
|
||||
</p>
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
|
||||
{/* Period locking */}
|
||||
<div className="border-t border-border/8 pt-8">
|
||||
<PeriodLockingSettings settings={settings} />
|
||||
</div>
|
||||
</SettingsFormWrapper>
|
||||
|
||||
{/* Voucher series — read-only, no form submit needed */}
|
||||
{/* Voucher series — read-only display */}
|
||||
<div className="border-t border-border/8 pt-8">
|
||||
<VoucherSeriesManager />
|
||||
<VoucherSeriesManager defaultSeries={settings.default_voucher_series || 'A'} />
|
||||
</div>
|
||||
|
||||
{/* Cross-links */}
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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>
|
||||
)
|
||||
}
|
||||
|
||||
+4
-2
@@ -305,7 +305,7 @@ export const CreateJournalEntrySchema = z.object({
|
||||
description: z.string().min(1, 'Description is required'),
|
||||
source_type: JournalEntrySourceTypeSchema.default('manual'),
|
||||
source_id: z.string().optional(),
|
||||
voucher_series: z.string().optional(),
|
||||
voucher_series: z.string().regex(/^[A-Z]$/, 'Verifikationsserie måste vara en bokstav A–Z').optional(),
|
||||
lines: z.array(CreateJournalEntryLineSchema).min(2, 'At least two lines are required for double-entry'),
|
||||
})
|
||||
|
||||
@@ -384,6 +384,8 @@ export const UpdateSettingsSchema = z.object({
|
||||
// Bookkeeping lock
|
||||
bookkeeping_locked_through: z.string().regex(/^\d{4}-\d{2}-\d{2}$/, 'Ogiltigt datumformat (YYYY-MM-DD)').nullable().optional(),
|
||||
auto_lock_period_days: z.number().int().positive().nullable().optional(),
|
||||
// Voucher series
|
||||
default_voucher_series: z.string().regex(/^[A-Z]$/, 'Verifikationsserie måste vara en bokstav A–Z').optional(),
|
||||
// Invoice PDF settings
|
||||
ore_rounding: z.boolean().optional(),
|
||||
invoice_show_ocr: z.boolean().optional(),
|
||||
@@ -584,7 +586,7 @@ export const PendingOperationsQuerySchema = z.object({
|
||||
|
||||
export const VoucherGapQuerySchema = z.object({
|
||||
fiscal_period_id: uuid,
|
||||
voucher_series: z.string().optional(),
|
||||
voucher_series: z.string().regex(/^[A-Z]$/, 'Verifikationsserie måste vara en bokstav A–Z').optional(),
|
||||
})
|
||||
|
||||
export const SaveGapExplanationSchema = z.object({
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
-- Add default voucher series to company_settings
|
||||
-- Allows companies to configure which series (A-Z) is pre-selected
|
||||
-- when creating manual journal entries. Defaults to 'A'.
|
||||
|
||||
ALTER TABLE public.company_settings
|
||||
ADD COLUMN IF NOT EXISTS default_voucher_series text NOT NULL DEFAULT 'A';
|
||||
|
||||
-- Enforce single uppercase letter
|
||||
ALTER TABLE public.company_settings
|
||||
ADD CONSTRAINT company_settings_default_voucher_series_check
|
||||
CHECK (default_voucher_series ~ '^[A-Z]$');
|
||||
@@ -520,6 +520,7 @@ export function makeCompanySettings(
|
||||
invoice_default_notes: null,
|
||||
bookkeeping_locked_through: null,
|
||||
auto_lock_period_days: null,
|
||||
default_voucher_series: 'A',
|
||||
ore_rounding: true,
|
||||
invoice_show_ocr: true,
|
||||
invoice_show_bankgiro: true,
|
||||
|
||||
@@ -191,6 +191,9 @@ export interface CompanySettings {
|
||||
bookkeeping_locked_through: string | null
|
||||
auto_lock_period_days: number | null
|
||||
|
||||
// Voucher series
|
||||
default_voucher_series: string
|
||||
|
||||
// Invoice PDF settings
|
||||
ore_rounding: boolean
|
||||
invoice_show_ocr: boolean
|
||||
|
||||
Reference in New Issue
Block a user