306 lines
11 KiB
Python
306 lines
11 KiB
Python
"""
|
||
docs/features/accounting-voucher-check/hxops-po-sample.py
|
||
|
||
用途:用 ERP 打印件同款模拟数据生成「HXOPS 制式采购单」样张,供与 apmr900 对照。
|
||
依赖:reportlab、Windows 宋体、welltrust-logo.png(从原件抽出的页眉 Logo)。
|
||
逻辑:独立脚本,不接入周检核;数据写死自 采购单.pdf;不贴「无」图。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from pathlib import Path
|
||
|
||
from reportlab.lib.colors import HexColor
|
||
from reportlab.lib.pagesizes import A4
|
||
from reportlab.lib.units import mm
|
||
from reportlab.pdfbase import pdfmetrics
|
||
from reportlab.pdfbase.ttfonts import TTFont
|
||
from reportlab.pdfgen import canvas
|
||
|
||
FONT = "SimSun"
|
||
FONT_PATH = Path(r"C:\Windows\Fonts\simsun.ttc")
|
||
LOGO_PATH = Path(__file__).resolve().parent / "welltrust-logo.png"
|
||
INK = HexColor("#111111")
|
||
LINE = HexColor("#333333")
|
||
SOFT = HexColor("#666666")
|
||
|
||
# 数据来自 e:\Work\Agent\立账凭证\采购单.pdf(apmr900 / Crystal Reports)
|
||
SAMPLE = {
|
||
"company_phone": "0512-63136988",
|
||
"company_fax": "",
|
||
"company_address": "江苏省苏州市吴江区经济开发区山湖西路789号",
|
||
"company_name": "苏州和信精密科技有限公司",
|
||
"maker": "梁春艳",
|
||
"page": "1/1",
|
||
"po_date": "2016/5/28",
|
||
"po_no": "5201-1605280003",
|
||
"po_type": "原料采购单",
|
||
"price_term": "A01 取核价单价",
|
||
"ship_address": "",
|
||
"pay_term": "月结一个月",
|
||
"tax_type": "进项(17%含税)",
|
||
"currency": "RMB",
|
||
"bill_address": "",
|
||
"vendor_name": "北川科技(苏州)有限公司",
|
||
"vendor_code": "VCCD00013",
|
||
"vendor_tel": "0512-62810369",
|
||
"vendor_fax": "0512-62810368",
|
||
"vendor_addr": "苏州市工业园区胜浦分区银胜路61号",
|
||
"untax_total": "4,455.50",
|
||
"tax_total": "5,212.94",
|
||
"lines": [
|
||
{
|
||
"item_no": "3450541801",
|
||
"seq": "1",
|
||
"qty": "730",
|
||
"unit": "PCS",
|
||
"arrive": "2016/6/16",
|
||
"untax_price": "3.350000",
|
||
"untax_amt": "2,445.50",
|
||
"halogen": "RoHS",
|
||
"name": "FRAME CUBE CORNER",
|
||
"spec": "H20*W36.5*L36.7",
|
||
"tax_price": "3.919500",
|
||
"tax_amt": "2,861.24",
|
||
"origin_no": "3301-1605260004",
|
||
"version": "-0001",
|
||
"vendor_item": "3450541801",
|
||
},
|
||
{
|
||
"item_no": "3450541801",
|
||
"seq": "2",
|
||
"qty": "600",
|
||
"unit": "PCS",
|
||
"arrive": "2016/6/16",
|
||
"untax_price": "3.350000",
|
||
"untax_amt": "2,010.00",
|
||
"halogen": "RoHS",
|
||
"name": "FRAME CUBE CORNER",
|
||
"spec": "H20*W36.5*L36.7",
|
||
"tax_price": "3.919500",
|
||
"tax_amt": "2,351.70",
|
||
"origin_no": "3301-1605100007",
|
||
"version": "-0001",
|
||
"vendor_item": "3450541801",
|
||
},
|
||
],
|
||
}
|
||
|
||
|
||
def _register_font() -> None:
|
||
"""注册宋体,接近鼎捷报表常用中文字形。"""
|
||
pdfmetrics.registerFont(TTFont(FONT, str(FONT_PATH), subfontIndex=0))
|
||
|
||
|
||
def _text(c: canvas.Canvas, x: float, y: float, text: str, size: float = 9, color=INK) -> None:
|
||
c.setFillColor(color)
|
||
c.setFont(FONT, size)
|
||
c.drawString(x, y, text)
|
||
|
||
|
||
def _right(c: canvas.Canvas, x: float, y: float, text: str, size: float = 8, color=INK) -> None:
|
||
"""右对齐文字,用于数量和金额。"""
|
||
c.setFillColor(color)
|
||
c.setFont(FONT, size)
|
||
c.drawRightString(x, y, text)
|
||
|
||
|
||
def _hline(c: canvas.Canvas, x1: float, x2: float, y: float, width: float = 0.4) -> None:
|
||
c.setStrokeColor(LINE)
|
||
c.setLineWidth(width)
|
||
c.line(x1, y, x2, y)
|
||
|
||
|
||
def _rect(c: canvas.Canvas, x: float, y: float, w: float, h: float, fill=None) -> None:
|
||
c.setStrokeColor(LINE)
|
||
c.setLineWidth(0.5)
|
||
if fill is not None:
|
||
c.setFillColor(fill)
|
||
c.rect(x, y, w, h, stroke=1, fill=1)
|
||
else:
|
||
c.rect(x, y, w, h, stroke=1, fill=0)
|
||
|
||
|
||
def build_pdf(out_path: Path) -> None:
|
||
"""
|
||
按 A4 竖版画出采购单样张。
|
||
核心逻辑:版心对齐原件 Crystal 报表的信息分区,不复制 Crystal 精确坐标。
|
||
"""
|
||
_register_font()
|
||
out_path.parent.mkdir(parents=True, exist_ok=True)
|
||
page_w, page_h = A4
|
||
c = canvas.Canvas(str(out_path), pagesize=A4)
|
||
c.setTitle("HXOPS 制式采购单样张")
|
||
c.setAuthor("HXOPS")
|
||
left = 16 * mm
|
||
right = page_w - 16 * mm
|
||
y = page_h - 10 * mm
|
||
|
||
# [STEP 1] Logo 在左;右侧公司名、「采购单」、地址、电话;制表者/页次单独下一行靠右。
|
||
logo_w = 52 * mm
|
||
logo_h = 14 * mm
|
||
logo_bottom = y - logo_h
|
||
if LOGO_PATH.exists():
|
||
c.drawImage(
|
||
str(LOGO_PATH),
|
||
left,
|
||
logo_bottom,
|
||
width=logo_w,
|
||
height=logo_h,
|
||
mask="auto",
|
||
preserveAspectRatio=True,
|
||
anchor="sw",
|
||
)
|
||
text_x = left + 56 * mm
|
||
block_mid = (text_x + right) / 2
|
||
c.setFont(FONT, 11)
|
||
c.setFillColor(INK)
|
||
c.drawCentredString(block_mid, y - 3.5 * mm, SAMPLE["company_name"])
|
||
c.setFont(FONT, 10)
|
||
c.drawCentredString(block_mid, y - 8.2 * mm, "采购单")
|
||
_text(c, text_x, y - 13 * mm, f"地址:{SAMPLE['company_address']}", 8)
|
||
_text(c, text_x, y - 17.5 * mm, f"公司电话:{SAMPLE['company_phone']} 公司传真:", 8)
|
||
_text(c, right - 52 * mm, y - 22.5 * mm, f"制表者: {SAMPLE['maker']}", 8)
|
||
c.setFont(FONT, 8)
|
||
c.drawRightString(right, y - 22.5 * mm, f"页次: {SAMPLE['page']}")
|
||
y = min(logo_bottom, y - 24 * mm) - 2 * mm
|
||
_hline(c, left, right, y, 0.8)
|
||
y -= 6 * mm
|
||
|
||
# [STEP 2] 原件左栏采购单号、右栏采购日期(与上一版左右对调)
|
||
col2 = left + 98 * mm
|
||
val1 = left + 22 * mm
|
||
val2 = col2 + 20 * mm
|
||
_text(c, left, y, "采购单号:")
|
||
_text(c, val1, y, f"{SAMPLE['po_no']} {SAMPLE['po_type']}")
|
||
_text(c, col2, y, "采购日期:")
|
||
_text(c, val2, y, SAMPLE["po_date"])
|
||
y -= 5.2 * mm
|
||
_text(c, left, y, "付款条件:")
|
||
_text(c, val1, y, SAMPLE["pay_term"])
|
||
_text(c, col2, y, "税 种:")
|
||
_text(c, val2, y, SAMPLE["tax_type"])
|
||
y -= 5.2 * mm
|
||
_text(c, left, y, "价格条件:")
|
||
_text(c, val1, y, SAMPLE["price_term"])
|
||
_text(c, col2, y, "币 种:")
|
||
_text(c, val2, y, SAMPLE["currency"])
|
||
y -= 5.2 * mm
|
||
_text(c, left, y, "送货地址:")
|
||
_text(c, val1, y, SAMPLE["ship_address"] or " ")
|
||
y -= 5.2 * mm
|
||
_text(c, left, y, "帐单地址:")
|
||
_text(c, val1, y, SAMPLE["bill_address"] or " ")
|
||
y -= 6 * mm
|
||
_text(c, left, y, "供应厂商:")
|
||
_text(c, val1, y, f"{SAMPLE['vendor_code']} {SAMPLE['vendor_name']}")
|
||
_text(c, left + 118 * mm, y, f"Tel: {SAMPLE['vendor_tel']}")
|
||
y -= 5 * mm
|
||
_text(c, val1, y, SAMPLE["vendor_addr"], 8)
|
||
_text(c, left + 118 * mm, y, f"Fax: {SAMPLE['vendor_fax']}")
|
||
y -= 4 * mm
|
||
_hline(c, left, right, y, 0.8)
|
||
y -= 1 * mm
|
||
|
||
# [STEP 4] 按 Crystal 原件:无满格单元格;表头三行折行;项次在最左。
|
||
x_seq = left
|
||
x_item = left + 8 * mm
|
||
x_origin = left + 50 * mm
|
||
x_arrive = left + 80 * mm
|
||
x_qty = left + 108 * mm
|
||
x_unit = left + 116 * mm
|
||
x_price = left + 142 * mm
|
||
x_amt = left + 164 * mm
|
||
x_halo = left + 168 * mm
|
||
header_h = 11 * mm
|
||
table_top = y
|
||
_hline(c, left, right, table_top, 0.7)
|
||
h1 = table_top - 3.6 * mm
|
||
h2 = table_top - 7.0 * mm
|
||
h3 = table_top - 10.2 * mm
|
||
_text(c, x_seq, h1, "项次", 7)
|
||
_text(c, x_item, h1, "料件编号", 7)
|
||
_text(c, x_item, h2, "品名/规格", 7)
|
||
_text(c, x_item, h3, "厂商料号额外品名规格说明", 6.5)
|
||
_text(c, x_origin, h1, "原始编号", 7)
|
||
_text(c, x_origin, h2, "版本", 7)
|
||
_text(c, x_origin, h3, "行序", 6.5)
|
||
_text(c, x_arrive, h1, "到货日", 7)
|
||
_text(c, x_qty - 8 * mm, h1, "数 量", 7)
|
||
_text(c, x_unit, h1, "单 位", 7)
|
||
_right(c, x_price, h1, "税前单价", 7)
|
||
_right(c, x_price, h2, "含税单价", 7)
|
||
_right(c, x_amt, h1, "税前金额", 7)
|
||
_right(c, x_amt, h2, "含税金额", 7)
|
||
_text(c, x_halo, h1, "卤素", 7)
|
||
_text(c, x_halo, h2, "管控", 7)
|
||
_text(c, x_halo + 10 * mm, h1, "环保", 7)
|
||
_text(c, x_halo + 10 * mm, h2, "等级", 7)
|
||
c.setStrokeColor(LINE)
|
||
c.setLineWidth(0.4)
|
||
c.line(x_price - 22 * mm, h2 - 1.2 * mm, x_price, h2 - 1.2 * mm)
|
||
c.line(x_amt - 22 * mm, h2 - 1.2 * mm, x_amt, h2 - 1.2 * mm)
|
||
y = table_top - header_h
|
||
_hline(c, left, right, y, 0.5)
|
||
|
||
# [STEP 5] 每笔三行:料号/品名/规格,无斑马底、无竖线格
|
||
row_h = 13 * mm
|
||
for line in SAMPLE["lines"]:
|
||
r1 = y - 3.8 * mm
|
||
r2 = y - 7.6 * mm
|
||
r3 = y - 11.2 * mm
|
||
_text(c, x_seq, r1, line["seq"], 8)
|
||
_text(c, x_item, r1, line["item_no"], 8)
|
||
_text(c, x_item, r2, line["name"], 8)
|
||
_text(c, x_item, r3, line["spec"], 8)
|
||
_text(c, x_origin, r1, line["vendor_item"], 8)
|
||
_text(c, x_origin, r2, line["origin_no"], 8)
|
||
_text(c, x_arrive, r1, line["arrive"], 8)
|
||
_right(c, x_qty, r1, line["qty"], 8)
|
||
_text(c, x_unit, r1, line["unit"], 8)
|
||
_right(c, x_price, r1, line["untax_price"], 8)
|
||
_right(c, x_price, r2, line["tax_price"], 8)
|
||
_right(c, x_amt, r1, line["untax_amt"], 8)
|
||
_right(c, x_amt, r2, line["tax_amt"], 8)
|
||
_text(c, x_halo, r1, line["halogen"], 8)
|
||
y -= row_h
|
||
|
||
# [STEP 6] 合计靠右,金额带原件那种细框
|
||
y -= 6 * mm
|
||
box_w = 22 * mm
|
||
box_h = 4.6 * mm
|
||
_text(c, x_price - 28 * mm, y, "税前金额总和:", 8)
|
||
_rect(c, x_amt - box_w, y - 1.2 * mm, box_w, box_h)
|
||
_right(c, x_amt - 1.2 * mm, y, SAMPLE["untax_total"], 8)
|
||
y -= 6 * mm
|
||
_text(c, x_price - 28 * mm, y, "含税金额总和:", 8)
|
||
_rect(c, x_amt - box_w, y - 1.2 * mm, box_w, box_h)
|
||
_right(c, x_amt - 1.2 * mm, y, SAMPLE["tax_total"], 8)
|
||
|
||
# 底部按 ERP 截图:三字在线上方均分;横线穿过 (apmr900) / (结束)
|
||
foot_y = 18 * mm
|
||
span = right - left
|
||
_text(c, left + 10 * mm, foot_y + 5 * mm, "核准", 10)
|
||
c.setFont(FONT, 10)
|
||
c.drawCentredString(left + span / 2, foot_y + 5 * mm, "审核")
|
||
c.drawRightString(right - 10 * mm, foot_y + 5 * mm, "经办")
|
||
_hline(c, left, right, foot_y, 0.7)
|
||
_text(c, left, foot_y - 1.1 * mm, "(apmr900)", 8)
|
||
c.setFont(FONT, 8)
|
||
c.drawRightString(right, foot_y - 1.1 * mm, "(结束)")
|
||
c.showPage()
|
||
c.save()
|
||
|
||
|
||
if __name__ == "__main__":
|
||
targets = [
|
||
Path(r"e:\Code\hxops\docs\features\accounting-voucher-check\采购单-hxops-sample.pdf"),
|
||
Path(r"e:\Work\Agent\立账凭证\采购单-hxops-sample.pdf"),
|
||
]
|
||
for path in targets:
|
||
try:
|
||
build_pdf(path)
|
||
print(path)
|
||
except PermissionError:
|
||
print(f"skip locked: {path}")
|