Odoo API:发票在验证后具有 "paid" 状态

Odoo API: invoice has "paid" status after validation

我正在编写一个 Python 脚本,它将我所有现有的发票导入到 Odoo 12 实例中。我正在使用 odoorpc 库来简化对我的 Odoo 实例的 RPC 调用。

我实现了使用 API 创建发票,并在 "draft" 状态下注册。然后,我想 "validate" 它并将其状态更新为 "open"。不幸的是,使用 Odoo UI 中的 "Validate" 按钮或从 RPC API 调用操作执行相同的操作:发票状态更改为 "paid"。我不理解这种行为,因为我没有注册任何付款(这将是我的下一个目标)。

这是我的脚本的简化版本,可用于重现该问题:

import odoorpc

import settings
"""settings module contains various constants used
to connect with Odoo on my VPS"""


if __name__ == "__main__":
    odoo = odoorpc.ODOO(settings.ODOO_HOST, port=settings.ODOO_PORT, timeout=10)
    odoo.login(settings.ODOO_DB, settings.ODOO_USER, settings.ODOO_PASSWORD)

    Partner = odoo.env["res.partner"]
    # This partner already exists in DB
    customer = Partner.browse([22])

    Invoice = odoo.env["account.invoice"]
    invoice_id = Invoice.create({
        'partner_id' : customer.id,
        'state': 'draft',
        # This is ID for "Customers Invoices" journal
        'journal_id': 1,
        'account_id': customer.property_account_receivable_id.id,
        # This is ID for default bank account, already registered
        'partner_bank_id': 1,
        'payment_term_id': odoo.env.ref("account.account_payment_term_net").id,
    })

    InvoiceLine = odoo.env["account.invoice.line"]
    InvoiceLine.create({
        "invoice_id": invoice_id,
        "name": "A basic product",
        "quantity": 6,
        "price_unit": 100.0,
        # Not sure about this one:
        "uom_id": 1,
        # No tax
        "invoice_line_tax_ids": [],
        'journal_id': 1,
        'account_id': customer.property_account_receivable_id.id,
    })

    inv = Invoice.browse([invoice_id])
    print("Before validating:", inv.state)

    inv.action_invoice_open()

    inv = Invoice.browse([invoice_id])
    print("After validating:", inv.state)

结果:

Before validating: draft
After validating: paid

我认为我的发票创建过程中存在某些遗漏或错误,但我没有找到确切应该修改的内容,以便按照与 UI 相同的方式创建发票。 谁能帮我找出脚本中的问题?

我找到了解决方案。看来发票和发票行的 account_id 必须不同。为解决此问题,我检索了 ID 为 1 的 account.journal 实例(针对 "Customers Invoices" 日记帐),然后使用它根据日记帐的 default_credit_account_id 字段填充发票行的 account_id

cust_invoices_journal = odoo.env["account.journal"].browse([1])
# [...]
invoice_id = Invoice.create({
    # [...]
    'journal_id': cust_invoices_journal.id,
    'account_id': customer.property_account_receivable_id.id,
    # [...]
})
# [...]
InvoiceLine.create({
    # [...]
    'account_id': cust_invoices_journal.default_credit_account_id.id,
    # [...]
})