"""概算、预算与它们的版本(草稿/送审/批准)。 对应重构范围内的两个模块:**项目立项(概算)信息** 与 **项目预算信息**。 为什么要引入 Revision ===================== 老系统给每张预算明细表都配了一份 ``*_temp`` 影子表(收入、采购、收款计划、付款计划、 项目管理、资金计划六对十二张表),"草稿"就是写 temp,"提交"就是整表搬到正式表。 于是任何一次读取都要先判断"这个用户在读草稿还是读正式版",跨表统计(利润、资金计划) 在两套表上算出来的结果还不一样。 这里改成一张 ``biz_revision``:一个版本一行,明细行用 ``revision_id`` 挂过去。 好处是草稿与正式版走完全相同的查询与计算代码,只是 ``status`` 不同;也天然支持 "驳回后改出新版本、老版本仍可追溯"。 计算快照 ======== ``TO-BE-EST-001`` / ``TO-BE-BUD-SSOT-001`` 要求"原始输入 + 计算结果快照 + 规则版本", 所以明细行只存人工输入与必要的回退值,所有派生金额(不含税、税额、分类合计、利润、 资金计划余额、垫资、还款、利息)一律落在 ``calc_snapshot.payload_json``,并记录 ``calc_version``。审批看的就是这份快照,事后重算对不上也能立刻发现。 """ from __future__ import annotations from datetime import date, datetime from decimal import Decimal from sqlalchemy import ForeignKey, Index, Integer, String, Text, UniqueConstraint, text from sqlalchemy.orm import Mapped, mapped_column from .base import Base, SoftDeleteMixin, date_col, money, percent, timestamp_col, u_bigint, u_tinyint class IncomeType: """收入分类(老 ``project_estimate_income.type`` / 明细 type)。""" DEVICE = 1 # 设备类 ENGINEERING = 2 # 工程类 SERVICE = 3 # 服务类 LABELS = {DEVICE: "设备类", ENGINEERING: "工程类", SERVICE: "服务类"} ALL = (DEVICE, ENGINEERING, SERVICE) class PurchaseType: """采购成本分类。""" DEVICE = 1 # 设备 CONSTRUCTION = 2 # 施工 SERVICE = 3 # 服务 OTHER = 4 # 其他 LABELS = {DEVICE: "设备", CONSTRUCTION: "施工", SERVICE: "服务", OTHER: "其他"} ALL = (DEVICE, CONSTRUCTION, SERVICE, OTHER) class CostFeeGroup: """成本大类(老 ``project_budget_cost.fee``)。""" PURCHASE = 1 # 采购成本 PROJECT_MANAGE = 2 # 项目管理成本 OTHER = 3 # 其他 class CostType: """成本汇总行的分类(老 ``project_*_cost.type``,1..6)。""" DEVICE = 1 CONSTRUCTION = 2 SERVICE = 3 OTHER = 4 PROJECT_MANAGE = 5 OTHER_OTHER = 6 LABELS = {DEVICE: "设备", CONSTRUCTION: "施工", SERVICE: "服务", OTHER: "其他", PROJECT_MANAGE: "项目管理成本", OTHER_OTHER: "其他其他"} class ProductCategory: """采购明细的产品归属(BUD-COST-002 的四个桶)。 这几个编码同时是 :data:`app.service.calc.mapping.PRODUCT_CATEGORY_TO_FIELD` 的键, 也就是项目主档上那四列产品金额的归属。前端曾经自己抄过一份 (``integration`` / ``other``),后果是选了那两个归属的采购额永远投影不到 ``project.huazhi_amount`` 这些列上——不报错,只是数对不上。所以取值只认这一份, ``tests/test_dict.py`` 里钉了一条"字典与内核映射表取值一致"的断言。 """ HUIZHI = "huizhi" HUAZHI = "huazhi" H3C = "h3c" ZIGUANG_OTHER = "ziguang_other" LABELS = {HUIZHI: "汇智产品", HUAZHI: "华智产品", H3C: "华三产品", ZIGUANG_OTHER: "紫光其他产品"} class OtherCostBucket: """概算"管理与财务费用"行属于利润公式的哪一层。 计算器按 ``bucket`` 取数(见 ``engine.estimate_payload``),列注释里那四个编码 就是全部合法值;前端那份多出来的 ``finance_cost`` 谁都不读,选了等于没录。 """ PROJECT_MANAGE = "project_manage" OTHER_COST = "other_cost" CAPITAL_INTEREST = "capital_interest" COMPANY_MANAGE = "company_manage" LABELS = {PROJECT_MANAGE: "项目管理费用", OTHER_COST: "其他成本", CAPITAL_INTEREST: "资金占用成本", COMPANY_MANAGE: "公司管理费用"} class EstimateManageType: """概算"管理与财务费用"行的分类(老 ``project_estimate_cost_manage.type``)。""" CAPITAL = 1 # 资金占用成本 COMPANY_MANAGE = 2 # 公司管理费用 class RevisionStatus: """版本状态。""" DRAFT = "draft" REVIEWING = "reviewing" APPROVED = "approved" REJECTED = "rejected" WITHDRAWN = "withdrawn" LABELS = {DRAFT: "草稿", REVIEWING: "审批中", APPROVED: "已通过", REJECTED: "已驳回", WITHDRAWN: "已撤回"} #: 可以改明细的状态 EDITABLE = (DRAFT, REJECTED, WITHDRAWN) #: 已经生效过、可以当作"上一版"引用的状态 EFFECTIVE = (APPROVED,) class RevisionModule: ESTIMATE = "estimate" BUDGET = "budget" class Revision(Base, SoftDeleteMixin): """一次概算/预算的版本(草稿或已送审)。 ``freeze_json`` 是送审那一刻把项目主档与关键指标冻结下来的 JSON。审批人看到的是 送审时的口径,即使发起人之后改了项目名也不会跟着变——老系统把五十多列平铺到 ``project_budget`` 上就是为了这件事,这里用一个 JSON 冻结块顶替那五十多列。 """ __tablename__ = "biz_revision" __table_args__ = ( UniqueConstraint("module", "project_id", "version", name="uk_biz_revision_version"), Index("idx_biz_revision_project", "project_id", "status"), {"mysql_charset": "utf8mb4", "mysql_collate": "utf8mb4_unicode_ci"}, ) id: Mapped[int] = mapped_column(u_bigint(), primary_key=True, autoincrement=True) module: Mapped[str] = mapped_column(String(16), nullable=False, comment="estimate/budget") project_id: Mapped[int] = mapped_column( u_bigint(), ForeignKey("project.id", ondelete="CASCADE"), nullable=False, index=True) version: Mapped[int] = mapped_column(Integer, default=1, server_default="1", nullable=False) status: Mapped[str] = mapped_column( String(16), default=RevisionStatus.DRAFT, nullable=False, index=True) title: Mapped[str] = mapped_column(String(255), default="", nullable=False, comment="版本说明") # 从哪一版派生出来的(驳回后改新版用) based_on_id: Mapped[int | None] = mapped_column(u_bigint(), nullable=True) snapshot_id: Mapped[int | None] = mapped_column(u_bigint(), nullable=True, comment="最近一次计算快照") calc_version: Mapped[str] = mapped_column(String(32), default="", nullable=False) freeze_json: Mapped[str] = mapped_column(Text, default="", nullable=False) creator_id: Mapped[int] = mapped_column( u_bigint(), ForeignKey("accounts.id", ondelete="RESTRICT"), nullable=False) creator_name: Mapped[str] = mapped_column(String(100), default="", nullable=False) submitted_at: Mapped[datetime | None] = mapped_column(timestamp_col(), nullable=True) decided_at: Mapped[datetime | None] = mapped_column(timestamp_col(), nullable=True) # 表头:老系统把"合同名称""解决方案协同""收入/采购明细附件"平铺在 project_budget 上, # 这些既不是明细行也不该进冻结块(冻结块是送审那一刻的口径,表头是录入过程中的草稿), # 所以单独一个 JSON 列,随版本走。 # 用 VARCHAR(2000) 而不是 Text:MySQL 不给 TEXT 列在 ALTER 时带默认值 # (1101 BLOB/TEXT can't have a default value),而开发档启动是"补列"式的, # 用 Text 会让老库升不上来。表头就几个名字与一个协同标记,两千字够用。 header_json: Mapped[str] = mapped_column(String(2000), default="", server_default="", nullable=False, comment="表头字段 JSON:合同名称、解决方案协同、附件名") created_at: Mapped[datetime] = mapped_column( timestamp_col(), default=datetime.now, server_default=text("CURRENT_TIMESTAMP(3)"), nullable=False) updated_at: Mapped[datetime] = mapped_column( timestamp_col(), default=datetime.now, onupdate=datetime.now, server_default=text("CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3)"), nullable=False) class CalcSnapshot(Base): """一次计算的完整结果。审批、导出、对账都读它,不再各自算第二遍。""" __tablename__ = "calc_snapshot" __table_args__ = ( Index("idx_calc_snapshot_revision", "module", "revision_id", "id"), {"mysql_charset": "utf8mb4", "mysql_collate": "utf8mb4_unicode_ci"}, ) id: Mapped[int] = mapped_column(u_bigint(), primary_key=True, autoincrement=True) module: Mapped[str] = mapped_column(String(16), nullable=False) project_id: Mapped[int] = mapped_column(u_bigint(), nullable=False, index=True) revision_id: Mapped[int] = mapped_column(u_bigint(), nullable=False, index=True) calc_version: Mapped[str] = mapped_column(String(32), nullable=False) # 触发来源:edit(改明细)/ preview(前端预览)/ submit(送审)/ approval(审批落库) trigger: Mapped[str] = mapped_column(String(16), default="edit", nullable=False) payload_json: Mapped[str] = mapped_column(Text, nullable=False) # 校验结果(限额、勾稽)也一起存,审批人要看得到当时的告警 validation_json: Mapped[str] = mapped_column(Text, default="", nullable=False) account_id: Mapped[int | None] = mapped_column(u_bigint(), nullable=True) account_name: Mapped[str] = mapped_column(String(100), default="", nullable=False) trace_id: Mapped[str] = mapped_column(String(64), default="", nullable=False) created_at: Mapped[datetime] = mapped_column( timestamp_col(), default=datetime.now, server_default=text("CURRENT_TIMESTAMP(3)"), nullable=False) # --------------------------------------------------------------------------- # 概算:明细行 # --------------------------------------------------------------------------- class EstimateIncomeLine(Base): """概算收入行。老概算只存分类汇总(一类一行),这里存到明细,满足 TO-BE-EST-001。""" __tablename__ = "estimate_income_line" __table_args__ = ( Index("idx_estimate_income_revision", "revision_id", "income_type"), {"mysql_charset": "utf8mb4", "mysql_collate": "utf8mb4_unicode_ci"}, ) id: Mapped[int] = mapped_column(u_bigint(), primary_key=True, autoincrement=True) revision_id: Mapped[int] = mapped_column( u_bigint(), ForeignKey("biz_revision.id", ondelete="CASCADE"), nullable=False, index=True) project_id: Mapped[int] = mapped_column(u_bigint(), nullable=False, index=True) income_type: Mapped[int] = mapped_column(Integer, nullable=False, comment="1设备 2工程 3服务") name: Mapped[str] = mapped_column(String(255), default="", nullable=False, comment="收入项名称") # 人工输入:含税金额(或 数量×单价 二选一,允许只填含税总额) include_amount: Mapped[Decimal] = mapped_column(money(), default=Decimal("0"), nullable=False, comment="含税金额") tax_rate: Mapped[Decimal] = mapped_column(percent(), default=Decimal("0"), nullable=False, comment="税率百分数,13 表示 13%") qty: Mapped[Decimal] = mapped_column(money(18, 4), default=Decimal("0"), nullable=False) unit_price: Mapped[Decimal] = mapped_column(money(20, 5), default=Decimal("0"), nullable=False) remark: Mapped[str] = mapped_column(String(500), default="", nullable=False) sort: Mapped[int] = mapped_column(Integer, default=100, server_default="100", nullable=False) class EstimateCostLine(Base): """概算采购成本行。""" __tablename__ = "estimate_cost_line" __table_args__ = ( Index("idx_estimate_cost_revision", "revision_id", "purchase_type"), {"mysql_charset": "utf8mb4", "mysql_collate": "utf8mb4_unicode_ci"}, ) id: Mapped[int] = mapped_column(u_bigint(), primary_key=True, autoincrement=True) revision_id: Mapped[int] = mapped_column( u_bigint(), ForeignKey("biz_revision.id", ondelete="CASCADE"), nullable=False, index=True) project_id: Mapped[int] = mapped_column(u_bigint(), nullable=False, index=True) purchase_type: Mapped[int] = mapped_column(Integer, nullable=False, comment="1设备 2施工 3服务 4其他") name: Mapped[str] = mapped_column(String(255), default="", nullable=False) include_amount: Mapped[Decimal] = mapped_column(money(), default=Decimal("0"), nullable=False) tax_rate: Mapped[Decimal] = mapped_column(percent(), default=Decimal("0"), nullable=False) qty: Mapped[Decimal] = mapped_column(money(18, 4), default=Decimal("0"), nullable=False) unit_price: Mapped[Decimal] = mapped_column(money(20, 5), default=Decimal("0"), nullable=False) # 产品归属,用于汇智/华智/华三/紫光其他四项的聚合(BUD-COST-002) product_category: Mapped[str] = mapped_column(String(64), default="", nullable=False) remark: Mapped[str] = mapped_column(String(500), default="", nullable=False) sort: Mapped[int] = mapped_column(Integer, default=100, server_default="100", nullable=False) class EstimateOtherLine(Base): """概算的"其他成本 / 项目管理费用 / 资金占用 / 公司管理费用"行。 老系统把这几项散在 ``project_estimate_cost_manage``(只有两类)与项目管理成本表里, 利润公式里到底是哪几项参与全靠读 getter 猜。这里显式一张表,用 ``bucket`` 说明它 属于利润公式的哪一层,计算器照 bucket 取数,不再猜。 """ __tablename__ = "estimate_other_line" __table_args__ = ( Index("idx_estimate_other_revision", "revision_id", "bucket"), {"mysql_charset": "utf8mb4", "mysql_collate": "utf8mb4_unicode_ci"}, ) id: Mapped[int] = mapped_column(u_bigint(), primary_key=True, autoincrement=True) revision_id: Mapped[int] = mapped_column( u_bigint(), ForeignKey("biz_revision.id", ondelete="CASCADE"), nullable=False, index=True) project_id: Mapped[int] = mapped_column(u_bigint(), nullable=False, index=True) bucket: Mapped[str] = mapped_column( String(32), nullable=False, comment="other_cost/project_manage/capital_interest/company_manage") name: Mapped[str] = mapped_column(String(255), default="", nullable=False) exclude_amount: Mapped[Decimal] = mapped_column(money(), default=Decimal("0"), nullable=False, comment="不含税金额") manual: Mapped[bool] = mapped_column(u_tinyint(), default=True, server_default="1", nullable=False, comment="人工估算还是系统派生(TO-BE-EST-001)") remark: Mapped[str] = mapped_column(String(500), default="", nullable=False) sort: Mapped[int] = mapped_column(Integer, default=100, server_default="100", nullable=False) # --------------------------------------------------------------------------- # 预算:明细行。截图里"项目预算信息"下的八个页签,一一对应下面这些表 # --------------------------------------------------------------------------- class BudgetIncomeLine(Base): """收入明细表。""" __tablename__ = "budget_income_line" __table_args__ = ( Index("idx_budget_income_revision", "revision_id", "income_type"), {"mysql_charset": "utf8mb4", "mysql_collate": "utf8mb4_unicode_ci"}, ) id: Mapped[int] = mapped_column(u_bigint(), primary_key=True, autoincrement=True) revision_id: Mapped[int] = mapped_column( u_bigint(), ForeignKey("biz_revision.id", ondelete="CASCADE"), nullable=False, index=True) project_id: Mapped[int] = mapped_column(u_bigint(), nullable=False, index=True) income_type: Mapped[int] = mapped_column(Integer, nullable=False, comment="1设备 2工程 3服务") name: Mapped[str] = mapped_column(String(255), default="", nullable=False, comment="产品名称") spec: Mapped[str] = mapped_column(String(255), default="", nullable=False, comment="规格型号") param: Mapped[str] = mapped_column(Text, default="", nullable=False, comment="参数") unit: Mapped[str] = mapped_column(String(64), default="", nullable=False, comment="单位") qty: Mapped[Decimal] = mapped_column(money(18, 4), default=Decimal("0"), nullable=False, comment="数量") unit_price: Mapped[Decimal] = mapped_column(money(20, 5), default=Decimal("0"), nullable=False, comment="单价") # 允许人工直接指定含税总额,且以它为准(TAX-001;DECISION-AMT-001 待业务签字) include_amount: Mapped[Decimal | None] = mapped_column(money(), nullable=True, comment="人工含税总额;为空时用 数量×单价") tax_rate: Mapped[Decimal] = mapped_column(percent(), default=Decimal("0"), nullable=False) expiration_date: Mapped[str] = mapped_column(String(32), default="", nullable=False) remark: Mapped[str] = mapped_column(String(500), default="", nullable=False) sort: Mapped[int] = mapped_column(Integer, default=100, server_default="100", nullable=False) class BudgetIncomePlanLine(Base): """收款计划表。""" __tablename__ = "budget_income_plan_line" __table_args__ = ( Index("idx_budget_income_plan_revision", "revision_id"), {"mysql_charset": "utf8mb4", "mysql_collate": "utf8mb4_unicode_ci"}, ) id: Mapped[int] = mapped_column(u_bigint(), primary_key=True, autoincrement=True) revision_id: Mapped[int] = mapped_column( u_bigint(), ForeignKey("biz_revision.id", ondelete="CASCADE"), nullable=False, index=True) project_id: Mapped[int] = mapped_column(u_bigint(), nullable=False, index=True) receive_point: Mapped[str] = mapped_column(String(255), default="", nullable=False, comment="收款节点") receive_date: Mapped[date | None] = mapped_column(date_col(), nullable=True, comment="收款时间") amount: Mapped[Decimal] = mapped_column(money(), default=Decimal("0"), nullable=False, comment="收款金额") # 保证金收款参与资金计划(PLAN-001),所以要能标出来 is_earnest: Mapped[bool] = mapped_column(u_tinyint(), default=False, server_default="0", nullable=False) remark: Mapped[str] = mapped_column(String(500), default="", nullable=False) sort: Mapped[int] = mapped_column(Integer, default=100, server_default="100", nullable=False) class BudgetPurchaseLine(Base): """采购成本明细表。""" __tablename__ = "budget_purchase_line" __table_args__ = ( Index("idx_budget_purchase_revision", "revision_id", "purchase_type"), {"mysql_charset": "utf8mb4", "mysql_collate": "utf8mb4_unicode_ci"}, ) id: Mapped[int] = mapped_column(u_bigint(), primary_key=True, autoincrement=True) revision_id: Mapped[int] = mapped_column( u_bigint(), ForeignKey("biz_revision.id", ondelete="CASCADE"), nullable=False, index=True) project_id: Mapped[int] = mapped_column(u_bigint(), nullable=False, index=True) purchase_type: Mapped[int] = mapped_column(Integer, nullable=False, comment="1设备 2施工 3服务 4其他") category: Mapped[str] = mapped_column(String(128), default="", nullable=False, comment="类别") # 产品归属:老代码同时兼容"分类 ID 字符串"和"中文分类名"(BUD-COST-002), # 这里只留编码,中文由字典表给。 product_category: Mapped[str] = mapped_column(String(64), default="", nullable=False, comment="汇智/华智/华三/紫光其他的编码") name: Mapped[str] = mapped_column(String(255), default="", nullable=False) unit: Mapped[str] = mapped_column(String(64), default="", nullable=False) qty: Mapped[Decimal] = mapped_column(money(18, 4), default=Decimal("0"), nullable=False) unit_price: Mapped[Decimal] = mapped_column(money(20, 5), default=Decimal("0"), nullable=False) include_amount: Mapped[Decimal | None] = mapped_column(money(), nullable=True) tax_rate: Mapped[Decimal] = mapped_column(percent(), default=Decimal("0"), nullable=False) contract_party: Mapped[str] = mapped_column(String(255), default="", nullable=False, comment="签约方") is_underwritten: Mapped[bool] = mapped_column(u_tinyint(), default=False, server_default="0", nullable=False, comment="是否垫资") underwritten_amount: Mapped[Decimal] = mapped_column(money(), default=Decimal("0"), nullable=False, comment="预估垫资金额") pay_date: Mapped[date | None] = mapped_column(date_col(), nullable=True, comment="支出时间") pay_amount: Mapped[Decimal] = mapped_column(money(), default=Decimal("0"), nullable=False, comment="支出金额") pay_way: Mapped[str] = mapped_column(String(255), default="", nullable=False, comment="付款方式;老库这列塞的是合同付款条款,实测最长 136 字") remark: Mapped[str] = mapped_column(String(500), default="", nullable=False) sort: Mapped[int] = mapped_column(Integer, default=100, server_default="100", nullable=False) class BudgetPayPlanLine(Base): """付款计划表。""" __tablename__ = "budget_pay_plan_line" __table_args__ = ( Index("idx_budget_pay_plan_revision", "revision_id"), {"mysql_charset": "utf8mb4", "mysql_collate": "utf8mb4_unicode_ci"}, ) id: Mapped[int] = mapped_column(u_bigint(), primary_key=True, autoincrement=True) revision_id: Mapped[int] = mapped_column( u_bigint(), ForeignKey("biz_revision.id", ondelete="CASCADE"), nullable=False, index=True) project_id: Mapped[int] = mapped_column(u_bigint(), nullable=False, index=True) pay_point: Mapped[str] = mapped_column(String(255), default="", nullable=False, comment="付款节点") pay_date: Mapped[date | None] = mapped_column(date_col(), nullable=True) # 采购成本项目:决定这笔付款落到资金计划的哪一列(PLAN-001 / PLAN-VAL-001) purchase_type: Mapped[int | None] = mapped_column(Integer, nullable=True, comment="1设备 2施工 3服务 4其他") amount: Mapped[Decimal] = mapped_column(money(), default=Decimal("0"), nullable=False) supplier: Mapped[str] = mapped_column(String(255), default="", nullable=False, comment="供应商") is_earnest: Mapped[bool] = mapped_column(u_tinyint(), default=False, server_default="0", nullable=False, comment="保证金付款") remark: Mapped[str] = mapped_column(String(500), default="", nullable=False) sort: Mapped[int] = mapped_column(Integer, default=100, server_default="100", nullable=False) class ManageItemCode: """项目管理费用里"差旅费/业务费"的稳定编码。 ``BUD-VAL-001`` 的老实现是用 ``type == 2 且 name 为 "4" 或 "5"`` 认这两项费用, 页面上加一个选项就会悄悄失效。这里改成稳定编码 + 显式标记 ``counts_to_travel_limit``。 """ LABOR = "labor" # 人工 TRAVEL = "travel" # 人员差旅费(计入 10% 限额) BUSINESS = "business" # 业务费用(计入 10% 限额) BIZ_ACCEPT = "biz_acceptance" # 商务及专项验收费 OFFICE = "office" # 办公费用 HQ_SUPPORT = "hq_support" # 总部支撑人力费用 OTHER = "other" TRAVEL_LIMIT_CODES = (TRAVEL, BUSINESS) class BudgetManageLine(Base): """项目管理费用表。""" __tablename__ = "budget_manage_line" __table_args__ = ( Index("idx_budget_manage_revision", "revision_id", "manage_type"), {"mysql_charset": "utf8mb4", "mysql_collate": "utf8mb4_unicode_ci"}, ) id: Mapped[int] = mapped_column(u_bigint(), primary_key=True, autoincrement=True) revision_id: Mapped[int] = mapped_column( u_bigint(), ForeignKey("biz_revision.id", ondelete="CASCADE"), nullable=False, index=True) project_id: Mapped[int] = mapped_column(u_bigint(), nullable=False, index=True) manage_type: Mapped[int] = mapped_column(Integer, default=1, nullable=False, comment="1人工 2业务费用 3其他") item_code: Mapped[str] = mapped_column(String(64), default=ManageItemCode.OTHER, nullable=False) item_name: Mapped[str] = mapped_column(String(255), default="", nullable=False, comment="名称") detail: Mapped[str] = mapped_column(Text, default="", nullable=False) unit: Mapped[str] = mapped_column(String(64), default="", nullable=False) # 数量允许小数:RISK-BUD-PM-001 里前端 parseInt 会截断 qty: Mapped[Decimal] = mapped_column(money(18, 4), default=Decimal("0"), nullable=False) unit_price: Mapped[Decimal] = mapped_column(money(20, 5), default=Decimal("0"), nullable=False) total: Mapped[Decimal | None] = mapped_column(money(), nullable=True, comment="人工总额;为空用 数量×单价") predict_method: Mapped[str] = mapped_column(String(255), default="", nullable=False, comment="预估计算方法") predict_why: Mapped[str] = mapped_column(String(255), default="", nullable=False, comment="预估依据") pay_date: Mapped[date | None] = mapped_column(date_col(), nullable=True) pay_amount: Mapped[Decimal] = mapped_column(money(), default=Decimal("0"), nullable=False) # 固定项(老系统 fourcal.fixedprojectmanagedetails 配的那批)不可删 deletable: Mapped[bool] = mapped_column(u_tinyint(), default=True, server_default="1", nullable=False) counts_to_travel_limit: Mapped[bool] = mapped_column( u_tinyint(), default=False, server_default="0", nullable=False, comment="是否计入差旅/业务费 10% 限额") remark: Mapped[str] = mapped_column(String(500), default="", nullable=False) sort: Mapped[int] = mapped_column(Integer, default=100, server_default="100", nullable=False) class BudgetFundMonth(Base): """资金计划表:一个月一行。 只存人工输入(各类支出、收款、保证金、年利率快照)。总支出、总收入、余额、垫资、 还款、利息全部由 :func:`app.service.calc.fund_plan.calculate` 按月份顺序整条重算 (``TO-BE-PLAN-001``),根除 ``RISK-PLAN-DERIVED-001``:付款计划一变就整条重算, 而不是只覆盖四列。 """ __tablename__ = "budget_fund_month" __table_args__ = ( UniqueConstraint("revision_id", "month", name="uk_budget_fund_month"), Index("idx_budget_fund_month_project", "project_id", "month"), {"mysql_charset": "utf8mb4", "mysql_collate": "utf8mb4_unicode_ci"}, ) id: Mapped[int] = mapped_column(u_bigint(), primary_key=True, autoincrement=True) revision_id: Mapped[int] = mapped_column( u_bigint(), ForeignKey("biz_revision.id", ondelete="CASCADE"), nullable=False, index=True) project_id: Mapped[int] = mapped_column(u_bigint(), nullable=False, index=True) month: Mapped[str] = mapped_column(String(8), nullable=False, comment="YYYY-MM") # --- 人工输入 ------------------------------------------------------------- device_cost: Mapped[Decimal] = mapped_column(money(), default=Decimal("0"), nullable=False, comment="设备支出") construction_cost: Mapped[Decimal] = mapped_column(money(), default=Decimal("0"), nullable=False, comment="施工支出") service_cost: Mapped[Decimal] = mapped_column(money(), default=Decimal("0"), nullable=False, comment="服务支出") other_cost: Mapped[Decimal] = mapped_column(money(), default=Decimal("0"), nullable=False, comment="其他支出") manage_cost: Mapped[Decimal] = mapped_column(money(), default=Decimal("0"), nullable=False, comment="项目管理支出") earnest_cost: Mapped[Decimal] = mapped_column(money(), default=Decimal("0"), nullable=False, comment="保证金支出") sale_income: Mapped[Decimal] = mapped_column(money(), default=Decimal("0"), nullable=False, comment="销售收款") earnest_income: Mapped[Decimal] = mapped_column(money(), default=Decimal("0"), nullable=False, comment="保证金收入") interest_rate: Mapped[Decimal] = mapped_column(percent(), default=Decimal("0"), nullable=False, comment="当月使用的年利率快照") opening_balance: Mapped[Decimal] = mapped_column(money(), default=Decimal("0"), nullable=False, comment="期初余额,首月一般为 0") # 该行金额是手工覆盖的还是由付款/收款计划汇总来的 manual_override: Mapped[bool] = mapped_column(u_tinyint(), default=False, server_default="0", nullable=False) # --- 派生:由计算器写入,禁止其它代码改 ----------------------------------- total_cost: Mapped[Decimal] = mapped_column(money(), default=Decimal("0"), nullable=False) total_income: Mapped[Decimal] = mapped_column(money(), default=Decimal("0"), nullable=False) balance: Mapped[Decimal] = mapped_column(money(), default=Decimal("0"), nullable=False, comment="月末资金余额") underwritten_plan: Mapped[Decimal] = mapped_column(money(), default=Decimal("0"), nullable=False, comment="垫资计划") repayment_plan: Mapped[Decimal] = mapped_column(money(), default=Decimal("0"), nullable=False, comment="还款计划") capital_interest: Mapped[Decimal] = mapped_column(money(), default=Decimal("0"), nullable=False, comment="资金利息") calc_version: Mapped[str] = mapped_column(String(32), default="", nullable=False) class BudgetOtherCostLine(Base): """其他成本行。 ``BUD-OTHER-001`` 的老实现是"其他成本不在预算明细里,而是去冗余汇总表读 type=6", 于是它既不能被编辑也无法追溯。这里给它一张正经的明细表,是否参与毛利由业务口径 配置决定(``DECISION-BUD-PROFIT-001``)。 """ __tablename__ = "budget_other_cost_line" __table_args__ = ( Index("idx_budget_other_cost_revision", "revision_id"), {"mysql_charset": "utf8mb4", "mysql_collate": "utf8mb4_unicode_ci"}, ) id: Mapped[int] = mapped_column(u_bigint(), primary_key=True, autoincrement=True) revision_id: Mapped[int] = mapped_column( u_bigint(), ForeignKey("biz_revision.id", ondelete="CASCADE"), nullable=False, index=True) project_id: Mapped[int] = mapped_column(u_bigint(), nullable=False, index=True) name: Mapped[str] = mapped_column(String(255), default="", nullable=False) exclude_amount: Mapped[Decimal] = mapped_column(money(), default=Decimal("0"), nullable=False) remark: Mapped[str] = mapped_column(String(500), default="", nullable=False) sort: Mapped[int] = mapped_column(Integer, default=100, server_default="100", nullable=False)