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

How to get Invoice Payment amount - Custom Report

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

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

我添加了上面的代码并且它是 returns 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)"}]}

可能有不止一笔付款,因此您应该将 content 字典中的所有金额相加。

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

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

如果返回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)"}]}')

然后您可以使用 建议的标准代码。如果您不理解该答案,可以查找 Python.

的“列表理解”