Fix/company creation bug (#212)

* feat: enhance JournalEntryForm with currency selection and exchange rate fetching

- Added currency selection to JournalEntryForm, allowing users to choose from multiple currencies (SEK, EUR, USD, GBP, NOK, DKK).
- Implemented fetching of exchange rates from Riksbanken API based on selected currency and entry date.
- Updated calculations for foreign amounts and SEK equivalents based on user input and fetched exchange rates.
- Improved form handling to reset currency-related fields when switching back to SEK.

feat: refactor WelcomeOnboarding to streamline company creation process

- Replaced direct company switching with a new server action to create a company from onboarding data.
- Added validation for fiscal period during onboarding steps, allowing for mid-month starts for the first fiscal period.
- Enhanced error handling and rollback mechanisms to ensure data integrity during company creation.

fix: update Step3TaxRegistration to allow flexible first-year start dates

- Modified date selection to include day, month, and year for the first-year start date.
- Updated validation messages to reflect changes in fiscal year start date handling.

test: expand validate-period-duration tests for fiscal period validation

- Added tests to validate that mid-month starts are allowed for the first fiscal period.
- Ensured that subsequent periods must start on the 1st of the month and enforced maximum duration constraints.

feat: implement currency rate API endpoint

- Created a new API route to fetch exchange rates for specified currencies, ensuring user authentication.
- Validated currency input and handled errors for invalid requests.

chore: update database constraints for fiscal periods

- Modified database constraints to allow custom start dates for the first fiscal period while enforcing day-1 starts for subsequent periods.

* fix: implement computeFiscalPeriod function for onboarding and refactor JournalEntryForm

* Fixed date issue

* Added migration
This commit is contained in:
Mattsson
2026-04-10 11:02:28 +02:00
committed by GitHub
parent e8928b7885
commit aa405b9a74
14 changed files with 664 additions and 555 deletions
+51 -15
View File
@@ -174,7 +174,12 @@ export default function Step3TaxRegistration({
const firstYearEnd = watch('first_year_end')
const fiscalYearEndMonth = watch('fiscal_year_end_month')
// State for first-year start date selectors (month/year)
// State for first-year start date selectors (day/month/year)
const [startDay, setStartDay] = useState<number>(
initialData.first_year_start
? parseDateParts(initialData.first_year_start).day
: 1
)
const [startMonth, setStartMonth] = useState<number>(
initialData.first_year_start
? parseDateParts(initialData.first_year_start).month
@@ -377,22 +382,53 @@ export default function Step3TaxRegistration({
const currentYear = new Date().getFullYear()
const years = Array.from({ length: 7 }, (_, i) => currentYear - 5 + i)
const handleMonthChange = (month: number) => {
setStartMonth(month)
if (month && startYear) {
field.onChange(`${startYear}-${String(month).padStart(2, '0')}-01`)
const updateField = (day: number, month: number, year: number) => {
if (month && year) {
const maxDay = lastDayOfMonth(year, month)
const clampedDay = Math.min(day, maxDay)
field.onChange(`${year}-${String(month).padStart(2, '0')}-${String(clampedDay).padStart(2, '0')}`)
}
}
const handleYearChange = (year: number) => {
setStartYear(year)
if (startMonth && year) {
field.onChange(`${year}-${String(startMonth).padStart(2, '0')}-01`)
}
updateField(startDay, startMonth, year)
}
const handleMonthChange = (month: number) => {
setStartMonth(month)
// Clamp day if needed when month changes
if (startYear) {
const maxDay = lastDayOfMonth(startYear, month)
if (startDay > maxDay) setStartDay(maxDay)
}
updateField(startDay, month, startYear)
}
const handleDayChange = (day: number) => {
setStartDay(day)
updateField(day, startMonth, startYear)
}
const maxDays = startMonth && startYear
? lastDayOfMonth(startYear, startMonth)
: 31
return (
<div className="grid grid-cols-1 sm:grid-cols-2 gap-2">
<div className="grid grid-cols-3 gap-2">
<Select
value={startYear ? startYear.toString() : ''}
onValueChange={(v) => { if (v) handleYearChange(parseInt(v)) }}
>
<SelectTrigger>
<SelectValue placeholder="År" />
</SelectTrigger>
<SelectContent>
{years.map((y) => (
<SelectItem key={y} value={y.toString()}>{y}</SelectItem>
))}
</SelectContent>
</Select>
<Select
value={startMonth ? startMonth.toString() : ''}
onValueChange={(v) => { if (v) handleMonthChange(parseInt(v)) }}
@@ -407,15 +443,15 @@ export default function Step3TaxRegistration({
</SelectContent>
</Select>
<Select
value={startYear ? startYear.toString() : ''}
onValueChange={(v) => { if (v) handleYearChange(parseInt(v)) }}
value={startDay.toString()}
onValueChange={(v) => { if (v) handleDayChange(parseInt(v)) }}
>
<SelectTrigger>
<SelectValue placeholder="År" />
<SelectValue placeholder="Dag" />
</SelectTrigger>
<SelectContent>
{years.map((y) => (
<SelectItem key={y} value={y.toString()}>{y}</SelectItem>
{Array.from({ length: maxDays }, (_, i) => i + 1).map((d) => (
<SelectItem key={d} value={d.toString()}>{d}</SelectItem>
))}
</SelectContent>
</Select>
@@ -424,7 +460,7 @@ export default function Step3TaxRegistration({
}}
/>
<p className="text-xs text-muted-foreground">
Månaden verksamheten startade. Räkenskapsåret börjar alltid den 1:a.
Datumet företaget registrerades. Första räkenskapsåret kan börja valfri dag.
</p>
{errors.first_year_start && (
<p className="text-xs text-destructive">{errors.first_year_start.message}</p>