unis_hrms/app/models/benefits.py

137 lines
6.7 KiB
Python

"""福利保险: 参保方案、参保登记、福利项目与发放、假期类型/额度/请假."""
from __future__ import annotations
from datetime import date, datetime
from decimal import Decimal
from sqlalchemy import JSON, Boolean, Date, DateTime, Index, Integer, String, Text
from sqlalchemy.orm import Mapped, mapped_column
from ..db.base import Base, Money, PKMixin, TimestampMixin
class InsurancePlan(PKMixin, TimestampMixin, Base):
"""参保方案 (五险一金比例与基数上下限, 按参保城市)."""
__tablename__ = "ben_insurance_plan"
code: Mapped[str | None] = mapped_column(String(32), unique=True)
name: Mapped[str] = mapped_column(String(64))
city: Mapped[str | None] = mapped_column(String(32), index=True)
effective_date: Mapped[date | None] = mapped_column(Date)
rules: Mapped[list | None] = mapped_column(JSON)
# [{key,label,company_rate,personal_rate,fund_type:social|fund,fixed_company,fixed_personal}]
base_floor: Mapped[Decimal | None] = mapped_column(Money)
base_ceiling: Mapped[Decimal | None] = mapped_column(Money)
fund_base_floor: Mapped[Decimal | None] = mapped_column(Money)
fund_base_ceiling: Mapped[Decimal | None] = mapped_column(Money)
is_default: Mapped[bool] = mapped_column(Boolean, default=False)
is_active: Mapped[bool] = mapped_column(Boolean, default=True)
remark: Mapped[str | None] = mapped_column(String(255))
class Enrollment(PKMixin, TimestampMixin, Base):
"""员工参保登记."""
__tablename__ = "ben_enrollment"
__table_args__ = (Index("ix_enroll_emp_status", "employee_id", "status"),)
employee_id: Mapped[int] = mapped_column(Integer, index=True)
plan_id: Mapped[int] = mapped_column(Integer, index=True)
social_base: Mapped[Decimal | None] = mapped_column(Money)
fund_base: Mapped[Decimal | None] = mapped_column(Money)
social_account: Mapped[str | None] = mapped_column(String(64))
fund_account: Mapped[str | None] = mapped_column(String(64))
start_date: Mapped[date | None] = mapped_column(Date)
end_date: Mapped[date | None] = mapped_column(Date)
status: Mapped[str] = mapped_column(String(16), default="active", index=True) # active/stopped
include_in_payroll: Mapped[bool] = mapped_column(Boolean, default=True)
remark: Mapped[str | None] = mapped_column(String(255))
class BenefitItem(PKMixin, TimestampMixin, Base):
__tablename__ = "ben_benefit_item"
code: Mapped[str | None] = mapped_column(String(32), unique=True)
name: Mapped[str] = mapped_column(String(64))
category: Mapped[str] = mapped_column(String(24), default="allowance")
# allowance 补贴 / festival 节日 / health 健康 / team 团建 / gift 礼品 / commercial 商业保险
default_amount: Mapped[Decimal | None] = mapped_column(Money)
unit: Mapped[str] = mapped_column(String(16), default="元")
period: Mapped[str] = mapped_column(String(16), default="monthly") # monthly/quarterly/yearly/once
taxable: Mapped[bool] = mapped_column(Boolean, default=True)
pay_with_salary: Mapped[bool] = mapped_column(Boolean, default=True)
is_active: Mapped[bool] = mapped_column(Boolean, default=True)
icon: Mapped[str | None] = mapped_column(String(32))
description: Mapped[str | None] = mapped_column(Text)
class BenefitRecord(PKMixin, TimestampMixin, Base):
__tablename__ = "ben_benefit_record"
__table_args__ = (Index("ix_benefit_emp_period", "employee_id", "year", "month"),)
employee_id: Mapped[int] = mapped_column(Integer, index=True)
item_id: Mapped[int] = mapped_column(Integer, index=True)
year: Mapped[int] = mapped_column(Integer, index=True)
month: Mapped[int] = mapped_column(Integer)
amount: Mapped[Decimal | None] = mapped_column(Money)
quantity: Mapped[int] = mapped_column(Integer, default=1)
status: Mapped[str] = mapped_column(String(16), default="pending", index=True) # pending/approved/paid/void
batch_label: Mapped[str | None] = mapped_column(String(64), index=True)
paid_at: Mapped[datetime | None] = mapped_column(DateTime)
source: Mapped[str] = mapped_column(String(16), default="manual")
remark: Mapped[str | None] = mapped_column(String(255))
class LeaveType(PKMixin, TimestampMixin, Base):
__tablename__ = "ben_leave_type"
code: Mapped[str | None] = mapped_column(String(32), unique=True)
name: Mapped[str] = mapped_column(String(32))
unit: Mapped[str] = mapped_column(String(8), default="day") # day/half-day/hour
paid: Mapped[bool] = mapped_column(Boolean, default=True)
requires_balance: Mapped[bool] = mapped_column(Boolean, default=True)
requires_certificate: Mapped[bool] = mapped_column(Boolean, default=False)
max_days_per_year: Mapped[int | None] = mapped_column(Integer)
color: Mapped[str | None] = mapped_column(String(16))
sort: Mapped[int] = mapped_column(Integer, default=0)
is_active: Mapped[bool] = mapped_column(Boolean, default=True)
description: Mapped[str | None] = mapped_column(String(255))
class LeaveBalance(PKMixin, TimestampMixin, Base):
__tablename__ = "ben_leave_balance"
employee_id: Mapped[int] = mapped_column(Integer, index=True)
leave_type_id: Mapped[int] = mapped_column(Integer, index=True)
year: Mapped[int] = mapped_column(Integer, index=True)
total_days: Mapped[float] = mapped_column(default=0)
used_days: Mapped[float] = mapped_column(default=0)
frozen_days: Mapped[float] = mapped_column(default=0)
expire_date: Mapped[date | None] = mapped_column(Date)
remark: Mapped[str | None] = mapped_column(String(255))
class LeaveRequest(PKMixin, TimestampMixin, Base):
__tablename__ = "ben_leave_request"
__table_args__ = (Index("ix_leave_status", "status", "employee_id"),)
employee_id: Mapped[int] = mapped_column(Integer, index=True)
leave_type_id: Mapped[int] = mapped_column(Integer, index=True)
start_date: Mapped[date] = mapped_column(Date)
end_date: Mapped[date] = mapped_column(Date)
period: Mapped[str] = mapped_column(String(16), default="full") # full/am/pm
days: Mapped[float] = mapped_column(default=0)
reason: Mapped[str | None] = mapped_column(Text)
address: Mapped[str | None] = mapped_column(String(255))
phone: Mapped[str | None] = mapped_column(String(32))
handover_employee_id: Mapped[int | None] = mapped_column(Integer)
status: Mapped[str] = mapped_column(String(16), default="pending", index=True)
# pending/approved/rejected/cancelled
approver_id: Mapped[int | None] = mapped_column(Integer, index=True)
approver_name: Mapped[str | None] = mapped_column(String(64))
approved_at: Mapped[datetime | None] = mapped_column(DateTime)
comment: Mapped[str | None] = mapped_column(String(255))
applicant_user_id: Mapped[int | None] = mapped_column(Integer)