feat(invoice): 立账凭证检核复制文件去掉文件名末尾的“.pdf(重命名)”
(cherry picked from commit 04264284137c368f98504fd01a2c49d7fd1c11c4)
This commit is contained in:
1 parent
3710aa4d81
commit
f269df7941
4 files changed
+131
-24
No files matched your search
@@ -4,7 +4,7 @@ backend/app/services/voucher_check/dms_client.py
|
||||
用途:销售开票附件文档库客户端(登录、列目录、建文件夹、上传、复制;不移动原件)。
|
||||
依赖:requests、Flask 配置;登录接口与合同库相同。
|
||||
逻辑:LibraryID 复用文档库;根 FolderID 必须是销售开票附件。上传走 Attachment/Upload + Item/CreateFiles;归档复制同名跳过。
|
||||
文档库复制后若把文件名写成「原名(重命名).扩展名」,用 Item/Rename 改回原文件名;同一目录多出来的该后缀副本用 Item/Recycle 删除。
|
||||
文档库复制后 LeafName 写成「原名.pdf(重命名)」,扩展名另存。改回「原名.pdf」会被判同名,必须改成不含扩展名的「原名」;多余副本用 Item/Recycle 删除。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -43,6 +43,35 @@ def canonical_leaf_name(name: str) -> str:
|
||||
return re.sub(r"(\.[A-Za-z0-9]{1,8})\s*\1$", r"\1", cleaned).strip()
|
||||
|
||||
|
||||
def dms_stored_leaf_name(filename: str) -> str:
|
||||
"""
|
||||
算出文档库改名接口要写入的 LeafName。
|
||||
核心逻辑:库里真实名字是「发票.pdf(重命名)」,扩展名在附件上。改成「发票.pdf」会和自己判成同名;改成「发票」后界面仍显示「发票.pdf」。
|
||||
"""
|
||||
name = str(filename or "").strip()
|
||||
suffix = Path(name).suffix
|
||||
if suffix and name.lower().endswith(suffix.lower()) and len(name) > len(suffix):
|
||||
return name[: -len(suffix)]
|
||||
return name
|
||||
|
||||
|
||||
def row_display_name(row: dict[str, Any]) -> str:
|
||||
"""
|
||||
还原文档库界面上的文件名。
|
||||
核心逻辑:LeafName 没有以附件扩展名结尾时,界面会把扩展名补在后面。
|
||||
"""
|
||||
leaf = str((row or {}).get("LeafName") or "").strip()
|
||||
attachment = (row or {}).get("MainFileAttachment") or {}
|
||||
if not isinstance(attachment, dict):
|
||||
attachment = {}
|
||||
ext = str(attachment.get("Extension") or "").strip()
|
||||
if ext and not ext.startswith("."):
|
||||
ext = f".{ext}"
|
||||
if ext and leaf and not leaf.lower().endswith(ext.lower()):
|
||||
return f"{leaf}{ext}"
|
||||
return leaf
|
||||
|
||||
|
||||
def leaf_names_match(actual: str, wanted: str) -> bool:
|
||||
"""
|
||||
判断目录里的文件名是否就是要找的那一份。
|
||||
@@ -299,7 +328,7 @@ class SalesDmsClient:
|
||||
def find_child_file(cls, *, parent_folder_id: str, leaf_name: str) -> dict[str, Any] | None:
|
||||
"""
|
||||
在父目录下按 LeafName 找非文件夹条目,找不到返回 None。
|
||||
核心逻辑:优先完全同名;否则返回文档库改成「原名(重命名).扩展名」的那一份,供归档改回原名,避免再次复制。
|
||||
核心逻辑:优先完全同名。库内名字可能是「原名.pdf(重命名)」或已改回的「原名」,都按界面显示名认成同一份。
|
||||
"""
|
||||
wanted = str(leaf_name or "").strip()
|
||||
if not wanted:
|
||||
@@ -307,9 +336,10 @@ class SalesDmsClient:
|
||||
renamed: dict[str, Any] | None = None
|
||||
for row in cls.list_files(folder_id=parent_folder_id):
|
||||
leaf = str(row.get("LeafName") or "").strip()
|
||||
if leaf == wanted:
|
||||
shown = row_display_name(row)
|
||||
if leaf == wanted or shown == wanted:
|
||||
return row
|
||||
if renamed is None and leaf_names_match(leaf, wanted):
|
||||
if renamed is None and (leaf_names_match(leaf, wanted) or leaf_names_match(shown, wanted)):
|
||||
renamed = row
|
||||
return renamed
|
||||
|
||||
@@ -455,21 +485,24 @@ class SalesDmsClient:
|
||||
row: dict[str, Any],
|
||||
) -> dict[str, Any]:
|
||||
"""
|
||||
所有归档要求文件名不带「(重命名)」。
|
||||
核心逻辑:当前条目只是原名加上文档库后缀时,改回原名后再按完全同名回查。多余副本由归档收尾删除。
|
||||
所有归档要求界面上不带「(重命名)」。
|
||||
核心逻辑:先确认这份就是目标文件。改名只写不含扩展名的 LeafName;写成「原名.pdf」会和「原名.pdf(重命名)」判成同名。
|
||||
"""
|
||||
leaf = str(row.get("LeafName") or "").strip()
|
||||
if leaf == wanted:
|
||||
shown = row_display_name(row)
|
||||
target = dms_stored_leaf_name(wanted)
|
||||
if leaf in {wanted, target} or shown == wanted:
|
||||
return row
|
||||
if canonical_leaf_name(leaf) != wanted:
|
||||
if canonical_leaf_name(leaf) != wanted and canonical_leaf_name(shown) != wanted:
|
||||
raise ServiceUnavailableError(
|
||||
"目标目录文件名与归档文件名不一致",
|
||||
code="VOUCHER_CHECK.ARCHIVE_FAILED",
|
||||
details={"leaf_name": wanted, "actual_name": leaf},
|
||||
)
|
||||
cls.rename_item(item_id=str(row.get("ID") or ""), leaf_name=wanted)
|
||||
cls.rename_item(item_id=str(row.get("ID") or ""), leaf_name=target)
|
||||
for item in cls.list_files(folder_id=parent_folder_id):
|
||||
if str(item.get("LeafName") or "").strip() == wanted:
|
||||
item_leaf = str(item.get("LeafName") or "").strip()
|
||||
if item_leaf in {wanted, target} or row_display_name(item) == wanted:
|
||||
return item
|
||||
raise ServiceUnavailableError(
|
||||
"去掉重命名后缀后未找到原文件名",
|
||||
@@ -511,9 +544,10 @@ class SalesDmsClient:
|
||||
for row in cls.list_files(folder_id=parent_folder_id):
|
||||
item_id = str(row.get("ID") or "").strip()
|
||||
leaf = str(row.get("LeafName") or "").strip()
|
||||
if not item_id or item_id == keep or "重命名" not in leaf:
|
||||
shown = row_display_name(row)
|
||||
if not item_id or item_id == keep or ("重命名" not in leaf and "重命名" not in shown):
|
||||
continue
|
||||
if canonical_leaf_name(leaf) == wanted:
|
||||
if canonical_leaf_name(leaf) == wanted or canonical_leaf_name(shown) == wanted:
|
||||
extras.append(item_id)
|
||||
if extras:
|
||||
cls.recycle_items(item_ids=extras)
|
||||
@@ -527,17 +561,15 @@ class SalesDmsClient:
|
||||
row: dict[str, Any],
|
||||
) -> dict[str, Any]:
|
||||
"""
|
||||
归档落盘后收成一个不带后缀的原名。
|
||||
核心逻辑:必要时先改回原名,再删除同目录其余「(重命名)」副本。
|
||||
归档落盘后收成一个界面上不带后缀的原名。
|
||||
核心逻辑:先删掉其余「(重命名)」副本,再把留下的那份改成不含扩展名的 LeafName。先改名会被文档库判成同名。
|
||||
"""
|
||||
if str(row.get("LeafName") or "").strip() != wanted:
|
||||
row = cls._restore_exact_leaf(parent_folder_id=parent_folder_id, wanted=wanted, row=row)
|
||||
cls._drop_extra_renamed_copies(
|
||||
parent_folder_id=parent_folder_id,
|
||||
wanted=wanted,
|
||||
keep_item_id=str(row.get("ID") or "").strip(),
|
||||
)
|
||||
return row
|
||||
return cls._restore_exact_leaf(parent_folder_id=parent_folder_id, wanted=wanted, row=row)
|
||||
|
||||
@classmethod
|
||||
def copy_file_skip_same_name(
|
||||
|
||||
@@ -20,7 +20,9 @@ from app.services.voucher_check.dms_client import (
|
||||
_extract_item_id,
|
||||
_is_name_conflict,
|
||||
canonical_leaf_name,
|
||||
dms_stored_leaf_name,
|
||||
leaf_names_match,
|
||||
row_display_name,
|
||||
)
|
||||
|
||||
|
||||
@@ -99,6 +101,14 @@ def test_canonical_leaf_name_strips_dms_rename_marker():
|
||||
assert canonical_leaf_name("QCG260921187-8501-2609210008.pdf (重命名) .pdf") == (
|
||||
"QCG260921187-8501-2609210008.pdf"
|
||||
)
|
||||
# 库内真实名字不带界面补上的第二个 .pdf。改回带 .pdf 的原名会被文档库判成同名。
|
||||
stored = "QCH2609188245-8501-2609170001.pdf(重命名)"
|
||||
assert canonical_leaf_name(stored) == "QCH2609188245-8501-2609170001.pdf"
|
||||
assert dms_stored_leaf_name("QCH2609188245-8501-2609170001.pdf") == "QCH2609188245-8501-2609170001"
|
||||
assert row_display_name({
|
||||
"LeafName": stored,
|
||||
"MainFileAttachment": {"Extension": ".pdf"},
|
||||
}) == "QCH2609188245-8501-2609170001.pdf(重命名).pdf"
|
||||
|
||||
|
||||
def test_find_child_file_accepts_renamed_copy(monkeypatch):
|
||||
@@ -143,8 +153,8 @@ def test_copy_renames_existing_suffix_back_to_original(monkeypatch):
|
||||
leaf_name="INV001.pdf",
|
||||
)
|
||||
assert skipped is True
|
||||
assert row["LeafName"] == "INV001.pdf"
|
||||
assert renames == [{"item_id": "dest-r", "leaf_name": "INV001.pdf"}]
|
||||
assert row["LeafName"] == "INV001"
|
||||
assert renames == [{"item_id": "dest-r", "leaf_name": "INV001"}]
|
||||
assert copies == []
|
||||
|
||||
|
||||
@@ -175,8 +185,8 @@ def test_copy_strips_rename_suffix_after_copy(monkeypatch):
|
||||
)
|
||||
assert skipped is False
|
||||
assert row["ID"] == "new-1"
|
||||
assert row["LeafName"] == "INV001.pdf"
|
||||
assert renames == [{"item_id": "new-1", "leaf_name": "INV001.pdf"}]
|
||||
assert row["LeafName"] == "INV001"
|
||||
assert renames == [{"item_id": "new-1", "leaf_name": "INV001"}]
|
||||
|
||||
|
||||
def test_copy_keeps_original_and_recycles_renamed_extras(monkeypatch):
|
||||
@@ -253,11 +263,76 @@ def test_copy_restores_one_of_three_renamed_copies_and_recycles_the_rest(monkeyp
|
||||
)
|
||||
assert skipped is True
|
||||
assert row["ID"] == "a"
|
||||
assert row["LeafName"] == wanted
|
||||
assert row["LeafName"] == "QCG260921187-8501-2609210008"
|
||||
assert recycled == [["b", "c"]]
|
||||
assert [item["ID"] for item in files["rows"]] == ["a"]
|
||||
|
||||
|
||||
def test_copy_renames_real_dms_leaf_without_extension(monkeypatch):
|
||||
# 现网 LeafName 是「原名.pdf(重命名)」,扩展名在附件上。改名必须去掉扩展名,不能写成「原名.pdf」。
|
||||
wanted = "QCH2609188245-8501-2609170001.pdf"
|
||||
stored = f"{wanted}(重命名)"
|
||||
files = {
|
||||
"rows": [
|
||||
{"ID": "a", "LeafName": stored, "MainFileAttachment": {"Extension": ".pdf"}},
|
||||
{"ID": "b", "LeafName": stored, "MainFileAttachment": {"Extension": ".pdf"}},
|
||||
]
|
||||
}
|
||||
renames: list[dict[str, str]] = []
|
||||
|
||||
def list_files(**_kwargs):
|
||||
return list(files["rows"])
|
||||
|
||||
def rename_item(**kwargs):
|
||||
renames.append(kwargs)
|
||||
for item in files["rows"]:
|
||||
if item["ID"] == kwargs["item_id"]:
|
||||
item["LeafName"] = kwargs["leaf_name"]
|
||||
return {"Code": 0}
|
||||
|
||||
def recycle_items(**kwargs):
|
||||
drop = set(kwargs["item_ids"])
|
||||
files["rows"] = [row for row in files["rows"] if row["ID"] not in drop]
|
||||
return {"Code": 0}
|
||||
|
||||
monkeypatch.setattr(SalesDmsClient, "list_files", list_files)
|
||||
monkeypatch.setattr(SalesDmsClient, "copy_items", lambda **_kwargs: (_ for _ in ()).throw(AssertionError("不应复制")))
|
||||
monkeypatch.setattr(SalesDmsClient, "rename_item", rename_item)
|
||||
monkeypatch.setattr(SalesDmsClient, "recycle_items", recycle_items)
|
||||
row, skipped = SalesDmsClient.copy_file_skip_same_name(
|
||||
source_item_id="src-1",
|
||||
dest_folder_id="inv-folder",
|
||||
leaf_name=wanted,
|
||||
)
|
||||
assert skipped is True
|
||||
assert row["ID"] == "a"
|
||||
assert renames == [{"item_id": "a", "leaf_name": "QCH2609188245-8501-2609170001"}]
|
||||
assert [item["ID"] for item in files["rows"]] == ["a"]
|
||||
|
||||
|
||||
def test_copy_skips_when_stored_leaf_is_already_the_stem(monkeypatch):
|
||||
# 改名成功后 LeafName 是不带扩展名的原名,下次检核不能再复制。
|
||||
monkeypatch.setattr(
|
||||
SalesDmsClient,
|
||||
"list_files",
|
||||
lambda **_kwargs: [{
|
||||
"ID": "a",
|
||||
"LeafName": "QCH2609188245-8501-2609170001",
|
||||
"MainFileAttachment": {"Extension": ".pdf"},
|
||||
}],
|
||||
)
|
||||
monkeypatch.setattr(SalesDmsClient, "copy_items", lambda **_kwargs: (_ for _ in ()).throw(AssertionError("不应复制")))
|
||||
monkeypatch.setattr(SalesDmsClient, "rename_item", lambda **_kwargs: (_ for _ in ()).throw(AssertionError("不应改名")))
|
||||
monkeypatch.setattr(SalesDmsClient, "recycle_items", lambda **_kwargs: (_ for _ in ()).throw(AssertionError("不应删除")))
|
||||
row, skipped = SalesDmsClient.copy_file_skip_same_name(
|
||||
source_item_id="src-1",
|
||||
dest_folder_id="inv-folder",
|
||||
leaf_name="QCH2609188245-8501-2609170001.pdf",
|
||||
)
|
||||
assert skipped is True
|
||||
assert row["ID"] == "a"
|
||||
|
||||
|
||||
def test_copy_skips_when_dest_has_same_name(monkeypatch):
|
||||
monkeypatch.setattr(
|
||||
SalesDmsClient,
|
||||
|
||||
@@ -281,7 +281,7 @@ OA 销货放行申请单改为查 E10 `ecology10.dbo.ft_xqqgsq`:出货时间 `
|
||||
|
||||
类型目录与一期文件对应(**已确认固定,不改名**):`PO订单`、`报关资料`、`销货放行申请单`、`发票`、`报关单`、`提单`。
|
||||
|
||||
ERP/OA 打印或目录扫描的成功文件先写入类型存放目录;完成 `invoice` 关联、命名和文件完整性校验后,**复制**到「所有归档」下该发票子目录(不移动类型目录原件)。同一 `invoice` 的所有成功资料必须集中在同一归档子目录。**重复文件策略:同名跳过**——目标子目录已有相同 `LeafName` 时不覆盖、不报缺件,记为已归档。文档库自动改成 `原名(重命名).扩展名` 时,用 `Item/Rename` 改回原文件名,同一目录多出来的该后缀副本用 `Item/Recycle` 删除。类型目录写入失败或归档复制失败属于技术失败,不可降级为缺件。
|
||||
ERP/OA 打印或目录扫描的成功文件先写入类型存放目录;完成 `invoice` 关联、命名和文件完整性校验后,**复制**到「所有归档」下该发票子目录(不移动类型目录原件)。同一 `invoice` 的所有成功资料必须集中在同一归档子目录。**重复文件策略:同名跳过**——目标子目录已有相同 `LeafName` 时不覆盖、不报缺件,记为已归档。文档库自动改成 `原名.pdf(重命名)` 时,先删掉多余副本,再用 `Item/Rename` 把留下的那份改成不含扩展名的 `原名`。改成 `原名.pdf` 会被文档库判成同名。类型目录写入失败或归档复制失败属于技术失败,不可降级为缺件。
|
||||
|
||||
### 5.2 建议持久化实体
|
||||
|
||||
|
||||
@@ -373,7 +373,7 @@ sequenceDiagram
|
||||
|
||||
- ERP/OA 打印或目录扫描的成功文件先写入当月类型存放目录,再校验关联字段和命名后复制到「所有归档/{invoice_no}/」。
|
||||
- 同一 invoice 的所有文件进入同一归档子目录;不同 invoice 不得混放。
|
||||
- 复制采用幂等策略:**同名跳过**。目标目录已有相同 `LeafName` 时不覆盖、不报缺件,记为已归档;不按内容指纹覆盖。文档库若把复制结果写成 `原名(重命名).扩展名`,调用 `Item/Rename` 改回原文件名。同一目录里多出来的该后缀副本调用 `Item/Recycle` 删除(进入回收站,不彻底删除)。归档只 `Copy`、不 `Move`。
|
||||
- 复制采用幂等策略:**同名跳过**。目标目录已有相同 `LeafName` 时不覆盖、不报缺件,记为已归档;不按内容指纹覆盖。文档库复制后的真实名字是 `原名.pdf(重命名)`,扩展名存在附件上,界面显示为 `原名.pdf(重命名).pdf`。把它改成 `原名.pdf` 会被判成同名;`Item/Rename` 只写入不含扩展名的 `原名`。同一目录多出来的该后缀副本先 `Item/Recycle` 删除(进入回收站,不彻底删除)。归档只 `Copy`、不 `Move`。
|
||||
- `invoice_no`、业务单号和路径变量必须做路径穿越校验。
|
||||
- 临时目录和日志不得输出密码、Token 或完整外部系统会话信息。
|
||||
- 归档记录保存来源路径、临时路径、归档路径、指纹、Profile 版本和处理时间。
|
||||
|
||||
Reference in new issue
Block a user