如何获取发票付款金额自定义报表

2024-04-28 00:13:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在发票模块中创建自定义报告

<span class="text-nowrap" t-esc="doc.invoice_payments_widget" />

我添加了上面的代码,它返回jSON格式。但是我只需要得到数量

{"title": "Less Payment", "outstanding": false, "content": [{"name": "Customer Payment: INV/2021/0006", "journal_name": "Bank", "amount": 500.0, "currency": "$", "digits": [69, 2], "position": "before", "date": "2021-03-26", "payment_id": 25, "account_payment_id": 2, "payment_method_name": "Manual", "move_id": 11, "ref": "BNK1/2021/0002 (INV/2021/0006)"}]}

Tags: 模块textnameiddoc报告发票invoice
2条回答

如果返回json,首先需要使用json库将其转换为Python结构:

import json

data = json.loads('{"title": "Less Payment", "outstanding": false, "content": [{"name": "Customer Payment: INV/2021/0006", "journal_name": "Bank", "amount": 500.0, "currency": "$", "digits": [69, 2], "position": "before", "date": "2021-03-26", "payment_id": 25, "account_payment_id": 2, "payment_method_name": "Manual", "move_id": 11, "ref": "BNK1/2021/0002 (INV/2021/0006)"}]}')

从那里你可以使用标准代码,比如CZoellner。如果您不理解这个答案,可以查找Python的“列表理解”

可能有多笔付款,因此您应该汇总content字典的所有金额

sum([content.get('amount', 0.0)
    for content in doc.invoice_payments_widget.get('content', [])])

我会为它编写一个方法,将它绑定到报告并调用它。它也会更干净

相关问题 更多 >