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

问题描述

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

<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)"}]}

解决方法

可能会有不止一笔付款,因此您应该汇总 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)"}]}')

然后您可以使用标准代码,例如建议的 CZoellner。如果您不明白该答案,您可以查找 Python 的“列表理解”。