支持供应商简称维护和订单PDF下载命名
This commit is contained in:
+29
-1
@@ -1,6 +1,8 @@
|
||||
import hashlib
|
||||
import io
|
||||
import json
|
||||
import re
|
||||
import unicodedata
|
||||
from datetime import datetime
|
||||
|
||||
from flask import abort, flash, redirect, render_template, request, send_file, url_for
|
||||
@@ -24,6 +26,32 @@ def pdf_bytes(order):
|
||||
return buf.getvalue()
|
||||
|
||||
|
||||
def pdf_download_name(order):
|
||||
# Use current master-data aliases without regenerating an approved PDF.
|
||||
company = (order.supplier_name or '').strip()
|
||||
matches = db.query(Supplier.short_name, Supplier.tax_no).filter(Supplier.name == company).all()
|
||||
tax_no = (order.supplier_tax_no or '').strip()
|
||||
if tax_no:
|
||||
same_tax = [row for row in matches if (row.tax_no or '').strip() == tax_no]
|
||||
if same_tax:
|
||||
matches = same_tax
|
||||
aliases = {(row.short_name or '').strip() or company for row in matches}
|
||||
# Ambiguous duplicate names fall back to the company recorded on the order.
|
||||
supplier = next(iter(aliases)) if len(aliases) == 1 else company
|
||||
|
||||
def part(value, fallback, limit):
|
||||
value = ''.join(c for c in str(value or '') if not unicodedata.category(c).startswith('C'))
|
||||
value = re.sub(r'[<>:"/\\|?*]', '_', value)
|
||||
value = re.sub(r'\s+', ' ', value).strip(' .')
|
||||
# Windows counts UTF-16 units; retain Chinese and bound the full filename.
|
||||
value = value.encode('utf-16-le')[:limit * 2].decode('utf-16-le', errors='ignore').rstrip(' .')
|
||||
return value or fallback
|
||||
|
||||
return '%s_%s_%s.pdf' % (part(order.doc_type_text, '订单', 32),
|
||||
part(order.order_no, '未编号', 64),
|
||||
part(supplier, '未填写供应商', 80))
|
||||
|
||||
|
||||
def register_order_routes(app):
|
||||
@app.route('/orders/new', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
@@ -254,7 +282,7 @@ def register_order_routes(app):
|
||||
else:
|
||||
data = pdf_bytes(order)
|
||||
response = send_file(io.BytesIO(data), mimetype='application/pdf', as_attachment=request.args.get('dl') == '1',
|
||||
download_name='%s %s.pdf' % (order.doc_type_text, order.order_no))
|
||||
download_name=pdf_download_name(order))
|
||||
response.headers['Cache-Control'] = 'private, no-store'
|
||||
return response
|
||||
|
||||
|
||||
Reference in New Issue
Block a user