feat: 细化差旅票据费用明细分类并自动计算出差补贴
将差旅费用明细拆分为火车票、机票、住宿票、乘车等细分类 型,根据票据字段自动生成行程/事由描述,结合规则引擎自 动计算出差补贴金额,前端适配费用明细编辑和差旅票据审 核交互,补充单元测试覆盖。
This commit is contained in:
@@ -27,7 +27,12 @@ from app.models.employee import Employee
|
||||
from app.models.financial_record import ExpenseClaim, ExpenseClaimItem
|
||||
from app.models.organization import OrganizationUnit
|
||||
from app.schemas.ontology import OntologyEntity, OntologyParseResult
|
||||
from app.schemas.reimbursement import ExpenseClaimItemCreate, ExpenseClaimItemUpdate
|
||||
from app.schemas.reimbursement import (
|
||||
ExpenseClaimItemCreate,
|
||||
ExpenseClaimItemUpdate,
|
||||
ExpenseClaimUpdate,
|
||||
TravelReimbursementCalculatorRequest,
|
||||
)
|
||||
from app.services.agent_asset_rule_library import AgentAssetRuleLibraryManager
|
||||
from app.services.agent_asset_spreadsheet import RISK_RULES_LIBRARY
|
||||
from app.services.agent_foundation import AgentFoundationService
|
||||
@@ -42,10 +47,15 @@ from app.services.expense_rule_runtime import (
|
||||
)
|
||||
from app.services.ocr import OcrService
|
||||
|
||||
EXPENSE_TYPE_LABELS = {
|
||||
"travel": "差旅",
|
||||
"hotel": "住宿",
|
||||
"transport": "交通",
|
||||
EXPENSE_TYPE_LABELS = {
|
||||
"travel": "差旅",
|
||||
"train_ticket": "火车票",
|
||||
"flight_ticket": "机票",
|
||||
"hotel_ticket": "住宿票",
|
||||
"ride_ticket": "乘车",
|
||||
"travel_allowance": "出差补贴",
|
||||
"hotel": "住宿",
|
||||
"transport": "交通",
|
||||
"meal": "餐费",
|
||||
"meeting": "会务",
|
||||
"entertainment": "招待",
|
||||
@@ -60,8 +70,45 @@ APPROVAL_VISIBLE_CLAIM_ROLE_CODES = {"manager", "approver"}
|
||||
CLAIM_DELETE_ROLE_CODES = {"executive"}
|
||||
MAX_DRAFT_CLAIMS_PER_USER = 3
|
||||
EDITABLE_CLAIM_STATUSES = ("draft", "supplement", "returned")
|
||||
SYSTEM_GENERATED_ITEM_TYPES = {"travel_allowance"}
|
||||
TRAVEL_DETAIL_ITEM_TYPES = {
|
||||
"train_ticket",
|
||||
"flight_ticket",
|
||||
"hotel_ticket",
|
||||
"ride_ticket",
|
||||
"travel_allowance",
|
||||
}
|
||||
DOCUMENT_TYPE_ITEM_TYPE_MAP = {
|
||||
"train_ticket": "train_ticket",
|
||||
"flight_itinerary": "flight_ticket",
|
||||
"hotel_invoice": "hotel_ticket",
|
||||
"taxi_receipt": "ride_ticket",
|
||||
"transport_receipt": "ride_ticket",
|
||||
}
|
||||
DOCUMENT_FACT_ITEM_TYPES = {"train_ticket", "flight_ticket", "hotel_ticket", "ride_ticket"}
|
||||
DOCUMENT_ROUTE_TEXT_PATTERN = re.compile(
|
||||
r"([A-Za-z0-9\u4e00-\u9fa5()()·]{2,40})\s*(?:至|到|→|->|—|–|-)\s*"
|
||||
r"([A-Za-z0-9\u4e00-\u9fa5()()·]{2,40})"
|
||||
)
|
||||
DOCUMENT_ROUTE_ORIGIN_LABELS = {"起点", "上车", "上车地点", "上车地址", "出发", "出发地", "出发站", "始发站", "乘车起点"}
|
||||
DOCUMENT_ROUTE_DESTINATION_LABELS = {
|
||||
"终点",
|
||||
"下车",
|
||||
"下车地点",
|
||||
"下车地址",
|
||||
"到达",
|
||||
"到达地",
|
||||
"到达站",
|
||||
"目的地",
|
||||
"乘车终点",
|
||||
}
|
||||
GENERIC_ATTACHMENT_BACKFILL_ITEM_TYPES = {"", "other", "travel", "transport", "hotel"}
|
||||
LOCATION_REQUIRED_EXPENSE_TYPES = {
|
||||
"travel",
|
||||
"train_ticket",
|
||||
"flight_ticket",
|
||||
"hotel_ticket",
|
||||
"ride_ticket",
|
||||
"meeting",
|
||||
"entertainment",
|
||||
}
|
||||
@@ -109,9 +156,14 @@ EXPENSE_SCENE_KEYWORDS = {
|
||||
"training": ("培训", "课程", "讲师", "教材", "学费", "认证"),
|
||||
}
|
||||
|
||||
EXPENSE_TYPE_ALLOWED_DOCUMENT_SCENES = {
|
||||
"travel": {"travel", "hotel", "transport", "meal"},
|
||||
"hotel": {"hotel"},
|
||||
EXPENSE_TYPE_ALLOWED_DOCUMENT_SCENES = {
|
||||
"travel": {"travel", "hotel", "transport", "meal"},
|
||||
"train_ticket": {"travel"},
|
||||
"flight_ticket": {"travel"},
|
||||
"hotel_ticket": {"hotel"},
|
||||
"ride_ticket": {"transport"},
|
||||
"travel_allowance": set(),
|
||||
"hotel": {"hotel"},
|
||||
"transport": {"transport", "travel"},
|
||||
"meal": {"meal", "entertainment"},
|
||||
"entertainment": {"entertainment", "meal"},
|
||||
@@ -343,23 +395,55 @@ class ExpenseClaimService:
|
||||
)
|
||||
stmt = self._apply_claim_scope(stmt, current_user, include_approval_scope=True)
|
||||
return self.db.scalar(stmt)
|
||||
|
||||
def update_claim_item(
|
||||
self,
|
||||
*,
|
||||
|
||||
def update_claim(
|
||||
self,
|
||||
*,
|
||||
claim_id: str,
|
||||
payload: ExpenseClaimUpdate,
|
||||
current_user: CurrentUserContext,
|
||||
) -> ExpenseClaim | None:
|
||||
claim = self.get_claim(claim_id, current_user)
|
||||
if claim is None:
|
||||
return None
|
||||
|
||||
self._ensure_draft_pending_claim(claim)
|
||||
before_json = self._serialize_claim(claim)
|
||||
|
||||
if payload.reason is not None:
|
||||
claim.reason = self._normalize_optional_text(payload.reason, allow_empty=True) or "待补充"
|
||||
|
||||
self.db.commit()
|
||||
self.db.refresh(claim)
|
||||
|
||||
self.audit_service.log_action(
|
||||
actor=current_user.name or current_user.username,
|
||||
action="expense_claim.update",
|
||||
resource_type="expense_claim",
|
||||
resource_id=claim.id,
|
||||
before_json=before_json,
|
||||
after_json=self._serialize_claim(claim),
|
||||
)
|
||||
|
||||
return claim
|
||||
|
||||
def update_claim_item(
|
||||
self,
|
||||
*,
|
||||
claim_id: str,
|
||||
item_id: str,
|
||||
payload: ExpenseClaimItemUpdate,
|
||||
current_user: CurrentUserContext,
|
||||
) -> ExpenseClaim | None:
|
||||
claim = self.get_claim(claim_id, current_user)
|
||||
if claim is None:
|
||||
return None
|
||||
|
||||
self._ensure_draft_claim(claim)
|
||||
item = next((entry for entry in claim.items if entry.id == item_id), None)
|
||||
if item is None:
|
||||
raise LookupError("Item not found")
|
||||
if claim is None:
|
||||
return None
|
||||
|
||||
self._ensure_draft_claim(claim)
|
||||
item = next((entry for entry in claim.items if entry.id == item_id), None)
|
||||
if item is None:
|
||||
raise LookupError("Item not found")
|
||||
self._ensure_mutable_claim_item(item)
|
||||
|
||||
before_json = self._serialize_claim(claim)
|
||||
|
||||
@@ -407,12 +491,12 @@ class ExpenseClaimService:
|
||||
current_user: CurrentUserContext,
|
||||
) -> ExpenseClaim | None:
|
||||
claim = self.get_claim(claim_id, current_user)
|
||||
if claim is None:
|
||||
return None
|
||||
|
||||
self._ensure_draft_claim(claim)
|
||||
before_json = self._serialize_claim(claim)
|
||||
payload = payload or ExpenseClaimItemCreate()
|
||||
if claim is None:
|
||||
return None
|
||||
|
||||
self._ensure_draft_claim(claim)
|
||||
before_json = self._serialize_claim(claim)
|
||||
payload = payload or ExpenseClaimItemCreate()
|
||||
|
||||
occurred_at = claim.occurred_at if claim.occurred_at is not None else datetime.now(UTC)
|
||||
item_amount = Decimal("0.00")
|
||||
@@ -509,11 +593,12 @@ class ExpenseClaimService:
|
||||
item_id=item_id,
|
||||
current_user=current_user,
|
||||
)
|
||||
if claim is None:
|
||||
return None
|
||||
|
||||
self._ensure_draft_claim(claim)
|
||||
normalized_name = self._normalize_attachment_filename(filename)
|
||||
if claim is None:
|
||||
return None
|
||||
|
||||
self._ensure_draft_claim(claim)
|
||||
self._ensure_mutable_claim_item(item)
|
||||
normalized_name = self._normalize_attachment_filename(filename)
|
||||
if not content:
|
||||
raise ValueError("上传文件不能为空。")
|
||||
|
||||
@@ -547,11 +632,20 @@ class ExpenseClaimService:
|
||||
ocr_document = documents[0]
|
||||
ocr_status = "recognized"
|
||||
document_info = self._build_attachment_document_info(ocr_document)
|
||||
self._backfill_item_type_from_attachment(
|
||||
item=item,
|
||||
document_info=document_info,
|
||||
)
|
||||
self._backfill_item_amount_from_attachment(
|
||||
item=item,
|
||||
document=ocr_document,
|
||||
document_info=document_info,
|
||||
)
|
||||
self._backfill_item_reason_from_attachment(
|
||||
item=item,
|
||||
document=ocr_document,
|
||||
document_info=document_info,
|
||||
)
|
||||
requirement_check = self._build_attachment_requirement_check(
|
||||
item=item,
|
||||
document_info=document_info,
|
||||
@@ -694,11 +788,12 @@ class ExpenseClaimService:
|
||||
item_id=item_id,
|
||||
current_user=current_user,
|
||||
)
|
||||
if claim is None:
|
||||
return None
|
||||
|
||||
self._ensure_draft_claim(claim)
|
||||
before_json = self._serialize_claim(claim)
|
||||
if claim is None:
|
||||
return None
|
||||
|
||||
self._ensure_draft_claim(claim)
|
||||
self._ensure_mutable_claim_item(item)
|
||||
before_json = self._serialize_claim(claim)
|
||||
previous_name = self._resolve_attachment_display_name(item.invoice_id)
|
||||
self._delete_item_attachment_files(item)
|
||||
item.invoice_id = None
|
||||
@@ -1234,15 +1329,18 @@ class ExpenseClaimService:
|
||||
|
||||
self.db.flush()
|
||||
if context_documents or attachment_names:
|
||||
document_specs = self._build_context_item_specs(
|
||||
context_documents=context_documents,
|
||||
attachment_names=attachment_names,
|
||||
occurred_at=final_occurred_at,
|
||||
expense_type=final_expense_type,
|
||||
amount=final_amount,
|
||||
reason=final_reason,
|
||||
location=final_location,
|
||||
)
|
||||
document_specs = self._build_context_item_specs(
|
||||
context_documents=context_documents,
|
||||
attachment_names=attachment_names,
|
||||
occurred_at=final_occurred_at,
|
||||
expense_type=final_expense_type,
|
||||
amount=final_amount,
|
||||
reason=final_reason,
|
||||
location=final_location,
|
||||
context_json=context_json,
|
||||
employee_grade=str(employee.grade or "").strip() if employee is not None else "",
|
||||
user_id=user_id,
|
||||
)
|
||||
else:
|
||||
document_specs = []
|
||||
|
||||
@@ -1486,28 +1584,31 @@ class ExpenseClaimService:
|
||||
)
|
||||
return normalized
|
||||
|
||||
def _build_context_item_specs(
|
||||
self,
|
||||
*,
|
||||
context_documents: list[dict[str, Any]],
|
||||
attachment_names: list[str],
|
||||
occurred_at: datetime,
|
||||
expense_type: str,
|
||||
amount: Decimal,
|
||||
reason: str,
|
||||
location: str,
|
||||
) -> list[dict[str, Any]]:
|
||||
specs: list[dict[str, Any]] = []
|
||||
if context_documents:
|
||||
for document in context_documents:
|
||||
def _build_context_item_specs(
|
||||
self,
|
||||
*,
|
||||
context_documents: list[dict[str, Any]],
|
||||
attachment_names: list[str],
|
||||
occurred_at: datetime,
|
||||
expense_type: str,
|
||||
amount: Decimal,
|
||||
reason: str,
|
||||
location: str,
|
||||
context_json: dict[str, Any],
|
||||
employee_grade: str | None = None,
|
||||
user_id: str = "",
|
||||
) -> list[dict[str, Any]]:
|
||||
specs: list[dict[str, Any]] = []
|
||||
if context_documents:
|
||||
for document in context_documents:
|
||||
specs.append(
|
||||
{
|
||||
"item_date": self._resolve_document_item_date(document, fallback=occurred_at.date()),
|
||||
"item_type": self._resolve_document_item_type(document, fallback=expense_type),
|
||||
"item_reason": reason,
|
||||
"item_location": location,
|
||||
"item_amount": self._resolve_document_item_amount(document),
|
||||
"invoice_id": str(document.get("filename") or "").strip() or None,
|
||||
"item_date": self._resolve_document_item_date(document, fallback=occurred_at.date()),
|
||||
"item_type": self._resolve_document_item_type(document, fallback=expense_type),
|
||||
"item_reason": self._resolve_document_item_reason(document, fallback=reason),
|
||||
"item_location": location,
|
||||
"item_amount": self._resolve_document_item_amount(document),
|
||||
"invoice_id": str(document.get("filename") or "").strip() or None,
|
||||
}
|
||||
)
|
||||
elif attachment_names:
|
||||
@@ -1535,13 +1636,191 @@ class ExpenseClaimService:
|
||||
if remaining > Decimal("0.00"):
|
||||
missing_specs[0]["item_amount"] = remaining
|
||||
|
||||
for spec in specs:
|
||||
if spec.get("item_amount") is None:
|
||||
spec["item_amount"] = Decimal("0.00")
|
||||
|
||||
return specs
|
||||
|
||||
def _replace_claim_items(
|
||||
for spec in specs:
|
||||
if spec.get("item_amount") is None:
|
||||
spec["item_amount"] = Decimal("0.00")
|
||||
|
||||
allowance_spec = self._build_travel_allowance_item_spec(
|
||||
context_documents=context_documents,
|
||||
specs=specs,
|
||||
occurred_at=occurred_at,
|
||||
expense_type=expense_type,
|
||||
location=location,
|
||||
context_json=context_json,
|
||||
employee_grade=employee_grade,
|
||||
user_id=user_id,
|
||||
)
|
||||
if allowance_spec is not None:
|
||||
specs = [spec for spec in specs if str(spec.get("item_type") or "").strip() != "travel_allowance"]
|
||||
specs.append(allowance_spec)
|
||||
|
||||
return specs
|
||||
|
||||
def _build_travel_allowance_item_spec(
|
||||
self,
|
||||
*,
|
||||
context_documents: list[dict[str, Any]],
|
||||
specs: list[dict[str, Any]],
|
||||
occurred_at: datetime,
|
||||
expense_type: str,
|
||||
location: str,
|
||||
context_json: dict[str, Any],
|
||||
employee_grade: str | None,
|
||||
user_id: str,
|
||||
) -> dict[str, Any] | None:
|
||||
if not self._should_add_travel_allowance_item(
|
||||
expense_type=expense_type,
|
||||
context_documents=context_documents,
|
||||
context_json=context_json,
|
||||
):
|
||||
return None
|
||||
|
||||
grade = str(employee_grade or context_json.get("grade") or "").strip()
|
||||
if not grade:
|
||||
return None
|
||||
|
||||
days, _, end_date = self._resolve_travel_allowance_days(
|
||||
context_json=context_json,
|
||||
occurred_at=occurred_at,
|
||||
)
|
||||
allowance_location = self._resolve_travel_allowance_location(
|
||||
location=location,
|
||||
context_documents=context_documents,
|
||||
)
|
||||
if days < 1 or not allowance_location:
|
||||
return None
|
||||
|
||||
try:
|
||||
from app.services.travel_reimbursement_calculator import TravelReimbursementCalculatorService
|
||||
|
||||
result = TravelReimbursementCalculatorService(self.db).calculate(
|
||||
TravelReimbursementCalculatorRequest(
|
||||
days=days,
|
||||
location=allowance_location,
|
||||
grade=grade,
|
||||
),
|
||||
CurrentUserContext(
|
||||
username=user_id,
|
||||
name="",
|
||||
role_codes=[],
|
||||
is_admin=False,
|
||||
),
|
||||
)
|
||||
except ValueError:
|
||||
return None
|
||||
|
||||
allowance_amount = Decimal(result.allowance_amount or Decimal("0.00")).quantize(Decimal("0.01"))
|
||||
allowance_rate = Decimal(result.total_allowance_rate or Decimal("0.00")).quantize(Decimal("0.01"))
|
||||
if allowance_amount <= Decimal("0.00") or allowance_rate <= Decimal("0.00"):
|
||||
return None
|
||||
|
||||
return {
|
||||
"item_date": end_date,
|
||||
"item_type": "travel_allowance",
|
||||
"item_reason": (
|
||||
f"系统自动计算出差补贴:{result.matched_city},{days}天,"
|
||||
f"{allowance_rate:.2f}元/天"
|
||||
),
|
||||
"item_location": str(result.allowance_region or allowance_location).strip(),
|
||||
"item_amount": allowance_amount,
|
||||
"invoice_id": None,
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def _should_add_travel_allowance_item(
|
||||
*,
|
||||
expense_type: str,
|
||||
context_documents: list[dict[str, Any]],
|
||||
context_json: dict[str, Any],
|
||||
) -> bool:
|
||||
normalized_expense_type = str(expense_type or "").strip().lower()
|
||||
if normalized_expense_type == "travel":
|
||||
return True
|
||||
|
||||
review_form_values = context_json.get("review_form_values")
|
||||
if isinstance(review_form_values, dict):
|
||||
review_type = str(
|
||||
review_form_values.get("expense_type")
|
||||
or review_form_values.get("scene_label")
|
||||
or review_form_values.get("reason_value")
|
||||
or ""
|
||||
)
|
||||
if any(keyword in review_type for keyword in ("差旅", "出差")):
|
||||
return True
|
||||
|
||||
for document in context_documents:
|
||||
document_type = str(document.get("document_type") or "").strip()
|
||||
scene_code = str(document.get("scene_code") or "").strip()
|
||||
if document_type in {"train_ticket", "flight_itinerary", "hotel_invoice"} or scene_code == "travel":
|
||||
return True
|
||||
return False
|
||||
|
||||
def _resolve_travel_allowance_days(
|
||||
self,
|
||||
*,
|
||||
context_json: dict[str, Any],
|
||||
occurred_at: datetime,
|
||||
) -> tuple[int, date, date]:
|
||||
start_date = occurred_at.date()
|
||||
end_date = start_date
|
||||
|
||||
business_time_context = context_json.get("business_time_context")
|
||||
if isinstance(business_time_context, dict):
|
||||
start_date = self._parse_iso_date_or_default(business_time_context.get("start_date"), start_date)
|
||||
end_date = self._parse_iso_date_or_default(business_time_context.get("end_date"), start_date)
|
||||
else:
|
||||
review_form_values = context_json.get("review_form_values")
|
||||
if isinstance(review_form_values, dict):
|
||||
time_text = str(
|
||||
review_form_values.get("time_range")
|
||||
or review_form_values.get("business_time")
|
||||
or review_form_values.get("occurred_date")
|
||||
or ""
|
||||
).strip()
|
||||
matched_dates = re.findall(r"\d{4}-\d{2}-\d{2}", time_text)
|
||||
if matched_dates:
|
||||
start_date = self._parse_iso_date_or_default(matched_dates[0], start_date)
|
||||
end_date = self._parse_iso_date_or_default(matched_dates[-1], start_date)
|
||||
|
||||
if end_date < start_date:
|
||||
end_date = start_date
|
||||
days = (end_date - start_date).days + 1
|
||||
return max(1, days), start_date, end_date
|
||||
|
||||
@staticmethod
|
||||
def _parse_iso_date_or_default(value: Any, fallback: date) -> date:
|
||||
try:
|
||||
return date.fromisoformat(str(value or "").strip())
|
||||
except ValueError:
|
||||
return fallback
|
||||
|
||||
@staticmethod
|
||||
def _resolve_travel_allowance_location(
|
||||
*,
|
||||
location: str,
|
||||
context_documents: list[dict[str, Any]],
|
||||
) -> str:
|
||||
normalized_location = str(location or "").strip()
|
||||
if normalized_location and normalized_location not in {"待补充", "未知", "暂无"}:
|
||||
return normalized_location
|
||||
|
||||
for document in context_documents:
|
||||
for field in list(document.get("document_fields") or []):
|
||||
if not isinstance(field, dict):
|
||||
continue
|
||||
key = str(field.get("key") or "").strip().lower()
|
||||
label = str(field.get("label") or "").strip()
|
||||
value = str(field.get("value") or "").strip()
|
||||
if key == "route" or "行程" in label:
|
||||
separators = ("-", "至", "→", "->")
|
||||
for separator in separators:
|
||||
if separator in value:
|
||||
return value.split(separator)[-1].strip()
|
||||
if key in {"destination", "arrival_city"} or label in {"目的地", "到达城市"}:
|
||||
return value
|
||||
return ""
|
||||
|
||||
def _replace_claim_items(
|
||||
self,
|
||||
*,
|
||||
claim: ExpenseClaim,
|
||||
@@ -1565,18 +1844,28 @@ class ExpenseClaimService:
|
||||
item.item_reason = spec["item_reason"]
|
||||
item.item_location = spec["item_location"]
|
||||
item.item_amount = spec["item_amount"]
|
||||
item.invoice_id = self._merge_attachment_reference(item.invoice_id, spec["invoice_id"])
|
||||
item.invoice_id = (
|
||||
None
|
||||
if str(spec.get("item_type") or "").strip() in SYSTEM_GENERATED_ITEM_TYPES
|
||||
else self._merge_attachment_reference(item.invoice_id, spec["invoice_id"])
|
||||
)
|
||||
|
||||
for stale_item in existing_items[len(item_specs) :]:
|
||||
claim.items.remove(stale_item)
|
||||
self.db.delete(stale_item)
|
||||
|
||||
def _append_document_items(
|
||||
self,
|
||||
*,
|
||||
claim: ExpenseClaim,
|
||||
item_specs: list[dict[str, Any]],
|
||||
) -> None:
|
||||
def _append_document_items(
|
||||
self,
|
||||
*,
|
||||
claim: ExpenseClaim,
|
||||
item_specs: list[dict[str, Any]],
|
||||
) -> None:
|
||||
system_specs = [
|
||||
spec for spec in item_specs if str(spec.get("item_type") or "").strip() in SYSTEM_GENERATED_ITEM_TYPES
|
||||
]
|
||||
normal_specs = [
|
||||
spec for spec in item_specs if str(spec.get("item_type") or "").strip() not in SYSTEM_GENERATED_ITEM_TYPES
|
||||
]
|
||||
existing_invoice_ids = {
|
||||
str(item.invoice_id or "").strip()
|
||||
for item in claim.items
|
||||
@@ -1587,7 +1876,7 @@ class ExpenseClaimService:
|
||||
for item in claim.items
|
||||
if str(item.invoice_id or "").strip()
|
||||
}
|
||||
for spec in item_specs:
|
||||
for spec in normal_specs:
|
||||
invoice_id = str(spec.get("invoice_id") or "").strip()
|
||||
invoice_name = self._resolve_attachment_display_name(invoice_id)
|
||||
if invoice_id and (invoice_id in existing_invoice_ids or invoice_name in existing_invoice_names):
|
||||
@@ -1607,15 +1896,40 @@ class ExpenseClaimService:
|
||||
if invoice_id:
|
||||
existing_invoice_ids.add(invoice_id)
|
||||
existing_invoice_names.add(invoice_name)
|
||||
|
||||
if system_specs:
|
||||
existing_system_items = [
|
||||
item for item in list(claim.items) if str(item.item_type or "").strip() in SYSTEM_GENERATED_ITEM_TYPES
|
||||
]
|
||||
for stale_item in existing_system_items:
|
||||
claim.items.remove(stale_item)
|
||||
self.db.delete(stale_item)
|
||||
for spec in system_specs:
|
||||
claim.items.append(
|
||||
ExpenseClaimItem(
|
||||
claim_id=claim.id,
|
||||
item_date=spec["item_date"],
|
||||
item_type=spec["item_type"],
|
||||
item_reason=spec["item_reason"],
|
||||
item_location=spec["item_location"],
|
||||
item_amount=spec["item_amount"],
|
||||
invoice_id=spec["invoice_id"],
|
||||
)
|
||||
)
|
||||
self.db.add(claim.items[-1])
|
||||
|
||||
def _resolve_document_item_type(self, document: dict[str, Any], *, fallback: str) -> str:
|
||||
scene_code = str(document.get("scene_code") or "").strip()
|
||||
if scene_code in {"travel", "hotel", "transport", "meal", "office", "meeting", "training"}:
|
||||
return scene_code
|
||||
|
||||
document_type = str(document.get("document_type") or "").strip()
|
||||
if document_type in {"flight_itinerary", "train_ticket"}:
|
||||
return "travel"
|
||||
def _resolve_document_item_type(self, document: dict[str, Any], *, fallback: str) -> str:
|
||||
document_type = str(document.get("document_type") or "").strip()
|
||||
mapped_type = DOCUMENT_TYPE_ITEM_TYPE_MAP.get(document_type)
|
||||
if mapped_type:
|
||||
return mapped_type
|
||||
|
||||
scene_code = str(document.get("scene_code") or "").strip()
|
||||
if scene_code in {"travel", "hotel", "transport", "meal", "office", "meeting", "training"}:
|
||||
return scene_code
|
||||
|
||||
if document_type in {"flight_itinerary", "train_ticket"}:
|
||||
return "travel"
|
||||
if document_type in {"taxi_receipt", "parking_toll_receipt", "transport_receipt"}:
|
||||
return "transport"
|
||||
if document_type == "hotel_invoice":
|
||||
@@ -1639,12 +1953,212 @@ class ExpenseClaimService:
|
||||
if "会务" in scene_label or "会议" in scene_label:
|
||||
return "meeting"
|
||||
if "培训" in scene_label:
|
||||
return "training"
|
||||
return fallback or "other"
|
||||
|
||||
def _resolve_document_item_amount(self, document: dict[str, Any]) -> Decimal | None:
|
||||
for field in list(document.get("document_fields") or []):
|
||||
if not isinstance(field, dict):
|
||||
return "training"
|
||||
return fallback or "other"
|
||||
|
||||
def _resolve_document_item_reason(self, document: dict[str, Any], *, fallback: str) -> str:
|
||||
document_type = str(document.get("document_type") or "").strip().lower()
|
||||
item_type = self._resolve_document_item_type(document, fallback="")
|
||||
|
||||
if document_type in {"train_ticket", "flight_itinerary"} or item_type in {"train_ticket", "flight_ticket"}:
|
||||
route = self._resolve_document_route_value(document)
|
||||
trip_no = self._resolve_document_fact_field(
|
||||
document,
|
||||
keys={"trip_no", "flight_no", "train_no"},
|
||||
labels={"车次", "航班"},
|
||||
)
|
||||
if route and trip_no:
|
||||
return f"{self._format_document_route(route)}({trip_no})"
|
||||
if route:
|
||||
return self._format_document_route(route)
|
||||
|
||||
if document_type in {"taxi_receipt", "transport_receipt"} or item_type == "ride_ticket":
|
||||
route = self._resolve_document_route_value(document)
|
||||
if route:
|
||||
return self._format_document_route(route)
|
||||
|
||||
if document_type == "hotel_invoice" or item_type == "hotel_ticket":
|
||||
merchant = self._resolve_document_fact_field(
|
||||
document,
|
||||
keys={"merchant_name", "merchant", "seller_name", "vendor_name", "hotel_name"},
|
||||
labels={"商户", "酒店", "宾馆", "销售方", "开票方"},
|
||||
)
|
||||
stay_range = self._resolve_document_stay_range(document)
|
||||
if merchant and stay_range:
|
||||
return f"{merchant},{stay_range}"
|
||||
if merchant:
|
||||
return merchant
|
||||
if stay_range:
|
||||
return stay_range
|
||||
|
||||
merchant = self._resolve_document_fact_field(
|
||||
document,
|
||||
keys={"merchant_name", "merchant", "seller_name", "vendor_name"},
|
||||
labels={"商户", "销售方", "开票方", "收款方"},
|
||||
)
|
||||
if merchant:
|
||||
return merchant
|
||||
|
||||
summary = str(document.get("summary") or "").strip()
|
||||
return summary or fallback or ""
|
||||
|
||||
def _resolve_document_route_value(self, document: dict[str, Any]) -> str:
|
||||
route = self._resolve_document_fact_field(
|
||||
document,
|
||||
keys={"route", "trip_route"},
|
||||
labels={"行程", "路线"},
|
||||
)
|
||||
if route:
|
||||
return route
|
||||
|
||||
origin = self._resolve_document_fact_field(
|
||||
document,
|
||||
keys={
|
||||
"origin",
|
||||
"from",
|
||||
"from_city",
|
||||
"departure",
|
||||
"departure_city",
|
||||
"start",
|
||||
"start_location",
|
||||
"start_address",
|
||||
"pickup_location",
|
||||
"pickup_address",
|
||||
"boarding_station",
|
||||
},
|
||||
labels=DOCUMENT_ROUTE_ORIGIN_LABELS,
|
||||
)
|
||||
destination = self._resolve_document_fact_field(
|
||||
document,
|
||||
keys={
|
||||
"destination",
|
||||
"to",
|
||||
"to_city",
|
||||
"arrival",
|
||||
"arrival_city",
|
||||
"end",
|
||||
"end_location",
|
||||
"end_address",
|
||||
"dropoff_location",
|
||||
"dropoff_address",
|
||||
"alighting_station",
|
||||
},
|
||||
labels=DOCUMENT_ROUTE_DESTINATION_LABELS,
|
||||
)
|
||||
if origin and destination:
|
||||
return f"{origin}-{destination}"
|
||||
|
||||
text = " ".join(
|
||||
[
|
||||
str(document.get("summary") or "").strip(),
|
||||
str(document.get("text") or "").strip(),
|
||||
]
|
||||
).strip()
|
||||
text_route = self._extract_document_route_from_text(text)
|
||||
if text_route:
|
||||
return text_route
|
||||
|
||||
text_origin = self._extract_document_labeled_text_value(text, DOCUMENT_ROUTE_ORIGIN_LABELS)
|
||||
text_destination = self._extract_document_labeled_text_value(text, DOCUMENT_ROUTE_DESTINATION_LABELS)
|
||||
if text_origin and text_destination:
|
||||
return f"{text_origin}-{text_destination}"
|
||||
return ""
|
||||
|
||||
@staticmethod
|
||||
def _resolve_document_fact_field(
|
||||
document: dict[str, Any],
|
||||
*,
|
||||
keys: set[str],
|
||||
labels: set[str],
|
||||
) -> str:
|
||||
raw_fields = document.get("document_fields")
|
||||
if not isinstance(raw_fields, list):
|
||||
raw_fields = document.get("fields")
|
||||
if not isinstance(raw_fields, list):
|
||||
return ""
|
||||
|
||||
normalized_keys = {str(key or "").strip().lower().replace("_", "") for key in keys}
|
||||
for field in raw_fields:
|
||||
if not isinstance(field, dict):
|
||||
continue
|
||||
field_key = str(field.get("key") or "").strip().lower().replace("_", "")
|
||||
label = str(field.get("label") or "").replace(" ", "")
|
||||
value = str(field.get("value") or "").strip()
|
||||
if not value:
|
||||
continue
|
||||
if field_key in normalized_keys or any(token in label for token in labels):
|
||||
return value
|
||||
return ""
|
||||
|
||||
@staticmethod
|
||||
def _format_document_route(route: str) -> str:
|
||||
normalized = (
|
||||
str(route or "")
|
||||
.strip()
|
||||
.replace("->", "-")
|
||||
.replace("→", "-")
|
||||
.replace("—", "-")
|
||||
.replace("–", "-")
|
||||
.replace("至", "-")
|
||||
.replace("到", "-")
|
||||
)
|
||||
if "-" not in normalized:
|
||||
return str(route or "").strip()
|
||||
origin, destination = [part.strip() for part in normalized.split("-", 1)]
|
||||
origin = origin.removeprefix("从").strip()
|
||||
destination = destination.removeprefix("至").removeprefix("到").strip()
|
||||
if not origin or not destination or origin == destination:
|
||||
return str(route or "").strip()
|
||||
return f"从{origin}到{destination}"
|
||||
|
||||
@staticmethod
|
||||
def _extract_document_route_from_text(text: str) -> str:
|
||||
match = DOCUMENT_ROUTE_TEXT_PATTERN.search(str(text or ""))
|
||||
if not match:
|
||||
return ""
|
||||
origin = str(match.group(1) or "").strip()
|
||||
destination = str(match.group(2) or "").strip()
|
||||
if not origin or not destination or origin == destination:
|
||||
return ""
|
||||
return f"{origin}-{destination}"
|
||||
|
||||
@staticmethod
|
||||
def _extract_document_labeled_text_value(text: str, labels: set[str]) -> str:
|
||||
for label in sorted(labels, key=len, reverse=True):
|
||||
pattern = re.compile(
|
||||
rf"{re.escape(label)}[::\s]*"
|
||||
r"([A-Za-z0-9\u4e00-\u9fa5()()·\-路街道号弄区县市省园桥站机场中心]{2,50})"
|
||||
)
|
||||
match = pattern.search(str(text or ""))
|
||||
if match:
|
||||
return str(match.group(1) or "").strip()
|
||||
return ""
|
||||
|
||||
def _resolve_document_stay_range(self, document: dict[str, Any]) -> str:
|
||||
check_in = self._resolve_document_fact_field(
|
||||
document,
|
||||
keys={"check_in", "checkin", "arrival_date", "start_date"},
|
||||
labels={"入住", "入住日期", "到店", "开始日期"},
|
||||
)
|
||||
check_out = self._resolve_document_fact_field(
|
||||
document,
|
||||
keys={"check_out", "checkout", "departure_date", "end_date"},
|
||||
labels={"离店", "退房", "离店日期", "结束日期"},
|
||||
)
|
||||
if check_in and check_out:
|
||||
return f"{check_in}至{check_out}"
|
||||
nights = self._resolve_document_fact_field(
|
||||
document,
|
||||
keys={"nights", "night_count", "room_nights"},
|
||||
labels={"间夜", "晚数", "入住天数"},
|
||||
)
|
||||
if nights:
|
||||
return f"{nights}晚"
|
||||
return ""
|
||||
|
||||
def _resolve_document_item_amount(self, document: dict[str, Any]) -> Decimal | None:
|
||||
for field in list(document.get("document_fields") or []):
|
||||
if not isinstance(field, dict):
|
||||
continue
|
||||
key = str(field.get("key") or "").strip().lower().replace("_", "")
|
||||
label = str(field.get("label") or "").replace(" ", "")
|
||||
@@ -2575,6 +3089,21 @@ class ExpenseClaimService:
|
||||
"fields": normalized_fields,
|
||||
}
|
||||
|
||||
def _backfill_item_type_from_attachment(
|
||||
self,
|
||||
*,
|
||||
item: ExpenseClaimItem,
|
||||
document_info: dict[str, Any],
|
||||
) -> None:
|
||||
current_type = str(item.item_type or "").strip().lower()
|
||||
if current_type not in GENERIC_ATTACHMENT_BACKFILL_ITEM_TYPES:
|
||||
return
|
||||
|
||||
document_type = str(document_info.get("document_type") or "").strip()
|
||||
mapped_type = DOCUMENT_TYPE_ITEM_TYPE_MAP.get(document_type)
|
||||
if mapped_type:
|
||||
item.item_type = mapped_type
|
||||
|
||||
def _backfill_item_amount_from_attachment(
|
||||
self,
|
||||
*,
|
||||
@@ -2596,6 +3125,27 @@ class ExpenseClaimService:
|
||||
if amount is not None and amount > Decimal("0.00"):
|
||||
item.item_amount = amount
|
||||
|
||||
def _backfill_item_reason_from_attachment(
|
||||
self,
|
||||
*,
|
||||
item: ExpenseClaimItem,
|
||||
document: Any,
|
||||
document_info: dict[str, Any],
|
||||
) -> None:
|
||||
reason = self._resolve_document_item_reason(
|
||||
{
|
||||
"document_type": str(document_info.get("document_type") or "").strip(),
|
||||
"scene_code": str(document_info.get("scene_code") or "").strip(),
|
||||
"scene_label": str(document_info.get("scene_label") or "").strip(),
|
||||
"document_fields": document_info.get("fields") or [],
|
||||
"summary": str(getattr(document, "summary", "") or ""),
|
||||
"text": str(getattr(document, "text", "") or ""),
|
||||
},
|
||||
fallback=str(item.item_reason or "").strip(),
|
||||
)
|
||||
if reason:
|
||||
item.item_reason = reason
|
||||
|
||||
def _build_attachment_requirement_check(
|
||||
self,
|
||||
*,
|
||||
@@ -3063,6 +3613,17 @@ class ExpenseClaimService:
|
||||
if not self._is_editable_claim_status(claim.status):
|
||||
raise ValueError("只有草稿、待补充或退回待提交状态的报销单才允许执行该操作。")
|
||||
|
||||
@staticmethod
|
||||
def _ensure_draft_pending_claim(claim: ExpenseClaim) -> None:
|
||||
status = str(claim.status or "").strip().lower()
|
||||
if status != "draft":
|
||||
raise ValueError("只有草稿待提交状态的报销单才允许编辑附加说明。")
|
||||
|
||||
@staticmethod
|
||||
def _ensure_mutable_claim_item(item: ExpenseClaimItem) -> None:
|
||||
if str(item.item_type or "").strip().lower() in SYSTEM_GENERATED_ITEM_TYPES:
|
||||
raise ValueError("系统自动计算的费用明细不可手动修改。")
|
||||
|
||||
def _delete_submitted_claim_assistant_sessions(self, claim_id: str | None) -> None:
|
||||
from app.services.agent_conversations import AgentConversationService
|
||||
|
||||
@@ -4531,10 +5092,16 @@ class ExpenseClaimService:
|
||||
primary_item.item_date.day,
|
||||
tzinfo=UTC,
|
||||
)
|
||||
claim.expense_type = str(primary_item.item_type or claim.expense_type or "other").strip() or "other"
|
||||
claim.reason = (
|
||||
self._normalize_optional_text(primary_item.item_reason, fallback=claim.reason or "待补充") or "待补充"
|
||||
)
|
||||
claim.expense_type = self._resolve_claim_expense_type_from_items(
|
||||
ordered_items,
|
||||
fallback=str(primary_item.item_type or claim.expense_type or "other").strip() or "other",
|
||||
)
|
||||
primary_item_type = str(primary_item.item_type or "").strip()
|
||||
if primary_item_type not in DOCUMENT_FACT_ITEM_TYPES:
|
||||
claim.reason = (
|
||||
self._normalize_optional_text(primary_item.item_reason, fallback=claim.reason or "待补充")
|
||||
or "待补充"
|
||||
)
|
||||
claim.location = (
|
||||
self._normalize_optional_text(primary_item.item_location, fallback=claim.location or "待补充")
|
||||
or "待补充"
|
||||
@@ -4543,8 +5110,20 @@ class ExpenseClaimService:
|
||||
claim,
|
||||
self._build_claim_attachment_risk_flags(ordered_items),
|
||||
)
|
||||
if str(claim.status or "").strip().lower() == "draft":
|
||||
claim.approval_stage = "待提交"
|
||||
if str(claim.status or "").strip().lower() == "draft":
|
||||
claim.approval_stage = "待提交"
|
||||
|
||||
@staticmethod
|
||||
def _resolve_claim_expense_type_from_items(
|
||||
items: list[ExpenseClaimItem],
|
||||
*,
|
||||
fallback: str,
|
||||
) -> str:
|
||||
fallback_type = str(fallback or "").strip() or "other"
|
||||
item_types = {str(item.item_type or "").strip().lower() for item in items}
|
||||
if item_types & TRAVEL_DETAIL_ITEM_TYPES:
|
||||
return "travel"
|
||||
return fallback_type
|
||||
|
||||
def _refresh_item_attachment_analysis(self, item: ExpenseClaimItem) -> None:
|
||||
file_path = self._resolve_attachment_path(item.invoice_id)
|
||||
|
||||
@@ -9,10 +9,12 @@ from typing import Any
|
||||
from sqlalchemy import or_, select
|
||||
from sqlalchemy.orm import Session, selectinload
|
||||
|
||||
from app.api.deps import CurrentUserContext
|
||||
from app.core.agent_enums import AgentAssetStatus, AgentAssetType
|
||||
from app.models.employee import Employee
|
||||
from app.models.financial_record import ExpenseClaim
|
||||
from app.schemas.agent_asset import AgentAssetListItem
|
||||
from app.schemas.reimbursement import TravelReimbursementCalculatorRequest
|
||||
from app.schemas.user_agent import (
|
||||
UserAgentCitation,
|
||||
UserAgentDraftPayload,
|
||||
@@ -37,6 +39,7 @@ from app.services.expense_claims import ExpenseClaimService
|
||||
from app.services.expense_rule_runtime import ExpenseRuleRuntimeService, RuntimeTravelPolicy, resolve_document_type_label
|
||||
from app.services.risk_ontology_bridge import resolve_rule_codes_for_risk_check
|
||||
from app.services.runtime_chat import RuntimeChatService
|
||||
from app.services.travel_reimbursement_calculator import TravelReimbursementCalculatorService
|
||||
|
||||
SCENARIO_LABELS = {
|
||||
"expense": "报销",
|
||||
@@ -187,6 +190,7 @@ DOCUMENT_AMOUNT_PATTERN = re.compile(
|
||||
)
|
||||
DOCUMENT_CURRENCY_AMOUNT_PATTERN = re.compile(r"[¥¥]\s*([0-9]+(?:[.,][0-9]{1,2})?)")
|
||||
TRAVEL_REVIEW_HOTEL_NIGHT_PATTERN = re.compile(r"(\d+)\s*(?:晚|间夜)")
|
||||
TRAVEL_ROUTE_PATTERN = re.compile(r"([\u4e00-\u9fa5]{2,12})\s*(?:至|→|->|-|—)\s*([\u4e00-\u9fa5]{2,12})")
|
||||
|
||||
SOURCE_LABELS = {
|
||||
"user_text": "用户描述",
|
||||
@@ -1900,6 +1904,11 @@ class UserAgentService:
|
||||
ocr_documents=ocr_documents,
|
||||
claim_groups=claim_groups,
|
||||
)
|
||||
travel_receipt_state = self._build_travel_receipt_state(
|
||||
payload,
|
||||
document_cards=document_cards,
|
||||
claim_groups=claim_groups,
|
||||
)
|
||||
missing_slot_keys = self._resolve_review_missing_slot_keys(
|
||||
payload,
|
||||
slot_cards=slot_cards,
|
||||
@@ -1911,10 +1920,11 @@ class UserAgentService:
|
||||
document_cards=document_cards,
|
||||
claim_groups=claim_groups,
|
||||
)
|
||||
risk_briefs.extend(self._build_travel_receipt_briefs(travel_receipt_state))
|
||||
association_choice_pending = self._is_review_association_choice_pending(payload)
|
||||
can_proceed = (
|
||||
False
|
||||
if association_choice_pending or submission_blocked
|
||||
if association_choice_pending or submission_blocked or travel_receipt_state.get("blocks_next_step")
|
||||
else self._can_proceed_review(
|
||||
payload,
|
||||
missing_slot_keys=missing_slot_keys,
|
||||
@@ -1943,7 +1953,15 @@ class UserAgentService:
|
||||
risk_briefs=risk_briefs,
|
||||
can_proceed=can_proceed,
|
||||
document_cards=document_cards,
|
||||
travel_receipt_state=travel_receipt_state,
|
||||
)
|
||||
missing_slot_labels = [SLOT_LABELS.get(key, key) for key in missing_slot_keys]
|
||||
missing_slot_labels.extend(
|
||||
str(item)
|
||||
for item in travel_receipt_state.get("required_missing_labels", [])
|
||||
if str(item).strip()
|
||||
)
|
||||
missing_slot_labels = list(dict.fromkeys(missing_slot_labels))
|
||||
|
||||
return UserAgentReviewPayload(
|
||||
intent_summary=intent_summary,
|
||||
@@ -1951,7 +1969,7 @@ class UserAgentService:
|
||||
scenario=payload.ontology.scenario,
|
||||
intent=payload.ontology.intent,
|
||||
can_proceed=can_proceed,
|
||||
missing_slots=[SLOT_LABELS.get(key, key) for key in missing_slot_keys],
|
||||
missing_slots=missing_slot_labels,
|
||||
risk_briefs=risk_briefs,
|
||||
slot_cards=slot_cards,
|
||||
document_cards=document_cards,
|
||||
@@ -2649,6 +2667,230 @@ class UserAgentService:
|
||||
return True
|
||||
return any(keyword in message_context for keyword in ("差旅", "出差", "机票", "火车", "高铁", "酒店", "住宿"))
|
||||
|
||||
def _build_travel_receipt_state(
|
||||
self,
|
||||
payload: UserAgentRequest,
|
||||
*,
|
||||
document_cards: list[UserAgentReviewDocumentCard],
|
||||
claim_groups: list[UserAgentReviewClaimGroup],
|
||||
) -> dict[str, Any]:
|
||||
empty_state: dict[str, Any] = {
|
||||
"is_travel_context": False,
|
||||
"has_long_distance_ticket": False,
|
||||
"ticket_type_label": "",
|
||||
"ticket_amount": Decimal("0.00"),
|
||||
"destination": "",
|
||||
"days": 1,
|
||||
"has_hotel_invoice": False,
|
||||
"has_local_transport": False,
|
||||
"required_missing_labels": [],
|
||||
"optional_missing_labels": [],
|
||||
"blocks_next_step": False,
|
||||
}
|
||||
if not document_cards or not self._is_travel_review_context(payload, document_cards, claim_groups):
|
||||
return empty_state
|
||||
|
||||
long_distance_cards = [card for card in document_cards if self._is_long_distance_travel_card(card)]
|
||||
if not long_distance_cards:
|
||||
return {
|
||||
**empty_state,
|
||||
"is_travel_context": True,
|
||||
}
|
||||
|
||||
has_hotel_invoice = any(self._is_review_hotel_card(card) for card in document_cards)
|
||||
has_local_transport = any(self._is_local_transport_receipt_card(card) for card in document_cards)
|
||||
required_missing_labels = [] if has_hotel_invoice else ["酒店的报销票据待上传(必须)"]
|
||||
optional_missing_labels = [] if has_local_transport else ["市内交通/乘车票据可继续上传(非必须)"]
|
||||
ticket_amount = sum(
|
||||
(self._extract_amount_decimal_from_card(card) or Decimal("0.00"))
|
||||
for card in long_distance_cards
|
||||
).quantize(Decimal("0.01"))
|
||||
|
||||
return {
|
||||
**empty_state,
|
||||
"is_travel_context": True,
|
||||
"has_long_distance_ticket": True,
|
||||
"ticket_type_label": self._resolve_travel_ticket_type_label(long_distance_cards),
|
||||
"ticket_amount": ticket_amount,
|
||||
"destination": self._resolve_travel_receipt_destination(payload, long_distance_cards),
|
||||
"days": self._resolve_travel_receipt_days(payload, long_distance_cards),
|
||||
"has_hotel_invoice": has_hotel_invoice,
|
||||
"has_local_transport": has_local_transport,
|
||||
"required_missing_labels": required_missing_labels,
|
||||
"optional_missing_labels": optional_missing_labels,
|
||||
"blocks_next_step": bool(required_missing_labels),
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def _is_long_distance_travel_card(card: UserAgentReviewDocumentCard) -> bool:
|
||||
document_type = str(card.document_type or "").strip().lower()
|
||||
return document_type in {"train_ticket", "flight_itinerary"}
|
||||
|
||||
@staticmethod
|
||||
def _is_local_transport_receipt_card(card: UserAgentReviewDocumentCard) -> bool:
|
||||
document_type = str(card.document_type or "").strip().lower()
|
||||
suggested_type = str(card.suggested_expense_type or "").strip().lower()
|
||||
return document_type in {"taxi_receipt", "parking_toll_receipt", "transport_receipt"} or (
|
||||
suggested_type == "transport" and document_type not in {"train_ticket", "flight_itinerary"}
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _resolve_travel_ticket_type_label(cards: list[UserAgentReviewDocumentCard]) -> str:
|
||||
labels: list[str] = []
|
||||
for card in cards:
|
||||
document_type = str(card.document_type or "").strip().lower()
|
||||
if document_type == "train_ticket" and "火车" not in labels:
|
||||
labels.append("火车")
|
||||
if document_type == "flight_itinerary" and "飞机" not in labels:
|
||||
labels.append("飞机")
|
||||
return "/".join(labels) if labels else "交通"
|
||||
|
||||
def _resolve_travel_receipt_destination(
|
||||
self,
|
||||
payload: UserAgentRequest,
|
||||
long_distance_cards: list[UserAgentReviewDocumentCard],
|
||||
) -> str:
|
||||
for card in long_distance_cards:
|
||||
for field in card.fields:
|
||||
if str(field.label or "").strip() not in {"行程", "路线"}:
|
||||
continue
|
||||
destination = self._extract_travel_destination_from_route(field.value)
|
||||
if destination:
|
||||
return self._normalize_travel_destination(destination)
|
||||
|
||||
card_text = self._build_review_document_card_text(card)
|
||||
route_match = TRAVEL_ROUTE_PATTERN.search(card_text)
|
||||
if route_match:
|
||||
return self._normalize_travel_destination(route_match.group(2))
|
||||
|
||||
location = self._resolve_location_value(payload)
|
||||
if location:
|
||||
return self._normalize_travel_destination(location)
|
||||
return ""
|
||||
|
||||
@staticmethod
|
||||
def _extract_travel_destination_from_route(value: str) -> str:
|
||||
route_text = str(value or "").strip()
|
||||
if not route_text:
|
||||
return ""
|
||||
route_match = TRAVEL_ROUTE_PATTERN.search(route_text)
|
||||
if route_match:
|
||||
return route_match.group(2).strip()
|
||||
parts = [
|
||||
item.strip()
|
||||
for item in re.split(r"\s*(?:至|到|→|->|-|—|~|~)\s*", route_text)
|
||||
if item.strip()
|
||||
]
|
||||
return parts[-1] if len(parts) >= 2 else ""
|
||||
|
||||
def _normalize_travel_destination(self, value: str) -> str:
|
||||
candidate = re.sub(
|
||||
r"(?:火车站|高铁站|动车站|车站|站|机场|航站楼)$",
|
||||
"",
|
||||
str(value or "").strip(),
|
||||
)
|
||||
if not candidate:
|
||||
return ""
|
||||
try:
|
||||
policy = ExpenseRuleRuntimeService(self.db).load_catalog().travel_policy
|
||||
except Exception:
|
||||
policy = None
|
||||
if policy is not None:
|
||||
policy_city = self._extract_policy_city_from_text(candidate, policy)
|
||||
if policy_city:
|
||||
return policy_city
|
||||
return candidate
|
||||
|
||||
def _resolve_travel_receipt_days(
|
||||
self,
|
||||
payload: UserAgentRequest,
|
||||
long_distance_cards: list[UserAgentReviewDocumentCard],
|
||||
) -> int:
|
||||
dates: list[datetime] = []
|
||||
for card in long_distance_cards:
|
||||
card_text = self._build_review_document_card_text(card)
|
||||
dates.extend(self._extract_dates_from_text(card_text))
|
||||
|
||||
if dates:
|
||||
return max(1, (max(dates).date() - min(dates).date()).days + 1)
|
||||
|
||||
start_date = self._parse_date_text(payload.ontology.time_range.start_date or "")
|
||||
end_date = self._parse_date_text(payload.ontology.time_range.end_date or "")
|
||||
if start_date and end_date:
|
||||
return max(1, (end_date.date() - start_date.date()).days + 1)
|
||||
return 1
|
||||
|
||||
@staticmethod
|
||||
def _extract_dates_from_text(text: str) -> list[datetime]:
|
||||
dates: list[datetime] = []
|
||||
for match in DATE_TEXT_PATTERN.finditer(str(text or "")):
|
||||
parsed = UserAgentService._parse_date_text(match.group(1))
|
||||
if parsed is not None:
|
||||
dates.append(parsed)
|
||||
return dates
|
||||
|
||||
@staticmethod
|
||||
def _parse_date_text(value: str) -> datetime | None:
|
||||
raw_value = str(value or "").strip()
|
||||
if not raw_value:
|
||||
return None
|
||||
normalized = (
|
||||
raw_value.replace("年", "-")
|
||||
.replace("月", "-")
|
||||
.replace("/", "-")
|
||||
.replace("日", "")
|
||||
.strip()
|
||||
)
|
||||
parts = [part for part in normalized.split("-") if part]
|
||||
if len(parts) != 3:
|
||||
return None
|
||||
try:
|
||||
year, month, day = (int(part) for part in parts)
|
||||
return datetime(year, month, day)
|
||||
except ValueError:
|
||||
return None
|
||||
|
||||
def _build_travel_receipt_briefs(
|
||||
self,
|
||||
travel_receipt_state: dict[str, Any],
|
||||
) -> list[UserAgentReviewRiskBrief]:
|
||||
if not travel_receipt_state.get("has_long_distance_ticket"):
|
||||
return []
|
||||
|
||||
required_labels = [
|
||||
str(item).strip()
|
||||
for item in travel_receipt_state.get("required_missing_labels", [])
|
||||
if str(item).strip()
|
||||
]
|
||||
optional_labels = [
|
||||
str(item).strip()
|
||||
for item in travel_receipt_state.get("optional_missing_labels", [])
|
||||
if str(item).strip()
|
||||
]
|
||||
if not required_labels and not optional_labels:
|
||||
return []
|
||||
|
||||
content_parts = [*required_labels, *optional_labels]
|
||||
required_text = ";".join(required_labels)
|
||||
optional_text = ";".join(optional_labels)
|
||||
return [
|
||||
UserAgentReviewRiskBrief(
|
||||
title="差旅票据待补充",
|
||||
level="warning" if required_labels else "info",
|
||||
content=";".join(content_parts),
|
||||
detail=(
|
||||
"系统已识别到长途交通票据,会按差旅报销口径核对住宿、交通等票据完整性。"
|
||||
+ (f"当前必须补充:{required_text}。" if required_text else "")
|
||||
+ (f"当前还可以补充:{optional_text}。" if optional_text else "")
|
||||
),
|
||||
suggestion=(
|
||||
"请先补充酒店住宿发票或住宿清单;在补齐前只能保存为草稿。"
|
||||
if required_labels
|
||||
else "如还有市内交通、打车、地铁或停车等乘车票据,可以继续上传;没有也可以进入下一步或保存草稿。"
|
||||
),
|
||||
)
|
||||
]
|
||||
|
||||
def _resolve_review_travel_allowance_standard(
|
||||
self,
|
||||
policy: RuntimeTravelPolicy,
|
||||
@@ -3008,7 +3250,7 @@ class UserAgentService:
|
||||
if draft_payload is not None and draft_payload.claim_no and not can_proceed:
|
||||
primary_action.description = f"保存后会生成草稿 {draft_payload.claim_no},后续仍可继续补充。"
|
||||
|
||||
return [
|
||||
actions = [
|
||||
UserAgentReviewAction(
|
||||
label="取消",
|
||||
action_type="cancel_review",
|
||||
@@ -3021,8 +3263,18 @@ class UserAgentService:
|
||||
description="打开结构化模板,按已识别字段逐项修改。",
|
||||
emphasis="secondary",
|
||||
),
|
||||
primary_action,
|
||||
]
|
||||
if can_proceed:
|
||||
actions.append(
|
||||
UserAgentReviewAction(
|
||||
label="保存为草稿",
|
||||
action_type="save_draft",
|
||||
description="先暂存当前已识别信息,稍后仍可从个人报销继续补充或提交。",
|
||||
emphasis="secondary",
|
||||
)
|
||||
)
|
||||
actions.append(primary_action)
|
||||
return actions
|
||||
|
||||
def _build_review_intent_summary(
|
||||
self,
|
||||
@@ -3086,20 +3338,22 @@ class UserAgentService:
|
||||
return "已按您当前确认的信息保存为草稿。后续您可以继续补充缺失项,或修改识别结果后再继续提交。"
|
||||
if review_action == "link_to_existing_draft":
|
||||
document_count = self._resolve_review_document_count(payload)
|
||||
followup_copy = self._build_review_action_followup_copy(review_payload)
|
||||
if draft_payload is not None and draft_payload.claim_no:
|
||||
return (
|
||||
f"已将本次上传的 {document_count} 张票据关联到草稿 {draft_payload.claim_no}。"
|
||||
"您可以继续补充识别字段,确认无误后再提交审批。"
|
||||
f"{followup_copy or '您可以继续补充识别字段,确认无误后再提交审批。'}"
|
||||
)
|
||||
return "已将本次上传的票据关联到现有草稿。您可以继续补充识别字段,确认无误后再提交审批。"
|
||||
return f"已将本次上传的票据关联到现有草稿。{followup_copy or '您可以继续补充识别字段,确认无误后再提交审批。'}"
|
||||
if review_action == "create_new_claim_from_documents":
|
||||
document_count = self._resolve_review_document_count(payload)
|
||||
followup_copy = self._build_review_action_followup_copy(review_payload)
|
||||
if draft_payload is not None and draft_payload.claim_no:
|
||||
return (
|
||||
f"已按当前上传的 {document_count} 张票据新建报销草稿 {draft_payload.claim_no}。"
|
||||
"您可以继续补充识别字段,确认无误后再提交审批。"
|
||||
f"{followup_copy or '您可以继续补充识别字段,确认无误后再提交审批。'}"
|
||||
)
|
||||
return "已按当前上传票据新建报销草稿。您可以继续补充识别字段,确认无误后再提交审批。"
|
||||
return f"已按当前上传票据新建报销草稿。{followup_copy or '您可以继续补充识别字段,确认无误后再提交审批。'}"
|
||||
if review_action == "next_step":
|
||||
if draft_payload is not None and draft_payload.status == "submitted":
|
||||
stage_text = draft_payload.approval_stage or "审批中"
|
||||
@@ -3135,6 +3389,7 @@ class UserAgentService:
|
||||
risk_briefs: list[UserAgentReviewRiskBrief],
|
||||
can_proceed: bool,
|
||||
document_cards: list[UserAgentReviewDocumentCard],
|
||||
travel_receipt_state: dict[str, Any] | None = None,
|
||||
) -> str:
|
||||
if self._is_review_association_choice_pending(payload):
|
||||
claim_no = str(payload.tool_payload.get("association_candidate_claim_no") or "").strip()
|
||||
@@ -3157,13 +3412,30 @@ class UserAgentService:
|
||||
"请先根据风险提示补充原因、调整金额或更换附件,整改后再继续提交。"
|
||||
)
|
||||
|
||||
travel_message = self._build_travel_receipt_guidance_message(
|
||||
payload,
|
||||
travel_receipt_state=travel_receipt_state or {},
|
||||
can_proceed=can_proceed,
|
||||
)
|
||||
if travel_message:
|
||||
return travel_message
|
||||
|
||||
missing_labels = self._resolve_review_missing_slot_labels(slot_cards)
|
||||
if travel_receipt_state:
|
||||
missing_labels.extend(
|
||||
str(item)
|
||||
for item in travel_receipt_state.get("required_missing_labels", [])
|
||||
if str(item).strip()
|
||||
)
|
||||
missing_labels = list(dict.fromkeys(missing_labels))
|
||||
|
||||
review_payload = UserAgentReviewPayload(
|
||||
intent_summary="",
|
||||
body_message="",
|
||||
scenario=payload.ontology.scenario,
|
||||
intent=payload.ontology.intent,
|
||||
can_proceed=can_proceed,
|
||||
missing_slots=self._resolve_review_missing_slot_labels(slot_cards),
|
||||
missing_slots=missing_labels,
|
||||
risk_briefs=risk_briefs,
|
||||
slot_cards=slot_cards,
|
||||
document_cards=[],
|
||||
@@ -3176,6 +3448,155 @@ class UserAgentService:
|
||||
f"{self._build_review_guidance_copy(review_payload, mention_save_draft=not can_proceed)}"
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _build_review_action_followup_copy(review_payload: UserAgentReviewPayload) -> str:
|
||||
missing_slots = [str(item).strip() for item in review_payload.missing_slots if str(item).strip()]
|
||||
receipt_briefs = [
|
||||
item
|
||||
for item in review_payload.risk_briefs
|
||||
if "差旅票据待补充" in str(item.title or "")
|
||||
]
|
||||
if missing_slots:
|
||||
return f"当前仍有 {'、'.join(missing_slots)},暂时只能保存为草稿,补齐后再继续下一步。"
|
||||
if receipt_briefs:
|
||||
return "当前必需票据已具备;如还有市内交通、打车、地铁或停车等乘车票据,可以继续上传,也可以继续下一步或保存草稿。"
|
||||
if review_payload.can_proceed:
|
||||
return "当前信息已较完整,您可以继续下一步,也可以先保存为草稿。"
|
||||
return ""
|
||||
|
||||
def _build_travel_receipt_guidance_message(
|
||||
self,
|
||||
payload: UserAgentRequest,
|
||||
*,
|
||||
travel_receipt_state: dict[str, Any],
|
||||
can_proceed: bool,
|
||||
) -> str:
|
||||
review_action = str(payload.context_json.get("review_action") or "").strip()
|
||||
if review_action or not travel_receipt_state.get("has_long_distance_ticket"):
|
||||
return ""
|
||||
|
||||
employee = self._resolve_employee_profile(payload)
|
||||
user_name = (
|
||||
str(employee.name).strip()
|
||||
if employee is not None and employee.name
|
||||
else str(payload.context_json.get("name") or payload.user_id or "同事").strip()
|
||||
)
|
||||
destination = str(travel_receipt_state.get("destination") or "待确认").strip()
|
||||
days = max(1, int(travel_receipt_state.get("days") or 1))
|
||||
ticket_type_label = str(travel_receipt_state.get("ticket_type_label") or "交通").strip()
|
||||
ticket_amount = self._coerce_decimal_money(travel_receipt_state.get("ticket_amount"))
|
||||
|
||||
required_labels = [
|
||||
str(item).strip()
|
||||
for item in travel_receipt_state.get("required_missing_labels", [])
|
||||
if str(item).strip()
|
||||
]
|
||||
optional_labels = [
|
||||
str(item).strip()
|
||||
for item in travel_receipt_state.get("optional_missing_labels", [])
|
||||
if str(item).strip()
|
||||
]
|
||||
|
||||
lines = [
|
||||
f"您好:{user_name},根据您提交的票据信息,您可能出差的地点为 {destination},天数为:{days} 天。",
|
||||
f"根据票据,您现在提交的是{ticket_type_label}票,一共金额为:{self._format_decimal_money(ticket_amount)} 元。",
|
||||
]
|
||||
|
||||
provide_items: list[str] = []
|
||||
if required_labels:
|
||||
provide_items.append("1. 酒店住宿发票/住宿清单(必须,当前待上传)")
|
||||
if optional_labels:
|
||||
provide_items.append(f"{len(provide_items) + 1}. 市内交通/乘车票据(非必须,如打车、地铁、停车等)")
|
||||
if provide_items:
|
||||
lines.append("根据公司相关报销制度,您还可以继续提供:\n" + "\n".join(provide_items))
|
||||
else:
|
||||
lines.append("根据公司相关报销制度,当前核心票据已较完整,无需继续上传票据。")
|
||||
|
||||
if required_labels:
|
||||
lines.append("酒店票据仍缺失,所以暂时不能继续下一步;您可以先保存为草稿,补齐后再提交。")
|
||||
elif can_proceed and optional_labels:
|
||||
lines.append("当前必需票据已具备;如暂时没有乘车票据,也可以继续下一步,或先保存为草稿。")
|
||||
elif can_proceed:
|
||||
lines.append("当前信息已较完整,确认无误后可以继续下一步,也可以先保存为草稿。")
|
||||
|
||||
estimate_copy = self._build_travel_receipt_estimate_copy(
|
||||
payload,
|
||||
travel_receipt_state=travel_receipt_state,
|
||||
)
|
||||
if estimate_copy:
|
||||
lines.append(estimate_copy)
|
||||
return "\n".join(line for line in lines if line)
|
||||
|
||||
def _build_travel_receipt_estimate_copy(
|
||||
self,
|
||||
payload: UserAgentRequest,
|
||||
*,
|
||||
travel_receipt_state: dict[str, Any],
|
||||
) -> str:
|
||||
destination = str(travel_receipt_state.get("destination") or "").strip()
|
||||
days = max(1, int(travel_receipt_state.get("days") or 1))
|
||||
ticket_type_label = str(travel_receipt_state.get("ticket_type_label") or "交通").strip()
|
||||
ticket_amount = self._coerce_decimal_money(travel_receipt_state.get("ticket_amount"))
|
||||
employee = self._resolve_employee_profile(payload)
|
||||
grade = self._resolve_review_employee_grade(payload, employee=employee)
|
||||
|
||||
if not destination or not grade:
|
||||
return (
|
||||
"根据公司差旅费报销依据,"
|
||||
f"您的职级为:{grade or '待确认'},去{destination or '出差地点待确认'},"
|
||||
f"当前可确认的{ticket_type_label}票据金额为:{self._format_decimal_money(ticket_amount)} 元;"
|
||||
"住宿和补贴金额需补齐职级或地点后再核算。"
|
||||
)
|
||||
|
||||
current_user = CurrentUserContext(
|
||||
username=str(payload.user_id or payload.context_json.get("name") or "anonymous").strip() or "anonymous",
|
||||
name=str(payload.context_json.get("name") or payload.user_id or "anonymous").strip() or "anonymous",
|
||||
role_codes=[
|
||||
str(item).strip()
|
||||
for item in list(payload.context_json.get("role_codes") or [])
|
||||
if str(item).strip()
|
||||
],
|
||||
is_admin=bool(payload.context_json.get("is_admin")),
|
||||
department_name=str(payload.context_json.get("department_name") or payload.context_json.get("department") or "").strip(),
|
||||
)
|
||||
try:
|
||||
calculation = TravelReimbursementCalculatorService(self.db).calculate(
|
||||
TravelReimbursementCalculatorRequest(days=days, location=destination, grade=grade),
|
||||
current_user,
|
||||
)
|
||||
except Exception:
|
||||
return (
|
||||
"根据公司差旅费报销依据,"
|
||||
f"您的职级为:{grade},去{destination},当前可确认的{ticket_type_label}票据金额为:"
|
||||
f"{self._format_decimal_money(ticket_amount)} 元;住宿和补贴标准暂时无法自动测算,请以规则中心最新差旅标准为准。"
|
||||
)
|
||||
|
||||
total_amount = (
|
||||
ticket_amount
|
||||
+ self._coerce_decimal_money(calculation.hotel_amount)
|
||||
+ self._coerce_decimal_money(calculation.allowance_amount)
|
||||
).quantize(Decimal("0.01"))
|
||||
return (
|
||||
"根据公司差旅费报销依据,"
|
||||
f"您的职级为:{calculation.grade},去{calculation.matched_city or destination},"
|
||||
"报销费用核算约为:"
|
||||
f"已提交{ticket_type_label} {self._format_decimal_money(ticket_amount)} 元 + "
|
||||
f"住宿标准 {self._format_decimal_money(calculation.hotel_rate)} 元/天 × {calculation.days} 天 + "
|
||||
f"出差补贴 {self._format_decimal_money(calculation.total_allowance_rate)} 元/天 × {calculation.days} 天 = "
|
||||
f"{self._format_decimal_money(total_amount)} 元。"
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _coerce_decimal_money(value: Any) -> Decimal:
|
||||
try:
|
||||
return Decimal(str(value or "0")).quantize(Decimal("0.01"))
|
||||
except (InvalidOperation, ValueError):
|
||||
return Decimal("0.00")
|
||||
|
||||
@staticmethod
|
||||
def _format_decimal_money(value: Any) -> str:
|
||||
return f"{UserAgentService._coerce_decimal_money(value):.2f}"
|
||||
|
||||
@staticmethod
|
||||
def _resolve_review_missing_slot_labels(
|
||||
slot_cards: list[UserAgentReviewSlotCard],
|
||||
@@ -4076,16 +4497,11 @@ class UserAgentService:
|
||||
|
||||
merchant_value = ""
|
||||
for document in ocr_documents:
|
||||
if str(document.get("document_type") or "").strip().lower() != "hotel_invoice":
|
||||
if not self._is_hotel_document_item(document):
|
||||
continue
|
||||
merchant_value = self._extract_document_merchant_name(document)
|
||||
if merchant_value:
|
||||
break
|
||||
if not merchant_value:
|
||||
for document in ocr_documents:
|
||||
merchant_value = self._extract_document_merchant_name(document)
|
||||
if merchant_value:
|
||||
break
|
||||
if merchant_value:
|
||||
return self._build_slot_value(
|
||||
value=merchant_value,
|
||||
@@ -4407,6 +4823,8 @@ class UserAgentService:
|
||||
label=display_label,
|
||||
value=value,
|
||||
)
|
||||
if display_label == "商户/酒店" and not self._is_hotel_document_item(item):
|
||||
continue
|
||||
if display_label and normalized_value:
|
||||
normalized_fields.setdefault(display_label, normalized_value)
|
||||
|
||||
@@ -4418,7 +4836,7 @@ class UserAgentService:
|
||||
if date_match and "时间" not in normalized_fields:
|
||||
normalized_fields["时间"] = date_match.group(1)
|
||||
|
||||
merchant = self._extract_document_merchant_name_from_text(text)
|
||||
merchant = self._extract_document_merchant_name_from_text(text) if self._is_hotel_document_item(item) else ""
|
||||
if merchant and "商户/酒店" not in normalized_fields:
|
||||
normalized_fields["商户/酒店"] = merchant
|
||||
return normalized_fields
|
||||
@@ -4484,9 +4902,25 @@ class UserAgentService:
|
||||
merchant = str(fields.get("商户/酒店") or "").strip()
|
||||
if merchant:
|
||||
return merchant
|
||||
if not self._is_hotel_document_item(item):
|
||||
return ""
|
||||
text = " ".join([str(item.get("summary") or ""), str(item.get("text") or "")]).strip()
|
||||
return self._extract_document_merchant_name_from_text(text)
|
||||
|
||||
@staticmethod
|
||||
def _is_hotel_document_item(item: dict[str, object]) -> bool:
|
||||
document_type = str(item.get("document_type") or "").strip().lower()
|
||||
scene_code = str(item.get("scene_code") or "").strip().lower()
|
||||
scene_label = str(item.get("scene_label") or "").strip()
|
||||
suggested_expense_type = str(item.get("suggested_expense_type") or "").strip().lower()
|
||||
return (
|
||||
document_type == "hotel_invoice"
|
||||
or scene_code == "hotel"
|
||||
or suggested_expense_type == "hotel"
|
||||
or "住宿" in scene_label
|
||||
or "酒店" in scene_label
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _extract_document_merchant_name_from_text(text: str) -> str:
|
||||
for keyword in ("酒店", "宾馆", "饭店", "酒楼", "餐厅", "航空", "铁路", "滴滴"):
|
||||
|
||||
Reference in New Issue
Block a user