* fix(reports): stabilize fetchAllRows paging to stop doubled/dropped balances (#790, #791) PostgREST `.range()` paging is only correct when the underlying query has a stable TOTAL order. Several aggregating report queries (general ledger, trial balance, grundbok, supplier/AR ledgers, etc.) paginated without `.order()`, so on datasets larger than one 1000-row page Postgres could return rows in a different order between requests — silently DUPLICATING or SKIPPING rows on a page boundary and doubling or dropping financial totals. - fetch-all.ts: document the ordering invariant and add an optional `dedupeBy` defense-in-depth that drops cross-page duplicates and warns when it fires (surfaces a missing `.order()` in logs instead of corrupting money). - Add a stable `.order()` (line PK or account_number) to every paginated query in lib/reports/ and the account-balances route; pass `dedupeBy` on the money-aggregating line queries. - Add fetch-all unit tests and update report test fixtures to carry row ids. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(api): declare the real { data, meta } envelope on v1 single/write/204 endpoints (#794) The OpenAPI generator derives each endpoint's documented body purely from its registered `response.success` Zod schema, and that schema is never validated at runtime — so a route could advertise a shape its handler never sends. #802 fixed this for list endpoints; the same drift was latent on single-resource and write endpoints, which declared the bare resource schema instead of the `{ data, meta }` envelope the handlers actually return. - registry.ts: extend `ResponseMetaSchema` with the optional `audit` block and `partial_expansions` list that writes/expansions emit; add the `NoBodyResponse` sentinel so 204 DELETE handlers document a bare 204 instead of a phantom 200. - Wrap every single/write endpoint's `response.success` in `dataEnvelope(...)` (or `NoBodyResponse` for 204s) across the v1 routes. - Add a response-envelope contract test that fails CI if any JSON endpoint forgets to wrap its schema, with binary downloads and 204s as the only exemptions. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(reports): extend paging dedupeBy to rc-basis-gaps and opening-balances Address PR review: these two money-aggregating line queries already had the stable `.order('id')` (so paging was correct) but didn't carry `id` in the select, so they couldn't use the `dedupeBy` defense-in-depth that general-ledger and trial-balance got. Select `id` and pass `dedupeBy: r => r.id` so the whole report layer applies the ordering invariant consistently. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
132 lines
4.8 KiB
TypeScript
132 lines
4.8 KiB
TypeScript
import type { SupabaseClient } from '@supabase/supabase-js'
|
|
import { fetchAllRows } from '@/lib/supabase/fetch-all'
|
|
|
|
/**
|
|
* Lönejournal — Monthly/annual per-employee salary register.
|
|
*
|
|
* Required per BFL as underlag for AGI reconciliation.
|
|
* Lists gross, tax, net, avgifter, and vacation accrual per employee per period.
|
|
*
|
|
* Per BFNAR 2013:2: Must be part of systemdokumentation and producible
|
|
* on demand for audit. Retained 7 years per BFL 7 kap.
|
|
*/
|
|
|
|
export interface SalaryJournalRow {
|
|
employeeId: string
|
|
employeeName: string
|
|
personnummerLast4: string
|
|
employmentType: string
|
|
periodYear: number
|
|
periodMonth: number
|
|
paymentDate: string
|
|
grossSalary: number
|
|
taxWithheld: number
|
|
netSalary: number
|
|
avgifterAmount: number
|
|
avgifterRate: number
|
|
vacationAccrual: number
|
|
vacationAccrualAvgifter: number
|
|
totalEmployerCost: number
|
|
sickDays: number
|
|
vabDays: number
|
|
parentalDays: number
|
|
vacationDaysTaken: number
|
|
salaryRunStatus: string
|
|
}
|
|
|
|
export interface SalaryJournalReport {
|
|
rows: SalaryJournalRow[]
|
|
totals: {
|
|
grossSalary: number
|
|
taxWithheld: number
|
|
netSalary: number
|
|
avgifterAmount: number
|
|
vacationAccrual: number
|
|
vacationAccrualAvgifter: number
|
|
totalEmployerCost: number
|
|
}
|
|
period: { year: number; monthFrom?: number; monthTo?: number }
|
|
}
|
|
|
|
/**
|
|
* Generate lönejournal for a year or specific month range.
|
|
*/
|
|
export async function generateSalaryJournal(
|
|
supabase: SupabaseClient,
|
|
companyId: string,
|
|
year: number,
|
|
monthFrom?: number,
|
|
monthTo?: number
|
|
): Promise<SalaryJournalReport> {
|
|
// Use !inner join to filter server-side by year and status, avoiding
|
|
// fetching all salary_run_employees across all years.
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
const data: any[] = await fetchAllRows(({ from, to }) =>
|
|
supabase
|
|
.from('salary_run_employees')
|
|
.select(`
|
|
*,
|
|
employee:employees(id, first_name, last_name, personnummer_last4, employment_type),
|
|
salary_run:salary_runs!inner(period_year, period_month, payment_date, status)
|
|
`)
|
|
.eq('company_id', companyId)
|
|
.eq('salary_runs.period_year', year)
|
|
.eq('salary_runs.status', 'booked')
|
|
.order('created_at')
|
|
// id tiebreaker — created_at is not unique, so it alone is not a stable
|
|
// total order for paging (see fetch-all.ts).
|
|
.order('id', { ascending: true })
|
|
.range(from, to)
|
|
)
|
|
|
|
const rows: SalaryJournalRow[] = data
|
|
.filter(sre => {
|
|
const run = sre.salary_run as { period_year: number; period_month: number; status: string } | null
|
|
if (!run || run.period_year !== year) return false
|
|
if (run.status !== 'booked') return false
|
|
if (monthFrom && run.period_month < monthFrom) return false
|
|
if (monthTo && run.period_month > monthTo) return false
|
|
return true
|
|
})
|
|
.map(sre => {
|
|
const emp = sre.employee as { first_name: string; last_name: string; personnummer_last4: string; employment_type: string } | null
|
|
const run = sre.salary_run as { period_year: number; period_month: number; payment_date: string; status: string }
|
|
return {
|
|
employeeId: sre.employee_id,
|
|
employeeName: emp ? `${emp.first_name} ${emp.last_name}` : 'Okänd',
|
|
personnummerLast4: emp?.personnummer_last4 || '????',
|
|
employmentType: emp?.employment_type || 'employee',
|
|
periodYear: run.period_year,
|
|
periodMonth: run.period_month,
|
|
paymentDate: run.payment_date,
|
|
grossSalary: sre.gross_salary,
|
|
taxWithheld: sre.tax_withheld,
|
|
netSalary: sre.net_salary,
|
|
avgifterAmount: sre.avgifter_amount,
|
|
avgifterRate: sre.avgifter_rate,
|
|
vacationAccrual: sre.vacation_accrual,
|
|
vacationAccrualAvgifter: sre.vacation_accrual_avgifter,
|
|
totalEmployerCost: sre.gross_salary + sre.avgifter_amount + sre.vacation_accrual + sre.vacation_accrual_avgifter,
|
|
sickDays: sre.sick_days,
|
|
vabDays: sre.vab_days,
|
|
parentalDays: sre.parental_days,
|
|
vacationDaysTaken: sre.vacation_days_taken,
|
|
salaryRunStatus: run.status,
|
|
}
|
|
})
|
|
.sort((a, b) => a.periodMonth - b.periodMonth || a.employeeName.localeCompare(b.employeeName))
|
|
|
|
const r = (x: number) => Math.round(x * 100) / 100
|
|
const totals = {
|
|
grossSalary: r(rows.reduce((s, r) => s + r.grossSalary, 0)),
|
|
taxWithheld: r(rows.reduce((s, r) => s + r.taxWithheld, 0)),
|
|
netSalary: r(rows.reduce((s, r) => s + r.netSalary, 0)),
|
|
avgifterAmount: r(rows.reduce((s, r) => s + r.avgifterAmount, 0)),
|
|
vacationAccrual: r(rows.reduce((s, r) => s + r.vacationAccrual, 0)),
|
|
vacationAccrualAvgifter: r(rows.reduce((s, r) => s + r.vacationAccrualAvgifter, 0)),
|
|
totalEmployerCost: r(rows.reduce((s, r) => s + r.totalEmployerCost, 0)),
|
|
}
|
|
|
|
return { rows, totals, period: { year, monthFrom, monthTo } }
|
|
}
|