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
@@ -0,0 +1,36 @@
-- Allow the first fiscal period of a company to start on any day of the month,
-- per BFL 3 kap. (the first fiscal year starts on the company registration date).
-- Subsequent periods must still start on the 1st of a month.
-- Drop the unconditional CHECK constraint that enforces day-1 starts
ALTER TABLE public.fiscal_periods
DROP CONSTRAINT IF EXISTS fiscal_period_start_first_of_month;
-- Replace with a trigger that only enforces day-1 for non-first periods
CREATE OR REPLACE FUNCTION enforce_first_of_month_for_subsequent_periods()
RETURNS trigger AS $$
BEGIN
-- If this period starts on the 1st, no check needed
IF EXTRACT(DAY FROM NEW.period_start) = 1 THEN
RETURN NEW;
END IF;
-- Allow any start day only if this is the first fiscal period for the company
IF EXISTS (
SELECT 1 FROM public.fiscal_periods
WHERE company_id = NEW.company_id
AND id IS DISTINCT FROM NEW.id
) THEN
RAISE EXCEPTION 'Non-first fiscal period must start on the 1st of a month';
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
DROP TRIGGER IF EXISTS enforce_period_start_day ON public.fiscal_periods;
CREATE TRIGGER enforce_period_start_day
BEFORE INSERT OR UPDATE ON public.fiscal_periods
FOR EACH ROW
EXECUTE FUNCTION enforce_first_of_month_for_subsequent_periods();