我想创建一个PDF格式的设备信息唯一的发票。
给定输入
使用代码中的print(result)
从控制台输出复制:
ID: INV/2022/00003
Partner: [8, 'TEST Customer']
Payment term: [13, 'Siding']
Products: Check House
Siding equipment:
Category: Siding, Name: NEW TEST, Install date: 2022-12-22, Material: False, Brand: False, Style: False, Color: False, Shake siding: False
Window equipment:
Category: Window, Name: aw, Install date: 2022-12-23, Brand: Jeldwen, Window Material: Aluminum, Serial numbers: 2164
Door equipment:
Roof equipment:
Category: Roof, Name: NEW TEST, Install date: 2022-12-23, Roof Brand: False, Roof Style: False, Roof Color: False, Impact Resistant: False
ID: INV/2022/00002
Partner: [3, 'keith.mikel@e5sol.com']
Payment term: False
Products: NEW House
Siding equipment:
Category: Siding, Name: Sing, Install date: 2022-12-23, Material: Vinyl, Brand: False, Style: False, Color: False, Shake siding: False
Category: Siding, Name: as, Install date: 2022-12-23, Material: False, Brand: False, Style: False, Color: False, Shake siding: False
Category: Siding, Name: sa, Install date: 2022-12-23, Material: False, Brand: False, Style: False, Color: False, Shake siding: False
Window equipment:
Door equipment:
Roof equipment:
Category: Roof, Name: test, Install date: 2022-12-23, Roof Brand: GAF / Certainteed, Roof Style: Architectural, Roof Color: Moire Black, Impact Resistant: Moire Black
Process finished with exit code 0
代码
创建的PDF应提取与给定产品/发票相关的设备数据:
from reportlab.pdfgen import canvas
for ID, data in results.items():
# Create a canvas object for the current invoice
c = canvas.Canvas(f"{data['products']}.pdf")
# Set the font and font size
c.setFont("Helvetica", 16)
c.drawString(10, 800, f"Housing Maintenance Report")
# Write the ID to the PDF
c.drawString(10, 780, f"Invoice ID: {ID}")
# Write the partner information to the PDF
c.drawString(10, 760, f"Customer: {data['partner']}")
# Write the payment term to the PDF
c.drawString(10, 740, f"Report Type: {data['payment_term']}")
# Write the heading for the products section to the PDF
c.drawString(10, 720, "House Address:")
# Write the list of products to the PDF
c.drawString(25, 700, ", ".join(data['products']))
# Write the heading for the equipment section to the PDF
c.drawString(10, 680, "Equipment:")
# Set the starting line number
line_number = 660
# Write the siding equipment information to the PDF
if siding_equipment:
c.drawString(25, 660, "Siding equipment:")
for equipment in siding_equipment:
# Decrement the line number by 20 after each piece of equipment
line_number -= 20
c.drawString(50, line_number, f"Category: {equipment[0][1]}")
line_number -= 20
c.drawString(75, line_number, f"Name: {equipment[1]}")
line_number -= 20
c.drawString(75, line_number, f"Install date: {equipment[2]}")
line_number -= 20
c.drawString(75, line_number, f"Material: {equipment[3]}")
line_number -= 20
c.drawString(75, line_number, f"Brand: {equipment[4]}")
line_number -= 20
c.drawString(75, line_number, f"Style: {equipment[5]}")
line_number -= 20
c.drawString(75, line_number, f"Color: {equipment[6]}")
line_number -= 20
c.drawString(75, line_number, f"Shake siding: {equipment[7]}")
line_number -= 20
# Write the siding equipment information to the PDF
if roof_equipment:
c.drawString(25, line_number, "Roof equipment:")
for equipment in roof_equipment:
# Decrement the line number by 20 after each piece of equipment
line_number -= 20
c.drawString(50, line_number, f"Category: {equipment[0][1]}")
line_number -= 20
c.drawString(75, line_number, f"Name: {equipment[1]}")
line_number -= 20
c.drawString(75, line_number, f"Install date: {equipment[2]}")
line_number -= 20
c.drawString(75, line_number, f"Brand: {equipment[3]}")
line_number -= 20
c.drawString(75, line_number, f"Style: {equipment[4]}")
line_number -= 20
c.drawString(75, line_number, f"Color: {equipment[5]}")
line_number -= 20
c.drawString(75, line_number, f"Impact Resistant: {equipment[6]}")
line_number -= 20
c.save()
问题
现在,当创建PDF时,设备条目将在所有创建的PDF中复制。
示例:
- 正在输入2个项目
NEW house
和Test House
。 - 创建2份PDF文档。
- 两个文档顶部的标题数据都是正确的
- 但是,壁板设备和屋顶设备的设备数据在两个文档中重复
我不能让它创建pdf与设备是唯一的,为他们每个人。
问题
1.这和压痕有关吗?
1.需要改变什么?
获取未付发票的代码
Outstanding = models.execute_kw(db, uid, password, 'account.move', 'search_read', [[['payment_state', '=', 'paid'], ['x_studio_report_sent', '=', False]]],
{'fields': ['name', 'partner_id', 'invoice_payment_term_id','invoice_line_ids']})
results = {}
for invoice in Outstanding:
ID = invoice['name']
partner = invoice['partner_id']
payment_term = invoice['invoice_payment_term_id']
invoice_lines = invoice['invoice_line_ids']
line_items = models.execute_kw(db, uid, password, 'account.move.line', 'search', [[['id', 'in', invoice_lines]]])
line_item_details = models.execute_kw(db, uid, password, 'account.move.line', 'read', [line_items], {'fields': ['product_id']})
products = []
for line_item in line_item_details:
product_id = line_item['product_id']
products.append(product_id[1])
results[ID] = {'partner': partner, 'payment_term': payment_term, 'products': products}
分配给发票上使用的产品的设备的代码
for ID, data in results.items():
print(f'ID: {ID}')
print(f'Partner: {data["partner"]}')
print(f'Payment term: {data["payment_term"]}')
print(f'Products: {", ".join(data["products"])}')
models = xmlrpc.client.ServerProxy('{}/xmlrpc/2/object'.format(url))
models.execute_kw(db, uid, password, 'maintenance.equipment', 'check_access_rights', ['read'],
{'raise_exception': False})
product_names = data['products']
# Execute the search_read method to fetch the product templates
Maintenance = models.execute_kw(db, uid, password, 'maintenance.equipment', 'search_read',
[[['x_studio_many2one_field_0NvLy', '=', product_names]]],
{'fields': ['category_id', 'name', 'assign_date',
'x_studio_siding_material', 'x_studio_siding_brand',
'x_studio_siding_style', 'x_studio_siding_color',
'x_studio_shake_siding',
'x_studio_window_serial_numbers', 'x_studio_selection_field_ZfZO8',
'x_studio_window_brand',
'x_studio_door_brand', 'x_studio_door_code',
'x_studio_roof_brand_1', 'x_studio_roof_style', 'x_studio_roof_color',
'x_studio_impact_resistant']})
window_equipment = []
siding_equipment = []
door_equipment = []
roof_equipment = []
siding_equipment = [
[equipment['category_id'], equipment['name'], equipment['assign_date'], equipment['x_studio_siding_material'],
equipment['x_studio_siding_brand'], equipment['x_studio_siding_style'], equipment['x_studio_siding_color'],
equipment['x_studio_shake_siding']] for equipment in Maintenance if equipment['category_id'] == [2, 'Siding']]
window_equipment = [
[equipment['category_id'], equipment['name'], equipment['assign_date'], equipment['x_studio_window_brand'],
equipment['x_studio_selection_field_ZfZO8'], equipment['x_studio_window_serial_numbers']] for equipment in
Maintenance if equipment['category_id'] == [3, 'Window']]
door_equipment = [
[equipment['category_id'], equipment['name'], equipment['assign_date'], equipment['x_studio_door_brand'],
equipment['x_studio_door_code']] for equipment in Maintenance if equipment['category_id'] == [4, 'Door']]
roof_equipment = [
[equipment['category_id'], equipment['name'], equipment['assign_date'], equipment['x_studio_roof_brand_1'],
equipment['x_studio_roof_style'], equipment['x_studio_roof_color'], equipment['x_studio_impact_resistant']] for
equipment in Maintenance if equipment['category_id'] == [1, 'Roof']]
# Siding equipment
print('Siding equipment:')
for entry in siding_equipment:
print(
f'Category: {entry[0][1]}, Name: {entry[1]}, Install date: {entry[2]}, Material: {entry[3]}, Brand: {entry[4]}, Style: {entry[5]}, Color: {entry[6]}, Shake siding: {entry[7]}')
print()
# Window equipment
print('Window equipment:')
for entry in window_equipment:
print(
f'Category: {entry[0][1]}, Name: {entry[1]}, Install date: {entry[2]}, Brand: {entry[3]}, Window Material: {entry[4]}, Serial numbers: {entry[5]}')
print()
# Door equipment
print('Door equipment:')
for entry in door_equipment:
print(
f'Category: {entry[0][1]}, Name: {entry[1]}, Install date: {entry[2]}, Door Brand: {entry[3]}, Door Code: {entry[3]}')
print()
# Roof equipment
print('Roof equipment:')
for entry in roof_equipment:
print(
f'Category: {entry[0][1]}, Name: {entry[1]}, Install date: {entry[2]}, Roof Brand: {entry[3]}, Roof Style: {entry[4]}, Roof Color: {entry[5]}, Impact Resistant: {entry[5]}')
print()
2条答案
按热度按时间v1l68za41#
通过重构提高可读性
很难看清到底发生了什么,因为行太多了,尤其是循环体很长。
我们可以通过重构代码来折叠或分组一些逻辑,为了减少循环体中的代码行,可以使用一种名为extract-method或extract-function的重构技术。
提取的函数:
write_siding_equipment(c, line_number, equipment)
write_roof_equipment(c, line_number, equipment)
这样代码就更容易阅读和维护了。
问题可见
现在您可以看到:
c
上背对背打印。line_number
不会重置,但会增加。roof_equipment
的嵌套循环内调用用于保存PDF的最终c.save
,这可能导致PDF重复bfnvny8b2#
我应该为这份工作付多少钱呢,花3个小时在Python的bug修复和软件开发上--考虑到一个ERP顾问的日常费用很容易就超过1000美元。
差不多170行代码之后
数据准备
定义数据检索和准备的方法:
准备发票和调试打印:
报告创建
定义方法:
使用方法:
由于StackOverflow不是代码编写服务,因此我留给您一些东西:
c.save()
.