* feat: add fiscal period backward chaining and entry date validation Support creating fiscal periods before the earliest existing period (backward chaining) for backfill scenarios, alongside the existing forward chaining. The engine now validates that entry dates fall within the selected fiscal period, with a Swedish error message. The journal entry form auto-selects the matching period and shows a warning with a CreatePeriodDialog when no period covers the entry date. * feat: support multi-bank-account for imports and reconciliation Plumb a configurable settlement account through the entire bank import pipeline — mapping engine, transaction entries, ingest, and reconciliation — so secondary bank accounts (e.g. 1931, 1932) work correctly instead of hardcoding 1930. Adds a get_unlinked_bank_lines RPC that generalizes the existing get_unlinked_1930_lines with a fallback for backwards compatibility. The bank file import UI now shows a bank account selector when multiple 19xx accounts exist. Also adds default_vat_code/sru_code to account creation and fixes uploadDocument argument order in enable-banking sync.
92 lines
2.8 KiB
TypeScript
92 lines
2.8 KiB
TypeScript
import { createClient } from '@/lib/supabase/server'
|
|
import { fetchAllRows } from '@/lib/supabase/fetch-all'
|
|
import { NextResponse } from 'next/server'
|
|
import { validateBody } from '@/lib/api/validate'
|
|
import { CreateAccountSchema } from '@/lib/api/schemas'
|
|
import { requireCompanyId } from '@/lib/company/context'
|
|
import { requireWritePermission } from '@/lib/auth/require-write'
|
|
|
|
export async function GET(request: Request) {
|
|
const supabase = await createClient()
|
|
const { data: { user } } = await supabase.auth.getUser()
|
|
|
|
if (!user) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const companyId = await requireCompanyId(supabase, user.id)
|
|
|
|
const { searchParams } = new URL(request.url)
|
|
const accountClass = searchParams.get('class')
|
|
const activeOnly = searchParams.get('active') !== 'false'
|
|
|
|
try {
|
|
const data = await fetchAllRows(({ from, to }) => {
|
|
let query = supabase
|
|
.from('chart_of_accounts')
|
|
.select('*')
|
|
.eq('company_id', companyId)
|
|
.order('sort_order')
|
|
|
|
if (activeOnly) {
|
|
query = query.eq('is_active', true)
|
|
}
|
|
|
|
if (accountClass) {
|
|
query = query.eq('account_class', parseInt(accountClass))
|
|
}
|
|
|
|
return query.range(from, to)
|
|
})
|
|
|
|
return NextResponse.json({ data })
|
|
} catch (error) {
|
|
return NextResponse.json({ error: error instanceof Error ? error.message : 'Failed to fetch accounts' }, { status: 500 })
|
|
}
|
|
}
|
|
|
|
export async function POST(request: Request) {
|
|
const supabase = await createClient()
|
|
const { data: { user } } = await supabase.auth.getUser()
|
|
|
|
if (!user) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const writeCheck = await requireWritePermission(supabase, user.id)
|
|
if (!writeCheck.ok) return writeCheck.response
|
|
|
|
const validation = await validateBody(request, CreateAccountSchema)
|
|
if (!validation.success) return validation.response
|
|
const body = validation.data
|
|
|
|
const companyId = await requireCompanyId(supabase, user.id)
|
|
|
|
const { data, error } = await supabase
|
|
.from('chart_of_accounts')
|
|
.insert({
|
|
user_id: user.id,
|
|
company_id: companyId,
|
|
account_number: body.account_number,
|
|
account_name: body.account_name,
|
|
account_class: parseInt(body.account_number[0]),
|
|
account_group: body.account_number.substring(0, 2),
|
|
account_type: body.account_type,
|
|
normal_balance: body.normal_balance,
|
|
plan_type: body.plan_type || 'k1',
|
|
is_system_account: false,
|
|
description: body.description || null,
|
|
default_vat_code: body.default_vat_code || null,
|
|
sru_code: body.sru_code || null,
|
|
sort_order: parseInt(body.account_number),
|
|
})
|
|
.select()
|
|
.single()
|
|
|
|
if (error) {
|
|
return NextResponse.json({ error: error.message }, { status: 500 })
|
|
}
|
|
|
|
return NextResponse.json({ data })
|
|
}
|