File size: 5,573 Bytes
3373b3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from docx import Document
from docx.shared import Inches
import io
import qrcode
from typing import Dict

def generate_contract(template_type: str, data: Dict) -> bytes:
    """生成合同文档"""
    doc = Document()

    if template_type == "采购合同":
        doc.add_heading('采购合同', 0)
        doc.add_paragraph(f'合同编号:{data.get("contract_no", "")}')
        doc.add_paragraph(f'甲方(采购方):{data.get("party_a", "")}')
        doc.add_paragraph(f'乙方(供应方):{data.get("party_b", "")}')
        doc.add_paragraph(f'签订日期:{data.get("date", "")}')
        doc.add_paragraph('')
        doc.add_paragraph(f'一、采购物品:{data.get("item", "")}')
        doc.add_paragraph(f'二、数量:{data.get("quantity", "")}')
        doc.add_paragraph(f'三、单价:{data.get("unit_price", "")}')
        doc.add_paragraph(f'四、总金额:{data.get("total", "")}')
        doc.add_paragraph(f'五、交货日期:{data.get("delivery_date", "")}')
        doc.add_paragraph('')
        doc.add_paragraph('甲方签字:________________     乙方签字:________________')

    elif template_type == "服务合同":
        doc.add_heading('服务合同', 0)
        doc.add_paragraph(f'合同编号:{data.get("contract_no", "")}')
        doc.add_paragraph(f'甲方(委托方):{data.get("party_a", "")}')
        doc.add_paragraph(f'乙方(服务方):{data.get("party_b", "")}')
        doc.add_paragraph(f'签订日期:{data.get("date", "")}')
        doc.add_paragraph('')
        doc.add_paragraph(f'一、服务内容:{data.get("service_content", "")}')
        doc.add_paragraph(f'二、服务期限:{data.get("service_period", "")}')
        doc.add_paragraph(f'三、服务费用:{data.get("fee", "")}')
        doc.add_paragraph(f'四、付款方式:{data.get("payment_method", "")}')
        doc.add_paragraph('')
        doc.add_paragraph('甲方签字:________________     乙方签字:________________')

    elif template_type == "劳动合同":
        doc.add_heading('劳动合同', 0)
        doc.add_paragraph(f'甲方(用人单位):{data.get("company", "")}')
        doc.add_paragraph(f'乙方(劳动者):{data.get("employee", "")}')
        doc.add_paragraph(f'身份证号:{data.get("id_card", "")}')
        doc.add_paragraph(f'签订日期:{data.get("date", "")}')
        doc.add_paragraph('')
        doc.add_paragraph(f'一、工作岗位:{data.get("position", "")}')
        doc.add_paragraph(f'二、工作地点:{data.get("location", "")}')
        doc.add_paragraph(f'三、合同期限:{data.get("contract_period", "")}')
        doc.add_paragraph(f'四、薪资待遇:{data.get("salary", "")}')
        doc.add_paragraph('')
        doc.add_paragraph('甲方盖章:________________     乙方签字:________________')

    buffer = io.BytesIO()
    doc.save(buffer)
    return buffer.getvalue()

def mail_merge(template_text: str, data_list: list) -> list:
    """批量邮件合并"""
    results = []
    for data in data_list:
        result = template_text
        for key, value in data.items():
            result = result.replace(f'{{{{{key}}}}}', str(value))
        results.append(result)
    return results

def amount_to_chinese(amount: float) -> str:
    """金额转换为中文大写"""
    digits = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']
    units = ['', '拾', '佰', '仟']
    big_units = ['', '万', '亿']

    if amount == 0:
        return '零元整'

    # 分离整数和小数部分
    amount = round(amount, 2)
    integer_part = int(amount)
    decimal_part = round((amount - integer_part) * 100)

    result = ''

    # 处理整数部分
    if integer_part > 0:
        str_int = str(integer_part)
        length = len(str_int)
        for i, char in enumerate(str_int):
            digit = int(char)
            pos = length - i - 1
            big_pos = pos // 4
            unit_pos = pos % 4

            if digit != 0:
                result += digits[digit] + units[unit_pos]
            else:
                if unit_pos == 0 and big_pos > 0:
                    result += big_units[big_pos]
                elif result and not result.endswith('零'):
                    result += '零'

            if unit_pos == 0 and big_pos > 0:
                result += big_units[big_pos]

        result = result.rstrip('零') + '元'

    # 处理小数部分
    if decimal_part > 0:
        jiao = decimal_part // 10
        fen = decimal_part % 10
        if jiao > 0:
            result += digits[jiao] + '角'
        if fen > 0:
            result += digits[fen] + '分'
    else:
        result += '整'

    return result

def generate_qrcode(data: str, size: int = 10) -> bytes:
    """生成二维码"""
    qr = qrcode.QRCode(version=1, box_size=size, border=2)
    qr.add_data(data)
    qr.make(fit=True)
    img = qr.make_image(fill_color="black", back_color="white")
    buffer = io.BytesIO()
    img.save(buffer, format='PNG')
    return buffer.getvalue()

def generate_barcode(data: str, barcode_type: str = "code128") -> bytes:
    """生成条形码"""
    try:
        import barcode
        from barcode.writer import ImageWriter

        code_class = barcode.get_barcode_class(barcode_type)
        code = code_class(data, writer=ImageWriter())
        buffer = io.BytesIO()
        code.write(buffer)
        return buffer.getvalue()
    except ImportError:
        # 如果没有python-barcode库,返回None
        return None