* feat: multi-tenant company refactor (GNU-19) Introduce companies table, company_members, and user_preferences to support multiple companies per user. All data scoping changes from user_id to company_id across the entire codebase. Key changes: - Database migration: new tables, company_id on 40+ tables, backfill, RLS rewrite from user_id to company-member-based, updated RPCs - Types: Company, CompanyMember, CompanyRole, UserPreferences types; company_id added to all entity interfaces; companyId on all events - Engine: all 7 core functions take companyId; storno, period, year-end services updated; 16 report generators updated - Middleware: company context resolution (cookie → prefs → first company) - API routes: ~120 routes updated with requireCompanyId() - Frontend: CompanyProvider context, layout/dashboard/onboarding updated - Extensions: context factory, 9 extensions, all lib files updated - Tests: 1880 tests passing, all helpers updated with company_id defaults Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: add database migrations for multi-tenant company and team system (GNU-19) Adds company_invitations, company creation RPC, team_members, account deletion RPC, and teams table refactor migrations. Updates base multi-tenant migration with cascading FKs and onboarding_step column. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: add team types and update core infrastructure for multi-tenancy (GNU-19) Adds TeamRole, MemberSource, and Team types. Refactors Supabase service client to be stateless, updates middleware for team-aware routing, extends CompanyContext with team/role fields, and updates extension service types to accept companyId. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * refactor: thread company_id through business logic functions (GNU-19) Replaces user_id scoping with company_id across all lib modules: bookkeeping, documents, transactions, invoices, reconciliation, tax, deadlines, and import. Updates corresponding tests. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * refactor: thread company_id through API routes and extensions (GNU-19) Updates all existing API routes to extract and pass companyId. Updates enable-banking and arcim-migration extensions for company-scoped transaction ingestion and sync. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: add company and team management API routes (GNU-19) Adds CRUD endpoints for company members, company invitations, team members, and team invitations. Includes invite token utilities, email templates, and company switch server action. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: add team/company UI components, pages, and dashboard updates (GNU-19) Adds CompanySwitcher, ConsultantEmptyState, Step0RoleChoice, company members and team management panels. Updates dashboard layout for team-aware routing, onboarding for multi-step role choice, and auth callback for team invite acceptance. Ignores supabase/.branches/. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: add null guards for company in import page (GNU-19) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: move appUrl declaration to outer scope in invite route (GNU-19) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: add optional chaining for company.name in members section (GNU-19) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: add optional chaining for second company.name in members section (GNU-19) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: add null guards for company in extension components (GNU-19) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: pass companyId to executeSIEImport in arcim-migration extension (GNU-19) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: update tests to use companyId instead of userId and improve type handling --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
171 lines
5.8 KiB
TypeScript
171 lines
5.8 KiB
TypeScript
import type { SupabaseClient } from '@supabase/supabase-js'
|
|
import { fetchAllRows } from '@/lib/supabase/fetch-all'
|
|
|
|
export interface JournalRegisterLine {
|
|
account_number: string
|
|
account_name: string
|
|
debit: number
|
|
credit: number
|
|
}
|
|
|
|
export interface JournalRegisterEntry {
|
|
voucher_series: string
|
|
voucher_number: number
|
|
date: string
|
|
description: string
|
|
source_type: string
|
|
status: string
|
|
lines: JournalRegisterLine[]
|
|
total_debit: number
|
|
total_credit: number
|
|
}
|
|
|
|
export interface JournalRegisterReport {
|
|
entries: JournalRegisterEntry[]
|
|
total_entries: number
|
|
total_debit: number
|
|
total_credit: number
|
|
period: { start: string; end: string }
|
|
}
|
|
|
|
/**
|
|
* Generate journal register (grundbok) for a fiscal period.
|
|
* BFL 5 kap. 1 § — registreringsordning: all vouchers in chronological registration order.
|
|
*
|
|
* Uses a joined query with pagination to handle any number of entries.
|
|
* Avoids the broken .in(entryIds) pattern that silently truncated at 1000 rows.
|
|
*
|
|
* Unlike the general ledger and trial balance, the grundbok includes ALL
|
|
* entries — the opening_balance_entry is NOT excluded, because it is a
|
|
* real voucher that should appear in registration order.
|
|
*/
|
|
export async function generateJournalRegister(
|
|
supabase: SupabaseClient,
|
|
companyId: string,
|
|
periodId: string
|
|
): Promise<JournalRegisterReport> {
|
|
|
|
// Get fiscal period dates
|
|
const { data: period } = await supabase
|
|
.from('fiscal_periods')
|
|
.select('period_start, period_end')
|
|
.eq('id', periodId)
|
|
.eq('company_id', companyId)
|
|
.single()
|
|
|
|
if (!period) {
|
|
return { entries: [], total_entries: 0, total_debit: 0, total_credit: 0, period: { start: '', end: '' } }
|
|
}
|
|
|
|
// Fetch all lines with joined entry data — single paginated query,
|
|
// no entry ID array, no truncation at 1000 rows
|
|
// Supabase types !inner joins as arrays; for many-to-one (line → entry)
|
|
// it returns a single object at runtime. Cast via `as any` on the query.
|
|
const rawLines = await fetchAllRows<{
|
|
account_number: string
|
|
debit_amount: number
|
|
credit_amount: number
|
|
journal_entry_id: string
|
|
journal_entries: {
|
|
id: string
|
|
entry_date: string
|
|
voucher_number: number
|
|
voucher_series: string
|
|
description: string
|
|
source_type: string
|
|
status: string
|
|
}
|
|
}>(({ from, to }) =>
|
|
supabase
|
|
.from('journal_entry_lines')
|
|
.select('account_number, debit_amount, credit_amount, journal_entry_id, journal_entries!inner(id, entry_date, voucher_number, voucher_series, description, source_type, status, company_id, fiscal_period_id)')
|
|
.eq('journal_entries.company_id', companyId)
|
|
.eq('journal_entries.fiscal_period_id', periodId)
|
|
.in('journal_entries.status', ['posted', 'reversed'])
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
.range(from, to) as any
|
|
)
|
|
|
|
if (rawLines.length === 0) {
|
|
return { entries: [], total_entries: 0, total_debit: 0, total_credit: 0, period: { start: period.period_start, end: period.period_end } }
|
|
}
|
|
|
|
// Fetch account names
|
|
const accounts = await fetchAllRows<{ account_number: string; account_name: string }>(({ from, to }) =>
|
|
supabase
|
|
.from('chart_of_accounts')
|
|
.select('account_number, account_name')
|
|
.eq('company_id', companyId)
|
|
.range(from, to)
|
|
)
|
|
|
|
const accountNameMap = new Map<string, string>()
|
|
for (const acc of accounts) {
|
|
accountNameMap.set(acc.account_number, acc.account_name)
|
|
}
|
|
|
|
// Extract unique entries and group lines by entry
|
|
const entryMap = new Map<string, typeof rawLines[0]['journal_entries']>()
|
|
const linesByEntry = new Map<string, JournalRegisterLine[]>()
|
|
|
|
for (const line of rawLines) {
|
|
const entryId = line.journal_entry_id
|
|
const entry = line.journal_entries
|
|
|
|
if (!entryMap.has(entryId)) {
|
|
entryMap.set(entryId, entry)
|
|
}
|
|
|
|
if (!linesByEntry.has(entryId)) {
|
|
linesByEntry.set(entryId, [])
|
|
}
|
|
|
|
linesByEntry.get(entryId)!.push({
|
|
account_number: line.account_number,
|
|
account_name: accountNameMap.get(line.account_number) || `Konto ${line.account_number}`,
|
|
debit: Math.round((Number(line.debit_amount) || 0) * 100) / 100,
|
|
credit: Math.round((Number(line.credit_amount) || 0) * 100) / 100,
|
|
})
|
|
}
|
|
|
|
// Build entries sorted by voucher_series, then voucher_number (registration order)
|
|
const sortedEntries = Array.from(entryMap.entries())
|
|
.sort(([, a], [, b]) => {
|
|
const seriesCompare = (a.voucher_series || 'A').localeCompare(b.voucher_series || 'A')
|
|
if (seriesCompare !== 0) return seriesCompare
|
|
return a.voucher_number - b.voucher_number
|
|
})
|
|
|
|
const result: JournalRegisterEntry[] = sortedEntries.map(([entryId, entry]) => {
|
|
const entryLines = linesByEntry.get(entryId) || []
|
|
// Sort lines by account number within each entry
|
|
entryLines.sort((a, b) => a.account_number.localeCompare(b.account_number))
|
|
|
|
const totalDebit = entryLines.reduce((sum, l) => sum + l.debit, 0)
|
|
const totalCredit = entryLines.reduce((sum, l) => sum + l.credit, 0)
|
|
|
|
return {
|
|
voucher_series: entry.voucher_series || 'A',
|
|
voucher_number: entry.voucher_number,
|
|
date: entry.entry_date,
|
|
description: entry.description || '',
|
|
source_type: entry.source_type || '',
|
|
status: entry.status,
|
|
lines: entryLines,
|
|
total_debit: Math.round(totalDebit * 100) / 100,
|
|
total_credit: Math.round(totalCredit * 100) / 100,
|
|
}
|
|
})
|
|
|
|
const grandTotalDebit = result.reduce((sum, e) => sum + e.total_debit, 0)
|
|
const grandTotalCredit = result.reduce((sum, e) => sum + e.total_credit, 0)
|
|
|
|
return {
|
|
entries: result,
|
|
total_entries: result.length,
|
|
total_debit: Math.round(grandTotalDebit * 100) / 100,
|
|
total_credit: Math.round(grandTotalCredit * 100) / 100,
|
|
period: { start: period.period_start, end: period.period_end },
|
|
}
|
|
}
|