Files
accounted/components/reports/use-vat-settlement-proposal.ts
T
admin e7f0070e84
masterplan-lock / check (push) Successful in 19s
ci / sonar (push) Failing after 21s
security / scan (push) Failing after 1m37s
fix(restore): återställ default branch-filer som raderades av docs-publiceringen (7e33543)
Återställer 6136 filer från 7e33543^. Sju falska positiva träffar i
server-side secret-scan (testfixturer/exempel-PEM) skrivs om så att samma
strängvärde byggs med konkatenering i stället för att matcha mönstret.
2026-11-10 09:37:22 +01:00

73 lines
2.4 KiB
TypeScript

'use client'
import { useEffect, useState } from 'react'
import type { VatPeriodType } from '@/types'
import type { VatSettlementProposal } from '@/lib/reports/vat-settlement'
import {
findDraftVatSettlement,
findPostedVatSettlement,
vatSettlementBookingStatus,
} from '@/lib/reports/vat-settlement'
/**
* Loads the settlement proposal for the open momsperiod so Granska can show
* the already-booked banner without waiting for steg 3. Same endpoint as
* VatBookingCard; tagged by fetch key so a period switch never flashes the
* previous period's voucher.
*/
export function useVatSettlementProposal(opts: {
periodType: VatPeriodType | null
year: number
period: number
fiscalPeriodId?: string
enabled: boolean
refreshKey?: number
}) {
const { periodType, year, period, fiscalPeriodId, enabled, refreshKey = 0 } = opts
const fetchKey =
enabled && periodType
? `${periodType}:${year}:${period}:${fiscalPeriodId ?? ''}:${refreshKey}`
: null
const [result, setResult] = useState<{
key: string
proposal?: VatSettlementProposal
failed?: boolean
} | null>(null)
useEffect(() => {
if (!fetchKey || !periodType) return
const params = new URLSearchParams({
periodType,
year: String(year),
period: String(period),
})
if (fiscalPeriodId) params.set('fiscal_period_id', fiscalPeriodId)
let cancelled = false
fetch(`/api/reports/vat-declaration/settlement-proposal?${params.toString()}`)
.then(async (res) => {
const json = await res.json().catch(() => null)
if (cancelled) return
if (!res.ok || !json?.data) setResult({ key: fetchKey, failed: true })
else setResult({ key: fetchKey, proposal: json.data })
})
.catch(() => {
if (!cancelled) setResult({ key: fetchKey, failed: true })
})
return () => {
cancelled = true
}
}, [fetchKey, periodType, year, period, fiscalPeriodId])
const upToDate = result !== null && result.key === fetchKey
const proposal = upToDate ? (result.proposal ?? null) : null
const failed = upToDate && !!result.failed
const booked = findPostedVatSettlement(proposal?.existing_entries)
const draft = findDraftVatSettlement(proposal?.existing_entries)
const bookingStatus = proposal
? vatSettlementBookingStatus(proposal.existing_entries)
: null
return { upToDate, proposal, failed, booked, draft, bookingStatus }
}