* fix(bookkeeping): harden correction account changes * feat(tax): enhance tax deadline generation with new settings and filing methods - Added new company settings: tax_turnover_over_40m, vat_has_eu_trade, vat_filing_method, periodisk_sammanstallning_enabled, and periodisk_sammanstallning_filing_method. - Updated deadline generation logic to accommodate new settings affecting VAT and employer declaration deadlines. - Implemented tests for new functionality, ensuring that completed obligations are preserved and not replaced by new pending rows. - Introduced a cron job to backfill missing tax deadlines for companies with settings but no upcoming deadlines. - Updated API routes for generating tax deadlines and handling cron jobs. - Modified database schema to include new columns for tax filing profiles and constraints for filing methods. * fix(invoices): record credit note reconciliation guard * fix(tax): correct automatic deadline settings * fix(tax): key AGI deadline to VAT taxable base and add storforetag payment deadline The 26th filing day for the skattedeklaration (AGI and VAT together) hinges on one statutory measure, a VAT taxable base above SEK 40 million (SFL 26 kap.), not a separate employer turnover. Drop employer_turnover_over_40m and derive the AGI schedule from vat_registered plus vat_taxable_base_over_40m, so a non-VAT-reporting employer is never shown the 26th when its binding date is the 12th. Also: - add a skatteinbetalning deadline row (12th, 17 January) for storforetag, whose deducted tax and employer contributions are due before the 26th filing date - normalize legally incoherent over-40m flag combinations to the earlier small-company schedule in a follow-up migration - replace hardcoded 27 December dates with the banking-day adjustment - extend the 40m help text to cover the SKV-decided early filing election and the payment-still-on-the-12th rule - document the regeneration race repaired by the daily backfill cron Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(migrations): add AGI and VAT filing logic with employer column removal * feat(settings): implement VAT registration logic and update related flags; enhance deadline handling --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
111 lines
4.9 KiB
TypeScript
111 lines
4.9 KiB
TypeScript
'use client'
|
|
|
|
import { useEffect } from 'react'
|
|
import { useTranslations } from 'next-intl'
|
|
import { useSearchParams, useRouter } from 'next/navigation'
|
|
import { TaxSettingsForm } from '@/components/settings/TaxSettingsForm'
|
|
import { SettingsFormWrapper } from '@/components/settings/SettingsFormWrapper'
|
|
import { SettingsLoadError } from '@/components/settings/SettingsLoadError'
|
|
import { SettingsLoadingSkeleton } from '@/components/settings/SettingsLoadingSkeleton'
|
|
import { SkatteverketConnectPanel } from '@/components/settings/SkatteverketConnectPanel'
|
|
import { useSettings } from '@/components/settings/useSettings'
|
|
import { useToast } from '@/components/ui/use-toast'
|
|
import { ENABLED_EXTENSION_IDS } from '@/lib/extensions/_generated/enabled-extensions'
|
|
import type { CompanySettings } from '@/types'
|
|
|
|
export function TaxSettingsContent() {
|
|
const { settings, isLoading, updateSettings, refetch } = useSettings()
|
|
const t = useTranslations('settings_skatteverket')
|
|
const searchParams = useSearchParams()
|
|
const router = useRouter()
|
|
const { toast } = useToast()
|
|
|
|
const hasSkatteverketExtension = ENABLED_EXTENSION_IDS.has('skatteverket')
|
|
|
|
// Skatteverket OAuth callback: the connect flow returns to /settings/tax with
|
|
// a status query param (returnTo set in SkatteverketConnectPanel).
|
|
useEffect(() => {
|
|
const connected = searchParams.get('skv_connected')
|
|
const error = searchParams.get('skv_error')
|
|
if (connected === 'true') {
|
|
toast({ title: t('connected_title'), description: t('connected_description') })
|
|
router.replace('/settings/tax')
|
|
} else if (error) {
|
|
let msg: string
|
|
try {
|
|
msg = decodeURIComponent(error)
|
|
} catch {
|
|
msg = error
|
|
}
|
|
toast({ title: t('connect_failed_title'), description: msg, variant: 'destructive' })
|
|
router.replace('/settings/tax')
|
|
}
|
|
}, [searchParams, router, toast, t])
|
|
|
|
if (isLoading) return <SettingsLoadingSkeleton />
|
|
if (!settings) return <SettingsLoadError onRetry={refetch} />
|
|
|
|
function handleSave(formData: FormData) {
|
|
const vatRegistered = formData.get('vat_registered') === 'true'
|
|
const paysSalaries = formData.get('pays_salaries') === 'true'
|
|
|
|
const updates: Record<string, unknown> = {
|
|
f_skatt: formData.get('f_skatt') === 'true',
|
|
vat_registered: vatRegistered,
|
|
vat_number: vatRegistered ? ((formData.get('vat_number') as string) || null) : null,
|
|
moms_period: vatRegistered ? ((formData.get('moms_period') as string) || null) : null,
|
|
vat_taxable_base_over_40m:
|
|
vatRegistered && formData.get('vat_taxable_base_over_40m') === 'true',
|
|
vat_has_eu_trade: vatRegistered && formData.get('vat_has_eu_trade') === 'true',
|
|
// The filing-method and PS selects are conditionally rendered: when a
|
|
// control is unmounted its FormData key is absent, and defaulting would
|
|
// silently overwrite the saved preference. Fall back to the stored
|
|
// value first, then the default.
|
|
vat_filing_method:
|
|
(formData.get('vat_filing_method') as string) ||
|
|
settings?.vat_filing_method ||
|
|
'electronic',
|
|
periodisk_sammanstallning_enabled:
|
|
vatRegistered && formData.get('periodisk_sammanstallning_enabled') === 'true',
|
|
periodisk_sammanstallning_period:
|
|
(formData.get('periodisk_sammanstallning_period') as string) ||
|
|
settings?.periodisk_sammanstallning_period ||
|
|
'monthly',
|
|
periodisk_sammanstallning_filing_method:
|
|
(formData.get('periodisk_sammanstallning_filing_method') as string) ||
|
|
settings?.periodisk_sammanstallning_filing_method ||
|
|
'electronic',
|
|
tax_contact_name: (formData.get('tax_contact_name') as string) || null,
|
|
tax_contact_phone: (formData.get('tax_contact_phone') as string) || null,
|
|
tax_contact_email: (formData.get('tax_contact_email') as string) || null,
|
|
fiscal_year_start_month: parseInt(formData.get('fiscal_year_start_month') as string) || 1,
|
|
pays_salaries: paysSalaries,
|
|
preliminary_tax_monthly: parseFloat(formData.get('preliminary_tax_monthly') as string) || null,
|
|
}
|
|
return {
|
|
updates,
|
|
onSuccess: (data: Record<string, unknown>) => {
|
|
updateSettings(data as Partial<CompanySettings>)
|
|
},
|
|
}
|
|
}
|
|
|
|
// Sandbox companies don't connect to the real Skatteverket: hide the panel,
|
|
// matching the old Skatteverket tab's visibility gate. Read straight off the
|
|
// already-loaded settings row (no separate query needed).
|
|
const showSkatteverket = hasSkatteverketExtension && !settings.is_sandbox
|
|
|
|
return (
|
|
<div className="space-y-8">
|
|
{/* Connection panel first: the skattekonto and momsdeklaration pages
|
|
send users here specifically to (re)connect — below the long tax
|
|
form it sat out of view. */}
|
|
{showSkatteverket && <SkatteverketConnectPanel />}
|
|
|
|
<SettingsFormWrapper onSave={handleSave} className="space-y-0">
|
|
<TaxSettingsForm settings={settings} />
|
|
</SettingsFormWrapper>
|
|
</div>
|
|
)
|
|
}
|