diff --git a/app/modules/wizards/service.py b/app/modules/wizards/service.py index a28a99d..c705d59 100644 --- a/app/modules/wizards/service.py +++ b/app/modules/wizards/service.py @@ -1,4 +1,4 @@ -from __future__ import annotations +from __future__ import annotations from dataclasses import dataclass from datetime import date, datetime, timedelta @@ -18,6 +18,7 @@ from app.modules.core.iam.password_flows_models import InviteToken from app.modules.core.rbac.models import Role, UserRole from app.modules.core.tenancy.models import Branch, FinancialYear, Tenant from app.modules.core.tenancy.settings_models import BranchSettings +from app.modules.employees.models import Employee class FirmWizardError(ValueError): @@ -294,6 +295,30 @@ def create_firm_from_wizard(db: Session, payload: dict[str, Any]) -> FirmWizardR db.flush() db.add(UserRole(user_id=firm_admin.id, role_id=role.id)) + # Auto-create and link Employee Master for the primary Firm Admin. + # Employee Portal requires employees.user_id to match the logged-in user. + employee_code = normalize_code(f"{payload['tenant_code']}_ADMIN")[:50] + firm_admin_employee = Employee( + tenant_id=tenant.id, + branch_id=branch.id, + user_id=firm_admin.id, + employee_code=employee_code, + full_name=payload["admin_full_name"], + email=payload["admin_email"], + mobile=payload["admin_mobile"] or None, + date_of_joining=date.today(), + employment_type="full_time", + status="active", + is_active=True, + department="Administration", + designation=payload["admin_designation"] or "Firm Admin", + created_by_user_id=firm_admin.id, + updated_by_user_id=firm_admin.id, + notes="Auto-created by Firm Creation Wizard for primary Firm Admin.", + ) + db.add(firm_admin_employee) + db.flush() + financial_year = None if payload["create_financial_year"]: financial_year = FinancialYear( @@ -321,3 +346,4 @@ def create_firm_from_wizard(db: Session, payload: dict[str, Any]) -> FirmWizardR invite_url=invite_url, invite_token=invite_token, ) +