我正在研究一个Python脚本,该脚本会将所有现有的发票导入到Odoo 12实例。我正在使用odoorpc
库来简化对我的Odoo实例的RPC调用。
我实现了使用API创建发票的过程,并将其注册为“草稿”状态。然后,我要“验证”它,并将其状态更新为“打开”。不幸的是,使用Odoo UI中的“ Validate”按钮或从RPC API调用操作都可以做到这一点:发票状态更改为“已付款”。我不了解这种行为,因为我没有注册任何付款(这将是我的下一个目标)。
这是我的脚本的简化版本,可用于重现此问题:
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相同的方式创建发票。 有人可以帮我找出脚本中的问题吗?
答案 0 :(得分:0)
我找到了解决方案。看来account_id
的发票和发票行必须不同。为了解决此问题,我检索了ID为1的account.journal
实例(用于“客户发票”日记帐),然后根据日记帐的account_id
字段使用它来填充发票行的default_credit_account_id
:< / p>
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,
# [...]
})