"""薪资: 薪资项目、账套(薪资方案)、员工薪资标准、期间、算薪批次、工资条.""" 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 ITEM_CATEGORIES = ("earning", "deduction", "insurance", "tax", "statutory") class SalaryItem(PKMixin, TimestampMixin, Base): """工资项目定义 (基本工资/绩效/加班费/缺勤扣款/社保个人...)""" __tablename__ = "sal_salary_item" code: Mapped[str] = mapped_column(String(32), unique=True) name: Mapped[str] = mapped_column(String(64)) category: Mapped[str] = mapped_column(String(16), default="earning") calc_type: Mapped[str] = mapped_column(String(16), default="fixed") # fixed/manual/formula formula: Mapped[str | None] = mapped_column(String(255)) include_in_gross: Mapped[bool] = mapped_column(Boolean, default=True) taxable: Mapped[bool] = mapped_column(Boolean, default=True) is_system: Mapped[bool] = mapped_column(Boolean, default=False) sort: Mapped[int] = mapped_column(Integer, default=0) is_active: Mapped[bool] = mapped_column(Boolean, default=True) remark: Mapped[str | None] = mapped_column(String(255)) class SalaryPlan(PKMixin, TimestampMixin, Base): """账套/薪资结构: 一组项目的默认金额.""" __tablename__ = "sal_salary_plan" code: Mapped[str | None] = mapped_column(String(32), unique=True) name: Mapped[str] = mapped_column(String(64)) department_id: Mapped[int | None] = mapped_column(Integer, index=True) job_level_id: Mapped[int | None] = mapped_column(Integer) effective_date: Mapped[date | None] = mapped_column(Date) items: Mapped[dict | None] = mapped_column(JSON) # {item_code: amount} pay_day: Mapped[int] = mapped_column(Integer, default=10) currency: Mapped[str] = mapped_column(String(8), default="CNY") is_active: Mapped[bool] = mapped_column(Boolean, default=True) remark: Mapped[str | None] = mapped_column(String(255)) class EmployeeSalary(PKMixin, TimestampMixin, Base): """员工薪资标准: 账套 + 个性化覆盖.""" __tablename__ = "sal_employee_salary" employee_id: Mapped[int] = mapped_column(Integer, unique=True, index=True) plan_id: Mapped[int] = mapped_column(Integer, index=True) overrides: Mapped[dict | None] = mapped_column(JSON) # {item_code: amount} effective_date: Mapped[date | None] = mapped_column(Date) bank_name: Mapped[str | None] = mapped_column(String(64)) bank_account: Mapped[str | None] = mapped_column(String(64)) special_deduction: Mapped[Decimal | None] = mapped_column(Money) # 专项附加扣除/月 is_active: Mapped[bool] = mapped_column(Boolean, default=True) remark: Mapped[str | None] = mapped_column(String(255)) class PayrollPeriod(PKMixin, Base): __tablename__ = "sal_payroll_period" year: Mapped[int] = mapped_column(Integer, index=True) month: Mapped[int] = mapped_column(Integer, index=True) label: Mapped[str] = mapped_column(String(32)) pay_date: Mapped[date | None] = mapped_column(Date) status: Mapped[str] = mapped_column(String(16), default="open") # open/closed/archived remark: Mapped[str | None] = mapped_column(String(255)) class PayrollBatch(PKMixin, TimestampMixin, Base): __tablename__ = "sal_payroll_batch" __table_args__ = (Index("ix_batch_period_status", "period_id", "status"),) name: Mapped[str] = mapped_column(String(64)) period_id: Mapped[int] = mapped_column(Integer, index=True) year: Mapped[int] = mapped_column(Integer, index=True) month: Mapped[int] = mapped_column(Integer) scope: Mapped[dict | None] = mapped_column(JSON) # {department_ids: [], employee_ids: []} status: Mapped[str] = mapped_column(String(16), default="draft", index=True) # draft -> calculated -> approved -> paid (rollback allowed before paid) employee_count: Mapped[int] = mapped_column(Integer, default=0) gross_total: Mapped[Decimal | None] = mapped_column(Money) deduction_total: Mapped[Decimal | None] = mapped_column(Money) net_total: Mapped[Decimal | None] = mapped_column(Money) tax_total: Mapped[Decimal | None] = mapped_column(Money) insurance_total: Mapped[Decimal | None] = mapped_column(Money) calculated_at: Mapped[datetime | None] = mapped_column(DateTime) calculated_by: Mapped[int | None] = mapped_column(Integer) approved_at: Mapped[datetime | None] = mapped_column(DateTime) approved_by: Mapped[int | None] = mapped_column(Integer) paid_at: Mapped[datetime | None] = mapped_column(DateTime) bank_batch_no: Mapped[str | None] = mapped_column(String(64)) remark: Mapped[str | None] = mapped_column(String(255)) class Payslip(PKMixin, TimestampMixin, Base): __tablename__ = "sal_payslip" __table_args__ = (Index("ix_payslip_emp_period", "employee_id", "year", "month"),) batch_id: Mapped[int | None] = mapped_column(Integer, index=True) employee_id: Mapped[int] = mapped_column(Integer, index=True) period_id: Mapped[int | None] = mapped_column(Integer, index=True) year: Mapped[int] = mapped_column(Integer, index=True) month: Mapped[int] = mapped_column(Integer) items: Mapped[dict | None] = mapped_column(JSON) # {item_code: amount} gross: Mapped[Decimal | None] = mapped_column(Money) insurance_personal: Mapped[Decimal | None] = mapped_column(Money) insurance_company: Mapped[Decimal | None] = mapped_column(Money) special_deduction: Mapped[Decimal | None] = mapped_column(Money) taxable_cumulative: Mapped[Decimal | None] = mapped_column(Money) tax: Mapped[Decimal | None] = mapped_column(Money) deductions: Mapped[Decimal | None] = mapped_column(Money) net: Mapped[Decimal | None] = mapped_column(Money) working_days: Mapped[float | None] = mapped_column() actual_days: Mapped[float | None] = mapped_column() insurance_plan_id: Mapped[int | None] = mapped_column(Integer) status: Mapped[str] = mapped_column(String(16), default="draft", index=True) confirmed_at: Mapped[datetime | None] = mapped_column(DateTime) remark: Mapped[str | None] = mapped_column(String(255))