diff --git a/app/(dashboard)/reports/page.tsx b/app/(dashboard)/reports/page.tsx index abc15bca..77bf3cb9 100644 --- a/app/(dashboard)/reports/page.tsx +++ b/app/(dashboard)/reports/page.tsx @@ -9,6 +9,7 @@ import { Badge } from '@/components/ui/badge' import { Tabs, TabsList, TabsTrigger, TabsContent } from '@/components/ui/tabs' import { Download, AlertCircle, ChevronDown, ChevronRight, ArrowRight } from 'lucide-react' import { AccountNumber } from '@/components/ui/account-number' +import { useCompany } from '@/contexts/CompanyContext' import { NEDeclarationView } from '@/components/reports/NEDeclarationView' import { INK2DeclarationView } from '@/components/reports/INK2DeclarationView' import { BankReconciliationView } from '@/components/reports/BankReconciliationView' @@ -48,8 +49,8 @@ export default function ReportsPage() { const [periods, setPeriods] = useState([]) const [selectedPeriod, setSelectedPeriod] = useState('') const [activeTab, setActiveTab] = useState('trial-balance') - const [entityType, setEntityType] = useState(null) const [isLoadingInit, setIsLoadingInit] = useState(true) + const { company } = useCompany() // Drill-down state: when navigating from a report to the GL for a specific account const [glAccountFilter, setGlAccountFilter] = useState(null) @@ -89,26 +90,14 @@ export default function ReportsPage() { } } - async function fetchEntityType() { - try { - const res = await fetch('/api/settings') - const { data } = await res.json() - if (data?.entity_type) { - setEntityType(data.entity_type) - } - } catch { - // Ignore - entity type is optional for tab visibility - } - } - useEffect(() => { - Promise.all([fetchPeriods(), fetchEntityType()]).finally(() => { + fetchPeriods().finally(() => { setIsLoadingInit(false) }) }, []) - const isEnskildFirma = entityType === 'enskild_firma' - const isAktiebolag = entityType === 'aktiebolag' + const isEnskildFirma = company?.entity_type === 'enskild_firma' + const isAktiebolag = company?.entity_type === 'aktiebolag' return (
diff --git a/app/api/settings/route.ts b/app/api/settings/route.ts index 1f748d1f..3a82d0cf 100644 --- a/app/api/settings/route.ts +++ b/app/api/settings/route.ts @@ -26,7 +26,20 @@ export async function GET() { return NextResponse.json({ error: error.message }, { status: 500 }) } - return NextResponse.json({ data }) + // Fall back to companies.entity_type if company_settings.entity_type is null + let responseData = data + if (data && !data.entity_type) { + const { data: company } = await supabase + .from('companies') + .select('entity_type') + .eq('id', companyId) + .single() + if (company?.entity_type) { + responseData = { ...data, entity_type: company.entity_type } + } + } + + return NextResponse.json({ data: responseData }) } export async function PUT(request: Request) { diff --git a/lib/reports/ink2/ink2-engine.ts b/lib/reports/ink2/ink2-engine.ts index 169ed5d6..cf21261c 100644 --- a/lib/reports/ink2/ink2-engine.ts +++ b/lib/reports/ink2/ink2-engine.ts @@ -714,8 +714,19 @@ export async function generateINK2Declaration( .eq('company_id', companyId) .single() - // Validate entity type - if (settings?.entity_type !== 'aktiebolag') { + // Resolve entity_type: prefer company_settings, fall back to companies table (NOT NULL, always reliable) + let entityType = settings?.entity_type + if (!entityType) { + const { data: company, error: companyError } = await supabase + .from('companies') + .select('entity_type') + .eq('id', companyId) + .single() + if (companyError) throw new Error(`Failed to resolve entity type: ${companyError.message}`) + entityType = company?.entity_type + } + + if (entityType !== 'aktiebolag') { throw new Error('INK2 declaration is only for aktiebolag (limited company)') } diff --git a/lib/reports/ne-bilaga/ne-engine.ts b/lib/reports/ne-bilaga/ne-engine.ts index 97dea6b4..bcbc399e 100644 --- a/lib/reports/ne-bilaga/ne-engine.ts +++ b/lib/reports/ne-bilaga/ne-engine.ts @@ -179,8 +179,19 @@ export async function generateNEDeclaration( .eq('company_id', companyId) .single() - // Check if it's enskild firma - if (settings?.entity_type !== 'enskild_firma') { + // Resolve entity_type: prefer company_settings, fall back to companies table (NOT NULL, always reliable) + let entityType = settings?.entity_type + if (!entityType) { + const { data: company, error: companyError } = await supabase + .from('companies') + .select('entity_type') + .eq('id', companyId) + .single() + if (companyError) throw new Error(`Failed to resolve entity type: ${companyError.message}`) + entityType = company?.entity_type + } + + if (entityType !== 'enskild_firma') { throw new Error('NE declaration is only for enskild firma (sole proprietorship)') }