File size: 5,594 Bytes
3e1cbf3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | """Soạn văn bản hành chính theo thể thức Nghị định 30/2020/NĐ-CP (M5).
Hỗ trợ: quyết định, công văn, thông báo, tờ trình. Phần "Căn cứ" lấy từ RAG
(chỉ văn bản còn hiệu lực). Có kiểm tra thể thức và xuất DOCX. Quy trình có bước
duyệt (HITL) trước khi xuất — xem routes /api/draft/preview và /api/draft/export.
Tham khảo cấu trúc thể thức: vbhc / ND30_Skills (ghi nguồn).
"""
from __future__ import annotations
from dataclasses import dataclass, field
DOC_TYPES = {
"quyet_dinh": "QUYẾT ĐỊNH",
"cong_van": "CÔNG VĂN",
"thong_bao": "THÔNG BÁO",
"to_trinh": "TỜ TRÌNH",
}
QUOC_HIEU = "CỘNG HÒA XÃ HỘI CHỦ NGHĨA VIỆT NAM"
TIEU_NGU = "Độc lập - Tự do - Hạnh phúc"
@dataclass
class DraftInput:
doc_type: str # key trong DOC_TYPES
org_name: str # tên cơ quan ban hành
org_parent: str | None = None # cơ quan chủ quản (nếu có)
number: str | None = None # số/ký hiệu, vd "12/QĐ-..."
place: str = "Hà Nội" # địa danh
date_str: str | None = None # ngày tháng năm (tự điền nếu trống)
subject: str = "" # trích yếu nội dung
bases: list[str] = field(default_factory=list) # các "Căn cứ"
body: str = "" # nội dung chính
signer_title: str = "GIÁM ĐỐC" # chức vụ người ký
signer_name: str = "" # họ tên người ký
recipients: list[str] = field(default_factory=lambda: ["Như trên", "Lưu: VT."])
def _today_vn() -> str:
from datetime import date
d = date.today()
return f"ngày {d.day:02d} tháng {d.month:02d} năm {d.year}"
def format_check(d: DraftInput) -> list[str]:
"""Kiểm tra thể thức tối thiểu theo NĐ30. Trả list cảnh báo."""
issues = []
if d.doc_type not in DOC_TYPES:
issues.append(f"Loại văn bản không hợp lệ: {d.doc_type}")
if not d.org_name.strip():
issues.append("Thiếu tên cơ quan ban hành.")
if not d.subject.strip():
issues.append("Thiếu trích yếu nội dung (phần tên loại và trích yếu).")
if not d.body.strip():
issues.append("Thiếu nội dung chính.")
if d.doc_type == "quyet_dinh" and not d.bases:
issues.append("Quyết định nên có phần 'Căn cứ'.")
if not d.signer_name.strip():
issues.append("Thiếu họ tên người ký.")
if not d.number:
issues.append("Thiếu số/ký hiệu văn bản.")
return issues
def render_text(d: DraftInput) -> str:
"""Bản xem trước dạng text theo bố cục NĐ30."""
date_str = d.date_str or _today_vn()
type_label = DOC_TYPES.get(d.doc_type, d.doc_type.upper())
L: list[str] = []
# Khối cơ quan (trái) và quốc hiệu (phải) — trình bày tuyến tính cho text.
if d.org_parent:
L.append(d.org_parent.upper())
L.append(d.org_name.upper())
L.append(f"Số: {d.number or '..../'}")
L.append("")
L.append(QUOC_HIEU)
L.append(TIEU_NGU)
L.append(f"{d.place}, {date_str}")
L.append("")
L.append(type_label)
if d.subject:
if d.doc_type == "cong_van":
L.append(f"V/v {d.subject}")
else:
L.append(d.subject)
L.append("")
if d.bases:
for b in d.bases:
L.append(f"Căn cứ {b};")
L.append("")
L.append(d.body.strip())
L.append("")
L.append(f"{'Nơi nhận:':<40}{d.signer_title}")
for r in d.recipients:
L.append(f"- {r};")
L.append("")
L.append(f"{'':<40}{d.signer_name}")
return "\n".join(L)
def render_docx(d: DraftInput) -> bytes:
"""Xuất DOCX theo thể thức NĐ30 (căn lề, canh giữa quốc hiệu/tên loại)."""
import io
from docx import Document
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.shared import Pt
date_str = d.date_str or _today_vn()
type_label = DOC_TYPES.get(d.doc_type, d.doc_type.upper())
doc = Document()
style = doc.styles["Normal"]
style.font.name = "Times New Roman"
style.font.size = Pt(13)
def p(text="", *, align=None, bold=False, italic=False, size=None):
par = doc.add_paragraph()
run = par.add_run(text)
run.bold = bold
run.italic = italic
if size:
run.font.size = Pt(size)
if align:
par.alignment = align
return par
center = WD_ALIGN_PARAGRAPH.CENTER
# Quốc hiệu - tiêu ngữ (canh giữa, trên cùng theo mẫu rút gọn)
p(QUOC_HIEU, align=center, bold=True)
p(TIEU_NGU, align=center, bold=True)
p("")
if d.org_parent:
p(d.org_parent.upper())
p(d.org_name.upper(), bold=True)
p(f"Số: {d.number or '..../'}")
p(f"{d.place}, {date_str}", align=WD_ALIGN_PARAGRAPH.RIGHT, italic=True)
p("")
p(type_label, align=center, bold=True, size=14)
if d.subject:
p(f"V/v {d.subject}" if d.doc_type == "cong_van" else d.subject, align=center, bold=True)
p("")
for b in d.bases:
p(f"Căn cứ {b};", italic=True)
if d.bases:
p("")
for para in d.body.strip().split("\n"):
p(para)
p("")
# Nơi nhận + chữ ký (hai cột rút gọn)
p("Nơi nhận:", bold=True, italic=True)
for r in d.recipients:
p(f"- {r};")
p("")
p(d.signer_title, align=center, bold=True)
p("")
p("")
p(d.signer_name, align=center, bold=True)
buf = io.BytesIO()
doc.save(buf)
return buf.getvalue()
|