* 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
67 lines
2.4 KiB
TypeScript
67 lines
2.4 KiB
TypeScript
/**
|
|
* Validates fiscal period duration per BFL 3 kap.
|
|
* Maximum 18 months for any fiscal period (first year may be extended).
|
|
* Normal ongoing periods are 12 months.
|
|
*/
|
|
|
|
/**
|
|
* Parse a YYYY-MM-DD string into numeric parts without timezone issues.
|
|
* Using new Date(dateStr) is unsafe because it creates UTC midnight,
|
|
* but getDate()/getMonth()/getFullYear() return local-timezone values —
|
|
* shifting the date by -1 day in Western timezones.
|
|
*/
|
|
export function parseDateParts(dateStr: string): { year: number; month: number; day: number } {
|
|
const [year, month, day] = dateStr.split('-').map(Number)
|
|
return { year, month, day }
|
|
}
|
|
|
|
/**
|
|
* Calculate the number of months between two dates (inclusive of partial months).
|
|
* Uses year/month arithmetic only — a mid-month start counts the start month fully,
|
|
* which is conservative for the 18-month cap check.
|
|
*/
|
|
export function monthsBetween(start: string, end: string): number {
|
|
const s = parseDateParts(start)
|
|
const e = parseDateParts(end)
|
|
return (e.year - s.year) * 12 + (e.month - s.month) + 1
|
|
}
|
|
|
|
export interface ValidatePeriodOptions {
|
|
/** Allow any start day (not just 1st of month) for the first fiscal period per BFL 3 kap. */
|
|
isFirstPeriod?: boolean
|
|
}
|
|
|
|
/**
|
|
* Validate a fiscal period's duration and date constraints.
|
|
* Returns null if valid, or an error message string if invalid.
|
|
*/
|
|
export function validatePeriodDuration(start: string, end: string, options?: ValidatePeriodOptions): string | null {
|
|
const startParts = parseDateParts(start)
|
|
const endParts = parseDateParts(end)
|
|
|
|
// end must be after start (YYYY-MM-DD strings are lexicographically orderable)
|
|
if (end <= start) {
|
|
return 'Period end must be after period start'
|
|
}
|
|
|
|
// start must be 1st of month — unless this is the first fiscal period (BFL 3 kap.)
|
|
if (startParts.day !== 1 && !options?.isFirstPeriod) {
|
|
return 'Period start must be the 1st of a month'
|
|
}
|
|
|
|
// end must be last day of month
|
|
// new Date(year, 1-indexed-month, 0) gives the last day of that month
|
|
const lastDayOfEndMonth = new Date(endParts.year, endParts.month, 0).getDate()
|
|
if (endParts.day !== lastDayOfEndMonth) {
|
|
return 'Period end must be the last day of a month'
|
|
}
|
|
|
|
// Max 18 months per BFL 3 kap.
|
|
const months = monthsBetween(start, end)
|
|
if (months > 18) {
|
|
return `Period duration ${months} months exceeds maximum 18 months (BFL 3 kap.)`
|
|
}
|
|
|
|
return null
|
|
}
|