Files
ctms/backend/app/crud/contract_fee.py
Cheng Zhou 720c98765f 迁移合同费用通用附件
1、删除旧费用附件 API、CRUD、模型和专用前端面板,统一改用通用附件能力。

2、调整合同费用接口为 study_id 语义,并补充合同、凭证、发票等附件类型的权限映射。

3、将合同费用新建和编辑改为抽屉流程,详情页与列表页复用同一编辑组件。

4、补充数据库迁移、附件列表、合同费用抽屉和权限场景测试,覆盖旧权限移除与新流程。
2026-06-04 11:07:13 +08:00

122 lines
4.0 KiB
Python

import uuid
from datetime import date
from typing import Sequence
from sqlalchemy import case, func, or_, select
from sqlalchemy.ext.asyncio import AsyncSession
from app.models.contract_fee import ContractFee
from app.models.contract_fee_payment import ContractFeePayment
from app.models.site import Site
from app.schemas.contract_fee import ContractFeeCreate, ContractFeeUpdate
async def create_contract_fee(
db: AsyncSession,
contract_in: ContractFeeCreate,
) -> ContractFee:
contract = ContractFee(
project_id=contract_in.study_id,
center_id=contract_in.center_id,
contract_no=contract_in.contract_no,
signed_date=contract_in.signed_date,
contract_amount=contract_in.contract_amount,
currency=contract_in.currency,
remark=contract_in.remark,
contract_cases=contract_in.contract_cases,
actual_cases=contract_in.actual_cases,
settlement_amount=contract_in.settlement_amount,
final_payment_amount=contract_in.final_payment_amount,
)
db.add(contract)
await db.commit()
await db.refresh(contract)
return contract
async def get_contract_fee(db: AsyncSession, contract_id: uuid.UUID) -> ContractFee | None:
result = await db.execute(select(ContractFee).where(ContractFee.id == contract_id))
return result.scalar_one_or_none()
async def get_contract_fee_by_study_center(
db: AsyncSession, study_id: uuid.UUID, center_id: uuid.UUID
) -> ContractFee | None:
result = await db.execute(
select(ContractFee).where(ContractFee.project_id == study_id, ContractFee.center_id == center_id)
)
return result.scalar_one_or_none()
async def list_contract_fees(
db: AsyncSession,
study_id: uuid.UUID,
center_id: uuid.UUID | None = None,
center_ids: set[uuid.UUID] | None = None,
q: str | None = None,
) -> Sequence[tuple[ContractFee, str, float, float, date | None, date | None]]:
paid_total = func.coalesce(
func.sum(case((ContractFeePayment.is_paid.is_(True), ContractFeePayment.amount), else_=0)),
0,
).label("paid_total")
verified_total = func.coalesce(
func.sum(case((ContractFeePayment.is_verified.is_(True), ContractFeePayment.amount), else_=0)),
0,
).label("verified_total")
last_paid_date = func.max(
case((ContractFeePayment.is_paid.is_(True), ContractFeePayment.paid_date), else_=None)
).label("last_paid_date")
last_verified_date = func.max(
case((ContractFeePayment.is_verified.is_(True), ContractFeePayment.verified_date), else_=None)
).label("last_verified_date")
stmt = (
select(
ContractFee,
Site.name.label("center_name"),
paid_total,
verified_total,
last_paid_date,
last_verified_date,
)
.join(Site, Site.id == ContractFee.center_id)
.outerjoin(ContractFeePayment, ContractFeePayment.contract_fee_id == ContractFee.id)
.where(ContractFee.project_id == study_id)
.group_by(ContractFee.id, Site.name)
)
if center_id:
stmt = stmt.where(ContractFee.center_id == center_id)
if center_ids is not None:
if not center_ids:
return []
stmt = stmt.where(ContractFee.center_id.in_(center_ids))
if q:
conditions = [Site.name.ilike(f"%{q}%")]
try:
conditions.append(Site.id == uuid.UUID(q))
except (ValueError, TypeError):
pass
stmt = stmt.where(or_(*conditions))
stmt = stmt.order_by(Site.name.asc())
result = await db.execute(stmt)
return result.all()
async def update_contract_fee(
db: AsyncSession, contract: ContractFee, contract_in: ContractFeeUpdate
) -> ContractFee:
update_data = contract_in.model_dump(exclude_unset=True)
for key, value in update_data.items():
setattr(contract, key, value)
await db.commit()
await db.refresh(contract)
return contract
async def delete_contract_fee(db: AsyncSession, contract: ContractFee) -> None:
await db.delete(contract)
await db.commit()