| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401 |
- from __future__ import annotations
- import mimetypes
- from decimal import Decimal
- from typing import List, Optional, Sequence, Tuple
- from fastapi import HTTPException, status
- from sqlalchemy import or_
- from sqlalchemy.orm import Session
- from app.core.bx_auth import BxUserContext
- from app.core.menu_guard import SUPER_ADMIN_ROLE
- from app.core.tenant_context import get_tenant_no
- from app.models.bx_models import BxAttachment, BxInvoice, BxInvoiceLedger, BxInvoiceWalletSummary
- from app.models.models import SysUser
- from app.services.bx.attachment import AttachmentService
- from app.services.bx.base import BxCrudService
- from app.services.bx.invoice_ledger import (
- ENTRY_CREDIT_UPLOAD,
- ENTRY_DEBIT_DELETE,
- InvoiceLedgerService,
- )
- from app.services.bx.storage import upload_bx_file
- from app.services.web_auth_roles import iter_user_menu_perm_codes
- INVOICE_WALLET_ADMIN_PERM = "bx:invoiceWallet:admin"
- REIMBURSE_CHANGE_APPLICANT_PERM = "bx:reimburseApply:changeApplicant"
- class InvoiceService:
- @staticmethod
- def _tenant_no(db: Session, user: BxUserContext) -> str:
- return get_tenant_no(db, user.to_tenant_user_dict())
- @staticmethod
- def is_wallet_admin(db: Session, user: BxUserContext) -> bool:
- if user.role == SUPER_ADMIN_ROLE:
- return True
- codes = iter_user_menu_perm_codes(db, user.id, user.tenant_no)
- return INVOICE_WALLET_ADMIN_PERM in codes
- @staticmethod
- def can_query_others_wallet(db: Session, user: BxUserContext) -> bool:
- """发票夹管理 或 报销改选申请人 权限可查询他人发票/余额。"""
- if InvoiceService.is_wallet_admin(db, user):
- return True
- codes = iter_user_menu_perm_codes(db, user.id, user.tenant_no)
- return REIMBURSE_CHANGE_APPLICANT_PERM in set(codes)
- @staticmethod
- def _require_wallet_admin(db: Session, user: BxUserContext) -> None:
- if not InvoiceService.is_wallet_admin(db, user):
- raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="需要发票夹管理权限")
- @staticmethod
- def _lookup_user_name(db: Session, user_id: str) -> str:
- row = (
- db.query(SysUser)
- .filter(SysUser.id == user_id, SysUser.del_flag == 0)
- .first()
- )
- if not row:
- raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="目标用户不存在")
- return (row.nick_name or row.user_no or "").strip()
- @staticmethod
- def _resolve_owner(
- db: Session,
- user: BxUserContext,
- target_user_id: Optional[str],
- ) -> Tuple[str, str]:
- target = (target_user_id or "").strip()
- if not target or target == user.id:
- return user.id, user.name
- InvoiceService._require_wallet_admin(db, user)
- return target, InvoiceService._lookup_user_name(db, target)
- @staticmethod
- def _resolve_owner_for_query(
- db: Session,
- user: BxUserContext,
- user_id: Optional[str],
- ) -> str:
- owner_id = (user_id or "").strip() or user.id
- if owner_id != user.id and not InvoiceService.can_query_others_wallet(db, user):
- raise HTTPException(
- status_code=status.HTTP_403_FORBIDDEN,
- detail="无权查看他人发票夹",
- )
- return owner_id
- @staticmethod
- def _count_invoices(db: Session, tenant_no: str, owner_user_id: str) -> int:
- return (
- db.query(BxInvoice)
- .filter(
- BxInvoice.tenant_no == tenant_no,
- BxInvoice.owner_user_id == owner_user_id,
- BxInvoice.del_flag == 0,
- )
- .count()
- )
- @staticmethod
- def get_mine_summary(
- db: Session,
- *,
- user: BxUserContext,
- owner_user_id: Optional[str] = None,
- ) -> dict:
- tenant_no = InvoiceService._tenant_no(db, user)
- owner_id = InvoiceService._resolve_owner_for_query(db, user, owner_user_id)
- owner_name = (
- user.name
- if owner_id == user.id
- else InvoiceService._lookup_user_name(db, owner_id)
- )
- row = (
- db.query(BxInvoiceWalletSummary)
- .filter(
- BxInvoiceWalletSummary.tenant_no == tenant_no,
- BxInvoiceWalletSummary.owner_user_id == owner_id,
- BxInvoiceWalletSummary.del_flag == 0,
- )
- .first()
- )
- if row:
- return InvoiceService._summary_to_dict(row)
- totals = InvoiceLedgerService.calc_summary(db, tenant_no, owner_id)
- return {
- "id": "",
- "owner_user_id": owner_id,
- "owner_user_name": owner_name,
- "total_amount": totals["total_amount"],
- "used_amount": totals["used_amount"],
- "balance": totals["balance"],
- "invoice_count": InvoiceService._count_invoices(db, tenant_no, owner_id),
- "version": 0,
- "create_time": None,
- "update_time": None,
- }
- @staticmethod
- def list_wallets(
- db: Session,
- *,
- user: BxUserContext,
- page: int = 1,
- page_size: int = 10,
- keyword: str = "",
- ) -> Tuple[Sequence[BxInvoiceWalletSummary], int]:
- InvoiceService._require_wallet_admin(db, user)
- tenant_no = InvoiceService._tenant_no(db, user)
- query = db.query(BxInvoiceWalletSummary).filter(
- BxInvoiceWalletSummary.tenant_no == tenant_no,
- BxInvoiceWalletSummary.del_flag == 0,
- )
- kw = (keyword or "").strip()
- if kw:
- query = query.filter(
- or_(
- BxInvoiceWalletSummary.owner_user_name.contains(kw),
- BxInvoiceWalletSummary.owner_user_id.contains(kw),
- )
- )
- total = query.count()
- rows = (
- query.order_by(BxInvoiceWalletSummary.update_time.desc(), BxInvoiceWalletSummary.create_time.desc())
- .offset((page - 1) * page_size)
- .limit(page_size)
- .all()
- )
- return rows, total
- @staticmethod
- def list_invoices(
- db: Session,
- *,
- user: BxUserContext,
- owner_user_id: Optional[str] = None,
- page: int = 1,
- page_size: int = 10,
- ) -> Tuple[Sequence[BxInvoice], int]:
- tenant_no = InvoiceService._tenant_no(db, user)
- owner_id = InvoiceService._resolve_owner_for_query(db, user, owner_user_id)
- filters = [BxInvoice.owner_user_id == owner_id]
- rows, total = BxCrudService.get_paged(
- BxInvoice,
- db,
- tenant_no,
- page,
- page_size,
- filters=filters,
- user=user,
- )
- return rows, total
- @staticmethod
- def list_ledgers(
- db: Session,
- *,
- user: BxUserContext,
- owner_user_id: Optional[str] = None,
- ) -> List[BxInvoiceLedger]:
- tenant_no = InvoiceService._tenant_no(db, user)
- owner_id = InvoiceService._resolve_owner_for_query(db, user, owner_user_id)
- rows = InvoiceLedgerService.list_entries(db, tenant_no, owner_id)
- return list(reversed(rows))
- @staticmethod
- def create(
- db: Session,
- *,
- user: BxUserContext,
- amount: Decimal,
- files: Sequence[Tuple[bytes, str, str]],
- remark: Optional[str] = None,
- target_user_id: Optional[str] = None,
- ) -> BxInvoice:
- """一次上传创建一条发票档案:金额为总额,files 全部挂到同一 biz_id,只写一条 credit_upload。"""
- tenant_no = InvoiceService._tenant_no(db, user)
- owner_id, owner_name = InvoiceService._resolve_owner(db, user, target_user_id)
- amt = Decimal(str(amount))
- if amt <= 0:
- raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="金额必须大于0")
- if not files:
- raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="请选择文件")
- invoice = BxCrudService.create(
- BxInvoice,
- db,
- {
- "owner_user_id": owner_id,
- "owner_user_name": owner_name,
- "amount": amt,
- "upload_by": user.id,
- "upload_by_name": user.name,
- "remark": (remark or "").strip() or None,
- },
- tenant_no=tenant_no,
- user_id=user.id,
- dept_id=user.dept_id,
- )
- for file_data, filename, content_type in files:
- if not file_data:
- raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="文件内容为空")
- safe_name = filename or "unknown"
- mime = content_type or mimetypes.guess_type(safe_name)[0] or "application/octet-stream"
- relative_path, _full_url = upload_bx_file(file_data, safe_name, biz_type="invoice")
- AttachmentService.create(
- db,
- {
- "biz_type": "invoice",
- "biz_id": invoice.id,
- "file_name": safe_name,
- "file_path": relative_path,
- "file_type": mime,
- "file_size": len(file_data),
- "attach_type": "invoice",
- },
- user=user,
- )
- InvoiceLedgerService.add_entry(
- db,
- tenant_no=tenant_no,
- owner_user_id=owner_id,
- entry_type=ENTRY_CREDIT_UPLOAD,
- amount=amt,
- user=user,
- invoice_id=invoice.id,
- )
- invoice_count = InvoiceService._count_invoices(db, tenant_no, owner_id)
- InvoiceLedgerService.refresh_summary(
- db,
- tenant_no,
- owner_id,
- owner_name,
- invoice_count,
- user.id,
- user.dept_id,
- )
- return invoice
- @staticmethod
- def delete(db: Session, *, user: BxUserContext, invoice_id: str) -> bool:
- InvoiceService._require_wallet_admin(db, user)
- tenant_no = InvoiceService._tenant_no(db, user)
- invoice = BxCrudService.get_by_id(
- BxInvoice, db, invoice_id, tenant_no, user=user, skip_scope=True
- )
- if not invoice:
- raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="发票不存在")
- InvoiceLedgerService.assert_can_delete_invoice(
- db, tenant_no, invoice.owner_user_id, invoice.amount
- )
- InvoiceLedgerService.add_entry(
- db,
- tenant_no=tenant_no,
- owner_user_id=invoice.owner_user_id,
- entry_type=ENTRY_DEBIT_DELETE,
- amount=Decimal(str(invoice.amount)),
- user=user,
- invoice_id=invoice.id,
- )
- invoice.del_flag = 1
- invoice.update_by = user.id
- db.query(BxAttachment).filter(
- BxAttachment.biz_type == "invoice",
- BxAttachment.biz_id == invoice.id,
- BxAttachment.tenant_no == tenant_no,
- BxAttachment.del_flag == 0,
- ).update(
- {BxAttachment.del_flag: 1, BxAttachment.update_by: user.id},
- synchronize_session="fetch",
- )
- db.flush()
- invoice_count = InvoiceService._count_invoices(db, tenant_no, invoice.owner_user_id)
- InvoiceLedgerService.refresh_summary(
- db,
- tenant_no,
- invoice.owner_user_id,
- invoice.owner_user_name,
- invoice_count,
- user.id,
- user.dept_id,
- )
- return True
- @staticmethod
- def _summary_to_dict(row: BxInvoiceWalletSummary) -> dict:
- return {
- "id": row.id,
- "owner_user_id": row.owner_user_id,
- "owner_user_name": row.owner_user_name,
- "total_amount": row.total_amount,
- "used_amount": row.used_amount,
- "balance": row.balance,
- "invoice_count": row.invoice_count,
- "version": row.version,
- "create_time": row.create_time,
- "update_time": row.update_time,
- }
- @staticmethod
- def out_rows_with_attachments(
- db: Session, tenant_no: str, invoices: Sequence[BxInvoice]
- ) -> List[dict]:
- if not invoices:
- return []
- ids = [inv.id for inv in invoices]
- attachments = (
- db.query(BxAttachment)
- .filter(
- BxAttachment.biz_type == "invoice",
- BxAttachment.biz_id.in_(ids),
- BxAttachment.tenant_no == tenant_no,
- BxAttachment.del_flag == 0,
- )
- .order_by(BxAttachment.create_time.asc())
- .all()
- )
- att_map: dict[str, List[BxAttachment]] = {}
- for att in attachments:
- att_map.setdefault(att.biz_id, []).append(att)
- out: List[dict] = []
- for inv in invoices:
- atts = att_map.get(inv.id, [])
- first = atts[0] if atts else None
- out.append(
- {
- "id": inv.id,
- "owner_user_id": inv.owner_user_id,
- "owner_user_name": inv.owner_user_name,
- "amount": inv.amount,
- "upload_by": inv.upload_by,
- "upload_by_name": inv.upload_by_name,
- "remark": inv.remark,
- "create_time": inv.create_time,
- "update_time": inv.update_time,
- "attachment_id": first.id if first else None,
- "file_name": first.file_name if first else None,
- "file_path": first.file_path if first else None,
- "file_type": first.file_type if first else None,
- "attachments": [
- {
- "id": a.id,
- "file_name": a.file_name,
- "file_path": a.file_path,
- "file_type": a.file_type,
- }
- for a in atts
- ],
- }
- )
- return out
|